@libp2p/utils 4.0.6 → 4.0.7-0b4a2ee79

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  [![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
2
2
  [![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
3
3
  [![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
4
- [![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)
4
+ [![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)
5
5
 
6
6
  > Package to aggregate shared logic and dependencies for the libp2p ecosystem
7
7
 
@@ -0,0 +1,145 @@
1
+ import { Uint8ArrayList } from 'uint8arraylist';
2
+ import type { AbortOptions } from '@libp2p/interface';
3
+ import type { Direction, ReadStatus, Stream, StreamStatus, StreamTimeline, WriteStatus } from '@libp2p/interface/connection';
4
+ import type { Logger } from '@libp2p/logger';
5
+ import type { Source } from 'it-stream-types';
6
+ export interface AbstractStreamInit {
7
+ /**
8
+ * A unique identifier for this stream
9
+ */
10
+ id: string;
11
+ /**
12
+ * The stream direction
13
+ */
14
+ direction: Direction;
15
+ /**
16
+ * A Logger implementation used to log stream-specific information
17
+ */
18
+ log: Logger;
19
+ /**
20
+ * User specific stream metadata
21
+ */
22
+ metadata?: Record<string, unknown>;
23
+ /**
24
+ * Invoked when the stream ends
25
+ */
26
+ onEnd?(err?: Error | undefined): void;
27
+ /**
28
+ * Invoked when the readable end of the stream is closed
29
+ */
30
+ onCloseRead?(): void;
31
+ /**
32
+ * Invoked when the writable end of the stream is closed
33
+ */
34
+ onCloseWrite?(): void;
35
+ /**
36
+ * Invoked when the the stream has been reset by the remote
37
+ */
38
+ onReset?(): void;
39
+ /**
40
+ * Invoked when the the stream has errored
41
+ */
42
+ onAbort?(err: Error): void;
43
+ /**
44
+ * How long to wait in ms for stream data to be written to the underlying
45
+ * connection when closing the writable end of the stream. (default: 500)
46
+ */
47
+ closeTimeout?: number;
48
+ /**
49
+ * After the stream sink has closed, a limit on how long it takes to send
50
+ * a close-write message to the remote peer.
51
+ */
52
+ sendCloseWriteTimeout?: number;
53
+ }
54
+ export declare abstract class AbstractStream implements Stream {
55
+ id: string;
56
+ direction: Direction;
57
+ timeline: StreamTimeline;
58
+ protocol?: string;
59
+ metadata: Record<string, unknown>;
60
+ source: AsyncGenerator<Uint8ArrayList, void, unknown>;
61
+ status: StreamStatus;
62
+ readStatus: ReadStatus;
63
+ writeStatus: WriteStatus;
64
+ readonly log: Logger;
65
+ private readonly sinkController;
66
+ private readonly sinkEnd;
67
+ private readonly closed;
68
+ private endErr;
69
+ private readonly streamSource;
70
+ private readonly onEnd?;
71
+ private readonly onCloseRead?;
72
+ private readonly onCloseWrite?;
73
+ private readonly onReset?;
74
+ private readonly onAbort?;
75
+ private readonly sendCloseWriteTimeout;
76
+ constructor(init: AbstractStreamInit);
77
+ sink(source: Source<Uint8ArrayList | Uint8Array>): Promise<void>;
78
+ protected onSourceEnd(err?: Error): void;
79
+ protected onSinkEnd(err?: Error): void;
80
+ close(options?: AbortOptions): Promise<void>;
81
+ closeRead(options?: AbortOptions): Promise<void>;
82
+ closeWrite(options?: AbortOptions): Promise<void>;
83
+ /**
84
+ * Close immediately for reading and writing and send a reset message (local
85
+ * error)
86
+ */
87
+ abort(err: Error): void;
88
+ /**
89
+ * Receive a reset message - close immediately for reading and writing (remote
90
+ * error)
91
+ */
92
+ reset(): void;
93
+ _closeSinkAndSource(err?: Error): void;
94
+ _closeSink(err?: Error): void;
95
+ _closeSource(err?: Error): void;
96
+ /**
97
+ * The remote closed for writing so we should expect to receive no more
98
+ * messages
99
+ */
100
+ remoteCloseWrite(): void;
101
+ /**
102
+ * The remote closed for reading so we should not send any more
103
+ * messages
104
+ */
105
+ remoteCloseRead(): void;
106
+ /**
107
+ * The underlying muxer has closed, no more messages can be sent or will
108
+ * be received, close immediately to free up resources
109
+ */
110
+ destroy(): void;
111
+ /**
112
+ * When an extending class reads data from it's implementation-specific source,
113
+ * call this method to allow the stream consumer to read the data.
114
+ */
115
+ sourcePush(data: Uint8ArrayList): void;
116
+ /**
117
+ * Returns the amount of unread data - can be used to prevent large amounts of
118
+ * data building up when the stream consumer is too slow.
119
+ */
120
+ sourceReadableLength(): number;
121
+ /**
122
+ * Send a message to the remote muxer informing them a new stream is being
123
+ * opened
124
+ */
125
+ abstract sendNewStream(options?: AbortOptions): void | Promise<void>;
126
+ /**
127
+ * Send a data message to the remote muxer
128
+ */
129
+ abstract sendData(buf: Uint8ArrayList, options?: AbortOptions): void | Promise<void>;
130
+ /**
131
+ * Send a reset message to the remote muxer
132
+ */
133
+ abstract sendReset(options?: AbortOptions): void | Promise<void>;
134
+ /**
135
+ * Send a message to the remote muxer, informing them no more data messages
136
+ * will be sent by this end of the stream
137
+ */
138
+ abstract sendCloseWrite(options?: AbortOptions): void | Promise<void>;
139
+ /**
140
+ * Send a message to the remote muxer, informing them no more data messages
141
+ * will be read by this end of the stream
142
+ */
143
+ abstract sendCloseRead(options?: AbortOptions): void | Promise<void>;
144
+ }
145
+ //# sourceMappingURL=abstract-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstract-stream.d.ts","sourceRoot":"","sources":["../../src/abstract-stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU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AG,IAAI;IAU9B,YAAY,CAAE,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI;IAShC;;;OAGG;IACH,gBAAgB,IAAK,IAAI;IAUzB;;;OAGG;IACH,eAAe,IAAK,IAAI;IAUxB;;;OAGG;IACH,OAAO,IAAK,IAAI;IAWhB;;;OAGG;IACH,UAAU,CAAE,IAAI,EAAE,cAAc,GAAG,IAAI;IAIvC;;;OAGG;IACH,oBAAoB,IAAK,MAAM;IAI/B;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAErE;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAE,GAAG,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAErF;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEjE;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEtE;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CACtE"}
@@ -0,0 +1,340 @@
1
+ import { CodeError } from '@libp2p/interface/errors';
2
+ import { pushable } from 'it-pushable';
3
+ import defer, {} from 'p-defer';
4
+ import { raceSignal } from 'race-signal';
5
+ import { Uint8ArrayList } from 'uint8arraylist';
6
+ import { closeSource } from './close-source.js';
7
+ const ERR_STREAM_RESET = 'ERR_STREAM_RESET';
8
+ const ERR_SINK_INVALID_STATE = 'ERR_SINK_INVALID_STATE';
9
+ const DEFAULT_SEND_CLOSE_WRITE_TIMEOUT = 5000;
10
+ function isPromise(thing) {
11
+ if (thing == null) {
12
+ return false;
13
+ }
14
+ return typeof thing.then === 'function' &&
15
+ typeof thing.catch === 'function' &&
16
+ typeof thing.finally === 'function';
17
+ }
18
+ export class AbstractStream {
19
+ id;
20
+ direction;
21
+ timeline;
22
+ protocol;
23
+ metadata;
24
+ source;
25
+ status;
26
+ readStatus;
27
+ writeStatus;
28
+ log;
29
+ sinkController;
30
+ sinkEnd;
31
+ closed;
32
+ endErr;
33
+ streamSource;
34
+ onEnd;
35
+ onCloseRead;
36
+ onCloseWrite;
37
+ onReset;
38
+ onAbort;
39
+ sendCloseWriteTimeout;
40
+ constructor(init) {
41
+ this.sinkController = new AbortController();
42
+ this.sinkEnd = defer();
43
+ this.closed = defer();
44
+ this.log = init.log;
45
+ // stream status
46
+ this.status = 'open';
47
+ this.readStatus = 'ready';
48
+ this.writeStatus = 'ready';
49
+ this.id = init.id;
50
+ this.metadata = init.metadata ?? {};
51
+ this.direction = init.direction;
52
+ this.timeline = {
53
+ open: Date.now()
54
+ };
55
+ this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT;
56
+ this.onEnd = init.onEnd;
57
+ this.onCloseRead = init?.onCloseRead;
58
+ this.onCloseWrite = init?.onCloseWrite;
59
+ this.onReset = init?.onReset;
60
+ this.onAbort = init?.onAbort;
61
+ this.source = this.streamSource = pushable({
62
+ onEnd: (err) => {
63
+ if (err != null) {
64
+ this.log.trace('source ended with error', err);
65
+ }
66
+ else {
67
+ this.log.trace('source ended');
68
+ }
69
+ this.onSourceEnd(err);
70
+ }
71
+ });
72
+ // necessary because the libp2p upgrader wraps the sink function
73
+ this.sink = this.sink.bind(this);
74
+ }
75
+ async sink(source) {
76
+ if (this.writeStatus !== 'ready') {
77
+ throw new CodeError(`writable end state is "${this.writeStatus}" not "ready"`, ERR_SINK_INVALID_STATE);
78
+ }
79
+ try {
80
+ this.writeStatus = 'writing';
81
+ const options = {
82
+ signal: this.sinkController.signal
83
+ };
84
+ if (this.direction === 'outbound') { // If initiator, open a new stream
85
+ const res = this.sendNewStream(options);
86
+ if (isPromise(res)) {
87
+ await res;
88
+ }
89
+ }
90
+ const abortListener = () => {
91
+ closeSource(source, this.log);
92
+ };
93
+ try {
94
+ this.sinkController.signal.addEventListener('abort', abortListener);
95
+ this.log.trace('sink reading from source');
96
+ for await (let data of source) {
97
+ data = data instanceof Uint8Array ? new Uint8ArrayList(data) : data;
98
+ const res = this.sendData(data, options);
99
+ if (isPromise(res)) { // eslint-disable-line max-depth
100
+ await res;
101
+ }
102
+ }
103
+ }
104
+ finally {
105
+ this.sinkController.signal.removeEventListener('abort', abortListener);
106
+ }
107
+ this.log.trace('sink finished reading from source, write status is "%s"', this.writeStatus);
108
+ if (this.writeStatus === 'writing') {
109
+ this.writeStatus = 'closing';
110
+ this.log.trace('send close write to remote');
111
+ await this.sendCloseWrite({
112
+ signal: AbortSignal.timeout(this.sendCloseWriteTimeout)
113
+ });
114
+ this.writeStatus = 'closed';
115
+ }
116
+ this.onSinkEnd();
117
+ }
118
+ catch (err) {
119
+ this.log.trace('sink ended with error, calling abort with error', err);
120
+ this.abort(err);
121
+ throw err;
122
+ }
123
+ finally {
124
+ this.log.trace('resolve sink end');
125
+ this.sinkEnd.resolve();
126
+ }
127
+ }
128
+ onSourceEnd(err) {
129
+ if (this.timeline.closeRead != null) {
130
+ return;
131
+ }
132
+ this.timeline.closeRead = Date.now();
133
+ this.readStatus = 'closed';
134
+ if (err != null && this.endErr == null) {
135
+ this.endErr = err;
136
+ }
137
+ this.onCloseRead?.();
138
+ if (this.timeline.closeWrite != null) {
139
+ this.log.trace('source and sink ended');
140
+ this.timeline.close = Date.now();
141
+ if (this.status !== 'aborted' && this.status !== 'reset') {
142
+ this.status = 'closed';
143
+ }
144
+ if (this.onEnd != null) {
145
+ this.onEnd(this.endErr);
146
+ }
147
+ this.closed.resolve();
148
+ }
149
+ else {
150
+ this.log.trace('source ended, waiting for sink to end');
151
+ }
152
+ }
153
+ onSinkEnd(err) {
154
+ if (this.timeline.closeWrite != null) {
155
+ return;
156
+ }
157
+ this.timeline.closeWrite = Date.now();
158
+ this.writeStatus = 'closed';
159
+ if (err != null && this.endErr == null) {
160
+ this.endErr = err;
161
+ }
162
+ this.onCloseWrite?.();
163
+ if (this.timeline.closeRead != null) {
164
+ this.log.trace('sink and source ended');
165
+ this.timeline.close = Date.now();
166
+ if (this.status !== 'aborted' && this.status !== 'reset') {
167
+ this.status = 'closed';
168
+ }
169
+ if (this.onEnd != null) {
170
+ this.onEnd(this.endErr);
171
+ }
172
+ this.closed.resolve();
173
+ }
174
+ else {
175
+ this.log.trace('sink ended, waiting for source to end');
176
+ }
177
+ }
178
+ // Close for both Reading and Writing
179
+ async close(options) {
180
+ this.log.trace('closing gracefully');
181
+ this.status = 'closing';
182
+ await Promise.all([
183
+ this.closeRead(options),
184
+ this.closeWrite(options)
185
+ ]);
186
+ // wait for read and write ends to close
187
+ await raceSignal(this.closed.promise, options?.signal);
188
+ this.status = 'closed';
189
+ this.log.trace('closed gracefully');
190
+ }
191
+ async closeRead(options = {}) {
192
+ if (this.readStatus === 'closing' || this.readStatus === 'closed') {
193
+ return;
194
+ }
195
+ this.log.trace('closing readable end of stream with starting read status "%s"', this.readStatus);
196
+ const readStatus = this.readStatus;
197
+ this.readStatus = 'closing';
198
+ if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
199
+ this.log.trace('send close read to remote');
200
+ await this.sendCloseRead(options);
201
+ }
202
+ if (readStatus === 'ready') {
203
+ this.log.trace('ending internal source queue with %d queued bytes', this.streamSource.readableLength);
204
+ this.streamSource.end();
205
+ }
206
+ this.log.trace('closed readable end of stream');
207
+ }
208
+ async closeWrite(options = {}) {
209
+ if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
210
+ return;
211
+ }
212
+ this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus);
213
+ if (this.writeStatus === 'ready') {
214
+ this.log.trace('sink was never sunk, sink an empty array');
215
+ await raceSignal(this.sink([]), options.signal);
216
+ }
217
+ if (this.writeStatus === 'writing') {
218
+ // stop reading from the source passed to `.sink` in the microtask queue
219
+ // - this lets any data queued by the user in the current tick get read
220
+ // before we exit
221
+ await new Promise((resolve, reject) => {
222
+ queueMicrotask(() => {
223
+ this.log.trace('aborting source passed to .sink');
224
+ this.sinkController.abort();
225
+ raceSignal(this.sinkEnd.promise, options.signal)
226
+ .then(resolve, reject);
227
+ });
228
+ });
229
+ }
230
+ this.writeStatus = 'closed';
231
+ this.log.trace('closed writable end of stream');
232
+ }
233
+ /**
234
+ * Close immediately for reading and writing and send a reset message (local
235
+ * error)
236
+ */
237
+ abort(err) {
238
+ if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
239
+ return;
240
+ }
241
+ this.log('abort with error', err);
242
+ // try to send a reset message
243
+ this.log('try to send reset to remote');
244
+ const res = this.sendReset();
245
+ if (isPromise(res)) {
246
+ res.catch((err) => {
247
+ this.log.error('error sending reset message', err);
248
+ });
249
+ }
250
+ this.status = 'aborted';
251
+ this.timeline.abort = Date.now();
252
+ this._closeSinkAndSource(err);
253
+ this.onAbort?.(err);
254
+ }
255
+ /**
256
+ * Receive a reset message - close immediately for reading and writing (remote
257
+ * error)
258
+ */
259
+ reset() {
260
+ if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
261
+ return;
262
+ }
263
+ const err = new CodeError('stream reset', ERR_STREAM_RESET);
264
+ this.status = 'reset';
265
+ this.timeline.reset = Date.now();
266
+ this._closeSinkAndSource(err);
267
+ this.onReset?.();
268
+ }
269
+ _closeSinkAndSource(err) {
270
+ this._closeSink(err);
271
+ this._closeSource(err);
272
+ }
273
+ _closeSink(err) {
274
+ // if the sink function is running, cause it to end
275
+ if (this.writeStatus === 'writing') {
276
+ this.log.trace('end sink source');
277
+ this.sinkController.abort();
278
+ }
279
+ this.onSinkEnd(err);
280
+ }
281
+ _closeSource(err) {
282
+ // if the source is not ending, end it
283
+ if (this.readStatus !== 'closing' && this.readStatus !== 'closed') {
284
+ this.log.trace('ending source with %d bytes to be read by consumer', this.streamSource.readableLength);
285
+ this.readStatus = 'closing';
286
+ this.streamSource.end(err);
287
+ }
288
+ }
289
+ /**
290
+ * The remote closed for writing so we should expect to receive no more
291
+ * messages
292
+ */
293
+ remoteCloseWrite() {
294
+ if (this.readStatus === 'closing' || this.readStatus === 'closed') {
295
+ this.log('received remote close write but local source is already closed');
296
+ return;
297
+ }
298
+ this.log.trace('remote close write');
299
+ this._closeSource();
300
+ }
301
+ /**
302
+ * The remote closed for reading so we should not send any more
303
+ * messages
304
+ */
305
+ remoteCloseRead() {
306
+ if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
307
+ this.log('received remote close read but local sink is already closed');
308
+ return;
309
+ }
310
+ this.log.trace('remote close read');
311
+ this._closeSink();
312
+ }
313
+ /**
314
+ * The underlying muxer has closed, no more messages can be sent or will
315
+ * be received, close immediately to free up resources
316
+ */
317
+ destroy() {
318
+ if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
319
+ this.log('received destroy but we are already closed');
320
+ return;
321
+ }
322
+ this.log.trace('stream destroyed');
323
+ this._closeSinkAndSource();
324
+ }
325
+ /**
326
+ * When an extending class reads data from it's implementation-specific source,
327
+ * call this method to allow the stream consumer to read the data.
328
+ */
329
+ sourcePush(data) {
330
+ this.streamSource.push(data);
331
+ }
332
+ /**
333
+ * Returns the amount of unread data - can be used to prevent large amounts of
334
+ * data building up when the stream consumer is too slow.
335
+ */
336
+ sourceReadableLength() {
337
+ return this.streamSource.readableLength;
338
+ }
339
+ }
340
+ //# sourceMappingURL=abstract-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstract-stream.js","sourceRoot":"","sources":["../../src/abstract-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAiB,QAAQ,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,KAAK,EAAE,EAAwB,MAAM,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C;IAED;;;OAGG;IACH,eAAe;QACb,IAAI,IAAI,CAAC,WAAW,KAAK,SAAS,IAAI,IAAI,CAAC,WAAW,KAAK,QAAQ,EAAE;YACnE,IAAI,CAAC,GAAG,CAAC,6DAA6D,CAAC,CAAA;YACvE,OAAM;SACP;QAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAA;QACnC,IAAI,CAAC,UAAU,EAAE,CAAA;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,IAAI,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;YACpF,IAAI,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAA;YACtD,OAAM;SACP;QAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAA;QAElC,IAAI,CAAC,mBAAmB,EAAE,CAAA;IAC5B,CAAC;IAED;;;OAGG;IACH,UAAU,CAAE,IAAoB;QAC9B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;IAED;;;OAGG;IACH,oBAAoB;QAClB,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,CAAA;IACzC,CAAC;CA6BF"}
@@ -0,0 +1,4 @@
1
+ import type { Logger } from '@libp2p/logger';
2
+ import type { Source } from 'it-stream-types';
3
+ export declare function closeSource(source: Source<unknown>, log: Logger): void;
4
+ //# sourceMappingURL=close-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close-source.d.ts","sourceRoot":"","sources":["../../src/close-source.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C,wBAAgB,WAAW,CAAE,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAQvE"}
@@ -0,0 +1,11 @@
1
+ import { getIterator } from 'get-iterator';
2
+ import { isPromise } from './is-promise.js';
3
+ export function closeSource(source, log) {
4
+ const res = getIterator(source).return?.();
5
+ if (isPromise(res)) {
6
+ res.catch(err => {
7
+ log.error('could not cause iterator to return', err);
8
+ });
9
+ }
10
+ }
11
+ //# sourceMappingURL=close-source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close-source.js","sourceRoot":"","sources":["../../src/close-source.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,MAAM,UAAU,WAAW,CAAE,MAAuB,EAAE,GAAW;IAC/D,MAAM,GAAG,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAA;IAE1C,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE;QAClB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YACd,GAAG,CAAC,KAAK,CAAC,oCAAoC,EAAE,GAAG,CAAC,CAAA;QACtD,CAAC,CAAC,CAAA;KACH;AACH,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"ip-port-to-multiaddr.d.ts","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,yBAAyB,CAAA;AAInE,eAAO,MAAM,MAAM;;;;CAIlB,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAwB/E"}
1
+ {"version":3,"file":"ip-port-to-multiaddr.d.ts","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,yBAAyB,CAAA;AAEnE,eAAO,MAAM,MAAM;;;;CAIlB,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAsB/E"}
@@ -1,8 +1,6 @@
1
1
  import { isIPv4, isIPv6 } from '@chainsafe/is-ip';
2
2
  import { CodeError } from '@libp2p/interface/errors';
3
- import { logger } from '@libp2p/logger';
4
3
  import { multiaddr } from '@multiformats/multiaddr';
5
- const log = logger('libp2p:ip-port-to-multiaddr');
6
4
  export const Errors = {
7
5
  ERR_INVALID_IP_PARAMETER: 'ERR_INVALID_IP_PARAMETER',
8
6
  ERR_INVALID_PORT_PARAMETER: 'ERR_INVALID_PORT_PARAMETER',
@@ -27,8 +25,6 @@ export function ipPortToMultiaddr(ip, port) {
27
25
  if (isIPv6(ip)) {
28
26
  return multiaddr(`/ip6/${ip}/tcp/${port}`);
29
27
  }
30
- const errMsg = `invalid ip:port for creating a multiaddr: ${ip}:${port}`;
31
- log.error(errMsg);
32
- throw new CodeError(errMsg, Errors.ERR_INVALID_IP);
28
+ throw new CodeError(`invalid ip:port for creating a multiaddr: ${ip}:${port}`, Errors.ERR_INVALID_IP);
33
29
  }
34
30
  //# sourceMappingURL=ip-port-to-multiaddr.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ip-port-to-multiaddr.js","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAEnE,MAAM,GAAG,GAAG,MAAM,CAAC,6BAA6B,CAAC,CAAA;AAEjD,MAAM,CAAC,MAAM,MAAM,GAAG;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nE,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,wBAAwB,EAAE,0BAA0B;IACpD,0BAA0B,EAAE,4BAA4B;IACxD,cAAc,EAAE,gBAAgB;CACjC,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAE,EAAU,EAAE,IAAqB;IAClE,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE;QAC1B,MAAM,IAAI,SAAS,CAAC,wBAAwB,EAAE,EAAE,EAAE,MAAM,CAAC,wBAAwB,CAAC,CAAA,CAAC,uEAAuE;KAC3J;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;KACtB;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE;QACf,MAAM,IAAI,SAAS,CAAC,0BAA0B,IAAI,EAAE,EAAE,MAAM,CAAC,0BAA0B,CAAC,CAAA;KACzF;IAED,IAAI,MAAM,CAAC,EAAE,CAAC,EAAE;QACd,OAAO,SAAS,CAAC,QAAQ,EAAE,QAAQ,IAAI,EAAE,CAAC,CAAA;KAC3C;IAED,IAAI,MAAM,CAAC,EAAE,CAAC,EAAE;QACd,OAAO,SAAS,CAAC,QAAQ,EAAE,QAAQ,IAAI,EAAE,CAAC,CAAA;KAC3C;IAED,MAAM,IAAI,SAAS,CAAC,6CAA6C,EAAE,IAAI,IAAI,EAAE,EAAE,MAAM,CAAC,cAAc,CAAC,CAAA;AACvG,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function isPromise<T = unknown>(thing: any): thing is Promise<T>;
2
+ //# sourceMappingURL=is-promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-promise.d.ts","sourceRoot":"","sources":["../../src/is-promise.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAE,CAAC,GAAG,OAAO,EAAG,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,CAQxE"}
@@ -0,0 +1,9 @@
1
+ export function isPromise(thing) {
2
+ if (thing == null) {
3
+ return false;
4
+ }
5
+ return typeof thing.then === 'function' &&
6
+ typeof thing.catch === 'function' &&
7
+ typeof thing.finally === 'function';
8
+ }
9
+ //# sourceMappingURL=is-promise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-promise.js","sourceRoot":"","sources":["../../src/is-promise.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,SAAS,CAAgB,KAAU;IACjD,IAAI,KAAK,IAAI,IAAI,EAAE;QACjB,OAAO,KAAK,CAAA;KACb;IAED,OAAO,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU;QACrC,OAAO,KAAK,CAAC,KAAK,KAAK,UAAU;QACjC,OAAO,KAAK,CAAC,OAAO,KAAK,UAAU,CAAA;AACvC,CAAC"}
@@ -0,0 +1,33 @@
1
+ import PQueue from 'p-queue';
2
+ import type { PeerId } from '@libp2p/interface/peer-id';
3
+ import type { QueueAddOptions, Options, Queue } from 'p-queue';
4
+ interface RunFunction {
5
+ (): Promise<unknown>;
6
+ }
7
+ export interface PeerPriorityQueueOptions extends QueueAddOptions {
8
+ peerId: PeerId;
9
+ }
10
+ /**
11
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
12
+ * that adds support for filtering jobs by peer id
13
+ */
14
+ declare class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
15
+ #private;
16
+ enqueue(run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void;
17
+ dequeue(): RunFunction | undefined;
18
+ filter(options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[];
19
+ get size(): number;
20
+ }
21
+ /**
22
+ * Extends PQueue to add support for querying queued jobs by peer id
23
+ */
24
+ export declare class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
25
+ constructor(options?: Options<PeerPriorityQueue, PeerPriorityQueueOptions>);
26
+ /**
27
+ * Returns true if this queue has a job for the passed peer id that has not yet
28
+ * started to run
29
+ */
30
+ hasJob(peerId: PeerId): boolean;
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=peer-job-queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-job-queue.d.ts","sourceRoot":"","sources":["../../src/peer-job-queue.ts"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAuB9D,UAAU,WAAW;IAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;CAAE;AAE9C,MAAM,WAAW,wBAAyB,SAAQ,eAAe;IAC/D,MAAM,EAAE,MAAM,CAAA;CACf;AAQD;;;GAGG;AACH,cAAM,iBAAkB,YAAW,KAAK,CAAC,WAAW,EAAE,wBAAwB,CAAC;;IAG7E,OAAO,CAAE,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAAG,IAAI;IA0B7E,OAAO,IAAK,WAAW,GAAG,SAAS;IAKnC,MAAM,CAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,GAAG,WAAW,EAAE;IAc5E,IAAI,IAAI,IAAK,MAAM,CAElB;CACF;AAED;;GAEG;AACH,qBAAa,YAAa,SAAQ,MAAM,CAAC,iBAAiB,EAAE,wBAAwB,CAAC;gBACtE,OAAO,GAAE,OAAO,CAAC,iBAAiB,EAAE,wBAAwB,CAAM;IAO/E;;;OAGG;IACH,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO;CAKjC"}