@libp2p/webrtc
Advanced tools
Comparing version 1.1.11 to 1.2.0
import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection'; | ||
import type { CounterGroup } from '@libp2p/interface-metrics'; | ||
import type { Multiaddr } from '@multiformats/multiaddr'; | ||
@@ -17,2 +18,6 @@ import type { Source, Sink } from 'it-stream-types'; | ||
timeline: MultiaddrConnectionTimeline; | ||
/** | ||
* Optional metrics counter group for this connection | ||
*/ | ||
metrics?: CounterGroup; | ||
} | ||
@@ -33,2 +38,6 @@ export declare class WebRTCMultiaddrConnection implements MultiaddrConnection { | ||
/** | ||
* Optional metrics counter group for this connection | ||
*/ | ||
metrics?: CounterGroup; | ||
/** | ||
* The stream source, a no-op as the transport natively supports multiplexing | ||
@@ -35,0 +44,0 @@ */ |
@@ -18,2 +18,6 @@ import { logger } from '@libp2p/logger'; | ||
/** | ||
* Optional metrics counter group for this connection | ||
*/ | ||
metrics; | ||
/** | ||
* The stream source, a no-op as the transport natively supports multiplexing | ||
@@ -38,4 +42,5 @@ */ | ||
this.peerConnection.close(); | ||
this.metrics?.increment({ close: true }); | ||
} | ||
} | ||
//# sourceMappingURL=maconn.js.map |
import type { Stream } from '@libp2p/interface-connection'; | ||
import type { CounterGroup } from '@libp2p/interface-metrics'; | ||
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'; | ||
import type { Source, Sink } from 'it-stream-types'; | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
export interface DataChannelMuxerFactoryInit { | ||
peerConnection: RTCPeerConnection; | ||
metrics?: CounterGroup; | ||
} | ||
export declare class DataChannelMuxerFactory implements StreamMuxerFactory { | ||
@@ -12,3 +17,4 @@ readonly protocol: string; | ||
private streamBuffer; | ||
constructor(peerConnection: RTCPeerConnection, protocol?: string); | ||
private readonly metrics?; | ||
constructor(peerConnection: RTCPeerConnection, metrics?: CounterGroup, protocol?: string); | ||
createStreamMuxer(init?: StreamMuxerInit | undefined): StreamMuxer; | ||
@@ -26,2 +32,6 @@ } | ||
/** | ||
* Optional metrics for this data channel muxer | ||
*/ | ||
private readonly metrics?; | ||
/** | ||
* Array of streams in the data channel | ||
@@ -46,3 +56,3 @@ */ | ||
sink: Sink<Source<Uint8Array | Uint8ArrayList>, Promise<void>>; | ||
constructor(peerConnection: RTCPeerConnection, streams: Stream[], protocol?: string, init?: StreamMuxerInit); | ||
constructor(peerConnection: RTCPeerConnection, streams: Stream[], protocol?: string, init?: StreamMuxerInit, metrics?: CounterGroup); | ||
newStream(): Stream; | ||
@@ -49,0 +59,0 @@ private wrapStreamEnd; |
@@ -10,3 +10,4 @@ import { WebRTCStream } from './stream.js'; | ||
streamBuffer = []; | ||
constructor(peerConnection, protocol = '/webrtc') { | ||
metrics; | ||
constructor(peerConnection, metrics, protocol = '/webrtc') { | ||
this.protocol = protocol; | ||
@@ -28,5 +29,6 @@ this.peerConnection = peerConnection; | ||
}; | ||
this.metrics = metrics; | ||
} | ||
createStreamMuxer(init) { | ||
return new DataChannelMuxer(this.peerConnection, this.streamBuffer, this.protocol, init); | ||
return new DataChannelMuxer(this.peerConnection, this.streamBuffer, this.protocol, init, this.metrics); | ||
} | ||
@@ -44,2 +46,6 @@ } | ||
/** | ||
* Optional metrics for this data channel muxer | ||
*/ | ||
metrics; | ||
/** | ||
* Array of streams in the data channel | ||
@@ -64,3 +70,3 @@ */ | ||
sink = nopSink; | ||
constructor(peerConnection, streams, protocol = '/webrtc', init) { | ||
constructor(peerConnection, streams, protocol = '/webrtc', init, metrics) { | ||
this.protocol = protocol; | ||
@@ -94,2 +100,3 @@ /** | ||
if ((init?.onIncomingStream) != null) { | ||
this.metrics?.increment({ incoming_stream: true }); | ||
init.onIncomingStream(stream); | ||
@@ -113,2 +120,6 @@ } | ||
const channel = this.peerConnection.createDataChannel(''); | ||
const closeCb = (stream) => { | ||
this.metrics?.increment({ stream_end: true }); | ||
this.init?.onStreamEnd?.(stream); | ||
}; | ||
const stream = new WebRTCStream({ | ||
@@ -122,5 +133,6 @@ channel, | ||
}, | ||
closeCb: this.wrapStreamEnd(this.init?.onStreamEnd) | ||
closeCb: this.wrapStreamEnd(closeCb) | ||
}); | ||
this.streams.push(stream); | ||
this.metrics?.increment({ outgoing_stream: true }); | ||
return stream; | ||
@@ -127,0 +139,0 @@ } |
import { type CreateListenerOptions, type Listener, symbol, type Transport } from '@libp2p/interface-transport'; | ||
import type { WebRTCDialOptions } from './options.js'; | ||
import type { Connection } from '@libp2p/interface-connection'; | ||
import type { CounterGroup, Metrics } from '@libp2p/interface-metrics'; | ||
import type { PeerId } from '@libp2p/interface-peer-id'; | ||
@@ -23,7 +24,9 @@ import type { Multiaddr } from '@multiformats/multiaddr'; | ||
peerId: PeerId; | ||
metrics?: Metrics; | ||
} | ||
export interface WebRTCMetrics { | ||
dialerEvents: CounterGroup; | ||
} | ||
export declare class WebRTCDirectTransport implements Transport { | ||
/** | ||
* The peer for this transport | ||
*/ | ||
private readonly metrics?; | ||
private readonly components; | ||
@@ -30,0 +33,0 @@ constructor(components: WebRTCDirectTransportComponents); |
@@ -34,8 +34,14 @@ import { noise as Noise } from '@chainsafe/libp2p-noise'; | ||
export class WebRTCDirectTransport { | ||
/** | ||
* The peer for this transport | ||
*/ | ||
metrics; | ||
components; | ||
constructor(components) { | ||
this.components = components; | ||
if (components.metrics != null) { | ||
this.metrics = { | ||
dialerEvents: components.metrics.registerCounterGroup('libp2p_webrtc_dialer_events_total', { | ||
label: 'event', | ||
help: 'Total count of WebRTC dial events by type' | ||
}) | ||
}; | ||
} | ||
} | ||
@@ -100,2 +106,3 @@ /** | ||
log.error(error); | ||
this.metrics?.dialerEvents.increment({ open_error: true }); | ||
reject(dataChannelError('data', error)); | ||
@@ -113,2 +120,4 @@ }, HANDSHAKE_TIMEOUT_MS); | ||
log.error(error); | ||
// NOTE: We use unknown error here but this could potentially be considered a reset by some standards. | ||
this.metrics?.dialerEvents.increment({ unknown_error: true }); | ||
reject(dataChannelError('data', error)); | ||
@@ -159,3 +168,4 @@ }; | ||
open: Date.now() | ||
} | ||
}, | ||
metrics: this.metrics?.dialerEvents | ||
}); | ||
@@ -179,3 +189,5 @@ const eventListeningName = isFirefox ? 'iceconnectionstatechange' : 'connectionstatechange'; | ||
}, { signal }); | ||
const muxerFactory = new DataChannelMuxerFactory(peerConnection); | ||
// Track opened peer connection | ||
this.metrics?.dialerEvents.increment({ peer_connection: true }); | ||
const muxerFactory = new DataChannelMuxerFactory(peerConnection, this.metrics?.dialerEvents); | ||
// For outbound connections, the remote is expected to start the noise handshake. | ||
@@ -182,0 +194,0 @@ // Therefore, we need to secure an inbound noise connection from the remote. |
{ | ||
"name": "@libp2p/webrtc", | ||
"version": "1.1.11", | ||
"version": "1.2.0", | ||
"description": "A libp2p transport using WebRTC connections", | ||
@@ -136,3 +136,3 @@ "author": "", | ||
"clean": "aegir clean", | ||
"dep-check": "aegir dep-check", | ||
"dep-check": "aegir dep-check -i protons", | ||
"release": "aegir release" | ||
@@ -143,2 +143,3 @@ }, | ||
"@libp2p/interface-connection": "^5.0.2", | ||
"@libp2p/interface-metrics": "^4.0.8", | ||
"@libp2p/interface-peer-id": "^2.0.2", | ||
@@ -174,8 +175,9 @@ "@libp2p/interface-peer-store": "^2.0.2", | ||
"@types/sinon": "^10.0.14", | ||
"aegir": "^39.0.5", | ||
"aegir": "^39.0.6", | ||
"eslint-plugin-etc": "^2.0.2", | ||
"it-pair": "^2.0.6", | ||
"protons": "^7.0.2", | ||
"sinon": "^15.0.4" | ||
"sinon": "^15.0.4", | ||
"sinon-ts": "^1.0.0" | ||
} | ||
} |
import { logger } from '@libp2p/logger' | ||
import { nopSink, nopSource } from './util.js' | ||
import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection' | ||
import type { CounterGroup } from '@libp2p/interface-metrics' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
@@ -24,2 +25,7 @@ import type { Source, Sink } from 'it-stream-types' | ||
timeline: MultiaddrConnectionTimeline | ||
/** | ||
* Optional metrics counter group for this connection | ||
*/ | ||
metrics?: CounterGroup | ||
} | ||
@@ -44,2 +50,7 @@ | ||
/** | ||
* Optional metrics counter group for this connection | ||
*/ | ||
metrics?: CounterGroup | ||
/** | ||
* The stream source, a no-op as the transport natively supports multiplexing | ||
@@ -64,3 +75,2 @@ */ | ||
} | ||
log.trace('closing connection') | ||
@@ -70,3 +80,4 @@ | ||
this.peerConnection.close() | ||
this.metrics?.increment({ close: true }) | ||
} | ||
} |
import { WebRTCStream } from './stream.js' | ||
import { nopSink, nopSource } from './util.js' | ||
import type { Stream } from '@libp2p/interface-connection' | ||
import type { CounterGroup } from '@libp2p/interface-metrics' | ||
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer' | ||
@@ -8,2 +9,7 @@ import type { Source, Sink } from 'it-stream-types' | ||
export interface DataChannelMuxerFactoryInit { | ||
peerConnection: RTCPeerConnection | ||
metrics?: CounterGroup | ||
} | ||
export class DataChannelMuxerFactory implements StreamMuxerFactory { | ||
@@ -15,4 +21,5 @@ /** | ||
private streamBuffer: WebRTCStream[] = [] | ||
private readonly metrics?: CounterGroup | ||
constructor (peerConnection: RTCPeerConnection, readonly protocol = '/webrtc') { | ||
constructor (peerConnection: RTCPeerConnection, metrics?: CounterGroup, readonly protocol = '/webrtc') { | ||
this.peerConnection = peerConnection | ||
@@ -33,6 +40,7 @@ // store any datachannels opened before upgrade has been completed | ||
} | ||
this.metrics = metrics | ||
} | ||
createStreamMuxer (init?: StreamMuxerInit | undefined): StreamMuxer { | ||
return new DataChannelMuxer(this.peerConnection, this.streamBuffer, this.protocol, init) | ||
return new DataChannelMuxer(this.peerConnection, this.streamBuffer, this.protocol, init, this.metrics) | ||
} | ||
@@ -51,2 +59,7 @@ } | ||
/** | ||
* Optional metrics for this data channel muxer | ||
*/ | ||
private readonly metrics?: CounterGroup | ||
/** | ||
* Array of streams in the data channel | ||
@@ -76,3 +89,3 @@ */ | ||
constructor (peerConnection: RTCPeerConnection, streams: Stream[], readonly protocol = '/webrtc', init?: StreamMuxerInit) { | ||
constructor (peerConnection: RTCPeerConnection, streams: Stream[], readonly protocol: string = '/webrtc', init?: StreamMuxerInit, metrics?: CounterGroup) { | ||
/** | ||
@@ -108,2 +121,3 @@ * Initialized stream muxer | ||
if ((init?.onIncomingStream) != null) { | ||
this.metrics?.increment({ incoming_stream: true }) | ||
init.onIncomingStream(stream) | ||
@@ -129,2 +143,6 @@ } | ||
const channel = this.peerConnection.createDataChannel('') | ||
const closeCb = (stream: Stream): void => { | ||
this.metrics?.increment({ stream_end: true }) | ||
this.init?.onStreamEnd?.(stream) | ||
} | ||
const stream = new WebRTCStream({ | ||
@@ -138,5 +156,6 @@ channel, | ||
}, | ||
closeCb: this.wrapStreamEnd(this.init?.onStreamEnd) | ||
closeCb: this.wrapStreamEnd(closeCb) | ||
}) | ||
this.streams.push(stream) | ||
this.metrics?.increment({ outgoing_stream: true }) | ||
@@ -143,0 +162,0 @@ return stream |
@@ -18,2 +18,3 @@ import { noise as Noise } from '@chainsafe/libp2p-noise' | ||
import type { Connection } from '@libp2p/interface-connection' | ||
import type { CounterGroup, Metrics } from '@libp2p/interface-metrics' | ||
import type { PeerId } from '@libp2p/interface-peer-id' | ||
@@ -46,11 +47,13 @@ import type { Multiaddr } from '@multiformats/multiaddr' | ||
*/ | ||
// @TODO(ddimaria): seems like an unnessary abstraction, consider removing | ||
export interface WebRTCDirectTransportComponents { | ||
peerId: PeerId | ||
metrics?: Metrics | ||
} | ||
export interface WebRTCMetrics { | ||
dialerEvents: CounterGroup | ||
} | ||
export class WebRTCDirectTransport implements Transport { | ||
/** | ||
* The peer for this transport | ||
*/ | ||
private readonly metrics?: WebRTCMetrics | ||
private readonly components: WebRTCDirectTransportComponents | ||
@@ -60,2 +63,10 @@ | ||
this.components = components | ||
if (components.metrics != null) { | ||
this.metrics = { | ||
dialerEvents: components.metrics.registerCounterGroup('libp2p_webrtc_dialer_events_total', { | ||
label: 'event', | ||
help: 'Total count of WebRTC dial events by type' | ||
}) | ||
} | ||
} | ||
} | ||
@@ -131,2 +142,3 @@ | ||
log.error(error) | ||
this.metrics?.dialerEvents.increment({ open_error: true }) | ||
reject(dataChannelError('data', error)) | ||
@@ -146,2 +158,4 @@ }, HANDSHAKE_TIMEOUT_MS) | ||
log.error(error) | ||
// NOTE: We use unknown error here but this could potentially be considered a reset by some standards. | ||
this.metrics?.dialerEvents.increment({ unknown_error: true }) | ||
reject(dataChannelError('data', error)) | ||
@@ -201,3 +215,4 @@ } | ||
open: Date.now() | ||
} | ||
}, | ||
metrics: this.metrics?.dialerEvents | ||
}) | ||
@@ -224,4 +239,7 @@ | ||
const muxerFactory = new DataChannelMuxerFactory(peerConnection) | ||
// Track opened peer connection | ||
this.metrics?.dialerEvents.increment({ peer_connection: true }) | ||
const muxerFactory = new DataChannelMuxerFactory(peerConnection, this.metrics?.dialerEvents) | ||
// For outbound connections, the remote is expected to start the noise handshake. | ||
@@ -228,0 +246,0 @@ // Therefore, we need to secure an inbound noise connection from the remote. |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
647517
5407
27
10