diff --git a/package.json b/package.json index 245e2ea..fe4c612 100644 --- a/package.json +++ b/package.json @@ -137,43 +137,42 @@ "release": "aegir release" }, "dependencies": { - "@chainsafe/libp2p-noise": "^11.0.0", - "@libp2p/interface-connection": "^4.0.0", - "@libp2p/interface-peer-id": "^2.0.0", - "@libp2p/interface-peer-store": "^2.0.0", - "@libp2p/interface-registrar": "^2.0.8", - "@libp2p/interface-stream-muxer": "^3.0.0", - "@libp2p/interface-transport": "^2.0.0", - "@libp2p/interfaces": "^3.2.0", - "@libp2p/logger": "^2.0.0", - "@libp2p/peer-id": "^2.0.0", - "@multiformats/multiaddr": "^12.1.1", - "@protobuf-ts/runtime": "^2.8.0", + "@chainsafe/libp2p-noise": "^11.0.4", + "@libp2p/interface-connection": "^5.0.2", + "@libp2p/interface-peer-id": "^2.0.2", + "@libp2p/interface-peer-store": "^2.0.2", + "@libp2p/interface-registrar": "^2.0.12", + "@libp2p/interface-stream-muxer": "^4.0.1", + "@libp2p/interface-transport": "^4.0.2", + "@libp2p/interfaces": "^3.3.2", + "@libp2p/logger": "^2.0.7", + "@libp2p/peer-id": "^2.0.3", + "@multiformats/multiaddr": "^12.1.2", + "@protobuf-ts/runtime": "^2.9.0", "abortable-iterator": "^4.0.2", - "err-code": "^3.0.1", + "detect-browser": "^5.3.0", "it-length-prefixed": "^8.0.3", - "it-merge": "^2.0.0", - "it-pb-stream": "^3.2.1", + "it-merge": "^3.0.0", + "it-pb-stream": "^4.0.1", "it-pipe": "^3.0.1", - "it-pushable": "^3.1.0", - "it-stream-types": "^1.0.4", + "it-pushable": "^3.1.3", + "it-stream-types": "^2.0.1", "multiformats": "^11.0.2", "multihashes": "^4.0.3", "p-defer": "^4.0.0", "protons-runtime": "^5.0.0", - "uint8arraylist": "^2.3.3", - "uint8arrays": "^4.0.2", - "detect-browser": "^5.3.0" + "uint8arraylist": "^2.4.3", + "uint8arrays": "^4.0.3" }, "devDependencies": { - "@libp2p/interface-mocks": "^9.0.0", - "@libp2p/peer-id-factory": "^2.0.0", - "@protobuf-ts/plugin": "^2.8.0", - "@protobuf-ts/protoc": "^2.8.0", - "aegir": "^38.1.6", + "@libp2p/interface-mocks": "^11.0.2", + "@libp2p/peer-id-factory": "^2.0.3", + "@protobuf-ts/protoc": "^2.9.0", + "@types/sinon": "^10.0.14", + "aegir": "^39.0.5", "eslint-plugin-etc": "^2.0.2", - "it-pair": "^2.0.3", + "it-pair": "^2.0.6", "protons": "^7.0.2", - "sinon": "^15.0.1" + "sinon": "^15.0.4" } } diff --git a/src/index.ts b/src/index.ts index fcf78b0..2814c8b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ -import type { Transport } from '@libp2p/interface-transport' -import type { WebRTCTransportComponents, WebRTCTransportInit } from './peer_transport/transport.js' import { WebRTCTransport } from './peer_transport/transport.js' -import { WebRTCDirectTransport, WebRTCDirectTransportComponents } from './transport.js' +import { WebRTCDirectTransport, type WebRTCDirectTransportComponents } from './transport.js' +import type { WebRTCTransportComponents, WebRTCTransportInit } from './peer_transport/transport.js' +import type { Transport } from '@libp2p/interface-transport' function webRTCDirect (): (components: WebRTCDirectTransportComponents) => Transport { return (components: WebRTCDirectTransportComponents) => new WebRTCDirectTransport(components) diff --git a/src/maconn.ts b/src/maconn.ts index 8720b8e..f977a00 100644 --- a/src/maconn.ts +++ b/src/maconn.ts @@ -1,10 +1,9 @@ -import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection' import { logger } from '@libp2p/logger' +import { nopSink, nopSource } from './util.js' +import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection' import type { Multiaddr } from '@multiformats/multiaddr' import type { Source, Sink } from 'it-stream-types' -import { nopSink, nopSource } from './util.js' - const log = logger('libp2p:webrtc:connection') interface WebRTCMultiaddrConnectionInit { @@ -43,12 +42,12 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection { /** * The stream source, a no-op as the transport natively supports multiplexing */ - source: Source = nopSource + source: AsyncGenerator = nopSource() /** * The stream destination, a no-op as the transport natively supports multiplexing */ - sink: Sink> = nopSink + sink: Sink, Promise> = nopSink constructor (init: WebRTCMultiaddrConnectionInit) { this.remoteAddr = init.remoteAddr diff --git a/src/muxer.ts b/src/muxer.ts index b1b55be..2522878 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -1,9 +1,9 @@ +import { WebRTCStream } from './stream.js' +import { nopSink, nopSource } from './util.js' import type { Stream } from '@libp2p/interface-connection' import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer' import type { Source, Sink } from 'it-stream-types' - -import { WebRTCStream } from './stream.js' -import { nopSink, nopSource } from './util.js' +import type { Uint8ArrayList } from 'uint8arraylist' export class DataChannelMuxerFactory implements StreamMuxerFactory { /** @@ -62,12 +62,12 @@ export class DataChannelMuxer implements StreamMuxer { /** * The stream source, a no-op as the transport natively supports multiplexing */ - source: Source = nopSource + source: AsyncGenerator = nopSource() /** * The stream destination, a no-op as the transport natively supports multiplexing */ - sink: Sink> = nopSink + sink: Sink, Promise> = nopSink constructor (peerConnection: RTCPeerConnection, streams: Stream[], readonly protocol = '/webrtc', init?: StreamMuxerInit) { /** diff --git a/src/peer_transport/handler.ts b/src/peer_transport/handler.ts index 7a0275a..6d13856 100644 --- a/src/peer_transport/handler.ts +++ b/src/peer_transport/handler.ts @@ -1,13 +1,13 @@ -import type { IncomingStreamData } from '@libp2p/interface-registrar' +import { logger } from '@libp2p/logger' +import { abortableDuplex } from 'abortable-iterator' import { pbStream } from 'it-pb-stream' import pDefer, { type DeferredPromise } from 'p-defer' -import { readCandidatesUntilConnected, resolveOnConnected } from './util.js' +import { DataChannelMuxerFactory } from '../muxer.js' import * as pb from './pb/index.js' -import { abortableDuplex } from 'abortable-iterator' -import { logger } from '@libp2p/logger' +import { readCandidatesUntilConnected, resolveOnConnected } from './util.js' import type { Stream } from '@libp2p/interface-connection' +import type { IncomingStreamData } from '@libp2p/interface-registrar' import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer' -import { DataChannelMuxerFactory } from '../muxer.js' const DEFAULT_TIMEOUT = 30 * 1000 diff --git a/src/peer_transport/listener.ts b/src/peer_transport/listener.ts index 3edf328..4f2ad80 100644 --- a/src/peer_transport/listener.ts +++ b/src/peer_transport/listener.ts @@ -1,9 +1,9 @@ -import type { PeerId } from '@libp2p/interface-peer-id' -import type { ListenerEvents, TransportManager, Upgrader, Listener } from '@libp2p/interface-transport' import { EventEmitter } from '@libp2p/interfaces/events' -import { multiaddr, Multiaddr } from '@multiformats/multiaddr' +import { multiaddr, type Multiaddr } from '@multiformats/multiaddr' import { inappropriateMultiaddr } from '../error.js' import { TRANSPORT } from './transport.js' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { ListenerEvents, TransportManager, Upgrader, Listener } from '@libp2p/interface-transport' export interface ListenerOptions { peerId: PeerId diff --git a/src/peer_transport/pb/index.ts b/src/peer_transport/pb/index.ts index 627fd35..d2721ae 100644 --- a/src/peer_transport/pb/index.ts +++ b/src/peer_transport/pb/index.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface Message { type?: Message.Type diff --git a/src/peer_transport/transport.ts b/src/peer_transport/transport.ts index 98a132b..ac848c6 100644 --- a/src/peer_transport/transport.ts +++ b/src/peer_transport/transport.ts @@ -1,18 +1,17 @@ -import type { Connection } from '@libp2p/interface-connection' -import { CreateListenerOptions, DialOptions, Listener, symbol, Transport } from '@libp2p/interface-transport' -import type { TransportManager, Upgrader } from '@libp2p/interface-transport' -import { multiaddr, Multiaddr, protocols } from '@multiformats/multiaddr' -import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar' -import type { PeerId } from '@libp2p/interface-peer-id' +import { type CreateListenerOptions, type DialOptions, type Listener, symbol, type Transport, type TransportManager, type Upgrader } from '@libp2p/interface-transport' +import { CodeError } from '@libp2p/interfaces/errors' +import { logger } from '@libp2p/logger' import { peerIdFromString } from '@libp2p/peer-id' +import { multiaddr, type Multiaddr, protocols } from '@multiformats/multiaddr' +import { codes } from '../error.js' import { WebRTCMultiaddrConnection } from '../maconn.js' -import type { Startable } from '@libp2p/interfaces/startable' +import { initiateConnection, handleIncomingStream } from './handler.js' import { WebRTCPeerListener } from './listener.js' +import type { Connection } from '@libp2p/interface-connection' +import type { PeerId } from '@libp2p/interface-peer-id' import type { PeerStore } from '@libp2p/interface-peer-store' -import { logger } from '@libp2p/logger' -import { initiateConnection, handleIncomingStream } from './handler.js' -import { CodeError } from '@libp2p/interfaces/errors' -import { codes } from '../error.js' +import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar' +import type { Startable } from '@libp2p/interfaces/startable' const log = logger('libp2p:webrtc:peer') @@ -46,7 +45,7 @@ export class WebRTCTransport implements Transport, Startable { } async start (): Promise { - await this.components.registrar.handle(SIGNALING_PROTO_ID, (data) => { + await this.components.registrar.handle(SIGNALING_PROTO_ID, (data: IncomingStreamData) => { this._onProtocol(data).catch(err => { log.error('failed to handle incoming connect from %p', data.connection.remotePeer, err) }) }) this._started = true @@ -61,13 +60,9 @@ export class WebRTCTransport implements Transport, Startable { return new WebRTCPeerListener(this.components) } - get [Symbol.toStringTag] (): string { - return '@libp2p/webrtc' - } + readonly [Symbol.toStringTag] = '@libp2p/webrtc' - get [symbol] (): true { - return true - } + readonly [symbol] = true filter (multiaddrs: Multiaddr[]): Multiaddr[] { return multiaddrs.filter((ma) => { diff --git a/src/peer_transport/util.ts b/src/peer_transport/util.ts index 627d123..d0811c2 100644 --- a/src/peer_transport/util.ts +++ b/src/peer_transport/util.ts @@ -1,7 +1,7 @@ import { logger } from '@libp2p/logger' -import type { DeferredPromise } from 'p-defer' -import * as pb from './pb/index.js' import { detect } from 'detect-browser' +import * as pb from './pb/index.js' +import type { DeferredPromise } from 'p-defer' const browser = detect() export const isFirefox = ((browser != null) && browser.name === 'firefox') diff --git a/src/sdp.ts b/src/sdp.ts index 1f3ac5a..f5ee546 100644 --- a/src/sdp.ts +++ b/src/sdp.ts @@ -1,11 +1,10 @@ import { logger } from '@libp2p/logger' -import type { Multiaddr } from '@multiformats/multiaddr' import { bases } from 'multiformats/basics' import * as multihashes from 'multihashes' -import type { HashCode, HashName } from 'multihashes' - import { inappropriateMultiaddr, invalidArgument, invalidFingerprint, unsupportedHashAlgorithm } from './error.js' import { CERTHASH_CODE } from './transport.js' +import type { Multiaddr } from '@multiformats/multiaddr' +import type { HashCode, HashName } from 'multihashes' const log = logger('libp2p:webrtc:sdp') diff --git a/src/stream.ts b/src/stream.ts index 4124704..d1a4413 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,14 +1,13 @@ -import type { Stream, StreamStat, Direction } from '@libp2p/interface-connection' import { logger } from '@libp2p/logger' import * as lengthPrefixed from 'it-length-prefixed' import merge from 'it-merge' import { pipe } from 'it-pipe' import { pushable } from 'it-pushable' -import defer, { DeferredPromise } from 'p-defer' -import type { Source } from 'it-stream-types' +import defer, { type DeferredPromise } from 'p-defer' import { Uint8ArrayList } from 'uint8arraylist' - import * as pb from '../proto_ts/message.js' +import type { Stream, StreamStat, Direction } from '@libp2p/interface-connection' +import type { Source } from 'it-stream-types' const log = logger('libp2p:webrtc:stream') @@ -182,7 +181,7 @@ export class WebRTCStream implements Stream { * Read unwrapped protobuf data from the underlying datachannel. * _src is exposed to the user via the `source` getter to . */ - private readonly _src: Source + private readonly _src: AsyncGenerator /** * push data from the underlying datachannel to the length prefix decoder @@ -282,9 +281,9 @@ export class WebRTCStream implements Stream { } // If user attempts to set a new source this should be a noop - set source (_src: Source) { } + set source (_src: AsyncGenerator) { } - get source (): Source { + get source (): AsyncGenerator { return this._src } diff --git a/src/transport.ts b/src/transport.ts index d143c7b..d10c872 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -1,22 +1,22 @@ import { noise as Noise } from '@chainsafe/libp2p-noise' -import type { Connection } from '@libp2p/interface-connection' -import type { PeerId } from '@libp2p/interface-peer-id' -import { CreateListenerOptions, Listener, symbol, Transport } from '@libp2p/interface-transport' +import { type CreateListenerOptions, type Listener, symbol, type Transport } from '@libp2p/interface-transport' import { logger } from '@libp2p/logger' import * as p from '@libp2p/peer-id' -import type { Multiaddr } from '@multiformats/multiaddr' +import { protocols } from '@multiformats/multiaddr' import * as multihashes from 'multihashes' -import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { concat } from 'uint8arrays/concat' +import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from './error.js' import { WebRTCMultiaddrConnection } from './maconn.js' import { DataChannelMuxerFactory } from './muxer.js' -import type { WebRTCDialOptions } from './options.js' +import { isFirefox } from './peer_transport/util.js' import * as sdp from './sdp.js' import { WebRTCStream } from './stream.js' import { genUfrag } from './util.js' -import { protocols } from '@multiformats/multiaddr' -import { isFirefox } from './peer_transport/util.js' +import type { WebRTCDialOptions } from './options.js' +import type { Connection } from '@libp2p/interface-connection' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { Multiaddr } from '@multiformats/multiaddr' const log = logger('libp2p:webrtc:transport') @@ -83,16 +83,12 @@ export class WebRTCDirectTransport implements Transport { /** * Implement toString() for WebRTCTransport */ - get [Symbol.toStringTag] (): string { - return '@libp2p/webrtc-direct' - } + readonly [Symbol.toStringTag] = '@libp2p/webrtc-direct' /** * Symbol.for('@libp2p/transport') */ - get [symbol] (): true { - return true - } + readonly [symbol] = true /** * Connect to a peer using a multiaddr @@ -225,7 +221,7 @@ export class WebRTCDirectTransport implements Transport { // Therefore, we need to secure an inbound noise connection from the remote. await noise.secureInbound(myPeerId, wrappedDuplex, theirPeerId) - return await options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory }) + return options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory }) } /** diff --git a/src/util.ts b/src/util.ts index 71cf481..c930531 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,6 +1,4 @@ -export const nopSource = { - async * [Symbol.asyncIterator] () {} -} +export const nopSource = async function * nop (): AsyncGenerator {} export const nopSink = async (_: any): Promise => {} diff --git a/test/peer.browser.spec.ts b/test/peer.browser.spec.ts index 6c1dcde..eef48f3 100644 --- a/test/peer.browser.spec.ts +++ b/test/peer.browser.spec.ts @@ -2,6 +2,7 @@ import { mockConnection, mockMultiaddrConnection, mockRegistrar, mockStream, moc import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' +import { detect } from 'detect-browser' import { pair } from 'it-pair' import { duplexPair } from 'it-pair/duplex' import { pbStream } from 'it-pb-stream' @@ -9,7 +10,6 @@ import sinon from 'sinon' import { initiateConnection, handleIncomingStream } from '../src/peer_transport/handler' import { Message } from '../src/peer_transport/pb/index.js' import { WebRTCTransport } from '../src/peer_transport/transport' -import { detect } from 'detect-browser' const browser = detect() @@ -98,7 +98,7 @@ describe('webrtc filter', () => { transportManager: sinon.stub() as any, peerId: sinon.stub() as any, registrar: mockRegistrar(), - upgrader: mockUpgrader(), + upgrader: mockUpgrader({}), peerStore: sinon.stub() as any }, {}) diff --git a/test/stream.browser.spec.ts b/test/stream.browser.spec.ts index ce606cb..8b5375b 100644 --- a/test/stream.browser.spec.ts +++ b/test/stream.browser.spec.ts @@ -1,7 +1,7 @@ -import * as underTest from '../src/stream' import { expect, assert } from 'aegir/chai' import { bytes } from 'multiformats' import * as pb from '../proto_ts/message.js' +import * as underTest from '../src/stream' const TEST_MESSAGE = 'test_messgae' function setup (): { peerConnection: RTCPeerConnection, datachannel: RTCDataChannel, webrtcStream: underTest.WebRTCStream } { diff --git a/test/transport.browser.spec.ts b/test/transport.browser.spec.ts index ec7f1ef..b270d3f 100644 --- a/test/transport.browser.spec.ts +++ b/test/transport.browser.spec.ts @@ -1,13 +1,13 @@ /* eslint-disable @typescript-eslint/no-floating-promises */ -import * as underTest from './../src/transport.js' -import { expectError } from './util.js' -import { UnimplementedError } from './../src/error.js' import { mockUpgrader } from '@libp2p/interface-mocks' -import { CreateListenerOptions, symbol } from '@libp2p/interface-transport' -import { multiaddr, Multiaddr } from '@multiformats/multiaddr' +import { type CreateListenerOptions, symbol } from '@libp2p/interface-transport' import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { multiaddr, type Multiaddr } from '@multiformats/multiaddr' import { expect, assert } from 'aegir/chai' +import { UnimplementedError } from './../src/error.js' +import * as underTest from './../src/transport.js' +import { expectError } from './util.js' function ignoredDialOption (): CreateListenerOptions { const upgrader = mockUpgrader({})