New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@libp2p/peer-store

Package Overview
Dependencies
Maintainers
6
Versions
656
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libp2p/peer-store - npm Package Compare versions

Comparing version 8.1.4 to 8.2.0

6

dist/src/index.d.ts
import type { Libp2pEvents } from '@libp2p/interface-libp2p';
import type { PeerId } from '@libp2p/interface-peer-id';
import type { PeerStore, Peer, PeerData } from '@libp2p/interface-peer-store';
import type { PeerStore, Peer, PeerData, PeerQuery } from '@libp2p/interface-peer-store';
import type { EventEmitter } from '@libp2p/interfaces/events';

@@ -30,4 +30,4 @@ import type { Multiaddr } from '@multiformats/multiaddr';

constructor(components: PersistentPeerStoreComponents, init?: PersistentPeerStoreInit);
forEach(fn: (peer: Peer) => void): Promise<void>;
all(): Promise<Peer[]>;
forEach(fn: (peer: Peer) => void, query?: PeerQuery): Promise<void>;
all(query?: PeerQuery): Promise<Peer[]>;
delete(peerId: PeerId): Promise<void>;

@@ -34,0 +34,0 @@ has(peerId: PeerId): Promise<boolean>;

import { logger } from '@libp2p/logger';
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record';
import all from 'it-all';
import { PersistentStore } from './store.js';

@@ -17,3 +18,3 @@ const log = logger('libp2p:peer-store');

}
async forEach(fn) {
async forEach(fn, query) {
log.trace('forEach await read lock');

@@ -23,3 +24,3 @@ const release = await this.store.lock.readLock();

try {
for await (const peer of this.store.all()) {
for await (const peer of this.store.all(query)) {
fn(peer);

@@ -33,3 +34,3 @@ }

}
async all() {
async all(query) {
log.trace('all await read lock');

@@ -39,7 +40,3 @@ const release = await this.store.lock.readLock();

try {
const output = [];
for await (const peer of this.store.all()) {
output.push(peer);
}
return output;
return await all(this.store.all(query));
}

@@ -46,0 +43,0 @@ finally {

@@ -5,3 +5,3 @@ import { type Mortice } from 'mortice';

import type { PeerId } from '@libp2p/interface-peer-id';
import type { Peer, PeerData } from '@libp2p/interface-peer-store';
import type { Peer, PeerData, PeerQuery } from '@libp2p/interface-peer-store';
/**

@@ -26,4 +26,4 @@ * Event detail emitted when peer data changes

merge(peerId: PeerId, data: PeerData): Promise<PeerUpdate>;
all(): AsyncGenerator<Peer, void, unknown>;
all(query?: PeerQuery): AsyncGenerator<Peer, void, unknown>;
}
//# sourceMappingURL=store.d.ts.map
import { CodeError } from '@libp2p/interfaces/errors';
import { PeerMap } from '@libp2p/peer-collections';
import { peerIdFromBytes } from '@libp2p/peer-id';

@@ -11,2 +12,29 @@ import mortice from 'mortice';

import { toPeerPB } from './utils/to-peer-pb.js';
function decodePeer(key, value, cache) {
// /peers/${peer-id-as-libp2p-key-cid-string-in-base-32}
const base32Str = key.toString().split('/')[2];
const buf = base32.decode(base32Str);
const peerId = peerIdFromBytes(buf);
const cached = cache.get(peerId);
if (cached != null) {
return cached;
}
const peer = bytesToPeer(peerId, value);
cache.set(peerId, peer);
return peer;
}
function mapQuery(query, cache) {
if (query == null) {
return {};
}
return {
prefix: NAMESPACE_COMMON,
filters: (query.filters ?? []).map(fn => ({ key, value }) => {
return fn(decodePeer(key, value, cache));
}),
orders: (query.orders ?? []).map(fn => (a, b) => {
return fn(decodePeer(a.key, a.value, cache), decodePeer(b.key, b.value, cache));
})
};
}
export class PersistentStore {

@@ -62,15 +90,11 @@ peerId;

}
async *all() {
for await (const { key, value } of this.datastore.query({
prefix: NAMESPACE_COMMON
})) {
// /peers/${peer-id-as-libp2p-key-cid-string-in-base-32}
const base32Str = key.toString().split('/')[2];
const buf = base32.decode(base32Str);
const peerId = peerIdFromBytes(buf);
if (peerId.equals(this.peerId)) {
async *all(query) {
const peerCache = new PeerMap();
for await (const { key, value } of this.datastore.query(mapQuery(query ?? {}, peerCache))) {
const peer = decodePeer(key, value, peerCache);
if (peer.id.equals(this.peerId)) {
// Skip self peer if present
continue;
}
yield bytesToPeer(peerId, value);
yield peer;
}

@@ -81,3 +105,3 @@ }

const existingBuf = await this.datastore.get(peerIdToDatastoreKey(peerId));
const existingPeer = await bytesToPeer(peerId, existingBuf);
const existingPeer = bytesToPeer(peerId, existingBuf);
return {

@@ -99,3 +123,3 @@ existingBuf,

return {
peer: await bytesToPeer(peerId, buf),
peer: bytesToPeer(peerId, buf),
previous: existingPeer,

@@ -107,3 +131,3 @@ updated: false

return {
peer: await bytesToPeer(peerId, buf),
peer: bytesToPeer(peerId, buf),
previous: existingPeer,

@@ -110,0 +134,0 @@ updated: true

import type { PeerId } from '@libp2p/interface-peer-id';
import type { Peer } from '@libp2p/interface-peer-store';
export declare function bytesToPeer(peerId: PeerId, buf: Uint8Array): Promise<Peer>;
export declare function bytesToPeer(peerId: PeerId, buf: Uint8Array): Peer;
//# sourceMappingURL=bytes-to-peer.d.ts.map

@@ -1,9 +0,11 @@

import { unmarshalPublicKey } from '@libp2p/crypto/keys';
import { createFromPubKey } from '@libp2p/peer-id-factory';
import { peerIdFromPeerId } from '@libp2p/peer-id';
import { multiaddr } from '@multiformats/multiaddr';
import { Peer as PeerPB } from '../pb/peer.js';
export async function bytesToPeer(peerId, buf) {
export function bytesToPeer(peerId, buf) {
const peer = PeerPB.decode(buf);
if (peer.publicKey != null && peerId.publicKey == null) {
peerId = await createFromPubKey(unmarshalPublicKey(peer.publicKey));
peerId = peerIdFromPeerId({
...peerId,
publicKey: peerId.publicKey
});
}

@@ -10,0 +12,0 @@ const tags = new Map();

{
"name": "@libp2p/peer-store",
"version": "8.1.4",
"version": "8.2.0",
"description": "Stores information about peers libp2p knows on the network",

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

"dependencies": {
"@libp2p/crypto": "^1.0.15",
"@libp2p/interface-libp2p": "^3.1.0",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-store": "^2.0.1",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.7",
"@libp2p/peer-collections": "^3.0.1",
"@libp2p/peer-id": "^2.0.0",

@@ -159,2 +159,3 @@ "@libp2p/peer-id-factory": "^2.0.0",

"interface-datastore": "^8.0.0",
"it-all": "^3.0.2",
"mortice": "^3.0.1",

@@ -161,0 +162,0 @@ "multiformats": "^11.0.0",

import { logger } from '@libp2p/logger'
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
import all from 'it-all'
import { PersistentStore, type PeerUpdate } from './store.js'
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerStore, Peer, PeerData } from '@libp2p/interface-peer-store'
import type { PeerStore, Peer, PeerData, PeerQuery } from '@libp2p/interface-peer-store'
import type { EventEmitter } from '@libp2p/interfaces/events'

@@ -44,3 +45,3 @@ import type { Multiaddr } from '@multiformats/multiaddr'

async forEach (fn: (peer: Peer) => void): Promise<void> {
async forEach (fn: (peer: Peer,) => void, query?: PeerQuery): Promise<void> {
log.trace('forEach await read lock')

@@ -51,3 +52,3 @@ const release = await this.store.lock.readLock()

try {
for await (const peer of this.store.all()) {
for await (const peer of this.store.all(query)) {
fn(peer)

@@ -61,3 +62,3 @@ }

async all (): Promise<Peer[]> {
async all (query?: PeerQuery): Promise<Peer[]> {
log.trace('all await read lock')

@@ -68,9 +69,3 @@ const release = await this.store.lock.readLock()

try {
const output: Peer[] = []
for await (const peer of this.store.all()) {
output.push(peer)
}
return output
return await all(this.store.all(query))
} finally {

@@ -77,0 +72,0 @@ log.trace('all release read lock')

import { CodeError } from '@libp2p/interfaces/errors'
import { PeerMap } from '@libp2p/peer-collections'
import { peerIdFromBytes } from '@libp2p/peer-id'

@@ -14,4 +15,4 @@ import mortice, { type Mortice } from 'mortice'

import type { PeerId } from '@libp2p/interface-peer-id'
import type { Peer, PeerData } from '@libp2p/interface-peer-store'
import type { Datastore } from 'interface-datastore'
import type { Peer, PeerData, PeerQuery } from '@libp2p/interface-peer-store'
import type { Datastore, Key, Query } from 'interface-datastore'

@@ -25,2 +26,37 @@ /**

function decodePeer (key: Key, value: Uint8Array, cache: PeerMap<Peer>): Peer {
// /peers/${peer-id-as-libp2p-key-cid-string-in-base-32}
const base32Str = key.toString().split('/')[2]
const buf = base32.decode(base32Str)
const peerId = peerIdFromBytes(buf)
const cached = cache.get(peerId)
if (cached != null) {
return cached
}
const peer = bytesToPeer(peerId, value)
cache.set(peerId, peer)
return peer
}
function mapQuery (query: PeerQuery, cache: PeerMap<Peer>): Query {
if (query == null) {
return {}
}
return {
prefix: NAMESPACE_COMMON,
filters: (query.filters ?? []).map(fn => ({ key, value }) => {
return fn(decodePeer(key, value, cache))
}),
orders: (query.orders ?? []).map(fn => (a, b) => {
return fn(decodePeer(a.key, a.value, cache), decodePeer(b.key, b.value, cache))
})
}
}
export class PersistentStore {

@@ -101,12 +137,9 @@ private readonly peerId: PeerId

async * all (): AsyncGenerator<Peer, void, unknown> {
for await (const { key, value } of this.datastore.query({
prefix: NAMESPACE_COMMON
})) {
// /peers/${peer-id-as-libp2p-key-cid-string-in-base-32}
const base32Str = key.toString().split('/')[2]
const buf = base32.decode(base32Str)
const peerId = peerIdFromBytes(buf)
async * all (query?: PeerQuery): AsyncGenerator<Peer, void, unknown> {
const peerCache = new PeerMap<Peer>()
if (peerId.equals(this.peerId)) {
for await (const { key, value } of this.datastore.query(mapQuery(query ?? {}, peerCache))) {
const peer = decodePeer(key, value, peerCache)
if (peer.id.equals(this.peerId)) {
// Skip self peer if present

@@ -116,3 +149,3 @@ continue

yield bytesToPeer(peerId, value)
yield peer
}

@@ -124,3 +157,3 @@ }

const existingBuf = await this.datastore.get(peerIdToDatastoreKey(peerId))
const existingPeer = await bytesToPeer(peerId, existingBuf)
const existingPeer = bytesToPeer(peerId, existingBuf)

@@ -145,3 +178,3 @@ return {

return {
peer: await bytesToPeer(peerId, buf),
peer: bytesToPeer(peerId, buf),
previous: existingPeer,

@@ -155,3 +188,3 @@ updated: false

return {
peer: await bytesToPeer(peerId, buf),
peer: bytesToPeer(peerId, buf),
previous: existingPeer,

@@ -158,0 +191,0 @@ updated: true

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

import { unmarshalPublicKey } from '@libp2p/crypto/keys'
import { createFromPubKey } from '@libp2p/peer-id-factory'
import { peerIdFromPeerId } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'

@@ -8,7 +7,10 @@ import { Peer as PeerPB } from '../pb/peer.js'

export async function bytesToPeer (peerId: PeerId, buf: Uint8Array): Promise<Peer> {
export function bytesToPeer (peerId: PeerId, buf: Uint8Array): Peer {
const peer = PeerPB.decode(buf)
if (peer.publicKey != null && peerId.publicKey == null) {
peerId = await createFromPubKey(unmarshalPublicKey(peer.publicKey))
peerId = peerIdFromPeerId({
...peerId,
publicKey: peerId.publicKey
})
}

@@ -15,0 +17,0 @@

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

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