@libp2p/utils 4.0.7-0f5c305af → 4.0.7-6625a27fc

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,144 @@
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 endErr;
68
+ private readonly streamSource;
69
+ private readonly onEnd?;
70
+ private readonly onCloseRead?;
71
+ private readonly onCloseWrite?;
72
+ private readonly onReset?;
73
+ private readonly onAbort?;
74
+ private readonly sendCloseWriteTimeout;
75
+ constructor(init: AbstractStreamInit);
76
+ sink(source: Source<Uint8ArrayList | Uint8Array>): Promise<void>;
77
+ protected onSourceEnd(err?: Error): void;
78
+ protected onSinkEnd(err?: Error): void;
79
+ close(options?: AbortOptions): Promise<void>;
80
+ closeRead(options?: AbortOptions): Promise<void>;
81
+ closeWrite(options?: AbortOptions): Promise<void>;
82
+ /**
83
+ * Close immediately for reading and writing and send a reset message (local
84
+ * error)
85
+ */
86
+ abort(err: Error): void;
87
+ /**
88
+ * Receive a reset message - close immediately for reading and writing (remote
89
+ * error)
90
+ */
91
+ reset(): void;
92
+ _closeSinkAndSource(err?: Error): void;
93
+ _closeSink(err?: Error): void;
94
+ _closeSource(err?: Error): void;
95
+ /**
96
+ * The remote closed for writing so we should expect to receive no more
97
+ * messages
98
+ */
99
+ remoteCloseWrite(): void;
100
+ /**
101
+ * The remote closed for reading so we should not send any more
102
+ * messages
103
+ */
104
+ remoteCloseRead(): void;
105
+ /**
106
+ * The underlying muxer has closed, no more messages can be sent or will
107
+ * be received, close immediately to free up resources
108
+ */
109
+ destroy(): void;
110
+ /**
111
+ * When an extending class reads data from it's implementation-specific source,
112
+ * call this method to allow the stream consumer to read the data.
113
+ */
114
+ sourcePush(data: Uint8ArrayList): void;
115
+ /**
116
+ * Returns the amount of unread data - can be used to prevent large amounts of
117
+ * data building up when the stream consumer is too slow.
118
+ */
119
+ sourceReadableLength(): number;
120
+ /**
121
+ * Send a message to the remote muxer informing them a new stream is being
122
+ * opened
123
+ */
124
+ abstract sendNewStream(options?: AbortOptions): void | Promise<void>;
125
+ /**
126
+ * Send a data message to the remote muxer
127
+ */
128
+ abstract sendData(buf: Uint8ArrayList, options?: AbortOptions): void | Promise<void>;
129
+ /**
130
+ * Send a reset message to the remote muxer
131
+ */
132
+ abstract sendReset(options?: AbortOptions): void | Promise<void>;
133
+ /**
134
+ * Send a message to the remote muxer, informing them no more data messages
135
+ * will be sent by this end of the stream
136
+ */
137
+ abstract sendCloseWrite(options?: AbortOptions): void | Promise<void>;
138
+ /**
139
+ * Send a message to the remote muxer, informing them no more data messages
140
+ * will be read by this end of the stream
141
+ */
142
+ abstract sendCloseRead(options?: AbortOptions): void | Promise<void>;
143
+ }
144
+ //# 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,EAAE,MAAM,EAAE,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAA;AAC5H,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAM7C,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,SAAS,EAAE,SAAS,CAAA;IAEpB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IAEX;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAElC;;OAEG;IACH,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,GAAG,IAAI,CAAA;IAErC;;OAEG;IACH,WAAW,CAAC,IAAI,IAAI,CAAA;IAEpB;;OAEG;IAC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,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,334 @@
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
+ endErr;
32
+ streamSource;
33
+ onEnd;
34
+ onCloseRead;
35
+ onCloseWrite;
36
+ onReset;
37
+ onAbort;
38
+ sendCloseWriteTimeout;
39
+ constructor(init) {
40
+ this.sinkController = new AbortController();
41
+ this.sinkEnd = defer();
42
+ this.log = init.log;
43
+ // stream status
44
+ this.status = 'open';
45
+ this.readStatus = 'ready';
46
+ this.writeStatus = 'ready';
47
+ this.id = init.id;
48
+ this.metadata = init.metadata ?? {};
49
+ this.direction = init.direction;
50
+ this.timeline = {
51
+ open: Date.now()
52
+ };
53
+ this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT;
54
+ this.onEnd = init.onEnd;
55
+ this.onCloseRead = init?.onCloseRead;
56
+ this.onCloseWrite = init?.onCloseWrite;
57
+ this.onReset = init?.onReset;
58
+ this.onAbort = init?.onAbort;
59
+ this.source = this.streamSource = pushable({
60
+ onEnd: (err) => {
61
+ if (err != null) {
62
+ this.log.trace('source ended with error', err);
63
+ }
64
+ else {
65
+ this.log.trace('source ended');
66
+ }
67
+ this.onSourceEnd(err);
68
+ }
69
+ });
70
+ // necessary because the libp2p upgrader wraps the sink function
71
+ this.sink = this.sink.bind(this);
72
+ }
73
+ async sink(source) {
74
+ if (this.writeStatus !== 'ready') {
75
+ throw new CodeError(`writable end state is "${this.writeStatus}" not "ready"`, ERR_SINK_INVALID_STATE);
76
+ }
77
+ try {
78
+ this.writeStatus = 'writing';
79
+ const options = {
80
+ signal: this.sinkController.signal
81
+ };
82
+ if (this.direction === 'outbound') { // If initiator, open a new stream
83
+ const res = this.sendNewStream(options);
84
+ if (isPromise(res)) {
85
+ await res;
86
+ }
87
+ }
88
+ const abortListener = () => {
89
+ closeSource(source, this.log);
90
+ };
91
+ try {
92
+ this.sinkController.signal.addEventListener('abort', abortListener);
93
+ this.log.trace('sink reading from source');
94
+ for await (let data of source) {
95
+ data = data instanceof Uint8Array ? new Uint8ArrayList(data) : data;
96
+ const res = this.sendData(data, options);
97
+ if (isPromise(res)) { // eslint-disable-line max-depth
98
+ await res;
99
+ }
100
+ }
101
+ }
102
+ finally {
103
+ this.sinkController.signal.removeEventListener('abort', abortListener);
104
+ }
105
+ this.log.trace('sink finished reading from source, write status is "%s"', this.writeStatus);
106
+ if (this.writeStatus === 'writing') {
107
+ this.writeStatus = 'closing';
108
+ this.log.trace('send close write to remote');
109
+ await this.sendCloseWrite({
110
+ signal: AbortSignal.timeout(this.sendCloseWriteTimeout)
111
+ });
112
+ this.writeStatus = 'closed';
113
+ }
114
+ this.onSinkEnd();
115
+ }
116
+ catch (err) {
117
+ this.log.trace('sink ended with error, calling abort with error', err);
118
+ this.abort(err);
119
+ throw err;
120
+ }
121
+ finally {
122
+ this.log.trace('resolve sink end');
123
+ this.sinkEnd.resolve();
124
+ }
125
+ }
126
+ onSourceEnd(err) {
127
+ if (this.timeline.closeRead != null) {
128
+ return;
129
+ }
130
+ this.timeline.closeRead = Date.now();
131
+ this.readStatus = 'closed';
132
+ if (err != null && this.endErr == null) {
133
+ this.endErr = err;
134
+ }
135
+ this.onCloseRead?.();
136
+ if (this.timeline.closeWrite != null) {
137
+ this.log.trace('source and sink ended');
138
+ this.timeline.close = Date.now();
139
+ if (this.status !== 'aborted' && this.status !== 'reset') {
140
+ this.status = 'closed';
141
+ }
142
+ if (this.onEnd != null) {
143
+ this.onEnd(this.endErr);
144
+ }
145
+ }
146
+ else {
147
+ this.log.trace('source ended, waiting for sink to end');
148
+ }
149
+ }
150
+ onSinkEnd(err) {
151
+ if (this.timeline.closeWrite != null) {
152
+ return;
153
+ }
154
+ this.timeline.closeWrite = Date.now();
155
+ this.writeStatus = 'closed';
156
+ if (err != null && this.endErr == null) {
157
+ this.endErr = err;
158
+ }
159
+ this.onCloseWrite?.();
160
+ if (this.timeline.closeRead != null) {
161
+ this.log.trace('sink and source ended');
162
+ this.timeline.close = Date.now();
163
+ if (this.status !== 'aborted' && this.status !== 'reset') {
164
+ this.status = 'closed';
165
+ }
166
+ if (this.onEnd != null) {
167
+ this.onEnd(this.endErr);
168
+ }
169
+ }
170
+ else {
171
+ this.log.trace('sink ended, waiting for source to end');
172
+ }
173
+ }
174
+ // Close for both Reading and Writing
175
+ async close(options) {
176
+ this.log.trace('closing gracefully');
177
+ this.status = 'closing';
178
+ await Promise.all([
179
+ this.closeRead(options),
180
+ this.closeWrite(options)
181
+ ]);
182
+ this.status = 'closed';
183
+ this.log.trace('closed gracefully');
184
+ }
185
+ async closeRead(options = {}) {
186
+ if (this.readStatus === 'closing' || this.readStatus === 'closed') {
187
+ return;
188
+ }
189
+ this.log.trace('closing readable end of stream with starting read status "%s"', this.readStatus);
190
+ const readStatus = this.readStatus;
191
+ this.readStatus = 'closing';
192
+ if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
193
+ this.log.trace('send close read to remote');
194
+ await this.sendCloseRead(options);
195
+ }
196
+ if (readStatus === 'ready') {
197
+ this.log.trace('ending internal source queue with %d queued bytes', this.streamSource.readableLength);
198
+ this.streamSource.end();
199
+ }
200
+ this.log.trace('closed readable end of stream');
201
+ }
202
+ async closeWrite(options = {}) {
203
+ if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
204
+ return;
205
+ }
206
+ this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus);
207
+ if (this.writeStatus === 'ready') {
208
+ this.log.trace('sink was never sunk, sink an empty array');
209
+ await raceSignal(this.sink([]), options.signal);
210
+ }
211
+ if (this.writeStatus === 'writing') {
212
+ // stop reading from the source passed to `.sink` in the microtask queue
213
+ // - this lets any data queued by the user in the current tick get read
214
+ // before we exit
215
+ await new Promise((resolve, reject) => {
216
+ queueMicrotask(() => {
217
+ this.log.trace('aborting source passed to .sink');
218
+ this.sinkController.abort();
219
+ raceSignal(this.sinkEnd.promise, options.signal)
220
+ .then(resolve, reject);
221
+ });
222
+ });
223
+ }
224
+ this.writeStatus = 'closed';
225
+ this.log.trace('closed writable end of stream');
226
+ }
227
+ /**
228
+ * Close immediately for reading and writing and send a reset message (local
229
+ * error)
230
+ */
231
+ abort(err) {
232
+ if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
233
+ return;
234
+ }
235
+ this.log('abort with error', err);
236
+ // try to send a reset message
237
+ this.log('try to send reset to remote');
238
+ const res = this.sendReset();
239
+ if (isPromise(res)) {
240
+ res.catch((err) => {
241
+ this.log.error('error sending reset message', err);
242
+ });
243
+ }
244
+ this.status = 'aborted';
245
+ this.timeline.abort = Date.now();
246
+ this._closeSinkAndSource(err);
247
+ this.onAbort?.(err);
248
+ }
249
+ /**
250
+ * Receive a reset message - close immediately for reading and writing (remote
251
+ * error)
252
+ */
253
+ reset() {
254
+ if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
255
+ return;
256
+ }
257
+ const err = new CodeError('stream reset', ERR_STREAM_RESET);
258
+ this.status = 'reset';
259
+ this.timeline.reset = Date.now();
260
+ this._closeSinkAndSource(err);
261
+ this.onReset?.();
262
+ }
263
+ _closeSinkAndSource(err) {
264
+ this._closeSink(err);
265
+ this._closeSource(err);
266
+ }
267
+ _closeSink(err) {
268
+ // if the sink function is running, cause it to end
269
+ if (this.writeStatus === 'writing') {
270
+ this.log.trace('end sink source');
271
+ this.sinkController.abort();
272
+ }
273
+ this.onSinkEnd(err);
274
+ }
275
+ _closeSource(err) {
276
+ // if the source is not ending, end it
277
+ if (this.readStatus !== 'closing' && this.readStatus !== 'closed') {
278
+ this.log.trace('ending source with %d bytes to be read by consumer', this.streamSource.readableLength);
279
+ this.readStatus = 'closing';
280
+ this.streamSource.end(err);
281
+ }
282
+ }
283
+ /**
284
+ * The remote closed for writing so we should expect to receive no more
285
+ * messages
286
+ */
287
+ remoteCloseWrite() {
288
+ if (this.readStatus === 'closing' || this.readStatus === 'closed') {
289
+ this.log('received remote close write but local source is already closed');
290
+ return;
291
+ }
292
+ this.log.trace('remote close write');
293
+ this._closeSource();
294
+ }
295
+ /**
296
+ * The remote closed for reading so we should not send any more
297
+ * messages
298
+ */
299
+ remoteCloseRead() {
300
+ if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
301
+ this.log('received remote close read but local sink is already closed');
302
+ return;
303
+ }
304
+ this.log.trace('remote close read');
305
+ this._closeSink();
306
+ }
307
+ /**
308
+ * The underlying muxer has closed, no more messages can be sent or will
309
+ * be received, close immediately to free up resources
310
+ */
311
+ destroy() {
312
+ if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
313
+ this.log('received destroy but we are already closed');
314
+ return;
315
+ }
316
+ this.log.trace('stream destroyed');
317
+ this._closeSinkAndSource();
318
+ }
319
+ /**
320
+ * When an extending class reads data from it's implementation-specific source,
321
+ * call this method to allow the stream consumer to read the data.
322
+ */
323
+ sourcePush(data) {
324
+ this.streamSource.push(data);
325
+ }
326
+ /**
327
+ * Returns the amount of unread data - can be used to prevent large amounts of
328
+ * data building up when the stream consumer is too slow.
329
+ */
330
+ sourceReadableLength() {
331
+ return this.streamSource.readableLength;
332
+ }
333
+ }
334
+ //# 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,SAAS,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAM/C,MAAM,gBAAgB,GAAG,kBAAkB,CAAA;AAC3C,MAAM,sBAAsB,GAAG,wBAAwB,CAAA;AACvD,MAAM,gCAAgC,GAAG,IAAI,CAAA;AA6D7C,SAAS,SAAS,CAAgB,KAAU;IAC1C,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;AAED,MAAM,OAAgB,cAAc;IAC3B,EAAE,CAAQ;IACV,SAAS,CAAW;IACpB,QAAQ,CAAgB;IACxB,QAAQ,CAAS;IACjB,QAAQ,CAAyB;IACjC,MAAM,CAA+C;IACrD,MAAM,CAAc;IACpB,UAAU,CAAY;IACtB,WAAW,CAAa;IACf,GAA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AAS,IAAI,IAAI,CAAC,UAAU,KAAK,QAAQ,EAAE;YACjE,IAAI,CAAC,GAAG,CAAC,gEAAgE,CAAC,CAAA;YAC1E,OAAM;SACP;QAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAA;QACpC,IAAI,CAAC,YAAY,EAAE,CAAA;IACrB,CAAC;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,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","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,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAEnE,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"}