@libp2p/peer-store 8.1.1 → 8.1.2
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/index.min.js +9 -9
- package/dist/src/index.d.ts +4 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +18 -22
- package/dist/src/index.js.map +1 -1
- package/dist/src/store.d.ts +3 -3
- package/dist/src/store.d.ts.map +1 -1
- package/dist/src/store.js +43 -45
- package/dist/src/store.js.map +1 -1
- package/dist/src/utils/bytes-to-peer.d.ts +1 -1
- package/dist/src/utils/bytes-to-peer.d.ts.map +1 -1
- package/dist/src/utils/bytes-to-peer.js +2 -2
- package/dist/src/utils/bytes-to-peer.js.map +1 -1
- package/dist/src/utils/dedupe-addresses.d.ts +2 -2
- package/dist/src/utils/dedupe-addresses.d.ts.map +1 -1
- package/dist/src/utils/dedupe-addresses.js +1 -1
- package/dist/src/utils/dedupe-addresses.js.map +1 -1
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts +1 -1
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts.map +1 -1
- package/dist/src/utils/peer-data-to-datastore-peer.js +1 -1
- package/dist/src/utils/peer-data-to-datastore-peer.js.map +1 -1
- package/dist/src/utils/peer-id-to-datastore-key.d.ts +1 -1
- package/dist/src/utils/peer-id-to-datastore-key.d.ts.map +1 -1
- package/dist/src/utils/peer-id-to-datastore-key.js +2 -2
- package/dist/src/utils/peer-id-to-datastore-key.js.map +1 -1
- package/dist/src/utils/to-peer-pb.d.ts +2 -2
- package/dist/src/utils/to-peer-pb.d.ts.map +1 -1
- package/dist/src/utils/to-peer-pb.js +1 -1
- package/dist/src/utils/to-peer-pb.js.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +7 -7
- package/src/store.ts +15 -15
- package/src/utils/bytes-to-peer.ts +3 -3
- package/src/utils/dedupe-addresses.ts +4 -4
- package/src/utils/peer-data-to-datastore-peer.ts +3 -3
- package/src/utils/peer-id-to-datastore-key.ts +2 -2
- package/src/utils/to-peer-pb.ts +6 -6
package/src/store.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
1
2
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
3
|
+
import mortice, { type Mortice } from 'mortice'
|
|
2
4
|
import { base32 } from 'multiformats/bases/base32'
|
|
3
|
-
import { Peer as PeerPB } from './pb/peer.js'
|
|
4
|
-
import type { Peer, PeerData } from '@libp2p/interface-peer-store'
|
|
5
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
6
|
-
import type { AddressFilter, PersistentPeerStoreComponents, PersistentPeerStoreInit } from './index.js'
|
|
7
5
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
8
|
-
import { NAMESPACE_COMMON, peerIdToDatastoreKey } from './utils/peer-id-to-datastore-key.js'
|
|
9
|
-
import { bytesToPeer } from './utils/bytes-to-peer.js'
|
|
10
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
11
6
|
import { codes } from './errors.js'
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
7
|
+
import { Peer as PeerPB } from './pb/peer.js'
|
|
8
|
+
import { bytesToPeer } from './utils/bytes-to-peer.js'
|
|
9
|
+
import { NAMESPACE_COMMON, peerIdToDatastoreKey } from './utils/peer-id-to-datastore-key.js'
|
|
15
10
|
import { toPeerPB } from './utils/to-peer-pb.js'
|
|
11
|
+
import type { AddressFilter, PersistentPeerStoreComponents, PersistentPeerStoreInit } from './index.js'
|
|
12
|
+
import type { PeerUpdate as PeerUpdateExternal } from '@libp2p/interface-libp2p'
|
|
13
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
14
|
+
import type { Peer, PeerData } from '@libp2p/interface-peer-store'
|
|
15
|
+
import type { Datastore } from 'interface-datastore'
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Event detail emitted when peer data changes
|
|
@@ -38,7 +38,7 @@ export class PersistentStore {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
async has (peerId: PeerId): Promise<boolean> {
|
|
41
|
-
return
|
|
41
|
+
return this.datastore.has(peerIdToDatastoreKey(peerId))
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
async delete (peerId: PeerId): Promise<void> {
|
|
@@ -52,7 +52,7 @@ export class PersistentStore {
|
|
|
52
52
|
async load (peerId: PeerId): Promise<Peer> {
|
|
53
53
|
const buf = await this.datastore.get(peerIdToDatastoreKey(peerId))
|
|
54
54
|
|
|
55
|
-
return
|
|
55
|
+
return bytesToPeer(peerId, buf)
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
async save (peerId: PeerId, data: PeerData): Promise<PeerUpdate> {
|
|
@@ -65,7 +65,7 @@ export class PersistentStore {
|
|
|
65
65
|
addressFilter: this.addressFilter
|
|
66
66
|
})
|
|
67
67
|
|
|
68
|
-
return
|
|
68
|
+
return this.#saveIfDifferent(peerId, peerPb, existingBuf, existingPeer)
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
async patch (peerId: PeerId, data: Partial<PeerData>): Promise<PeerUpdate> {
|
|
@@ -79,7 +79,7 @@ export class PersistentStore {
|
|
|
79
79
|
existingPeer
|
|
80
80
|
})
|
|
81
81
|
|
|
82
|
-
return
|
|
82
|
+
return this.#saveIfDifferent(peerId, peerPb, existingBuf, existingPeer)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
async merge (peerId: PeerId, data: PeerData): Promise<PeerUpdate> {
|
|
@@ -93,7 +93,7 @@ export class PersistentStore {
|
|
|
93
93
|
existingPeer
|
|
94
94
|
})
|
|
95
95
|
|
|
96
|
-
return
|
|
96
|
+
return this.#saveIfDifferent(peerId, peerPb, existingBuf, existingPeer)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async * all (): AsyncGenerator<Peer, void, unknown> {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
2
|
+
import { createFromPubKey } from '@libp2p/peer-id-factory'
|
|
1
3
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
2
4
|
import { Peer as PeerPB } from '../pb/peer.js'
|
|
3
|
-
import type { Peer, Tag } from '@libp2p/interface-peer-store'
|
|
4
|
-
import { createFromPubKey } from '@libp2p/peer-id-factory'
|
|
5
|
-
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
6
5
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
6
|
+
import type { Peer, Tag } from '@libp2p/interface-peer-store'
|
|
7
7
|
|
|
8
8
|
export async function bytesToPeer (peerId: PeerId, buf: Uint8Array): Promise<Peer> {
|
|
9
9
|
const peer = PeerPB.decode(buf)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
1
2
|
import { isMultiaddr, multiaddr } from '@multiformats/multiaddr'
|
|
2
|
-
import
|
|
3
|
-
import type { Address } from '@libp2p/interface-peer-store'
|
|
3
|
+
import { codes } from '../errors.js'
|
|
4
4
|
import type { AddressFilter } from '../index.js'
|
|
5
|
+
import type { Address as AddressPB } from '../pb/peer.js'
|
|
5
6
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
6
|
-
import {
|
|
7
|
-
import { codes } from '../errors.js'
|
|
7
|
+
import type { Address } from '@libp2p/interface-peer-store'
|
|
8
8
|
|
|
9
9
|
export async function dedupeFilterAndSortAddresses (peerId: PeerId, filter: AddressFilter, addresses: Array<Address | AddressPB | undefined>): Promise<AddressPB[]> {
|
|
10
10
|
const addressMap = new Map<string, Address>()
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
|
|
2
2
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
|
-
import { codes } from '../errors.js'
|
|
4
3
|
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
5
|
-
import type { Peer as PeerPB } from '../pb/peer.js'
|
|
6
4
|
import { equals as uint8arrayEquals } from 'uint8arrays/equals'
|
|
7
|
-
import
|
|
5
|
+
import { codes } from '../errors.js'
|
|
6
|
+
import type { Peer as PeerPB } from '../pb/peer.js'
|
|
8
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
+
import type { PeerData } from '@libp2p/interface-peer-store'
|
|
9
9
|
|
|
10
10
|
export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
11
11
|
if (data == null) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { isPeerId, type PeerId } from '@libp2p/interface-peer-id'
|
|
1
2
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
|
-
import { codes } from '../errors.js'
|
|
3
3
|
import { Key } from 'interface-datastore/key'
|
|
4
|
-
import {
|
|
4
|
+
import { codes } from '../errors.js'
|
|
5
5
|
|
|
6
6
|
export const NAMESPACE_COMMON = '/peers/'
|
|
7
7
|
|
package/src/utils/to-peer-pb.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
2
|
-
import type { Address, Peer, PeerData, TagOptions } from '@libp2p/interface-peer-store'
|
|
3
1
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
4
|
-
import { codes } from '../errors.js'
|
|
5
2
|
import { equals as uint8arrayEquals } from 'uint8arrays/equals'
|
|
3
|
+
import { codes } from '../errors.js'
|
|
4
|
+
import { dedupeFilterAndSortAddresses } from './dedupe-addresses.js'
|
|
6
5
|
import type { AddressFilter } from '../index.js'
|
|
7
6
|
import type { Tag, Peer as PeerPB } from '../pb/peer.js'
|
|
8
|
-
import {
|
|
7
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
+
import type { Address, Peer, PeerData, TagOptions } from '@libp2p/interface-peer-store'
|
|
9
9
|
|
|
10
10
|
export interface ToPBPeerOptions {
|
|
11
11
|
addressFilter?: AddressFilter
|
|
@@ -28,7 +28,7 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
let addresses: Address[] = existingPeer?.addresses ?? []
|
|
31
|
-
let protocols
|
|
31
|
+
let protocols = new Set<string>(existingPeer?.protocols ?? [])
|
|
32
32
|
let metadata: Map<string, Uint8Array> = existingPeer?.metadata ?? new Map()
|
|
33
33
|
let tags: Map<string, Tag> = existingPeer?.tags ?? new Map()
|
|
34
34
|
let peerRecordEnvelope: Uint8Array | undefined = existingPeer?.peerRecordEnvelope
|
|
@@ -111,7 +111,7 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
111
111
|
|
|
112
112
|
if (data.tags != null) {
|
|
113
113
|
const tagsEntries = data.tags instanceof Map ? [...data.tags.entries()] : Object.entries(data.tags)
|
|
114
|
-
const mergedTags
|
|
114
|
+
const mergedTags = new Map<string, Tag | TagOptions>(tags)
|
|
115
115
|
|
|
116
116
|
for (const [key, value] of tagsEntries) {
|
|
117
117
|
if (value == null) {
|