diff --git a/packages/helia/package.json b/packages/helia/package.json index 64d56532..0f535c8b 100644 --- a/packages/helia/package.json +++ b/packages/helia/package.json @@ -78,12 +78,13 @@ "@libp2p/webrtc": "^4.0.20", "@libp2p/websockets": "^8.0.16", "@libp2p/webtransport": "^4.0.20", + "@multiformats/dns": "^1.0.1", "blockstore-core": "^4.4.0", "datastore-core": "^9.2.9", "interface-blockstore": "^5.2.10", "interface-datastore": "^8.2.11", "ipns": "^9.0.0", - "libp2p": "^1.2.4", + "libp2p": "^1.3.0", "multiformats": "^13.1.0" }, "devDependencies": { diff --git a/packages/helia/src/index.ts b/packages/helia/src/index.ts index e0377960..cb4ed8b7 100644 --- a/packages/helia/src/index.ts +++ b/packages/helia/src/index.ts @@ -100,6 +100,7 @@ export async function createHelia (init: Partial = {}): Promise({ ...init, libp2p: { + dns: init.dns, ...init.libp2p, // ignore the libp2p start parameter as it should be on the main init diff --git a/packages/helia/src/utils/libp2p-defaults.browser.ts b/packages/helia/src/utils/libp2p-defaults.browser.ts index cf7cfe57..0e9b3f2e 100644 --- a/packages/helia/src/utils/libp2p-defaults.browser.ts +++ b/packages/helia/src/utils/libp2p-defaults.browser.ts @@ -34,6 +34,7 @@ export interface DefaultLibp2pServices extends Record { export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOptions { return { peerId: options.peerId, + dns: options.dns, addresses: { listen: [ '/webrtc' diff --git a/packages/helia/src/utils/libp2p-defaults.ts b/packages/helia/src/utils/libp2p-defaults.ts index 3f4de600..83bb03b3 100644 --- a/packages/helia/src/utils/libp2p-defaults.ts +++ b/packages/helia/src/utils/libp2p-defaults.ts @@ -38,6 +38,7 @@ export interface DefaultLibp2pServices extends Record { export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOptions { return { peerId: options.peerId, + dns: options.dns, addresses: { listen: [ '/ip4/0.0.0.0/tcp/0', diff --git a/packages/helia/src/utils/libp2p.ts b/packages/helia/src/utils/libp2p.ts index 64a3b56e..f5597e08 100644 --- a/packages/helia/src/utils/libp2p.ts +++ b/packages/helia/src/utils/libp2p.ts @@ -6,6 +6,7 @@ import { libp2pDefaults } from './libp2p-defaults.js' import type { DefaultLibp2pServices } from './libp2p-defaults.js' import type { ComponentLogger, Libp2p, PeerId } 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' @@ -20,6 +21,7 @@ export interface CreateLibp2pOptions> { export interface Libp2pDefaultsOptions { peerId?: PeerId keychain?: KeychainInit + dns?: DNS } export async function createLibp2p = DefaultLibp2pServices> (options: CreateLibp2pOptions): Promise> { diff --git a/packages/interface/package.json b/packages/interface/package.json index 6a3fee99..b5460981 100644 --- a/packages/interface/package.json +++ b/packages/interface/package.json @@ -71,6 +71,7 @@ }, "dependencies": { "@libp2p/interface": "^1.1.4", + "@multiformats/dns": "^1.0.1", "interface-blockstore": "^5.2.10", "interface-datastore": "^8.2.11", "interface-store": "^5.1.8", diff --git a/packages/interface/src/index.ts b/packages/interface/src/index.ts index a12024cc..303dcea1 100644 --- a/packages/interface/src/index.ts +++ b/packages/interface/src/index.ts @@ -18,6 +18,7 @@ import type { Blocks } from './blocks.js' import type { Pins } from './pins.js' import type { Routing } from './routing.js' import type { AbortOptions, ComponentLogger } from '@libp2p/interface' +import type { DNS } from '@multiformats/dns' import type { Datastore } from 'interface-datastore' import type { MultihashHasher } from 'multiformats' import type { CID } from 'multiformats/cid' @@ -67,6 +68,12 @@ export interface Helia { */ hashers: Record + /** + * The DNS property can be used to perform lookups of various record types and + * will use a resolver appropriate to the current platform. + */ + dns: DNS + /** * Starts the Helia node */ diff --git a/packages/utils/package.json b/packages/utils/package.json index 58627f2f..817d042c 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -61,6 +61,7 @@ "@libp2p/logger": "^4.0.7", "@libp2p/peer-collections": "^5.1.7", "@libp2p/utils": "^5.2.6", + "@multiformats/dns": "^1.0.1", "any-signal": "^4.1.1", "blockstore-core": "^4.4.0", "cborg": "^4.0.9", diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 7890f879..ef618e7b 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -19,6 +19,7 @@ import { contentRoutingSymbol, peerRoutingSymbol, start, stop } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' +import { dns } from '@multiformats/dns' import drain from 'it-drain' import { CustomProgressEvent } from 'progress-events' import { PinsImpl } from './pins.js' @@ -32,6 +33,7 @@ import type { DAGWalker, GCOptions, Helia as HeliaInterface, Routing } from '@he import type { BlockBroker } from '@helia/interface/blocks' import type { Pins } from '@helia/interface/pins' import type { ComponentLogger, Logger } from '@libp2p/interface' +import type { DNS } from '@multiformats/dns' import type { Blockstore } from 'interface-blockstore' import type { Datastore } from 'interface-datastore' import type { CID } from 'multiformats/cid' @@ -103,6 +105,11 @@ export interface HeliaInit { * Components used by subclasses */ components?: Record + + /** + * An optional DNS implementation used to perform queries for DNS records. + */ + dns?: DNS } interface Components { @@ -112,6 +119,7 @@ interface Components { dagWalkers: Record logger: ComponentLogger blockBrokers: BlockBroker[] + dns: DNS } export class Helia implements HeliaInterface { @@ -122,6 +130,7 @@ export class Helia implements HeliaInterface { public routing: Routing public dagWalkers: Record public hashers: Record + public dns: DNS private readonly log: Logger constructor (init: HeliaInit) { @@ -129,6 +138,7 @@ export class Helia implements HeliaInterface { this.log = this.logger.forComponent('helia') this.hashers = defaultHashers(init.hashers) this.dagWalkers = defaultDagWalkers(init.dagWalkers) + this.dns = init.dns ?? dns() const components: Components = { blockstore: init.blockstore, @@ -137,6 +147,7 @@ export class Helia implements HeliaInterface { dagWalkers: this.dagWalkers, logger: this.logger, blockBrokers: [], + dns: this.dns, ...(init.components ?? {}) }