@libp2p/utils 4.0.7-effcfaa8e → 5.0.0

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.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/dist/src/abstract-stream.d.ts +144 -0
  3. package/dist/src/abstract-stream.d.ts.map +1 -0
  4. package/dist/src/abstract-stream.js +340 -0
  5. package/dist/src/abstract-stream.js.map +1 -0
  6. package/dist/src/address-sort.d.ts +1 -1
  7. package/dist/src/address-sort.d.ts.map +1 -1
  8. package/dist/src/address-sort.js.map +1 -1
  9. package/dist/src/array-equals.js.map +1 -1
  10. package/dist/src/close-source.d.ts +4 -0
  11. package/dist/src/close-source.d.ts.map +1 -0
  12. package/dist/src/close-source.js +11 -0
  13. package/dist/src/close-source.js.map +1 -0
  14. package/dist/src/ip-port-to-multiaddr.d.ts.map +1 -1
  15. package/dist/src/ip-port-to-multiaddr.js +2 -6
  16. package/dist/src/ip-port-to-multiaddr.js.map +1 -1
  17. package/dist/src/is-promise.d.ts +2 -0
  18. package/dist/src/is-promise.d.ts.map +1 -0
  19. package/dist/src/is-promise.js +9 -0
  20. package/dist/src/is-promise.js.map +1 -0
  21. package/dist/src/multiaddr/is-private.js.map +1 -1
  22. package/dist/src/peer-job-queue.d.ts +33 -0
  23. package/dist/src/peer-job-queue.d.ts.map +1 -0
  24. package/dist/src/peer-job-queue.js +81 -0
  25. package/dist/src/peer-job-queue.js.map +1 -0
  26. package/dist/src/stream-to-ma-conn.d.ts +2 -1
  27. package/dist/src/stream-to-ma-conn.d.ts.map +1 -1
  28. package/dist/src/stream-to-ma-conn.js +57 -35
  29. package/dist/src/stream-to-ma-conn.js.map +1 -1
  30. package/dist/src/tracked-map.d.ts +17 -0
  31. package/dist/src/tracked-map.d.ts.map +1 -0
  32. package/dist/src/tracked-map.js +38 -0
  33. package/dist/src/tracked-map.js.map +1 -0
  34. package/dist/typedoc-urls.json +36 -0
  35. package/package.json +43 -9
  36. package/src/abstract-stream.ts +508 -0
  37. package/src/address-sort.ts +1 -1
  38. package/src/close-source.ts +14 -0
  39. package/src/ip-port-to-multiaddr.ts +2 -7
  40. package/src/is-promise.ts +9 -0
  41. package/src/peer-job-queue.ts +118 -0
  42. package/src/stream-to-ma-conn.ts +59 -36
  43. package/src/tracked-map.ts +65 -0
@@ -0,0 +1,33 @@
1
+ import PQueue from 'p-queue';
2
+ import type { PeerId } from '@libp2p/interface';
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,mBAAmB,CAAA;AAC/C,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"}
@@ -0,0 +1,81 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface';
3
+ import PQueue from 'p-queue';
4
+ // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
5
+ // Used to compute insertion index to keep queue sorted after insertion
6
+ function lowerBound(array, value, comparator) {
7
+ let first = 0;
8
+ let count = array.length;
9
+ while (count > 0) {
10
+ const step = Math.trunc(count / 2);
11
+ let it = first + step;
12
+ if (comparator(array[it], value) <= 0) {
13
+ first = ++it;
14
+ count -= step + 1;
15
+ }
16
+ else {
17
+ count = step;
18
+ }
19
+ }
20
+ return first;
21
+ }
22
+ /**
23
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
24
+ * that adds support for filtering jobs by peer id
25
+ */
26
+ class PeerPriorityQueue {
27
+ #queue = [];
28
+ enqueue(run, options) {
29
+ const peerId = options?.peerId;
30
+ const priority = options?.priority ?? 0;
31
+ if (peerId == null) {
32
+ throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS);
33
+ }
34
+ const element = {
35
+ priority,
36
+ peerId,
37
+ run
38
+ };
39
+ if (this.size > 0 && this.#queue[this.size - 1].priority >= priority) {
40
+ this.#queue.push(element);
41
+ return;
42
+ }
43
+ const index = lowerBound(this.#queue, element, (a, b) => b.priority - a.priority);
44
+ this.#queue.splice(index, 0, element);
45
+ }
46
+ dequeue() {
47
+ const item = this.#queue.shift();
48
+ return item?.run;
49
+ }
50
+ filter(options) {
51
+ if (options.peerId != null) {
52
+ const peerId = options.peerId;
53
+ return this.#queue.filter((element) => peerId.equals(element.peerId)).map((element) => element.run);
54
+ }
55
+ return this.#queue.filter((element) => element.priority === options.priority).map((element) => element.run);
56
+ }
57
+ get size() {
58
+ return this.#queue.length;
59
+ }
60
+ }
61
+ /**
62
+ * Extends PQueue to add support for querying queued jobs by peer id
63
+ */
64
+ export class PeerJobQueue extends PQueue {
65
+ constructor(options = {}) {
66
+ super({
67
+ ...options,
68
+ queueClass: PeerPriorityQueue
69
+ });
70
+ }
71
+ /**
72
+ * Returns true if this queue has a job for the passed peer id that has not yet
73
+ * started to run
74
+ */
75
+ hasJob(peerId) {
76
+ return this.sizeBy({
77
+ peerId
78
+ }) > 0;
79
+ }
80
+ }
81
+ //# sourceMappingURL=peer-job-queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-job-queue.js","sourceRoot":"","sources":["../../src/peer-job-queue.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,MAAM,MAAM,SAAS,CAAA;AAI5B,mFAAmF;A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ACH,MAAM,CAAE,MAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC;YACjB,MAAM;SACP,CAAC,GAAG,CAAC,CAAA;IACR,CAAC;CACF"}
@@ -1,9 +1,10 @@
1
- import type { MultiaddrConnection, Stream } from '@libp2p/interface/connection';
1
+ import type { ComponentLogger, MultiaddrConnection, Stream } from '@libp2p/interface';
2
2
  import type { Multiaddr } from '@multiformats/multiaddr';
3
3
  export interface StreamProperties {
4
4
  stream: Stream;
5
5
  remoteAddr: Multiaddr;
6
6
  localAddr: Multiaddr;
7
+ logger: ComponentLogger;
7
8
  }
8
9
  /**
9
10
  * Convert a duplex iterable into a MultiaddrConnection.
@@ -1 +1 @@
1
- {"version":3,"file":"stream-to-ma-conn.d.ts","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAA;AAC/E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAIxD,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,SAAS,CAAA;IACrB,SAAS,EAAE,SAAS,CAAA;CACrB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAE,KAAK,EAAE,gBAAgB,GAAG,mBAAmB,CAiDlF"}
1
+ {"version":3,"file":"stream-to-ma-conn.d.ts","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AACrF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,SAAS,CAAA;IACrB,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,eAAe,CAAA;CACxB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAE,KAAK,EAAE,gBAAgB,GAAG,mBAAmB,CA2ElF"}
@@ -1,52 +1,74 @@
1
- import { logger } from '@libp2p/logger';
2
- const log = logger('libp2p:stream:converter');
3
1
  /**
4
2
  * Convert a duplex iterable into a MultiaddrConnection.
5
3
  * https://github.com/libp2p/interface-transport#multiaddrconnection
6
4
  */
7
5
  export function streamToMaConnection(props) {
8
- const { stream, remoteAddr } = props;
9
- const { sink, source } = stream;
10
- const mapSource = (async function* () {
11
- for await (const list of source) {
12
- if (list instanceof Uint8Array) {
13
- yield list;
14
- }
15
- else {
16
- yield* list;
6
+ const { stream, remoteAddr, logger } = props;
7
+ const log = logger.forComponent('libp2p:stream:converter');
8
+ let closedRead = false;
9
+ let closedWrite = false;
10
+ // piggyback on `stream.close` invocations to close maconn
11
+ const streamClose = stream.close.bind(stream);
12
+ stream.close = async (options) => {
13
+ await streamClose(options);
14
+ close(true);
15
+ };
16
+ // piggyback on `stream.abort` invocations to close maconn
17
+ const streamAbort = stream.abort.bind(stream);
18
+ stream.abort = (err) => {
19
+ streamAbort(err);
20
+ close(true);
21
+ };
22
+ // piggyback on `stream.sink` invocations to close maconn
23
+ const streamSink = stream.sink.bind(stream);
24
+ stream.sink = async (source) => {
25
+ try {
26
+ await streamSink(source);
27
+ }
28
+ catch (err) {
29
+ // If aborted we can safely ignore
30
+ if (err.type !== 'aborted') {
31
+ // If the source errored the socket will already have been destroyed by
32
+ // toIterable.duplex(). If the socket errored it will already be
33
+ // destroyed. There's nothing to do here except log the error & return.
34
+ log(err);
17
35
  }
18
36
  }
19
- }());
37
+ finally {
38
+ closedWrite = true;
39
+ close();
40
+ }
41
+ };
20
42
  const maConn = {
21
- async sink(source) {
43
+ log,
44
+ sink: stream.sink,
45
+ source: (async function* () {
22
46
  try {
23
- await sink(source);
24
- close();
25
- }
26
- catch (err) {
27
- // If aborted we can safely ignore
28
- if (err.type !== 'aborted') {
29
- // If the source errored the socket will already have been destroyed by
30
- // toIterable.duplex(). If the socket errored it will already be
31
- // destroyed. There's nothing to do here except log the error & return.
32
- log(err);
47
+ for await (const list of stream.source) {
48
+ if (list instanceof Uint8Array) {
49
+ yield list;
50
+ }
51
+ else {
52
+ yield* list;
53
+ }
33
54
  }
34
55
  }
35
- },
36
- source: mapSource,
56
+ finally {
57
+ closedRead = true;
58
+ close();
59
+ }
60
+ }()),
37
61
  remoteAddr,
38
62
  timeline: { open: Date.now(), close: undefined },
39
- async close(options) {
40
- close();
41
- await stream.close(options);
42
- },
43
- abort(err) {
44
- close();
45
- stream.abort(err);
46
- }
63
+ close: stream.close,
64
+ abort: stream.abort
47
65
  };
48
- function close() {
49
- if (maConn.timeline.close == null) {
66
+ function close(force) {
67
+ if (force === true) {
68
+ closedRead = true;
69
+ closedWrite = true;
70
+ }
71
+ if (closedRead && closedWrite && maConn.timeline.close == null) {
50
72
  maConn.timeline.close = Date.now();
51
73
  }
52
74
  }
@@ -1 +1 @@
1
- {"version":3,"file":"stream-to-ma-conn.js","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAKvC,MAAM,GAAG,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAA;AAQ7C;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAE,KAAuB;IAC3D,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,KAAK,CAAA;IACpC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAAA;IAE/B,MAAM,SAAS,GAAG,CAAC,KAAK,SAAU,CAAC;QACjC,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,MAAM,EAAE;YAC/B,IAAI,IAAI,YAAY,UAAU,EAAE;gBAC9B,MAAM,IAAI,CAAA;aACX;iBAAM;gBACL,KAAM,CAAC,CAAC,IAAI,CAAA;aACb;SACF;IACH,CAAC,EAAE,CAAC,C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I,MAAM,CAAC,MAAM,EAAE,CAAC;oBACvC,IAAI,IAAI,YAAY,UAAU,EAAE,CAAC;wBAC/B,MAAM,IAAI,CAAA;oBACZ,CAAC;yBAAM,CAAC;wBACN,KAAM,CAAC,CAAC,IAAI,CAAA;oBACd,CAAC;gBACH,CAAC;YACH,CAAC;oBAAS,CAAC;gBACT,UAAU,GAAG,IAAI,CAAA;gBACjB,KAAK,EAAE,CAAA;YACT,CAAC;QACH,CAAC,EAAE,CAAC;QACJ,UAAU;QACV,QAAQ,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE;QAChD,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,KAAK,EAAE,MAAM,CAAC,KAAK;KACpB,CAAA;IAED,SAAS,KAAK,CAAE,KAAe;QAC7B,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,UAAU,GAAG,IAAI,CAAA;YACjB,WAAW,GAAG,IAAI,CAAA;QACpB,CAAC;QAED,IAAI,UAAU,IAAI,WAAW,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;YAC/D,MAAM,CAAC,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QACpC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { Metrics } from '@libp2p/interface';
2
+ export interface TrackedMapInit {
3
+ name: string;
4
+ metrics: Metrics;
5
+ }
6
+ export interface CreateTrackedMapInit {
7
+ /**
8
+ * The metric name to use
9
+ */
10
+ name: string;
11
+ /**
12
+ * A metrics implementation
13
+ */
14
+ metrics?: Metrics;
15
+ }
16
+ export declare function trackedMap<K, V>(config: CreateTrackedMapInit): Map<K, V>;
17
+ //# sourceMappingURL=tracked-map.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracked-map.d.ts","sourceRoot":"","sources":["../../src/tracked-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,OAAO,CAAA;CACjB;AAoCD,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,wBAAgB,UAAU,CAAE,CAAC,EAAE,CAAC,EAAG,MAAM,EAAE,oBAAoB,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAW1E"}
@@ -0,0 +1,38 @@
1
+ class TrackedMap extends Map {
2
+ metric;
3
+ constructor(init) {
4
+ super();
5
+ const { name, metrics } = init;
6
+ this.metric = metrics.registerMetric(name);
7
+ this.updateComponentMetric();
8
+ }
9
+ set(key, value) {
10
+ super.set(key, value);
11
+ this.updateComponentMetric();
12
+ return this;
13
+ }
14
+ delete(key) {
15
+ const deleted = super.delete(key);
16
+ this.updateComponentMetric();
17
+ return deleted;
18
+ }
19
+ clear() {
20
+ super.clear();
21
+ this.updateComponentMetric();
22
+ }
23
+ updateComponentMetric() {
24
+ this.metric.update(this.size);
25
+ }
26
+ }
27
+ export function trackedMap(config) {
28
+ const { name, metrics } = config;
29
+ let map;
30
+ if (metrics != null) {
31
+ map = new TrackedMap({ name, metrics });
32
+ }
33
+ else {
34
+ map = new Map();
35
+ }
36
+ return map;
37
+ }
38
+ //# sourceMappingURL=tracked-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracked-map.js","sourceRoot":"","sources":["../../src/tracked-map.ts"],"names":[],"mappings":"AAOA,MAAM,UAAiB,SAAQ,GAAS;IACrB,MAAM,CAAQ;IAE/B,YAAa,IAAoB;QAC/B,KAAK,EAAE,CAAA;QAEP,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;QAE9B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;QAC1C,IAAI,CAAC,qBAAqB,EAAE,CAAA;IAC9B,CAAC;IAED,GAAG,CAAE,GAAM,EAAE,KAAQ;QACnB,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QACrB,IAAI,CAAC,qBAAqB,EAAE,CAAA;QAC5B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,CAAE,GAAM;QACZ,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACjC,IAAI,CAAC,qBAAqB,EAAE,CAAA;QAC5B,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,KAAK;QACH,KAAK,CAAC,KAAK,EAAE,CAAA;QACb,IAAI,CAAC,qBAAqB,EAAE,CAAA;IAC9B,CAAC;IAEO,qBAAqB;QAC3B,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC/B,CAAC;CACF;AAcD,MAAM,UAAU,UAAU,CAAS,MAA4B;IAC7D,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;IAChC,IAAI,GAAc,CAAA;IAElB,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;QACpB,GAAG,GAAG,IAAI,UAAU,CAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;IAC/C,CAAC;SAAM,CAAC;QACN,GAAG,GAAG,IAAI,GAAG,EAAQ,CAAA;IACvB,CAAC;IAED,OAAO,GAAG,CAAA;AACZ,CAAC"}
@@ -0,0 +1,36 @@
1
+ {
2
+ "AbstractStream": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.abstract_stream.AbstractStream.html",
3
+ "./abstract-stream:AbstractStream": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.abstract_stream.AbstractStream.html",
4
+ "AbstractStreamInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.abstract_stream.AbstractStreamInit.html",
5
+ "./abstract-stream:AbstractStreamInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.abstract_stream.AbstractStreamInit.html",
6
+ "certifiedAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.certifiedAddressesFirst.html",
7
+ "./address-sort:certifiedAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.certifiedAddressesFirst.html",
8
+ "circuitRelayAddressesLast": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.circuitRelayAddressesLast.html",
9
+ "./address-sort:circuitRelayAddressesLast": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.circuitRelayAddressesLast.html",
10
+ "defaultAddressSort": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.defaultAddressSort.html",
11
+ "./address-sort:defaultAddressSort": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.defaultAddressSort.html",
12
+ "publicAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.publicAddressesFirst.html",
13
+ "./address-sort:publicAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.publicAddressesFirst.html",
14
+ "arrayEquals": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.array_equals.arrayEquals.html",
15
+ "./array-equals:arrayEquals": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.array_equals.arrayEquals.html",
16
+ "closeSource": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.close_source.closeSource.html",
17
+ "./close-source:closeSource": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.close_source.closeSource.html",
18
+ "Errors": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.ip_port_to_multiaddr.Errors.html",
19
+ "./ip-port-to-multiaddr:Errors": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.ip_port_to_multiaddr.Errors.html",
20
+ "ipPortToMultiaddr": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.ip_port_to_multiaddr.ipPortToMultiaddr.html",
21
+ "./ip-port-to-multiaddr:ipPortToMultiaddr": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.ip_port_to_multiaddr.ipPortToMultiaddr.html",
22
+ "isPromise": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.is_promise.isPromise.html",
23
+ "./is-promise:isPromise": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.is_promise.isPromise.html",
24
+ "isLoopback": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_loopback.isLoopback.html",
25
+ "./multiaddr/is-loopback:isLoopback": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_loopback.isLoopback.html",
26
+ "isPrivate": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_private.isPrivate.html",
27
+ "./multiaddr/is-private:isPrivate": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_private.isPrivate.html",
28
+ "PeerJobQueue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.peer_job_queue.PeerJobQueue.html",
29
+ "./peer-job-queue:PeerJobQueue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.peer_job_queue.PeerJobQueue.html",
30
+ "PeerPriorityQueueOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.peer_job_queue.PeerPriorityQueueOptions.html",
31
+ "./peer-job-queue:PeerPriorityQueueOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.peer_job_queue.PeerPriorityQueueOptions.html",
32
+ "StreamProperties": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.stream_to_ma_conn.StreamProperties.html",
33
+ "./stream-to-ma-conn:StreamProperties": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.stream_to_ma_conn.StreamProperties.html",
34
+ "streamToMaConnection": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.stream_to_ma_conn.streamToMaConnection.html",
35
+ "./stream-to-ma-conn:streamToMaConnection": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.stream_to_ma_conn.streamToMaConnection.html"
36
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@libp2p/utils",
3
- "version": "4.0.7-effcfaa8e",
3
+ "version": "5.0.0",
4
4
  "description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
5
5
  "license": "Apache-2.0 OR MIT",
6
- "homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/utils#readme",
6
+ "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/libp2p/js-libp2p.git"
@@ -11,6 +11,10 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/libp2p/js-libp2p/issues"
13
13
  },
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "provenance": true
17
+ },
14
18
  "type": "module",
15
19
  "types": "./dist/src/index.d.ts",
16
20
  "typesVersions": {
@@ -40,6 +44,10 @@
40
44
  "types": "./src/index.d.ts",
41
45
  "import": "./dist/src/index.js"
42
46
  },
47
+ "./abstract-stream": {
48
+ "types": "./dist/src/abstract-stream.d.ts",
49
+ "import": "./dist/src/abstract-stream.js"
50
+ },
43
51
  "./address-sort": {
44
52
  "types": "./dist/src/address-sort.d.ts",
45
53
  "import": "./dist/src/address-sort.js"
@@ -48,10 +56,18 @@
48
56
  "types": "./dist/src/array-equals.d.ts",
49
57
  "import": "./dist/src/array-equals.js"
50
58
  },
59
+ "./close-source": {
60
+ "types": "./dist/src/close-source.d.ts",
61
+ "import": "./dist/src/close-source.js"
62
+ },
51
63
  "./ip-port-to-multiaddr": {
52
64
  "types": "./dist/src/ip-port-to-multiaddr.d.ts",
53
65
  "import": "./dist/src/ip-port-to-multiaddr.js"
54
66
  },
67
+ "./is-promise": {
68
+ "types": "./dist/src/is-promise.d.ts",
69
+ "import": "./dist/src/is-promise.js"
70
+ },
55
71
  "./multiaddr/is-loopback": {
56
72
  "types": "./dist/src/multiaddr/is-loopback.d.ts",
57
73
  "import": "./dist/src/multiaddr/is-loopback.js"
@@ -60,9 +76,17 @@
60
76
  "types": "./dist/src/multiaddr/is-private.d.ts",
61
77
  "import": "./dist/src/multiaddr/is-private.js"
62
78
  },
79
+ "./peer-job-queue": {
80
+ "types": "./dist/src/peer-job-queue.d.ts",
81
+ "import": "./dist/src/peer-job-queue.js"
82
+ },
63
83
  "./stream-to-ma-conn": {
64
84
  "types": "./dist/src/stream-to-ma-conn.d.ts",
65
85
  "import": "./dist/src/stream-to-ma-conn.js"
86
+ },
87
+ "./tracked-map": {
88
+ "types": "./dist/src/tracked-map.d.ts",
89
+ "import": "./dist/src/tracked-map.js"
66
90
  }
67
91
  },
68
92
  "eslintConfig": {
@@ -87,20 +111,30 @@
87
111
  },
88
112
  "dependencies": {
89
113
  "@chainsafe/is-ip": "^2.0.2",
90
- "@libp2p/interface": "0.1.6-effcfaa8e",
91
- "@libp2p/logger": "3.1.0-effcfaa8e",
92
- "@multiformats/multiaddr": "^12.1.5",
93
- "@multiformats/multiaddr-matcher": "^1.0.1",
114
+ "@libp2p/interface": "^1.0.0",
115
+ "@multiformats/multiaddr": "^12.1.10",
116
+ "@multiformats/multiaddr-matcher": "^1.1.0",
117
+ "get-iterator": "^2.0.1",
94
118
  "is-loopback-addr": "^2.0.1",
119
+ "it-pushable": "^3.2.2",
95
120
  "it-stream-types": "^2.0.1",
96
- "private-ip": "^3.0.0",
121
+ "p-queue": "^7.4.1",
122
+ "private-ip": "^3.0.1",
123
+ "race-signal": "^1.0.1",
97
124
  "uint8arraylist": "^2.4.3"
98
125
  },
99
126
  "devDependencies": {
127
+ "@libp2p/logger": "^4.0.0",
128
+ "@libp2p/peer-id-factory": "^3.0.9",
100
129
  "aegir": "^41.0.2",
101
- "it-all": "^3.0.1",
130
+ "delay": "^6.0.0",
131
+ "it-all": "^3.0.3",
132
+ "it-drain": "^3.0.5",
102
133
  "it-pair": "^2.0.6",
103
134
  "it-pipe": "^3.0.1",
104
- "uint8arrays": "^4.0.4"
135
+ "p-defer": "^4.0.0",
136
+ "sinon": "^17.0.1",
137
+ "sinon-ts": "^2.0.0",
138
+ "uint8arrays": "^4.0.6"
105
139
  }
106
140
  }