@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,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,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAyB,MAAM,oBAAoB,CAAA;AAClF,OAAO,EAAE,KAAK,EAAa,MAAM,aAAa,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAUzC,MAAM,OAAO,oBAAoB;IACd,UAAU,CAAQ;IAClB,UAAU,CAAQ;IAClB,eAAe,CAAQ;IACvB,KAAK,CAAQ;IACb,YAAY,CAAgB;IAC5B,IAAI,CAAM;IACV,IAAI,CAAQ;IAE7B,YAAa,IAA8B;QACzC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,CAAC,CAAA;QACtC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,UAAU,CAAA;QAChE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,IAAI,CAAC,CAAA;QAChD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,CAAC,CAAA;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,KAAK,CAAA;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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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/utils",
3
- "version": "5.3.2",
3
+ "version": "5.4.0-1488a7371",
4
4
  "description": "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"
@@ -60,6 +64,14 @@
60
64
  "types": "./dist/src/close-source.d.ts",
61
65
  "import": "./dist/src/close-source.js"
62
66
  },
67
+ "./close": {
68
+ "types": "./dist/src/close.d.ts",
69
+ "import": "./dist/src/close.js"
70
+ },
71
+ "./filters": {
72
+ "types": "./dist/src/filters/index.d.ts",
73
+ "import": "./dist/src/filters/index.js"
74
+ },
63
75
  "./ip-port-to-multiaddr": {
64
76
  "types": "./dist/src/ip-port-to-multiaddr.d.ts",
65
77
  "import": "./dist/src/ip-port-to-multiaddr.js"
@@ -128,23 +140,29 @@
128
140
  },
129
141
  "dependencies": {
130
142
  "@chainsafe/is-ip": "^2.0.2",
131
- "@libp2p/interface": "^1.3.0",
132
- "@libp2p/logger": "^4.0.11",
143
+ "@libp2p/crypto": "4.1.1-1488a7371",
144
+ "@libp2p/interface": "1.3.1-1488a7371",
145
+ "@libp2p/logger": "4.0.12-1488a7371",
133
146
  "@multiformats/multiaddr": "^12.2.1",
134
147
  "@multiformats/multiaddr-matcher": "^1.2.0",
148
+ "@sindresorhus/fnv1a": "^3.1.0",
149
+ "@types/murmurhash3js-revisited": "^3.0.3",
150
+ "any-signal": "^4.1.1",
135
151
  "delay": "^6.0.0",
136
152
  "get-iterator": "^2.0.1",
137
153
  "is-loopback-addr": "^2.0.2",
138
154
  "it-pushable": "^3.2.3",
139
155
  "it-stream-types": "^2.0.1",
156
+ "murmurhash3js-revisited": "^3.0.0",
140
157
  "netmask": "^2.0.2",
141
158
  "p-defer": "^4.0.1",
142
159
  "race-event": "^1.2.0",
143
160
  "race-signal": "^1.0.2",
144
- "uint8arraylist": "^2.4.8"
161
+ "uint8arraylist": "^2.4.8",
162
+ "uint8arrays": "^5.0.3"
145
163
  },
146
164
  "devDependencies": {
147
- "@libp2p/peer-id-factory": "^4.1.0",
165
+ "@libp2p/peer-id-factory": "4.1.1-1488a7371",
148
166
  "@types/netmask": "^2.0.5",
149
167
  "aegir": "^42.2.5",
150
168
  "delay": "^6.0.0",
@@ -153,8 +171,7 @@
153
171
  "it-pair": "^2.0.6",
154
172
  "it-pipe": "^3.0.1",
155
173
  "sinon": "^17.0.1",
156
- "sinon-ts": "^2.0.0",
157
- "uint8arrays": "^5.0.3"
174
+ "sinon-ts": "^2.0.0"
158
175
  },
159
176
  "sideEffects": false
160
177
  }
@@ -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
+ }
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
+ }
@@ -0,0 +1,142 @@
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
+ import type { Filter } from './index.js'
8
+
9
+ const LN2_SQUARED = Math.LN2 * Math.LN2
10
+
11
+ export interface BloomFilterOptions {
12
+ seeds?: number[]
13
+ hashes?: number
14
+ bits?: number
15
+ }
16
+
17
+ export class BloomFilter implements Filter {
18
+ public readonly seeds: number[]
19
+ public readonly bits: number
20
+ public buffer: Uint8Array
21
+
22
+ constructor (options: BloomFilterOptions = {}) {
23
+ if (options.seeds != null) {
24
+ this.seeds = options.seeds
25
+ } else {
26
+ this.seeds = generateSeeds(options.hashes ?? 8)
27
+ }
28
+
29
+ this.bits = options.bits ?? 1024
30
+ this.buffer = alloc(Math.ceil(this.bits / 8))
31
+ }
32
+
33
+ /**
34
+ * Add an item to the filter
35
+ */
36
+ add (item: Uint8Array | string): void {
37
+ if (typeof item === 'string') {
38
+ item = uint8ArrayFromString(item)
39
+ }
40
+
41
+ for (let i = 0; i < this.seeds.length; i++) {
42
+ const hash = mur.x86.hash32(item, this.seeds[i])
43
+ const bit = hash % this.bits
44
+
45
+ this.setbit(bit)
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Test if the filter has an item. If it returns false it definitely does not
51
+ * have the item. If it returns true, it probably has the item but there's
52
+ * an `errorRate` chance it doesn't.
53
+ */
54
+ has (item: Uint8Array | string): boolean {
55
+ if (typeof item === 'string') {
56
+ item = uint8ArrayFromString(item)
57
+ }
58
+
59
+ for (let i = 0; i < this.seeds.length; i++) {
60
+ const hash = mur.x86.hash32(item, this.seeds[i])
61
+ const bit = hash % this.bits
62
+
63
+ const isSet = this.getbit(bit)
64
+
65
+ if (!isSet) {
66
+ return false
67
+ }
68
+ }
69
+
70
+ return true
71
+ }
72
+
73
+ /**
74
+ * Reset the filter
75
+ */
76
+ clear (): void {
77
+ this.buffer.fill(0)
78
+ }
79
+
80
+ setbit (bit: number): void {
81
+ let pos = 0
82
+ let shift = bit
83
+ while (shift > 7) {
84
+ pos++
85
+ shift -= 8
86
+ }
87
+
88
+ let bitfield = this.buffer[pos]
89
+ bitfield |= (0x1 << shift)
90
+ this.buffer[pos] = bitfield
91
+ }
92
+
93
+ getbit (bit: number): boolean {
94
+ let pos = 0
95
+ let shift = bit
96
+ while (shift > 7) {
97
+ pos++
98
+ shift -= 8
99
+ }
100
+
101
+ const bitfield = this.buffer[pos]
102
+ return (bitfield & (0x1 << shift)) !== 0
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
108
+ * specified item count and error rate.
109
+ */
110
+ export function createBloomFilter (itemcount: number, errorRate: number = 0.005): Filter {
111
+ const opts = optimize(itemcount, errorRate)
112
+ return new BloomFilter(opts)
113
+ }
114
+
115
+ function optimize (itemCount: number, errorRate: number = 0.005): { bits: number, hashes: number } {
116
+ const bits = Math.round(-1 * itemCount * Math.log(errorRate) / LN2_SQUARED)
117
+ const hashes = Math.round((bits / itemCount) * Math.LN2)
118
+
119
+ return { bits, hashes }
120
+ }
121
+
122
+ function generateSeeds (count: number): number[] {
123
+ let buf: Uint8ArrayList
124
+ let j: number
125
+ const seeds = []
126
+
127
+ for (let i = 0; i < count; i++) {
128
+ buf = new Uint8ArrayList(randomBytes(4))
129
+ seeds[i] = buf.getUint32(0, true)
130
+
131
+ // Make sure we don't end up with two identical seeds,
132
+ // which is unlikely but possible.
133
+ for (j = 0; j < i; j++) {
134
+ if (seeds[i] === seeds[j]) {
135
+ i--
136
+ break
137
+ }
138
+ }
139
+ }
140
+
141
+ return seeds
142
+ }
@@ -0,0 +1,64 @@
1
+ import { Fingerprint } from './fingerprint.js'
2
+ import { getRandomInt } from './utils.js'
3
+
4
+ export class Bucket {
5
+ private readonly contents: Array<Fingerprint | null>
6
+
7
+ constructor (size: number) {
8
+ this.contents = new Array(size).fill(null)
9
+ }
10
+
11
+ has (fingerprint: Fingerprint): boolean {
12
+ if (!(fingerprint instanceof Fingerprint)) {
13
+ throw new TypeError('Invalid Fingerprint')
14
+ }
15
+
16
+ return this.contents.some((fp) => {
17
+ return fingerprint.equals(fp)
18
+ })
19
+ }
20
+
21
+ add (fingerprint: Fingerprint): boolean {
22
+ if (!(fingerprint instanceof Fingerprint)) {
23
+ throw new TypeError('Invalid Fingerprint')
24
+ }
25
+
26
+ for (let i = 0; i < this.contents.length; i++) {
27
+ if (this.contents[i] == null) {
28
+ this.contents[i] = fingerprint
29
+ return true
30
+ }
31
+ }
32
+
33
+ return true
34
+ }
35
+
36
+ swap (fingerprint: Fingerprint): Fingerprint | null {
37
+ if (!(fingerprint instanceof Fingerprint)) {
38
+ throw new TypeError('Invalid Fingerprint')
39
+ }
40
+
41
+ const i = getRandomInt(0, this.contents.length - 1)
42
+ const current = this.contents[i]
43
+ this.contents[i] = fingerprint
44
+
45
+ return current
46
+ }
47
+
48
+ remove (fingerprint: Fingerprint): boolean {
49
+ if (!(fingerprint instanceof Fingerprint)) {
50
+ throw new TypeError('Invalid Fingerprint')
51
+ }
52
+
53
+ const found = this.contents.findIndex((fp) => {
54
+ return fingerprint.equals(fp)
55
+ })
56
+
57
+ if (found > -1) {
58
+ this.contents[found] = null
59
+ return true
60
+ } else {
61
+ return false
62
+ }
63
+ }
64
+ }