@libp2p/peer-store 7.0.2 → 8.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/README.md +0 -170
- package/dist/index.min.js +14 -14
- package/dist/src/errors.d.ts +0 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -2
- package/dist/src/errors.js.map +1 -1
- package/dist/src/index.d.ts +23 -25
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +86 -88
- package/dist/src/index.js.map +1 -1
- package/dist/src/pb/peer.d.ts +28 -9
- package/dist/src/pb/peer.d.ts.map +1 -1
- package/dist/src/pb/peer.js +147 -31
- package/dist/src/pb/peer.js.map +1 -1
- package/dist/src/store.d.ts +18 -28
- package/dist/src/store.d.ts.map +1 -1
- package/dist/src/store.js +77 -147
- package/dist/src/store.js.map +1 -1
- package/dist/src/utils/bytes-to-peer.d.ts +4 -0
- package/dist/src/utils/bytes-to-peer.d.ts.map +1 -0
- package/dist/src/utils/bytes-to-peer.js +33 -0
- package/dist/src/utils/bytes-to-peer.js.map +1 -0
- package/dist/src/utils/dedupe-addresses.d.ts +6 -0
- package/dist/src/utils/dedupe-addresses.d.ts.map +1 -0
- package/dist/src/utils/dedupe-addresses.js +41 -0
- package/dist/src/utils/dedupe-addresses.js.map +1 -0
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts +5 -0
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts.map +1 -0
- package/dist/src/utils/peer-data-to-datastore-peer.js +92 -0
- package/dist/src/utils/peer-data-to-datastore-peer.js.map +1 -0
- package/dist/src/utils/peer-id-to-datastore-key.d.ts +5 -0
- package/dist/src/utils/peer-id-to-datastore-key.d.ts.map +1 -0
- package/dist/src/utils/peer-id-to-datastore-key.js +13 -0
- package/dist/src/utils/peer-id-to-datastore-key.js.map +1 -0
- package/dist/src/utils/to-peer-pb.d.ts +10 -0
- package/dist/src/utils/to-peer-pb.d.ts.map +1 -0
- package/dist/src/utils/to-peer-pb.js +182 -0
- package/dist/src/utils/to-peer-pb.js.map +1 -0
- package/dist/typedoc-urls.json +13 -2
- package/package.json +8 -10
- package/src/errors.ts +1 -2
- package/src/index.ts +97 -103
- package/src/pb/peer.proto +10 -7
- package/src/pb/peer.ts +187 -37
- package/src/store.ts +102 -189
- package/src/utils/bytes-to-peer.ts +41 -0
- package/src/utils/dedupe-addresses.ts +51 -0
- package/src/utils/peer-data-to-datastore-peer.ts +116 -0
- package/src/utils/peer-id-to-datastore-key.ts +15 -0
- package/src/utils/to-peer-pb.ts +237 -0
- package/dist/src/address-book.d.ts +0 -29
- package/dist/src/address-book.d.ts.map +0 -1
- package/dist/src/address-book.js +0 -304
- package/dist/src/address-book.js.map +0 -1
- package/dist/src/key-book.d.ts +0 -21
- package/dist/src/key-book.d.ts.map +0 -1
- package/dist/src/key-book.js +0 -121
- package/dist/src/key-book.js.map +0 -1
- package/dist/src/metadata-book.d.ts +0 -28
- package/dist/src/metadata-book.d.ts.map +0 -1
- package/dist/src/metadata-book.js +0 -211
- package/dist/src/metadata-book.js.map +0 -1
- package/dist/src/pb/tags.d.ts +0 -21
- package/dist/src/pb/tags.d.ts.map +0 -1
- package/dist/src/pb/tags.js +0 -111
- package/dist/src/pb/tags.js.map +0 -1
- package/dist/src/proto-book.d.ts +0 -18
- package/dist/src/proto-book.d.ts.map +0 -1
- package/dist/src/proto-book.js +0 -199
- package/dist/src/proto-book.js.map +0 -1
- package/src/address-book.ts +0 -367
- package/src/key-book.ts +0 -140
- package/src/metadata-book.ts +0 -244
- package/src/pb/tags.proto +0 -11
- package/src/pb/tags.ts +0 -145
- package/src/proto-book.ts +0 -234
package/src/proto-book.ts
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
|
-
import { codes } from './errors.js'
|
|
4
|
-
import { peerIdFromPeerId } from '@libp2p/peer-id'
|
|
5
|
-
import { CustomEvent } from '@libp2p/interfaces/events'
|
|
6
|
-
import type { Store } from './store.js'
|
|
7
|
-
import type { Peer, PeerProtocolsChangeData, PeerStore, ProtoBook } from '@libp2p/interface-peer-store'
|
|
8
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
9
|
-
|
|
10
|
-
const log = logger('libp2p:peer-store:proto-book')
|
|
11
|
-
|
|
12
|
-
const EVENT_NAME = 'change:protocols'
|
|
13
|
-
|
|
14
|
-
export class PeerStoreProtoBook implements ProtoBook {
|
|
15
|
-
private readonly dispatchEvent: PeerStore['dispatchEvent']
|
|
16
|
-
private readonly store: Store
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* The ProtoBook is responsible for keeping the known supported
|
|
20
|
-
* protocols of a peer
|
|
21
|
-
*/
|
|
22
|
-
constructor (dispatchEvent: PeerStore['dispatchEvent'], store: Store) {
|
|
23
|
-
this.dispatchEvent = dispatchEvent
|
|
24
|
-
this.store = store
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async get (peerId: PeerId): Promise<string[]> {
|
|
28
|
-
log.trace('get wait for read lock')
|
|
29
|
-
const release = await this.store.lock.readLock()
|
|
30
|
-
log.trace('get got read lock')
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
const peer = await this.store.load(peerId)
|
|
34
|
-
|
|
35
|
-
return peer.protocols
|
|
36
|
-
} catch (err: any) {
|
|
37
|
-
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
38
|
-
throw err
|
|
39
|
-
}
|
|
40
|
-
} finally {
|
|
41
|
-
log.trace('get release read lock')
|
|
42
|
-
release()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return []
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async set (peerId: PeerId, protocols: string[]): Promise<void> {
|
|
49
|
-
peerId = peerIdFromPeerId(peerId)
|
|
50
|
-
|
|
51
|
-
if (!Array.isArray(protocols)) {
|
|
52
|
-
log.error('protocols must be provided to store data')
|
|
53
|
-
throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
log.trace('set await write lock')
|
|
57
|
-
const release = await this.store.lock.writeLock()
|
|
58
|
-
log.trace('set got write lock')
|
|
59
|
-
|
|
60
|
-
let peer
|
|
61
|
-
let updatedPeer
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
try {
|
|
65
|
-
peer = await this.store.load(peerId)
|
|
66
|
-
|
|
67
|
-
if (new Set([
|
|
68
|
-
...protocols
|
|
69
|
-
]).size === peer.protocols.length) {
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
} catch (err: any) {
|
|
73
|
-
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
74
|
-
throw err
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
updatedPeer = await this.store.patchOrCreate(peerId, {
|
|
79
|
-
protocols
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
log('stored provided protocols for %p', peerId)
|
|
83
|
-
} finally {
|
|
84
|
-
log.trace('set release write lock')
|
|
85
|
-
release()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
this.dispatchEvent(new CustomEvent<PeerProtocolsChangeData>(EVENT_NAME, {
|
|
89
|
-
detail: {
|
|
90
|
-
peerId,
|
|
91
|
-
protocols: updatedPeer.protocols,
|
|
92
|
-
oldProtocols: peer == null ? [] : peer.protocols
|
|
93
|
-
}
|
|
94
|
-
}))
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async add (peerId: PeerId, protocols: string[]): Promise<void> {
|
|
98
|
-
peerId = peerIdFromPeerId(peerId)
|
|
99
|
-
|
|
100
|
-
if (!Array.isArray(protocols)) {
|
|
101
|
-
log.error('protocols must be provided to store data')
|
|
102
|
-
throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
log.trace('add await write lock')
|
|
106
|
-
const release = await this.store.lock.writeLock()
|
|
107
|
-
log.trace('add got write lock')
|
|
108
|
-
|
|
109
|
-
let peer: Peer | undefined
|
|
110
|
-
let updatedPeer
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
try {
|
|
114
|
-
peer = await this.store.load(peerId)
|
|
115
|
-
|
|
116
|
-
if (new Set([
|
|
117
|
-
...peer.protocols,
|
|
118
|
-
...protocols
|
|
119
|
-
]).size === peer.protocols.length) {
|
|
120
|
-
return
|
|
121
|
-
}
|
|
122
|
-
} catch (err: any) {
|
|
123
|
-
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
124
|
-
throw err
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
updatedPeer = await this.store.mergeOrCreate(peerId, {
|
|
129
|
-
protocols
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
log('added provided protocols for %p', peerId)
|
|
133
|
-
} finally {
|
|
134
|
-
log.trace('add release write lock')
|
|
135
|
-
release()
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
this.dispatchEvent(new CustomEvent<PeerProtocolsChangeData>(EVENT_NAME, {
|
|
139
|
-
detail: {
|
|
140
|
-
peerId,
|
|
141
|
-
protocols: updatedPeer.protocols,
|
|
142
|
-
oldProtocols: peer == null ? [] : peer.protocols
|
|
143
|
-
}
|
|
144
|
-
}))
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async remove (peerId: PeerId, protocols: string[]): Promise<void> {
|
|
148
|
-
peerId = peerIdFromPeerId(peerId)
|
|
149
|
-
|
|
150
|
-
if (!Array.isArray(protocols)) {
|
|
151
|
-
log.error('protocols must be provided to store data')
|
|
152
|
-
throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
log.trace('remove await write lock')
|
|
156
|
-
const release = await this.store.lock.writeLock()
|
|
157
|
-
log.trace('remove got write lock')
|
|
158
|
-
|
|
159
|
-
let peer: Peer | undefined
|
|
160
|
-
let updatedPeer: Peer
|
|
161
|
-
|
|
162
|
-
try {
|
|
163
|
-
try {
|
|
164
|
-
peer = await this.store.load(peerId)
|
|
165
|
-
const protocolSet = new Set(peer.protocols)
|
|
166
|
-
|
|
167
|
-
for (const protocol of protocols) {
|
|
168
|
-
protocolSet.delete(protocol)
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (peer.protocols.length === protocolSet.size) {
|
|
172
|
-
return
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
protocols = Array.from(protocolSet)
|
|
176
|
-
} catch (err: any) {
|
|
177
|
-
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
178
|
-
throw err
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
updatedPeer = await this.store.patchOrCreate(peerId, {
|
|
183
|
-
protocols
|
|
184
|
-
})
|
|
185
|
-
} finally {
|
|
186
|
-
log.trace('remove release write lock')
|
|
187
|
-
release()
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
this.dispatchEvent(new CustomEvent<PeerProtocolsChangeData>(EVENT_NAME, {
|
|
191
|
-
detail: {
|
|
192
|
-
peerId,
|
|
193
|
-
protocols: updatedPeer.protocols,
|
|
194
|
-
oldProtocols: peer == null ? [] : peer.protocols
|
|
195
|
-
}
|
|
196
|
-
}))
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async delete (peerId: PeerId): Promise<void> {
|
|
200
|
-
peerId = peerIdFromPeerId(peerId)
|
|
201
|
-
|
|
202
|
-
log.trace('delete await write lock')
|
|
203
|
-
const release = await this.store.lock.writeLock()
|
|
204
|
-
log.trace('delete got write lock')
|
|
205
|
-
let peer: Peer | undefined
|
|
206
|
-
|
|
207
|
-
try {
|
|
208
|
-
try {
|
|
209
|
-
peer = await this.store.load(peerId)
|
|
210
|
-
} catch (err: any) {
|
|
211
|
-
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
212
|
-
throw err
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
await this.store.patchOrCreate(peerId, {
|
|
217
|
-
protocols: []
|
|
218
|
-
})
|
|
219
|
-
} finally {
|
|
220
|
-
log.trace('delete release write lock')
|
|
221
|
-
release()
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (peer != null) {
|
|
225
|
-
this.dispatchEvent(new CustomEvent<PeerProtocolsChangeData>(EVENT_NAME, {
|
|
226
|
-
detail: {
|
|
227
|
-
peerId,
|
|
228
|
-
protocols: [],
|
|
229
|
-
oldProtocols: peer.protocols
|
|
230
|
-
}
|
|
231
|
-
}))
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|