@libp2p/webrtc 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +16 -16
- package/dist/src/peer_transport/transport.d.ts +1 -0
- package/dist/src/peer_transport/transport.d.ts.map +1 -1
- package/dist/src/peer_transport/transport.js +24 -16
- package/dist/src/peer_transport/transport.js.map +1 -1
- package/package.json +2 -2
- package/src/peer_transport/transport.ts +27 -17
|
@@ -31,6 +31,7 @@ export declare class WebRTCTransport implements Transport, Startable {
|
|
|
31
31
|
get [Symbol.toStringTag](): string;
|
|
32
32
|
get [symbol](): true;
|
|
33
33
|
filter(multiaddrs: Multiaddr[]): Multiaddr[];
|
|
34
|
+
private splitAddr;
|
|
34
35
|
dial(ma: Multiaddr, options: DialOptions): Promise<Connection>;
|
|
35
36
|
_onProtocol({ connection, stream }: IncomingStreamData): Promise<void>;
|
|
36
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../src/peer_transport/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAC7G,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAA;AAC7E,OAAO,EAAa,SAAS,EAAa,MAAM,yBAAyB,CAAA;AACzE,OAAO,KAAK,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAChF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../src/peer_transport/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,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AAG,QAAQ;IAIzD,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAK,MAAM,CAElC;IAED,IAAI,CAAC,MAAM,CAAC,IAAK,IAAI,CAEpB;IAED,MAAM,CAAE,UAAU,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;IAO7C,OAAO,CAAC,SAAS;IAgCX,IAAI,CAAE,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IA2C/D,WAAW,CAAE,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;CAuB9E"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { symbol } from '@libp2p/interface-transport';
|
|
2
2
|
import { multiaddr, protocols } from '@multiformats/multiaddr';
|
|
3
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
3
4
|
import { WebRTCMultiaddrConnection } from '../maconn.js';
|
|
4
5
|
import { WebRTCPeerListener } from './listener.js';
|
|
5
6
|
import { logger } from '@libp2p/logger';
|
|
@@ -44,15 +45,7 @@ export class WebRTCTransport {
|
|
|
44
45
|
return codes.includes(CODE);
|
|
45
46
|
});
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
-
* dial connects to a remote via the circuit relay or any other protocol
|
|
49
|
-
* and proceeds to upgrade to a webrtc connection.
|
|
50
|
-
* multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
|
|
51
|
-
* For a circuit relay, this will be of the form
|
|
52
|
-
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
|
|
53
|
-
*/
|
|
54
|
-
async dial(ma, options) {
|
|
55
|
-
log.trace('dialing address: ', ma);
|
|
48
|
+
splitAddr(ma) {
|
|
56
49
|
const addrs = ma.toString().split(`${TRANSPORT}/`);
|
|
57
50
|
if (addrs.length !== 2) {
|
|
58
51
|
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR);
|
|
@@ -64,10 +57,6 @@ export class WebRTCTransport {
|
|
|
64
57
|
if (destinationIdString == null) {
|
|
65
58
|
throw new CodeError('bad destination', codes.ERR_INVALID_MULTIADDR);
|
|
66
59
|
}
|
|
67
|
-
if (options.signal == null) {
|
|
68
|
-
const controller = new AbortController();
|
|
69
|
-
options.signal = controller.signal;
|
|
70
|
-
}
|
|
71
60
|
const lastProtoInRemote = remoteAddr.protos().pop();
|
|
72
61
|
if (lastProtoInRemote === undefined) {
|
|
73
62
|
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR);
|
|
@@ -75,7 +64,23 @@ export class WebRTCTransport {
|
|
|
75
64
|
if (lastProtoInRemote.name !== 'p2p') {
|
|
76
65
|
remoteAddr = remoteAddr.encapsulate(`/p2p/${destinationIdString}`);
|
|
77
66
|
}
|
|
78
|
-
|
|
67
|
+
return { baseAddr: remoteAddr, peerId: peerIdFromString(destinationIdString) };
|
|
68
|
+
}
|
|
69
|
+
/*
|
|
70
|
+
* dial connects to a remote via the circuit relay or any other protocol
|
|
71
|
+
* and proceeds to upgrade to a webrtc connection.
|
|
72
|
+
* multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
|
|
73
|
+
* For a circuit relay, this will be of the form
|
|
74
|
+
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
|
|
75
|
+
*/
|
|
76
|
+
async dial(ma, options) {
|
|
77
|
+
log.trace('dialing address: ', ma);
|
|
78
|
+
const { baseAddr, peerId } = this.splitAddr(ma);
|
|
79
|
+
if (options.signal == null) {
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
options.signal = controller.signal;
|
|
82
|
+
}
|
|
83
|
+
const connection = await this.components.transportManager.dial(baseAddr);
|
|
79
84
|
const rawStream = await connection.newStream([SIGNALING_PROTO_ID], options);
|
|
80
85
|
try {
|
|
81
86
|
const [pc, muxerFactory] = await initiateConnection({
|
|
@@ -83,10 +88,11 @@ export class WebRTCTransport {
|
|
|
83
88
|
rtcConfiguration: this.init.rtcConfiguration,
|
|
84
89
|
signal: options.signal
|
|
85
90
|
});
|
|
91
|
+
const webrtcMultiaddr = baseAddr.encapsulate(`${TRANSPORT}/p2p/${peerId.toString()}`);
|
|
86
92
|
const result = await options.upgrader.upgradeOutbound(new WebRTCMultiaddrConnection({
|
|
87
93
|
peerConnection: pc,
|
|
88
94
|
timeline: { open: (new Date()).getTime() },
|
|
89
|
-
remoteAddr:
|
|
95
|
+
remoteAddr: webrtcMultiaddr
|
|
90
96
|
}), {
|
|
91
97
|
skipProtection: true,
|
|
92
98
|
skipEncryption: true,
|
|
@@ -109,10 +115,12 @@ export class WebRTCTransport {
|
|
|
109
115
|
connection,
|
|
110
116
|
stream
|
|
111
117
|
});
|
|
118
|
+
const remotePeerId = connection.remoteAddr.getPeerId();
|
|
119
|
+
const webrtcMultiaddr = connection.remoteAddr.encapsulate(`${TRANSPORT}/p2p/${remotePeerId}`);
|
|
112
120
|
await this.components.upgrader.upgradeInbound(new WebRTCMultiaddrConnection({
|
|
113
121
|
peerConnection: pc,
|
|
114
122
|
timeline: { open: (new Date()).getTime() },
|
|
115
|
-
remoteAddr:
|
|
123
|
+
remoteAddr: webrtcMultiaddr
|
|
116
124
|
}), {
|
|
117
125
|
skipEncryption: true,
|
|
118
126
|
skipProtection: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../src/peer_transport/transport.ts"],"names":[],"mappings":"AACA,OAAO,EAAgD,MAAM,EAAa,MAAM,6BAA6B,CAAA;AAE7G,OAAO,EAAE,SAAS,EAAa,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAGzE,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AAExD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAElD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EA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AAK,CAAC,WAAW,CAAE,EAAE,UAAU,EAAE,MAAM,EAAsB;QAC3D,IAAI;YACF,MAAM,CAAC,EAAE,EAAE,YAAY,CAAC,GAAG,MAAM,oBAAoB,CAAC;gBACpD,gBAAgB,EAAE,IAAI,CAAC,IAAI,CAAC,gBAAgB;gBAC5C,UAAU;gBACV,MAAM;aACP,CAAC,CAAA;YACF,MAAM,YAAY,GAAG,UAAU,CAAC,UAAU,CAAC,SAAS,EAAE,CAAA;YACtD,MAAM,eAAe,GAAG,UAAU,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,SAAS,QAAQ,YAAY,EAAE,CAAC,CAAA;YAC7F,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,yBAAyB,CAAC;gBAC1E,cAAc,EAAE,EAAE;gBAClB,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE;gBAC1C,UAAU,EAAE,eAAe;aAC5B,CAAC,EAAE;gBACF,cAAc,EAAE,IAAI;gBACpB,cAAc,EAAE,IAAI;gBACpB,YAAY;aACb,CAAC,CAAA;SACH;QAAC,OAAO,GAAG,EAAE;YACZ,MAAM,CAAC,KAAK,EAAE,CAAA;YACd,MAAM,GAAG,CAAA;SACV;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/webrtc",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "A libp2p transport using WebRTC connections",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "Apache-2.0 OR MIT",
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
},
|
|
139
139
|
"dependencies": {
|
|
140
140
|
"@chainsafe/libp2p-noise": "^11.0.0",
|
|
141
|
-
"@libp2p/interface-connection": "^
|
|
141
|
+
"@libp2p/interface-connection": "^4.0.0",
|
|
142
142
|
"@libp2p/interface-peer-id": "^2.0.0",
|
|
143
143
|
"@libp2p/interface-peer-store": "^1.2.8",
|
|
144
144
|
"@libp2p/interface-registrar": "^2.0.8",
|
|
@@ -4,6 +4,7 @@ import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
|
|
|
4
4
|
import { multiaddr, Multiaddr, protocols } from '@multiformats/multiaddr'
|
|
5
5
|
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
6
6
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
7
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
7
8
|
import { WebRTCMultiaddrConnection } from '../maconn.js'
|
|
8
9
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
10
|
import { WebRTCPeerListener } from './listener.js'
|
|
@@ -75,15 +76,7 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
75
76
|
})
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
|
|
79
|
-
* dial connects to a remote via the circuit relay or any other protocol
|
|
80
|
-
* and proceeds to upgrade to a webrtc connection.
|
|
81
|
-
* multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
|
|
82
|
-
* For a circuit relay, this will be of the form
|
|
83
|
-
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
|
|
84
|
-
*/
|
|
85
|
-
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
|
|
86
|
-
log.trace('dialing address: ', ma)
|
|
79
|
+
private splitAddr (ma: Multiaddr): { baseAddr: Multiaddr, peerId: PeerId } {
|
|
87
80
|
const addrs = ma.toString().split(`${TRANSPORT}/`)
|
|
88
81
|
if (addrs.length !== 2) {
|
|
89
82
|
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR)
|
|
@@ -97,11 +90,6 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
97
90
|
throw new CodeError('bad destination', codes.ERR_INVALID_MULTIADDR)
|
|
98
91
|
}
|
|
99
92
|
|
|
100
|
-
if (options.signal == null) {
|
|
101
|
-
const controller = new AbortController()
|
|
102
|
-
options.signal = controller.signal
|
|
103
|
-
}
|
|
104
|
-
|
|
105
93
|
const lastProtoInRemote = remoteAddr.protos().pop()
|
|
106
94
|
if (lastProtoInRemote === undefined) {
|
|
107
95
|
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR)
|
|
@@ -110,7 +98,26 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
110
98
|
remoteAddr = remoteAddr.encapsulate(`/p2p/${destinationIdString}`)
|
|
111
99
|
}
|
|
112
100
|
|
|
113
|
-
|
|
101
|
+
return { baseAddr: remoteAddr, peerId: peerIdFromString(destinationIdString) }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/*
|
|
105
|
+
* dial connects to a remote via the circuit relay or any other protocol
|
|
106
|
+
* and proceeds to upgrade to a webrtc connection.
|
|
107
|
+
* multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
|
|
108
|
+
* For a circuit relay, this will be of the form
|
|
109
|
+
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
|
|
110
|
+
*/
|
|
111
|
+
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
|
|
112
|
+
log.trace('dialing address: ', ma)
|
|
113
|
+
const { baseAddr, peerId } = this.splitAddr(ma)
|
|
114
|
+
|
|
115
|
+
if (options.signal == null) {
|
|
116
|
+
const controller = new AbortController()
|
|
117
|
+
options.signal = controller.signal
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const connection = await this.components.transportManager.dial(baseAddr)
|
|
114
121
|
|
|
115
122
|
const rawStream = await connection.newStream([SIGNALING_PROTO_ID], options)
|
|
116
123
|
|
|
@@ -120,11 +127,12 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
120
127
|
rtcConfiguration: this.init.rtcConfiguration,
|
|
121
128
|
signal: options.signal
|
|
122
129
|
})
|
|
130
|
+
const webrtcMultiaddr = baseAddr.encapsulate(`${TRANSPORT}/p2p/${peerId.toString()}`)
|
|
123
131
|
const result = await options.upgrader.upgradeOutbound(
|
|
124
132
|
new WebRTCMultiaddrConnection({
|
|
125
133
|
peerConnection: pc,
|
|
126
134
|
timeline: { open: (new Date()).getTime() },
|
|
127
|
-
remoteAddr:
|
|
135
|
+
remoteAddr: webrtcMultiaddr
|
|
128
136
|
}),
|
|
129
137
|
{
|
|
130
138
|
skipProtection: true,
|
|
@@ -150,10 +158,12 @@ export class WebRTCTransport implements Transport, Startable {
|
|
|
150
158
|
connection,
|
|
151
159
|
stream
|
|
152
160
|
})
|
|
161
|
+
const remotePeerId = connection.remoteAddr.getPeerId()
|
|
162
|
+
const webrtcMultiaddr = connection.remoteAddr.encapsulate(`${TRANSPORT}/p2p/${remotePeerId}`)
|
|
153
163
|
await this.components.upgrader.upgradeInbound(new WebRTCMultiaddrConnection({
|
|
154
164
|
peerConnection: pc,
|
|
155
165
|
timeline: { open: (new Date()).getTime() },
|
|
156
|
-
remoteAddr:
|
|
166
|
+
remoteAddr: webrtcMultiaddr
|
|
157
167
|
}), {
|
|
158
168
|
skipEncryption: true,
|
|
159
169
|
skipProtection: true,
|