@libp2p/utils 6.5.4 → 6.5.5
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/filters/bloom-filter.d.ts +1 -1
- package/dist/src/filters/bloom-filter.js +7 -7
- package/dist/src/moving-average.d.ts +2 -2
- package/dist/src/moving-average.js +4 -4
- package/dist/src/rate-limiter.d.ts +1 -1
- package/dist/src/stream-to-ma-conn.js +3 -3
- package/dist/src/stream-to-ma-conn.js.map +1 -1
- package/package.json +6 -6
- package/src/filters/bloom-filter.ts +7 -7
- package/src/moving-average.ts +4 -4
- package/src/rate-limiter.ts +1 -1
- package/src/stream-to-ma-conn.ts +3 -3
|
@@ -30,5 +30,5 @@ export declare class BloomFilter implements Filter {
|
|
|
30
30
|
* Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
|
|
31
31
|
* specified item count and error rate.
|
|
32
32
|
*/
|
|
33
|
-
export declare function createBloomFilter(
|
|
33
|
+
export declare function createBloomFilter(itemCount: number, errorRate?: number): Filter;
|
|
34
34
|
//# sourceMappingURL=bloom-filter.d.ts.map
|
|
@@ -60,23 +60,23 @@ export class BloomFilter {
|
|
|
60
60
|
setbit(bit) {
|
|
61
61
|
const pos = Math.floor(bit / 8);
|
|
62
62
|
const shift = bit % 8;
|
|
63
|
-
let
|
|
64
|
-
|
|
65
|
-
this.buffer[pos] =
|
|
63
|
+
let bitField = this.buffer[pos];
|
|
64
|
+
bitField |= (0x1 << shift);
|
|
65
|
+
this.buffer[pos] = bitField;
|
|
66
66
|
}
|
|
67
67
|
getbit(bit) {
|
|
68
68
|
const pos = Math.floor(bit / 8);
|
|
69
69
|
const shift = bit % 8;
|
|
70
|
-
const
|
|
71
|
-
return (
|
|
70
|
+
const bitField = this.buffer[pos];
|
|
71
|
+
return (bitField & (0x1 << shift)) !== 0;
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
|
|
76
76
|
* specified item count and error rate.
|
|
77
77
|
*/
|
|
78
|
-
export function createBloomFilter(
|
|
79
|
-
const opts = optimize(
|
|
78
|
+
export function createBloomFilter(itemCount, errorRate = 0.005) {
|
|
79
|
+
const opts = optimize(itemCount, errorRate);
|
|
80
80
|
return new BloomFilter(opts);
|
|
81
81
|
}
|
|
82
82
|
function optimize(itemCount, errorRate = 0.005) {
|
|
@@ -9,9 +9,9 @@ export declare class MovingAverage {
|
|
|
9
9
|
variance: number;
|
|
10
10
|
deviation: number;
|
|
11
11
|
forecast: number;
|
|
12
|
-
private readonly
|
|
12
|
+
private readonly timeSpan;
|
|
13
13
|
private previousTime?;
|
|
14
|
-
constructor(
|
|
14
|
+
constructor(timeSpan: number);
|
|
15
15
|
alpha(t: number, pt: number): number;
|
|
16
16
|
push(value: number, time?: number): void;
|
|
17
17
|
}
|
|
@@ -9,17 +9,17 @@ export class MovingAverage {
|
|
|
9
9
|
variance;
|
|
10
10
|
deviation;
|
|
11
11
|
forecast;
|
|
12
|
-
|
|
12
|
+
timeSpan;
|
|
13
13
|
previousTime;
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
14
|
+
constructor(timeSpan) {
|
|
15
|
+
this.timeSpan = timeSpan;
|
|
16
16
|
this.movingAverage = 0;
|
|
17
17
|
this.variance = 0;
|
|
18
18
|
this.deviation = 0;
|
|
19
19
|
this.forecast = 0;
|
|
20
20
|
}
|
|
21
21
|
alpha(t, pt) {
|
|
22
|
-
return 1 - (Math.exp(-(t - pt) / this.
|
|
22
|
+
return 1 - (Math.exp(-(t - pt) / this.timeSpan));
|
|
23
23
|
}
|
|
24
24
|
push(value, time = Date.now()) {
|
|
25
25
|
if (this.previousTime != null) {
|
|
@@ -9,19 +9,19 @@ export function streamToMaConnection(props) {
|
|
|
9
9
|
const log = logger.forComponent('libp2p:stream:converter');
|
|
10
10
|
let closedRead = false;
|
|
11
11
|
let closedWrite = false;
|
|
12
|
-
// piggyback on `stream.close` invocations to close
|
|
12
|
+
// piggyback on `stream.close` invocations to close multiaddr connection
|
|
13
13
|
const streamClose = stream.close.bind(stream);
|
|
14
14
|
stream.close = async (options) => {
|
|
15
15
|
await streamClose(options);
|
|
16
16
|
close(true);
|
|
17
17
|
};
|
|
18
|
-
// piggyback on `stream.abort` invocations to close
|
|
18
|
+
// piggyback on `stream.abort` invocations to close multiaddr connection
|
|
19
19
|
const streamAbort = stream.abort.bind(stream);
|
|
20
20
|
stream.abort = (err) => {
|
|
21
21
|
streamAbort(err);
|
|
22
22
|
close(true);
|
|
23
23
|
};
|
|
24
|
-
// piggyback on `stream.sink` invocations to close
|
|
24
|
+
// piggyback on `stream.sink` invocations to close multiaddr connection
|
|
25
25
|
const streamSink = stream.sink.bind(stream);
|
|
26
26
|
stream.sink = async (source) => {
|
|
27
27
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-to-ma-conn.js","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,YAAY,CAAA;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAsB9B;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAE,KAAuB;IAC3D,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,KAAK,CAAA;IACrE,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,yBAAyB,CAAC,CAAA;IAE1D,IAAI,UAAU,GAAG,KAAK,CAAA;IACtB,IAAI,WAAW,GAAG,KAAK,CAAA;IAEvB,
|
|
1
|
+
{"version":3,"file":"stream-to-ma-conn.js","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,YAAY,CAAA;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAsB9B;;;GAGG;AACH,MAAM,UAAU,oBAAo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"@libp2p/utils",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.5",
|
|
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",
|
|
@@ -189,9 +189,9 @@
|
|
|
189
189
|
"dependencies": {
|
|
190
190
|
"@chainsafe/is-ip": "^2.0.2",
|
|
191
191
|
"@chainsafe/netmask": "^2.0.0",
|
|
192
|
-
"@libp2p/crypto": "^5.0.
|
|
193
|
-
"@libp2p/interface": "^2.6.
|
|
194
|
-
"@libp2p/logger": "^5.1.
|
|
192
|
+
"@libp2p/crypto": "^5.0.13",
|
|
193
|
+
"@libp2p/interface": "^2.6.1",
|
|
194
|
+
"@libp2p/logger": "^5.1.10",
|
|
195
195
|
"@multiformats/multiaddr": "^12.3.3",
|
|
196
196
|
"@sindresorhus/fnv1a": "^3.1.0",
|
|
197
197
|
"any-signal": "^4.1.1",
|
|
@@ -210,9 +210,9 @@
|
|
|
210
210
|
"uint8arrays": "^5.1.0"
|
|
211
211
|
},
|
|
212
212
|
"devDependencies": {
|
|
213
|
-
"@libp2p/peer-id": "^5.0.
|
|
213
|
+
"@libp2p/peer-id": "^5.0.14",
|
|
214
214
|
"@types/netmask": "^2.0.5",
|
|
215
|
-
"aegir": "^45.
|
|
215
|
+
"aegir": "^45.1.1",
|
|
216
216
|
"benchmark": "^2.1.4",
|
|
217
217
|
"delay": "^6.0.0",
|
|
218
218
|
"it-all": "^3.0.6",
|
|
@@ -81,17 +81,17 @@ export class BloomFilter implements Filter {
|
|
|
81
81
|
const pos = Math.floor(bit / 8)
|
|
82
82
|
const shift = bit % 8
|
|
83
83
|
|
|
84
|
-
let
|
|
85
|
-
|
|
86
|
-
this.buffer[pos] =
|
|
84
|
+
let bitField = this.buffer[pos]
|
|
85
|
+
bitField |= (0x1 << shift)
|
|
86
|
+
this.buffer[pos] = bitField
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
getbit (bit: number): boolean {
|
|
90
90
|
const pos = Math.floor(bit / 8)
|
|
91
91
|
const shift = bit % 8
|
|
92
92
|
|
|
93
|
-
const
|
|
94
|
-
return (
|
|
93
|
+
const bitField = this.buffer[pos]
|
|
94
|
+
return (bitField & (0x1 << shift)) !== 0
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
|
|
@@ -99,8 +99,8 @@ export class BloomFilter implements Filter {
|
|
|
99
99
|
* Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
|
|
100
100
|
* specified item count and error rate.
|
|
101
101
|
*/
|
|
102
|
-
export function createBloomFilter (
|
|
103
|
-
const opts = optimize(
|
|
102
|
+
export function createBloomFilter (itemCount: number, errorRate: number = 0.005): Filter {
|
|
103
|
+
const opts = optimize(itemCount, errorRate)
|
|
104
104
|
return new BloomFilter(opts)
|
|
105
105
|
}
|
|
106
106
|
|
package/src/moving-average.ts
CHANGED
|
@@ -9,11 +9,11 @@ export class MovingAverage {
|
|
|
9
9
|
public variance: number
|
|
10
10
|
public deviation: number
|
|
11
11
|
public forecast: number
|
|
12
|
-
private readonly
|
|
12
|
+
private readonly timeSpan: number
|
|
13
13
|
private previousTime?: number
|
|
14
14
|
|
|
15
|
-
constructor (
|
|
16
|
-
this.
|
|
15
|
+
constructor (timeSpan: number) {
|
|
16
|
+
this.timeSpan = timeSpan
|
|
17
17
|
this.movingAverage = 0
|
|
18
18
|
this.variance = 0
|
|
19
19
|
this.deviation = 0
|
|
@@ -21,7 +21,7 @@ export class MovingAverage {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
alpha (t: number, pt: number): number {
|
|
24
|
-
return 1 - (Math.exp(-(t - pt) / this.
|
|
24
|
+
return 1 - (Math.exp(-(t - pt) / this.timeSpan))
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
push (value: number, time: number = Date.now()): void {
|
package/src/rate-limiter.ts
CHANGED
package/src/stream-to-ma-conn.ts
CHANGED
|
@@ -32,21 +32,21 @@ export function streamToMaConnection (props: StreamProperties): MultiaddrConnect
|
|
|
32
32
|
let closedRead = false
|
|
33
33
|
let closedWrite = false
|
|
34
34
|
|
|
35
|
-
// piggyback on `stream.close` invocations to close
|
|
35
|
+
// piggyback on `stream.close` invocations to close multiaddr connection
|
|
36
36
|
const streamClose = stream.close.bind(stream)
|
|
37
37
|
stream.close = async (options) => {
|
|
38
38
|
await streamClose(options)
|
|
39
39
|
close(true)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
// piggyback on `stream.abort` invocations to close
|
|
42
|
+
// piggyback on `stream.abort` invocations to close multiaddr connection
|
|
43
43
|
const streamAbort = stream.abort.bind(stream)
|
|
44
44
|
stream.abort = (err) => {
|
|
45
45
|
streamAbort(err)
|
|
46
46
|
close(true)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
// piggyback on `stream.sink` invocations to close
|
|
49
|
+
// piggyback on `stream.sink` invocations to close multiaddr connection
|
|
50
50
|
const streamSink = stream.sink.bind(stream)
|
|
51
51
|
stream.sink = async (source) => {
|
|
52
52
|
try {
|