@libp2p/utils 5.4.0-a11e135c2 → 5.4.0-d9366f9aa

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.
@@ -0,0 +1,7 @@
1
+ import type { AbortOptions } from '@libp2p/interface';
2
+ import type { ClearableSignal } from 'any-signal';
3
+ export declare function createTimeoutOptions(timeout: number): AbortOptions;
4
+ export declare function createTimeoutOptions(timeout: number, ...existingSignals: AbortSignal[]): {
5
+ signal: ClearableSignal;
6
+ };
7
+ //# sourceMappingURL=abort-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abort-options.d.ts","sourceRoot":"","sources":["../../src/abort-options.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjD,wBAAgB,oBAAoB,CAAE,OAAO,EAAE,MAAM,GAAG,YAAY,CAAA;AACpE,wBAAgB,oBAAoB,CAAE,OAAO,EAAE,MAAM,EAAE,GAAG,eAAe,EAAE,WAAW,EAAE,GAAG;IAAE,MAAM,EAAE,eAAe,CAAA;CAAE,CAAA"}
@@ -0,0 +1,14 @@
1
+ import { setMaxListeners } from '@libp2p/interface';
2
+ import { anySignal } from 'any-signal';
3
+ export function createTimeoutOptions(timeout, ...existingSignals) {
4
+ let signal = AbortSignal.timeout(timeout);
5
+ setMaxListeners(Infinity, signal);
6
+ if (existingSignals.length > 0) {
7
+ signal = anySignal([signal, ...existingSignals]);
8
+ setMaxListeners(Infinity, signal);
9
+ }
10
+ return {
11
+ signal
12
+ };
13
+ }
14
+ //# sourceMappingURL=abort-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abort-options.js","sourceRoot":"","sources":["../../src/abort-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAMtC,MAAM,UAAU,oBAAoB,CAAE,OAAe,EAAE,GAAG,eAA8B;IACtF,IAAI,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACzC,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IAEjC,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,GAAG,SAAS,CAAC,CAAC,MAAM,EAAE,GAAG,eAAe,CAAC,CAAC,CAAA;QAChD,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IACnC,CAAC;IAED,OAAO;QACL,MAAM;KACP,CAAA;AACH,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { Connection, Stream, AbortOptions } from '@libp2p/interface';
2
+ /**
3
+ * Close the passed stream, falling back to aborting the stream if closing
4
+ * cleanly fails.
5
+ */
6
+ export declare function safelyCloseStream(stream?: Stream, options?: AbortOptions): Promise<void>;
7
+ export interface SafelyCloseConnectionOptions extends AbortOptions {
8
+ /**
9
+ * Only close the stream if it either has no protocol streams open or only
10
+ * ones in this list.
11
+ *
12
+ * @default ['/ipfs/id/1.0.0']
13
+ */
14
+ closableProtocols?: string[];
15
+ }
16
+ /**
17
+ * Close the passed connection if it has no streams, or only closable protocol
18
+ * streams, falling back to aborting the connection if closing it cleanly fails.
19
+ */
20
+ export declare function safelyCloseConnectionIfUnused(connection?: Connection, options?: SafelyCloseConnectionOptions): Promise<void>;
21
+ //# sourceMappingURL=close.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close.d.ts","sourceRoot":"","sources":["../../src/close.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEzE;;;GAGG;AACH,wBAAsB,iBAAiB,CAAE,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAM/F;AAuBD,MAAM,WAAW,4BAA6B,SAAQ,YAAY;IAChE;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAA;CAC7B;AAED;;;GAGG;AACH,wBAAsB,6BAA6B,CAAE,UAAU,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,4BAA4B,GAAG,OAAO,CAAC,IAAI,CAAC,CAenI"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Close the passed stream, falling back to aborting the stream if closing
3
+ * cleanly fails.
4
+ */
5
+ export async function safelyCloseStream(stream, options) {
6
+ try {
7
+ await stream?.close(options);
8
+ }
9
+ catch (err) {
10
+ stream?.abort(err);
11
+ }
12
+ }
13
+ /**
14
+ * These are speculative protocols that are run automatically on connection open
15
+ * so are usually not the reason the connection was opened.
16
+ *
17
+ * Consequently when requested it should be safe to close connections that only
18
+ * have these protocol streams open.
19
+ */
20
+ const DEFAULT_CLOSABLE_PROTOCOLS = [
21
+ // identify
22
+ '/ipfs/id/1.0.0',
23
+ // identify-push
24
+ '/ipfs/id/push/1.0.0',
25
+ // autonat
26
+ '/libp2p/autonat/1.0.0',
27
+ // dcutr
28
+ '/libp2p/dcutr'
29
+ ];
30
+ /**
31
+ * Close the passed connection if it has no streams, or only closable protocol
32
+ * streams, falling back to aborting the connection if closing it cleanly fails.
33
+ */
34
+ export async function safelyCloseConnectionIfUnused(connection, options) {
35
+ const streamProtocols = connection?.streams?.map(stream => stream.protocol) ?? [];
36
+ const closableProtocols = options?.closableProtocols ?? DEFAULT_CLOSABLE_PROTOCOLS;
37
+ // if the connection has protocols not in the closable protocols list, do not
38
+ // close the connection
39
+ if (streamProtocols.filter(proto => proto != null && !closableProtocols.includes(proto)).length > 0) {
40
+ return;
41
+ }
42
+ try {
43
+ await connection?.close(options);
44
+ }
45
+ catch (err) {
46
+ connection?.abort(err);
47
+ }
48
+ }
49
+ //# sourceMappingURL=close.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close.js","sourceRoot":"","sources":["../../src/close.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAE,MAAe,EAAE,OAAsB;IAC9E,IAAI,CAAC;QACH,MAAM,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IAC9B,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;IACpB,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,0BAA0B,GAAG;IACjC,WAAW;IACX,gBAAgB;IAEhB,gBAAgB;IAChB,qBAAqB;IAErB,UAAU;IACV,uBAAuB;IAEvB,QAAQ;IACR,eAAe;CAChB,CAAA;AAYD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAE,UAAuB,EAAE,OAAsC;IAClH,MAAM,eAAe,GAAG,UAAU,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;IACjF,MAAM,iBAAiB,GAAG,OAAO,EAAE,iBAAiB,IAAI,0BAA0B,CAAA;IAElF,6EAA6E;IAC7E,uBAAuB;IACvB,IAAI,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpG,OAAM;IACR,CAAC;IAED,IAAI,CAAC;QACH,MAAM,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/utils",
3
- "version": "5.4.0-a11e135c2",
3
+ "version": "5.4.0-d9366f9aa",
4
4
  "description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
@@ -44,6 +44,10 @@
44
44
  "types": "./src/index.d.ts",
45
45
  "import": "./dist/src/index.js"
46
46
  },
47
+ "./abort-options": {
48
+ "types": "./dist/src/abort-options.d.ts",
49
+ "import": "./dist/src/abort-options.js"
50
+ },
47
51
  "./abstract-stream": {
48
52
  "types": "./dist/src/abstract-stream.d.ts",
49
53
  "import": "./dist/src/abstract-stream.js"
@@ -60,6 +64,10 @@
60
64
  "types": "./dist/src/close-source.d.ts",
61
65
  "import": "./dist/src/close-source.js"
62
66
  },
67
+ "./close": {
68
+ "types": "./dist/src/close.d.ts",
69
+ "import": "./dist/src/close.js"
70
+ },
63
71
  "./filters": {
64
72
  "types": "./dist/src/filters/index.d.ts",
65
73
  "import": "./dist/src/filters/index.js"
@@ -132,13 +140,14 @@
132
140
  },
133
141
  "dependencies": {
134
142
  "@chainsafe/is-ip": "^2.0.2",
135
- "@libp2p/crypto": "4.1.1-a11e135c2",
136
- "@libp2p/interface": "1.3.1-a11e135c2",
137
- "@libp2p/logger": "4.0.12-a11e135c2",
143
+ "@libp2p/crypto": "4.1.1-d9366f9aa",
144
+ "@libp2p/interface": "1.3.1-d9366f9aa",
145
+ "@libp2p/logger": "4.0.12-d9366f9aa",
138
146
  "@multiformats/multiaddr": "^12.2.1",
139
147
  "@multiformats/multiaddr-matcher": "^1.2.0",
140
148
  "@sindresorhus/fnv1a": "^3.1.0",
141
149
  "@types/murmurhash3js-revisited": "^3.0.3",
150
+ "any-signal": "^4.1.1",
142
151
  "delay": "^6.0.0",
143
152
  "get-iterator": "^2.0.1",
144
153
  "is-loopback-addr": "^2.0.2",
@@ -153,7 +162,7 @@
153
162
  "uint8arrays": "^5.0.3"
154
163
  },
155
164
  "devDependencies": {
156
- "@libp2p/peer-id-factory": "4.1.1-a11e135c2",
165
+ "@libp2p/peer-id-factory": "4.1.1-d9366f9aa",
157
166
  "@types/netmask": "^2.0.5",
158
167
  "aegir": "^42.2.5",
159
168
  "delay": "^6.0.0",
@@ -0,0 +1,20 @@
1
+ import { setMaxListeners } from '@libp2p/interface'
2
+ import { anySignal } from 'any-signal'
3
+ import type { AbortOptions } from '@libp2p/interface'
4
+ import type { ClearableSignal } from 'any-signal'
5
+
6
+ export function createTimeoutOptions (timeout: number): AbortOptions
7
+ export function createTimeoutOptions (timeout: number, ...existingSignals: AbortSignal[]): { signal: ClearableSignal }
8
+ export function createTimeoutOptions (timeout: number, ...existingSignals: AbortSignal[]): AbortOptions {
9
+ let signal = AbortSignal.timeout(timeout)
10
+ setMaxListeners(Infinity, signal)
11
+
12
+ if (existingSignals.length > 0) {
13
+ signal = anySignal([signal, ...existingSignals])
14
+ setMaxListeners(Infinity, signal)
15
+ }
16
+
17
+ return {
18
+ signal
19
+ }
20
+ }
package/src/close.ts ADDED
@@ -0,0 +1,65 @@
1
+ import type { Connection, Stream, AbortOptions } from '@libp2p/interface'
2
+
3
+ /**
4
+ * Close the passed stream, falling back to aborting the stream if closing
5
+ * cleanly fails.
6
+ */
7
+ export async function safelyCloseStream (stream?: Stream, options?: AbortOptions): Promise<void> {
8
+ try {
9
+ await stream?.close(options)
10
+ } catch (err: any) {
11
+ stream?.abort(err)
12
+ }
13
+ }
14
+
15
+ /**
16
+ * These are speculative protocols that are run automatically on connection open
17
+ * so are usually not the reason the connection was opened.
18
+ *
19
+ * Consequently when requested it should be safe to close connections that only
20
+ * have these protocol streams open.
21
+ */
22
+ const DEFAULT_CLOSABLE_PROTOCOLS = [
23
+ // identify
24
+ '/ipfs/id/1.0.0',
25
+
26
+ // identify-push
27
+ '/ipfs/id/push/1.0.0',
28
+
29
+ // autonat
30
+ '/libp2p/autonat/1.0.0',
31
+
32
+ // dcutr
33
+ '/libp2p/dcutr'
34
+ ]
35
+
36
+ export interface SafelyCloseConnectionOptions extends AbortOptions {
37
+ /**
38
+ * Only close the stream if it either has no protocol streams open or only
39
+ * ones in this list.
40
+ *
41
+ * @default ['/ipfs/id/1.0.0']
42
+ */
43
+ closableProtocols?: string[]
44
+ }
45
+
46
+ /**
47
+ * Close the passed connection if it has no streams, or only closable protocol
48
+ * streams, falling back to aborting the connection if closing it cleanly fails.
49
+ */
50
+ export async function safelyCloseConnectionIfUnused (connection?: Connection, options?: SafelyCloseConnectionOptions): Promise<void> {
51
+ const streamProtocols = connection?.streams?.map(stream => stream.protocol) ?? []
52
+ const closableProtocols = options?.closableProtocols ?? DEFAULT_CLOSABLE_PROTOCOLS
53
+
54
+ // if the connection has protocols not in the closable protocols list, do not
55
+ // close the connection
56
+ if (streamProtocols.filter(proto => proto != null && !closableProtocols.includes(proto)).length > 0) {
57
+ return
58
+ }
59
+
60
+ try {
61
+ await connection?.close(options)
62
+ } catch (err: any) {
63
+ connection?.abort(err)
64
+ }
65
+ }