@libp2p/websockets 9.2.18 → 9.2.19
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket-to-conn.d.ts","sourceRoot":"","sources":["../../src/socket-to-conn.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACzG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,CAAC,EAAE,YAAY,CAAA;IACtB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAID,wBAAgB,cAAc,CAAE,MAAM,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,mBAAmB,GAAG,mBAAmB,
|
|
1
|
+
{"version":3,"file":"socket-to-conn.d.ts","sourceRoot":"","sources":["../../src/socket-to-conn.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACzG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,CAAC,EAAE,YAAY,CAAA;IACtB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAID,wBAAgB,cAAc,CAAE,MAAM,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,mBAAmB,GAAG,mBAAmB,CAkGjI"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { AbortError } from '@libp2p/interface';
|
|
1
|
+
import { AbortError, ConnectionFailedError } from '@libp2p/interface';
|
|
2
2
|
import { CLOSE_TIMEOUT } from './constants.js';
|
|
3
3
|
// Convert a stream into a MultiaddrConnection
|
|
4
4
|
// https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
5
5
|
export function socketToMaConn(stream, remoteAddr, options) {
|
|
6
|
-
const log = options.logger.forComponent('libp2p:websockets:maconn');
|
|
7
6
|
const metrics = options.metrics;
|
|
8
7
|
const metricPrefix = options.metricPrefix ?? '';
|
|
9
8
|
const maConn = {
|
|
10
|
-
log,
|
|
9
|
+
log: options.logger.forComponent('libp2p:websockets:connection'),
|
|
11
10
|
async sink(source) {
|
|
12
11
|
try {
|
|
13
12
|
await stream.sink((async function* () {
|
|
@@ -23,7 +22,7 @@ export function socketToMaConn(stream, remoteAddr, options) {
|
|
|
23
22
|
}
|
|
24
23
|
catch (err) {
|
|
25
24
|
if (err.type !== 'aborted') {
|
|
26
|
-
log.error(err);
|
|
25
|
+
maConn.log.error(err);
|
|
27
26
|
}
|
|
28
27
|
}
|
|
29
28
|
},
|
|
@@ -41,7 +40,7 @@ export function socketToMaConn(stream, remoteAddr, options) {
|
|
|
41
40
|
}
|
|
42
41
|
const listener = () => {
|
|
43
42
|
const { host, port } = maConn.remoteAddr.toOptions();
|
|
44
|
-
log('timeout closing stream to %s:%s after %dms, destroying it manually', host, port, Date.now() - start);
|
|
43
|
+
maConn.log('timeout closing stream to %s:%s after %dms, destroying it manually', host, port, Date.now() - start);
|
|
45
44
|
this.abort(new AbortError('Socket close timeout'));
|
|
46
45
|
};
|
|
47
46
|
options.signal?.addEventListener('abort', listener);
|
|
@@ -49,7 +48,7 @@ export function socketToMaConn(stream, remoteAddr, options) {
|
|
|
49
48
|
await stream.close();
|
|
50
49
|
}
|
|
51
50
|
catch (err) {
|
|
52
|
-
log.error('error closing WebSocket gracefully', err);
|
|
51
|
+
maConn.log.error('error closing WebSocket gracefully - %e', err);
|
|
53
52
|
this.abort(err);
|
|
54
53
|
}
|
|
55
54
|
finally {
|
|
@@ -58,8 +57,7 @@ export function socketToMaConn(stream, remoteAddr, options) {
|
|
|
58
57
|
}
|
|
59
58
|
},
|
|
60
59
|
abort(err) {
|
|
61
|
-
|
|
62
|
-
log('timeout closing stream to %s:%s due to error', host, port, err);
|
|
60
|
+
maConn.log.error('destroying WebSocket after error - %e', err);
|
|
63
61
|
stream.destroy();
|
|
64
62
|
maConn.timeline.close = Date.now();
|
|
65
63
|
// ws WebSocket.terminate does not accept an Error arg to emit an 'error'
|
|
@@ -69,14 +67,21 @@ export function socketToMaConn(stream, remoteAddr, options) {
|
|
|
69
67
|
metrics?.increment({ [`${metricPrefix}error`]: true });
|
|
70
68
|
}
|
|
71
69
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
// track local vs remote closing
|
|
71
|
+
let closedLocally = false;
|
|
72
|
+
const close = stream.socket.close.bind(stream.socket);
|
|
73
|
+
stream.socket.close = (...args) => {
|
|
74
|
+
closedLocally = true;
|
|
75
|
+
return close(...args);
|
|
76
|
+
};
|
|
77
|
+
stream.socket.addEventListener('close', (evt) => {
|
|
78
|
+
maConn.log('closed %s, code %d, reason "%s", wasClean %s', closedLocally ? 'locally' : 'by remote', evt.code, evt.reason, evt.wasClean);
|
|
79
|
+
if (!evt.wasClean) {
|
|
80
|
+
maConn.abort(new ConnectionFailedError(`${closedLocally ? 'Local' : 'Remote'} did not close WebSocket cleanly`));
|
|
81
|
+
return;
|
|
79
82
|
}
|
|
83
|
+
metrics?.increment({ [`${metricPrefix}close`]: true });
|
|
84
|
+
maConn.timeline.close = Date.now();
|
|
80
85
|
}, { once: true });
|
|
81
86
|
return maConn;
|
|
82
87
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket-to-conn.js","sourceRoot":"","sources":["../../src/socket-to-conn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"socket-to-conn.js","sourceRoot":"","sources":["../../src/socket-to-conn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAY9C,8CAA8C;AAC9C,oEAAoE;AACpE,MAAM,UAAU,cAAc,CAAE,MAAuB,EAAE,UAAqB,EAAE,OAA4B;IAC1G,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAA;IAC/B,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,EAAE,CAAA;IAE/C,MAAM,MAAM,GAAwB;QAClC,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,8BAA8B,CAAC;QAEhE,KAAK,CAAC,IAAI,CAAE,MAAM;YAChB,IAAI,CAAC;gBACH,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,SAAU,CAAC;oBACjC,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;wBAC/B,IAAI,GAAG,YAAY,UAAU,EAAE,CAAC;4BAC9B,MAAM,GAAG,CAAA;wBACX,CAAC;6BAAM,CAAC;4BACN,MAAM,GAAG,CAAC,QAAQ,EAAE,CAAA;wBACtB,CAAC;oBACH,CAAC;gBACH,CAAC,CAAC,EAAE,CAAC,CAAA;YACP,CAAC;YAAC,OAAO,GAAQ,EAAE,CAAC;gBAClB,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;oBAC3B,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;gBACvB,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,EAAE,MAAM,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,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAA;QACtD,MAAM,CAAC,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;IACpC,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IAElB,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/websockets",
|
|
3
|
-
"version": "9.2.
|
|
3
|
+
"version": "9.2.19",
|
|
4
4
|
"description": "JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-websockets#readme",
|
|
@@ -67,8 +67,8 @@
|
|
|
67
67
|
"test:electron-main": "aegir test -t electron-main -f ./dist/test/node.js --cov"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@libp2p/interface": "^2.
|
|
71
|
-
"@libp2p/utils": "^6.7.
|
|
70
|
+
"@libp2p/interface": "^2.11.0",
|
|
71
|
+
"@libp2p/utils": "^6.7.2",
|
|
72
72
|
"@multiformats/multiaddr": "^12.4.4",
|
|
73
73
|
"@multiformats/multiaddr-matcher": "^2.0.0",
|
|
74
74
|
"@multiformats/multiaddr-to-uri": "^11.0.0",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"ws": "^8.18.2"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@libp2p/logger": "^5.
|
|
85
|
+
"@libp2p/logger": "^5.2.0",
|
|
86
86
|
"aegir": "^47.0.14",
|
|
87
87
|
"is-loopback-addr": "^2.0.2",
|
|
88
88
|
"p-wait-for": "^5.0.2",
|
package/src/socket-to-conn.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbortError } from '@libp2p/interface'
|
|
1
|
+
import { AbortError, ConnectionFailedError } from '@libp2p/interface'
|
|
2
2
|
import { CLOSE_TIMEOUT } from './constants.js'
|
|
3
3
|
import type { AbortOptions, ComponentLogger, CounterGroup, MultiaddrConnection } from '@libp2p/interface'
|
|
4
4
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
@@ -14,12 +14,11 @@ export interface SocketToConnOptions {
|
|
|
14
14
|
// Convert a stream into a MultiaddrConnection
|
|
15
15
|
// https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
16
16
|
export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr, options: SocketToConnOptions): MultiaddrConnection {
|
|
17
|
-
const log = options.logger.forComponent('libp2p:websockets:maconn')
|
|
18
17
|
const metrics = options.metrics
|
|
19
18
|
const metricPrefix = options.metricPrefix ?? ''
|
|
20
19
|
|
|
21
20
|
const maConn: MultiaddrConnection = {
|
|
22
|
-
log,
|
|
21
|
+
log: options.logger.forComponent('libp2p:websockets:connection'),
|
|
23
22
|
|
|
24
23
|
async sink (source) {
|
|
25
24
|
try {
|
|
@@ -34,7 +33,7 @@ export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr,
|
|
|
34
33
|
})())
|
|
35
34
|
} catch (err: any) {
|
|
36
35
|
if (err.type !== 'aborted') {
|
|
37
|
-
log.error(err)
|
|
36
|
+
maConn.log.error(err)
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
},
|
|
@@ -59,7 +58,7 @@ export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr,
|
|
|
59
58
|
|
|
60
59
|
const listener = (): void => {
|
|
61
60
|
const { host, port } = maConn.remoteAddr.toOptions()
|
|
62
|
-
log('timeout closing stream to %s:%s after %dms, destroying it manually',
|
|
61
|
+
maConn.log('timeout closing stream to %s:%s after %dms, destroying it manually',
|
|
63
62
|
host, port, Date.now() - start)
|
|
64
63
|
|
|
65
64
|
this.abort(new AbortError('Socket close timeout'))
|
|
@@ -70,7 +69,7 @@ export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr,
|
|
|
70
69
|
try {
|
|
71
70
|
await stream.close()
|
|
72
71
|
} catch (err: any) {
|
|
73
|
-
log.error('error closing WebSocket gracefully', err)
|
|
72
|
+
maConn.log.error('error closing WebSocket gracefully - %e', err)
|
|
74
73
|
this.abort(err)
|
|
75
74
|
} finally {
|
|
76
75
|
options.signal?.removeEventListener('abort', listener)
|
|
@@ -79,10 +78,7 @@ export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr,
|
|
|
79
78
|
},
|
|
80
79
|
|
|
81
80
|
abort (err: Error): void {
|
|
82
|
-
|
|
83
|
-
log('timeout closing stream to %s:%s due to error',
|
|
84
|
-
host, port, err)
|
|
85
|
-
|
|
81
|
+
maConn.log.error('destroying WebSocket after error - %e', err)
|
|
86
82
|
stream.destroy()
|
|
87
83
|
maConn.timeline.close = Date.now()
|
|
88
84
|
|
|
@@ -94,15 +90,24 @@ export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr,
|
|
|
94
90
|
}
|
|
95
91
|
}
|
|
96
92
|
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
// track local vs remote closing
|
|
94
|
+
let closedLocally = false
|
|
95
|
+
const close = stream.socket.close.bind(stream.socket)
|
|
96
|
+
stream.socket.close = (...args) => {
|
|
97
|
+
closedLocally = true
|
|
98
|
+
return close(...args)
|
|
99
|
+
}
|
|
99
100
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
maConn.
|
|
101
|
+
stream.socket.addEventListener('close', (evt) => {
|
|
102
|
+
maConn.log('closed %s, code %d, reason "%s", wasClean %s', closedLocally ? 'locally' : 'by remote', evt.code, evt.reason, evt.wasClean)
|
|
103
|
+
|
|
104
|
+
if (!evt.wasClean) {
|
|
105
|
+
maConn.abort(new ConnectionFailedError(`${closedLocally ? 'Local' : 'Remote'} did not close WebSocket cleanly`))
|
|
106
|
+
return
|
|
105
107
|
}
|
|
108
|
+
|
|
109
|
+
metrics?.increment({ [`${metricPrefix}close`]: true })
|
|
110
|
+
maConn.timeline.close = Date.now()
|
|
106
111
|
}, { once: true })
|
|
107
112
|
|
|
108
113
|
return maConn
|