diff --git a/src/connection/connection.js b/src/connection/connection.js index d1ff248..3fb7291 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -2,10 +2,11 @@ const PeerId = require('peer-id') const multiaddr = require('multiaddr') -const withIs = require('class-is') const errCode = require('err-code') const Status = require('./status') +const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection') + function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) { if (localAddr && !multiaddr.isMultiaddr(localAddr)) { throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS') @@ -138,6 +139,24 @@ class Connection { this.tags = [] } + get [Symbol.toStringTag] () { + return 'Connection' + } + + get [connectionSymbol]() { + return true + } + + /** + * Checks if the given value is a `Connection` instance. + * + * @param {any} other + * @returns {other is Connection} + */ + static isConnection(other) { + return Boolean(other && other[connectionSymbol]) + } + /** * Get connection metadata * @this {Connection} @@ -227,8 +246,5 @@ class Connection { } } -/** - * @module - * @type {typeof Connection} - */ -module.exports = withIs(Connection, { className: 'Connection', symbolName: '@libp2p/interface-connection/connection' }) + +module.exports = Connection diff --git a/src/topology/index.js b/src/topology/index.js index 77af929..30c6996 100644 --- a/src/topology/index.js +++ b/src/topology/index.js @@ -1,13 +1,13 @@ 'use strict' -const withIs = require('class-is') const noop = () => {} +const topologySymbol = Symbol.for('@libp2p/js-interfaces/topology') class Topology { /** * @param {Object} props - * @param {number} props.min minimum needed connections (default: 0) - * @param {number} props.max maximum needed connections (default: Infinity) + * @param {number} [props.min] minimum needed connections (default: 0) + * @param {number} [props.max] maximum needed connections (default: Infinity) * @param {Object} [props.handlers] * @param {function} [props.handlers.onConnect] protocol "onConnect" handler * @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler @@ -32,6 +32,24 @@ class Topology { this.peers = new Set() } + get [Symbol.toStringTag] () { + return 'Topology' + } + + get [topologySymbol]() { + return true + } + + /** + * Checks if the given value is a Topology instance. + * + * @param {any} other + * @returns {other is Topology} + */ + static isTopology(other) { + return Boolean(other && other[topologySymbol]) + } + set registrar (registrar) { this._registrar = registrar } @@ -51,8 +69,4 @@ class Topology { } } -/** - * @module - * @type {Topology} - */ -module.exports = withIs(Topology, { className: 'Topology', symbolName: '@libp2p/js-interfaces/topology' }) +module.exports = Topology diff --git a/src/topology/multicodec-topology.js b/src/topology/multicodec-topology.js index 2cdc076..52ddef4 100644 --- a/src/topology/multicodec-topology.js +++ b/src/topology/multicodec-topology.js @@ -1,14 +1,14 @@ 'use strict' -const withIs = require('class-is') - const Topology = require('./index') +const multicodecTopologySymbol = Symbol.for('@libp2p/js-interfaces/topology/multicodec-topology') + class MulticodecTopology extends Topology { /** * @param {Object} props - * @param {number} props.min minimum needed connections (default: 0) - * @param {number} props.max maximum needed connections (default: Infinity) + * @param {number} [props.min] minimum needed connections (default: 0) + * @param {number} [props.max] maximum needed connections (default: Infinity) * @param {Array} props.multicodecs protocol multicodecs * @param {Object} props.handlers * @param {function} props.handlers.onConnect protocol "onConnect" handler @@ -46,6 +46,24 @@ class MulticodecTopology extends Topology { this._onPeerConnect = this._onPeerConnect.bind(this) } + get [Symbol.toStringTag] () { + return 'Topology' + } + + get [multicodecTopologySymbol]() { + return true + } + + /** + * Checks if the given value is a `MulticodecTopology` instance. + * + * @param {any} other + * @returns {other is MulticodecTopology} + */ + static isMulticodecTopology(other) { + return Boolean(other && other[multicodecTopologySymbol]) + } + set registrar (registrar) { this._registrar = registrar this._registrar.peerStore.on('change:protocols', this._onProtocolChange) @@ -120,8 +138,4 @@ class MulticodecTopology extends Topology { } } -/** - * @module - * @type {MulticodecTopology} - */ -module.exports = withIs(MulticodecTopology, { className: 'MulticodecTopology', symbolName: '@libp2p/js-interfaces/topology/multicodec-topology' }) +module.exports = MulticodecTopology