@libp2p/utils 5.3.2 → 5.4.0-1488a7371

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 (52) 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/close.d.ts +21 -0
  6. package/dist/src/close.d.ts.map +1 -0
  7. package/dist/src/close.js +49 -0
  8. package/dist/src/close.js.map +1 -0
  9. package/dist/src/filters/bloom-filter.d.ts +34 -0
  10. package/dist/src/filters/bloom-filter.d.ts.map +1 -0
  11. package/dist/src/filters/bloom-filter.js +113 -0
  12. package/dist/src/filters/bloom-filter.js.map +1 -0
  13. package/dist/src/filters/bucket.d.ts +10 -0
  14. package/dist/src/filters/bucket.d.ts.map +1 -0
  15. package/dist/src/filters/bucket.js +53 -0
  16. package/dist/src/filters/bucket.js.map +1 -0
  17. package/dist/src/filters/cuckoo-filter.d.ts +41 -0
  18. package/dist/src/filters/cuckoo-filter.d.ts.map +1 -0
  19. package/dist/src/filters/cuckoo-filter.js +134 -0
  20. package/dist/src/filters/cuckoo-filter.js.map +1 -0
  21. package/dist/src/filters/fingerprint.d.ts +11 -0
  22. package/dist/src/filters/fingerprint.d.ts.map +1 -0
  23. package/dist/src/filters/fingerprint.js +34 -0
  24. package/dist/src/filters/fingerprint.js.map +1 -0
  25. package/dist/src/filters/hashes.d.ts +8 -0
  26. package/dist/src/filters/hashes.d.ts.map +1 -0
  27. package/dist/src/filters/hashes.js +29 -0
  28. package/dist/src/filters/hashes.js.map +1 -0
  29. package/dist/src/filters/index.d.ts +9 -0
  30. package/dist/src/filters/index.d.ts.map +1 -0
  31. package/dist/src/filters/index.js +4 -0
  32. package/dist/src/filters/index.js.map +1 -0
  33. package/dist/src/filters/scalable-cuckoo-filter.d.ts +24 -0
  34. package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -0
  35. package/dist/src/filters/scalable-cuckoo-filter.js +87 -0
  36. package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -0
  37. package/dist/src/filters/utils.d.ts +2 -0
  38. package/dist/src/filters/utils.d.ts.map +1 -0
  39. package/dist/src/filters/utils.js +4 -0
  40. package/dist/src/filters/utils.js.map +1 -0
  41. package/package.json +24 -7
  42. package/src/abort-options.ts +20 -0
  43. package/src/close.ts +65 -0
  44. package/src/filters/bloom-filter.ts +142 -0
  45. package/src/filters/bucket.ts +64 -0
  46. package/src/filters/cuckoo-filter.ts +197 -0
  47. package/src/filters/fingerprint.ts +44 -0
  48. package/src/filters/hashes.ts +38 -0
  49. package/src/filters/index.ts +9 -0
  50. package/src/filters/scalable-cuckoo-filter.ts +111 -0
  51. package/src/filters/utils.ts +3 -0
  52. package/dist/typedoc-urls.json +0 -76
@@ -0,0 +1,7 @@
1
+ import type { AbortOptions } from '@libp2p/interface';
2
+ import type { ClearableSignal } from 'any-signal';
3
+ export declare function createTimeoutOptions(timeout: number): AbortOptions;
4
+ export declare function createTimeoutOptions(timeout: number, ...existingSignals: AbortSignal[]): {
5
+ signal: ClearableSignal;
6
+ };
7
+ //# sourceMappingURL=abort-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abort-options.d.ts","sourceRoot":"","sources":["../../src/abort-options.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjD,wBAAgB,oBAAoB,CAAE,OAAO,EAAE,MAAM,GAAG,YAAY,CAAA;AACpE,wBAAgB,oBAAoB,CAAE,OAAO,EAAE,MAAM,EAAE,GAAG,eAAe,EAAE,WAAW,EAAE,GAAG;IAAE,MAAM,EAAE,eAAe,CAAA;CAAE,CAAA"}
@@ -0,0 +1,14 @@
1
+ import { setMaxListeners } from '@libp2p/interface';
2
+ import { anySignal } from 'any-signal';
3
+ export function createTimeoutOptions(timeout, ...existingSignals) {
4
+ let signal = AbortSignal.timeout(timeout);
5
+ setMaxListeners(Infinity, signal);
6
+ if (existingSignals.length > 0) {
7
+ signal = anySignal([signal, ...existingSignals]);
8
+ setMaxListeners(Infinity, signal);
9
+ }
10
+ return {
11
+ signal
12
+ };
13
+ }
14
+ //# sourceMappingURL=abort-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abort-options.js","sourceRoot":"","sources":["../../src/abort-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAMtC,MAAM,UAAU,oBAAoB,CAAE,OAAe,EAAE,GAAG,eAA8B;IACtF,IAAI,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACzC,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IAEjC,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,GAAG,SAAS,CAAC,CAAC,MAAM,EAAE,GAAG,eAAe,CAAC,CAAC,CAAA;QAChD,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IACnC,CAAC;IAED,OAAO;QACL,MAAM;KACP,CAAA;AACH,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { Connection, Stream, AbortOptions } from '@libp2p/interface';
2
+ /**
3
+ * Close the passed stream, falling back to aborting the stream if closing
4
+ * cleanly fails.
5
+ */
6
+ export declare function safelyCloseStream(stream?: Stream, options?: AbortOptions): Promise<void>;
7
+ export interface SafelyCloseConnectionOptions extends AbortOptions {
8
+ /**
9
+ * Only close the stream if it either has no protocol streams open or only
10
+ * ones in this list.
11
+ *
12
+ * @default ['/ipfs/id/1.0.0']
13
+ */
14
+ closableProtocols?: string[];
15
+ }
16
+ /**
17
+ * Close the passed connection if it has no streams, or only closable protocol
18
+ * streams, falling back to aborting the connection if closing it cleanly fails.
19
+ */
20
+ export declare function safelyCloseConnectionIfUnused(connection?: Connection, options?: SafelyCloseConnectionOptions): Promise<void>;
21
+ //# sourceMappingURL=close.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close.d.ts","sourceRoot":"","sources":["../../src/close.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEzE;;;GAGG;AACH,wBAAsB,iBAAiB,CAAE,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAM/F;AAuBD,MAAM,WAAW,4BAA6B,SAAQ,YAAY;IAChE;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAA;CAC7B;AAED;;;GAGG;AACH,wBAAsB,6BAA6B,CAAE,UAAU,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,4BAA4B,GAAG,OAAO,CAAC,IAAI,CAAC,CAenI"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Close the passed stream, falling back to aborting the stream if closing
3
+ * cleanly fails.
4
+ */
5
+ export async function safelyCloseStream(stream, options) {
6
+ try {
7
+ await stream?.close(options);
8
+ }
9
+ catch (err) {
10
+ stream?.abort(err);
11
+ }
12
+ }
13
+ /**
14
+ * These are speculative protocols that are run automatically on connection open
15
+ * so are usually not the reason the connection was opened.
16
+ *
17
+ * Consequently when requested it should be safe to close connections that only
18
+ * have these protocol streams open.
19
+ */
20
+ const DEFAULT_CLOSABLE_PROTOCOLS = [
21
+ // identify
22
+ '/ipfs/id/1.0.0',
23
+ // identify-push
24
+ '/ipfs/id/push/1.0.0',
25
+ // autonat
26
+ '/libp2p/autonat/1.0.0',
27
+ // dcutr
28
+ '/libp2p/dcutr'
29
+ ];
30
+ /**
31
+ * Close the passed connection if it has no streams, or only closable protocol
32
+ * streams, falling back to aborting the connection if closing it cleanly fails.
33
+ */
34
+ export async function safelyCloseConnectionIfUnused(connection, options) {
35
+ const streamProtocols = connection?.streams?.map(stream => stream.protocol) ?? [];
36
+ const closableProtocols = options?.closableProtocols ?? DEFAULT_CLOSABLE_PROTOCOLS;
37
+ // if the connection has protocols not in the closable protocols list, do not
38
+ // close the connection
39
+ if (streamProtocols.filter(proto => proto != null && !closableProtocols.includes(proto)).length > 0) {
40
+ return;
41
+ }
42
+ try {
43
+ await connection?.close(options);
44
+ }
45
+ catch (err) {
46
+ connection?.abort(err);
47
+ }
48
+ }
49
+ //# sourceMappingURL=close.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close.js","sourceRoot":"","sources":["../../src/close.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAE,MAAe,EAAE,OAAsB;IAC9E,IAAI,CAAC;QACH,MAAM,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IAC9B,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;IACpB,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,0BAA0B,GAAG;IACjC,WAAW;IACX,gBAAgB;IAEhB,gBAAgB;IAChB,qBAAqB;IAErB,UAAU;IACV,uBAAuB;IAEvB,QAAQ;IACR,eAAe;CAChB,CAAA;AAYD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAE,UAAuB,EAAE,OAAsC;IAClH,MAAM,eAAe,GAAG,UAAU,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;IACjF,MAAM,iBAAiB,GAAG,OAAO,EAAE,iBAAiB,IAAI,0BAA0B,CAAA;IAElF,6EAA6E;IAC7E,uBAAuB;IACvB,IAAI,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpG,OAAM;IACR,CAAC;IAED,IAAI,CAAC;QACH,MAAM,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;AACH,CAAC"}
@@ -0,0 +1,34 @@
1
+ import type { Filter } from './index.js';
2
+ export interface BloomFilterOptions {
3
+ seeds?: number[];
4
+ hashes?: number;
5
+ bits?: number;
6
+ }
7
+ export declare class BloomFilter implements Filter {
8
+ readonly seeds: number[];
9
+ readonly bits: number;
10
+ buffer: Uint8Array;
11
+ constructor(options?: BloomFilterOptions);
12
+ /**
13
+ * Add an item to the filter
14
+ */
15
+ add(item: Uint8Array | string): void;
16
+ /**
17
+ * Test if the filter has an item. If it returns false it definitely does not
18
+ * have the item. If it returns true, it probably has the item but there's
19
+ * an `errorRate` chance it doesn't.
20
+ */
21
+ has(item: Uint8Array | string): boolean;
22
+ /**
23
+ * Reset the filter
24
+ */
25
+ clear(): void;
26
+ setbit(bit: number): void;
27
+ getbit(bit: number): boolean;
28
+ }
29
+ /**
30
+ * Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
31
+ * specified item count and error rate.
32
+ */
33
+ export declare function createBloomFilter(itemcount: number, errorRate?: number): Filter;
34
+ //# sourceMappingURL=bloom-filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bloom-filter.d.ts","sourceRoot":"","sources":["../../../src/filters/bloom-filter.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAIxC,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,qBAAa,WAAY,YAAW,MAAM;IACxC,SAAgB,KAAK,EAAE,MAAM,EAAE,CAAA;IAC/B,SAAgB,IAAI,EAAE,MAAM,CAAA;IACrB,MAAM,EAAE,UAAU,CAAA;gBAEZ,OAAO,GAAE,kBAAuB;IAW7C;;OAEG;IACH,GAAG,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI;IAarC;;;;OAIG;IACH,GAAG,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IAmBxC;;OAEG;IACH,KAAK,IAAK,IAAI;IAId,MAAM,CAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAa1B,MAAM,CAAE,GAAG,EAAE,MAAM,GAAG,OAAO;CAW9B;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAE,SAAS,EAAE,MAAM,EAAE,SAAS,GAAE,MAAc,GAAG,MAAM,CAGvF"}
@@ -0,0 +1,113 @@
1
+ // ported from xxbloom - https://github.com/ceejbot/xxbloom/blob/master/LICENSE
2
+ import { randomBytes } from '@libp2p/crypto';
3
+ import mur from 'murmurhash3js-revisited';
4
+ import { Uint8ArrayList } from 'uint8arraylist';
5
+ import { alloc } from 'uint8arrays/alloc';
6
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
7
+ const LN2_SQUARED = Math.LN2 * Math.LN2;
8
+ export class BloomFilter {
9
+ seeds;
10
+ bits;
11
+ buffer;
12
+ constructor(options = {}) {
13
+ if (options.seeds != null) {
14
+ this.seeds = options.seeds;
15
+ }
16
+ else {
17
+ this.seeds = generateSeeds(options.hashes ?? 8);
18
+ }
19
+ this.bits = options.bits ?? 1024;
20
+ this.buffer = alloc(Math.ceil(this.bits / 8));
21
+ }
22
+ /**
23
+ * Add an item to the filter
24
+ */
25
+ add(item) {
26
+ if (typeof item === 'string') {
27
+ item = uint8ArrayFromString(item);
28
+ }
29
+ for (let i = 0; i < this.seeds.length; i++) {
30
+ const hash = mur.x86.hash32(item, this.seeds[i]);
31
+ const bit = hash % this.bits;
32
+ this.setbit(bit);
33
+ }
34
+ }
35
+ /**
36
+ * Test if the filter has an item. If it returns false it definitely does not
37
+ * have the item. If it returns true, it probably has the item but there's
38
+ * an `errorRate` chance it doesn't.
39
+ */
40
+ has(item) {
41
+ if (typeof item === 'string') {
42
+ item = uint8ArrayFromString(item);
43
+ }
44
+ for (let i = 0; i < this.seeds.length; i++) {
45
+ const hash = mur.x86.hash32(item, this.seeds[i]);
46
+ const bit = hash % this.bits;
47
+ const isSet = this.getbit(bit);
48
+ if (!isSet) {
49
+ return false;
50
+ }
51
+ }
52
+ return true;
53
+ }
54
+ /**
55
+ * Reset the filter
56
+ */
57
+ clear() {
58
+ this.buffer.fill(0);
59
+ }
60
+ setbit(bit) {
61
+ let pos = 0;
62
+ let shift = bit;
63
+ while (shift > 7) {
64
+ pos++;
65
+ shift -= 8;
66
+ }
67
+ let bitfield = this.buffer[pos];
68
+ bitfield |= (0x1 << shift);
69
+ this.buffer[pos] = bitfield;
70
+ }
71
+ getbit(bit) {
72
+ let pos = 0;
73
+ let shift = bit;
74
+ while (shift > 7) {
75
+ pos++;
76
+ shift -= 8;
77
+ }
78
+ const bitfield = this.buffer[pos];
79
+ return (bitfield & (0x1 << shift)) !== 0;
80
+ }
81
+ }
82
+ /**
83
+ * Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
84
+ * specified item count and error rate.
85
+ */
86
+ export function createBloomFilter(itemcount, errorRate = 0.005) {
87
+ const opts = optimize(itemcount, errorRate);
88
+ return new BloomFilter(opts);
89
+ }
90
+ function optimize(itemCount, errorRate = 0.005) {
91
+ const bits = Math.round(-1 * itemCount * Math.log(errorRate) / LN2_SQUARED);
92
+ const hashes = Math.round((bits / itemCount) * Math.LN2);
93
+ return { bits, hashes };
94
+ }
95
+ function generateSeeds(count) {
96
+ let buf;
97
+ let j;
98
+ const seeds = [];
99
+ for (let i = 0; i < count; i++) {
100
+ buf = new Uint8ArrayList(randomBytes(4));
101
+ seeds[i] = buf.getUint32(0, true);
102
+ // Make sure we don't end up with two identical seeds,
103
+ // which is unlikely but possible.
104
+ for (j = 0; j < i; j++) {
105
+ if (seeds[i] === seeds[j]) {
106
+ i--;
107
+ break;
108
+ }
109
+ }
110
+ }
111
+ return seeds;
112
+ }
113
+ //# sourceMappingURL=bloom-filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bloom-filter.js","sourceRoot":"","sources":["../../../src/filters/bloom-filter.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,GAAG,MAAM,yBAAyB,CAAA;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAG5E,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAA;AAQvC,MAAM,OAAO,WAAW;IACN,KAAK,CAAU;IACf,IAAI,CAAQ;IACrB,MAAM,CAAY;IAEzB,YAAa,UAA8B,EAAE;QAC3C,IAAI,OAAO,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;YAC1B,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC5B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,CAAC,CAAA;QACjD,CAAC;QAED,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,CAAA;QAChC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAA;IAC/C,C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AAI,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAA;IAExD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAA;AACzB,CAAC;AAED,SAAS,aAAa,CAAE,KAAa;IACnC,IAAI,GAAmB,CAAA;IACvB,IAAI,CAAS,CAAA;IACb,MAAM,KAAK,GAAG,EAAE,CAAA;IAEhB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;QAC/B,GAAG,GAAG,IAAI,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAA;QACxC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;QAEjC,sDAAsD;QACtD,kCAAkC;QAClC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACvB,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC1B,CAAC,EAAE,CAAA;gBACH,MAAK;YACP,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { Fingerprint } from './fingerprint.js';
2
+ export declare class Bucket {
3
+ private readonly contents;
4
+ constructor(size: number);
5
+ has(fingerprint: Fingerprint): boolean;
6
+ add(fingerprint: Fingerprint): boolean;
7
+ swap(fingerprint: Fingerprint): Fingerprint | null;
8
+ remove(fingerprint: Fingerprint): boolean;
9
+ }
10
+ //# sourceMappingURL=bucket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bucket.d.ts","sourceRoot":"","sources":["../../../src/filters/bucket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;gBAEvC,IAAI,EAAE,MAAM;IAIzB,GAAG,CAAE,WAAW,EAAE,WAAW,GAAG,OAAO;IAUvC,GAAG,CAAE,WAAW,EAAE,WAAW,GAAG,OAAO;IAevC,IAAI,CAAE,WAAW,EAAE,WAAW,GAAG,WAAW,GAAG,IAAI;IAYnD,MAAM,CAAE,WAAW,EAAE,WAAW,GAAG,OAAO;CAgB3C"}
@@ -0,0 +1,53 @@
1
+ import { Fingerprint } from './fingerprint.js';
2
+ import { getRandomInt } from './utils.js';
3
+ export class Bucket {
4
+ contents;
5
+ constructor(size) {
6
+ this.contents = new Array(size).fill(null);
7
+ }
8
+ has(fingerprint) {
9
+ if (!(fingerprint instanceof Fingerprint)) {
10
+ throw new TypeError('Invalid Fingerprint');
11
+ }
12
+ return this.contents.some((fp) => {
13
+ return fingerprint.equals(fp);
14
+ });
15
+ }
16
+ add(fingerprint) {
17
+ if (!(fingerprint instanceof Fingerprint)) {
18
+ throw new TypeError('Invalid Fingerprint');
19
+ }
20
+ for (let i = 0; i < this.contents.length; i++) {
21
+ if (this.contents[i] == null) {
22
+ this.contents[i] = fingerprint;
23
+ return true;
24
+ }
25
+ }
26
+ return true;
27
+ }
28
+ swap(fingerprint) {
29
+ if (!(fingerprint instanceof Fingerprint)) {
30
+ throw new TypeError('Invalid Fingerprint');
31
+ }
32
+ const i = getRandomInt(0, this.contents.length - 1);
33
+ const current = this.contents[i];
34
+ this.contents[i] = fingerprint;
35
+ return current;
36
+ }
37
+ remove(fingerprint) {
38
+ if (!(fingerprint instanceof Fingerprint)) {
39
+ throw new TypeError('Invalid Fingerprint');
40
+ }
41
+ const found = this.contents.findIndex((fp) => {
42
+ return fingerprint.equals(fp);
43
+ });
44
+ if (found > -1) {
45
+ this.contents[found] = null;
46
+ return true;
47
+ }
48
+ else {
49
+ return false;
50
+ }
51
+ }
52
+ }
53
+ //# sourceMappingURL=bucket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bucket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AAS,CAAC,qBAAqB,CAAC,CAAA;QAC5C,CAAC;QAED,MAAM,CAAC,GAAG,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;QACnD,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;QAChC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,WAAW,CAAA;QAE9B,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,MAAM,CAAE,WAAwB;QAC9B,IAAI,CAAC,CAAC,WAAW,YAAY,WAAW,CAAC,EAAE,CAAC;YAC1C,MAAM,IAAI,SAAS,CAAC,qBAAqB,CAAC,CAAA;QAC5C,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE;YAC3C,OAAO,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QAC/B,CAAC,CAAC,CAAA;QAEF,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;YACf,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,CAAA;YAC3B,OAAO,IAAI,CAAA;QACb,CAAC;aAAM,CAAC;YACN,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,41 @@
1
+ import { type Hash } from './hashes.js';
2
+ import type { Filter } from './index.js';
3
+ export interface CuckooFilterInit {
4
+ /**
5
+ * How many items the filter is expected to contain
6
+ */
7
+ filterSize: number;
8
+ /**
9
+ * How many items to put in each bucket
10
+ */
11
+ bucketSize?: number;
12
+ /**
13
+ * How many bytes the fingerprint is expected to be
14
+ */
15
+ fingerprintSize?: number;
16
+ /**
17
+ * A non-cryptographic hash implementation
18
+ */
19
+ hash?: Hash;
20
+ /**
21
+ * A number used to seed the hash
22
+ */
23
+ seed?: number;
24
+ }
25
+ export declare class CuckooFilter implements Filter {
26
+ private readonly bucketSize;
27
+ private readonly filterSize;
28
+ private readonly fingerprintSize;
29
+ private readonly buckets;
30
+ count: number;
31
+ private readonly hash;
32
+ private readonly seed;
33
+ constructor(init: CuckooFilterInit);
34
+ add(item: Uint8Array | string): boolean;
35
+ has(item: Uint8Array | string): boolean;
36
+ remove(item: Uint8Array | string): boolean;
37
+ get reliable(): boolean;
38
+ }
39
+ export declare function optimize(maxItems: number, errorRate?: number): CuckooFilterInit;
40
+ export declare function createCuckooFilter(maxItems: number, errorRate?: number): Filter;
41
+ //# sourceMappingURL=cuckoo-filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cuckoo-filter.d.ts","sourceRoot":"","sources":["../../../src/filters/cuckoo-filter.ts"],"names":[],"mappings":"AAGA,OAAO,EAAS,KAAK,IAAI,EAAE,MAAM,aAAa,CAAA;AAE9C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAIxC,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,UAAU,EAAE,MAAM,CAAA;IAElB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IAExB;;OAEG;IACH,IAAI,CAAC,EAAE,IAAI,CAAA;IAEX;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,qBAAa,YAAa,YAAW,MAAM;IACzC,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,OAAO,CAAU;IAC3B,KAAK,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAM;IAC3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;gBAEhB,IAAI,EAAE,gBAAgB;IAUnC,GAAG,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IAsDxC,GAAG,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IAkBxC,MAAM,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO;IAwB3C,IAAI,QAAQ,IAAK,OAAO,CAEvB;CACF;AAsBD,wBAAgB,QAAQ,CAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,GAAE,MAAc,GAAG,gBAAgB,CAevF;AAED,wBAAgB,kBAAkB,CAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,GAAE,MAAc,GAAG,MAAM,CAGvF"}
@@ -0,0 +1,134 @@
1
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
2
+ import { Bucket } from './bucket.js';
3
+ import { Fingerprint, MAX_FINGERPRINT_SIZE } from './fingerprint.js';
4
+ import { fnv1a } from './hashes.js';
5
+ import { getRandomInt } from './utils.js';
6
+ const maxCuckooCount = 500;
7
+ export class CuckooFilter {
8
+ bucketSize;
9
+ filterSize;
10
+ fingerprintSize;
11
+ buckets;
12
+ count;
13
+ hash;
14
+ seed;
15
+ constructor(init) {
16
+ this.filterSize = init.filterSize;
17
+ this.bucketSize = init.bucketSize ?? 4;
18
+ this.fingerprintSize = init.fingerprintSize ?? 2;
19
+ this.count = 0;
20
+ this.buckets = [];
21
+ this.hash = init.hash ?? fnv1a;
22
+ this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10));
23
+ }
24
+ add(item) {
25
+ if (typeof item === 'string') {
26
+ item = uint8ArrayFromString(item);
27
+ }
28
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize);
29
+ const j = this.hash.hash(item, this.seed) % this.filterSize;
30
+ const k = (j ^ fingerprint.hash()) % this.filterSize;
31
+ if (this.buckets[j] == null) {
32
+ this.buckets[j] = new Bucket(this.bucketSize);
33
+ }
34
+ if (this.buckets[k] == null) {
35
+ this.buckets[k] = new Bucket(this.bucketSize);
36
+ }
37
+ if (this.buckets[j].add(fingerprint) || this.buckets[k].add(fingerprint)) {
38
+ this.count++;
39
+ return true;
40
+ }
41
+ const rand = [j, k];
42
+ let i = rand[getRandomInt(0, rand.length - 1)];
43
+ if (this.buckets[i] == null) {
44
+ this.buckets[i] = new Bucket(this.bucketSize);
45
+ }
46
+ for (let n = 0; n < maxCuckooCount; n++) {
47
+ const swapped = this.buckets[i].swap(fingerprint);
48
+ if (swapped == null) {
49
+ continue;
50
+ }
51
+ i = (i ^ swapped.hash()) % this.filterSize;
52
+ if (this.buckets[i] == null) {
53
+ this.buckets[i] = new Bucket(this.bucketSize);
54
+ }
55
+ if (this.buckets[i].add(swapped)) {
56
+ this.count++;
57
+ return true;
58
+ }
59
+ else {
60
+ continue;
61
+ }
62
+ }
63
+ return false;
64
+ }
65
+ has(item) {
66
+ if (typeof item === 'string') {
67
+ item = uint8ArrayFromString(item);
68
+ }
69
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize);
70
+ const j = this.hash.hash(item, this.seed) % this.filterSize;
71
+ const inJ = this.buckets[j]?.has(fingerprint) ?? false;
72
+ if (inJ) {
73
+ return inJ;
74
+ }
75
+ const k = (j ^ fingerprint.hash()) % this.filterSize;
76
+ return this.buckets[k]?.has(fingerprint) ?? false;
77
+ }
78
+ remove(item) {
79
+ if (typeof item === 'string') {
80
+ item = uint8ArrayFromString(item);
81
+ }
82
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize);
83
+ const j = this.hash.hash(item, this.seed) % this.filterSize;
84
+ const inJ = this.buckets[j]?.remove(fingerprint) ?? false;
85
+ if (inJ) {
86
+ this.count--;
87
+ return inJ;
88
+ }
89
+ const k = (j ^ fingerprint.hash()) % this.filterSize;
90
+ const inK = this.buckets[k]?.remove(fingerprint) ?? false;
91
+ if (inK) {
92
+ this.count--;
93
+ }
94
+ return inK;
95
+ }
96
+ get reliable() {
97
+ return Math.floor(100 * (this.count / this.filterSize)) <= 95;
98
+ }
99
+ }
100
+ // max load constants, defined in the cuckoo paper
101
+ const MAX_LOAD = {
102
+ 1: 0.5,
103
+ 2: 0.84,
104
+ 4: 0.95,
105
+ 8: 0.98
106
+ };
107
+ function calculateBucketSize(errorRate = 0.001) {
108
+ if (errorRate > 0.002) {
109
+ return 2;
110
+ }
111
+ if (errorRate > 0.00001) {
112
+ return 4;
113
+ }
114
+ return 8;
115
+ }
116
+ export function optimize(maxItems, errorRate = 0.001) {
117
+ // https://www.eecs.harvard.edu/~michaelm/postscripts/cuckoo-conext2014.pdf
118
+ // Section 5.1 Optimal Bucket Size
119
+ const bucketSize = calculateBucketSize(errorRate);
120
+ const load = MAX_LOAD[bucketSize];
121
+ // https://stackoverflow.com/questions/57555236/how-to-size-a-cuckoo-filter/57617208#57617208
122
+ const filterSize = Math.round(maxItems / load);
123
+ const fingerprintSize = Math.min(Math.ceil(Math.log(filterSize / bucketSize)) + 2, MAX_FINGERPRINT_SIZE);
124
+ return {
125
+ filterSize,
126
+ bucketSize,
127
+ fingerprintSize
128
+ };
129
+ }
130
+ export function createCuckooFilter(maxItems, errorRate = 0.005) {
131
+ const opts = optimize(maxItems, errorRate);
132
+ return new CuckooFilter(opts);
133
+ }
134
+ //# sourceMappingURL=cuckoo-filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cuckoo-filter.js","sourceRoot":"","sources":["../../../src/filters/cuckoo-filter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACpE,OAAO,EAAE,KAAK,EAAa,MAAM,aAAa,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAGzC,MAAM,cAAc,GAAG,GAAG,CAAA;AA6B1B,MAAM,OAAO,YAAY;IACN,UAAU,CAAQ;IACl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AAE,CAAC;QACtB,OAAO,CAAC,CAAA;IACV,CAAC;IAED,IAAI,SAAS,GAAG,OAAO,EAAE,CAAC;QACxB,OAAO,CAAC,CAAA;IACV,CAAC;IAED,OAAO,CAAC,CAAA;AACV,CAAC;AAED,MAAM,UAAU,QAAQ,CAAE,QAAgB,EAAE,YAAoB,KAAK;IACnE,2EAA2E;IAC3E,kCAAkC;IAClC,MAAM,UAAU,GAAG,mBAAmB,CAAC,SAAS,CAAC,CAAA;IACjD,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAA;IAEjC,6FAA6F;IAC7F,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAA;IAC9C,MAAM,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,GAAG,UAAU,CAAC,CAAC,GAAG,CAAC,EAAE,oBAAoB,CAAC,CAAA;IAExG,OAAO;QACL,UAAU;QACV,UAAU;QACV,eAAe;KAChB,CAAA;AACH,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAE,QAAgB,EAAE,YAAoB,KAAK;IAC7E,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;IAC1C,OAAO,IAAI,YAAY,CAAC,IAAI,CAAC,CAAA;AAC/B,CAAC"}
@@ -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":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A;QACd,CAAC;QAED,OAAO,gBAAgB,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,EA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