@libp2p/utils 5.3.2-e1923b0a7 → 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.
- package/dist/src/abort-options.d.ts +7 -0
- package/dist/src/abort-options.d.ts.map +1 -0
- package/dist/src/abort-options.js +14 -0
- package/dist/src/abort-options.js.map +1 -0
- package/dist/src/close.d.ts +21 -0
- package/dist/src/close.d.ts.map +1 -0
- package/dist/src/close.js +49 -0
- package/dist/src/close.js.map +1 -0
- package/dist/src/{bloom-filter.d.ts → filters/bloom-filter.d.ts} +7 -6
- package/dist/src/filters/bloom-filter.d.ts.map +1 -0
- package/dist/src/{bloom-filter.js → filters/bloom-filter.js} +11 -11
- package/dist/src/filters/bloom-filter.js.map +1 -0
- package/dist/src/filters/bucket.d.ts +10 -0
- package/dist/src/filters/bucket.d.ts.map +1 -0
- package/dist/src/filters/bucket.js +53 -0
- package/dist/src/filters/bucket.js.map +1 -0
- package/dist/src/filters/cuckoo-filter.d.ts +41 -0
- package/dist/src/filters/cuckoo-filter.d.ts.map +1 -0
- package/dist/src/filters/cuckoo-filter.js +134 -0
- package/dist/src/filters/cuckoo-filter.js.map +1 -0
- package/dist/src/filters/fingerprint.d.ts +11 -0
- package/dist/src/filters/fingerprint.d.ts.map +1 -0
- package/dist/src/filters/fingerprint.js +34 -0
- package/dist/src/filters/fingerprint.js.map +1 -0
- package/dist/src/filters/hashes.d.ts +8 -0
- package/dist/src/filters/hashes.d.ts.map +1 -0
- package/dist/src/filters/hashes.js +29 -0
- package/dist/src/filters/hashes.js.map +1 -0
- package/dist/src/filters/index.d.ts +9 -0
- package/dist/src/filters/index.d.ts.map +1 -0
- package/dist/src/filters/index.js +4 -0
- package/dist/src/filters/index.js.map +1 -0
- package/dist/src/filters/scalable-cuckoo-filter.d.ts +24 -0
- package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -0
- package/dist/src/filters/scalable-cuckoo-filter.js +87 -0
- package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -0
- package/dist/src/filters/utils.d.ts +2 -0
- package/dist/src/filters/utils.d.ts.map +1 -0
- package/dist/src/filters/utils.js +4 -0
- package/dist/src/filters/utils.js.map +1 -0
- package/package.json +19 -9
- package/src/abort-options.ts +20 -0
- package/src/close.ts +65 -0
- package/src/{bloom-filter.ts → filters/bloom-filter.ts} +14 -13
- package/src/filters/bucket.ts +64 -0
- package/src/filters/cuckoo-filter.ts +197 -0
- package/src/filters/fingerprint.ts +44 -0
- package/src/filters/hashes.ts +38 -0
- package/src/filters/index.ts +9 -0
- package/src/filters/scalable-cuckoo-filter.ts +111 -0
- package/src/filters/utils.ts +3 -0
- package/dist/src/bloom-filter.d.ts.map +0 -1
- package/dist/src/bloom-filter.js.map +0 -1
|
@@ -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;QAC9B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IA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,QAAQ,EAAE,SAAS,CAAC;QAChC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;KACnB,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -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 @@
|
|
|
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
|
+
"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"
|
|
@@ -56,14 +60,18 @@
|
|
|
56
60
|
"types": "./dist/src/array-equals.d.ts",
|
|
57
61
|
"import": "./dist/src/array-equals.js"
|
|
58
62
|
},
|
|
59
|
-
"./bloom-filter": {
|
|
60
|
-
"types": "./dist/src/bloom-filter.d.ts",
|
|
61
|
-
"import": "./dist/src/bloom-filter.js"
|
|
62
|
-
},
|
|
63
63
|
"./close-source": {
|
|
64
64
|
"types": "./dist/src/close-source.d.ts",
|
|
65
65
|
"import": "./dist/src/close-source.js"
|
|
66
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
|
+
},
|
|
67
75
|
"./ip-port-to-multiaddr": {
|
|
68
76
|
"types": "./dist/src/ip-port-to-multiaddr.d.ts",
|
|
69
77
|
"import": "./dist/src/ip-port-to-multiaddr.js"
|
|
@@ -132,12 +140,14 @@
|
|
|
132
140
|
},
|
|
133
141
|
"dependencies": {
|
|
134
142
|
"@chainsafe/is-ip": "^2.0.2",
|
|
135
|
-
"@libp2p/crypto": "4.1.
|
|
136
|
-
"@libp2p/interface": "1.3.
|
|
137
|
-
"@libp2p/logger": "4.0.
|
|
143
|
+
"@libp2p/crypto": "4.1.1-1488a7371",
|
|
144
|
+
"@libp2p/interface": "1.3.1-1488a7371",
|
|
145
|
+
"@libp2p/logger": "4.0.12-1488a7371",
|
|
138
146
|
"@multiformats/multiaddr": "^12.2.1",
|
|
139
147
|
"@multiformats/multiaddr-matcher": "^1.2.0",
|
|
148
|
+
"@sindresorhus/fnv1a": "^3.1.0",
|
|
140
149
|
"@types/murmurhash3js-revisited": "^3.0.3",
|
|
150
|
+
"any-signal": "^4.1.1",
|
|
141
151
|
"delay": "^6.0.0",
|
|
142
152
|
"get-iterator": "^2.0.1",
|
|
143
153
|
"is-loopback-addr": "^2.0.2",
|
|
@@ -152,7 +162,7 @@
|
|
|
152
162
|
"uint8arrays": "^5.0.3"
|
|
153
163
|
},
|
|
154
164
|
"devDependencies": {
|
|
155
|
-
"@libp2p/peer-id-factory": "4.1.
|
|
165
|
+
"@libp2p/peer-id-factory": "4.1.1-1488a7371",
|
|
156
166
|
"@types/netmask": "^2.0.5",
|
|
157
167
|
"aegir": "^42.2.5",
|
|
158
168
|
"delay": "^6.0.0",
|
|
@@ -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
|
+
}
|
|
@@ -4,6 +4,7 @@ import mur from 'murmurhash3js-revisited'
|
|
|
4
4
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
5
|
import { alloc } from 'uint8arrays/alloc'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
+
import type { Filter } from './index.js'
|
|
7
8
|
|
|
8
9
|
const LN2_SQUARED = Math.LN2 * Math.LN2
|
|
9
10
|
|
|
@@ -13,16 +14,7 @@ export interface BloomFilterOptions {
|
|
|
13
14
|
bits?: number
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
export class BloomFilter {
|
|
17
|
-
/**
|
|
18
|
-
* Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
|
|
19
|
-
* specified item count and error rate.
|
|
20
|
-
*/
|
|
21
|
-
static create (itemcount: number, errorRate: number = 0.005): BloomFilter {
|
|
22
|
-
const opts = optimize(itemcount, errorRate)
|
|
23
|
-
return new BloomFilter(opts)
|
|
24
|
-
}
|
|
25
|
-
|
|
17
|
+
export class BloomFilter implements Filter {
|
|
26
18
|
public readonly seeds: number[]
|
|
27
19
|
public readonly bits: number
|
|
28
20
|
public buffer: Uint8Array
|
|
@@ -111,9 +103,18 @@ export class BloomFilter {
|
|
|
111
103
|
}
|
|
112
104
|
}
|
|
113
105
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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)
|
|
117
118
|
|
|
118
119
|
return { bits, hashes }
|
|
119
120
|
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
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, type Hash } from './hashes.js'
|
|
5
|
+
import { getRandomInt } from './utils.js'
|
|
6
|
+
import type { Filter } from './index.js'
|
|
7
|
+
|
|
8
|
+
const maxCuckooCount = 500
|
|
9
|
+
|
|
10
|
+
export interface CuckooFilterInit {
|
|
11
|
+
/**
|
|
12
|
+
* How many items the filter is expected to contain
|
|
13
|
+
*/
|
|
14
|
+
filterSize: number
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* How many items to put in each bucket
|
|
18
|
+
*/
|
|
19
|
+
bucketSize?: number
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How many bytes the fingerprint is expected to be
|
|
23
|
+
*/
|
|
24
|
+
fingerprintSize?: number
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A non-cryptographic hash implementation
|
|
28
|
+
*/
|
|
29
|
+
hash?: Hash
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A number used to seed the hash
|
|
33
|
+
*/
|
|
34
|
+
seed?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class CuckooFilter implements Filter {
|
|
38
|
+
private readonly bucketSize: number
|
|
39
|
+
private readonly filterSize: number
|
|
40
|
+
private readonly fingerprintSize: number
|
|
41
|
+
private readonly buckets: Bucket[]
|
|
42
|
+
public count: number
|
|
43
|
+
private readonly hash: Hash
|
|
44
|
+
private readonly seed: number
|
|
45
|
+
|
|
46
|
+
constructor (init: CuckooFilterInit) {
|
|
47
|
+
this.filterSize = init.filterSize
|
|
48
|
+
this.bucketSize = init.bucketSize ?? 4
|
|
49
|
+
this.fingerprintSize = init.fingerprintSize ?? 2
|
|
50
|
+
this.count = 0
|
|
51
|
+
this.buckets = []
|
|
52
|
+
this.hash = init.hash ?? fnv1a
|
|
53
|
+
this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
add (item: Uint8Array | string): boolean {
|
|
57
|
+
if (typeof item === 'string') {
|
|
58
|
+
item = uint8ArrayFromString(item)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
|
|
62
|
+
const j = this.hash.hash(item, this.seed) % this.filterSize
|
|
63
|
+
const k = (j ^ fingerprint.hash()) % this.filterSize
|
|
64
|
+
|
|
65
|
+
if (this.buckets[j] == null) {
|
|
66
|
+
this.buckets[j] = new Bucket(this.bucketSize)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (this.buckets[k] == null) {
|
|
70
|
+
this.buckets[k] = new Bucket(this.bucketSize)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (this.buckets[j].add(fingerprint) || this.buckets[k].add(fingerprint)) {
|
|
74
|
+
this.count++
|
|
75
|
+
return true
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const rand = [j, k]
|
|
79
|
+
let i = rand[getRandomInt(0, rand.length - 1)]
|
|
80
|
+
|
|
81
|
+
if (this.buckets[i] == null) {
|
|
82
|
+
this.buckets[i] = new Bucket(this.bucketSize)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (let n = 0; n < maxCuckooCount; n++) {
|
|
86
|
+
const swapped = this.buckets[i].swap(fingerprint)
|
|
87
|
+
|
|
88
|
+
if (swapped == null) {
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
i = (i ^ swapped.hash()) % this.filterSize
|
|
93
|
+
|
|
94
|
+
if (this.buckets[i] == null) {
|
|
95
|
+
this.buckets[i] = new Bucket(this.bucketSize)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (this.buckets[i].add(swapped)) {
|
|
99
|
+
this.count++
|
|
100
|
+
|
|
101
|
+
return true
|
|
102
|
+
} else {
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
has (item: Uint8Array | string): boolean {
|
|
111
|
+
if (typeof item === 'string') {
|
|
112
|
+
item = uint8ArrayFromString(item)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
|
|
116
|
+
const j = this.hash.hash(item, this.seed) % this.filterSize
|
|
117
|
+
const inJ = this.buckets[j]?.has(fingerprint) ?? false
|
|
118
|
+
|
|
119
|
+
if (inJ) {
|
|
120
|
+
return inJ
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const k = (j ^ fingerprint.hash()) % this.filterSize
|
|
124
|
+
|
|
125
|
+
return this.buckets[k]?.has(fingerprint) ?? false
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
remove (item: Uint8Array | string): boolean {
|
|
129
|
+
if (typeof item === 'string') {
|
|
130
|
+
item = uint8ArrayFromString(item)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
|
|
134
|
+
const j = this.hash.hash(item, this.seed) % this.filterSize
|
|
135
|
+
const inJ = this.buckets[j]?.remove(fingerprint) ?? false
|
|
136
|
+
|
|
137
|
+
if (inJ) {
|
|
138
|
+
this.count--
|
|
139
|
+
return inJ
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const k = (j ^ fingerprint.hash()) % this.filterSize
|
|
143
|
+
const inK = this.buckets[k]?.remove(fingerprint) ?? false
|
|
144
|
+
|
|
145
|
+
if (inK) {
|
|
146
|
+
this.count--
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return inK
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
get reliable (): boolean {
|
|
153
|
+
return Math.floor(100 * (this.count / this.filterSize)) <= 95
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// max load constants, defined in the cuckoo paper
|
|
158
|
+
const MAX_LOAD = {
|
|
159
|
+
1: 0.5,
|
|
160
|
+
2: 0.84,
|
|
161
|
+
4: 0.95,
|
|
162
|
+
8: 0.98
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function calculateBucketSize (errorRate: number = 0.001): 2 | 4 | 8 {
|
|
166
|
+
if (errorRate > 0.002) {
|
|
167
|
+
return 2
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (errorRate > 0.00001) {
|
|
171
|
+
return 4
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return 8
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function optimize (maxItems: number, errorRate: number = 0.001): CuckooFilterInit {
|
|
178
|
+
// https://www.eecs.harvard.edu/~michaelm/postscripts/cuckoo-conext2014.pdf
|
|
179
|
+
// Section 5.1 Optimal Bucket Size
|
|
180
|
+
const bucketSize = calculateBucketSize(errorRate)
|
|
181
|
+
const load = MAX_LOAD[bucketSize]
|
|
182
|
+
|
|
183
|
+
// https://stackoverflow.com/questions/57555236/how-to-size-a-cuckoo-filter/57617208#57617208
|
|
184
|
+
const filterSize = Math.round(maxItems / load)
|
|
185
|
+
const fingerprintSize = Math.min(Math.ceil(Math.log(filterSize / bucketSize)) + 2, MAX_FINGERPRINT_SIZE)
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
filterSize,
|
|
189
|
+
bucketSize,
|
|
190
|
+
fingerprintSize
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function createCuckooFilter (maxItems: number, errorRate: number = 0.005): Filter {
|
|
195
|
+
const opts = optimize(maxItems, errorRate)
|
|
196
|
+
return new CuckooFilter(opts)
|
|
197
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
2
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
3
|
+
import type { Hash } from './hashes'
|
|
4
|
+
|
|
5
|
+
export const MAX_FINGERPRINT_SIZE = 64
|
|
6
|
+
|
|
7
|
+
export class Fingerprint {
|
|
8
|
+
private readonly fp: Uint8Array
|
|
9
|
+
private readonly h: Hash
|
|
10
|
+
private readonly seed: number
|
|
11
|
+
|
|
12
|
+
constructor (buf: Uint8Array, hash: Hash, seed: number, fingerprintSize: number = 2) {
|
|
13
|
+
if (fingerprintSize > MAX_FINGERPRINT_SIZE) {
|
|
14
|
+
throw new TypeError('Invalid Fingerprint Size')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const fnv = hash.hashV(buf, seed)
|
|
18
|
+
const fp = uint8ArrayAlloc(fingerprintSize)
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < fp.length; i++) {
|
|
21
|
+
fp[i] = fnv[i]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (fp.length === 0) {
|
|
25
|
+
fp[0] = 7
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.fp = fp
|
|
29
|
+
this.h = hash
|
|
30
|
+
this.seed = seed
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
hash (): number {
|
|
34
|
+
return this.h.hash(this.fp, this.seed)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
equals (other?: any): boolean {
|
|
38
|
+
if (!(other?.fp instanceof Uint8Array)) {
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return uint8ArrayEquals(this.fp, other.fp)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import fnv1aHash from '@sindresorhus/fnv1a'
|
|
2
|
+
import mur from 'murmurhash3js-revisited'
|
|
3
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
|
+
|
|
5
|
+
export interface Hash {
|
|
6
|
+
hash(input: Uint8Array, seed: number): number
|
|
7
|
+
hashV(input: Uint8Array, seed: number): Uint8Array
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const murmur3: Hash = {
|
|
11
|
+
hash: (input, seed) => {
|
|
12
|
+
return mur.x86.hash32(input, seed)
|
|
13
|
+
},
|
|
14
|
+
hashV: (input, seed) => {
|
|
15
|
+
return numberToBuffer(murmur3.hash(input, seed))
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const fnv1a: Hash = {
|
|
20
|
+
hash: (input) => {
|
|
21
|
+
return Number(fnv1aHash(input, {
|
|
22
|
+
size: 32
|
|
23
|
+
}))
|
|
24
|
+
},
|
|
25
|
+
hashV: (input, seed) => {
|
|
26
|
+
return numberToBuffer(fnv1a.hash(input, seed))
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function numberToBuffer (num: bigint | number): Uint8Array {
|
|
31
|
+
let hex = num.toString(16)
|
|
32
|
+
|
|
33
|
+
if (hex.length % 2 === 1) {
|
|
34
|
+
hex = `0${hex}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return uint8ArrayFromString(hex, 'base16')
|
|
38
|
+
}
|
|
@@ -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
|
+
|
|
5
|
+
export interface Filter {
|
|
6
|
+
add(item: Uint8Array | string): void
|
|
7
|
+
has(item: Uint8Array | string): boolean
|
|
8
|
+
remove?(buf: Uint8Array | string): boolean
|
|
9
|
+
}
|