diff --git a/package.json b/package.json index 9b9041b..a3de50b 100644 --- a/package.json +++ b/package.json @@ -27,12 +27,10 @@ "test": "aegir test", "test:node": "aegir test --target node", "test:browser": "aegir test --target browser", - "test:types": "aegir ts -p check", "prepublishOnly": "npm run generate:types", "release": "aegir release -t node -t browser", "release-minor": "aegir release --type minor -t node -t browser", - "release-major": "aegir release --type major -t node -t browser", - "remove:types": "rimraf './src/**/*.d.ts'" + "release-major": "aegir release --type major -t node -t browser" }, "repository": { "type": "git", @@ -49,6 +47,7 @@ }, "homepage": "https://github.com/libp2p/js-interfaces#readme", "dependencies": { + "@types/bl": "^2.1.0", "abort-controller": "^3.0.0", "abortable-iterator": "^3.0.0", "chai": "^4.2.0", diff --git a/src/pubsub/index.js b/src/pubsub/index.js index 9982db7..0ff111c 100644 --- a/src/pubsub/index.js +++ b/src/pubsub/index.js @@ -21,6 +21,17 @@ const { verifySignature } = require('./message/sign') +/** + * @typedef {any} Libp2p + * @typedef {import('peer-id')} PeerId + * @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream + * @typedef {import('../connection/connection')} Connection + * @typedef {import('./message').RPC} RPC + * @typedef {import('./message').SubOpts} RPCSubOpts + * @typedef {import('./message').Message} RPCMessage + * @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType + */ + /** * @typedef {Object} InMessage * @property {string} [from] @@ -30,9 +41,6 @@ const { * @property {Uint8Array} data * @property {Uint8Array} [signature] * @property {Uint8Array} [key] - * - * @typedef PeerId - * @type import('peer-id') */ /** @@ -213,7 +221,7 @@ class PubsubBaseProtocol extends EventEmitter { /** * On an inbound stream opened. * - * @private + * @protected * @param {Object} props * @param {string} props.protocol * @param {MuxedStream} props.stream @@ -231,7 +239,7 @@ class PubsubBaseProtocol extends EventEmitter { /** * Registrar notifies an established connection with pubsub protocol. * - * @private + * @protected * @param {PeerId} peerId - remote peer-id * @param {Connection} conn - connection to the peer */ @@ -254,7 +262,7 @@ class PubsubBaseProtocol extends EventEmitter { /** * Registrar notifies a closing connection with pubsub protocol. * - * @private + * @protected * @param {PeerId} peerId - peerId * @param {Error} [err] - error for connection end */ @@ -268,7 +276,7 @@ class PubsubBaseProtocol extends EventEmitter { /** * Notifies the router that a peer has been connected * - * @private + * @protected * @param {PeerId} peerId * @param {string} protocol * @returns {PeerStreams} @@ -299,7 +307,7 @@ class PubsubBaseProtocol extends EventEmitter { /** * Notifies the router that a peer has been disconnected. * - * @private + * @protected * @param {PeerId} peerId * @returns {PeerStreams | undefined} */ @@ -341,7 +349,6 @@ class PubsubBaseProtocol extends EventEmitter { stream, async (source) => { for await (const data of source) { - // @ts-ignore data slice from BufferList const rpcBytes = data instanceof Uint8Array ? data : data.slice() const rpcMsg = this._decodeRpc(rpcBytes) @@ -589,7 +596,7 @@ class PubsubBaseProtocol extends EventEmitter { * Normalizes the message and signs it, if signing is enabled. * Should be used by the routers to create the message to send. * - * @private + * @protected * @param {RPCMessage} message * @returns {Promise} */ @@ -728,17 +735,6 @@ class PubsubBaseProtocol extends EventEmitter { } } -/** - * @typedef {any} Libp2p - * @typedef {object} MuxedStream - * @type import('../stream-muxer/types').MuxedStream - * @typedef {import('../connection/connection')} Connection - * @typedef {import('./message').RPC} RPC - * @typedef {import('./message').SubOpts} RPCSubOpts - * @typedef {import('./message').Message} RPCMessage - * @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType - */ - PubsubBaseProtocol.message = message PubsubBaseProtocol.utils = utils PubsubBaseProtocol.SignaturePolicy = SignaturePolicy diff --git a/src/pubsub/message/sign.js b/src/pubsub/message/sign.js index 4879562..5424c55 100644 --- a/src/pubsub/message/sign.js +++ b/src/pubsub/message/sign.js @@ -65,7 +65,10 @@ async function verifySignature (message) { */ async function messagePublicKey (message) { // should be available in the from property of the message (peer id) - // @ts-ignore - from type changed + if (!message.from) { + throw new Error('Could not get the public key from the originator id') + } + const from = PeerId.createFromCID(message.from) if (message.key) { diff --git a/src/pubsub/peer-streams.js b/src/pubsub/peer-streams.js index 4f35cda..976557c 100644 --- a/src/pubsub/peer-streams.js +++ b/src/pubsub/peer-streams.js @@ -16,18 +16,9 @@ const log = debug('libp2p-pubsub:peer-streams') log.error = debug('libp2p-pubsub:peer-streams:error') /** - * @callback Sink - * @param {Uint8Array} source - * @returns {Promise} - * - * @typedef {object} MuxedStream - * @type import('../stream-muxer/types').MuxedStream - * - * @typedef PeerId - * @type import('peer-id') - * - * @typedef PushableStream - * @type import('it-pushable').Pushable + * @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream + * @typedef {import('peer-id')} PeerId + * @typedef {import('it-pushable').Pushable} PushableStream */ /** diff --git a/src/stream-muxer/types.ts b/src/stream-muxer/types.ts index 6902868..0c1884a 100644 --- a/src/stream-muxer/types.ts +++ b/src/stream-muxer/types.ts @@ -1,3 +1,5 @@ +import BufferList from 'bl' + /** * A libp2p stream muxer */ @@ -5,6 +7,7 @@ export interface Muxer { new (options: MuxerOptions): Muxer; // eslint-disable-line multicodec: string; readonly streams: Array; + prototype: Muxer; /** * Initiate a new stream with the given name. If no name is * provided, the id of th stream will be used. @@ -33,15 +36,14 @@ export type MuxedTimeline = { close?: number; } -export type MuxedStream = { +export interface MuxedStream extends AsyncIterable { close: () => void; abort: () => void; reset: () => void; sink: Sink; - source: () => AsyncIterable; + source: () => AsyncIterable; timeline: MuxedTimeline; id: string; - [Symbol.asyncIterator](): AsyncIterator; } -type Sink = (source: Uint8Array) => Promise; +export type Sink = (source: Uint8Array) => Promise; diff --git a/src/transport/types.ts b/src/transport/types.ts index 0716406..9365c40 100644 --- a/src/transport/types.ts +++ b/src/transport/types.ts @@ -1,12 +1,14 @@ import events from 'events' import Multiaddr from 'multiaddr' import Connection from '../connection/connection' +import { Sink } from '../stream-muxer/types' /** * A libp2p transport is understood as something that offers a dial and listen interface to establish connections. */ export interface Transport { new (upgrader: Upgrader, ...others: any): Transport; // eslint-disable-line + prototype: Transport; /** * Dial a given multiaddr. */ @@ -65,5 +67,3 @@ export type MultiaddrConnection = { localAddr?: Multiaddr; timeline: MultiaddrConnectionTimeline; } - -export type Sink = (source: Uint8Array) => Promise;