@libp2p/peer-collections 0.0.0
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/LICENSE +4 -0
- package/README.md +49 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +4 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/list.d.ts +30 -0
- package/dist/src/list.d.ts.map +1 -0
- package/dist/src/list.js +110 -0
- package/dist/src/list.js.map +1 -0
- package/dist/src/map.d.ts +26 -0
- package/dist/src/map.d.ts.map +1 -0
- package/dist/src/map.js +63 -0
- package/dist/src/map.js.map +1 -0
- package/dist/src/set.d.ts +23 -0
- package/dist/src/set.d.ts.map +1 -0
- package/dist/src/set.js +53 -0
- package/dist/src/set.js.map +1 -0
- package/dist/src/util.d.ts +2 -0
- package/dist/src/util.d.ts.map +1 -0
- package/dist/src/util.js +24 -0
- package/dist/src/util.js.map +1 -0
- package/package.json +142 -0
- package/src/index.ts +3 -0
- package/src/list.ts +149 -0
- package/src/map.ts +85 -0
- package/src/set.ts +73 -0
- package/src/util.ts +28 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# libp2p-peer-connections <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
> store values against peer ids
|
|
4
|
+
|
|
5
|
+
## Table of Contents <!-- omit in toc -->
|
|
6
|
+
|
|
7
|
+
- [Description](#description)
|
|
8
|
+
- [Example](#example)
|
|
9
|
+
- [Installation](#installation)
|
|
10
|
+
- [License](#license)
|
|
11
|
+
- [Contribution](#contribution)
|
|
12
|
+
|
|
13
|
+
## Description
|
|
14
|
+
|
|
15
|
+
We can't use PeerIds as collection keys because collection keys are compared using same-value-zero equality, so this is just a group of collections that stringifies PeerIds before storing them.
|
|
16
|
+
|
|
17
|
+
PeerIds cache stringified versions of themselves so this should be a cheap operation.
|
|
18
|
+
|
|
19
|
+
## Example
|
|
20
|
+
|
|
21
|
+
```JavaScript
|
|
22
|
+
import { peerMap, peerSet, peerList } from '@libp2p/peer-collections'
|
|
23
|
+
|
|
24
|
+
const map = peerMap<string>()
|
|
25
|
+
map.set(peerId, 'value')
|
|
26
|
+
|
|
27
|
+
const set = peerSet()
|
|
28
|
+
set.add(peerId)
|
|
29
|
+
|
|
30
|
+
const list = peerList()
|
|
31
|
+
list.push(peerId)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```console
|
|
37
|
+
$ npm i @libp2p/peer-collections
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
Licensed under either of
|
|
43
|
+
|
|
44
|
+
* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
|
|
45
|
+
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)
|
|
46
|
+
|
|
47
|
+
### Contribution
|
|
48
|
+
|
|
49
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAC3C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAC3C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
2
|
+
/**
|
|
3
|
+
* We can't use PeerIds as list entries because list entries are
|
|
4
|
+
* compared using same-value-zero equality, so this is just
|
|
5
|
+
* a map that stringifies the PeerIds before storing them.
|
|
6
|
+
*
|
|
7
|
+
* PeerIds cache stringified versions of themselves so this
|
|
8
|
+
* should be a cheap operation.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PeerList {
|
|
11
|
+
private readonly list;
|
|
12
|
+
constructor(list?: PeerList);
|
|
13
|
+
[Symbol.iterator](): IterableIterator<PeerId>;
|
|
14
|
+
concat(list: PeerList): PeerList;
|
|
15
|
+
entries(): IterableIterator<[number, PeerId]>;
|
|
16
|
+
every(predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): boolean;
|
|
17
|
+
filter(predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerList;
|
|
18
|
+
find(predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerId | undefined;
|
|
19
|
+
findIndex(predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): number;
|
|
20
|
+
forEach(predicate: (peerId: PeerId, index: number, arr: PeerList) => void): void;
|
|
21
|
+
includes(peerId: PeerId): boolean;
|
|
22
|
+
indexOf(peerId: PeerId): number;
|
|
23
|
+
pop(): PeerId | undefined;
|
|
24
|
+
push(...peerIds: PeerId[]): void;
|
|
25
|
+
shift(): PeerId | undefined;
|
|
26
|
+
unshift(...peerIds: PeerId[]): number;
|
|
27
|
+
get length(): number;
|
|
28
|
+
}
|
|
29
|
+
export declare function peerList(map?: PeerList): PeerList;
|
|
30
|
+
//# sourceMappingURL=list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/list.ts"],"names":[],"m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O,EAAE,MAAM,EAAE;IAU7B,IAAI,MAAM,WAET;CACF;AAED,wBAAgB,QAAQ,CAAE,GAAG,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAElD"}
|
package/dist/src/list.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
|
+
import { mapIterable } from './util.js';
|
|
3
|
+
/**
|
|
4
|
+
* We can't use PeerIds as list entries because list entries are
|
|
5
|
+
* compared using same-value-zero equality, so this is just
|
|
6
|
+
* a map that stringifies the PeerIds before storing them.
|
|
7
|
+
*
|
|
8
|
+
* PeerIds cache stringified versions of themselves so this
|
|
9
|
+
* should be a cheap operation.
|
|
10
|
+
*/
|
|
11
|
+
export class PeerList {
|
|
12
|
+
constructor(list) {
|
|
13
|
+
this.list = [];
|
|
14
|
+
if (list != null) {
|
|
15
|
+
for (const value of list) {
|
|
16
|
+
this.list.push(value.toString());
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
[Symbol.iterator]() {
|
|
21
|
+
return mapIterable(this.list.entries(), (val) => {
|
|
22
|
+
return peerIdFromString(val[1]);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
concat(list) {
|
|
26
|
+
const output = new PeerList(this);
|
|
27
|
+
for (const value of list) {
|
|
28
|
+
output.push(value);
|
|
29
|
+
}
|
|
30
|
+
return output;
|
|
31
|
+
}
|
|
32
|
+
entries() {
|
|
33
|
+
return mapIterable(this.list.entries(), (val) => {
|
|
34
|
+
return [val[0], peerIdFromString(val[1])];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
every(predicate) {
|
|
38
|
+
return this.list.every((str, index) => {
|
|
39
|
+
return predicate(peerIdFromString(str), index, this);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
filter(predicate) {
|
|
43
|
+
const output = new PeerList();
|
|
44
|
+
this.list.forEach((str, index) => {
|
|
45
|
+
const peerId = peerIdFromString(str);
|
|
46
|
+
if (predicate(peerId, index, this)) {
|
|
47
|
+
output.push(peerId);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return output;
|
|
51
|
+
}
|
|
52
|
+
find(predicate) {
|
|
53
|
+
const str = this.list.find((str, index) => {
|
|
54
|
+
return predicate(peerIdFromString(str), index, this);
|
|
55
|
+
});
|
|
56
|
+
if (str == null) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return peerIdFromString(str);
|
|
60
|
+
}
|
|
61
|
+
findIndex(predicate) {
|
|
62
|
+
return this.list.findIndex((str, index) => {
|
|
63
|
+
return predicate(peerIdFromString(str), index, this);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
forEach(predicate) {
|
|
67
|
+
this.list.forEach((str, index) => {
|
|
68
|
+
predicate(peerIdFromString(str), index, this);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
includes(peerId) {
|
|
72
|
+
return this.list.includes(peerId.toString());
|
|
73
|
+
}
|
|
74
|
+
indexOf(peerId) {
|
|
75
|
+
return this.list.indexOf(peerId.toString());
|
|
76
|
+
}
|
|
77
|
+
pop() {
|
|
78
|
+
const str = this.list.pop();
|
|
79
|
+
if (str == null) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
return peerIdFromString(str);
|
|
83
|
+
}
|
|
84
|
+
push(...peerIds) {
|
|
85
|
+
for (const peerId of peerIds) {
|
|
86
|
+
this.list.push(peerId.toString());
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
shift() {
|
|
90
|
+
const str = this.list.shift();
|
|
91
|
+
if (str == null) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
return peerIdFromString(str);
|
|
95
|
+
}
|
|
96
|
+
unshift(...peerIds) {
|
|
97
|
+
let len = this.list.length;
|
|
98
|
+
for (let i = peerIds.length - 1; i > -1; i--) {
|
|
99
|
+
len = this.list.unshift(peerIds[i].toString());
|
|
100
|
+
}
|
|
101
|
+
return len;
|
|
102
|
+
}
|
|
103
|
+
get length() {
|
|
104
|
+
return this.list.length;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export function peerList(map) {
|
|
108
|
+
return new PeerList(map);
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=list.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.js","sourceRoot":"","sources":["../../src/list.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC;;;;;;;GAOG;AACH,MAAM,OAAO,QAAQ;IAGnB,YAAa,IAAe;QAC1B,IAAI,CAAC,IAAI,GAAG,EAAE,CAAA;QAEd,IAAI,IAAI,IAAI,IAAI,EAAE;YAChB,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE;gBACxB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAA;aACjC;SACF;IACH,CAAC;IAED,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,WAAW,CAChB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EACnB,CAAC,GAAG,EAAE,EAAE;YACN,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;QACjC,CAAC,CACF,CAAA;IACH,CAAC;IAED,MAAM,CAAE,IAAc;QACpB,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAA;QAEjC,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE;YACxB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;SACnB;QAED,OAAO,MAAM,CAAA;IACf,CAAC;IAED,OAAO;QACL,OAAO,WAAW,CAChB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EACnB,CAAC,GAAG,EAAE,EAAE;YACN,OAAO,CAAC,G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K,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YAC5C,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAA;SAC/C;QAED,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAA;IACzB,CAAC;CACF;AAED,MAAM,UAAU,QAAQ,CAAE,GAAc;IACtC,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAA;AAC1B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
2
|
+
/**
|
|
3
|
+
* We can't use PeerIds as map keys because map keys are
|
|
4
|
+
* compared using same-value-zero equality, so this is just
|
|
5
|
+
* a map that stringifies the PeerIds before storing them.
|
|
6
|
+
*
|
|
7
|
+
* PeerIds cache stringified versions of themselves so this
|
|
8
|
+
* should be a cheap operation.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PeerMap<T> {
|
|
11
|
+
private readonly map;
|
|
12
|
+
constructor(map?: PeerMap<T>);
|
|
13
|
+
[Symbol.iterator](): IterableIterator<[PeerId, T]>;
|
|
14
|
+
clear(): void;
|
|
15
|
+
delete(peer: PeerId): void;
|
|
16
|
+
entries(): IterableIterator<[PeerId, T]>;
|
|
17
|
+
forEach(fn: (value: T, key: PeerId, map: PeerMap<T>) => void): void;
|
|
18
|
+
get(peer: PeerId): T | undefined;
|
|
19
|
+
has(peer: PeerId): boolean;
|
|
20
|
+
set(peer: PeerId, value: T): void;
|
|
21
|
+
keys(): IterableIterator<PeerId>;
|
|
22
|
+
values(): IterableIterator<T>;
|
|
23
|
+
get size(): number;
|
|
24
|
+
}
|
|
25
|
+
export declare function peerMap<T>(map?: PeerMap<T>): PeerMap<T>;
|
|
26
|
+
//# sourceMappingURL=map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAIxD;;;;;;;GAOG;AACH,qBAAa,OAAO,CAAE,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAgB;gBAEvB,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IAU7B,CAAC,MAAM,CAAC,QAAQ,CAAC;IAIjB,KAAK;IAIL,MAAM,CAAE,IAAI,EAAE,MAAM;IAIpB,OAAO,IAAK,gBAAgB,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IASzC,OAAO,CAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,IAAI;IAMpE,GAAG,CAAE,IAAI,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAIjC,GAAG,CAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAI3B,GAAG,CAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAI3B,IAAI,IAAK,gBAAgB,CAAC,MAAM,CAAC;IASjC,MAAM;IAIN,IAAI,IAAI,WAEP;CACF;AAED,wBAAgB,OAAO,CAAE,CAAC,EAAG,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAEzD"}
|
package/dist/src/map.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
|
+
import { mapIterable } from './util.js';
|
|
3
|
+
/**
|
|
4
|
+
* We can't use PeerIds as map keys because map keys are
|
|
5
|
+
* compared using same-value-zero equality, so this is just
|
|
6
|
+
* a map that stringifies the PeerIds before storing them.
|
|
7
|
+
*
|
|
8
|
+
* PeerIds cache stringified versions of themselves so this
|
|
9
|
+
* should be a cheap operation.
|
|
10
|
+
*/
|
|
11
|
+
export class PeerMap {
|
|
12
|
+
constructor(map) {
|
|
13
|
+
this.map = new Map();
|
|
14
|
+
if (map != null) {
|
|
15
|
+
for (const [key, value] of map.entries()) {
|
|
16
|
+
this.map.set(key.toString(), value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
[Symbol.iterator]() {
|
|
21
|
+
return this.entries();
|
|
22
|
+
}
|
|
23
|
+
clear() {
|
|
24
|
+
this.map.clear();
|
|
25
|
+
}
|
|
26
|
+
delete(peer) {
|
|
27
|
+
this.map.delete(peer.toString());
|
|
28
|
+
}
|
|
29
|
+
entries() {
|
|
30
|
+
return mapIterable(this.map.entries(), (val) => {
|
|
31
|
+
return [peerIdFromString(val[0]), val[1]];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
forEach(fn) {
|
|
35
|
+
this.map.forEach((value, key) => {
|
|
36
|
+
fn(value, peerIdFromString(key), this);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
get(peer) {
|
|
40
|
+
return this.map.get(peer.toString());
|
|
41
|
+
}
|
|
42
|
+
has(peer) {
|
|
43
|
+
return this.map.has(peer.toString());
|
|
44
|
+
}
|
|
45
|
+
set(peer, value) {
|
|
46
|
+
this.map.set(peer.toString(), value);
|
|
47
|
+
}
|
|
48
|
+
keys() {
|
|
49
|
+
return mapIterable(this.map.keys(), (val) => {
|
|
50
|
+
return peerIdFromString(val);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
values() {
|
|
54
|
+
return this.map.values();
|
|
55
|
+
}
|
|
56
|
+
get size() {
|
|
57
|
+
return this.map.size;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function peerMap(map) {
|
|
61
|
+
return new PeerMap(map);
|
|
62
|
+
}
|
|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AAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAA;QACxC,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAE,IAAY;QACf,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;IACtC,CAAC;IAED,GAAG,CAAE,IAAY;QACf,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;IACtC,CAAC;IAED,GAAG,CAAE,IAAY,EAAE,KAAQ;QACzB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAA;IACtC,CAAC;IAED,IAAI;QACF,OAAO,WAAW,CAChB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EACf,CAAC,GAAG,EAAE,EAAE;YACN,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAA;QAC9B,CAAC,CACF,CAAA;IACH,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAA;IAC1B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAA;IACtB,CAAC;CACF;AAED,MAAM,UAAU,OAAO,CAAM,GAAgB;IAC3C,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,CAAA;AACzB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
2
|
+
/**
|
|
3
|
+
* We can't use PeerIds as set entries because set entries are
|
|
4
|
+
* compared using same-value-zero equality, so this is just
|
|
5
|
+
* a map that stringifies the PeerIds before storing them.
|
|
6
|
+
*
|
|
7
|
+
* PeerIds cache stringified versions of themselves so this
|
|
8
|
+
* should be a cheap operation.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PeerSet {
|
|
11
|
+
private readonly set;
|
|
12
|
+
constructor(set?: PeerSet);
|
|
13
|
+
get size(): number;
|
|
14
|
+
[Symbol.iterator](): IterableIterator<PeerId>;
|
|
15
|
+
add(peer: PeerId): void;
|
|
16
|
+
clear(): void;
|
|
17
|
+
delete(peer: PeerId): void;
|
|
18
|
+
entries(): IterableIterator<[PeerId, PeerId]>;
|
|
19
|
+
has(peer: PeerId): boolean;
|
|
20
|
+
values(): IterableIterator<PeerId>;
|
|
21
|
+
}
|
|
22
|
+
export declare function peerSet(set?: PeerSet): PeerSet;
|
|
23
|
+
//# sourceMappingURL=set.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/set.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAIxD;;;;;;;GAOG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAa;gBAEpB,GAAG,CAAC,EAAE,OAAO;IAU1B,IAAI,IAAI,WAEP;IAED,CAAC,MAAM,CAAC,QAAQ,CAAC;IAIjB,GAAG,CAAE,IAAI,EAAE,MAAM;IAIjB,KAAK;IAIL,MAAM,CAAE,IAAI,EAAE,MAAM;IAIpB,OAAO,IAAK,gBAAgB,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAW9C,GAAG,CAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAI3B,MAAM;CAQP;AAED,wBAAgB,OAAO,CAAE,GAAG,CAAC,EAAE,OAAO,GAAG,OAAO,CAE/C"}
|
package/dist/src/set.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
|
+
import { mapIterable } from './util.js';
|
|
3
|
+
/**
|
|
4
|
+
* We can't use PeerIds as set entries because set entries are
|
|
5
|
+
* compared using same-value-zero equality, so this is just
|
|
6
|
+
* a map that stringifies the PeerIds before storing them.
|
|
7
|
+
*
|
|
8
|
+
* PeerIds cache stringified versions of themselves so this
|
|
9
|
+
* should be a cheap operation.
|
|
10
|
+
*/
|
|
11
|
+
export class PeerSet {
|
|
12
|
+
constructor(set) {
|
|
13
|
+
this.set = new Set();
|
|
14
|
+
if (set != null) {
|
|
15
|
+
for (const key of set) {
|
|
16
|
+
this.set.add(key.toString());
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
get size() {
|
|
21
|
+
return this.set.size;
|
|
22
|
+
}
|
|
23
|
+
[Symbol.iterator]() {
|
|
24
|
+
return this.values();
|
|
25
|
+
}
|
|
26
|
+
add(peer) {
|
|
27
|
+
this.set.add(peer.toString());
|
|
28
|
+
}
|
|
29
|
+
clear() {
|
|
30
|
+
this.set.clear();
|
|
31
|
+
}
|
|
32
|
+
delete(peer) {
|
|
33
|
+
this.set.delete(peer.toString());
|
|
34
|
+
}
|
|
35
|
+
entries() {
|
|
36
|
+
return mapIterable(this.set.entries(), (val) => {
|
|
37
|
+
const peerId = peerIdFromString(val[0]);
|
|
38
|
+
return [peerId, peerId];
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
has(peer) {
|
|
42
|
+
return this.set.has(peer.toString());
|
|
43
|
+
}
|
|
44
|
+
values() {
|
|
45
|
+
return mapIterable(this.set.values(), (val) => {
|
|
46
|
+
return peerIdFromString(val);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function peerSet(set) {
|
|
51
|
+
return new PeerSet(set);
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=set.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set.js","sourceRoot":"","sources":["../../src/set.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC;;;;;;;GAOG;AACH,MAAM,OAAO,OAAO;IAGlB,YAA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B,CAAC,CACF,CAAA;IACH,CAAC;CACF;AAED,MAAM,UAAU,OAAO,CAAE,GAAa;IACpC,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,CAAA;AACzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AACA,wBAAgB,WAAW,CAAE,CAAC,EAAE,CAAC,EAAG,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CA0BtG"}
|
package/dist/src/util.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function mapIterable(iter, map) {
|
|
2
|
+
const iterator = {
|
|
3
|
+
[Symbol.iterator]: () => {
|
|
4
|
+
return iterator;
|
|
5
|
+
},
|
|
6
|
+
next: () => {
|
|
7
|
+
const next = iter.next();
|
|
8
|
+
const val = next.value;
|
|
9
|
+
if (next.done === true || val == null) {
|
|
10
|
+
const result = {
|
|
11
|
+
done: true,
|
|
12
|
+
value: undefined
|
|
13
|
+
};
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
done: false,
|
|
18
|
+
value: map(val)
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return iterator;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,WAAW,CAAS,IAAyB,EAAE,GAAkB;IAC/E,MAAM,QAAQ,GAAwB;QACpC,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE;YACtB,OAAO,QAAQ,CAAA;QACjB,CAAC;QACD,IAAI,EAAE,GAAG,EAAE;YACT,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAA;YACxB,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAA;YAEtB,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,EAAE;gBACrC,MAAM,MAAM,GAA8B;oBACxC,IAAI,EAAE,IAAI;oBACV,KAAK,EAAE,SAAS;iBACjB,CAAA;gBAED,OAAO,MAAM,CAAA;aACd;YAED,OAAO;gBACL,IAAI,EAAE,KAAK;gBACX,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC;aAChB,CAAA;QACH,CAAC;KACF,CAAA;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@libp2p/peer-collections",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Stores values against a peer id",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-peer-collections#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p-interfaces.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p-interfaces/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"IPFS"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=16.0.0",
|
|
19
|
+
"npm": ">=7.0.0"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"types": "./dist/src/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"dist/src",
|
|
26
|
+
"!dist/test",
|
|
27
|
+
"!**/*.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/src/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"eslintConfig": {
|
|
35
|
+
"extends": "ipfs",
|
|
36
|
+
"parserOptions": {
|
|
37
|
+
"sourceType": "module"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"release": {
|
|
41
|
+
"branches": [
|
|
42
|
+
"master"
|
|
43
|
+
],
|
|
44
|
+
"plugins": [
|
|
45
|
+
[
|
|
46
|
+
"@semantic-release/commit-analyzer",
|
|
47
|
+
{
|
|
48
|
+
"preset": "conventionalcommits",
|
|
49
|
+
"releaseRules": [
|
|
50
|
+
{
|
|
51
|
+
"breaking": true,
|
|
52
|
+
"release": "major"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"revert": true,
|
|
56
|
+
"release": "patch"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"type": "feat",
|
|
60
|
+
"release": "minor"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"type": "fix",
|
|
64
|
+
"release": "patch"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"type": "chore",
|
|
68
|
+
"release": "patch"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"type": "docs",
|
|
72
|
+
"release": "patch"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"type": "test",
|
|
76
|
+
"release": "patch"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"scope": "no-release",
|
|
80
|
+
"release": false
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
"@semantic-release/release-notes-generator",
|
|
87
|
+
{
|
|
88
|
+
"preset": "conventionalcommits",
|
|
89
|
+
"presetConfig": {
|
|
90
|
+
"types": [
|
|
91
|
+
{
|
|
92
|
+
"type": "feat",
|
|
93
|
+
"section": "Features"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"type": "fix",
|
|
97
|
+
"section": "Bug Fixes"
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
"type": "chore",
|
|
101
|
+
"section": "Trivial Changes"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"type": "docs",
|
|
105
|
+
"section": "Trivial Changes"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"type": "test",
|
|
109
|
+
"section": "Tests"
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
],
|
|
115
|
+
"@semantic-release/changelog",
|
|
116
|
+
"@semantic-release/npm",
|
|
117
|
+
"@semantic-release/github",
|
|
118
|
+
"@semantic-release/git"
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
"scripts": {
|
|
122
|
+
"lint": "aegir lint",
|
|
123
|
+
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
|
|
124
|
+
"build": "tsc",
|
|
125
|
+
"pretest": "npm run build",
|
|
126
|
+
"test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js",
|
|
127
|
+
"test:chrome": "npm run test -- -t browser",
|
|
128
|
+
"test:chrome-webworker": "npm run test -- -t webworker",
|
|
129
|
+
"test:firefox": "npm run test -- -t browser -- --browser firefox",
|
|
130
|
+
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
|
|
131
|
+
"test:node": "npm run test -- -t node --cov",
|
|
132
|
+
"test:electron-main": "npm run test -- -t electron-main"
|
|
133
|
+
},
|
|
134
|
+
"dependencies": {
|
|
135
|
+
"@libp2p/interfaces": "^1.3.0"
|
|
136
|
+
},
|
|
137
|
+
"devDependencies": {
|
|
138
|
+
"@libp2p/peer-id": "^1.1.0",
|
|
139
|
+
"@libp2p/peer-id-factory": "^1.0.0",
|
|
140
|
+
"aegir": "^36.1.3"
|
|
141
|
+
}
|
|
142
|
+
}
|
package/src/index.ts
ADDED
package/src/list.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
2
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
+
import { mapIterable } from './util.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* We can't use PeerIds as list entries because list entries are
|
|
7
|
+
* compared using same-value-zero equality, so this is just
|
|
8
|
+
* a map that stringifies the PeerIds before storing them.
|
|
9
|
+
*
|
|
10
|
+
* PeerIds cache stringified versions of themselves so this
|
|
11
|
+
* should be a cheap operation.
|
|
12
|
+
*/
|
|
13
|
+
export class PeerList {
|
|
14
|
+
private readonly list: string[]
|
|
15
|
+
|
|
16
|
+
constructor (list?: PeerList) {
|
|
17
|
+
this.list = []
|
|
18
|
+
|
|
19
|
+
if (list != null) {
|
|
20
|
+
for (const value of list) {
|
|
21
|
+
this.list.push(value.toString())
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
[Symbol.iterator] () {
|
|
27
|
+
return mapIterable<[number, string], PeerId>(
|
|
28
|
+
this.list.entries(),
|
|
29
|
+
(val) => {
|
|
30
|
+
return peerIdFromString(val[1])
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
concat (list: PeerList) {
|
|
36
|
+
const output = new PeerList(this)
|
|
37
|
+
|
|
38
|
+
for (const value of list) {
|
|
39
|
+
output.push(value)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return output
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
entries (): IterableIterator<[number, PeerId]> {
|
|
46
|
+
return mapIterable<[number, string], [number, PeerId]>(
|
|
47
|
+
this.list.entries(),
|
|
48
|
+
(val) => {
|
|
49
|
+
return [val[0], peerIdFromString(val[1])]
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
every (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): boolean {
|
|
55
|
+
return this.list.every((str, index) => {
|
|
56
|
+
return predicate(peerIdFromString(str), index, this)
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
filter (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerList {
|
|
61
|
+
const output = new PeerList()
|
|
62
|
+
|
|
63
|
+
this.list.forEach((str, index) => {
|
|
64
|
+
const peerId = peerIdFromString(str)
|
|
65
|
+
|
|
66
|
+
if (predicate(peerId, index, this)) {
|
|
67
|
+
output.push(peerId)
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
return output
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
find (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerId | undefined {
|
|
75
|
+
const str = this.list.find((str, index) => {
|
|
76
|
+
return predicate(peerIdFromString(str), index, this)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
if (str == null) {
|
|
80
|
+
return undefined
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return peerIdFromString(str)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
findIndex (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): number {
|
|
87
|
+
return this.list.findIndex((str, index) => {
|
|
88
|
+
return predicate(peerIdFromString(str), index, this)
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
forEach (predicate: (peerId: PeerId, index: number, arr: PeerList) => void): void {
|
|
93
|
+
this.list.forEach((str, index) => {
|
|
94
|
+
predicate(peerIdFromString(str), index, this)
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
includes (peerId: PeerId): boolean {
|
|
99
|
+
return this.list.includes(peerId.toString())
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
indexOf (peerId: PeerId): number {
|
|
103
|
+
return this.list.indexOf(peerId.toString())
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
pop (): PeerId | undefined {
|
|
107
|
+
const str = this.list.pop()
|
|
108
|
+
|
|
109
|
+
if (str == null) {
|
|
110
|
+
return undefined
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return peerIdFromString(str)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
push (...peerIds: PeerId[]) {
|
|
117
|
+
for (const peerId of peerIds) {
|
|
118
|
+
this.list.push(peerId.toString())
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
shift (): PeerId | undefined {
|
|
123
|
+
const str = this.list.shift()
|
|
124
|
+
|
|
125
|
+
if (str == null) {
|
|
126
|
+
return undefined
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return peerIdFromString(str)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
unshift (...peerIds: PeerId[]) {
|
|
133
|
+
let len = this.list.length
|
|
134
|
+
|
|
135
|
+
for (let i = peerIds.length - 1; i > -1; i--) {
|
|
136
|
+
len = this.list.unshift(peerIds[i].toString())
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return len
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
get length () {
|
|
143
|
+
return this.list.length
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function peerList (map?: PeerList): PeerList {
|
|
148
|
+
return new PeerList(map)
|
|
149
|
+
}
|
package/src/map.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
2
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
+
import { mapIterable } from './util.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* We can't use PeerIds as map keys because map keys are
|
|
7
|
+
* compared using same-value-zero equality, so this is just
|
|
8
|
+
* a map that stringifies the PeerIds before storing them.
|
|
9
|
+
*
|
|
10
|
+
* PeerIds cache stringified versions of themselves so this
|
|
11
|
+
* should be a cheap operation.
|
|
12
|
+
*/
|
|
13
|
+
export class PeerMap <T> {
|
|
14
|
+
private readonly map: Map<string, T>
|
|
15
|
+
|
|
16
|
+
constructor (map?: PeerMap<T>) {
|
|
17
|
+
this.map = new Map()
|
|
18
|
+
|
|
19
|
+
if (map != null) {
|
|
20
|
+
for (const [key, value] of map.entries()) {
|
|
21
|
+
this.map.set(key.toString(), value)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
[Symbol.iterator] () {
|
|
27
|
+
return this.entries()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clear () {
|
|
31
|
+
this.map.clear()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
delete (peer: PeerId) {
|
|
35
|
+
this.map.delete(peer.toString())
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
entries (): IterableIterator<[PeerId, T]> {
|
|
39
|
+
return mapIterable<[string, T], [PeerId, T]>(
|
|
40
|
+
this.map.entries(),
|
|
41
|
+
(val) => {
|
|
42
|
+
return [peerIdFromString(val[0]), val[1]]
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
forEach (fn: (value: T, key: PeerId, map: PeerMap<T>) => void): void {
|
|
48
|
+
this.map.forEach((value, key) => {
|
|
49
|
+
fn(value, peerIdFromString(key), this)
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get (peer: PeerId): T | undefined {
|
|
54
|
+
return this.map.get(peer.toString())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
has (peer: PeerId): boolean {
|
|
58
|
+
return this.map.has(peer.toString())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
set (peer: PeerId, value: T) {
|
|
62
|
+
this.map.set(peer.toString(), value)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
keys (): IterableIterator<PeerId> {
|
|
66
|
+
return mapIterable<string, PeerId>(
|
|
67
|
+
this.map.keys(),
|
|
68
|
+
(val) => {
|
|
69
|
+
return peerIdFromString(val)
|
|
70
|
+
}
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
values () {
|
|
75
|
+
return this.map.values()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get size () {
|
|
79
|
+
return this.map.size
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function peerMap <T> (map?: PeerMap<T>): PeerMap<T> {
|
|
84
|
+
return new PeerMap(map)
|
|
85
|
+
}
|
package/src/set.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
2
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
+
import { mapIterable } from './util.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* We can't use PeerIds as set entries because set entries are
|
|
7
|
+
* compared using same-value-zero equality, so this is just
|
|
8
|
+
* a map that stringifies the PeerIds before storing them.
|
|
9
|
+
*
|
|
10
|
+
* PeerIds cache stringified versions of themselves so this
|
|
11
|
+
* should be a cheap operation.
|
|
12
|
+
*/
|
|
13
|
+
export class PeerSet {
|
|
14
|
+
private readonly set: Set<string>
|
|
15
|
+
|
|
16
|
+
constructor (set?: PeerSet) {
|
|
17
|
+
this.set = new Set()
|
|
18
|
+
|
|
19
|
+
if (set != null) {
|
|
20
|
+
for (const key of set) {
|
|
21
|
+
this.set.add(key.toString())
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get size () {
|
|
27
|
+
return this.set.size
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
[Symbol.iterator] () {
|
|
31
|
+
return this.values()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
add (peer: PeerId) {
|
|
35
|
+
this.set.add(peer.toString())
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
clear () {
|
|
39
|
+
this.set.clear()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
delete (peer: PeerId) {
|
|
43
|
+
this.set.delete(peer.toString())
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
entries (): IterableIterator<[PeerId, PeerId]> {
|
|
47
|
+
return mapIterable<[string, string], [PeerId, PeerId]>(
|
|
48
|
+
this.set.entries(),
|
|
49
|
+
(val) => {
|
|
50
|
+
const peerId = peerIdFromString(val[0])
|
|
51
|
+
|
|
52
|
+
return [peerId, peerId]
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
has (peer: PeerId): boolean {
|
|
58
|
+
return this.set.has(peer.toString())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
values () {
|
|
62
|
+
return mapIterable<string, PeerId>(
|
|
63
|
+
this.set.values(),
|
|
64
|
+
(val) => {
|
|
65
|
+
return peerIdFromString(val)
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function peerSet (set?: PeerSet): PeerSet {
|
|
72
|
+
return new PeerSet(set)
|
|
73
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
export function mapIterable <T, R> (iter: IterableIterator<T>, map: (val: T) => R): IterableIterator<R> {
|
|
3
|
+
const iterator: IterableIterator<R> = {
|
|
4
|
+
[Symbol.iterator]: () => {
|
|
5
|
+
return iterator
|
|
6
|
+
},
|
|
7
|
+
next: () => {
|
|
8
|
+
const next = iter.next()
|
|
9
|
+
const val = next.value
|
|
10
|
+
|
|
11
|
+
if (next.done === true || val == null) {
|
|
12
|
+
const result: IteratorReturnResult<any> = {
|
|
13
|
+
done: true,
|
|
14
|
+
value: undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return result
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
done: false,
|
|
22
|
+
value: map(val)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return iterator
|
|
28
|
+
}
|