Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@libp2p/peer-collections

Package Overview
Dependencies
Maintainers
6
Versions
570
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libp2p/peer-collections - npm Package Compare versions

Comparing version 5.2.9 to 6.0.0

6

dist/src/filter.js

@@ -12,9 +12,9 @@ import { createScalableCuckooFilter } from '@libp2p/utils/filters';

has(peerId) {
return this.filter.has(peerId.toBytes());
return this.filter.has(peerId.toMultihash().bytes);
}
add(peerId) {
this.filter.add(peerId.toBytes());
this.filter.add(peerId.toMultihash().bytes);
}
remove(peerId) {
this.filter.remove?.(peerId.toBytes());
this.filter.remove?.(peerId.toMultihash().bytes);
}

@@ -21,0 +21,0 @@ }

@@ -14,5 +14,6 @@ /**

* import { peerList } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -27,7 +28,8 @@ * const list = peerList()

* import { trackedPeerList } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
* import { createLibp2p } from 'libp2p'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -42,5 +44,6 @@ * const list = trackedPeerList({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerMap } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -56,6 +59,7 @@ * const map = peerMap<string>()

* import { createLibp2p } from 'libp2p'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -70,5 +74,6 @@ * const map = trackedPeerMap({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerSet } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -84,6 +89,7 @@ * const set = peerSet()

* import { createLibp2p } from 'libp2p'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -98,5 +104,6 @@ * const set = trackedPeerSet({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerFilter } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -103,0 +110,0 @@ * const filter = peerFilter(1024)

@@ -14,5 +14,6 @@ /**

* import { peerList } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -27,7 +28,8 @@ * const list = peerList()

* import { trackedPeerList } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
* import { createLibp2p } from 'libp2p'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -42,5 +44,6 @@ * const list = trackedPeerList({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerMap } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -56,6 +59,7 @@ * const map = peerMap<string>()

* import { createLibp2p } from 'libp2p'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -70,5 +74,6 @@ * const map = trackedPeerMap({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerSet } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -84,6 +89,7 @@ * const set = peerSet()

* import { createLibp2p } from 'libp2p'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -98,5 +104,6 @@ * const set = trackedPeerSet({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerFilter } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -103,0 +110,0 @@ * const filter = peerFilter(1024)

@@ -1,2 +0,1 @@

import { peerIdFromString } from '@libp2p/peer-id';
import { mapIterable } from './util.js';

@@ -26,3 +25,3 @@ /**

for (const value of list) {
this.list.push(value.toString());
this.list.push(value);
}

@@ -33,3 +32,3 @@ }

return mapIterable(this.list.entries(), (val) => {
return peerIdFromString(val[1]);
return val[1];
});

@@ -46,8 +45,8 @@ }

return mapIterable(this.list.entries(), (val) => {
return [val[0], peerIdFromString(val[1])];
return [val[0], val[1]];
});
}
every(predicate) {
return this.list.every((str, index) => {
return predicate(peerIdFromString(str), index, this);
return this.list.every((peerId, index) => {
return predicate(peerId, index, this);
});

@@ -57,4 +56,3 @@ }

const output = new PeerList();
this.list.forEach((str, index) => {
const peerId = peerIdFromString(str);
this.list.forEach((peerId, index) => {
if (predicate(peerId, index, this)) {

@@ -67,44 +65,44 @@ output.push(peerId);

find(predicate) {
const str = this.list.find((str, index) => {
return predicate(peerIdFromString(str), index, this);
const peerId = this.list.find((peerId, index) => {
return predicate(peerId, index, this);
});
if (str == null) {
if (peerId == null) {
return undefined;
}
return peerIdFromString(str);
return peerId;
}
findIndex(predicate) {
return this.list.findIndex((str, index) => {
return predicate(peerIdFromString(str), index, this);
return this.list.findIndex((peerId, index) => {
return predicate(peerId, index, this);
});
}
forEach(predicate) {
this.list.forEach((str, index) => {
predicate(peerIdFromString(str), index, this);
this.list.forEach((peerId, index) => {
predicate(peerId, index, this);
});
}
includes(peerId) {
return this.list.includes(peerId.toString());
return this.includes(peerId);
}
indexOf(peerId) {
return this.list.indexOf(peerId.toString());
return this.list.findIndex(id => id.equals(peerId));
}
pop() {
const str = this.list.pop();
if (str == null) {
const peerId = this.list.pop();
if (peerId == null) {
return undefined;
}
return peerIdFromString(str);
return peerId;
}
push(...peerIds) {
for (const peerId of peerIds) {
this.list.push(peerId.toString());
this.list.push(peerId);
}
}
shift() {
const str = this.list.shift();
if (str == null) {
const peerId = this.list.shift();
if (peerId == null) {
return undefined;
}
return peerIdFromString(str);
return peerId;
}

@@ -114,3 +112,3 @@ unshift(...peerIds) {

for (let i = peerIds.length - 1; i > -1; i--) {
len = this.list.unshift(peerIds[i].toString());
len = this.list.unshift(peerIds[i]);
}

@@ -117,0 +115,0 @@ return len;

@@ -1,2 +0,1 @@

import { peerIdFromString } from '@libp2p/peer-id';
import { mapIterable } from './util.js';

@@ -26,3 +25,3 @@ /**

for (const [key, value] of map.entries()) {
this.map.set(key.toString(), value);
this.map.set(key.toString(), { key, value });
}

@@ -42,3 +41,3 @@ }

return mapIterable(this.map.entries(), (val) => {
return [peerIdFromString(val[0]), val[1]];
return [val[1].key, val[1].value];
});

@@ -48,7 +47,7 @@ }

this.map.forEach((value, key) => {
fn(value, peerIdFromString(key), this);
fn(value.value, value.key, this);
});
}
get(peer) {
return this.map.get(peer.toString());
return this.map.get(peer.toString())?.value;
}

@@ -59,11 +58,11 @@ has(peer) {

set(peer, value) {
this.map.set(peer.toString(), value);
this.map.set(peer.toString(), { key: peer, value });
}
keys() {
return mapIterable(this.map.keys(), (val) => {
return peerIdFromString(val);
return mapIterable(this.map.values(), (val) => {
return val.key;
});
}
values() {
return this.map.values();
return mapIterable(this.map.values(), (val) => val.value);
}

@@ -70,0 +69,0 @@ get size() {

@@ -1,3 +0,2 @@

import { peerIdFromString } from '@libp2p/peer-id';
import { mapIterable } from './util.js';
import { mapIterable, peerIdFromString } from './util.js';
/**

@@ -53,4 +52,4 @@ * We can't use PeerIds as set entries because set entries are

this.set.forEach((str) => {
const id = peerIdFromString(str);
predicate(id, id, this);
const peerId = peerIdFromString(str);
predicate(peerId, peerId, this);
});

@@ -57,0 +56,0 @@ }

@@ -0,1 +1,2 @@

import type { PeerId } from '@libp2p/interface';
/**

@@ -5,2 +6,3 @@ * Calls the passed map function on every entry of the passed iterable iterator

export declare function mapIterable<T, R>(iter: IterableIterator<T>, map: (val: T) => R): IterableIterator<R>;
export declare function peerIdFromString(str: string): PeerId;
//# sourceMappingURL=util.d.ts.map

@@ -0,1 +1,4 @@

import { peerIdFromMultihash } from '@libp2p/peer-id';
import { base58btc } from 'multiformats/bases/base58';
import * as Digest from 'multiformats/hashes/digest';
/**

@@ -27,2 +30,6 @@ * Calls the passed map function on every entry of the passed iterable iterator

}
export function peerIdFromString(str) {
const multihash = Digest.decode(base58btc.decode(`z${str}`));
return peerIdFromMultihash(multihash);
}
//# sourceMappingURL=util.js.map
{
"name": "@libp2p/peer-collections",
"version": "5.2.9",
"version": "6.0.0",
"description": "Stores values against a peer id",

@@ -57,8 +57,9 @@ "license": "Apache-2.0 OR MIT",

"dependencies": {
"@libp2p/interface": "^1.7.0",
"@libp2p/peer-id": "^4.2.4",
"@libp2p/utils": "^5.4.9"
"@libp2p/interface": "^2.0.0",
"@libp2p/peer-id": "^5.0.0",
"@libp2p/utils": "^6.0.0",
"multiformats": "^13.2.2"
},
"devDependencies": {
"@libp2p/peer-id-factory": "^4.2.4",
"@libp2p/crypto": "^5.0.0",
"@types/sinon": "^17.0.3",

@@ -65,0 +66,0 @@ "aegir": "^44.0.1",

@@ -37,5 +37,6 @@ # @libp2p/peer-collections

import { peerList } from '@libp2p/peer-collections'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -50,7 +51,8 @@ const list = peerList()

import { trackedPeerList } from '@libp2p/peer-collections'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
import { createLibp2p } from 'libp2p'
const libp2p = await createLibp2p()
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -65,5 +67,6 @@ const list = trackedPeerList({ name: 'my_metric_name', metrics: libp2p.metrics })

import { peerMap } from '@libp2p/peer-collections'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -79,6 +82,7 @@ const map = peerMap<string>()

import { createLibp2p } from 'libp2p'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
const libp2p = await createLibp2p()
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -93,5 +97,6 @@ const map = trackedPeerMap({ name: 'my_metric_name', metrics: libp2p.metrics })

import { peerSet } from '@libp2p/peer-collections'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -107,6 +112,7 @@ const set = peerSet()

import { createLibp2p } from 'libp2p'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
const libp2p = await createLibp2p()
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -121,5 +127,6 @@ const set = trackedPeerSet({ name: 'my_metric_name', metrics: libp2p.metrics })

import { peerFilter } from '@libp2p/peer-collections'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
const peerId = await createEd25519PeerId()
const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))

@@ -126,0 +133,0 @@ const filter = peerFilter(1024)

@@ -17,11 +17,11 @@ import { createScalableCuckooFilter } from '@libp2p/utils/filters'

has (peerId: PeerId): boolean {
return this.filter.has(peerId.toBytes())
return this.filter.has(peerId.toMultihash().bytes)
}
add (peerId: PeerId): void {
this.filter.add(peerId.toBytes())
this.filter.add(peerId.toMultihash().bytes)
}
remove (peerId: PeerId): void {
this.filter.remove?.(peerId.toBytes())
this.filter.remove?.(peerId.toMultihash().bytes)
}

@@ -28,0 +28,0 @@ }

@@ -14,5 +14,6 @@ /**

* import { peerList } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -27,7 +28,8 @@ * const list = peerList()

* import { trackedPeerList } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
* import { createLibp2p } from 'libp2p'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -42,5 +44,6 @@ * const list = trackedPeerList({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerMap } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -56,6 +59,7 @@ * const map = peerMap<string>()

* import { createLibp2p } from 'libp2p'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -70,5 +74,6 @@ * const map = trackedPeerMap({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerSet } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -84,6 +89,7 @@ * const set = peerSet()

* import { createLibp2p } from 'libp2p'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const libp2p = await createLibp2p()
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -98,5 +104,6 @@ * const set = trackedPeerSet({ name: 'my_metric_name', metrics: libp2p.metrics })

* import { peerFilter } from '@libp2p/peer-collections'
* import { createEd25519PeerId } from '@libp2p/peer-id-factory'
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { generateKeyPair } from '@libp2p/crypto/keys'
*
* const peerId = await createEd25519PeerId()
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
*

@@ -103,0 +110,0 @@ * const filter = peerFilter(1024)

@@ -1,2 +0,1 @@

import { peerIdFromString } from '@libp2p/peer-id'
import { mapIterable } from './util.js'

@@ -23,3 +22,3 @@ import type { PeerId } from '@libp2p/interface'

export class PeerList {
private list: string[]
private list: PeerId[]

@@ -31,3 +30,3 @@ constructor (list?: PeerList | Iterable<PeerId>) {

for (const value of list) {
this.list.push(value.toString())
this.list.push(value)
}

@@ -38,6 +37,6 @@ }

[Symbol.iterator] (): IterableIterator<PeerId> {
return mapIterable<[number, string], PeerId>(
return mapIterable<[number, PeerId], PeerId>(
this.list.entries(),
(val) => {
return peerIdFromString(val[1])
return val[1]
}

@@ -58,6 +57,6 @@ )

entries (): IterableIterator<[number, PeerId]> {
return mapIterable<[number, string], [number, PeerId]>(
return mapIterable<[number, PeerId], [number, PeerId]>(
this.list.entries(),
(val) => {
return [val[0], peerIdFromString(val[1])]
return [val[0], val[1]]
}

@@ -68,4 +67,4 @@ )

every (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): boolean {
return this.list.every((str, index) => {
return predicate(peerIdFromString(str), index, this)
return this.list.every((peerId, index) => {
return predicate(peerId, index, this)
})

@@ -77,5 +76,3 @@ }

this.list.forEach((str, index) => {
const peerId = peerIdFromString(str)
this.list.forEach((peerId, index) => {
if (predicate(peerId, index, this)) {

@@ -90,16 +87,16 @@ output.push(peerId)

find (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerId | undefined {
const str = this.list.find((str, index) => {
return predicate(peerIdFromString(str), index, this)
const peerId = this.list.find((peerId, index) => {
return predicate(peerId, index, this)
})
if (str == null) {
if (peerId == null) {
return undefined
}
return peerIdFromString(str)
return peerId
}
findIndex (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): number {
return this.list.findIndex((str, index) => {
return predicate(peerIdFromString(str), index, this)
return this.list.findIndex((peerId, index) => {
return predicate(peerId, index, this)
})

@@ -109,4 +106,4 @@ }

forEach (predicate: (peerId: PeerId, index: number, arr: PeerList) => void): void {
this.list.forEach((str, index) => {
predicate(peerIdFromString(str), index, this)
this.list.forEach((peerId, index) => {
predicate(peerId, index, this)
})

@@ -116,17 +113,17 @@ }

includes (peerId: PeerId): boolean {
return this.list.includes(peerId.toString())
return this.includes(peerId)
}
indexOf (peerId: PeerId): number {
return this.list.indexOf(peerId.toString())
return this.list.findIndex(id => id.equals(peerId))
}
pop (): PeerId | undefined {
const str = this.list.pop()
const peerId = this.list.pop()
if (str == null) {
if (peerId == null) {
return undefined
}
return peerIdFromString(str)
return peerId
}

@@ -136,3 +133,3 @@

for (const peerId of peerIds) {
this.list.push(peerId.toString())
this.list.push(peerId)
}

@@ -142,9 +139,9 @@ }

shift (): PeerId | undefined {
const str = this.list.shift()
const peerId = this.list.shift()
if (str == null) {
if (peerId == null) {
return undefined
}
return peerIdFromString(str)
return peerId
}

@@ -156,3 +153,3 @@

for (let i = peerIds.length - 1; i > -1; i--) {
len = this.list.unshift(peerIds[i].toString())
len = this.list.unshift(peerIds[i])
}

@@ -159,0 +156,0 @@

@@ -1,2 +0,1 @@

import { peerIdFromString } from '@libp2p/peer-id'
import { mapIterable } from './util.js'

@@ -23,3 +22,3 @@ import type { PeerId } from '@libp2p/interface'

export class PeerMap <T> {
private readonly map: Map<string, T>
private readonly map: Map<string, { key: PeerId, value: T }>

@@ -31,3 +30,3 @@ constructor (map?: PeerMap<T>) {

for (const [key, value] of map.entries()) {
this.map.set(key.toString(), value)
this.map.set(key.toString(), { key, value })
}

@@ -50,6 +49,6 @@ }

entries (): IterableIterator<[PeerId, T]> {
return mapIterable<[string, T], [PeerId, T]>(
return mapIterable<[string, { key: PeerId, value: T }], [PeerId, T]>(
this.map.entries(),
(val) => {
return [peerIdFromString(val[0]), val[1]]
return [val[1].key, val[1].value]
}

@@ -61,3 +60,3 @@ )

this.map.forEach((value, key) => {
fn(value, peerIdFromString(key), this)
fn(value.value, value.key, this)
})

@@ -67,3 +66,3 @@ }

get (peer: PeerId): T | undefined {
return this.map.get(peer.toString())
return this.map.get(peer.toString())?.value
}

@@ -76,10 +75,10 @@

set (peer: PeerId, value: T): void {
this.map.set(peer.toString(), value)
this.map.set(peer.toString(), { key: peer, value })
}
keys (): IterableIterator<PeerId> {
return mapIterable<string, PeerId>(
this.map.keys(),
return mapIterable<{ key: PeerId, value: T }, PeerId>(
this.map.values(),
(val) => {
return peerIdFromString(val)
return val.key
}

@@ -90,3 +89,3 @@ )

values (): IterableIterator<T> {
return this.map.values()
return mapIterable(this.map.values(), (val) => val.value)
}

@@ -93,0 +92,0 @@

@@ -1,3 +0,2 @@

import { peerIdFromString } from '@libp2p/peer-id'
import { mapIterable } from './util.js'
import { mapIterable, peerIdFromString } from './util.js'
import type { PeerId } from '@libp2p/interface'

@@ -68,5 +67,5 @@

this.set.forEach((str) => {
const id = peerIdFromString(str)
const peerId = peerIdFromString(str)
predicate(id, id, this)
predicate(peerId, peerId, this)
})

@@ -73,0 +72,0 @@ }

@@ -0,1 +1,6 @@

import { peerIdFromMultihash } from '@libp2p/peer-id'
import { base58btc } from 'multiformats/bases/base58'
import * as Digest from 'multiformats/hashes/digest'
import type { PeerId } from '@libp2p/interface'
/**

@@ -31,1 +36,6 @@ * Calls the passed map function on every entry of the passed iterable iterator

}
export function peerIdFromString (str: string): PeerId {
const multihash = Digest.decode(base58btc.decode(`z${str}`))
return peerIdFromMultihash(multihash)
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc