Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect trustless gateway options for sessions #566

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/block-brokers/src/trustless-gateway/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class TrustlessGatewayBlockBroker implements BlockBroker<TrustlessGateway
return createTrustlessGatewaySession({
logger: this.logger,
routing: this.routing
}, options)
}, {
...options,
allowLocal: this.allowLocal,
allowInsecure: this.allowInsecure
})
}
}
30 changes: 30 additions & 0 deletions packages/block-brokers/test/trustless-gateway-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { uriToMultiaddr } from '@multiformats/uri-to-multiaddr'
import { expect } from 'aegir/chai'
import { filterNonHTTPMultiaddrs } from '../src/trustless-gateway/utils.js'

describe('trustless-gateway-block-broker-utils', () => {
it('filterNonHTTPMultiaddrs respects allowInsecure multiaddrs correctly', async function () {
const nonSecureMaddr = uriToMultiaddr('http://mygw.com')
const secureMaddr = uriToMultiaddr('https://mygw.com')

const filtered = filterNonHTTPMultiaddrs([nonSecureMaddr, secureMaddr], true, true)

expect(filtered.length).to.deep.equal(2)
})

it('filterNonHTTPMultiaddrs filters local multiaddrs correctly', async function () {
const localMaddr = uriToMultiaddr('http://localhost')

const filtered = filterNonHTTPMultiaddrs([localMaddr], true, true)

expect(filtered.length).to.deep.equal(1)
})

it('filterNonHTTPMultiaddrs filters multiaddrs correctly', async function () {
const localMaddr = uriToMultiaddr('http://localhost')

const filtered = filterNonHTTPMultiaddrs([localMaddr], false, false)

expect(filtered.length).to.deep.equal(0)
})
})
3 changes: 2 additions & 1 deletion packages/helia/src/utils/libp2p-defaults.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import * as libp2pInfo from 'libp2p/version'
import { name, version } from '../version.js'
import { bootstrapConfig } from './bootstrappers.js'
import type { Libp2pDefaultsOptions } from './libp2p.js'
import type { ServiceMap } from '@libp2p/interface'
import type { Libp2pOptions } from 'libp2p'

export interface DefaultLibp2pServices extends Record<string, unknown> {
export interface DefaultLibp2pServices extends ServiceMap {
2color marked this conversation as resolved.
Show resolved Hide resolved
autoNAT: unknown
dcutr: unknown
delegatedRouting: unknown
Expand Down
3 changes: 2 additions & 1 deletion packages/helia/src/utils/libp2p-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import * as libp2pInfo from 'libp2p/version'
import { name, version } from '../version.js'
import { bootstrapConfig } from './bootstrappers.js'
import type { Libp2pDefaultsOptions } from './libp2p.js'
import type { ServiceMap } from '@libp2p/interface'
import type { Libp2pOptions } from 'libp2p'

export interface DefaultLibp2pServices extends Record<string, unknown> {
export interface DefaultLibp2pServices extends ServiceMap {
2color marked this conversation as resolved.
Show resolved Hide resolved
autoNAT: unknown
dcutr: unknown
delegatedRouting: unknown
Expand Down
6 changes: 3 additions & 3 deletions packages/helia/src/utils/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { Key } from 'interface-datastore'
import { createLibp2p as create } from 'libp2p'
import { libp2pDefaults } from './libp2p-defaults.js'
import type { DefaultLibp2pServices } from './libp2p-defaults.js'
import type { ComponentLogger, Libp2p, PeerId } from '@libp2p/interface'
import type { ComponentLogger, Libp2p, PeerId, ServiceMap } from '@libp2p/interface'
import type { Keychain, KeychainInit } from '@libp2p/keychain'
import type { DNS } from '@multiformats/dns'
import type { Datastore } from 'interface-datastore'
import type { Libp2pOptions } from 'libp2p'

export interface CreateLibp2pOptions<T extends Record<string, unknown>> {
export interface CreateLibp2pOptions<T extends ServiceMap> {
datastore: Datastore
libp2p?: Libp2pOptions<T>
logger?: ComponentLogger
Expand All @@ -24,7 +24,7 @@ export interface Libp2pDefaultsOptions {
dns?: DNS
}

export async function createLibp2p <T extends Record<string, unknown> = DefaultLibp2pServices> (options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
export async function createLibp2p <T extends ServiceMap = DefaultLibp2pServices> (options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
const peerId = options.libp2p?.peerId
const logger = options.logger ?? defaultLogger()
const selfKey = new Key('/pkcs8/self')
Expand Down
Loading