@libp2p/utils 5.3.2 → 5.4.0-4ad63bb79

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 (62) hide show
  1. package/dist/src/abort-options.d.ts +7 -0
  2. package/dist/src/abort-options.d.ts.map +1 -0
  3. package/dist/src/abort-options.js +14 -0
  4. package/dist/src/abort-options.js.map +1 -0
  5. package/dist/src/adaptive-timeout.d.ts +35 -0
  6. package/dist/src/adaptive-timeout.d.ts.map +1 -0
  7. package/dist/src/adaptive-timeout.js +63 -0
  8. package/dist/src/adaptive-timeout.js.map +1 -0
  9. package/dist/src/close.d.ts +21 -0
  10. package/dist/src/close.d.ts.map +1 -0
  11. package/dist/src/close.js +49 -0
  12. package/dist/src/close.js.map +1 -0
  13. package/dist/src/filters/bloom-filter.d.ts +34 -0
  14. package/dist/src/filters/bloom-filter.d.ts.map +1 -0
  15. package/dist/src/filters/bloom-filter.js +113 -0
  16. package/dist/src/filters/bloom-filter.js.map +1 -0
  17. package/dist/src/filters/bucket.d.ts +10 -0
  18. package/dist/src/filters/bucket.d.ts.map +1 -0
  19. package/dist/src/filters/bucket.js +53 -0
  20. package/dist/src/filters/bucket.js.map +1 -0
  21. package/dist/src/filters/cuckoo-filter.d.ts +41 -0
  22. package/dist/src/filters/cuckoo-filter.d.ts.map +1 -0
  23. package/dist/src/filters/cuckoo-filter.js +134 -0
  24. package/dist/src/filters/cuckoo-filter.js.map +1 -0
  25. package/dist/src/filters/fingerprint.d.ts +11 -0
  26. package/dist/src/filters/fingerprint.d.ts.map +1 -0
  27. package/dist/src/filters/fingerprint.js +34 -0
  28. package/dist/src/filters/fingerprint.js.map +1 -0
  29. package/dist/src/filters/hashes.d.ts +8 -0
  30. package/dist/src/filters/hashes.d.ts.map +1 -0
  31. package/dist/src/filters/hashes.js +29 -0
  32. package/dist/src/filters/hashes.js.map +1 -0
  33. package/dist/src/filters/index.d.ts +9 -0
  34. package/dist/src/filters/index.d.ts.map +1 -0
  35. package/dist/src/filters/index.js +4 -0
  36. package/dist/src/filters/index.js.map +1 -0
  37. package/dist/src/filters/scalable-cuckoo-filter.d.ts +24 -0
  38. package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -0
  39. package/dist/src/filters/scalable-cuckoo-filter.js +87 -0
  40. package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -0
  41. package/dist/src/filters/utils.d.ts +2 -0
  42. package/dist/src/filters/utils.d.ts.map +1 -0
  43. package/dist/src/filters/utils.js +4 -0
  44. package/dist/src/filters/utils.js.map +1 -0
  45. package/dist/src/moving-average.d.ts +18 -0
  46. package/dist/src/moving-average.d.ts.map +1 -0
  47. package/dist/src/moving-average.js +43 -0
  48. package/dist/src/moving-average.js.map +1 -0
  49. package/package.json +32 -7
  50. package/src/abort-options.ts +20 -0
  51. package/src/adaptive-timeout.ts +94 -0
  52. package/src/close.ts +65 -0
  53. package/src/filters/bloom-filter.ts +142 -0
  54. package/src/filters/bucket.ts +64 -0
  55. package/src/filters/cuckoo-filter.ts +197 -0
  56. package/src/filters/fingerprint.ts +44 -0
  57. package/src/filters/hashes.ts +38 -0
  58. package/src/filters/index.ts +9 -0
  59. package/src/filters/scalable-cuckoo-filter.ts +111 -0
  60. package/src/filters/utils.ts +3 -0
  61. package/src/moving-average.ts +45 -0
  62. package/dist/typedoc-urls.json +0 -76
@@ -0,0 +1,11 @@
1
+ import type { Hash } from './hashes';
2
+ export declare const MAX_FINGERPRINT_SIZE = 64;
3
+ export declare class Fingerprint {
4
+ private readonly fp;
5
+ private readonly h;
6
+ private readonly seed;
7
+ constructor(buf: Uint8Array, hash: Hash, seed: number, fingerprintSize?: number);
8
+ hash(): number;
9
+ equals(other?: any): boolean;
10
+ }
11
+ //# sourceMappingURL=fingerprint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fingerprint.d.ts","sourceRoot":"","sources":["../../../src/filters/fingerprint.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AAEpC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AAEtC,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAY;IAC/B,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAM;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;gBAEhB,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,eAAe,GAAE,MAAU;IAqBnF,IAAI,IAAK,MAAM;IAIf,MAAM,CAAE,KAAK,CAAC,EAAE,GAAG,GAAG,OAAO;CAO9B"}
@@ -0,0 +1,34 @@
1
+ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc';
2
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
3
+ export const MAX_FINGERPRINT_SIZE = 64;
4
+ export class Fingerprint {
5
+ fp;
6
+ h;
7
+ seed;
8
+ constructor(buf, hash, seed, fingerprintSize = 2) {
9
+ if (fingerprintSize > MAX_FINGERPRINT_SIZE) {
10
+ throw new TypeError('Invalid Fingerprint Size');
11
+ }
12
+ const fnv = hash.hashV(buf, seed);
13
+ const fp = uint8ArrayAlloc(fingerprintSize);
14
+ for (let i = 0; i < fp.length; i++) {
15
+ fp[i] = fnv[i];
16
+ }
17
+ if (fp.length === 0) {
18
+ fp[0] = 7;
19
+ }
20
+ this.fp = fp;
21
+ this.h = hash;
22
+ this.seed = seed;
23
+ }
24
+ hash() {
25
+ return this.h.hash(this.fp, this.seed);
26
+ }
27
+ equals(other) {
28
+ if (!(other?.fp instanceof Uint8Array)) {
29
+ return false;
30
+ }
31
+ return uint8ArrayEquals(this.fp, other.fp);
32
+ }
33
+ }
34
+ //# sourceMappingURL=fingerprint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fingerprint.js","sourceRoot":"","sources":["../../../src/filters/fingerprint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IA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AgB,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,CAAC,CAAA;IAC5C,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ export interface Hash {
2
+ hash(input: Uint8Array, seed: number): number;
3
+ hashV(input: Uint8Array, seed: number): Uint8Array;
4
+ }
5
+ export declare const murmur3: Hash;
6
+ export declare const fnv1a: Hash;
7
+ export declare function numberToBuffer(num: bigint | number): Uint8Array;
8
+ //# sourceMappingURL=hashes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashes.d.ts","sourceRoot":"","sources":["../../../src/filters/hashes.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,IAAI;IACnB,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;IAC7C,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAAA;CACnD;AAED,eAAO,MAAM,OAAO,EAAE,IAOrB,CAAA;AAED,eAAO,MAAM,KAAK,EAAE,IASnB,CAAA;AAED,wBAAgB,cAAc,CAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,CAQhE"}
@@ -0,0 +1,29 @@
1
+ import fnv1aHash from '@sindresorhus/fnv1a';
2
+ import mur from 'murmurhash3js-revisited';
3
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
4
+ export const murmur3 = {
5
+ hash: (input, seed) => {
6
+ return mur.x86.hash32(input, seed);
7
+ },
8
+ hashV: (input, seed) => {
9
+ return numberToBuffer(murmur3.hash(input, seed));
10
+ }
11
+ };
12
+ export const fnv1a = {
13
+ hash: (input) => {
14
+ return Number(fnv1aHash(input, {
15
+ size: 32
16
+ }));
17
+ },
18
+ hashV: (input, seed) => {
19
+ return numberToBuffer(fnv1a.hash(input, seed));
20
+ }
21
+ };
22
+ export function numberToBuffer(num) {
23
+ let hex = num.toString(16);
24
+ if (hex.length % 2 === 1) {
25
+ hex = `0${hex}`;
26
+ }
27
+ return uint8ArrayFromString(hex, 'base16');
28
+ }
29
+ //# sourceMappingURL=hashes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashes.js","sourceRoot":"","sources":["../../../src/filters/hashes.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,qBAAqB,CAAA;AAC3C,OAAO,GAAG,MAAM,yBAAyB,CAAA;AACzC,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAO5E,MAAM,CAAC,MAAM,OAAO,GAAS;IAC3B,IAAI,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACpB,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACpC,CAAC;IACD,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACrB,OAAO,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAA;IAClD,CAAC;CACF,CAAA;AAED,MAAM,CAAC,MAAM,KAAK,GAAS;IACzB,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;QACd,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE;YAC7B,IAAI,EAAE,EAAE;SACT,CAAC,CAAC,CAAA;IACL,CAAC;IACD,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACrB,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAA;IAChD,CAAC;CACF,CAAA;AAED,MAAM,UAAU,cAAc,CAAE,GAAoB;IAClD,IAAI,GAAG,GAAG,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;IAE1B,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,GAAG,GAAG,IAAI,GAAG,EAAE,CAAA;IACjB,CAAC;IAED,OAAO,oBAAoB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;AAC5C,CAAC"}
@@ -0,0 +1,9 @@
1
+ export { BloomFilter, createBloomFilter, type BloomFilterOptions } from './bloom-filter.js';
2
+ export { CuckooFilter, createCuckooFilter, type CuckooFilterInit } from './cuckoo-filter.js';
3
+ export { ScalableCuckooFilter, createScalableCuckooFilter, type ScalableCuckooFilterInit } from './scalable-cuckoo-filter.js';
4
+ export interface Filter {
5
+ add(item: Uint8Array | string): void;
6
+ has(item: Uint8Array | string): boolean;
7
+ remove?(buf: Uint8Array | string): boolean;
8
+ }
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/filters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3F,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC5F,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,KAAK,wBAAwB,EAAE,MAAM,6BAA6B,CAAA;AAE7H,MAAM,WAAW,MAAM;IACrB,GAAG,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,CAAA;IACpC,GAAG,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO,CAAA;IACvC,MAAM,CAAC,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO,CAAA;CAC3C"}
@@ -0,0 +1,4 @@
1
+ export { BloomFilter, createBloomFilter } from './bloom-filter.js';
2
+ export { CuckooFilter, createCuckooFilter } from './cuckoo-filter.js';
3
+ export { ScalableCuckooFilter, createScalableCuckooFilter } from './scalable-cuckoo-filter.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/filters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAA2B,MAAM,mBAAmB,CAAA;AAC3F,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAyB,MAAM,oBAAoB,CAAA;AAC5F,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAiC,MAAM,6BAA6B,CAAA"}
@@ -0,0 +1,24 @@
1
+ import { type CuckooFilterInit } from './cuckoo-filter.js';
2
+ import type { Filter } from './index.js';
3
+ export interface ScalableCuckooFilterInit extends CuckooFilterInit {
4
+ /**
5
+ * A number to multiply maxItems by when adding new sub-filters
6
+ */
7
+ scale?: number;
8
+ }
9
+ export declare class ScalableCuckooFilter implements Filter {
10
+ private readonly filterSize;
11
+ private readonly bucketSize;
12
+ private readonly fingerprintSize;
13
+ private readonly scale;
14
+ private readonly filterSeries;
15
+ private readonly hash;
16
+ private readonly seed;
17
+ constructor(init: ScalableCuckooFilterInit);
18
+ add(item: Uint8Array | string): boolean;
19
+ has(item: Uint8Array | string): boolean;
20
+ remove(item: Uint8Array | string): boolean;
21
+ get count(): number;
22
+ }
23
+ export declare function createScalableCuckooFilter(maxItems: number, errorRate?: number, options?: Pick<ScalableCuckooFilterInit, 'hash' | 'seed' | 'scale'>): Filter;
24
+ //# sourceMappingURL=scalable-cuckoo-filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scalable-cuckoo-filter.d.ts","sourceRoot":"","sources":["../../../src/filters/scalable-cuckoo-filter.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAGlF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,WAAW,wBAAyB,SAAQ,gBAAgB;IAChE;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,qBAAa,oBAAqB,YAAW,MAAM;IACjD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;IACnC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;IACnC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAQ;IACxC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAgB;IAC7C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAM;IAC3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;gBAEhB,IAAI,EAAE,wBAAwB;IAkB3C,GAAG,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IA8BxC,GAAG,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IAcxC,MAAM,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IAc3C,IAAI,KAAK,IAAK,MAAM,CAInB;CACF;AAED,wBAAgB,0BAA0B,CAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,GAAE,MAAc,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,wBAAwB,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GAAG,MAAM,CAKpK"}
@@ -0,0 +1,87 @@
1
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
2
+ import { CuckooFilter, optimize } from './cuckoo-filter.js';
3
+ import { fnv1a } from './hashes.js';
4
+ import { getRandomInt } from './utils.js';
5
+ export class ScalableCuckooFilter {
6
+ filterSize;
7
+ bucketSize;
8
+ fingerprintSize;
9
+ scale;
10
+ filterSeries;
11
+ hash;
12
+ seed;
13
+ constructor(init) {
14
+ this.bucketSize = init.bucketSize ?? 4;
15
+ this.filterSize = init.filterSize ?? (1 << 18) / this.bucketSize;
16
+ this.fingerprintSize = init.fingerprintSize ?? 2;
17
+ this.scale = init.scale ?? 2;
18
+ this.hash = init.hash ?? fnv1a;
19
+ this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10));
20
+ this.filterSeries = [
21
+ new CuckooFilter({
22
+ filterSize: this.filterSize,
23
+ bucketSize: this.bucketSize,
24
+ fingerprintSize: this.fingerprintSize,
25
+ hash: this.hash,
26
+ seed: this.seed
27
+ })
28
+ ];
29
+ }
30
+ add(item) {
31
+ if (typeof item === 'string') {
32
+ item = uint8ArrayFromString(item);
33
+ }
34
+ if (this.has(item)) {
35
+ return true;
36
+ }
37
+ let current = this.filterSeries.find((cuckoo) => {
38
+ return cuckoo.reliable;
39
+ });
40
+ if (current == null) {
41
+ const curSize = this.filterSize * Math.pow(this.scale, this.filterSeries.length);
42
+ current = new CuckooFilter({
43
+ filterSize: curSize,
44
+ bucketSize: this.bucketSize,
45
+ fingerprintSize: this.fingerprintSize,
46
+ hash: this.hash,
47
+ seed: this.seed
48
+ });
49
+ this.filterSeries.push(current);
50
+ }
51
+ return current.add(item);
52
+ }
53
+ has(item) {
54
+ if (typeof item === 'string') {
55
+ item = uint8ArrayFromString(item);
56
+ }
57
+ for (let i = 0; i < this.filterSeries.length; i++) {
58
+ if (this.filterSeries[i].has(item)) {
59
+ return true;
60
+ }
61
+ }
62
+ return false;
63
+ }
64
+ remove(item) {
65
+ if (typeof item === 'string') {
66
+ item = uint8ArrayFromString(item);
67
+ }
68
+ for (let i = 0; i < this.filterSeries.length; i++) {
69
+ if (this.filterSeries[i].remove(item)) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ }
75
+ get count() {
76
+ return this.filterSeries.reduce((acc, curr) => {
77
+ return acc + curr.count;
78
+ }, 0);
79
+ }
80
+ }
81
+ export function createScalableCuckooFilter(maxItems, errorRate = 0.001, options) {
82
+ return new ScalableCuckooFilter({
83
+ ...optimize(maxItems, errorRate),
84
+ ...(options ?? {})
85
+ });
86
+ }
87
+ //# sourceMappingURL=scalable-cuckoo-filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scalable-cuckoo-filter.js","sourceRoot":"","sources":["../../../src/filters/scalable-cuckoo-filter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAA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AA;YACb,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,CAAE,IAAyB;QAC/B,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,IAAI,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAA;QACnC,CAAC;QAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAClD,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;gBACtC,OAAO,IAAI,CAAA;YACb,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;YAC5C,OAAO,GAAG,GAAG,IAAI,CAAC,KAAK,CAAA;QACzB,CAAC,EAAE,CAAC,CAAC,CAAA;IACP,CAAC;CACF;AAED,MAAM,UAAU,0BAA0B,CAAE,QAAgB,EAAE,YAAoB,KAAK,EAAE,OAAmE;IAC1J,OAAO,IAAI,oBAAoB,CAAC;QAC9B,GAAG,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;QAChC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;KACnB,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function getRandomInt(min: number, max: number): number;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/filters/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9D"}
@@ -0,0 +1,4 @@
1
+ export function getRandomInt(min, max) {
2
+ return Math.floor(Math.random() * (max - min)) + min;
3
+ }
4
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/filters/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAE,GAAW,EAAE,GAAW;IACpD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAA;AACtD,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Implements exponential moving average. Ported from `moving-average`.
3
+ *
4
+ * @see https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
5
+ * @see https://www.npmjs.com/package/moving-average
6
+ */
7
+ export declare class MovingAverage {
8
+ movingAverage: number;
9
+ variance: number;
10
+ deviation: number;
11
+ forecast: number;
12
+ private readonly timespan;
13
+ private previousTime?;
14
+ constructor(timespan: number);
15
+ alpha(t: number, pt: number): number;
16
+ push(value: number, time?: number): void;
17
+ }
18
+ //# sourceMappingURL=moving-average.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moving-average.d.ts","sourceRoot":"","sources":["../../src/moving-average.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,aAAa;IACjB,aAAa,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,YAAY,CAAC,CAAQ;gBAEhB,QAAQ,EAAE,MAAM;IAQ7B,KAAK,CAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM;IAIrC,IAAI,CAAE,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,MAAmB,GAAG,IAAI;CAkBtD"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Implements exponential moving average. Ported from `moving-average`.
3
+ *
4
+ * @see https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
5
+ * @see https://www.npmjs.com/package/moving-average
6
+ */
7
+ export class MovingAverage {
8
+ movingAverage;
9
+ variance;
10
+ deviation;
11
+ forecast;
12
+ timespan;
13
+ previousTime;
14
+ constructor(timespan) {
15
+ this.timespan = timespan;
16
+ this.movingAverage = 0;
17
+ this.variance = 0;
18
+ this.deviation = 0;
19
+ this.forecast = 0;
20
+ }
21
+ alpha(t, pt) {
22
+ return 1 - (Math.exp(-(t - pt) / this.timespan));
23
+ }
24
+ push(value, time = Date.now()) {
25
+ if (this.previousTime != null) {
26
+ // calculate moving average
27
+ const a = this.alpha(time, this.previousTime);
28
+ const diff = value - this.movingAverage;
29
+ const incr = a * diff;
30
+ this.movingAverage = a * value + (1 - a) * this.movingAverage;
31
+ // calculate variance & deviation
32
+ this.variance = (1 - a) * (this.variance + diff * incr);
33
+ this.deviation = Math.sqrt(this.variance);
34
+ // calculate forecast
35
+ this.forecast = this.movingAverage + a * diff;
36
+ }
37
+ else {
38
+ this.movingAverage = value;
39
+ }
40
+ this.previousTime = time;
41
+ }
42
+ }
43
+ //# sourceMappingURL=moving-average.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moving-average.js","sourceRoot":"","sources":["../../src/moving-average.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IACjB,aAAa,CAAQ;IACrB,QAAQ,CAAQ;IAChB,SAAS,CAAQ;IACjB,QAAQ,CAAQ;IACN,QAAQ,CAAQ;IACzB,YAAY,CAAS;IAE7B,YAAa,QAAgB;QAC3B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,aAAa,GAAG,CAAC,CAAA;QACtB,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAA;QACjB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAA;QAClB,IAAI,CAAC,QAAQ,GAAG,C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"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"
@@ -52,6 +56,10 @@
52
56
  "types": "./dist/src/address-sort.d.ts",
53
57
  "import": "./dist/src/address-sort.js"
54
58
  },
59
+ "./adaptive-timeout": {
60
+ "types": "./dist/src/adaptive-timeout.d.ts",
61
+ "import": "./dist/src/adaptive-timeout.js"
62
+ },
55
63
  "./array-equals": {
56
64
  "types": "./dist/src/array-equals.d.ts",
57
65
  "import": "./dist/src/array-equals.js"
@@ -60,6 +68,14 @@
60
68
  "types": "./dist/src/close-source.d.ts",
61
69
  "import": "./dist/src/close-source.js"
62
70
  },
71
+ "./close": {
72
+ "types": "./dist/src/close.d.ts",
73
+ "import": "./dist/src/close.js"
74
+ },
75
+ "./filters": {
76
+ "types": "./dist/src/filters/index.d.ts",
77
+ "import": "./dist/src/filters/index.js"
78
+ },
63
79
  "./ip-port-to-multiaddr": {
64
80
  "types": "./dist/src/ip-port-to-multiaddr.d.ts",
65
81
  "import": "./dist/src/ip-port-to-multiaddr.js"
@@ -68,6 +84,10 @@
68
84
  "types": "./dist/src/is-promise.d.ts",
69
85
  "import": "./dist/src/is-promise.js"
70
86
  },
87
+ "./moving-average": {
88
+ "types": "./dist/src/moving-average.d.ts",
89
+ "import": "./dist/src/moving-average.js"
90
+ },
71
91
  "./multiaddr/is-loopback": {
72
92
  "types": "./dist/src/multiaddr/is-loopback.d.ts",
73
93
  "import": "./dist/src/multiaddr/is-loopback.js"
@@ -128,23 +148,29 @@
128
148
  },
129
149
  "dependencies": {
130
150
  "@chainsafe/is-ip": "^2.0.2",
131
- "@libp2p/interface": "^1.3.0",
132
- "@libp2p/logger": "^4.0.11",
151
+ "@libp2p/crypto": "4.1.1-4ad63bb79",
152
+ "@libp2p/interface": "1.3.1-4ad63bb79",
153
+ "@libp2p/logger": "4.0.12-4ad63bb79",
133
154
  "@multiformats/multiaddr": "^12.2.1",
134
155
  "@multiformats/multiaddr-matcher": "^1.2.0",
156
+ "@sindresorhus/fnv1a": "^3.1.0",
157
+ "@types/murmurhash3js-revisited": "^3.0.3",
158
+ "any-signal": "^4.1.1",
135
159
  "delay": "^6.0.0",
136
160
  "get-iterator": "^2.0.1",
137
161
  "is-loopback-addr": "^2.0.2",
138
162
  "it-pushable": "^3.2.3",
139
163
  "it-stream-types": "^2.0.1",
164
+ "murmurhash3js-revisited": "^3.0.0",
140
165
  "netmask": "^2.0.2",
141
166
  "p-defer": "^4.0.1",
142
167
  "race-event": "^1.2.0",
143
168
  "race-signal": "^1.0.2",
144
- "uint8arraylist": "^2.4.8"
169
+ "uint8arraylist": "^2.4.8",
170
+ "uint8arrays": "^5.0.3"
145
171
  },
146
172
  "devDependencies": {
147
- "@libp2p/peer-id-factory": "^4.1.0",
173
+ "@libp2p/peer-id-factory": "4.1.1-4ad63bb79",
148
174
  "@types/netmask": "^2.0.5",
149
175
  "aegir": "^42.2.5",
150
176
  "delay": "^6.0.0",
@@ -153,8 +179,7 @@
153
179
  "it-pair": "^2.0.6",
154
180
  "it-pipe": "^3.0.1",
155
181
  "sinon": "^17.0.1",
156
- "sinon-ts": "^2.0.0",
157
- "uint8arrays": "^5.0.3"
182
+ "sinon-ts": "^2.0.0"
158
183
  },
159
184
  "sideEffects": false
160
185
  }
@@ -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
+ }
@@ -0,0 +1,94 @@
1
+ import { setMaxListeners } from '@libp2p/interface'
2
+ import { anySignal, type ClearableSignal } from 'any-signal'
3
+ import { MovingAverage } from './moving-average.js'
4
+ import type { MetricGroup, Metrics } from '@libp2p/interface'
5
+
6
+ export const DEFAULT_TIMEOUT_MULTIPLIER = 1.2
7
+ export const DEFAULT_FAILURE_MULTIPLIER = 2
8
+ export const DEFAULT_MIN_TIMEOUT = 2000
9
+
10
+ export interface AdaptiveTimeoutSignal extends ClearableSignal {
11
+ start: number
12
+ timeout: number
13
+ }
14
+
15
+ export interface AdaptiveTimeoutInit {
16
+ metricName?: string
17
+ metrics?: Metrics
18
+ interval?: number
19
+ initialValue?: number
20
+ timeoutMultiplier?: number
21
+ failureMultiplier?: number
22
+ minTimeout?: number
23
+ }
24
+
25
+ export interface GetTimeoutSignalOptions {
26
+ timeoutFactor?: number
27
+ signal?: AbortSignal
28
+ }
29
+
30
+ export class AdaptiveTimeout {
31
+ private readonly success: MovingAverage
32
+ private readonly failure: MovingAverage
33
+ private readonly next: MovingAverage
34
+ private readonly metric?: MetricGroup
35
+ private readonly timeoutMultiplier: number
36
+ private readonly failureMultiplier: number
37
+ private readonly minTimeout: number
38
+
39
+ constructor (init: AdaptiveTimeoutInit = {}) {
40
+ this.success = new MovingAverage(init.interval ?? 5000)
41
+ this.failure = new MovingAverage(init.interval ?? 5000)
42
+ this.next = new MovingAverage(init.interval ?? 5000)
43
+ this.failureMultiplier = init.failureMultiplier ?? DEFAULT_FAILURE_MULTIPLIER
44
+ this.timeoutMultiplier = init.timeoutMultiplier ?? DEFAULT_TIMEOUT_MULTIPLIER
45
+ this.minTimeout = init.minTimeout ?? DEFAULT_MIN_TIMEOUT
46
+
47
+ if (init.metricName != null) {
48
+ this.metric = init.metrics?.registerMetricGroup(init.metricName)
49
+ }
50
+ }
51
+
52
+ getTimeoutSignal (options: GetTimeoutSignalOptions = {}): AdaptiveTimeoutSignal {
53
+ // calculate timeout for individual peers based on moving average of
54
+ // previous successful requests
55
+ const timeout = Math.max(
56
+ Math.round(this.next.movingAverage * (options.timeoutFactor ?? this.timeoutMultiplier)),
57
+ this.minTimeout
58
+ )
59
+ const sendTimeout = AbortSignal.timeout(timeout)
60
+ const timeoutSignal = anySignal([options.signal, sendTimeout]) as AdaptiveTimeoutSignal
61
+ setMaxListeners(Infinity, timeoutSignal, sendTimeout)
62
+
63
+ timeoutSignal.start = Date.now()
64
+ timeoutSignal.timeout = timeout
65
+
66
+ return timeoutSignal
67
+ }
68
+
69
+ cleanUp (signal: AdaptiveTimeoutSignal): void {
70
+ const time = Date.now() - signal.start
71
+
72
+ if (signal.aborted) {
73
+ this.failure.push(time)
74
+ this.next.push(time * this.failureMultiplier)
75
+ this.metric?.update({
76
+ failureMovingAverage: this.failure.movingAverage,
77
+ failureDeviation: this.failure.deviation,
78
+ failureForecast: this.failure.forecast,
79
+ failureVariance: this.failure.variance,
80
+ failure: time
81
+ })
82
+ } else {
83
+ this.success.push(time)
84
+ this.next.push(time)
85
+ this.metric?.update({
86
+ successMovingAverage: this.success.movingAverage,
87
+ successDeviation: this.success.deviation,
88
+ successForecast: this.success.forecast,
89
+ successVariance: this.success.variance,
90
+ success: time
91
+ })
92
+ }
93
+ }
94
+ }
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
+ }