From d197f554d7cdadb3b05ed2d6c69fda2c4362b1eb Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Sun, 27 Feb 2022 15:05:30 +0000 Subject: [PATCH] fix: rename crypto to connection-encrypter (#179) * Renames interfaces/crypto to interfaces/connection-encrypter because that's what it does and we already have @libp2p/crypto * Rename *Options to *Init as it's more idiomatic * `Topology` class isn't exported any more, instead use `createTopology` to get an instance of the interface --- packages/libp2p-connection/package.json | 6 +-- packages/libp2p-connection/src/index.ts | 6 +-- .../package.json | 18 ++++----- .../{crypto => connection-encrypter}/index.ts | 10 ++--- .../utils/index.ts | 0 .../src/mocks/index.ts | 2 +- .../src/mocks/muxer.ts | 16 ++++---- .../src/mocks/upgrader.ts | 8 ++-- .../src/stream-muxer/base-test.ts | 4 +- .../src/stream-muxer/close-test.ts | 4 +- .../src/stream-muxer/index.ts | 4 +- .../src/stream-muxer/mega-stress-test.ts | 6 +-- .../src/stream-muxer/spawner.ts | 4 +- .../src/stream-muxer/stress-test.ts | 6 +-- .../index.spec.ts | 2 +- .../mock-crypto.ts | 6 +-- .../test/mocks/muxer.ts | 4 +- .../test/peer-discovery/mock-discovery.ts | 8 ++-- packages/libp2p-interfaces/package.json | 16 ++++---- .../README.md | 8 ++-- .../errors.ts | 0 .../{crypto => connection-encrypter}/index.ts | 8 +++- .../src/content-routing/index.ts | 4 +- .../src/peer-discovery/index.ts | 4 +- .../src/peer-routing/index.ts | 13 ++++--- .../src/stream-muxer/index.ts | 6 +-- .../libp2p-interfaces/src/topology/README.md | 33 +++++----------- .../libp2p-interfaces/src/topology/index.ts | 8 +++- .../libp2p-interfaces/src/transport/README.md | 4 +- packages/libp2p-logger/package.json | 2 +- .../libp2p-multistream-select/package.json | 4 +- packages/libp2p-peer-id-factory/package.json | 4 +- packages/libp2p-peer-id/package.json | 2 +- packages/libp2p-peer-id/src/index.ts | 38 +++++++++---------- packages/libp2p-peer-map/package.json | 6 +-- packages/libp2p-peer-record/package.json | 10 ++--- .../libp2p-peer-record/src/envelope/index.ts | 6 +-- .../src/peer-record/index.ts | 6 +-- packages/libp2p-peer-store/package.json | 12 +++--- packages/libp2p-peer-store/src/index.ts | 10 ++--- packages/libp2p-pubsub/package.json | 12 +++--- packages/libp2p-pubsub/src/index.ts | 4 +- packages/libp2p-pubsub/src/peer-streams.ts | 8 ++-- packages/libp2p-topology/README.md | 6 +-- packages/libp2p-topology/package.json | 4 +- packages/libp2p-topology/src/index.ts | 31 +++++++-------- packages/libp2p-tracked-map/package.json | 2 +- packages/libp2p-tracked-map/src/index.ts | 6 +-- 48 files changed, 191 insertions(+), 200 deletions(-) rename packages/libp2p-interface-compliance-tests/src/{crypto => connection-encrypter}/index.ts (91%) rename packages/libp2p-interface-compliance-tests/src/{crypto => connection-encrypter}/utils/index.ts (100%) rename packages/libp2p-interface-compliance-tests/test/{crypto => connection-encrypter}/index.spec.ts (74%) rename packages/libp2p-interface-compliance-tests/test/{crypto => connection-encrypter}/mock-crypto.ts (94%) rename packages/libp2p-interfaces/src/{crypto => connection-encrypter}/README.md (94%) rename packages/libp2p-interfaces/src/{crypto => connection-encrypter}/errors.ts (100%) rename packages/libp2p-interfaces/src/{crypto => connection-encrypter}/index.ts (76%) diff --git a/packages/libp2p-connection/package.json b/packages/libp2p-connection/package.json index 3d621c6b7..55d8e99a9 100644 --- a/packages/libp2p-connection/package.json +++ b/packages/libp2p-connection/package.json @@ -154,13 +154,13 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6", + "@libp2p/interfaces": "^1.3.12", "@multiformats/multiaddr": "^10.1.5", "err-code": "^3.0.1" }, "devDependencies": { - "@libp2p/interface-compliance-tests": "^1.1.2", - "@libp2p/peer-id-factory": "^1.0.5", + "@libp2p/interface-compliance-tests": "^1.1.14", + "@libp2p/peer-id-factory": "^1.0.6", "aegir": "^36.1.3", "it-pair": "^2.0.2" } diff --git a/packages/libp2p-connection/src/index.ts b/packages/libp2p-connection/src/index.ts index d23f7b289..f1b0c6cd5 100644 --- a/packages/libp2p-connection/src/index.ts +++ b/packages/libp2p-connection/src/index.ts @@ -6,7 +6,7 @@ import type { PeerId } from '@libp2p/interfaces/peer-id' const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection') -interface ConnectionOptions { +interface ConnectionInit { localAddr: Multiaddr remoteAddr: Multiaddr localPeer: PeerId @@ -74,8 +74,8 @@ export class Connection { * An implementation of the js-libp2p connection. * Any libp2p transport should use an upgrader to return this connection. */ - constructor (options: ConnectionOptions) { - const { localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat } = options + constructor (init: ConnectionInit) { + const { localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat } = init this.id = `${(parseInt(String(Math.random() * 1e9))).toString(36)}${Date.now()}` this.localAddr = localAddr diff --git a/packages/libp2p-interface-compliance-tests/package.json b/packages/libp2p-interface-compliance-tests/package.json index 535266501..537015e89 100644 --- a/packages/libp2p-interface-compliance-tests/package.json +++ b/packages/libp2p-interface-compliance-tests/package.json @@ -52,9 +52,9 @@ "import": "./dist/src/connection/index.js", "types": "./dist/src/connection/index.d.ts" }, - "./crypto": { - "import": "./dist/src/crypto/index.js", - "types": "./dist/src/crypto/index.d.ts" + "./connection-encrypter": { + "import": "./dist/src/connection-encrypter/index.js", + "types": "./dist/src/connection-encrypter/index.d.ts" }, "./mocks": { "import": "./dist/src/mocks/index.js", @@ -199,12 +199,12 @@ }, "dependencies": { "@libp2p/crypto": "^0.22.7", - "@libp2p/interfaces": "^1.3.6", - "@libp2p/logger": "^1.0.3", - "@libp2p/multistream-select": "^1.0.0", - "@libp2p/peer-id": "^1.0.4", - "@libp2p/peer-id-factory": "^1.0.0", - "@libp2p/pubsub": "^1.1.0", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/logger": "^1.1.0", + "@libp2p/multistream-select": "^1.0.1", + "@libp2p/peer-id": "^1.1.6", + "@libp2p/peer-id-factory": "^1.0.6", + "@libp2p/pubsub": "^1.2.8", "@multiformats/multiaddr": "^10.1.5", "abortable-iterator": "^4.0.2", "aegir": "^36.1.3", diff --git a/packages/libp2p-interface-compliance-tests/src/crypto/index.ts b/packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts similarity index 91% rename from packages/libp2p-interface-compliance-tests/src/crypto/index.ts rename to packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts index bcd26812c..07bab68b3 100644 --- a/packages/libp2p-interface-compliance-tests/src/crypto/index.ts +++ b/packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts @@ -4,16 +4,16 @@ import * as PeerIdFactory from '@libp2p/peer-id-factory' import all from 'it-all' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import peers from '../utils/peers.js' -import { UnexpectedPeerError } from '@libp2p/interfaces/crypto/errors' +import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors' import { createMaConnPair } from './utils/index.js' import type { TestSetup } from '../index.js' -import type { Crypto } from '@libp2p/interfaces/crypto' +import type { Encrypter } from '@libp2p/interfaces/connection-encrypter' import type { PeerId } from '@libp2p/interfaces/peer-id' import type { Source } from 'it-stream-types' -export default (common: TestSetup) => { - describe('interface-crypto compliance tests', () => { - let crypto: Crypto +export default (common: TestSetup) => { + describe('interface-connection-encrypter compliance tests', () => { + let crypto: Encrypter let localPeer: PeerId let remotePeer: PeerId let mitmPeer: PeerId diff --git a/packages/libp2p-interface-compliance-tests/src/crypto/utils/index.ts b/packages/libp2p-interface-compliance-tests/src/connection-encrypter/utils/index.ts similarity index 100% rename from packages/libp2p-interface-compliance-tests/src/crypto/utils/index.ts rename to packages/libp2p-interface-compliance-tests/src/connection-encrypter/utils/index.ts diff --git a/packages/libp2p-interface-compliance-tests/src/mocks/index.ts b/packages/libp2p-interface-compliance-tests/src/mocks/index.ts index dec9cef1d..361365a93 100644 --- a/packages/libp2p-interface-compliance-tests/src/mocks/index.ts +++ b/packages/libp2p-interface-compliance-tests/src/mocks/index.ts @@ -6,4 +6,4 @@ export { mockMultiaddrConnection } from './multiaddr-connection.js' export { mockMuxer } from './muxer.js' export { mockRegistrar } from './registrar.js' export { mockUpgrader } from './upgrader.js' -export type { MockUpgraderOptions } from './upgrader.js' +export type { MockUpgraderInit } from './upgrader.js' diff --git a/packages/libp2p-interface-compliance-tests/src/mocks/muxer.ts b/packages/libp2p-interface-compliance-tests/src/mocks/muxer.ts index dbf081dc1..ad776863a 100644 --- a/packages/libp2p-interface-compliance-tests/src/mocks/muxer.ts +++ b/packages/libp2p-interface-compliance-tests/src/mocks/muxer.ts @@ -7,7 +7,7 @@ import errCode from 'err-code' import { Logger, logger } from '@libp2p/logger' import * as ndjson from 'it-ndjson' import type { Stream } from '@libp2p/interfaces/connection' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' import type { Source } from 'it-stream-types' import { pipe } from 'it-pipe' import map from 'it-map' @@ -54,8 +54,8 @@ class MuxedStream { private readonly resetController: AbortController private readonly log: Logger - constructor (opts: { id: string, type: 'initiator' | 'recipient', push: Pushable, onEnd: (err?: Error) => void }) { - const { id, type, push, onEnd } = opts + constructor (init: { id: string, type: 'initiator' | 'recipient', push: Pushable, onEnd: (err?: Error) => void }) { + const { id, type, push, onEnd } = init this.log = logger(`libp2p:mock-muxer:stream:${id}:${type}`) @@ -224,17 +224,17 @@ class MockMuxer implements Muxer { private readonly registryInitiatorStreams: Map private readonly registryRecipientStreams: Map - private readonly options: MuxerOptions + private readonly options: MuxerInit private readonly log: Logger - constructor (options?: MuxerOptions) { + constructor (init?: MuxerInit) { this.name = `muxer:${muxers++}` this.log = logger(`libp2p:mock-muxer:${this.name}`) this.registryInitiatorStreams = new Map() this.registryRecipientStreams = new Map() this.log('create muxer') - this.options = options ?? {} + this.options = init ?? {} // receives data from the muxer at the other end of the stream this.source = this.input = pushable({ onEnd: (err) => { @@ -349,8 +349,8 @@ class MockMuxer implements Muxer { } } -export function mockMuxer (options?: MuxerOptions): Muxer { - const mockMuxer = new MockMuxer(options) +export function mockMuxer (init?: MuxerInit): Muxer { + const mockMuxer = new MockMuxer(init) void Promise.resolve().then(async () => { void pipe( diff --git a/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts b/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts index 993a2183f..9b32317b9 100644 --- a/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts +++ b/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts @@ -3,11 +3,11 @@ import { mockConnection } from './connection.js' import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport' import type { Registrar } from '@libp2p/interfaces/registrar' -export interface MockUpgraderOptions { +export interface MockUpgraderInit { registrar?: Registrar } -export function mockUpgrader (options: MockUpgraderOptions = {}) { +export function mockUpgrader (init: MockUpgraderInit = {}) { const ensureProps = (multiaddrConnection: MultiaddrConnection) => { ['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => { expect(multiaddrConnection).to.have.property(prop) @@ -20,14 +20,14 @@ export function mockUpgrader (options: MockUpgraderOptions = {}) { ensureProps(multiaddrConnection) return mockConnection(multiaddrConnection, { direction: 'outbound', - registrar: options.registrar + registrar: init.registrar }) }, async upgradeInbound (multiaddrConnection) { ensureProps(multiaddrConnection) return mockConnection(multiaddrConnection, { direction: 'inbound', - registrar: options.registrar + registrar: init.registrar }) } } diff --git a/packages/libp2p-interface-compliance-tests/src/stream-muxer/base-test.ts b/packages/libp2p-interface-compliance-tests/src/stream-muxer/base-test.ts index 648283e58..b1f168692 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/base-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/base-test.ts @@ -11,14 +11,14 @@ import { isValidTick } from '../utils/is-valid-tick.js' import type { DeferredPromise } from 'p-defer' import type { TestSetup } from '../index.js' import type { Stream } from '@libp2p/interfaces/connection' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' import type { Source, Duplex } from 'it-stream-types' async function drainAndClose (stream: Duplex) { return await pipe([], stream, drain) } -export default (common: TestSetup) => { +export default (common: TestSetup) => { describe('base', () => { it('Open a stream from the dialer', async () => { const p = duplexPair() diff --git a/packages/libp2p-interface-compliance-tests/src/stream-muxer/close-test.ts b/packages/libp2p-interface-compliance-tests/src/stream-muxer/close-test.ts index 98d4637ae..e756678fe 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/close-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/close-test.ts @@ -7,7 +7,7 @@ import drain from 'it-drain' import { expect } from 'aegir/utils/chai.js' import delay from 'delay' import type { TestSetup } from '../index.js' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' function randomBuffer () { return uint8ArrayFromString(Math.random().toString()) @@ -22,7 +22,7 @@ const infiniteRandom = { } } -export default (common: TestSetup) => { +export default (common: TestSetup) => { describe('close', () => { it('closing underlying socket closes streams', async () => { let openedStreams = 0 diff --git a/packages/libp2p-interface-compliance-tests/src/stream-muxer/index.ts b/packages/libp2p-interface-compliance-tests/src/stream-muxer/index.ts index afed411fc..8d1a1c974 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/index.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/index.ts @@ -3,9 +3,9 @@ import closeTest from './close-test.js' import stressTest from './stress-test.js' import megaStressTest from './mega-stress-test.js' import type { TestSetup } from '../index.js' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' -export default (common: TestSetup) => { +export default (common: TestSetup) => { describe('interface-stream-muxer', () => { baseTest(common) closeTest(common) diff --git a/packages/libp2p-interface-compliance-tests/src/stream-muxer/mega-stress-test.ts b/packages/libp2p-interface-compliance-tests/src/stream-muxer/mega-stress-test.ts index b15920340..871aec24c 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/mega-stress-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/mega-stress-test.ts @@ -1,9 +1,9 @@ import spawn from './spawner.js' import type { TestSetup } from '../index.js' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' -export default (common: TestSetup) => { - const createMuxer = async (opts?: MuxerOptions) => await common.setup(opts) +export default (common: TestSetup) => { + const createMuxer = async (init?: MuxerInit) => await common.setup(init) describe.skip('mega stress test', function () { it('10,000 streams with 10,000 msg', async () => await spawn(createMuxer, 10000, 10000, 5000)) diff --git a/packages/libp2p-interface-compliance-tests/src/stream-muxer/spawner.ts b/packages/libp2p-interface-compliance-tests/src/stream-muxer/spawner.ts index ffb747a56..aa7834464 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/spawner.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/spawner.ts @@ -5,9 +5,9 @@ import pLimit from 'p-limit' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import drain from 'it-drain' import all from 'it-all' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' -export default async (createMuxer: (options?: MuxerOptions) => Promise, nStreams: number, nMsg: number, limit?: number) => { +export default async (createMuxer: (init?: MuxerInit) => Promise, nStreams: number, nMsg: number, limit?: number) => { const [dialerSocket, listenerSocket] = duplexPair() const msg = uint8ArrayFromString('simple msg') diff --git a/packages/libp2p-interface-compliance-tests/src/stream-muxer/stress-test.ts b/packages/libp2p-interface-compliance-tests/src/stream-muxer/stress-test.ts index eef5f88df..965bd0508 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/stress-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/stress-test.ts @@ -1,9 +1,9 @@ import spawn from './spawner.js' import type { TestSetup } from '../index.js' -import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer' +import type { Muxer, MuxerInit } from '@libp2p/interfaces/stream-muxer' -export default (common: TestSetup) => { - const createMuxer = async (opts?: MuxerOptions) => await common.setup(opts) +export default (common: TestSetup) => { + const createMuxer = async (init?: MuxerInit) => await common.setup(init) describe('stress test', () => { it('1 stream with 1 msg', async () => await spawn(createMuxer, 1, 1)) diff --git a/packages/libp2p-interface-compliance-tests/test/crypto/index.spec.ts b/packages/libp2p-interface-compliance-tests/test/connection-encrypter/index.spec.ts similarity index 74% rename from packages/libp2p-interface-compliance-tests/test/crypto/index.spec.ts rename to packages/libp2p-interface-compliance-tests/test/connection-encrypter/index.spec.ts index f97383c7f..bb9d77938 100644 --- a/packages/libp2p-interface-compliance-tests/test/crypto/index.spec.ts +++ b/packages/libp2p-interface-compliance-tests/test/connection-encrypter/index.spec.ts @@ -1,4 +1,4 @@ -import tests from '../../src/crypto/index.js' +import tests from '../../src/connection-encrypter/index.js' import mockCrypto from './mock-crypto.js' describe('compliance tests', () => { diff --git a/packages/libp2p-interface-compliance-tests/test/crypto/mock-crypto.ts b/packages/libp2p-interface-compliance-tests/test/connection-encrypter/mock-crypto.ts similarity index 94% rename from packages/libp2p-interface-compliance-tests/test/crypto/mock-crypto.ts rename to packages/libp2p-interface-compliance-tests/test/connection-encrypter/mock-crypto.ts index 807b6376a..f5bcabd8a 100644 --- a/packages/libp2p-interface-compliance-tests/test/crypto/mock-crypto.ts +++ b/packages/libp2p-interface-compliance-tests/test/connection-encrypter/mock-crypto.ts @@ -2,9 +2,9 @@ import { peerIdFromBytes } from '@libp2p/peer-id' import { handshake } from 'it-handshake' import { duplexPair } from 'it-pair/duplex' import { pipe } from 'it-pipe' -import { UnexpectedPeerError } from '@libp2p/interfaces/crypto/errors' +import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors' import { Multiaddr } from '@multiformats/multiaddr' -import type { Crypto } from '@libp2p/interfaces/crypto' +import type { Encrypter } from '@libp2p/interfaces/connection-encrypter' import type { Transform, Source } from 'it-stream-types' // A basic transform that does nothing to the data @@ -16,7 +16,7 @@ const transform = (): Transform => { })() } -const crypto: Crypto = { +const crypto: Encrypter = { protocol: 'insecure', secureInbound: async (localPeer, duplex, expectedPeer) => { // 1. Perform a basic handshake. diff --git a/packages/libp2p-interface-compliance-tests/test/mocks/muxer.ts b/packages/libp2p-interface-compliance-tests/test/mocks/muxer.ts index c2d4123d8..a2aa756fe 100644 --- a/packages/libp2p-interface-compliance-tests/test/mocks/muxer.ts +++ b/packages/libp2p-interface-compliance-tests/test/mocks/muxer.ts @@ -3,8 +3,8 @@ import { mockMuxer } from '../../src/mocks/muxer.js' describe('compliance tests', () => { tests({ - async setup (options) { - return mockMuxer(options) + async setup (init) { + return mockMuxer(init) }, async teardown () { diff --git a/packages/libp2p-interface-compliance-tests/test/peer-discovery/mock-discovery.ts b/packages/libp2p-interface-compliance-tests/test/peer-discovery/mock-discovery.ts index b83879950..a7b7ef3b3 100644 --- a/packages/libp2p-interface-compliance-tests/test/peer-discovery/mock-discovery.ts +++ b/packages/libp2p-interface-compliance-tests/test/peer-discovery/mock-discovery.ts @@ -3,7 +3,7 @@ import * as PeerIdFactory from '@libp2p/peer-id-factory' import { EventEmitter, CustomEvent } from '@libp2p/interfaces' import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery' -interface MockDiscoveryOptions { +interface MockDiscoveryInit { discoveryDelay?: number } @@ -11,14 +11,14 @@ interface MockDiscoveryOptions { * Emits 'peer' events on discovery. */ export class MockDiscovery extends EventEmitter implements PeerDiscovery { - public readonly options: MockDiscoveryOptions + public readonly options: MockDiscoveryInit private _isRunning: boolean private _timer: any - constructor (options = {}) { + constructor (init = {}) { super() - this.options = options + this.options = init this._isRunning = false } diff --git a/packages/libp2p-interfaces/package.json b/packages/libp2p-interfaces/package.json index 988eb697e..ecc8fb84e 100644 --- a/packages/libp2p-interfaces/package.json +++ b/packages/libp2p-interfaces/package.json @@ -52,6 +52,14 @@ "import": "./dist/src/connection/index.js", "types": "./dist/src/connection/index.d.ts" }, + "./connection-encrypter": { + "import": "./dist/src/connection-encrypter/index.js", + "types": "./dist/src/connection-encrypter/index.d.ts" + }, + "./connection-encrypter/errors": { + "import": "./dist/src/connection-encrypter/errors.js", + "types": "./dist/src/connection-encrypter/errors.d.ts" + }, "./connection/status": { "import": "./dist/src/connection/status.js", "types": "./dist/src/connection/status.d.ts" @@ -60,14 +68,6 @@ "import": "./dist/src/content-routing/index.js", "types": "./dist/src/content-routing/index.d.ts" }, - "./crypto": { - "import": "./dist/src/crypto/index.js", - "types": "./dist/src/crypto/index.d.ts" - }, - "./crypto/errors": { - "import": "./dist/src/crypto/errors.js", - "types": "./dist/src/crypto/errors.d.ts" - }, "./dht": { "import": "./dist/src/dht/index.js", "types": "./dist/src/dht/index.d.ts" diff --git a/packages/libp2p-interfaces/src/crypto/README.md b/packages/libp2p-interfaces/src/connection-encrypter/README.md similarity index 94% rename from packages/libp2p-interfaces/src/crypto/README.md rename to packages/libp2p-interfaces/src/connection-encrypter/README.md index 9ec72a258..a0631421d 100644 --- a/packages/libp2p-interfaces/src/crypto/README.md +++ b/packages/libp2p-interfaces/src/connection-encrypter/README.md @@ -1,4 +1,4 @@ -interface-crypto +interface-connection-encrypter ================== > A test suite you can use to implement a libp2p crypto module. A libp2p crypto module is used to ensure all exchanged data between two peers is encrypted. @@ -9,7 +9,7 @@ interface-crypto - [js-libp2p-secio](https://github.com/NodeFactoryIo/js-libp2p-secio) ## Table of Contents -- [interface-crypto](#interface-crypto) +- [interface-connection-encrypter](#interface-connection-encrypter) - [Table of Contents](#table-of-contents) - [Using the Test Suite](#using-the-test-suite) - [API](#api) @@ -23,8 +23,8 @@ interface-crypto You can also check out the [internal test suite](../../test/crypto/compliance.spec.js) to see the setup in action. ```js -const tests = require('libp2p-interfaces-compliance-tests/crypto') -const yourCrypto = require('./your-crypto') +const tests = require('libp2p-interfaces-compliance-tests/connection-encrypter') +const yourCrypto = require('./your-encrypter') tests({ setup () { diff --git a/packages/libp2p-interfaces/src/crypto/errors.ts b/packages/libp2p-interfaces/src/connection-encrypter/errors.ts similarity index 100% rename from packages/libp2p-interfaces/src/crypto/errors.ts rename to packages/libp2p-interfaces/src/connection-encrypter/errors.ts diff --git a/packages/libp2p-interfaces/src/crypto/index.ts b/packages/libp2p-interfaces/src/connection-encrypter/index.ts similarity index 76% rename from packages/libp2p-interfaces/src/crypto/index.ts rename to packages/libp2p-interfaces/src/connection-encrypter/index.ts index 0eea6a06a..6cc66fd6d 100644 --- a/packages/libp2p-interfaces/src/crypto/index.ts +++ b/packages/libp2p-interfaces/src/connection-encrypter/index.ts @@ -1,11 +1,15 @@ import type { PeerId } from '../peer-id' import type { MultiaddrConnection } from '../transport' +export interface EncrypterFactory { + new (init?: EncrypterInit): Crypto +} + /** - * A libp2p crypto module must be compliant to this interface + * A libp2p connection encrypter module must be compliant to this interface * to ensure all exchanged data between two peers is encrypted. */ -export interface Crypto { +export interface Encrypter { protocol: string /** * Encrypt outgoing data to the remote party. diff --git a/packages/libp2p-interfaces/src/content-routing/index.ts b/packages/libp2p-interfaces/src/content-routing/index.ts index 9308da183..253d49b95 100644 --- a/packages/libp2p-interfaces/src/content-routing/index.ts +++ b/packages/libp2p-interfaces/src/content-routing/index.ts @@ -2,8 +2,8 @@ import type { CID } from 'multiformats/cid' import type { AbortOptions } from '../index' import type { PeerData } from '../peer-data' -export interface ContentRoutingFactory { - new (options?: any): ContentRouting +export interface ContentRoutingFactory { + new (init?: ContentRoutingInit): ContentRouting } export interface ContentRouting { diff --git a/packages/libp2p-interfaces/src/peer-discovery/index.ts b/packages/libp2p-interfaces/src/peer-discovery/index.ts index 1a229f549..fccbdb49a 100644 --- a/packages/libp2p-interfaces/src/peer-discovery/index.ts +++ b/packages/libp2p-interfaces/src/peer-discovery/index.ts @@ -1,8 +1,8 @@ import type { PeerData } from '../peer-data/index.js' import type { EventEmitter, Startable } from '../index.js' -export interface PeerDiscoveryFactory { - new (options?: any): PeerDiscovery +export interface PeerDiscoveryFactory { + new (init?: PeerDiscoveryInit): PeerDiscovery tag: string } diff --git a/packages/libp2p-interfaces/src/peer-routing/index.ts b/packages/libp2p-interfaces/src/peer-routing/index.ts index 5059af994..f6de78dfc 100644 --- a/packages/libp2p-interfaces/src/peer-routing/index.ts +++ b/packages/libp2p-interfaces/src/peer-routing/index.ts @@ -1,13 +1,14 @@ -import type { PeerId } from '../peer-id' -import type { PeerData } from '../peer-data' +import type { PeerId } from '../peer-id/index.js' +import type { PeerData } from '../peer-data/index.js' +import type { AbortOptions } from '../index.js' -export interface PeerRoutingFactory { - new (options?: any): PeerRouting +export interface PeerRoutingFactory { + new (init?: PeerRoutingInit): PeerRouting } export interface PeerRouting { - findPeer: (peerId: PeerId, options?: Object) => Promise - getClosestPeers: (key: Uint8Array, options?: Object) => AsyncIterable + findPeer: (peerId: PeerId, options?: AbortOptions) => Promise + getClosestPeers: (key: Uint8Array, options?: AbortOptions) => AsyncIterable } export default PeerRouting diff --git a/packages/libp2p-interfaces/src/stream-muxer/index.ts b/packages/libp2p-interfaces/src/stream-muxer/index.ts index 906110b01..75ab8c71b 100644 --- a/packages/libp2p-interfaces/src/stream-muxer/index.ts +++ b/packages/libp2p-interfaces/src/stream-muxer/index.ts @@ -2,8 +2,8 @@ import type { Duplex } from 'it-stream-types' import type { Stream } from '../connection/index.js' import type { AbortOptions } from '../index.js' -export interface MuxerFactory { - new (options: MuxerOptions): Muxer +export interface MuxerFactory { + new (init?: T): Muxer multicodec: string } @@ -19,7 +19,7 @@ export interface Muxer extends Duplex { newStream: (name?: string) => Stream } -export interface MuxerOptions extends AbortOptions { +export interface MuxerInit extends AbortOptions { onIncomingStream?: (stream: Stream) => void onStreamEnd?: (stream: Stream) => void } diff --git a/packages/libp2p-interfaces/src/topology/README.md b/packages/libp2p-interfaces/src/topology/README.md index 98c38b309..7caa4b13f 100644 --- a/packages/libp2p-interfaces/src/topology/README.md +++ b/packages/libp2p-interfaces/src/topology/README.md @@ -38,34 +38,19 @@ TBA ### Topology ```js -const Topology = require('libp2p-interfaces/src/topology') +import { createTopology } from '@libp2p/topology' -const toplogy = new Topology({ - min: 0, - max: 50 -}) -``` - -### Multicodec Topology - -```js -const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology') - -const toplogy = new MulticodecTopology({ +const toplogy = createTopology({ min: 0, max: 50, multicodecs: ['/echo/1.0.0'], - handlers: { - onConnect: (peerId, conn) => {}, - onDisconnect: (peerId) => {} - } + onConnect: (peerId, conn) => {}, + onDisconnect: (peerId) => {} }) ``` ## API -The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API a subset of the `MulticodecTopology` API. - ### Topology - `Topology` @@ -75,13 +60,13 @@ The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API #### Constructor ```js -const toplogy = new Topology({ +import { createTopology } from '@libp2p/topology' + +const toplogy = createTopology({ min: 0, max: 50, - handlers: { - onConnect: (peerId, conn) => {}, - onDisconnect: (peerId) => {} - } + onConnect: (peerId, conn) => {}, + onDisconnect: (peerId) => {} }) ``` diff --git a/packages/libp2p-interfaces/src/topology/index.ts b/packages/libp2p-interfaces/src/topology/index.ts index c1ffeb7d6..460213bdf 100644 --- a/packages/libp2p-interfaces/src/topology/index.ts +++ b/packages/libp2p-interfaces/src/topology/index.ts @@ -9,7 +9,7 @@ export interface onDisconnectHandler { (peerId: PeerId, conn?: Connection): void } -export interface TopologyOptions { +export interface TopologyInit { /** * minimum needed connections */ @@ -31,3 +31,9 @@ export interface Topology { onConnect: (peerId: PeerId, conn: Connection) => void onDisconnect: (peerId: PeerId) => void } + +export const symbol = Symbol.for('@libp2p/topology') + +export function isTopology (other: any): other is Topology { + return symbol in other +} diff --git a/packages/libp2p-interfaces/src/transport/README.md b/packages/libp2p-interfaces/src/transport/README.md index 5377396bc..c1b48b165 100644 --- a/packages/libp2p-interfaces/src/transport/README.md +++ b/packages/libp2p-interfaces/src/transport/README.md @@ -39,8 +39,8 @@ const YourTransport = require('../src') describe('compliance', () => { tests({ - setup (options) { - let transport = new YourTransport(options) + setup (init) { + let transport = new YourTransport(init) const addrs = [ multiaddr('valid-multiaddr-for-your-transport'), diff --git a/packages/libp2p-logger/package.json b/packages/libp2p-logger/package.json index 06423ac9d..168f86b03 100644 --- a/packages/libp2p-logger/package.json +++ b/packages/libp2p-logger/package.json @@ -132,7 +132,7 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6", + "@libp2p/interfaces": "^1.3.12", "debug": "^4.3.3", "interface-datastore": "^6.1.0", "multiformats": "^9.6.3" diff --git a/packages/libp2p-multistream-select/package.json b/packages/libp2p-multistream-select/package.json index f53709457..4bce13c79 100644 --- a/packages/libp2p-multistream-select/package.json +++ b/packages/libp2p-multistream-select/package.json @@ -136,8 +136,8 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6", - "@libp2p/logger": "^1.0.3", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/logger": "^1.1.0", "abortable-iterator": "^4.0.2", "err-code": "^3.0.1", "it-first": "^1.0.6", diff --git a/packages/libp2p-peer-id-factory/package.json b/packages/libp2p-peer-id-factory/package.json index 84c5358ad..d6ede70a5 100644 --- a/packages/libp2p-peer-id-factory/package.json +++ b/packages/libp2p-peer-id-factory/package.json @@ -140,8 +140,8 @@ }, "dependencies": { "@libp2p/crypto": "^0.22.7", - "@libp2p/interfaces": "^1.3.6", - "@libp2p/peer-id": "^1.0.4", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/peer-id": "^1.1.6", "multiformats": "^9.6.3", "protobufjs": "^6.10.2", "uint8arrays": "^3.0.0" diff --git a/packages/libp2p-peer-id/package.json b/packages/libp2p-peer-id/package.json index 96b2a0f30..eb670c3e5 100644 --- a/packages/libp2p-peer-id/package.json +++ b/packages/libp2p-peer-id/package.json @@ -132,7 +132,7 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6", + "@libp2p/interfaces": "^1.3.12", "err-code": "^3.0.1", "multiformats": "^9.6.3", "uint8arrays": "^3.0.0" diff --git a/packages/libp2p-peer-id/src/index.ts b/packages/libp2p-peer-id/src/index.ts index a52cfe779..1fcb2295d 100644 --- a/packages/libp2p-peer-id/src/index.ts +++ b/packages/libp2p-peer-id/src/index.ts @@ -23,24 +23,24 @@ const LIBP2P_KEY_CODE = 0x72 const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36 const MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH = 37 -interface PeerIdOptions { +interface PeerIdInit { type: 'RSA' | 'Ed25519' | 'secp256k1' multihash: MultihashDigest privateKey?: Uint8Array } -interface RSAPeerIdOptions { +interface RSAPeerIdInit { multihash: MultihashDigest privateKey?: Uint8Array publicKey?: Uint8Array } -interface Ed25519PeerIdOptions { +interface Ed25519PeerIdInit { multihash: MultihashDigest privateKey?: Uint8Array } -interface Secp256k1PeerIdOptions { +interface Secp256k1PeerIdInit { multihash: MultihashDigest privateKey?: Uint8Array } @@ -52,10 +52,10 @@ class PeerIdImpl { public readonly publicKey?: Uint8Array private readonly strings: Map - constructor (opts: PeerIdOptions) { - this.type = opts.type - this.multihash = opts.multihash - this.privateKey = opts.privateKey + constructor (init: PeerIdInit) { + this.type = init.type + this.multihash = init.multihash + this.privateKey = init.privateKey // mark toString cache as non-enumerable this.strings = new Map() @@ -118,10 +118,10 @@ class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId { public readonly type = 'RSA' public readonly publicKey?: Uint8Array - constructor (opts: RSAPeerIdOptions) { - super({ ...opts, type: 'RSA' }) + constructor (init: RSAPeerIdInit) { + super({ ...init, type: 'RSA' }) - this.publicKey = opts.publicKey + this.publicKey = init.publicKey } } @@ -129,10 +129,10 @@ class Ed25519PeerIdImpl extends PeerIdImpl implements Ed25519PeerId { public readonly type = 'Ed25519' public readonly publicKey: Uint8Array - constructor (opts: Ed25519PeerIdOptions) { - super({ ...opts, type: 'Ed25519' }) + constructor (init: Ed25519PeerIdInit) { + super({ ...init, type: 'Ed25519' }) - this.publicKey = opts.multihash.digest + this.publicKey = init.multihash.digest } } @@ -140,15 +140,15 @@ class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId { public readonly type = 'secp256k1' public readonly publicKey: Uint8Array - constructor (opts: Secp256k1PeerIdOptions) { - super({ ...opts, type: 'secp256k1' }) + constructor (init: Secp256k1PeerIdInit) { + super({ ...init, type: 'secp256k1' }) - this.publicKey = opts.multihash.digest + this.publicKey = init.multihash.digest } } -export function createPeerId (opts: PeerIdOptions) { - return new PeerIdImpl(opts) +export function createPeerId (init: PeerIdInit) { + return new PeerIdImpl(init) } export function peerIdFromPeerId (other: any): PeerId { diff --git a/packages/libp2p-peer-map/package.json b/packages/libp2p-peer-map/package.json index 53fa4a968..fb7baef27 100644 --- a/packages/libp2p-peer-map/package.json +++ b/packages/libp2p-peer-map/package.json @@ -132,11 +132,11 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6" + "@libp2p/interfaces": "^1.3.12" }, "devDependencies": { - "@libp2p/peer-id": "^1.1.3", - "@libp2p/peer-id-factory": "^1.0.5", + "@libp2p/peer-id": "^1.1.6", + "@libp2p/peer-id-factory": "^1.0.6", "aegir": "^36.1.3" } } diff --git a/packages/libp2p-peer-record/package.json b/packages/libp2p-peer-record/package.json index e200a5c1f..126e47da4 100644 --- a/packages/libp2p-peer-record/package.json +++ b/packages/libp2p-peer-record/package.json @@ -145,9 +145,9 @@ }, "dependencies": { "@libp2p/crypto": "^0.22.7", - "@libp2p/interfaces": "^1.3.6", - "@libp2p/logger": "^1.0.3", - "@libp2p/peer-id": "^1.0.4", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/logger": "^1.1.0", + "@libp2p/peer-id": "^1.1.6", "@libp2p/utils": "^1.0.5", "@multiformats/multiaddr": "^10.1.5", "err-code": "^3.0.1", @@ -163,8 +163,8 @@ "varint": "^6.0.0" }, "devDependencies": { - "@libp2p/interface-compliance-tests": "^1.1.2", - "@libp2p/peer-id-factory": "^1.0.5", + "@libp2p/interface-compliance-tests": "^1.1.14", + "@libp2p/peer-id-factory": "^1.0.6", "@types/varint": "^6.0.0", "aegir": "^36.1.3", "sinon": "^13.0.1" diff --git a/packages/libp2p-peer-record/src/envelope/index.ts b/packages/libp2p-peer-record/src/envelope/index.ts index aea8c4935..f3a30954f 100644 --- a/packages/libp2p-peer-record/src/envelope/index.ts +++ b/packages/libp2p-peer-record/src/envelope/index.ts @@ -10,7 +10,7 @@ import { peerIdFromKeys } from '@libp2p/peer-id' import type { PeerId } from '@libp2p/interfaces/peer-id' import type { Record, Envelope } from '@libp2p/interfaces/record' -export interface EnvelopeOptions { +export interface EnvelopeInit { peerId: PeerId payloadType: Uint8Array payload: Uint8Array @@ -84,8 +84,8 @@ export class RecordEnvelope implements Envelope { * The Envelope is responsible for keeping an arbitrary signed record * by a libp2p peer. */ - constructor (options: EnvelopeOptions) { - const { peerId, payloadType, payload, signature } = options + constructor (init: EnvelopeInit) { + const { peerId, payloadType, payload, signature } = init this.peerId = peerId this.payloadType = payloadType diff --git a/packages/libp2p-peer-record/src/peer-record/index.ts b/packages/libp2p-peer-record/src/peer-record/index.ts index 21f8fbeb7..ef0787e25 100644 --- a/packages/libp2p-peer-record/src/peer-record/index.ts +++ b/packages/libp2p-peer-record/src/peer-record/index.ts @@ -8,7 +8,7 @@ import { ENVELOPE_PAYLOAD_TYPE_PEER_RECORD } from './consts.js' -export interface PeerRecordOptions { +export interface PeerRecordInit { peerId: PeerId /** @@ -49,8 +49,8 @@ export class PeerRecord { public codec = PeerRecord.CODEC private marshaled?: Uint8Array - constructor (options: PeerRecordOptions) { - const { peerId, multiaddrs, seqNumber } = options + constructor (init: PeerRecordInit) { + const { peerId, multiaddrs, seqNumber } = init this.peerId = peerId this.multiaddrs = multiaddrs ?? [] diff --git a/packages/libp2p-peer-store/package.json b/packages/libp2p-peer-store/package.json index c28eaed63..eb4277007 100644 --- a/packages/libp2p-peer-store/package.json +++ b/packages/libp2p-peer-store/package.json @@ -140,9 +140,9 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6", - "@libp2p/logger": "^1.0.3", - "@libp2p/peer-record": "^1.0.1", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/logger": "^1.1.0", + "@libp2p/peer-record": "^1.0.5", "@multiformats/multiaddr": "^10.1.5", "interface-datastore": "^6.1.0", "it-all": "^1.0.6", @@ -155,9 +155,9 @@ "protobufjs": "^6.10.2" }, "devDependencies": { - "@libp2p/interface-compliance-tests": "^1.1.2", - "@libp2p/peer-id": "^1.1.3", - "@libp2p/peer-id-factory": "^1.0.5", + "@libp2p/interface-compliance-tests": "^1.1.14", + "@libp2p/peer-id": "^1.1.6", + "@libp2p/peer-id-factory": "^1.0.6", "@libp2p/utils": "^1.0.5", "aegir": "^36.1.3", "datastore-core": "^7.0.1", diff --git a/packages/libp2p-peer-store/src/index.ts b/packages/libp2p-peer-store/src/index.ts index 2c5dc2d46..1be1d2cee 100644 --- a/packages/libp2p-peer-store/src/index.ts +++ b/packages/libp2p-peer-store/src/index.ts @@ -21,7 +21,7 @@ export interface AddressSorter { (addresses: Address[]): Address[] } -export interface PeerStoreOptions { +export interface PeerStoreInit { peerId: PeerId datastore: Datastore addressFilter?: AddressFilter @@ -39,10 +39,10 @@ export class PeerStoreImpl extends EventEmitter implements Peer private readonly peerId: PeerId private readonly store: Store - constructor (options: PeerStoreOptions) { + constructor (init: PeerStoreInit) { super() - const { peerId, datastore, addressFilter } = options + const { peerId, datastore, addressFilter } = init this.peerId = peerId this.store = new PersistentStore(datastore) @@ -122,6 +122,6 @@ export class PeerStoreImpl extends EventEmitter implements Peer } } -export function createPeerStore (opts: PeerStoreOptions): PeerStore { - return new PeerStoreImpl(opts) +export function createPeerStore (init: PeerStoreInit): PeerStore { + return new PeerStoreImpl(init) } diff --git a/packages/libp2p-pubsub/package.json b/packages/libp2p-pubsub/package.json index 50cfbdb1c..244512c7e 100644 --- a/packages/libp2p-pubsub/package.json +++ b/packages/libp2p-pubsub/package.json @@ -183,11 +183,11 @@ }, "dependencies": { "@libp2p/crypto": "^0.22.7", - "@libp2p/interfaces": "^1.3.6", - "@libp2p/logger": "^1.0.3", - "@libp2p/peer-id": "^1.0.4", - "@libp2p/peer-map": "^1.0.0", - "@libp2p/topology": "^1.0.0", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/logger": "^1.1.0", + "@libp2p/peer-id": "^1.1.6", + "@libp2p/peer-map": "^1.0.2", + "@libp2p/topology": "^1.1.4", "@multiformats/multiaddr": "^10.1.5", "err-code": "^3.0.1", "iso-random-stream": "^2.0.0", @@ -198,7 +198,7 @@ "uint8arrays": "^3.0.0" }, "devDependencies": { - "@libp2p/peer-id-factory": "^1.0.5", + "@libp2p/peer-id-factory": "^1.0.6", "abortable-iterator": "^4.0.2", "aegir": "^36.1.3", "it-pair": "^2.0.2", diff --git a/packages/libp2p-pubsub/src/index.ts b/packages/libp2p-pubsub/src/index.ts index 1435d419e..92e870916 100644 --- a/packages/libp2p-pubsub/src/index.ts +++ b/packages/libp2p-pubsub/src/index.ts @@ -3,7 +3,7 @@ import { EventEmitter, CustomEvent, EventHandler } from '@libp2p/interfaces' import errcode from 'err-code' import { pipe } from 'it-pipe' import Queue from 'p-queue' -import { Topology } from '@libp2p/topology' +import { createTopology } from '@libp2p/topology' import { codes } from './errors.js' import { PeerStreams as PeerStreamsImpl } from './peer-streams.js' import { toMessage, ensureArray, randomSeqno, noSignMsgId, msgId, toRpcMessage } from './utils.js' @@ -125,7 +125,7 @@ export abstract class PubsubBaseProtocol { private readonly _inboundAbortController: AbortController private closed: boolean - constructor (opts: Options) { + constructor (init: PeerStreamsInit) { super() - this.id = opts.id - this.protocol = opts.protocol + this.id = init.id + this.protocol = init.protocol this._inboundAbortController = new AbortController() this.closed = false diff --git a/packages/libp2p-topology/README.md b/packages/libp2p-topology/README.md index 16b400832..750efa1ae 100644 --- a/packages/libp2p-topology/README.md +++ b/packages/libp2p-topology/README.md @@ -15,11 +15,9 @@ npm i libp2p-connection ``` ```javascript -import { Topology } from '@libp2p/topology' -import { MulticodecTopology } from '@libp2p/topology/multicodec-topology' +import { createTopology } from '@libp2p/topology' -const topology = new Topology({ ... }) -const multicodecTopology = new MulticodecTopology({ ... }) +const topology = createTopology({ ... }) ``` ## Contribute diff --git a/packages/libp2p-topology/package.json b/packages/libp2p-topology/package.json index f9f21e77f..245f3dd59 100644 --- a/packages/libp2p-topology/package.json +++ b/packages/libp2p-topology/package.json @@ -146,8 +146,8 @@ "build": "tsc" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6", - "@libp2p/logger": "^1.0.3", + "@libp2p/interfaces": "^1.3.12", + "@libp2p/logger": "^1.1.0", "@multiformats/multiaddr": "^10.1.5", "err-code": "^3.0.1", "it-all": "^1.0.6" diff --git a/packages/libp2p-topology/src/index.ts b/packages/libp2p-topology/src/index.ts index b6f03970d..6c375030e 100644 --- a/packages/libp2p-topology/src/index.ts +++ b/packages/libp2p-topology/src/index.ts @@ -1,11 +1,11 @@ +import { symbol } from '@libp2p/interfaces/topology' import type { PeerId } from '@libp2p/interfaces/peer-id' -import type { TopologyOptions, onConnectHandler, onDisconnectHandler } from '@libp2p/interfaces/topology' +import type { Topology, TopologyInit, onConnectHandler, onDisconnectHandler } from '@libp2p/interfaces/topology' import type { Registrar } from '@libp2p/interfaces/registrar' const noop = () => {} -const topologySymbol = Symbol.for('@libp2p/topology') -export class Topology { +class TopologyImpl implements Topology { public min: number public max: number @@ -18,30 +18,23 @@ export class Topology { protected _registrar: Registrar | undefined - constructor (options: TopologyOptions) { - this.min = options.min ?? 0 - this.max = options.max ?? Infinity + constructor (init: TopologyInit) { + this.min = init.min ?? 0 + this.max = init.max ?? Infinity this.peers = new Set() - this.onConnect = options.onConnect ?? noop - this.onDisconnect = options.onDisconnect ?? noop + this.onConnect = init.onConnect ?? noop + this.onDisconnect = init.onDisconnect ?? noop } get [Symbol.toStringTag] () { - return topologySymbol.toString() + return symbol.toString() } - get [topologySymbol] () { + get [symbol] () { return true } - /** - * Checks if the given value is a Topology instance - */ - static isTopology (other: any): other is Topology { - return topologySymbol in other - } - /** * Notify about peer disconnected event */ @@ -49,3 +42,7 @@ export class Topology { this.onDisconnect(peerId) } } + +export function createTopology (init: TopologyInit): Topology { + return new TopologyImpl(init) +} diff --git a/packages/libp2p-tracked-map/package.json b/packages/libp2p-tracked-map/package.json index da3735850..a6e727992 100644 --- a/packages/libp2p-tracked-map/package.json +++ b/packages/libp2p-tracked-map/package.json @@ -132,7 +132,7 @@ "test:electron-main": "npm run test -- -t electron-main" }, "dependencies": { - "@libp2p/interfaces": "^1.3.6" + "@libp2p/interfaces": "^1.3.12" }, "devDependencies": { "aegir": "^36.1.3", diff --git a/packages/libp2p-tracked-map/src/index.ts b/packages/libp2p-tracked-map/src/index.ts index ea979f8f2..0c4d9f448 100644 --- a/packages/libp2p-tracked-map/src/index.ts +++ b/packages/libp2p-tracked-map/src/index.ts @@ -1,6 +1,6 @@ import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics' -export interface TrackedMapOptions { +export interface TrackedMapInit { metrics: ComponentMetricsTracker system?: string component: string @@ -13,10 +13,10 @@ class TrackedMap extends Map { private readonly metric: string private readonly metrics: ComponentMetricsTracker - constructor (options: TrackedMapOptions) { + constructor (init: TrackedMapInit) { super() - const { system, component, metric, metrics } = options + const { system, component, metric, metrics } = init this.system = system ?? 'libp2p' this.component = component this.metric = metric