diff --git a/package.json b/package.json index d1197f171..a4af7e3b9 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "build": "lerna run build", "lint": "lerna run lint", "dep-check": "lerna run dep-check", - "release": "lerna exec --concurrency 1 -- semantic-release -e semantic-release-monorepo" + "release": "lerna run --concurrency 1 release -- --" }, "dependencies": { "lerna": "^4.0.0", diff --git a/packages/libp2p-connection/package.json b/packages/libp2p-connection/package.json index 332c81cf8..ccb4d4410 100644 --- a/packages/libp2p-connection/package.json +++ b/packages/libp2p-connection/package.json @@ -141,17 +141,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0", @@ -161,7 +162,7 @@ "devDependencies": { "@libp2p/interface-compliance-tests": "^1.1.0", "@libp2p/peer-id-factory": "^1.0.0", - "aegir": "^36.1.3", + "aegir": "^37.0.6", "it-pair": "^2.0.2" } } diff --git a/packages/libp2p-connection/test/compliance.spec.ts b/packages/libp2p-connection/test/compliance.spec.ts index 3b6bf2c50..90a511e4a 100644 --- a/packages/libp2p-connection/test/compliance.spec.ts +++ b/packages/libp2p-connection/test/compliance.spec.ts @@ -35,9 +35,12 @@ describe('compliance tests', () => { const id = `${streamId++}` const stream: Stream = { ...pair(), - close: async () => { - await stream.sink(async function * () {}()) - connection.removeStream(stream.id) + close: () => { + void stream.sink(async function * () {}()) + .then(() => { + connection.removeStream(stream.id) + }) + .catch() }, id, abort: () => {}, diff --git a/packages/libp2p-connection/test/index.spec.ts b/packages/libp2p-connection/test/index.spec.ts index 7459bf957..43912a48f 100644 --- a/packages/libp2p-connection/test/index.spec.ts +++ b/packages/libp2p-connection/test/index.spec.ts @@ -54,7 +54,9 @@ describe('connection tests', () => { const id = `${streamId++}` const stream: Stream = { ...pair(), - close: async () => await stream.sink(async function * () {}()), + close: () => { + void stream.sink(async function * () {}()).catch() + }, id, abort: () => {}, reset: () => {}, diff --git a/packages/libp2p-connection/tsconfig.json b/packages/libp2p-connection/tsconfig.json index d25bef66d..58d1628fd 100644 --- a/packages/libp2p-connection/tsconfig.json +++ b/packages/libp2p-connection/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-interface-compliance-tests/package.json b/packages/libp2p-interface-compliance-tests/package.json index 0bb797846..e3218562b 100644 --- a/packages/libp2p-interface-compliance-tests/package.json +++ b/packages/libp2p-interface-compliance-tests/package.json @@ -185,17 +185,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/crypto": "^0.22.8", @@ -207,7 +208,7 @@ "@libp2p/pubsub": "^1.2.0", "@multiformats/multiaddr": "^10.1.5", "abortable-iterator": "^4.0.2", - "aegir": "^36.1.3", + "aegir": "^37.0.6", "any-signal": "^3.0.0", "delay": "^5.0.0", "err-code": "^3.0.1", diff --git a/packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts b/packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts index 9cf240f15..ccfc7dec0 100644 --- a/packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts +++ b/packages/libp2p-interface-compliance-tests/src/connection-encrypter/index.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { pipe } from 'it-pipe' import * as PeerIdFactory from '@libp2p/peer-id-factory' import all from 'it-all' diff --git a/packages/libp2p-interface-compliance-tests/src/connection/connection.ts b/packages/libp2p-interface-compliance-tests/src/connection/connection.ts index 8cac6767a..9b1ef05b8 100644 --- a/packages/libp2p-interface-compliance-tests/src/connection/connection.ts +++ b/packages/libp2p-interface-compliance-tests/src/connection/connection.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import type { TestSetup } from '../index.js' import type { Connection } from '@libp2p/interfaces/connection' diff --git a/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts b/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts index 0360f1f8e..3dd7d5f43 100644 --- a/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts +++ b/packages/libp2p-interface-compliance-tests/src/mocks/upgrader.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { mockConnection } from './connection.js' import type { Upgrader, MultiaddrConnection, UpgraderEvents } from '@libp2p/interfaces/transport' import type { Registrar } from '@libp2p/interfaces/registrar' diff --git a/packages/libp2p-interface-compliance-tests/src/peer-discovery/index.ts b/packages/libp2p-interface-compliance-tests/src/peer-discovery/index.ts index 08d9bc73e..edabc605c 100644 --- a/packages/libp2p-interface-compliance-tests/src/peer-discovery/index.ts +++ b/packages/libp2p-interface-compliance-tests/src/peer-discovery/index.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { Multiaddr } from '@multiformats/multiaddr' import delay from 'delay' import pDefer from 'p-defer' diff --git a/packages/libp2p-interface-compliance-tests/src/pubsub/api.ts b/packages/libp2p-interface-compliance-tests/src/pubsub/api.ts index 32ee3e9c1..afdfaba45 100644 --- a/packages/libp2p-interface-compliance-tests/src/pubsub/api.ts +++ b/packages/libp2p-interface-compliance-tests/src/pubsub/api.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pDefer from 'p-defer' import pWaitFor from 'p-wait-for' diff --git a/packages/libp2p-interface-compliance-tests/src/pubsub/connection-handlers.ts b/packages/libp2p-interface-compliance-tests/src/pubsub/connection-handlers.ts index c3fb5b935..ac10b440e 100644 --- a/packages/libp2p-interface-compliance-tests/src/pubsub/connection-handlers.ts +++ b/packages/libp2p-interface-compliance-tests/src/pubsub/connection-handlers.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pDefer from 'p-defer' import pWaitFor from 'p-wait-for' diff --git a/packages/libp2p-interface-compliance-tests/src/pubsub/emit-self.ts b/packages/libp2p-interface-compliance-tests/src/pubsub/emit-self.ts index 00cc3bacb..d3412a113 100644 --- a/packages/libp2p-interface-compliance-tests/src/pubsub/emit-self.ts +++ b/packages/libp2p-interface-compliance-tests/src/pubsub/emit-self.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { createEd25519PeerId } from '@libp2p/peer-id-factory' diff --git a/packages/libp2p-interface-compliance-tests/src/pubsub/messages.ts b/packages/libp2p-interface-compliance-tests/src/pubsub/messages.ts index 91e6bfc50..6954b9ddf 100644 --- a/packages/libp2p-interface-compliance-tests/src/pubsub/messages.ts +++ b/packages/libp2p-interface-compliance-tests/src/pubsub/messages.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' diff --git a/packages/libp2p-interface-compliance-tests/src/pubsub/multiple-nodes.ts b/packages/libp2p-interface-compliance-tests/src/pubsub/multiple-nodes.ts index e564d934e..661febee0 100644 --- a/packages/libp2p-interface-compliance-tests/src/pubsub/multiple-nodes.ts +++ b/packages/libp2p-interface-compliance-tests/src/pubsub/multiple-nodes.ts @@ -1,5 +1,5 @@ /* eslint max-nested-callbacks: ["error", 6] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pDefer from 'p-defer' import pWaitFor from 'p-wait-for' diff --git a/packages/libp2p-interface-compliance-tests/src/pubsub/two-nodes.ts b/packages/libp2p-interface-compliance-tests/src/pubsub/two-nodes.ts index d55b1d20e..947b1b233 100644 --- a/packages/libp2p-interface-compliance-tests/src/pubsub/two-nodes.ts +++ b/packages/libp2p-interface-compliance-tests/src/pubsub/two-nodes.ts @@ -1,5 +1,5 @@ /* eslint max-nested-callbacks: ["error", 6] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pDefer from 'p-defer' import pWaitFor from 'p-wait-for' diff --git a/packages/libp2p-interface-compliance-tests/src/record/index.ts b/packages/libp2p-interface-compliance-tests/src/record/index.ts index a5729a4c6..dd3d169a7 100644 --- a/packages/libp2p-interface-compliance-tests/src/record/index.ts +++ b/packages/libp2p-interface-compliance-tests/src/record/index.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import type { TestSetup } from '../index.js' import type { Record } from '@libp2p/interfaces/record' 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 988826ef3..b14113519 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 @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { duplexPair } from 'it-pair/duplex' import { pipe } from 'it-pipe' import drain from 'it-drain' 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 543fb817d..c07c45335 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 @@ -4,7 +4,7 @@ import { duplexPair } from 'it-pair/duplex' import { abortableSource } from 'abortable-iterator' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import drain from 'it-drain' -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import delay from 'delay' import type { TestSetup } from '../index.js' import type { StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer' 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 9fefcf56b..9a6816824 100644 --- a/packages/libp2p-interface-compliance-tests/src/stream-muxer/spawner.ts +++ b/packages/libp2p-interface-compliance-tests/src/stream-muxer/spawner.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { duplexPair } from 'it-pair/duplex' import { pipe } from 'it-pipe' import pLimit from 'p-limit' diff --git a/packages/libp2p-interface-compliance-tests/src/topology/topology.ts b/packages/libp2p-interface-compliance-tests/src/topology/topology.ts index 280fbe02d..a4e14066d 100644 --- a/packages/libp2p-interface-compliance-tests/src/topology/topology.ts +++ b/packages/libp2p-interface-compliance-tests/src/topology/topology.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import type { TestSetup } from '../index.js' import type { Topology } from '@libp2p/interfaces/topology' diff --git a/packages/libp2p-interface-compliance-tests/src/transport/dial-test.ts b/packages/libp2p-interface-compliance-tests/src/transport/dial-test.ts index f9383d46b..65fec3381 100644 --- a/packages/libp2p-interface-compliance-tests/src/transport/dial-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/transport/dial-test.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { isValidTick } from '../utils/is-valid-tick.js' import { mockUpgrader } from '../mocks/upgrader.js' import { mockRegistrar } from '../mocks/registrar.js' diff --git a/packages/libp2p-interface-compliance-tests/src/transport/filter-test.ts b/packages/libp2p-interface-compliance-tests/src/transport/filter-test.ts index f04bc8e74..83eb16685 100644 --- a/packages/libp2p-interface-compliance-tests/src/transport/filter-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/transport/filter-test.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import type { TestSetup } from '../index.js' import type { Transport } from '@libp2p/interfaces/transport' import type { TransportTestFixtures } from './index.js' diff --git a/packages/libp2p-interface-compliance-tests/src/transport/listen-test.ts b/packages/libp2p-interface-compliance-tests/src/transport/listen-test.ts index dbca7a359..06e5cf282 100644 --- a/packages/libp2p-interface-compliance-tests/src/transport/listen-test.ts +++ b/packages/libp2p-interface-compliance-tests/src/transport/listen-test.ts @@ -1,5 +1,5 @@ /* eslint max-nested-callbacks: ["error", 8] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pWaitFor from 'p-wait-for' import { pipe } from 'it-pipe' diff --git a/packages/libp2p-interface-compliance-tests/tsconfig.json b/packages/libp2p-interface-compliance-tests/tsconfig.json index 25567206d..37d90abfc 100644 --- a/packages/libp2p-interface-compliance-tests/tsconfig.json +++ b/packages/libp2p-interface-compliance-tests/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-interfaces/package.json b/packages/libp2p-interfaces/package.json index b5833dea9..f72b7a48b 100644 --- a/packages/libp2p-interfaces/package.json +++ b/packages/libp2p-interfaces/package.json @@ -221,16 +221,19 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc" + "dep-check": "aegir dep-check", + "build": "aegir build", + "release": "aegir release" }, "dependencies": { "@multiformats/multiaddr": "^10.1.5", + "err-code": "^3.0.1", "interface-datastore": "^6.1.0", "multiformats": "^9.6.3" }, "devDependencies": { - "aegir": "^36.1.3" + "aegir": "^37.0.6" } } diff --git a/packages/libp2p-interfaces/src/pubsub/index.ts b/packages/libp2p-interfaces/src/pubsub/index.ts index 074381d03..1ff870d83 100644 --- a/packages/libp2p-interfaces/src/pubsub/index.ts +++ b/packages/libp2p-interfaces/src/pubsub/index.ts @@ -35,22 +35,22 @@ export interface Message { } export interface PubSubRPCMessage { - from?: Uint8Array | null - topic?: string | null - data?: Uint8Array | null - sequenceNumber?: Uint8Array | null - signature?: Uint8Array | null - key?: Uint8Array | null + from?: Uint8Array + topic?: string + data?: Uint8Array + sequenceNumber?: Uint8Array + signature?: Uint8Array + key?: Uint8Array } export interface PubSubRPCSubscription { - subscribe?: boolean | null - topic?: string | null + subscribe?: boolean + topic?: string } export interface PubSubRPC { - subscriptions?: PubSubRPCSubscription[] | null - messages?: PubSubRPCMessage[] | null + subscriptions: PubSubRPCSubscription[] + messages: PubSubRPCMessage[] } export interface PeerStreams extends EventEmitter { diff --git a/packages/libp2p-interfaces/tsconfig.json b/packages/libp2p-interfaces/tsconfig.json index 5b4acb638..5fe8ea40d 100644 --- a/packages/libp2p-interfaces/tsconfig.json +++ b/packages/libp2p-interfaces/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src" diff --git a/packages/libp2p-logger/package.json b/packages/libp2p-logger/package.json index b0cc4d731..3181d9a81 100644 --- a/packages/libp2p-logger/package.json +++ b/packages/libp2p-logger/package.json @@ -119,17 +119,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0", @@ -139,6 +140,6 @@ }, "devDependencies": { "@types/debug": "^4.1.7", - "aegir": "^36.1.3" + "aegir": "^37.0.6" } } diff --git a/packages/libp2p-logger/test/index.spec.ts b/packages/libp2p-logger/test/index.spec.ts index 4ea761a85..71d2b4822 100644 --- a/packages/libp2p-logger/test/index.spec.ts +++ b/packages/libp2p-logger/test/index.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { logger } from '../src/index.js' describe('logger', () => { diff --git a/packages/libp2p-logger/tsconfig.json b/packages/libp2p-logger/tsconfig.json index 4b68f4ffa..d59d66e96 100644 --- a/packages/libp2p-logger/tsconfig.json +++ b/packages/libp2p-logger/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-multistream-select/package.json b/packages/libp2p-multistream-select/package.json index c7c2959ff..9fad2f17c 100644 --- a/packages/libp2p-multistream-select/package.json +++ b/packages/libp2p-multistream-select/package.json @@ -123,17 +123,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0", @@ -152,7 +153,7 @@ "uint8arrays": "^3.0.0" }, "devDependencies": { - "aegir": "^36.1.3", + "aegir": "^37.0.6", "iso-random-stream": "^2.0.2", "it-all": "^1.0.6", "it-map": "^1.0.6", diff --git a/packages/libp2p-multistream-select/test/dialer.spec.ts b/packages/libp2p-multistream-select/test/dialer.spec.ts index 73af873af..04a8cf1e4 100644 --- a/packages/libp2p-multistream-select/test/dialer.spec.ts +++ b/packages/libp2p-multistream-select/test/dialer.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 5] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { pipe } from 'it-pipe' import all from 'it-all' import { Uint8ArrayList } from 'uint8arraylist' diff --git a/packages/libp2p-multistream-select/test/integration.spec.ts b/packages/libp2p-multistream-select/test/integration.spec.ts index e949c9d05..a41682bd3 100644 --- a/packages/libp2p-multistream-select/test/integration.spec.ts +++ b/packages/libp2p-multistream-select/test/integration.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { pipe } from 'it-pipe' import all from 'it-all' import { Uint8ArrayList } from 'uint8arraylist' diff --git a/packages/libp2p-multistream-select/test/listener.spec.ts b/packages/libp2p-multistream-select/test/listener.spec.ts index cf0bf6fe9..907278d22 100644 --- a/packages/libp2p-multistream-select/test/listener.spec.ts +++ b/packages/libp2p-multistream-select/test/listener.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { pipe } from 'it-pipe' import { Uint8ArrayList } from 'uint8arraylist' import { reader } from 'it-reader' diff --git a/packages/libp2p-multistream-select/test/multistream.spec.ts b/packages/libp2p-multistream-select/test/multistream.spec.ts index 1b0de43e4..3efd823a3 100644 --- a/packages/libp2p-multistream-select/test/multistream.spec.ts +++ b/packages/libp2p-multistream-select/test/multistream.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import * as Varint from 'varint' import { Uint8ArrayList } from 'uint8arraylist' import { reader } from 'it-reader' diff --git a/packages/libp2p-multistream-select/tsconfig.json b/packages/libp2p-multistream-select/tsconfig.json index 738ecdd06..d96dd69f8 100644 --- a/packages/libp2p-multistream-select/tsconfig.json +++ b/packages/libp2p-multistream-select/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-peer-collections/package.json b/packages/libp2p-peer-collections/package.json index d8e8c7a87..0ac3cd21b 100644 --- a/packages/libp2p-peer-collections/package.json +++ b/packages/libp2p-peer-collections/package.json @@ -119,17 +119,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0" @@ -137,6 +138,6 @@ "devDependencies": { "@libp2p/peer-id": "^1.1.0", "@libp2p/peer-id-factory": "^1.0.0", - "aegir": "^36.1.3" + "aegir": "^37.0.6" } } diff --git a/packages/libp2p-peer-collections/test/list.spec.ts b/packages/libp2p-peer-collections/test/list.spec.ts index fc28af1f0..a586aabbe 100644 --- a/packages/libp2p-peer-collections/test/list.spec.ts +++ b/packages/libp2p-peer-collections/test/list.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { PeerList } from '../src/index.js' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { peerIdFromBytes } from '@libp2p/peer-id' diff --git a/packages/libp2p-peer-collections/test/map.spec.ts b/packages/libp2p-peer-collections/test/map.spec.ts index 71895563d..f1d4ac9c2 100644 --- a/packages/libp2p-peer-collections/test/map.spec.ts +++ b/packages/libp2p-peer-collections/test/map.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { PeerMap } from '../src/index.js' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { peerIdFromBytes } from '@libp2p/peer-id' diff --git a/packages/libp2p-peer-collections/test/set.spec.ts b/packages/libp2p-peer-collections/test/set.spec.ts index a7f810d0d..b1efeb008 100644 --- a/packages/libp2p-peer-collections/test/set.spec.ts +++ b/packages/libp2p-peer-collections/test/set.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { PeerSet } from '../src/index.js' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { peerIdFromBytes } from '@libp2p/peer-id' diff --git a/packages/libp2p-peer-collections/tsconfig.json b/packages/libp2p-peer-collections/tsconfig.json index 800317aa7..7a7d79b82 100644 --- a/packages/libp2p-peer-collections/tsconfig.json +++ b/packages/libp2p-peer-collections/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-peer-id-factory/package.json b/packages/libp2p-peer-id-factory/package.json index ee98faa89..6193f3345 100644 --- a/packages/libp2p-peer-id-factory/package.json +++ b/packages/libp2p-peer-id-factory/package.json @@ -123,31 +123,30 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main", - "build:proto": "npm run build:proto:js && npm run build:proto:types", - "build:proto:js": "pbjs -t static-module -w es6 --es6 -r libp2p-peer-id --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/proto.js ./src/proto.proto", - "build:proto:types": "pbts -o src/proto.d.ts src/proto.js" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/crypto": "^0.22.8", "@libp2p/interfaces": "^1.3.0", "@libp2p/peer-id": "^1.1.0", "multiformats": "^9.6.3", - "protobufjs": "^6.11.2", + "protons-runtime": "^1.0.2", "uint8arrays": "^3.0.0" }, "devDependencies": { - "aegir": "^36.1.3", + "aegir": "^37.0.6", + "protons": "^3.0.2", "util": "^0.12.4" } } diff --git a/packages/libp2p-peer-id-factory/src/index.ts b/packages/libp2p-peer-id-factory/src/index.ts index 35c4aba25..9886840c9 100644 --- a/packages/libp2p-peer-id-factory/src/index.ts +++ b/packages/libp2p-peer-id-factory/src/index.ts @@ -51,7 +51,7 @@ export function exportToProtobuf (peerId: RSAPeerId | Ed25519PeerId | Secp256k1P id: peerId.multihash.bytes, pubKey: peerId.publicKey, privKey: excludePrivateKey === true || peerId.privateKey == null ? undefined : peerId.privateKey - }).finish() + }) } export async function createFromProtobuf (buf: Uint8Array) { diff --git a/packages/libp2p-peer-id-factory/src/proto.d.ts b/packages/libp2p-peer-id-factory/src/proto.d.ts deleted file mode 100644 index ad307931a..000000000 --- a/packages/libp2p-peer-id-factory/src/proto.d.ts +++ /dev/null @@ -1,71 +0,0 @@ -import * as $protobuf from "protobufjs"; -/** Properties of a PeerIdProto. */ -export interface IPeerIdProto { - - /** PeerIdProto id */ - id: Uint8Array; - - /** PeerIdProto pubKey */ - pubKey?: (Uint8Array|null); - - /** PeerIdProto privKey */ - privKey?: (Uint8Array|null); -} - -/** Represents a PeerIdProto. */ -export class PeerIdProto implements IPeerIdProto { - - /** - * Constructs a new PeerIdProto. - * @param [p] Properties to set - */ - constructor(p?: IPeerIdProto); - - /** PeerIdProto id. */ - public id: Uint8Array; - - /** PeerIdProto pubKey. */ - public pubKey: Uint8Array; - - /** PeerIdProto privKey. */ - public privKey: Uint8Array; - - /** - * Encodes the specified PeerIdProto message. Does not implicitly {@link PeerIdProto.verify|verify} messages. - * @param m PeerIdProto message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IPeerIdProto, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a PeerIdProto message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns PeerIdProto - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerIdProto; - - /** - * Creates a PeerIdProto message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns PeerIdProto - */ - public static fromObject(d: { [k: string]: any }): PeerIdProto; - - /** - * Creates a plain object from a PeerIdProto message. Also converts values to other types if specified. - * @param m PeerIdProto - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: PeerIdProto, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this PeerIdProto to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} diff --git a/packages/libp2p-peer-id-factory/src/proto.js b/packages/libp2p-peer-id-factory/src/proto.js deleted file mode 100644 index 98c6b39fc..000000000 --- a/packages/libp2p-peer-id-factory/src/proto.js +++ /dev/null @@ -1,207 +0,0 @@ -// @ts-nocheck - -/*eslint-disable*/ -import $protobuf from "protobufjs/minimal.js"; - -// Common aliases -const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; - -// Exported root namespace -const $root = $protobuf.roots["libp2p-peer-id"] || ($protobuf.roots["libp2p-peer-id"] = {}); - -/** - * Properties of a PeerIdProto. - * @exports IPeerIdProto - * @interface IPeerIdProto - * @property {Uint8Array} id PeerIdProto id - * @property {Uint8Array|null} [pubKey] PeerIdProto pubKey - * @property {Uint8Array|null} [privKey] PeerIdProto privKey - */ - -/** - * Constructs a new PeerIdProto. - * @exports PeerIdProto - * @classdesc Represents a PeerIdProto. - * @implements IPeerIdProto - * @constructor - * @param {IPeerIdProto=} [p] Properties to set - */ -export function PeerIdProto(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; -} - -/** - * PeerIdProto id. - * @member {Uint8Array} id - * @memberof PeerIdProto - * @instance - */ -PeerIdProto.prototype.id = $util.newBuffer([]); - -/** - * PeerIdProto pubKey. - * @member {Uint8Array} pubKey - * @memberof PeerIdProto - * @instance - */ -PeerIdProto.prototype.pubKey = $util.newBuffer([]); - -/** - * PeerIdProto privKey. - * @member {Uint8Array} privKey - * @memberof PeerIdProto - * @instance - */ -PeerIdProto.prototype.privKey = $util.newBuffer([]); - -/** - * Encodes the specified PeerIdProto message. Does not implicitly {@link PeerIdProto.verify|verify} messages. - * @function encode - * @memberof PeerIdProto - * @static - * @param {IPeerIdProto} m PeerIdProto message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ -PeerIdProto.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - w.uint32(10).bytes(m.id); - if (m.pubKey != null && Object.hasOwnProperty.call(m, "pubKey")) - w.uint32(18).bytes(m.pubKey); - if (m.privKey != null && Object.hasOwnProperty.call(m, "privKey")) - w.uint32(26).bytes(m.privKey); - return w; -}; - -/** - * Decodes a PeerIdProto message from the specified reader or buffer. - * @function decode - * @memberof PeerIdProto - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {PeerIdProto} PeerIdProto - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ -PeerIdProto.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new PeerIdProto(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.id = r.bytes(); - break; - case 2: - m.pubKey = r.bytes(); - break; - case 3: - m.privKey = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - if (!m.hasOwnProperty("id")) - throw $util.ProtocolError("missing required 'id'", { instance: m }); - return m; -}; - -/** - * Creates a PeerIdProto message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof PeerIdProto - * @static - * @param {Object.} d Plain object - * @returns {PeerIdProto} PeerIdProto - */ -PeerIdProto.fromObject = function fromObject(d) { - if (d instanceof PeerIdProto) - return d; - var m = new PeerIdProto(); - if (d.id != null) { - if (typeof d.id === "string") - $util.base64.decode(d.id, m.id = $util.newBuffer($util.base64.length(d.id)), 0); - else if (d.id.length) - m.id = d.id; - } - if (d.pubKey != null) { - if (typeof d.pubKey === "string") - $util.base64.decode(d.pubKey, m.pubKey = $util.newBuffer($util.base64.length(d.pubKey)), 0); - else if (d.pubKey.length) - m.pubKey = d.pubKey; - } - if (d.privKey != null) { - if (typeof d.privKey === "string") - $util.base64.decode(d.privKey, m.privKey = $util.newBuffer($util.base64.length(d.privKey)), 0); - else if (d.privKey.length) - m.privKey = d.privKey; - } - return m; -}; - -/** - * Creates a plain object from a PeerIdProto message. Also converts values to other types if specified. - * @function toObject - * @memberof PeerIdProto - * @static - * @param {PeerIdProto} m PeerIdProto - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ -PeerIdProto.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.defaults) { - if (o.bytes === String) - d.id = ""; - else { - d.id = []; - if (o.bytes !== Array) - d.id = $util.newBuffer(d.id); - } - if (o.bytes === String) - d.pubKey = ""; - else { - d.pubKey = []; - if (o.bytes !== Array) - d.pubKey = $util.newBuffer(d.pubKey); - } - if (o.bytes === String) - d.privKey = ""; - else { - d.privKey = []; - if (o.bytes !== Array) - d.privKey = $util.newBuffer(d.privKey); - } - } - if (m.id != null && m.hasOwnProperty("id")) { - d.id = o.bytes === String ? $util.base64.encode(m.id, 0, m.id.length) : o.bytes === Array ? Array.prototype.slice.call(m.id) : m.id; - } - if (m.pubKey != null && m.hasOwnProperty("pubKey")) { - d.pubKey = o.bytes === String ? $util.base64.encode(m.pubKey, 0, m.pubKey.length) : o.bytes === Array ? Array.prototype.slice.call(m.pubKey) : m.pubKey; - } - if (m.privKey != null && m.hasOwnProperty("privKey")) { - d.privKey = o.bytes === String ? $util.base64.encode(m.privKey, 0, m.privKey.length) : o.bytes === Array ? Array.prototype.slice.call(m.privKey) : m.privKey; - } - return d; -}; - -/** - * Converts this PeerIdProto to JSON. - * @function toJSON - * @memberof PeerIdProto - * @instance - * @returns {Object.} JSON object - */ -PeerIdProto.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); -}; diff --git a/packages/libp2p-peer-id-factory/src/proto.proto b/packages/libp2p-peer-id-factory/src/proto.proto index 6d0c41485..989958a1e 100644 --- a/packages/libp2p-peer-id-factory/src/proto.proto +++ b/packages/libp2p-peer-id-factory/src/proto.proto @@ -2,6 +2,6 @@ syntax = "proto3"; message PeerIdProto { required bytes id = 1; - bytes pubKey = 2; - bytes privKey = 3; + optional bytes pubKey = 2; + optional bytes privKey = 3; } diff --git a/packages/libp2p-peer-id-factory/src/proto.ts b/packages/libp2p-peer-id-factory/src/proto.ts new file mode 100644 index 000000000..618196e76 --- /dev/null +++ b/packages/libp2p-peer-id-factory/src/proto.ts @@ -0,0 +1,28 @@ +/* eslint-disable import/export */ +/* eslint-disable @typescript-eslint/no-namespace */ + +import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime' + +export interface PeerIdProto { + id: Uint8Array + pubKey?: Uint8Array + privKey?: Uint8Array +} + +export namespace PeerIdProto { + export const codec = () => { + return message({ + 1: { name: 'id', codec: bytes }, + 2: { name: 'pubKey', codec: bytes, optional: true }, + 3: { name: 'privKey', codec: bytes, optional: true } + }) + } + + export const encode = (obj: PeerIdProto): Uint8Array => { + return encodeMessage(obj, PeerIdProto.codec()) + } + + export const decode = (buf: Uint8Array): PeerIdProto => { + return decodeMessage(buf, PeerIdProto.codec()) + } +} diff --git a/packages/libp2p-peer-id-factory/test/index.spec.ts b/packages/libp2p-peer-id-factory/test/index.spec.ts index 50d45759d..b699922f4 100644 --- a/packages/libp2p-peer-id-factory/test/index.spec.ts +++ b/packages/libp2p-peer-id-factory/test/index.spec.ts @@ -1,7 +1,7 @@ /* eslint max-nested-callbacks: ["error", 8] */ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { keys } from '@libp2p/crypto' import { CID } from 'multiformats/cid' import * as Digest from 'multiformats/hashes/digest' diff --git a/packages/libp2p-peer-id-factory/tsconfig.json b/packages/libp2p-peer-id-factory/tsconfig.json index d9a1f60f4..a58dc440b 100644 --- a/packages/libp2p-peer-id-factory/tsconfig.json +++ b/packages/libp2p-peer-id-factory/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-peer-id/package.json b/packages/libp2p-peer-id/package.json index 42bbf77a6..d5fd832db 100644 --- a/packages/libp2p-peer-id/package.json +++ b/packages/libp2p-peer-id/package.json @@ -119,17 +119,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0", @@ -138,6 +139,6 @@ "uint8arrays": "^3.0.0" }, "devDependencies": { - "aegir": "^36.1.3" + "aegir": "^37.0.6" } } diff --git a/packages/libp2p-peer-id/test/index.spec.js b/packages/libp2p-peer-id/test/index.spec.ts similarity index 98% rename from packages/libp2p-peer-id/test/index.spec.js rename to packages/libp2p-peer-id/test/index.spec.ts index e351cdd4a..e218e488b 100644 --- a/packages/libp2p-peer-id/test/index.spec.js +++ b/packages/libp2p-peer-id/test/index.spec.ts @@ -1,5 +1,5 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { createPeerId, peerIdFromBytes, peerIdFromString } from '../src/index.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' diff --git a/packages/libp2p-peer-id/tsconfig.json b/packages/libp2p-peer-id/tsconfig.json index 4b68f4ffa..d59d66e96 100644 --- a/packages/libp2p-peer-id/tsconfig.json +++ b/packages/libp2p-peer-id/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-peer-record/package.json b/packages/libp2p-peer-record/package.json index f14c842ea..4f97a90c7 100644 --- a/packages/libp2p-peer-record/package.json +++ b/packages/libp2p-peer-record/package.json @@ -125,23 +125,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc && npm run build:copy-proto-files", - "generate": "npm run generate:envelope && npm run generate:envelope-types && npm run generate:peer-record && npm run generate:peer-record-types", - "generate:envelope": "pbjs -t static-module -w es6 -r libp2p-peer-record-envelope --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/envelope/envelope.js ./src/envelope/envelope.proto", - "generate:envelope-types": "pbts -o src/envelope/envelope.d.ts src/envelope/envelope.js", - "generate:peer-record": "pbjs -t static-module -w es6 -r libp2p-peer-record --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/peer-record/peer-record.js ./src/peer-record/peer-record.proto", - "generate:peer-record-types": "pbts -o src/peer-record/peer-record.d.ts src/peer-record/peer-record.js", - "build:copy-proto-files": "cp src/envelope/envelope.js src/envelope/*.d.ts dist/src/envelope && cp src/peer-record/peer-record.js src/peer-record/*.d.ts dist/src/peer-record", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/crypto": "^0.22.8", @@ -158,7 +153,7 @@ "it-map": "^1.0.6", "it-pipe": "^2.0.3", "multiformats": "^9.6.3", - "protobufjs": "^6.11.2", + "protons-runtime": "^1.0.2", "uint8arrays": "^3.0.0", "varint": "^6.0.0" }, @@ -166,7 +161,8 @@ "@libp2p/interface-compliance-tests": "^1.1.0", "@libp2p/peer-id-factory": "^1.0.0", "@types/varint": "^6.0.0", - "aegir": "^36.1.3", + "aegir": "^37.0.6", + "protons": "^3.0.2", "sinon": "^13.0.1" } } diff --git a/packages/libp2p-peer-record/src/envelope/envelope.d.ts b/packages/libp2p-peer-record/src/envelope/envelope.d.ts deleted file mode 100644 index 440590c14..000000000 --- a/packages/libp2p-peer-record/src/envelope/envelope.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as $protobuf from "protobufjs"; -/** Properties of an Envelope. */ -export interface IEnvelope { - - /** Envelope publicKey */ - publicKey?: (Uint8Array|null); - - /** Envelope payloadType */ - payloadType?: (Uint8Array|null); - - /** Envelope payload */ - payload?: (Uint8Array|null); - - /** Envelope signature */ - signature?: (Uint8Array|null); -} - -/** Represents an Envelope. */ -export class Envelope implements IEnvelope { - - /** - * Constructs a new Envelope. - * @param [p] Properties to set - */ - constructor(p?: IEnvelope); - - /** Envelope publicKey. */ - public publicKey: Uint8Array; - - /** Envelope payloadType. */ - public payloadType: Uint8Array; - - /** Envelope payload. */ - public payload: Uint8Array; - - /** Envelope signature. */ - public signature: Uint8Array; - - /** - * Encodes the specified Envelope message. Does not implicitly {@link Envelope.verify|verify} messages. - * @param m Envelope message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IEnvelope, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes an Envelope message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns Envelope - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Envelope; - - /** - * Creates an Envelope message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns Envelope - */ - public static fromObject(d: { [k: string]: any }): Envelope; - - /** - * Creates a plain object from an Envelope message. Also converts values to other types if specified. - * @param m Envelope - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: Envelope, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this Envelope to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} diff --git a/packages/libp2p-peer-record/src/envelope/envelope.js b/packages/libp2p-peer-record/src/envelope/envelope.js deleted file mode 100644 index 9a21dc901..000000000 --- a/packages/libp2p-peer-record/src/envelope/envelope.js +++ /dev/null @@ -1,241 +0,0 @@ -/*eslint-disable*/ -import $protobuf from "protobufjs/minimal.js"; - -// Common aliases -const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; - -// Exported root namespace -const $root = $protobuf.roots["libp2p-peer-record-envelope"] || ($protobuf.roots["libp2p-peer-record-envelope"] = {}); - -export const Envelope = $root.Envelope = (() => { - - /** - * Properties of an Envelope. - * @exports IEnvelope - * @interface IEnvelope - * @property {Uint8Array|null} [publicKey] Envelope publicKey - * @property {Uint8Array|null} [payloadType] Envelope payloadType - * @property {Uint8Array|null} [payload] Envelope payload - * @property {Uint8Array|null} [signature] Envelope signature - */ - - /** - * Constructs a new Envelope. - * @exports Envelope - * @classdesc Represents an Envelope. - * @implements IEnvelope - * @constructor - * @param {IEnvelope=} [p] Properties to set - */ - function Envelope(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * Envelope publicKey. - * @member {Uint8Array} publicKey - * @memberof Envelope - * @instance - */ - Envelope.prototype.publicKey = $util.newBuffer([]); - - /** - * Envelope payloadType. - * @member {Uint8Array} payloadType - * @memberof Envelope - * @instance - */ - Envelope.prototype.payloadType = $util.newBuffer([]); - - /** - * Envelope payload. - * @member {Uint8Array} payload - * @memberof Envelope - * @instance - */ - Envelope.prototype.payload = $util.newBuffer([]); - - /** - * Envelope signature. - * @member {Uint8Array} signature - * @memberof Envelope - * @instance - */ - Envelope.prototype.signature = $util.newBuffer([]); - - /** - * Encodes the specified Envelope message. Does not implicitly {@link Envelope.verify|verify} messages. - * @function encode - * @memberof Envelope - * @static - * @param {IEnvelope} m Envelope message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - Envelope.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.publicKey != null && Object.hasOwnProperty.call(m, "publicKey")) - w.uint32(10).bytes(m.publicKey); - if (m.payloadType != null && Object.hasOwnProperty.call(m, "payloadType")) - w.uint32(18).bytes(m.payloadType); - if (m.payload != null && Object.hasOwnProperty.call(m, "payload")) - w.uint32(26).bytes(m.payload); - if (m.signature != null && Object.hasOwnProperty.call(m, "signature")) - w.uint32(42).bytes(m.signature); - return w; - }; - - /** - * Decodes an Envelope message from the specified reader or buffer. - * @function decode - * @memberof Envelope - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {Envelope} Envelope - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - Envelope.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.Envelope(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.publicKey = r.bytes(); - break; - case 2: - m.payloadType = r.bytes(); - break; - case 3: - m.payload = r.bytes(); - break; - case 5: - m.signature = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates an Envelope message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof Envelope - * @static - * @param {Object.} d Plain object - * @returns {Envelope} Envelope - */ - Envelope.fromObject = function fromObject(d) { - if (d instanceof $root.Envelope) - return d; - var m = new $root.Envelope(); - if (d.publicKey != null) { - if (typeof d.publicKey === "string") - $util.base64.decode(d.publicKey, m.publicKey = $util.newBuffer($util.base64.length(d.publicKey)), 0); - else if (d.publicKey.length) - m.publicKey = d.publicKey; - } - if (d.payloadType != null) { - if (typeof d.payloadType === "string") - $util.base64.decode(d.payloadType, m.payloadType = $util.newBuffer($util.base64.length(d.payloadType)), 0); - else if (d.payloadType.length) - m.payloadType = d.payloadType; - } - if (d.payload != null) { - if (typeof d.payload === "string") - $util.base64.decode(d.payload, m.payload = $util.newBuffer($util.base64.length(d.payload)), 0); - else if (d.payload.length) - m.payload = d.payload; - } - if (d.signature != null) { - if (typeof d.signature === "string") - $util.base64.decode(d.signature, m.signature = $util.newBuffer($util.base64.length(d.signature)), 0); - else if (d.signature.length) - m.signature = d.signature; - } - return m; - }; - - /** - * Creates a plain object from an Envelope message. Also converts values to other types if specified. - * @function toObject - * @memberof Envelope - * @static - * @param {Envelope} m Envelope - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - Envelope.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.defaults) { - if (o.bytes === String) - d.publicKey = ""; - else { - d.publicKey = []; - if (o.bytes !== Array) - d.publicKey = $util.newBuffer(d.publicKey); - } - if (o.bytes === String) - d.payloadType = ""; - else { - d.payloadType = []; - if (o.bytes !== Array) - d.payloadType = $util.newBuffer(d.payloadType); - } - if (o.bytes === String) - d.payload = ""; - else { - d.payload = []; - if (o.bytes !== Array) - d.payload = $util.newBuffer(d.payload); - } - if (o.bytes === String) - d.signature = ""; - else { - d.signature = []; - if (o.bytes !== Array) - d.signature = $util.newBuffer(d.signature); - } - } - if (m.publicKey != null && m.hasOwnProperty("publicKey")) { - d.publicKey = o.bytes === String ? $util.base64.encode(m.publicKey, 0, m.publicKey.length) : o.bytes === Array ? Array.prototype.slice.call(m.publicKey) : m.publicKey; - } - if (m.payloadType != null && m.hasOwnProperty("payloadType")) { - d.payloadType = o.bytes === String ? $util.base64.encode(m.payloadType, 0, m.payloadType.length) : o.bytes === Array ? Array.prototype.slice.call(m.payloadType) : m.payloadType; - } - if (m.payload != null && m.hasOwnProperty("payload")) { - d.payload = o.bytes === String ? $util.base64.encode(m.payload, 0, m.payload.length) : o.bytes === Array ? Array.prototype.slice.call(m.payload) : m.payload; - } - if (m.signature != null && m.hasOwnProperty("signature")) { - d.signature = o.bytes === String ? $util.base64.encode(m.signature, 0, m.signature.length) : o.bytes === Array ? Array.prototype.slice.call(m.signature) : m.signature; - } - return d; - }; - - /** - * Converts this Envelope to JSON. - * @function toJSON - * @memberof Envelope - * @instance - * @returns {Object.} JSON object - */ - Envelope.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return Envelope; -})(); - -export { $root as default }; diff --git a/packages/libp2p-peer-record/src/envelope/envelope.ts b/packages/libp2p-peer-record/src/envelope/envelope.ts new file mode 100644 index 000000000..f725a5a10 --- /dev/null +++ b/packages/libp2p-peer-record/src/envelope/envelope.ts @@ -0,0 +1,30 @@ +/* eslint-disable import/export */ +/* eslint-disable @typescript-eslint/no-namespace */ + +import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime' + +export interface Envelope { + publicKey: Uint8Array + payloadType: Uint8Array + payload: Uint8Array + signature: Uint8Array +} + +export namespace Envelope { + export const codec = () => { + return message({ + 1: { name: 'publicKey', codec: bytes }, + 2: { name: 'payloadType', codec: bytes }, + 3: { name: 'payload', codec: bytes }, + 5: { name: 'signature', codec: bytes } + }) + } + + export const encode = (obj: Envelope): Uint8Array => { + return encodeMessage(obj, Envelope.codec()) + } + + export const decode = (buf: Uint8Array): Envelope => { + return decodeMessage(buf, Envelope.codec()) + } +} diff --git a/packages/libp2p-peer-record/src/envelope/index.ts b/packages/libp2p-peer-record/src/envelope/index.ts index f3a30954f..f76095dfe 100644 --- a/packages/libp2p-peer-record/src/envelope/index.ts +++ b/packages/libp2p-peer-record/src/envelope/index.ts @@ -97,13 +97,17 @@ export class RecordEnvelope implements Envelope { * Marshal the envelope content */ marshal () { + if (this.peerId.publicKey == null) { + throw new Error('Missing public key') + } + if (this.marshaled == null) { this.marshaled = Protobuf.encode({ publicKey: this.peerId.publicKey, payloadType: this.payloadType, payload: this.payload, signature: this.signature - }).finish() + }) } return this.marshaled diff --git a/packages/libp2p-peer-record/src/peer-record/index.ts b/packages/libp2p-peer-record/src/peer-record/index.ts index ef0787e25..57ab0e6da 100644 --- a/packages/libp2p-peer-record/src/peer-record/index.ts +++ b/packages/libp2p-peer-record/src/peer-record/index.ts @@ -19,7 +19,7 @@ export interface PeerRecordInit { /** * Monotonically-increasing sequence counter that's used to order PeerRecords in time. */ - seqNumber?: number + seqNumber?: bigint } /** @@ -34,7 +34,7 @@ export class PeerRecord { const peerRecord = Protobuf.decode(buf) const peerId = peerIdFromBytes(peerRecord.peerId) const multiaddrs = (peerRecord.addresses ?? []).map((a) => new Multiaddr(a.multiaddr)) - const seqNumber = Number(peerRecord.seq) + const seqNumber = peerRecord.seq return new PeerRecord({ peerId, multiaddrs, seqNumber }) } @@ -44,7 +44,7 @@ export class PeerRecord { public peerId: PeerId public multiaddrs: Multiaddr[] - public seqNumber: number + public seqNumber: bigint public domain = PeerRecord.DOMAIN public codec = PeerRecord.CODEC private marshaled?: Uint8Array @@ -54,7 +54,7 @@ export class PeerRecord { this.peerId = peerId this.multiaddrs = multiaddrs ?? [] - this.seqNumber = seqNumber ?? Date.now() + this.seqNumber = seqNumber ?? BigInt(Date.now()) } /** @@ -64,11 +64,11 @@ export class PeerRecord { if (this.marshaled == null) { this.marshaled = Protobuf.encode({ peerId: this.peerId.toBytes(), - seq: this.seqNumber, + seq: BigInt(this.seqNumber), addresses: this.multiaddrs.map((m) => ({ multiaddr: m.bytes })) - }).finish() + }) } return this.marshaled diff --git a/packages/libp2p-peer-record/src/peer-record/peer-record.d.ts b/packages/libp2p-peer-record/src/peer-record/peer-record.d.ts deleted file mode 100644 index a851b5330..000000000 --- a/packages/libp2p-peer-record/src/peer-record/peer-record.d.ts +++ /dev/null @@ -1,133 +0,0 @@ -import * as $protobuf from "protobufjs"; -/** Properties of a PeerRecord. */ -export interface IPeerRecord { - - /** PeerRecord peerId */ - peerId?: (Uint8Array|null); - - /** PeerRecord seq */ - seq?: (number|null); - - /** PeerRecord addresses */ - addresses?: (PeerRecord.IAddressInfo[]|null); -} - -/** Represents a PeerRecord. */ -export class PeerRecord implements IPeerRecord { - - /** - * Constructs a new PeerRecord. - * @param [p] Properties to set - */ - constructor(p?: IPeerRecord); - - /** PeerRecord peerId. */ - public peerId: Uint8Array; - - /** PeerRecord seq. */ - public seq: number; - - /** PeerRecord addresses. */ - public addresses: PeerRecord.IAddressInfo[]; - - /** - * Encodes the specified PeerRecord message. Does not implicitly {@link PeerRecord.verify|verify} messages. - * @param m PeerRecord message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IPeerRecord, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a PeerRecord message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns PeerRecord - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerRecord; - - /** - * Creates a PeerRecord message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns PeerRecord - */ - public static fromObject(d: { [k: string]: any }): PeerRecord; - - /** - * Creates a plain object from a PeerRecord message. Also converts values to other types if specified. - * @param m PeerRecord - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: PeerRecord, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this PeerRecord to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -export namespace PeerRecord { - - /** Properties of an AddressInfo. */ - interface IAddressInfo { - - /** AddressInfo multiaddr */ - multiaddr?: (Uint8Array|null); - } - - /** Represents an AddressInfo. */ - class AddressInfo implements IAddressInfo { - - /** - * Constructs a new AddressInfo. - * @param [p] Properties to set - */ - constructor(p?: PeerRecord.IAddressInfo); - - /** AddressInfo multiaddr. */ - public multiaddr: Uint8Array; - - /** - * Encodes the specified AddressInfo message. Does not implicitly {@link PeerRecord.AddressInfo.verify|verify} messages. - * @param m AddressInfo message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: PeerRecord.IAddressInfo, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes an AddressInfo message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns AddressInfo - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerRecord.AddressInfo; - - /** - * Creates an AddressInfo message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns AddressInfo - */ - public static fromObject(d: { [k: string]: any }): PeerRecord.AddressInfo; - - /** - * Creates a plain object from an AddressInfo message. Also converts values to other types if specified. - * @param m AddressInfo - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: PeerRecord.AddressInfo, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this AddressInfo to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; - } -} diff --git a/packages/libp2p-peer-record/src/peer-record/peer-record.js b/packages/libp2p-peer-record/src/peer-record/peer-record.js deleted file mode 100644 index 6a05c202d..000000000 --- a/packages/libp2p-peer-record/src/peer-record/peer-record.js +++ /dev/null @@ -1,365 +0,0 @@ -/*eslint-disable*/ -import $protobuf from "protobufjs/minimal.js"; - -// Common aliases -const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; - -// Exported root namespace -const $root = $protobuf.roots["libp2p-peer-record"] || ($protobuf.roots["libp2p-peer-record"] = {}); - -export const PeerRecord = $root.PeerRecord = (() => { - - /** - * Properties of a PeerRecord. - * @exports IPeerRecord - * @interface IPeerRecord - * @property {Uint8Array|null} [peerId] PeerRecord peerId - * @property {number|null} [seq] PeerRecord seq - * @property {Array.|null} [addresses] PeerRecord addresses - */ - - /** - * Constructs a new PeerRecord. - * @exports PeerRecord - * @classdesc Represents a PeerRecord. - * @implements IPeerRecord - * @constructor - * @param {IPeerRecord=} [p] Properties to set - */ - function PeerRecord(p) { - this.addresses = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * PeerRecord peerId. - * @member {Uint8Array} peerId - * @memberof PeerRecord - * @instance - */ - PeerRecord.prototype.peerId = $util.newBuffer([]); - - /** - * PeerRecord seq. - * @member {number} seq - * @memberof PeerRecord - * @instance - */ - PeerRecord.prototype.seq = $util.Long ? $util.Long.fromBits(0,0,true) : 0; - - /** - * PeerRecord addresses. - * @member {Array.} addresses - * @memberof PeerRecord - * @instance - */ - PeerRecord.prototype.addresses = $util.emptyArray; - - /** - * Encodes the specified PeerRecord message. Does not implicitly {@link PeerRecord.verify|verify} messages. - * @function encode - * @memberof PeerRecord - * @static - * @param {IPeerRecord} m PeerRecord message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - PeerRecord.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.peerId != null && Object.hasOwnProperty.call(m, "peerId")) - w.uint32(10).bytes(m.peerId); - if (m.seq != null && Object.hasOwnProperty.call(m, "seq")) - w.uint32(16).uint64(m.seq); - if (m.addresses != null && m.addresses.length) { - for (var i = 0; i < m.addresses.length; ++i) - $root.PeerRecord.AddressInfo.encode(m.addresses[i], w.uint32(26).fork()).ldelim(); - } - return w; - }; - - /** - * Decodes a PeerRecord message from the specified reader or buffer. - * @function decode - * @memberof PeerRecord - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {PeerRecord} PeerRecord - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - PeerRecord.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.PeerRecord(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.peerId = r.bytes(); - break; - case 2: - m.seq = r.uint64(); - break; - case 3: - if (!(m.addresses && m.addresses.length)) - m.addresses = []; - m.addresses.push($root.PeerRecord.AddressInfo.decode(r, r.uint32())); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a PeerRecord message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof PeerRecord - * @static - * @param {Object.} d Plain object - * @returns {PeerRecord} PeerRecord - */ - PeerRecord.fromObject = function fromObject(d) { - if (d instanceof $root.PeerRecord) - return d; - var m = new $root.PeerRecord(); - if (d.peerId != null) { - if (typeof d.peerId === "string") - $util.base64.decode(d.peerId, m.peerId = $util.newBuffer($util.base64.length(d.peerId)), 0); - else if (d.peerId.length) - m.peerId = d.peerId; - } - if (d.seq != null) { - if ($util.Long) - (m.seq = $util.Long.fromValue(d.seq)).unsigned = true; - else if (typeof d.seq === "string") - m.seq = parseInt(d.seq, 10); - else if (typeof d.seq === "number") - m.seq = d.seq; - else if (typeof d.seq === "object") - m.seq = new $util.LongBits(d.seq.low >>> 0, d.seq.high >>> 0).toNumber(true); - } - if (d.addresses) { - if (!Array.isArray(d.addresses)) - throw TypeError(".PeerRecord.addresses: array expected"); - m.addresses = []; - for (var i = 0; i < d.addresses.length; ++i) { - if (typeof d.addresses[i] !== "object") - throw TypeError(".PeerRecord.addresses: object expected"); - m.addresses[i] = $root.PeerRecord.AddressInfo.fromObject(d.addresses[i]); - } - } - return m; - }; - - /** - * Creates a plain object from a PeerRecord message. Also converts values to other types if specified. - * @function toObject - * @memberof PeerRecord - * @static - * @param {PeerRecord} m PeerRecord - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - PeerRecord.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.addresses = []; - } - if (o.defaults) { - if (o.bytes === String) - d.peerId = ""; - else { - d.peerId = []; - if (o.bytes !== Array) - d.peerId = $util.newBuffer(d.peerId); - } - if ($util.Long) { - var n = new $util.Long(0, 0, true); - d.seq = o.longs === String ? n.toString() : o.longs === Number ? n.toNumber() : n; - } else - d.seq = o.longs === String ? "0" : 0; - } - if (m.peerId != null && m.hasOwnProperty("peerId")) { - d.peerId = o.bytes === String ? $util.base64.encode(m.peerId, 0, m.peerId.length) : o.bytes === Array ? Array.prototype.slice.call(m.peerId) : m.peerId; - } - if (m.seq != null && m.hasOwnProperty("seq")) { - if (typeof m.seq === "number") - d.seq = o.longs === String ? String(m.seq) : m.seq; - else - d.seq = o.longs === String ? $util.Long.prototype.toString.call(m.seq) : o.longs === Number ? new $util.LongBits(m.seq.low >>> 0, m.seq.high >>> 0).toNumber(true) : m.seq; - } - if (m.addresses && m.addresses.length) { - d.addresses = []; - for (var j = 0; j < m.addresses.length; ++j) { - d.addresses[j] = $root.PeerRecord.AddressInfo.toObject(m.addresses[j], o); - } - } - return d; - }; - - /** - * Converts this PeerRecord to JSON. - * @function toJSON - * @memberof PeerRecord - * @instance - * @returns {Object.} JSON object - */ - PeerRecord.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - PeerRecord.AddressInfo = (function() { - - /** - * Properties of an AddressInfo. - * @memberof PeerRecord - * @interface IAddressInfo - * @property {Uint8Array|null} [multiaddr] AddressInfo multiaddr - */ - - /** - * Constructs a new AddressInfo. - * @memberof PeerRecord - * @classdesc Represents an AddressInfo. - * @implements IAddressInfo - * @constructor - * @param {PeerRecord.IAddressInfo=} [p] Properties to set - */ - function AddressInfo(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * AddressInfo multiaddr. - * @member {Uint8Array} multiaddr - * @memberof PeerRecord.AddressInfo - * @instance - */ - AddressInfo.prototype.multiaddr = $util.newBuffer([]); - - /** - * Encodes the specified AddressInfo message. Does not implicitly {@link PeerRecord.AddressInfo.verify|verify} messages. - * @function encode - * @memberof PeerRecord.AddressInfo - * @static - * @param {PeerRecord.IAddressInfo} m AddressInfo message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - AddressInfo.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.multiaddr != null && Object.hasOwnProperty.call(m, "multiaddr")) - w.uint32(10).bytes(m.multiaddr); - return w; - }; - - /** - * Decodes an AddressInfo message from the specified reader or buffer. - * @function decode - * @memberof PeerRecord.AddressInfo - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {PeerRecord.AddressInfo} AddressInfo - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - AddressInfo.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.PeerRecord.AddressInfo(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.multiaddr = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates an AddressInfo message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof PeerRecord.AddressInfo - * @static - * @param {Object.} d Plain object - * @returns {PeerRecord.AddressInfo} AddressInfo - */ - AddressInfo.fromObject = function fromObject(d) { - if (d instanceof $root.PeerRecord.AddressInfo) - return d; - var m = new $root.PeerRecord.AddressInfo(); - if (d.multiaddr != null) { - if (typeof d.multiaddr === "string") - $util.base64.decode(d.multiaddr, m.multiaddr = $util.newBuffer($util.base64.length(d.multiaddr)), 0); - else if (d.multiaddr.length) - m.multiaddr = d.multiaddr; - } - return m; - }; - - /** - * Creates a plain object from an AddressInfo message. Also converts values to other types if specified. - * @function toObject - * @memberof PeerRecord.AddressInfo - * @static - * @param {PeerRecord.AddressInfo} m AddressInfo - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - AddressInfo.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.defaults) { - if (o.bytes === String) - d.multiaddr = ""; - else { - d.multiaddr = []; - if (o.bytes !== Array) - d.multiaddr = $util.newBuffer(d.multiaddr); - } - } - if (m.multiaddr != null && m.hasOwnProperty("multiaddr")) { - d.multiaddr = o.bytes === String ? $util.base64.encode(m.multiaddr, 0, m.multiaddr.length) : o.bytes === Array ? Array.prototype.slice.call(m.multiaddr) : m.multiaddr; - } - return d; - }; - - /** - * Converts this AddressInfo to JSON. - * @function toJSON - * @memberof PeerRecord.AddressInfo - * @instance - * @returns {Object.} JSON object - */ - AddressInfo.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return AddressInfo; - })(); - - return PeerRecord; -})(); - -export { $root as default }; diff --git a/packages/libp2p-peer-record/src/peer-record/peer-record.ts b/packages/libp2p-peer-record/src/peer-record/peer-record.ts new file mode 100644 index 000000000..3fcb5f77f --- /dev/null +++ b/packages/libp2p-peer-record/src/peer-record/peer-record.ts @@ -0,0 +1,48 @@ +/* eslint-disable import/export */ +/* eslint-disable @typescript-eslint/no-namespace */ + +import { encodeMessage, decodeMessage, message, bytes, uint64 } from 'protons-runtime' + +export interface PeerRecord { + peerId: Uint8Array + seq: bigint + addresses: PeerRecord.AddressInfo[] +} + +export namespace PeerRecord { + export interface AddressInfo { + multiaddr: Uint8Array + } + + export namespace AddressInfo { + export const codec = () => { + return message({ + 1: { name: 'multiaddr', codec: bytes } + }) + } + + export const encode = (obj: AddressInfo): Uint8Array => { + return encodeMessage(obj, AddressInfo.codec()) + } + + export const decode = (buf: Uint8Array): AddressInfo => { + return decodeMessage(buf, AddressInfo.codec()) + } + } + + export const codec = () => { + return message({ + 1: { name: 'peerId', codec: bytes }, + 2: { name: 'seq', codec: uint64 }, + 3: { name: 'addresses', codec: PeerRecord.AddressInfo.codec(), repeats: true } + }) + } + + export const encode = (obj: PeerRecord): Uint8Array => { + return encodeMessage(obj, PeerRecord.codec()) + } + + export const decode = (buf: Uint8Array): PeerRecord => { + return decodeMessage(buf, PeerRecord.codec()) + } +} diff --git a/packages/libp2p-peer-record/test/envelope.spec.ts b/packages/libp2p-peer-record/test/envelope.spec.ts index 9054a2e24..87648b033 100644 --- a/packages/libp2p-peer-record/test/envelope.spec.ts +++ b/packages/libp2p-peer-record/test/envelope.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { equals as uint8arrayEquals } from 'uint8arrays/equals' import { RecordEnvelope } from '../src/envelope/index.js' diff --git a/packages/libp2p-peer-record/test/peer-record.spec.ts b/packages/libp2p-peer-record/test/peer-record.spec.ts index ab667dd7a..0d02ac30a 100644 --- a/packages/libp2p-peer-record/test/peer-record.spec.ts +++ b/packages/libp2p-peer-record/test/peer-record.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import tests from '@libp2p/interface-compliance-tests/record' import { Multiaddr } from '@multiformats/multiaddr' import { peerIdFromKeys } from '@libp2p/peer-id' @@ -61,7 +61,7 @@ describe('PeerRecord', () => { const multiaddrs = [ new Multiaddr('/ip4/127.0.0.1/tcp/2000') ] - const seqNumber = Date.now() + const seqNumber = BigInt(Date.now()) const peerRecord = new PeerRecord({ peerId, multiaddrs, seqNumber }) expect(peerRecord).to.exist() @@ -76,7 +76,7 @@ describe('PeerRecord', () => { const multiaddrs = [ new Multiaddr('/ip4/127.0.0.1/tcp/2000') ] - const seqNumber = Date.now() + const seqNumber = BigInt(Date.now()) const peerRecord = new PeerRecord({ peerId, multiaddrs, seqNumber }) // Marshal @@ -102,10 +102,10 @@ describe('PeerRecord', () => { }) it('equals returns false if the peer record has a different seqNumber', () => { - const ts0 = Date.now() + const ts0 = BigInt(Date.now()) const peerRecord0 = new PeerRecord({ peerId, seqNumber: ts0 }) - const ts1 = ts0 + 20 + const ts1 = ts0 + 20n const peerRecord1 = new PeerRecord({ peerId, seqNumber: ts1 }) const equals = peerRecord0.equals(peerRecord1) @@ -137,7 +137,7 @@ describe('PeerRecord inside Envelope', () => { const multiaddrs = [ new Multiaddr('/ip4/127.0.0.1/tcp/2000') ] - const seqNumber = Date.now() + const seqNumber = BigInt(Date.now()) peerRecord = new PeerRecord({ peerId, multiaddrs, seqNumber }) }) diff --git a/packages/libp2p-peer-record/tsconfig.json b/packages/libp2p-peer-record/tsconfig.json index 5d9dc44b4..3ab2966fd 100644 --- a/packages/libp2p-peer-record/tsconfig.json +++ b/packages/libp2p-peer-record/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-peer-store/package.json b/packages/libp2p-peer-store/package.json index d4cad67fa..64a2cb460 100644 --- a/packages/libp2p-peer-store/package.json +++ b/packages/libp2p-peer-store/package.json @@ -123,21 +123,18 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc && npm run build:copy-proto-files", - "generate": "npm run generate:proto && npm run generate:proto-types && tsc", - "generate:proto": "pbjs -t static-module -w es6 -r libp2p-peer-store --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/pb/peer.js ./src/pb/peer.proto", - "generate:proto-types": "pbts -o src/pb/peer.d.ts src/pb/peer.js", - "build:copy-proto-files": "mkdirp dist/src/pb && cp src/pb/*.js dist/src/pb && cp src/pb/*.d.ts dist/src/pb", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0", @@ -152,18 +149,19 @@ "it-pipe": "^2.0.3", "mortice": "^3.0.0", "multiformats": "^9.6.3", - "protobufjs": "^6.11.2" + "protons-runtime": "^1.0.2" }, "devDependencies": { "@libp2p/interface-compliance-tests": "^1.1.0", "@libp2p/peer-id": "^1.1.0", "@libp2p/peer-id-factory": "^1.0.0", "@libp2p/utils": "^1.0.9", - "aegir": "^36.1.3", + "aegir": "^37.0.6", "datastore-core": "^7.0.1", "err-code": "^3.0.1", "p-defer": "^4.0.0", "p-wait-for": "^4.1.0", + "protons": "^3.0.2", "sinon": "^13.0.1", "uint8arrays": "^3.0.0" } diff --git a/packages/libp2p-peer-store/src/pb/peer.d.ts b/packages/libp2p-peer-store/src/pb/peer.d.ts deleted file mode 100644 index e6ccdfe6c..000000000 --- a/packages/libp2p-peer-store/src/pb/peer.d.ts +++ /dev/null @@ -1,222 +0,0 @@ -import * as $protobuf from "protobufjs"; -/** Properties of a Peer. */ -export interface IPeer { - - /** Peer addresses */ - addresses?: (IAddress[]|null); - - /** Peer protocols */ - protocols?: (string[]|null); - - /** Peer metadata */ - metadata?: (IMetadata[]|null); - - /** Peer pubKey */ - pubKey?: (Uint8Array|null); - - /** Peer peerRecordEnvelope */ - peerRecordEnvelope?: (Uint8Array|null); -} - -/** Represents a Peer. */ -export class Peer implements IPeer { - - /** - * Constructs a new Peer. - * @param [p] Properties to set - */ - constructor(p?: IPeer); - - /** Peer addresses. */ - public addresses: IAddress[]; - - /** Peer protocols. */ - public protocols: string[]; - - /** Peer metadata. */ - public metadata: IMetadata[]; - - /** Peer pubKey. */ - public pubKey?: (Uint8Array|null); - - /** Peer peerRecordEnvelope. */ - public peerRecordEnvelope?: (Uint8Array|null); - - /** Peer _pubKey. */ - public _pubKey?: "pubKey"; - - /** Peer _peerRecordEnvelope. */ - public _peerRecordEnvelope?: "peerRecordEnvelope"; - - /** - * Encodes the specified Peer message. Does not implicitly {@link Peer.verify|verify} messages. - * @param m Peer message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IPeer, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a Peer message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns Peer - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Peer; - - /** - * Creates a Peer message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns Peer - */ - public static fromObject(d: { [k: string]: any }): Peer; - - /** - * Creates a plain object from a Peer message. Also converts values to other types if specified. - * @param m Peer - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: Peer, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this Peer to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of an Address. */ -export interface IAddress { - - /** Address multiaddr */ - multiaddr?: (Uint8Array|null); - - /** Address isCertified */ - isCertified?: (boolean|null); -} - -/** Represents an Address. */ -export class Address implements IAddress { - - /** - * Constructs a new Address. - * @param [p] Properties to set - */ - constructor(p?: IAddress); - - /** Address multiaddr. */ - public multiaddr: Uint8Array; - - /** Address isCertified. */ - public isCertified?: (boolean|null); - - /** Address _isCertified. */ - public _isCertified?: "isCertified"; - - /** - * Encodes the specified Address message. Does not implicitly {@link Address.verify|verify} messages. - * @param m Address message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IAddress, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes an Address message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns Address - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Address; - - /** - * Creates an Address message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns Address - */ - public static fromObject(d: { [k: string]: any }): Address; - - /** - * Creates a plain object from an Address message. Also converts values to other types if specified. - * @param m Address - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: Address, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this Address to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of a Metadata. */ -export interface IMetadata { - - /** Metadata key */ - key?: (string|null); - - /** Metadata value */ - value?: (Uint8Array|null); -} - -/** Represents a Metadata. */ -export class Metadata implements IMetadata { - - /** - * Constructs a new Metadata. - * @param [p] Properties to set - */ - constructor(p?: IMetadata); - - /** Metadata key. */ - public key: string; - - /** Metadata value. */ - public value: Uint8Array; - - /** - * Encodes the specified Metadata message. Does not implicitly {@link Metadata.verify|verify} messages. - * @param m Metadata message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IMetadata, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a Metadata message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns Metadata - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Metadata; - - /** - * Creates a Metadata message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns Metadata - */ - public static fromObject(d: { [k: string]: any }): Metadata; - - /** - * Creates a plain object from a Metadata message. Also converts values to other types if specified. - * @param m Metadata - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: Metadata, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this Metadata to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} diff --git a/packages/libp2p-peer-store/src/pb/peer.js b/packages/libp2p-peer-store/src/pb/peer.js deleted file mode 100644 index 01a2998ca..000000000 --- a/packages/libp2p-peer-store/src/pb/peer.js +++ /dev/null @@ -1,641 +0,0 @@ -/*eslint-disable*/ -import $protobuf from "protobufjs/minimal.js"; - -// Common aliases -const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; - -// Exported root namespace -const $root = $protobuf.roots["libp2p-peer-store"] || ($protobuf.roots["libp2p-peer-store"] = {}); - -export const Peer = $root.Peer = (() => { - - /** - * Properties of a Peer. - * @exports IPeer - * @interface IPeer - * @property {Array.|null} [addresses] Peer addresses - * @property {Array.|null} [protocols] Peer protocols - * @property {Array.|null} [metadata] Peer metadata - * @property {Uint8Array|null} [pubKey] Peer pubKey - * @property {Uint8Array|null} [peerRecordEnvelope] Peer peerRecordEnvelope - */ - - /** - * Constructs a new Peer. - * @exports Peer - * @classdesc Represents a Peer. - * @implements IPeer - * @constructor - * @param {IPeer=} [p] Properties to set - */ - function Peer(p) { - this.addresses = []; - this.protocols = []; - this.metadata = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * Peer addresses. - * @member {Array.} addresses - * @memberof Peer - * @instance - */ - Peer.prototype.addresses = $util.emptyArray; - - /** - * Peer protocols. - * @member {Array.} protocols - * @memberof Peer - * @instance - */ - Peer.prototype.protocols = $util.emptyArray; - - /** - * Peer metadata. - * @member {Array.} metadata - * @memberof Peer - * @instance - */ - Peer.prototype.metadata = $util.emptyArray; - - /** - * Peer pubKey. - * @member {Uint8Array|null|undefined} pubKey - * @memberof Peer - * @instance - */ - Peer.prototype.pubKey = null; - - /** - * Peer peerRecordEnvelope. - * @member {Uint8Array|null|undefined} peerRecordEnvelope - * @memberof Peer - * @instance - */ - Peer.prototype.peerRecordEnvelope = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * Peer _pubKey. - * @member {"pubKey"|undefined} _pubKey - * @memberof Peer - * @instance - */ - Object.defineProperty(Peer.prototype, "_pubKey", { - get: $util.oneOfGetter($oneOfFields = ["pubKey"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Peer _peerRecordEnvelope. - * @member {"peerRecordEnvelope"|undefined} _peerRecordEnvelope - * @memberof Peer - * @instance - */ - Object.defineProperty(Peer.prototype, "_peerRecordEnvelope", { - get: $util.oneOfGetter($oneOfFields = ["peerRecordEnvelope"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified Peer message. Does not implicitly {@link Peer.verify|verify} messages. - * @function encode - * @memberof Peer - * @static - * @param {IPeer} m Peer message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - Peer.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.addresses != null && m.addresses.length) { - for (var i = 0; i < m.addresses.length; ++i) - $root.Address.encode(m.addresses[i], w.uint32(10).fork()).ldelim(); - } - if (m.protocols != null && m.protocols.length) { - for (var i = 0; i < m.protocols.length; ++i) - w.uint32(18).string(m.protocols[i]); - } - if (m.metadata != null && m.metadata.length) { - for (var i = 0; i < m.metadata.length; ++i) - $root.Metadata.encode(m.metadata[i], w.uint32(26).fork()).ldelim(); - } - if (m.pubKey != null && Object.hasOwnProperty.call(m, "pubKey")) - w.uint32(34).bytes(m.pubKey); - if (m.peerRecordEnvelope != null && Object.hasOwnProperty.call(m, "peerRecordEnvelope")) - w.uint32(42).bytes(m.peerRecordEnvelope); - return w; - }; - - /** - * Decodes a Peer message from the specified reader or buffer. - * @function decode - * @memberof Peer - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {Peer} Peer - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - Peer.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.Peer(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - if (!(m.addresses && m.addresses.length)) - m.addresses = []; - m.addresses.push($root.Address.decode(r, r.uint32())); - break; - case 2: - if (!(m.protocols && m.protocols.length)) - m.protocols = []; - m.protocols.push(r.string()); - break; - case 3: - if (!(m.metadata && m.metadata.length)) - m.metadata = []; - m.metadata.push($root.Metadata.decode(r, r.uint32())); - break; - case 4: - m.pubKey = r.bytes(); - break; - case 5: - m.peerRecordEnvelope = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a Peer message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof Peer - * @static - * @param {Object.} d Plain object - * @returns {Peer} Peer - */ - Peer.fromObject = function fromObject(d) { - if (d instanceof $root.Peer) - return d; - var m = new $root.Peer(); - if (d.addresses) { - if (!Array.isArray(d.addresses)) - throw TypeError(".Peer.addresses: array expected"); - m.addresses = []; - for (var i = 0; i < d.addresses.length; ++i) { - if (typeof d.addresses[i] !== "object") - throw TypeError(".Peer.addresses: object expected"); - m.addresses[i] = $root.Address.fromObject(d.addresses[i]); - } - } - if (d.protocols) { - if (!Array.isArray(d.protocols)) - throw TypeError(".Peer.protocols: array expected"); - m.protocols = []; - for (var i = 0; i < d.protocols.length; ++i) { - m.protocols[i] = String(d.protocols[i]); - } - } - if (d.metadata) { - if (!Array.isArray(d.metadata)) - throw TypeError(".Peer.metadata: array expected"); - m.metadata = []; - for (var i = 0; i < d.metadata.length; ++i) { - if (typeof d.metadata[i] !== "object") - throw TypeError(".Peer.metadata: object expected"); - m.metadata[i] = $root.Metadata.fromObject(d.metadata[i]); - } - } - if (d.pubKey != null) { - if (typeof d.pubKey === "string") - $util.base64.decode(d.pubKey, m.pubKey = $util.newBuffer($util.base64.length(d.pubKey)), 0); - else if (d.pubKey.length) - m.pubKey = d.pubKey; - } - if (d.peerRecordEnvelope != null) { - if (typeof d.peerRecordEnvelope === "string") - $util.base64.decode(d.peerRecordEnvelope, m.peerRecordEnvelope = $util.newBuffer($util.base64.length(d.peerRecordEnvelope)), 0); - else if (d.peerRecordEnvelope.length) - m.peerRecordEnvelope = d.peerRecordEnvelope; - } - return m; - }; - - /** - * Creates a plain object from a Peer message. Also converts values to other types if specified. - * @function toObject - * @memberof Peer - * @static - * @param {Peer} m Peer - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - Peer.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.addresses = []; - d.protocols = []; - d.metadata = []; - } - if (m.addresses && m.addresses.length) { - d.addresses = []; - for (var j = 0; j < m.addresses.length; ++j) { - d.addresses[j] = $root.Address.toObject(m.addresses[j], o); - } - } - if (m.protocols && m.protocols.length) { - d.protocols = []; - for (var j = 0; j < m.protocols.length; ++j) { - d.protocols[j] = m.protocols[j]; - } - } - if (m.metadata && m.metadata.length) { - d.metadata = []; - for (var j = 0; j < m.metadata.length; ++j) { - d.metadata[j] = $root.Metadata.toObject(m.metadata[j], o); - } - } - if (m.pubKey != null && m.hasOwnProperty("pubKey")) { - d.pubKey = o.bytes === String ? $util.base64.encode(m.pubKey, 0, m.pubKey.length) : o.bytes === Array ? Array.prototype.slice.call(m.pubKey) : m.pubKey; - if (o.oneofs) - d._pubKey = "pubKey"; - } - if (m.peerRecordEnvelope != null && m.hasOwnProperty("peerRecordEnvelope")) { - d.peerRecordEnvelope = o.bytes === String ? $util.base64.encode(m.peerRecordEnvelope, 0, m.peerRecordEnvelope.length) : o.bytes === Array ? Array.prototype.slice.call(m.peerRecordEnvelope) : m.peerRecordEnvelope; - if (o.oneofs) - d._peerRecordEnvelope = "peerRecordEnvelope"; - } - return d; - }; - - /** - * Converts this Peer to JSON. - * @function toJSON - * @memberof Peer - * @instance - * @returns {Object.} JSON object - */ - Peer.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return Peer; -})(); - -export const Address = $root.Address = (() => { - - /** - * Properties of an Address. - * @exports IAddress - * @interface IAddress - * @property {Uint8Array|null} [multiaddr] Address multiaddr - * @property {boolean|null} [isCertified] Address isCertified - */ - - /** - * Constructs a new Address. - * @exports Address - * @classdesc Represents an Address. - * @implements IAddress - * @constructor - * @param {IAddress=} [p] Properties to set - */ - function Address(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * Address multiaddr. - * @member {Uint8Array} multiaddr - * @memberof Address - * @instance - */ - Address.prototype.multiaddr = $util.newBuffer([]); - - /** - * Address isCertified. - * @member {boolean|null|undefined} isCertified - * @memberof Address - * @instance - */ - Address.prototype.isCertified = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * Address _isCertified. - * @member {"isCertified"|undefined} _isCertified - * @memberof Address - * @instance - */ - Object.defineProperty(Address.prototype, "_isCertified", { - get: $util.oneOfGetter($oneOfFields = ["isCertified"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified Address message. Does not implicitly {@link Address.verify|verify} messages. - * @function encode - * @memberof Address - * @static - * @param {IAddress} m Address message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - Address.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.multiaddr != null && Object.hasOwnProperty.call(m, "multiaddr")) - w.uint32(10).bytes(m.multiaddr); - if (m.isCertified != null && Object.hasOwnProperty.call(m, "isCertified")) - w.uint32(16).bool(m.isCertified); - return w; - }; - - /** - * Decodes an Address message from the specified reader or buffer. - * @function decode - * @memberof Address - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {Address} Address - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - Address.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.Address(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.multiaddr = r.bytes(); - break; - case 2: - m.isCertified = r.bool(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates an Address message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof Address - * @static - * @param {Object.} d Plain object - * @returns {Address} Address - */ - Address.fromObject = function fromObject(d) { - if (d instanceof $root.Address) - return d; - var m = new $root.Address(); - if (d.multiaddr != null) { - if (typeof d.multiaddr === "string") - $util.base64.decode(d.multiaddr, m.multiaddr = $util.newBuffer($util.base64.length(d.multiaddr)), 0); - else if (d.multiaddr.length) - m.multiaddr = d.multiaddr; - } - if (d.isCertified != null) { - m.isCertified = Boolean(d.isCertified); - } - return m; - }; - - /** - * Creates a plain object from an Address message. Also converts values to other types if specified. - * @function toObject - * @memberof Address - * @static - * @param {Address} m Address - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - Address.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.defaults) { - if (o.bytes === String) - d.multiaddr = ""; - else { - d.multiaddr = []; - if (o.bytes !== Array) - d.multiaddr = $util.newBuffer(d.multiaddr); - } - } - if (m.multiaddr != null && m.hasOwnProperty("multiaddr")) { - d.multiaddr = o.bytes === String ? $util.base64.encode(m.multiaddr, 0, m.multiaddr.length) : o.bytes === Array ? Array.prototype.slice.call(m.multiaddr) : m.multiaddr; - } - if (m.isCertified != null && m.hasOwnProperty("isCertified")) { - d.isCertified = m.isCertified; - if (o.oneofs) - d._isCertified = "isCertified"; - } - return d; - }; - - /** - * Converts this Address to JSON. - * @function toJSON - * @memberof Address - * @instance - * @returns {Object.} JSON object - */ - Address.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return Address; -})(); - -export const Metadata = $root.Metadata = (() => { - - /** - * Properties of a Metadata. - * @exports IMetadata - * @interface IMetadata - * @property {string|null} [key] Metadata key - * @property {Uint8Array|null} [value] Metadata value - */ - - /** - * Constructs a new Metadata. - * @exports Metadata - * @classdesc Represents a Metadata. - * @implements IMetadata - * @constructor - * @param {IMetadata=} [p] Properties to set - */ - function Metadata(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * Metadata key. - * @member {string} key - * @memberof Metadata - * @instance - */ - Metadata.prototype.key = ""; - - /** - * Metadata value. - * @member {Uint8Array} value - * @memberof Metadata - * @instance - */ - Metadata.prototype.value = $util.newBuffer([]); - - /** - * Encodes the specified Metadata message. Does not implicitly {@link Metadata.verify|verify} messages. - * @function encode - * @memberof Metadata - * @static - * @param {IMetadata} m Metadata message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - Metadata.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.key != null && Object.hasOwnProperty.call(m, "key")) - w.uint32(10).string(m.key); - if (m.value != null && Object.hasOwnProperty.call(m, "value")) - w.uint32(18).bytes(m.value); - return w; - }; - - /** - * Decodes a Metadata message from the specified reader or buffer. - * @function decode - * @memberof Metadata - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {Metadata} Metadata - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - Metadata.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.Metadata(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.key = r.string(); - break; - case 2: - m.value = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a Metadata message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof Metadata - * @static - * @param {Object.} d Plain object - * @returns {Metadata} Metadata - */ - Metadata.fromObject = function fromObject(d) { - if (d instanceof $root.Metadata) - return d; - var m = new $root.Metadata(); - if (d.key != null) { - m.key = String(d.key); - } - if (d.value != null) { - if (typeof d.value === "string") - $util.base64.decode(d.value, m.value = $util.newBuffer($util.base64.length(d.value)), 0); - else if (d.value.length) - m.value = d.value; - } - return m; - }; - - /** - * Creates a plain object from a Metadata message. Also converts values to other types if specified. - * @function toObject - * @memberof Metadata - * @static - * @param {Metadata} m Metadata - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - Metadata.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.defaults) { - d.key = ""; - if (o.bytes === String) - d.value = ""; - else { - d.value = []; - if (o.bytes !== Array) - d.value = $util.newBuffer(d.value); - } - } - if (m.key != null && m.hasOwnProperty("key")) { - d.key = m.key; - } - if (m.value != null && m.hasOwnProperty("value")) { - d.value = o.bytes === String ? $util.base64.encode(m.value, 0, m.value.length) : o.bytes === Array ? Array.prototype.slice.call(m.value) : m.value; - } - return d; - }; - - /** - * Converts this Metadata to JSON. - * @function toJSON - * @memberof Metadata - * @instance - * @returns {Object.} JSON object - */ - Metadata.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return Metadata; -})(); - -export { $root as default }; diff --git a/packages/libp2p-peer-store/src/pb/peer.ts b/packages/libp2p-peer-store/src/pb/peer.ts new file mode 100644 index 000000000..676ded3bd --- /dev/null +++ b/packages/libp2p-peer-store/src/pb/peer.ts @@ -0,0 +1,76 @@ +/* eslint-disable import/export */ +/* eslint-disable @typescript-eslint/no-namespace */ + +import { encodeMessage, decodeMessage, message, string, bytes, bool } from 'protons-runtime' + +export interface Peer { + addresses: Address[] + protocols: string[] + metadata: Metadata[] + pubKey?: Uint8Array + peerRecordEnvelope?: Uint8Array +} + +export namespace Peer { + export const codec = () => { + return message({ + 1: { name: 'addresses', codec: Address.codec(), repeats: true }, + 2: { name: 'protocols', codec: string, repeats: true }, + 3: { name: 'metadata', codec: Metadata.codec(), repeats: true }, + 4: { name: 'pubKey', codec: bytes, optional: true }, + 5: { name: 'peerRecordEnvelope', codec: bytes, optional: true } + }) + } + + export const encode = (obj: Peer): Uint8Array => { + return encodeMessage(obj, Peer.codec()) + } + + export const decode = (buf: Uint8Array): Peer => { + return decodeMessage(buf, Peer.codec()) + } +} + +export interface Address { + multiaddr: Uint8Array + isCertified?: boolean +} + +export namespace Address { + export const codec = () => { + return message
({ + 1: { name: 'multiaddr', codec: bytes }, + 2: { name: 'isCertified', codec: bool, optional: true } + }) + } + + export const encode = (obj: Address): Uint8Array => { + return encodeMessage(obj, Address.codec()) + } + + export const decode = (buf: Uint8Array): Address => { + return decodeMessage(buf, Address.codec()) + } +} + +export interface Metadata { + key: string + value: Uint8Array +} + +export namespace Metadata { + export const codec = () => { + return message({ + 1: { name: 'key', codec: string }, + 2: { name: 'value', codec: bytes } + }) + } + + export const encode = (obj: Metadata): Uint8Array => { + return encodeMessage(obj, Metadata.codec()) + } + + export const decode = (buf: Uint8Array): Metadata => { + return decodeMessage(buf, Metadata.codec()) + } +} diff --git a/packages/libp2p-peer-store/src/store.ts b/packages/libp2p-peer-store/src/store.ts index 4a308d946..bcaaf4ccf 100644 --- a/packages/libp2p-peer-store/src/store.ts +++ b/packages/libp2p-peer-store/src/store.ts @@ -5,7 +5,7 @@ import { codes } from './errors.js' import { Key } from 'interface-datastore/key' import { base32 } from 'multiformats/bases/base32' import { Multiaddr } from '@multiformats/multiaddr' -import { Peer as PeerPB } from './pb/peer.js' +import { Metadata, Peer as PeerPB } from './pb/peer.js' import mortice from 'mortice' import { equals as uint8arrayEquals } from 'uint8arrays/equals' import type { Peer } from '@libp2p/interfaces/peer-store' @@ -75,10 +75,12 @@ export class PersistentStore { return { ...peer, id: peerId, - addresses: peer.addresses.map(({ multiaddr, isCertified }) => ({ - multiaddr: new Multiaddr(multiaddr), - isCertified: isCertified ?? false - })), + addresses: peer.addresses.map(({ multiaddr, isCertified }) => { + return { + multiaddr: new Multiaddr(multiaddr), + isCertified: isCertified ?? false + } + }), metadata, pubKey: peer.pubKey ?? undefined, peerRecordEnvelope: peer.peerRecordEnvelope ?? undefined @@ -110,13 +112,23 @@ export class PersistentStore { isCertified })) + const metadata: Metadata[] = [] + + ;[...peer.metadata.keys()].sort().forEach(key => { + const value = peer.metadata.get(key) + + if (value != null) { + metadata.push({ key, value }) + } + }) + const buf = PeerPB.encode({ addresses, protocols: peer.protocols.sort(), pubKey: peer.pubKey, - metadata: [...peer.metadata.keys()].sort().map(key => ({ key, value: peer.metadata.get(key) })), + metadata, peerRecordEnvelope: peer.peerRecordEnvelope - }).finish() + }) await this.datastore.put(this._peerIdToDatastoreKey(peer.id), buf) diff --git a/packages/libp2p-peer-store/test/address-book.spec.ts b/packages/libp2p-peer-store/test/address-book.spec.ts index 6704bbb49..ae2fd5bbb 100644 --- a/packages/libp2p-peer-store/test/address-book.spec.ts +++ b/packages/libp2p-peer-store/test/address-book.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { Multiaddr } from '@multiformats/multiaddr' import { arrayEquals } from '@libp2p/utils/array-equals' import type { PeerId } from '@libp2p/interfaces/peer-id' @@ -645,12 +645,12 @@ describe('addressBook', () => { const peerRecord1 = new PeerRecord({ peerId, multiaddrs, - seqNumber: Date.now() + seqNumber: BigInt(Date.now()) }) const peerRecord2 = new PeerRecord({ peerId, multiaddrs, - seqNumber: Date.now() - 1 + seqNumber: BigInt(Date.now() - 1) }) const envelope1 = await RecordEnvelope.seal(peerRecord1, peerId) const envelope2 = await RecordEnvelope.seal(peerRecord2, peerId) diff --git a/packages/libp2p-peer-store/test/key-book.spec.ts b/packages/libp2p-peer-store/test/key-book.spec.ts index e28fba2a1..ce5047466 100644 --- a/packages/libp2p-peer-store/test/key-book.spec.ts +++ b/packages/libp2p-peer-store/test/key-book.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import { MemoryDatastore } from 'datastore-core/memory' import { PersistentPeerStore } from '../src/index.js' diff --git a/packages/libp2p-peer-store/test/metadata-book.spec.ts b/packages/libp2p-peer-store/test/metadata-book.spec.ts index b48636c53..d705ae2b2 100644 --- a/packages/libp2p-peer-store/test/metadata-book.spec.ts +++ b/packages/libp2p-peer-store/test/metadata-book.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { MemoryDatastore } from 'datastore-core/memory' import pDefer from 'p-defer' diff --git a/packages/libp2p-peer-store/test/peer-store.spec.ts b/packages/libp2p-peer-store/test/peer-store.spec.ts index 082eeedb1..9ddce6308 100644 --- a/packages/libp2p-peer-store/test/peer-store.spec.ts +++ b/packages/libp2p-peer-store/test/peer-store.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { PersistentPeerStore } from '../src/index.js' import { Multiaddr } from '@multiformats/multiaddr' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' diff --git a/packages/libp2p-peer-store/test/proto-book.spec.ts b/packages/libp2p-peer-store/test/proto-book.spec.ts index 60c5d6e3d..aa45f9275 100644 --- a/packages/libp2p-peer-store/test/proto-book.spec.ts +++ b/packages/libp2p-peer-store/test/proto-book.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import { MemoryDatastore } from 'datastore-core/memory' import pDefer from 'p-defer' diff --git a/packages/libp2p-peer-store/tsconfig.json b/packages/libp2p-peer-store/tsconfig.json index 9c8e193a5..37bbb88c4 100644 --- a/packages/libp2p-peer-store/tsconfig.json +++ b/packages/libp2p-peer-store/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-pubsub/package.json b/packages/libp2p-pubsub/package.json index 69faa37d9..162f2eeda 100644 --- a/packages/libp2p-pubsub/package.json +++ b/packages/libp2p-pubsub/package.json @@ -157,22 +157,19 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "postbuild": "npm run build:copy-proto-files", - "generate": "npm run generate:proto && npm run generate:proto-types", - "generate:proto": "pbjs -t static-module -w es6 -r libp2p-pubsub --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o test/message/rpc.js ./test/message/rpc.proto", - "generate:proto-types": "pbts -o test/message/rpc.d.ts test/message/rpc.js", - "build:copy-proto-files": "mkdirp dist/test/message && cp test/message/rpc.js test/message/rpc.d.ts dist/test/message", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "generate": "protons test/message/rpc.proto", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/crypto": "^0.22.8", @@ -193,10 +190,14 @@ "devDependencies": { "@libp2p/peer-id-factory": "^1.0.0", "abortable-iterator": "^4.0.2", - "aegir": "^36.1.3", + "aegir": "^37.0.6", + "delay": "^5.0.0", "it-pair": "^2.0.2", "it-pushable": "^2.0.1", - "protobufjs": "^6.10.2", + "p-wait-for": "^4.1.0", + "protons": "^3.0.2", + "protons-runtime": "^1.0.2", + "sinon": "^13.0.1", "util": "^0.12.4" } } diff --git a/packages/libp2p-pubsub/src/index.ts b/packages/libp2p-pubsub/src/index.ts index 0bdc92fce..59779a0de 100644 --- a/packages/libp2p-pubsub/src/index.ts +++ b/packages/libp2p-pubsub/src/index.ts @@ -174,19 +174,24 @@ export abstract class PubSubBaseProtocol extends EventEmitter impl /** * Registrar notifies an established connection with pubsub protocol */ - protected async _onPeerConnected (peerId: PeerId, conn: Connection) { + protected _onPeerConnected (peerId: PeerId, conn: Connection) { log('connected %p', peerId) - try { - const { stream, protocol } = await conn.newStream(this.multicodecs) - const peer = this.addPeer(peerId, protocol) - await peer.attachOutboundStream(stream) - } catch (err: any) { - log.error(err) - } + void Promise.resolve().then(async () => { + try { + const { stream, protocol } = await conn.newStream(this.multicodecs) + const peer = this.addPeer(peerId, protocol) + await peer.attachOutboundStream(stream) + } catch (err: any) { + log.error(err) + } - // Immediately send my own subscriptions to the newly established conn - this.send(peerId, { subscriptions: Array.from(this.subscriptions).map(sub => sub.toString()), subscribe: true }) + // Immediately send my own subscriptions to the newly established conn + this.send(peerId, { subscriptions: Array.from(this.subscriptions).map(sub => sub.toString()), subscribe: true }) + }) + .catch(err => { + log.error(err) + }) } /** diff --git a/packages/libp2p-pubsub/test/emit-self.spec.ts b/packages/libp2p-pubsub/test/emit-self.spec.ts index 745173b1f..d53155d30 100644 --- a/packages/libp2p-pubsub/test/emit-self.spec.ts +++ b/packages/libp2p-pubsub/test/emit-self.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { createPeerId, MockRegistrar, diff --git a/packages/libp2p-pubsub/test/instance.spec.ts b/packages/libp2p-pubsub/test/instance.spec.ts index 850be2a09..d8c2425ba 100644 --- a/packages/libp2p-pubsub/test/instance.spec.ts +++ b/packages/libp2p-pubsub/test/instance.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { PubSubBaseProtocol } from '../src/index.js' import type { PubSubRPC, PubSubRPCMessage } from '@libp2p/interfaces/pubsub' diff --git a/packages/libp2p-pubsub/test/lifecycle.spec.ts b/packages/libp2p-pubsub/test/lifecycle.spec.ts index 6f5229c88..93ca8f27e 100644 --- a/packages/libp2p-pubsub/test/lifecycle.spec.ts +++ b/packages/libp2p-pubsub/test/lifecycle.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import { PubSubBaseProtocol } from '../src/index.js' import { diff --git a/packages/libp2p-pubsub/test/message.spec.ts b/packages/libp2p-pubsub/test/message.spec.ts index 4ce365fd9..efab59d70 100644 --- a/packages/libp2p-pubsub/test/message.spec.ts +++ b/packages/libp2p-pubsub/test/message.spec.ts @@ -1,5 +1,5 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { diff --git a/packages/libp2p-pubsub/test/message/rpc.d.ts b/packages/libp2p-pubsub/test/message/rpc.d.ts deleted file mode 100644 index 168463589..000000000 --- a/packages/libp2p-pubsub/test/message/rpc.d.ts +++ /dev/null @@ -1,669 +0,0 @@ -import * as $protobuf from "protobufjs"; -/** Properties of a RPC. */ -export interface IRPC { - - /** RPC subscriptions */ - subscriptions?: (RPC.ISubOpts[]|null); - - /** RPC messages */ - messages?: (RPC.IMessage[]|null); - - /** RPC control */ - control?: (IControlMessage|null); -} - -/** Represents a RPC. */ -export class RPC implements IRPC { - - /** - * Constructs a new RPC. - * @param [p] Properties to set - */ - constructor(p?: IRPC); - - /** RPC subscriptions. */ - public subscriptions: RPC.ISubOpts[]; - - /** RPC messages. */ - public messages: RPC.IMessage[]; - - /** RPC control. */ - public control?: (IControlMessage|null); - - /** RPC _control. */ - public _control?: "control"; - - /** - * Encodes the specified RPC message. Does not implicitly {@link RPC.verify|verify} messages. - * @param m RPC message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IRPC, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a RPC message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns RPC - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC; - - /** - * Creates a RPC message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns RPC - */ - public static fromObject(d: { [k: string]: any }): RPC; - - /** - * Creates a plain object from a RPC message. Also converts values to other types if specified. - * @param m RPC - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: RPC, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this RPC to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -export namespace RPC { - - /** Properties of a SubOpts. */ - interface ISubOpts { - - /** SubOpts subscribe */ - subscribe?: (boolean|null); - - /** SubOpts topic */ - topic?: (string|null); - } - - /** Represents a SubOpts. */ - class SubOpts implements ISubOpts { - - /** - * Constructs a new SubOpts. - * @param [p] Properties to set - */ - constructor(p?: RPC.ISubOpts); - - /** SubOpts subscribe. */ - public subscribe?: (boolean|null); - - /** SubOpts topic. */ - public topic?: (string|null); - - /** SubOpts _subscribe. */ - public _subscribe?: "subscribe"; - - /** SubOpts _topic. */ - public _topic?: "topic"; - - /** - * Encodes the specified SubOpts message. Does not implicitly {@link RPC.SubOpts.verify|verify} messages. - * @param m SubOpts message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: RPC.ISubOpts, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a SubOpts message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns SubOpts - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.SubOpts; - - /** - * Creates a SubOpts message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns SubOpts - */ - public static fromObject(d: { [k: string]: any }): RPC.SubOpts; - - /** - * Creates a plain object from a SubOpts message. Also converts values to other types if specified. - * @param m SubOpts - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: RPC.SubOpts, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this SubOpts to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; - } - - /** Properties of a Message. */ - interface IMessage { - - /** Message from */ - from?: (Uint8Array|null); - - /** Message data */ - data?: (Uint8Array|null); - - /** Message seqno */ - seqno?: (Uint8Array|null); - - /** Message topic */ - topic?: (string|null); - - /** Message signature */ - signature?: (Uint8Array|null); - - /** Message key */ - key?: (Uint8Array|null); - } - - /** Represents a Message. */ - class Message implements IMessage { - - /** - * Constructs a new Message. - * @param [p] Properties to set - */ - constructor(p?: RPC.IMessage); - - /** Message from. */ - public from?: (Uint8Array|null); - - /** Message data. */ - public data?: (Uint8Array|null); - - /** Message seqno. */ - public seqno?: (Uint8Array|null); - - /** Message topic. */ - public topic?: (string|null); - - /** Message signature. */ - public signature?: (Uint8Array|null); - - /** Message key. */ - public key?: (Uint8Array|null); - - /** Message _from. */ - public _from?: "from"; - - /** Message _data. */ - public _data?: "data"; - - /** Message _seqno. */ - public _seqno?: "seqno"; - - /** Message _topic. */ - public _topic?: "topic"; - - /** Message _signature. */ - public _signature?: "signature"; - - /** Message _key. */ - public _key?: "key"; - - /** - * Encodes the specified Message message. Does not implicitly {@link RPC.Message.verify|verify} messages. - * @param m Message message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: RPC.IMessage, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a Message message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns Message - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): RPC.Message; - - /** - * Creates a Message message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns Message - */ - public static fromObject(d: { [k: string]: any }): RPC.Message; - - /** - * Creates a plain object from a Message message. Also converts values to other types if specified. - * @param m Message - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: RPC.Message, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this Message to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; - } -} - -/** Properties of a ControlMessage. */ -export interface IControlMessage { - - /** ControlMessage ihave */ - ihave?: (IControlIHave[]|null); - - /** ControlMessage iwant */ - iwant?: (IControlIWant[]|null); - - /** ControlMessage graft */ - graft?: (IControlGraft[]|null); - - /** ControlMessage prune */ - prune?: (IControlPrune[]|null); -} - -/** Represents a ControlMessage. */ -export class ControlMessage implements IControlMessage { - - /** - * Constructs a new ControlMessage. - * @param [p] Properties to set - */ - constructor(p?: IControlMessage); - - /** ControlMessage ihave. */ - public ihave: IControlIHave[]; - - /** ControlMessage iwant. */ - public iwant: IControlIWant[]; - - /** ControlMessage graft. */ - public graft: IControlGraft[]; - - /** ControlMessage prune. */ - public prune: IControlPrune[]; - - /** - * Encodes the specified ControlMessage message. Does not implicitly {@link ControlMessage.verify|verify} messages. - * @param m ControlMessage message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IControlMessage, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a ControlMessage message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns ControlMessage - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): ControlMessage; - - /** - * Creates a ControlMessage message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns ControlMessage - */ - public static fromObject(d: { [k: string]: any }): ControlMessage; - - /** - * Creates a plain object from a ControlMessage message. Also converts values to other types if specified. - * @param m ControlMessage - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: ControlMessage, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this ControlMessage to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of a ControlIHave. */ -export interface IControlIHave { - - /** ControlIHave topic */ - topic?: (string|null); - - /** ControlIHave messageIDs */ - messageIDs?: (Uint8Array[]|null); -} - -/** Represents a ControlIHave. */ -export class ControlIHave implements IControlIHave { - - /** - * Constructs a new ControlIHave. - * @param [p] Properties to set - */ - constructor(p?: IControlIHave); - - /** ControlIHave topic. */ - public topic?: (string|null); - - /** ControlIHave messageIDs. */ - public messageIDs: Uint8Array[]; - - /** ControlIHave _topic. */ - public _topic?: "topic"; - - /** - * Encodes the specified ControlIHave message. Does not implicitly {@link ControlIHave.verify|verify} messages. - * @param m ControlIHave message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IControlIHave, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a ControlIHave message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns ControlIHave - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): ControlIHave; - - /** - * Creates a ControlIHave message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns ControlIHave - */ - public static fromObject(d: { [k: string]: any }): ControlIHave; - - /** - * Creates a plain object from a ControlIHave message. Also converts values to other types if specified. - * @param m ControlIHave - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: ControlIHave, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this ControlIHave to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of a ControlIWant. */ -export interface IControlIWant { - - /** ControlIWant messageIDs */ - messageIDs?: (Uint8Array[]|null); -} - -/** Represents a ControlIWant. */ -export class ControlIWant implements IControlIWant { - - /** - * Constructs a new ControlIWant. - * @param [p] Properties to set - */ - constructor(p?: IControlIWant); - - /** ControlIWant messageIDs. */ - public messageIDs: Uint8Array[]; - - /** - * Encodes the specified ControlIWant message. Does not implicitly {@link ControlIWant.verify|verify} messages. - * @param m ControlIWant message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IControlIWant, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a ControlIWant message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns ControlIWant - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): ControlIWant; - - /** - * Creates a ControlIWant message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns ControlIWant - */ - public static fromObject(d: { [k: string]: any }): ControlIWant; - - /** - * Creates a plain object from a ControlIWant message. Also converts values to other types if specified. - * @param m ControlIWant - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: ControlIWant, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this ControlIWant to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of a ControlGraft. */ -export interface IControlGraft { - - /** ControlGraft topic */ - topic?: (string|null); -} - -/** Represents a ControlGraft. */ -export class ControlGraft implements IControlGraft { - - /** - * Constructs a new ControlGraft. - * @param [p] Properties to set - */ - constructor(p?: IControlGraft); - - /** ControlGraft topic. */ - public topic?: (string|null); - - /** ControlGraft _topic. */ - public _topic?: "topic"; - - /** - * Encodes the specified ControlGraft message. Does not implicitly {@link ControlGraft.verify|verify} messages. - * @param m ControlGraft message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IControlGraft, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a ControlGraft message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns ControlGraft - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): ControlGraft; - - /** - * Creates a ControlGraft message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns ControlGraft - */ - public static fromObject(d: { [k: string]: any }): ControlGraft; - - /** - * Creates a plain object from a ControlGraft message. Also converts values to other types if specified. - * @param m ControlGraft - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: ControlGraft, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this ControlGraft to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of a ControlPrune. */ -export interface IControlPrune { - - /** ControlPrune topic */ - topic?: (string|null); - - /** ControlPrune peers */ - peers?: (IPeerInfo[]|null); - - /** ControlPrune backoff */ - backoff?: (number|null); -} - -/** Represents a ControlPrune. */ -export class ControlPrune implements IControlPrune { - - /** - * Constructs a new ControlPrune. - * @param [p] Properties to set - */ - constructor(p?: IControlPrune); - - /** ControlPrune topic. */ - public topic?: (string|null); - - /** ControlPrune peers. */ - public peers: IPeerInfo[]; - - /** ControlPrune backoff. */ - public backoff?: (number|null); - - /** ControlPrune _topic. */ - public _topic?: "topic"; - - /** ControlPrune _backoff. */ - public _backoff?: "backoff"; - - /** - * Encodes the specified ControlPrune message. Does not implicitly {@link ControlPrune.verify|verify} messages. - * @param m ControlPrune message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IControlPrune, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a ControlPrune message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns ControlPrune - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): ControlPrune; - - /** - * Creates a ControlPrune message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns ControlPrune - */ - public static fromObject(d: { [k: string]: any }): ControlPrune; - - /** - * Creates a plain object from a ControlPrune message. Also converts values to other types if specified. - * @param m ControlPrune - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: ControlPrune, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this ControlPrune to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} - -/** Properties of a PeerInfo. */ -export interface IPeerInfo { - - /** PeerInfo peerID */ - peerID?: (Uint8Array|null); - - /** PeerInfo signedPeerRecord */ - signedPeerRecord?: (Uint8Array|null); -} - -/** Represents a PeerInfo. */ -export class PeerInfo implements IPeerInfo { - - /** - * Constructs a new PeerInfo. - * @param [p] Properties to set - */ - constructor(p?: IPeerInfo); - - /** PeerInfo peerID. */ - public peerID?: (Uint8Array|null); - - /** PeerInfo signedPeerRecord. */ - public signedPeerRecord?: (Uint8Array|null); - - /** PeerInfo _peerID. */ - public _peerID?: "peerID"; - - /** PeerInfo _signedPeerRecord. */ - public _signedPeerRecord?: "signedPeerRecord"; - - /** - * Encodes the specified PeerInfo message. Does not implicitly {@link PeerInfo.verify|verify} messages. - * @param m PeerInfo message or plain object to encode - * @param [w] Writer to encode to - * @returns Writer - */ - public static encode(m: IPeerInfo, w?: $protobuf.Writer): $protobuf.Writer; - - /** - * Decodes a PeerInfo message from the specified reader or buffer. - * @param r Reader or buffer to decode from - * @param [l] Message length if known beforehand - * @returns PeerInfo - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerInfo; - - /** - * Creates a PeerInfo message from a plain object. Also converts values to their respective internal types. - * @param d Plain object - * @returns PeerInfo - */ - public static fromObject(d: { [k: string]: any }): PeerInfo; - - /** - * Creates a plain object from a PeerInfo message. Also converts values to other types if specified. - * @param m PeerInfo - * @param [o] Conversion options - * @returns Plain object - */ - public static toObject(m: PeerInfo, o?: $protobuf.IConversionOptions): { [k: string]: any }; - - /** - * Converts this PeerInfo to JSON. - * @returns JSON object - */ - public toJSON(): { [k: string]: any }; -} diff --git a/packages/libp2p-pubsub/test/message/rpc.js b/packages/libp2p-pubsub/test/message/rpc.js deleted file mode 100644 index 271961497..000000000 --- a/packages/libp2p-pubsub/test/message/rpc.js +++ /dev/null @@ -1,1876 +0,0 @@ -/*eslint-disable*/ -import $protobuf from "protobufjs/minimal.js"; - -// Common aliases -const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; - -// Exported root namespace -const $root = $protobuf.roots["libp2p-pubsub-rpc"] || ($protobuf.roots["libp2p-pubsub-rpc"] = {}); - -export const RPC = $root.RPC = (() => { - - /** - * Properties of a RPC. - * @exports IRPC - * @interface IRPC - * @property {Array.|null} [subscriptions] RPC subscriptions - * @property {Array.|null} [messages] RPC messages - * @property {IControlMessage|null} [control] RPC control - */ - - /** - * Constructs a new RPC. - * @exports RPC - * @classdesc Represents a RPC. - * @implements IRPC - * @constructor - * @param {IRPC=} [p] Properties to set - */ - function RPC(p) { - this.subscriptions = []; - this.messages = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * RPC subscriptions. - * @member {Array.} subscriptions - * @memberof RPC - * @instance - */ - RPC.prototype.subscriptions = $util.emptyArray; - - /** - * RPC messages. - * @member {Array.} messages - * @memberof RPC - * @instance - */ - RPC.prototype.messages = $util.emptyArray; - - /** - * RPC control. - * @member {IControlMessage|null|undefined} control - * @memberof RPC - * @instance - */ - RPC.prototype.control = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * RPC _control. - * @member {"control"|undefined} _control - * @memberof RPC - * @instance - */ - Object.defineProperty(RPC.prototype, "_control", { - get: $util.oneOfGetter($oneOfFields = ["control"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified RPC message. Does not implicitly {@link RPC.verify|verify} messages. - * @function encode - * @memberof RPC - * @static - * @param {IRPC} m RPC message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - RPC.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.subscriptions != null && m.subscriptions.length) { - for (var i = 0; i < m.subscriptions.length; ++i) - $root.RPC.SubOpts.encode(m.subscriptions[i], w.uint32(10).fork()).ldelim(); - } - if (m.messages != null && m.messages.length) { - for (var i = 0; i < m.messages.length; ++i) - $root.RPC.Message.encode(m.messages[i], w.uint32(18).fork()).ldelim(); - } - if (m.control != null && Object.hasOwnProperty.call(m, "control")) - $root.ControlMessage.encode(m.control, w.uint32(26).fork()).ldelim(); - return w; - }; - - /** - * Decodes a RPC message from the specified reader or buffer. - * @function decode - * @memberof RPC - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {RPC} RPC - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - RPC.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - if (!(m.subscriptions && m.subscriptions.length)) - m.subscriptions = []; - m.subscriptions.push($root.RPC.SubOpts.decode(r, r.uint32())); - break; - case 2: - if (!(m.messages && m.messages.length)) - m.messages = []; - m.messages.push($root.RPC.Message.decode(r, r.uint32())); - break; - case 3: - m.control = $root.ControlMessage.decode(r, r.uint32()); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a RPC message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof RPC - * @static - * @param {Object.} d Plain object - * @returns {RPC} RPC - */ - RPC.fromObject = function fromObject(d) { - if (d instanceof $root.RPC) - return d; - var m = new $root.RPC(); - if (d.subscriptions) { - if (!Array.isArray(d.subscriptions)) - throw TypeError(".RPC.subscriptions: array expected"); - m.subscriptions = []; - for (var i = 0; i < d.subscriptions.length; ++i) { - if (typeof d.subscriptions[i] !== "object") - throw TypeError(".RPC.subscriptions: object expected"); - m.subscriptions[i] = $root.RPC.SubOpts.fromObject(d.subscriptions[i]); - } - } - if (d.messages) { - if (!Array.isArray(d.messages)) - throw TypeError(".RPC.messages: array expected"); - m.messages = []; - for (var i = 0; i < d.messages.length; ++i) { - if (typeof d.messages[i] !== "object") - throw TypeError(".RPC.messages: object expected"); - m.messages[i] = $root.RPC.Message.fromObject(d.messages[i]); - } - } - if (d.control != null) { - if (typeof d.control !== "object") - throw TypeError(".RPC.control: object expected"); - m.control = $root.ControlMessage.fromObject(d.control); - } - return m; - }; - - /** - * Creates a plain object from a RPC message. Also converts values to other types if specified. - * @function toObject - * @memberof RPC - * @static - * @param {RPC} m RPC - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - RPC.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.subscriptions = []; - d.messages = []; - } - if (m.subscriptions && m.subscriptions.length) { - d.subscriptions = []; - for (var j = 0; j < m.subscriptions.length; ++j) { - d.subscriptions[j] = $root.RPC.SubOpts.toObject(m.subscriptions[j], o); - } - } - if (m.messages && m.messages.length) { - d.messages = []; - for (var j = 0; j < m.messages.length; ++j) { - d.messages[j] = $root.RPC.Message.toObject(m.messages[j], o); - } - } - if (m.control != null && m.hasOwnProperty("control")) { - d.control = $root.ControlMessage.toObject(m.control, o); - if (o.oneofs) - d._control = "control"; - } - return d; - }; - - /** - * Converts this RPC to JSON. - * @function toJSON - * @memberof RPC - * @instance - * @returns {Object.} JSON object - */ - RPC.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - RPC.SubOpts = (function() { - - /** - * Properties of a SubOpts. - * @memberof RPC - * @interface ISubOpts - * @property {boolean|null} [subscribe] SubOpts subscribe - * @property {string|null} [topic] SubOpts topic - */ - - /** - * Constructs a new SubOpts. - * @memberof RPC - * @classdesc Represents a SubOpts. - * @implements ISubOpts - * @constructor - * @param {RPC.ISubOpts=} [p] Properties to set - */ - function SubOpts(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * SubOpts subscribe. - * @member {boolean|null|undefined} subscribe - * @memberof RPC.SubOpts - * @instance - */ - SubOpts.prototype.subscribe = null; - - /** - * SubOpts topic. - * @member {string|null|undefined} topic - * @memberof RPC.SubOpts - * @instance - */ - SubOpts.prototype.topic = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * SubOpts _subscribe. - * @member {"subscribe"|undefined} _subscribe - * @memberof RPC.SubOpts - * @instance - */ - Object.defineProperty(SubOpts.prototype, "_subscribe", { - get: $util.oneOfGetter($oneOfFields = ["subscribe"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * SubOpts _topic. - * @member {"topic"|undefined} _topic - * @memberof RPC.SubOpts - * @instance - */ - Object.defineProperty(SubOpts.prototype, "_topic", { - get: $util.oneOfGetter($oneOfFields = ["topic"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified SubOpts message. Does not implicitly {@link RPC.SubOpts.verify|verify} messages. - * @function encode - * @memberof RPC.SubOpts - * @static - * @param {RPC.ISubOpts} m SubOpts message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - SubOpts.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.subscribe != null && Object.hasOwnProperty.call(m, "subscribe")) - w.uint32(8).bool(m.subscribe); - if (m.topic != null && Object.hasOwnProperty.call(m, "topic")) - w.uint32(18).string(m.topic); - return w; - }; - - /** - * Decodes a SubOpts message from the specified reader or buffer. - * @function decode - * @memberof RPC.SubOpts - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {RPC.SubOpts} SubOpts - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - SubOpts.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.SubOpts(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.subscribe = r.bool(); - break; - case 2: - m.topic = r.string(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a SubOpts message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof RPC.SubOpts - * @static - * @param {Object.} d Plain object - * @returns {RPC.SubOpts} SubOpts - */ - SubOpts.fromObject = function fromObject(d) { - if (d instanceof $root.RPC.SubOpts) - return d; - var m = new $root.RPC.SubOpts(); - if (d.subscribe != null) { - m.subscribe = Boolean(d.subscribe); - } - if (d.topic != null) { - m.topic = String(d.topic); - } - return m; - }; - - /** - * Creates a plain object from a SubOpts message. Also converts values to other types if specified. - * @function toObject - * @memberof RPC.SubOpts - * @static - * @param {RPC.SubOpts} m SubOpts - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - SubOpts.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (m.subscribe != null && m.hasOwnProperty("subscribe")) { - d.subscribe = m.subscribe; - if (o.oneofs) - d._subscribe = "subscribe"; - } - if (m.topic != null && m.hasOwnProperty("topic")) { - d.topic = m.topic; - if (o.oneofs) - d._topic = "topic"; - } - return d; - }; - - /** - * Converts this SubOpts to JSON. - * @function toJSON - * @memberof RPC.SubOpts - * @instance - * @returns {Object.} JSON object - */ - SubOpts.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return SubOpts; - })(); - - RPC.Message = (function() { - - /** - * Properties of a Message. - * @memberof RPC - * @interface IMessage - * @property {Uint8Array|null} [from] Message from - * @property {Uint8Array|null} [data] Message data - * @property {Uint8Array|null} [seqno] Message seqno - * @property {string|null} [topic] Message topic - * @property {Uint8Array|null} [signature] Message signature - * @property {Uint8Array|null} [key] Message key - */ - - /** - * Constructs a new Message. - * @memberof RPC - * @classdesc Represents a Message. - * @implements IMessage - * @constructor - * @param {RPC.IMessage=} [p] Properties to set - */ - function Message(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * Message from. - * @member {Uint8Array|null|undefined} from - * @memberof RPC.Message - * @instance - */ - Message.prototype.from = null; - - /** - * Message data. - * @member {Uint8Array|null|undefined} data - * @memberof RPC.Message - * @instance - */ - Message.prototype.data = null; - - /** - * Message seqno. - * @member {Uint8Array|null|undefined} seqno - * @memberof RPC.Message - * @instance - */ - Message.prototype.seqno = null; - - /** - * Message topic. - * @member {string|null|undefined} topic - * @memberof RPC.Message - * @instance - */ - Message.prototype.topic = null; - - /** - * Message signature. - * @member {Uint8Array|null|undefined} signature - * @memberof RPC.Message - * @instance - */ - Message.prototype.signature = null; - - /** - * Message key. - * @member {Uint8Array|null|undefined} key - * @memberof RPC.Message - * @instance - */ - Message.prototype.key = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * Message _from. - * @member {"from"|undefined} _from - * @memberof RPC.Message - * @instance - */ - Object.defineProperty(Message.prototype, "_from", { - get: $util.oneOfGetter($oneOfFields = ["from"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Message _data. - * @member {"data"|undefined} _data - * @memberof RPC.Message - * @instance - */ - Object.defineProperty(Message.prototype, "_data", { - get: $util.oneOfGetter($oneOfFields = ["data"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Message _seqno. - * @member {"seqno"|undefined} _seqno - * @memberof RPC.Message - * @instance - */ - Object.defineProperty(Message.prototype, "_seqno", { - get: $util.oneOfGetter($oneOfFields = ["seqno"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Message _topic. - * @member {"topic"|undefined} _topic - * @memberof RPC.Message - * @instance - */ - Object.defineProperty(Message.prototype, "_topic", { - get: $util.oneOfGetter($oneOfFields = ["topic"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Message _signature. - * @member {"signature"|undefined} _signature - * @memberof RPC.Message - * @instance - */ - Object.defineProperty(Message.prototype, "_signature", { - get: $util.oneOfGetter($oneOfFields = ["signature"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Message _key. - * @member {"key"|undefined} _key - * @memberof RPC.Message - * @instance - */ - Object.defineProperty(Message.prototype, "_key", { - get: $util.oneOfGetter($oneOfFields = ["key"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified Message message. Does not implicitly {@link RPC.Message.verify|verify} messages. - * @function encode - * @memberof RPC.Message - * @static - * @param {RPC.IMessage} m Message message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - Message.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.from != null && Object.hasOwnProperty.call(m, "from")) - w.uint32(10).bytes(m.from); - if (m.data != null && Object.hasOwnProperty.call(m, "data")) - w.uint32(18).bytes(m.data); - if (m.seqno != null && Object.hasOwnProperty.call(m, "seqno")) - w.uint32(26).bytes(m.seqno); - if (m.topic != null && Object.hasOwnProperty.call(m, "topic")) - w.uint32(34).string(m.topic); - if (m.signature != null && Object.hasOwnProperty.call(m, "signature")) - w.uint32(42).bytes(m.signature); - if (m.key != null && Object.hasOwnProperty.call(m, "key")) - w.uint32(50).bytes(m.key); - return w; - }; - - /** - * Decodes a Message message from the specified reader or buffer. - * @function decode - * @memberof RPC.Message - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {RPC.Message} Message - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - Message.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.RPC.Message(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.from = r.bytes(); - break; - case 2: - m.data = r.bytes(); - break; - case 3: - m.seqno = r.bytes(); - break; - case 4: - m.topic = r.string(); - break; - case 5: - m.signature = r.bytes(); - break; - case 6: - m.key = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a Message message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof RPC.Message - * @static - * @param {Object.} d Plain object - * @returns {RPC.Message} Message - */ - Message.fromObject = function fromObject(d) { - if (d instanceof $root.RPC.Message) - return d; - var m = new $root.RPC.Message(); - if (d.from != null) { - if (typeof d.from === "string") - $util.base64.decode(d.from, m.from = $util.newBuffer($util.base64.length(d.from)), 0); - else if (d.from.length) - m.from = d.from; - } - if (d.data != null) { - if (typeof d.data === "string") - $util.base64.decode(d.data, m.data = $util.newBuffer($util.base64.length(d.data)), 0); - else if (d.data.length) - m.data = d.data; - } - if (d.seqno != null) { - if (typeof d.seqno === "string") - $util.base64.decode(d.seqno, m.seqno = $util.newBuffer($util.base64.length(d.seqno)), 0); - else if (d.seqno.length) - m.seqno = d.seqno; - } - if (d.topic != null) { - m.topic = String(d.topic); - } - if (d.signature != null) { - if (typeof d.signature === "string") - $util.base64.decode(d.signature, m.signature = $util.newBuffer($util.base64.length(d.signature)), 0); - else if (d.signature.length) - m.signature = d.signature; - } - if (d.key != null) { - if (typeof d.key === "string") - $util.base64.decode(d.key, m.key = $util.newBuffer($util.base64.length(d.key)), 0); - else if (d.key.length) - m.key = d.key; - } - return m; - }; - - /** - * Creates a plain object from a Message message. Also converts values to other types if specified. - * @function toObject - * @memberof RPC.Message - * @static - * @param {RPC.Message} m Message - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - Message.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (m.from != null && m.hasOwnProperty("from")) { - d.from = o.bytes === String ? $util.base64.encode(m.from, 0, m.from.length) : o.bytes === Array ? Array.prototype.slice.call(m.from) : m.from; - if (o.oneofs) - d._from = "from"; - } - if (m.data != null && m.hasOwnProperty("data")) { - d.data = o.bytes === String ? $util.base64.encode(m.data, 0, m.data.length) : o.bytes === Array ? Array.prototype.slice.call(m.data) : m.data; - if (o.oneofs) - d._data = "data"; - } - if (m.seqno != null && m.hasOwnProperty("seqno")) { - d.seqno = o.bytes === String ? $util.base64.encode(m.seqno, 0, m.seqno.length) : o.bytes === Array ? Array.prototype.slice.call(m.seqno) : m.seqno; - if (o.oneofs) - d._seqno = "seqno"; - } - if (m.topic != null && m.hasOwnProperty("topic")) { - d.topic = m.topic; - if (o.oneofs) - d._topic = "topic"; - } - if (m.signature != null && m.hasOwnProperty("signature")) { - d.signature = o.bytes === String ? $util.base64.encode(m.signature, 0, m.signature.length) : o.bytes === Array ? Array.prototype.slice.call(m.signature) : m.signature; - if (o.oneofs) - d._signature = "signature"; - } - if (m.key != null && m.hasOwnProperty("key")) { - d.key = o.bytes === String ? $util.base64.encode(m.key, 0, m.key.length) : o.bytes === Array ? Array.prototype.slice.call(m.key) : m.key; - if (o.oneofs) - d._key = "key"; - } - return d; - }; - - /** - * Converts this Message to JSON. - * @function toJSON - * @memberof RPC.Message - * @instance - * @returns {Object.} JSON object - */ - Message.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return Message; - })(); - - return RPC; -})(); - -export const ControlMessage = $root.ControlMessage = (() => { - - /** - * Properties of a ControlMessage. - * @exports IControlMessage - * @interface IControlMessage - * @property {Array.|null} [ihave] ControlMessage ihave - * @property {Array.|null} [iwant] ControlMessage iwant - * @property {Array.|null} [graft] ControlMessage graft - * @property {Array.|null} [prune] ControlMessage prune - */ - - /** - * Constructs a new ControlMessage. - * @exports ControlMessage - * @classdesc Represents a ControlMessage. - * @implements IControlMessage - * @constructor - * @param {IControlMessage=} [p] Properties to set - */ - function ControlMessage(p) { - this.ihave = []; - this.iwant = []; - this.graft = []; - this.prune = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * ControlMessage ihave. - * @member {Array.} ihave - * @memberof ControlMessage - * @instance - */ - ControlMessage.prototype.ihave = $util.emptyArray; - - /** - * ControlMessage iwant. - * @member {Array.} iwant - * @memberof ControlMessage - * @instance - */ - ControlMessage.prototype.iwant = $util.emptyArray; - - /** - * ControlMessage graft. - * @member {Array.} graft - * @memberof ControlMessage - * @instance - */ - ControlMessage.prototype.graft = $util.emptyArray; - - /** - * ControlMessage prune. - * @member {Array.} prune - * @memberof ControlMessage - * @instance - */ - ControlMessage.prototype.prune = $util.emptyArray; - - /** - * Encodes the specified ControlMessage message. Does not implicitly {@link ControlMessage.verify|verify} messages. - * @function encode - * @memberof ControlMessage - * @static - * @param {IControlMessage} m ControlMessage message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - ControlMessage.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.ihave != null && m.ihave.length) { - for (var i = 0; i < m.ihave.length; ++i) - $root.ControlIHave.encode(m.ihave[i], w.uint32(10).fork()).ldelim(); - } - if (m.iwant != null && m.iwant.length) { - for (var i = 0; i < m.iwant.length; ++i) - $root.ControlIWant.encode(m.iwant[i], w.uint32(18).fork()).ldelim(); - } - if (m.graft != null && m.graft.length) { - for (var i = 0; i < m.graft.length; ++i) - $root.ControlGraft.encode(m.graft[i], w.uint32(26).fork()).ldelim(); - } - if (m.prune != null && m.prune.length) { - for (var i = 0; i < m.prune.length; ++i) - $root.ControlPrune.encode(m.prune[i], w.uint32(34).fork()).ldelim(); - } - return w; - }; - - /** - * Decodes a ControlMessage message from the specified reader or buffer. - * @function decode - * @memberof ControlMessage - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {ControlMessage} ControlMessage - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - ControlMessage.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.ControlMessage(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - if (!(m.ihave && m.ihave.length)) - m.ihave = []; - m.ihave.push($root.ControlIHave.decode(r, r.uint32())); - break; - case 2: - if (!(m.iwant && m.iwant.length)) - m.iwant = []; - m.iwant.push($root.ControlIWant.decode(r, r.uint32())); - break; - case 3: - if (!(m.graft && m.graft.length)) - m.graft = []; - m.graft.push($root.ControlGraft.decode(r, r.uint32())); - break; - case 4: - if (!(m.prune && m.prune.length)) - m.prune = []; - m.prune.push($root.ControlPrune.decode(r, r.uint32())); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a ControlMessage message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof ControlMessage - * @static - * @param {Object.} d Plain object - * @returns {ControlMessage} ControlMessage - */ - ControlMessage.fromObject = function fromObject(d) { - if (d instanceof $root.ControlMessage) - return d; - var m = new $root.ControlMessage(); - if (d.ihave) { - if (!Array.isArray(d.ihave)) - throw TypeError(".ControlMessage.ihave: array expected"); - m.ihave = []; - for (var i = 0; i < d.ihave.length; ++i) { - if (typeof d.ihave[i] !== "object") - throw TypeError(".ControlMessage.ihave: object expected"); - m.ihave[i] = $root.ControlIHave.fromObject(d.ihave[i]); - } - } - if (d.iwant) { - if (!Array.isArray(d.iwant)) - throw TypeError(".ControlMessage.iwant: array expected"); - m.iwant = []; - for (var i = 0; i < d.iwant.length; ++i) { - if (typeof d.iwant[i] !== "object") - throw TypeError(".ControlMessage.iwant: object expected"); - m.iwant[i] = $root.ControlIWant.fromObject(d.iwant[i]); - } - } - if (d.graft) { - if (!Array.isArray(d.graft)) - throw TypeError(".ControlMessage.graft: array expected"); - m.graft = []; - for (var i = 0; i < d.graft.length; ++i) { - if (typeof d.graft[i] !== "object") - throw TypeError(".ControlMessage.graft: object expected"); - m.graft[i] = $root.ControlGraft.fromObject(d.graft[i]); - } - } - if (d.prune) { - if (!Array.isArray(d.prune)) - throw TypeError(".ControlMessage.prune: array expected"); - m.prune = []; - for (var i = 0; i < d.prune.length; ++i) { - if (typeof d.prune[i] !== "object") - throw TypeError(".ControlMessage.prune: object expected"); - m.prune[i] = $root.ControlPrune.fromObject(d.prune[i]); - } - } - return m; - }; - - /** - * Creates a plain object from a ControlMessage message. Also converts values to other types if specified. - * @function toObject - * @memberof ControlMessage - * @static - * @param {ControlMessage} m ControlMessage - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - ControlMessage.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.ihave = []; - d.iwant = []; - d.graft = []; - d.prune = []; - } - if (m.ihave && m.ihave.length) { - d.ihave = []; - for (var j = 0; j < m.ihave.length; ++j) { - d.ihave[j] = $root.ControlIHave.toObject(m.ihave[j], o); - } - } - if (m.iwant && m.iwant.length) { - d.iwant = []; - for (var j = 0; j < m.iwant.length; ++j) { - d.iwant[j] = $root.ControlIWant.toObject(m.iwant[j], o); - } - } - if (m.graft && m.graft.length) { - d.graft = []; - for (var j = 0; j < m.graft.length; ++j) { - d.graft[j] = $root.ControlGraft.toObject(m.graft[j], o); - } - } - if (m.prune && m.prune.length) { - d.prune = []; - for (var j = 0; j < m.prune.length; ++j) { - d.prune[j] = $root.ControlPrune.toObject(m.prune[j], o); - } - } - return d; - }; - - /** - * Converts this ControlMessage to JSON. - * @function toJSON - * @memberof ControlMessage - * @instance - * @returns {Object.} JSON object - */ - ControlMessage.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return ControlMessage; -})(); - -export const ControlIHave = $root.ControlIHave = (() => { - - /** - * Properties of a ControlIHave. - * @exports IControlIHave - * @interface IControlIHave - * @property {string|null} [topic] ControlIHave topic - * @property {Array.|null} [messageIDs] ControlIHave messageIDs - */ - - /** - * Constructs a new ControlIHave. - * @exports ControlIHave - * @classdesc Represents a ControlIHave. - * @implements IControlIHave - * @constructor - * @param {IControlIHave=} [p] Properties to set - */ - function ControlIHave(p) { - this.messageIDs = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * ControlIHave topic. - * @member {string|null|undefined} topic - * @memberof ControlIHave - * @instance - */ - ControlIHave.prototype.topic = null; - - /** - * ControlIHave messageIDs. - * @member {Array.} messageIDs - * @memberof ControlIHave - * @instance - */ - ControlIHave.prototype.messageIDs = $util.emptyArray; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * ControlIHave _topic. - * @member {"topic"|undefined} _topic - * @memberof ControlIHave - * @instance - */ - Object.defineProperty(ControlIHave.prototype, "_topic", { - get: $util.oneOfGetter($oneOfFields = ["topic"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified ControlIHave message. Does not implicitly {@link ControlIHave.verify|verify} messages. - * @function encode - * @memberof ControlIHave - * @static - * @param {IControlIHave} m ControlIHave message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - ControlIHave.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.topic != null && Object.hasOwnProperty.call(m, "topic")) - w.uint32(10).string(m.topic); - if (m.messageIDs != null && m.messageIDs.length) { - for (var i = 0; i < m.messageIDs.length; ++i) - w.uint32(18).bytes(m.messageIDs[i]); - } - return w; - }; - - /** - * Decodes a ControlIHave message from the specified reader or buffer. - * @function decode - * @memberof ControlIHave - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {ControlIHave} ControlIHave - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - ControlIHave.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.ControlIHave(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.topic = r.string(); - break; - case 2: - if (!(m.messageIDs && m.messageIDs.length)) - m.messageIDs = []; - m.messageIDs.push(r.bytes()); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a ControlIHave message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof ControlIHave - * @static - * @param {Object.} d Plain object - * @returns {ControlIHave} ControlIHave - */ - ControlIHave.fromObject = function fromObject(d) { - if (d instanceof $root.ControlIHave) - return d; - var m = new $root.ControlIHave(); - if (d.topic != null) { - m.topic = String(d.topic); - } - if (d.messageIDs) { - if (!Array.isArray(d.messageIDs)) - throw TypeError(".ControlIHave.messageIDs: array expected"); - m.messageIDs = []; - for (var i = 0; i < d.messageIDs.length; ++i) { - if (typeof d.messageIDs[i] === "string") - $util.base64.decode(d.messageIDs[i], m.messageIDs[i] = $util.newBuffer($util.base64.length(d.messageIDs[i])), 0); - else if (d.messageIDs[i].length) - m.messageIDs[i] = d.messageIDs[i]; - } - } - return m; - }; - - /** - * Creates a plain object from a ControlIHave message. Also converts values to other types if specified. - * @function toObject - * @memberof ControlIHave - * @static - * @param {ControlIHave} m ControlIHave - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - ControlIHave.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.messageIDs = []; - } - if (m.topic != null && m.hasOwnProperty("topic")) { - d.topic = m.topic; - if (o.oneofs) - d._topic = "topic"; - } - if (m.messageIDs && m.messageIDs.length) { - d.messageIDs = []; - for (var j = 0; j < m.messageIDs.length; ++j) { - d.messageIDs[j] = o.bytes === String ? $util.base64.encode(m.messageIDs[j], 0, m.messageIDs[j].length) : o.bytes === Array ? Array.prototype.slice.call(m.messageIDs[j]) : m.messageIDs[j]; - } - } - return d; - }; - - /** - * Converts this ControlIHave to JSON. - * @function toJSON - * @memberof ControlIHave - * @instance - * @returns {Object.} JSON object - */ - ControlIHave.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return ControlIHave; -})(); - -export const ControlIWant = $root.ControlIWant = (() => { - - /** - * Properties of a ControlIWant. - * @exports IControlIWant - * @interface IControlIWant - * @property {Array.|null} [messageIDs] ControlIWant messageIDs - */ - - /** - * Constructs a new ControlIWant. - * @exports ControlIWant - * @classdesc Represents a ControlIWant. - * @implements IControlIWant - * @constructor - * @param {IControlIWant=} [p] Properties to set - */ - function ControlIWant(p) { - this.messageIDs = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * ControlIWant messageIDs. - * @member {Array.} messageIDs - * @memberof ControlIWant - * @instance - */ - ControlIWant.prototype.messageIDs = $util.emptyArray; - - /** - * Encodes the specified ControlIWant message. Does not implicitly {@link ControlIWant.verify|verify} messages. - * @function encode - * @memberof ControlIWant - * @static - * @param {IControlIWant} m ControlIWant message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - ControlIWant.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.messageIDs != null && m.messageIDs.length) { - for (var i = 0; i < m.messageIDs.length; ++i) - w.uint32(10).bytes(m.messageIDs[i]); - } - return w; - }; - - /** - * Decodes a ControlIWant message from the specified reader or buffer. - * @function decode - * @memberof ControlIWant - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {ControlIWant} ControlIWant - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - ControlIWant.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.ControlIWant(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - if (!(m.messageIDs && m.messageIDs.length)) - m.messageIDs = []; - m.messageIDs.push(r.bytes()); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a ControlIWant message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof ControlIWant - * @static - * @param {Object.} d Plain object - * @returns {ControlIWant} ControlIWant - */ - ControlIWant.fromObject = function fromObject(d) { - if (d instanceof $root.ControlIWant) - return d; - var m = new $root.ControlIWant(); - if (d.messageIDs) { - if (!Array.isArray(d.messageIDs)) - throw TypeError(".ControlIWant.messageIDs: array expected"); - m.messageIDs = []; - for (var i = 0; i < d.messageIDs.length; ++i) { - if (typeof d.messageIDs[i] === "string") - $util.base64.decode(d.messageIDs[i], m.messageIDs[i] = $util.newBuffer($util.base64.length(d.messageIDs[i])), 0); - else if (d.messageIDs[i].length) - m.messageIDs[i] = d.messageIDs[i]; - } - } - return m; - }; - - /** - * Creates a plain object from a ControlIWant message. Also converts values to other types if specified. - * @function toObject - * @memberof ControlIWant - * @static - * @param {ControlIWant} m ControlIWant - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - ControlIWant.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.messageIDs = []; - } - if (m.messageIDs && m.messageIDs.length) { - d.messageIDs = []; - for (var j = 0; j < m.messageIDs.length; ++j) { - d.messageIDs[j] = o.bytes === String ? $util.base64.encode(m.messageIDs[j], 0, m.messageIDs[j].length) : o.bytes === Array ? Array.prototype.slice.call(m.messageIDs[j]) : m.messageIDs[j]; - } - } - return d; - }; - - /** - * Converts this ControlIWant to JSON. - * @function toJSON - * @memberof ControlIWant - * @instance - * @returns {Object.} JSON object - */ - ControlIWant.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return ControlIWant; -})(); - -export const ControlGraft = $root.ControlGraft = (() => { - - /** - * Properties of a ControlGraft. - * @exports IControlGraft - * @interface IControlGraft - * @property {string|null} [topic] ControlGraft topic - */ - - /** - * Constructs a new ControlGraft. - * @exports ControlGraft - * @classdesc Represents a ControlGraft. - * @implements IControlGraft - * @constructor - * @param {IControlGraft=} [p] Properties to set - */ - function ControlGraft(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * ControlGraft topic. - * @member {string|null|undefined} topic - * @memberof ControlGraft - * @instance - */ - ControlGraft.prototype.topic = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * ControlGraft _topic. - * @member {"topic"|undefined} _topic - * @memberof ControlGraft - * @instance - */ - Object.defineProperty(ControlGraft.prototype, "_topic", { - get: $util.oneOfGetter($oneOfFields = ["topic"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified ControlGraft message. Does not implicitly {@link ControlGraft.verify|verify} messages. - * @function encode - * @memberof ControlGraft - * @static - * @param {IControlGraft} m ControlGraft message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - ControlGraft.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.topic != null && Object.hasOwnProperty.call(m, "topic")) - w.uint32(10).string(m.topic); - return w; - }; - - /** - * Decodes a ControlGraft message from the specified reader or buffer. - * @function decode - * @memberof ControlGraft - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {ControlGraft} ControlGraft - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - ControlGraft.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.ControlGraft(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.topic = r.string(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a ControlGraft message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof ControlGraft - * @static - * @param {Object.} d Plain object - * @returns {ControlGraft} ControlGraft - */ - ControlGraft.fromObject = function fromObject(d) { - if (d instanceof $root.ControlGraft) - return d; - var m = new $root.ControlGraft(); - if (d.topic != null) { - m.topic = String(d.topic); - } - return m; - }; - - /** - * Creates a plain object from a ControlGraft message. Also converts values to other types if specified. - * @function toObject - * @memberof ControlGraft - * @static - * @param {ControlGraft} m ControlGraft - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - ControlGraft.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (m.topic != null && m.hasOwnProperty("topic")) { - d.topic = m.topic; - if (o.oneofs) - d._topic = "topic"; - } - return d; - }; - - /** - * Converts this ControlGraft to JSON. - * @function toJSON - * @memberof ControlGraft - * @instance - * @returns {Object.} JSON object - */ - ControlGraft.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return ControlGraft; -})(); - -export const ControlPrune = $root.ControlPrune = (() => { - - /** - * Properties of a ControlPrune. - * @exports IControlPrune - * @interface IControlPrune - * @property {string|null} [topic] ControlPrune topic - * @property {Array.|null} [peers] ControlPrune peers - * @property {number|null} [backoff] ControlPrune backoff - */ - - /** - * Constructs a new ControlPrune. - * @exports ControlPrune - * @classdesc Represents a ControlPrune. - * @implements IControlPrune - * @constructor - * @param {IControlPrune=} [p] Properties to set - */ - function ControlPrune(p) { - this.peers = []; - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * ControlPrune topic. - * @member {string|null|undefined} topic - * @memberof ControlPrune - * @instance - */ - ControlPrune.prototype.topic = null; - - /** - * ControlPrune peers. - * @member {Array.} peers - * @memberof ControlPrune - * @instance - */ - ControlPrune.prototype.peers = $util.emptyArray; - - /** - * ControlPrune backoff. - * @member {number|null|undefined} backoff - * @memberof ControlPrune - * @instance - */ - ControlPrune.prototype.backoff = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * ControlPrune _topic. - * @member {"topic"|undefined} _topic - * @memberof ControlPrune - * @instance - */ - Object.defineProperty(ControlPrune.prototype, "_topic", { - get: $util.oneOfGetter($oneOfFields = ["topic"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * ControlPrune _backoff. - * @member {"backoff"|undefined} _backoff - * @memberof ControlPrune - * @instance - */ - Object.defineProperty(ControlPrune.prototype, "_backoff", { - get: $util.oneOfGetter($oneOfFields = ["backoff"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified ControlPrune message. Does not implicitly {@link ControlPrune.verify|verify} messages. - * @function encode - * @memberof ControlPrune - * @static - * @param {IControlPrune} m ControlPrune message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - ControlPrune.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.topic != null && Object.hasOwnProperty.call(m, "topic")) - w.uint32(10).string(m.topic); - if (m.peers != null && m.peers.length) { - for (var i = 0; i < m.peers.length; ++i) - $root.PeerInfo.encode(m.peers[i], w.uint32(18).fork()).ldelim(); - } - if (m.backoff != null && Object.hasOwnProperty.call(m, "backoff")) - w.uint32(24).uint64(m.backoff); - return w; - }; - - /** - * Decodes a ControlPrune message from the specified reader or buffer. - * @function decode - * @memberof ControlPrune - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {ControlPrune} ControlPrune - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - ControlPrune.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.ControlPrune(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.topic = r.string(); - break; - case 2: - if (!(m.peers && m.peers.length)) - m.peers = []; - m.peers.push($root.PeerInfo.decode(r, r.uint32())); - break; - case 3: - m.backoff = r.uint64(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a ControlPrune message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof ControlPrune - * @static - * @param {Object.} d Plain object - * @returns {ControlPrune} ControlPrune - */ - ControlPrune.fromObject = function fromObject(d) { - if (d instanceof $root.ControlPrune) - return d; - var m = new $root.ControlPrune(); - if (d.topic != null) { - m.topic = String(d.topic); - } - if (d.peers) { - if (!Array.isArray(d.peers)) - throw TypeError(".ControlPrune.peers: array expected"); - m.peers = []; - for (var i = 0; i < d.peers.length; ++i) { - if (typeof d.peers[i] !== "object") - throw TypeError(".ControlPrune.peers: object expected"); - m.peers[i] = $root.PeerInfo.fromObject(d.peers[i]); - } - } - if (d.backoff != null) { - if ($util.Long) - (m.backoff = $util.Long.fromValue(d.backoff)).unsigned = true; - else if (typeof d.backoff === "string") - m.backoff = parseInt(d.backoff, 10); - else if (typeof d.backoff === "number") - m.backoff = d.backoff; - else if (typeof d.backoff === "object") - m.backoff = new $util.LongBits(d.backoff.low >>> 0, d.backoff.high >>> 0).toNumber(true); - } - return m; - }; - - /** - * Creates a plain object from a ControlPrune message. Also converts values to other types if specified. - * @function toObject - * @memberof ControlPrune - * @static - * @param {ControlPrune} m ControlPrune - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - ControlPrune.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (o.arrays || o.defaults) { - d.peers = []; - } - if (m.topic != null && m.hasOwnProperty("topic")) { - d.topic = m.topic; - if (o.oneofs) - d._topic = "topic"; - } - if (m.peers && m.peers.length) { - d.peers = []; - for (var j = 0; j < m.peers.length; ++j) { - d.peers[j] = $root.PeerInfo.toObject(m.peers[j], o); - } - } - if (m.backoff != null && m.hasOwnProperty("backoff")) { - if (typeof m.backoff === "number") - d.backoff = o.longs === String ? String(m.backoff) : m.backoff; - else - d.backoff = o.longs === String ? $util.Long.prototype.toString.call(m.backoff) : o.longs === Number ? new $util.LongBits(m.backoff.low >>> 0, m.backoff.high >>> 0).toNumber(true) : m.backoff; - if (o.oneofs) - d._backoff = "backoff"; - } - return d; - }; - - /** - * Converts this ControlPrune to JSON. - * @function toJSON - * @memberof ControlPrune - * @instance - * @returns {Object.} JSON object - */ - ControlPrune.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return ControlPrune; -})(); - -export const PeerInfo = $root.PeerInfo = (() => { - - /** - * Properties of a PeerInfo. - * @exports IPeerInfo - * @interface IPeerInfo - * @property {Uint8Array|null} [peerID] PeerInfo peerID - * @property {Uint8Array|null} [signedPeerRecord] PeerInfo signedPeerRecord - */ - - /** - * Constructs a new PeerInfo. - * @exports PeerInfo - * @classdesc Represents a PeerInfo. - * @implements IPeerInfo - * @constructor - * @param {IPeerInfo=} [p] Properties to set - */ - function PeerInfo(p) { - if (p) - for (var ks = Object.keys(p), i = 0; i < ks.length; ++i) - if (p[ks[i]] != null) - this[ks[i]] = p[ks[i]]; - } - - /** - * PeerInfo peerID. - * @member {Uint8Array|null|undefined} peerID - * @memberof PeerInfo - * @instance - */ - PeerInfo.prototype.peerID = null; - - /** - * PeerInfo signedPeerRecord. - * @member {Uint8Array|null|undefined} signedPeerRecord - * @memberof PeerInfo - * @instance - */ - PeerInfo.prototype.signedPeerRecord = null; - - // OneOf field names bound to virtual getters and setters - let $oneOfFields; - - /** - * PeerInfo _peerID. - * @member {"peerID"|undefined} _peerID - * @memberof PeerInfo - * @instance - */ - Object.defineProperty(PeerInfo.prototype, "_peerID", { - get: $util.oneOfGetter($oneOfFields = ["peerID"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * PeerInfo _signedPeerRecord. - * @member {"signedPeerRecord"|undefined} _signedPeerRecord - * @memberof PeerInfo - * @instance - */ - Object.defineProperty(PeerInfo.prototype, "_signedPeerRecord", { - get: $util.oneOfGetter($oneOfFields = ["signedPeerRecord"]), - set: $util.oneOfSetter($oneOfFields) - }); - - /** - * Encodes the specified PeerInfo message. Does not implicitly {@link PeerInfo.verify|verify} messages. - * @function encode - * @memberof PeerInfo - * @static - * @param {IPeerInfo} m PeerInfo message or plain object to encode - * @param {$protobuf.Writer} [w] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - PeerInfo.encode = function encode(m, w) { - if (!w) - w = $Writer.create(); - if (m.peerID != null && Object.hasOwnProperty.call(m, "peerID")) - w.uint32(10).bytes(m.peerID); - if (m.signedPeerRecord != null && Object.hasOwnProperty.call(m, "signedPeerRecord")) - w.uint32(18).bytes(m.signedPeerRecord); - return w; - }; - - /** - * Decodes a PeerInfo message from the specified reader or buffer. - * @function decode - * @memberof PeerInfo - * @static - * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from - * @param {number} [l] Message length if known beforehand - * @returns {PeerInfo} PeerInfo - * @throws {Error} If the payload is not a reader or valid buffer - * @throws {$protobuf.util.ProtocolError} If required fields are missing - */ - PeerInfo.decode = function decode(r, l) { - if (!(r instanceof $Reader)) - r = $Reader.create(r); - var c = l === undefined ? r.len : r.pos + l, m = new $root.PeerInfo(); - while (r.pos < c) { - var t = r.uint32(); - switch (t >>> 3) { - case 1: - m.peerID = r.bytes(); - break; - case 2: - m.signedPeerRecord = r.bytes(); - break; - default: - r.skipType(t & 7); - break; - } - } - return m; - }; - - /** - * Creates a PeerInfo message from a plain object. Also converts values to their respective internal types. - * @function fromObject - * @memberof PeerInfo - * @static - * @param {Object.} d Plain object - * @returns {PeerInfo} PeerInfo - */ - PeerInfo.fromObject = function fromObject(d) { - if (d instanceof $root.PeerInfo) - return d; - var m = new $root.PeerInfo(); - if (d.peerID != null) { - if (typeof d.peerID === "string") - $util.base64.decode(d.peerID, m.peerID = $util.newBuffer($util.base64.length(d.peerID)), 0); - else if (d.peerID.length) - m.peerID = d.peerID; - } - if (d.signedPeerRecord != null) { - if (typeof d.signedPeerRecord === "string") - $util.base64.decode(d.signedPeerRecord, m.signedPeerRecord = $util.newBuffer($util.base64.length(d.signedPeerRecord)), 0); - else if (d.signedPeerRecord.length) - m.signedPeerRecord = d.signedPeerRecord; - } - return m; - }; - - /** - * Creates a plain object from a PeerInfo message. Also converts values to other types if specified. - * @function toObject - * @memberof PeerInfo - * @static - * @param {PeerInfo} m PeerInfo - * @param {$protobuf.IConversionOptions} [o] Conversion options - * @returns {Object.} Plain object - */ - PeerInfo.toObject = function toObject(m, o) { - if (!o) - o = {}; - var d = {}; - if (m.peerID != null && m.hasOwnProperty("peerID")) { - d.peerID = o.bytes === String ? $util.base64.encode(m.peerID, 0, m.peerID.length) : o.bytes === Array ? Array.prototype.slice.call(m.peerID) : m.peerID; - if (o.oneofs) - d._peerID = "peerID"; - } - if (m.signedPeerRecord != null && m.hasOwnProperty("signedPeerRecord")) { - d.signedPeerRecord = o.bytes === String ? $util.base64.encode(m.signedPeerRecord, 0, m.signedPeerRecord.length) : o.bytes === Array ? Array.prototype.slice.call(m.signedPeerRecord) : m.signedPeerRecord; - if (o.oneofs) - d._signedPeerRecord = "signedPeerRecord"; - } - return d; - }; - - /** - * Converts this PeerInfo to JSON. - * @function toJSON - * @memberof PeerInfo - * @instance - * @returns {Object.} JSON object - */ - PeerInfo.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return PeerInfo; -})(); - -export { $root as default }; diff --git a/packages/libp2p-pubsub/test/message/rpc.ts b/packages/libp2p-pubsub/test/message/rpc.ts new file mode 100644 index 000000000..34a638d65 --- /dev/null +++ b/packages/libp2p-pubsub/test/message/rpc.ts @@ -0,0 +1,213 @@ +/* eslint-disable import/export */ +/* eslint-disable @typescript-eslint/no-namespace */ + +import { encodeMessage, decodeMessage, message, bool, string, bytes, uint64 } from 'protons-runtime' + +export interface RPC { + subscriptions: RPC.SubOpts[] + messages: RPC.Message[] + control?: ControlMessage +} + +export namespace RPC { + export interface SubOpts { + subscribe?: boolean + topic?: string + } + + export namespace SubOpts { + export const codec = () => { + return message({ + 1: { name: 'subscribe', codec: bool, optional: true }, + 2: { name: 'topic', codec: string, optional: true } + }) + } + + export const encode = (obj: SubOpts): Uint8Array => { + return encodeMessage(obj, SubOpts.codec()) + } + + export const decode = (buf: Uint8Array): SubOpts => { + return decodeMessage(buf, SubOpts.codec()) + } + } + export interface Message { + from?: Uint8Array + data?: Uint8Array + seqno?: Uint8Array + topic?: string + signature?: Uint8Array + key?: Uint8Array + } + + export namespace Message { + export const codec = () => { + return message({ + 1: { name: 'from', codec: bytes, optional: true }, + 2: { name: 'data', codec: bytes, optional: true }, + 3: { name: 'seqno', codec: bytes, optional: true }, + 4: { name: 'topic', codec: string, optional: true }, + 5: { name: 'signature', codec: bytes, optional: true }, + 6: { name: 'key', codec: bytes, optional: true } + }) + } + + export const encode = (obj: Message): Uint8Array => { + return encodeMessage(obj, Message.codec()) + } + + export const decode = (buf: Uint8Array): Message => { + return decodeMessage(buf, Message.codec()) + } + } + + export const codec = () => { + return message({ + 1: { name: 'subscriptions', codec: RPC.SubOpts.codec(), repeats: true }, + 2: { name: 'messages', codec: RPC.Message.codec(), repeats: true }, + 3: { name: 'control', codec: ControlMessage.codec(), optional: true } + }) + } + + export const encode = (obj: RPC): Uint8Array => { + return encodeMessage(obj, RPC.codec()) + } + + export const decode = (buf: Uint8Array): RPC => { + return decodeMessage(buf, RPC.codec()) + } +} + +export interface ControlMessage { + ihave: ControlIHave[] + iwant: ControlIWant[] + graft: ControlGraft[] + prune: ControlPrune[] +} + +export namespace ControlMessage { + export const codec = () => { + return message({ + 1: { name: 'ihave', codec: ControlIHave.codec(), repeats: true }, + 2: { name: 'iwant', codec: ControlIWant.codec(), repeats: true }, + 3: { name: 'graft', codec: ControlGraft.codec(), repeats: true }, + 4: { name: 'prune', codec: ControlPrune.codec(), repeats: true } + }) + } + + export const encode = (obj: ControlMessage): Uint8Array => { + return encodeMessage(obj, ControlMessage.codec()) + } + + export const decode = (buf: Uint8Array): ControlMessage => { + return decodeMessage(buf, ControlMessage.codec()) + } +} + +export interface ControlIHave { + topic?: string + messageIDs: Uint8Array[] +} + +export namespace ControlIHave { + export const codec = () => { + return message({ + 1: { name: 'topic', codec: string, optional: true }, + 2: { name: 'messageIDs', codec: bytes, repeats: true } + }) + } + + export const encode = (obj: ControlIHave): Uint8Array => { + return encodeMessage(obj, ControlIHave.codec()) + } + + export const decode = (buf: Uint8Array): ControlIHave => { + return decodeMessage(buf, ControlIHave.codec()) + } +} + +export interface ControlIWant { + messageIDs: Uint8Array[] +} + +export namespace ControlIWant { + export const codec = () => { + return message({ + 1: { name: 'messageIDs', codec: bytes, repeats: true } + }) + } + + export const encode = (obj: ControlIWant): Uint8Array => { + return encodeMessage(obj, ControlIWant.codec()) + } + + export const decode = (buf: Uint8Array): ControlIWant => { + return decodeMessage(buf, ControlIWant.codec()) + } +} + +export interface ControlGraft { + topic?: string +} + +export namespace ControlGraft { + export const codec = () => { + return message({ + 1: { name: 'topic', codec: string, optional: true } + }) + } + + export const encode = (obj: ControlGraft): Uint8Array => { + return encodeMessage(obj, ControlGraft.codec()) + } + + export const decode = (buf: Uint8Array): ControlGraft => { + return decodeMessage(buf, ControlGraft.codec()) + } +} + +export interface ControlPrune { + topic?: string + peers: PeerInfo[] + backoff?: bigint +} + +export namespace ControlPrune { + export const codec = () => { + return message({ + 1: { name: 'topic', codec: string, optional: true }, + 2: { name: 'peers', codec: PeerInfo.codec(), repeats: true }, + 3: { name: 'backoff', codec: uint64, optional: true } + }) + } + + export const encode = (obj: ControlPrune): Uint8Array => { + return encodeMessage(obj, ControlPrune.codec()) + } + + export const decode = (buf: Uint8Array): ControlPrune => { + return decodeMessage(buf, ControlPrune.codec()) + } +} + +export interface PeerInfo { + peerID?: Uint8Array + signedPeerRecord?: Uint8Array +} + +export namespace PeerInfo { + export const codec = () => { + return message({ + 1: { name: 'peerID', codec: bytes, optional: true }, + 2: { name: 'signedPeerRecord', codec: bytes, optional: true } + }) + } + + export const encode = (obj: PeerInfo): Uint8Array => { + return encodeMessage(obj, PeerInfo.codec()) + } + + export const decode = (buf: Uint8Array): PeerInfo => { + return decodeMessage(buf, PeerInfo.codec()) + } +} diff --git a/packages/libp2p-pubsub/test/pubsub.spec.ts b/packages/libp2p-pubsub/test/pubsub.spec.ts index f2784cd27..4507022df 100644 --- a/packages/libp2p-pubsub/test/pubsub.spec.ts +++ b/packages/libp2p-pubsub/test/pubsub.spec.ts @@ -1,5 +1,5 @@ /* eslint max-nested-callbacks: ["error", 6] */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pWaitFor from 'p-wait-for' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' diff --git a/packages/libp2p-pubsub/test/sign.spec.ts b/packages/libp2p-pubsub/test/sign.spec.ts index 5f97671a8..94e7e1959 100644 --- a/packages/libp2p-pubsub/test/sign.spec.ts +++ b/packages/libp2p-pubsub/test/sign.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { RPC } from './message/rpc.js' @@ -14,7 +14,7 @@ import type { Message, PubSubRPCMessage } from '@libp2p/interfaces/pubsub' import type { PeerId } from '@libp2p/interfaces/peer-id' function encodeMessage (message: PubSubRPCMessage) { - return RPC.Message.encode(message).finish() + return RPC.Message.encode(message) } describe('message signing', () => { @@ -34,7 +34,7 @@ describe('message signing', () => { topic: 'test-topic' } - const bytesToSign = uint8ArrayConcat([SignPrefix, RPC.Message.encode(toRpcMessage(message)).finish()]) + const bytesToSign = uint8ArrayConcat([SignPrefix, RPC.Message.encode(toRpcMessage(message))]) if (peerId.privateKey == null) { throw new Error('No private key found on PeerId') @@ -67,7 +67,7 @@ describe('message signing', () => { topic: 'test-topic' } - const bytesToSign = uint8ArrayConcat([SignPrefix, RPC.Message.encode(toRpcMessage(message)).finish()]) + const bytesToSign = uint8ArrayConcat([SignPrefix, RPC.Message.encode(toRpcMessage(message))]) if (secPeerId.privateKey == null) { throw new Error('No private key found on PeerId') @@ -98,7 +98,7 @@ describe('message signing', () => { topic: 'test-topic' } - const bytesToSign = uint8ArrayConcat([SignPrefix, RPC.Message.encode(toRpcMessage(message)).finish()]) + const bytesToSign = uint8ArrayConcat([SignPrefix, RPC.Message.encode(toRpcMessage(message))]) if (peerId.privateKey == null) { throw new Error('No private key found on PeerId') diff --git a/packages/libp2p-pubsub/test/topic-validators.spec.ts b/packages/libp2p-pubsub/test/topic-validators.spec.ts index 2df3948cc..b48cd2c77 100644 --- a/packages/libp2p-pubsub/test/topic-validators.spec.ts +++ b/packages/libp2p-pubsub/test/topic-validators.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import sinon from 'sinon' import pWaitFor from 'p-wait-for' import errCode from 'err-code' diff --git a/packages/libp2p-pubsub/test/utils.spec.ts b/packages/libp2p-pubsub/test/utils.spec.ts index c4dbe1ffa..53bddc4fa 100644 --- a/packages/libp2p-pubsub/test/utils.spec.ts +++ b/packages/libp2p-pubsub/test/utils.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import * as utils from '../src/utils.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import type { Message, PubSubRPCMessage } from '@libp2p/interfaces/pubsub' diff --git a/packages/libp2p-pubsub/test/utils/index.ts b/packages/libp2p-pubsub/test/utils/index.ts index 22ec080d6..7e9cf9166 100644 --- a/packages/libp2p-pubsub/test/utils/index.ts +++ b/packages/libp2p-pubsub/test/utils/index.ts @@ -24,7 +24,7 @@ export class PubsubImplementation extends PubSubBaseProtocol { } encodeRpc (rpc: PubSubRPC): Uint8Array { - return RPC.encode(rpc).finish() + return RPC.encode(rpc) } decodeMessage (bytes: Uint8Array): PubSubRPCMessage { @@ -32,7 +32,7 @@ export class PubsubImplementation extends PubSubBaseProtocol { } encodeMessage (rpc: PubSubRPCMessage): Uint8Array { - return RPC.Message.encode(rpc).finish() + return RPC.Message.encode(rpc) } } diff --git a/packages/libp2p-pubsub/tsconfig.json b/packages/libp2p-pubsub/tsconfig.json index d622f4b8a..92794a30c 100644 --- a/packages/libp2p-pubsub/tsconfig.json +++ b/packages/libp2p-pubsub/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-topology/package.json b/packages/libp2p-topology/package.json index 904b9a315..20175cc7e 100644 --- a/packages/libp2p-topology/package.json +++ b/packages/libp2p-topology/package.json @@ -141,9 +141,11 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc" + "dep-check": "aegir dep-check", + "build": "aegir build", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0", @@ -153,6 +155,6 @@ "it-all": "^1.0.6" }, "devDependencies": { - "aegir": "^36.1.3" + "aegir": "^37.0.6" } } diff --git a/packages/libp2p-topology/tsconfig.json b/packages/libp2p-topology/tsconfig.json index 738ecdd06..d96dd69f8 100644 --- a/packages/libp2p-topology/tsconfig.json +++ b/packages/libp2p-topology/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src", diff --git a/packages/libp2p-tracked-map/package.json b/packages/libp2p-tracked-map/package.json index ab089adb5..69b10efe4 100644 --- a/packages/libp2p-tracked-map/package.json +++ b/packages/libp2p-tracked-map/package.json @@ -119,23 +119,24 @@ ] }, "scripts": { + "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js", - "build": "tsc", - "pretest": "npm run build", - "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js", - "test:chrome": "npm run test -- -t browser", - "test:chrome-webworker": "npm run test -- -t webworker", - "test:firefox": "npm run test -- -t browser -- --browser firefox", - "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox", - "test:node": "npm run test -- -t node --cov", - "test:electron-main": "npm run test -- -t electron-main" + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "release": "aegir release" }, "dependencies": { "@libp2p/interfaces": "^1.3.0" }, "devDependencies": { - "aegir": "^36.1.3", + "aegir": "^37.0.6", "sinon": "^13.0.1" } } diff --git a/packages/libp2p-tracked-map/test/index.spec.ts b/packages/libp2p-tracked-map/test/index.spec.ts index 566401c7b..5747c5dde 100644 --- a/packages/libp2p-tracked-map/test/index.spec.ts +++ b/packages/libp2p-tracked-map/test/index.spec.ts @@ -1,4 +1,4 @@ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { trackedMap } from '../src/index.js' import sinon from 'sinon' import type { ComponentMetricsTracker, ComponentMetricsUpdate } from '@libp2p/interfaces/metrics' diff --git a/packages/libp2p-tracked-map/tsconfig.json b/packages/libp2p-tracked-map/tsconfig.json index 4b68f4ffa..d59d66e96 100644 --- a/packages/libp2p-tracked-map/tsconfig.json +++ b/packages/libp2p-tracked-map/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist", - "emitDeclarationOnly": false, - "module": "ES2020" + "outDir": "dist" }, "include": [ "src",