@libp2p/peer-store 6.0.1 → 6.0.3

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.
@@ -28,7 +28,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
28
28
  /**
29
29
  * Get the known data of a provided peer
30
30
  */
31
- async get (peerId: PeerId) {
31
+ async get (peerId: PeerId): Promise<Map<string, Uint8Array>> {
32
32
  peerId = peerIdFromPeerId(peerId)
33
33
 
34
34
  log.trace('get await read lock')
@@ -54,7 +54,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
54
54
  /**
55
55
  * Get specific metadata value, if it exists
56
56
  */
57
- async getValue (peerId: PeerId, key: string) {
57
+ async getValue (peerId: PeerId, key: string): Promise<Uint8Array | undefined> {
58
58
  peerId = peerIdFromPeerId(peerId)
59
59
 
60
60
  log.trace('getValue await read lock')
@@ -75,7 +75,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
75
75
  }
76
76
  }
77
77
 
78
- async set (peerId: PeerId, metadata: Map<string, Uint8Array>) {
78
+ async set (peerId: PeerId, metadata: Map<string, Uint8Array>): Promise<void> {
79
79
  peerId = peerIdFromPeerId(peerId)
80
80
 
81
81
  if (!(metadata instanceof Map)) {
@@ -118,7 +118,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
118
118
  /**
119
119
  * Set metadata key and value of a provided peer
120
120
  */
121
- async setValue (peerId: PeerId, key: string, value: Uint8Array) {
121
+ async setValue (peerId: PeerId, key: string, value: Uint8Array): Promise<void> {
122
122
  peerId = peerIdFromPeerId(peerId)
123
123
 
124
124
  if (typeof key !== 'string' || !(value instanceof Uint8Array)) {
@@ -164,7 +164,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
164
164
  }))
165
165
  }
166
166
 
167
- async delete (peerId: PeerId) {
167
+ async delete (peerId: PeerId): Promise<void> {
168
168
  peerId = peerIdFromPeerId(peerId)
169
169
 
170
170
  log.trace('delete await write lock')
@@ -203,7 +203,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
203
203
  }
204
204
  }
205
205
 
206
- async deleteValue (peerId: PeerId, key: string) {
206
+ async deleteValue (peerId: PeerId, key: string): Promise<void> {
207
207
  peerId = peerIdFromPeerId(peerId)
208
208
 
209
209
  log.trace('deleteValue await write lock')
package/src/pb/peer.ts CHANGED
@@ -2,10 +2,11 @@
2
2
  /* eslint-disable complexity */
3
3
  /* eslint-disable @typescript-eslint/no-namespace */
4
4
  /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
5
6
 
6
7
  import { encodeMessage, decodeMessage, message } from 'protons-runtime'
7
- import type { Uint8ArrayList } from 'uint8arraylist'
8
8
  import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
9
10
 
10
11
  export interface Peer {
11
12
  addresses: Address[]
@@ -28,9 +29,7 @@ export namespace Peer {
28
29
  if (obj.addresses != null) {
29
30
  for (const value of obj.addresses) {
30
31
  w.uint32(10)
31
- Address.codec().encode(value, w, {
32
- writeDefaults: true
33
- })
32
+ Address.codec().encode(value, w)
34
33
  }
35
34
  }
36
35
 
@@ -44,9 +43,7 @@ export namespace Peer {
44
43
  if (obj.metadata != null) {
45
44
  for (const value of obj.metadata) {
46
45
  w.uint32(26)
47
- Metadata.codec().encode(value, w, {
48
- writeDefaults: true
49
- })
46
+ Metadata.codec().encode(value, w)
50
47
  }
51
48
  }
52
49
 
@@ -104,7 +101,7 @@ export namespace Peer {
104
101
  return _codec
105
102
  }
106
103
 
107
- export const encode = (obj: Peer): Uint8Array => {
104
+ export const encode = (obj: Partial<Peer>): Uint8Array => {
108
105
  return encodeMessage(obj, Peer.codec())
109
106
  }
110
107
 
@@ -128,7 +125,7 @@ export namespace Address {
128
125
  w.fork()
129
126
  }
130
127
 
131
- if (opts.writeDefaults === true || (obj.multiaddr != null && obj.multiaddr.byteLength > 0)) {
128
+ if ((obj.multiaddr != null && obj.multiaddr.byteLength > 0)) {
132
129
  w.uint32(10)
133
130
  w.bytes(obj.multiaddr)
134
131
  }
@@ -171,7 +168,7 @@ export namespace Address {
171
168
  return _codec
172
169
  }
173
170
 
174
- export const encode = (obj: Address): Uint8Array => {
171
+ export const encode = (obj: Partial<Address>): Uint8Array => {
175
172
  return encodeMessage(obj, Address.codec())
176
173
  }
177
174
 
@@ -195,12 +192,12 @@ export namespace Metadata {
195
192
  w.fork()
196
193
  }
197
194
 
198
- if (opts.writeDefaults === true || obj.key !== '') {
195
+ if ((obj.key != null && obj.key !== '')) {
199
196
  w.uint32(10)
200
197
  w.string(obj.key)
201
198
  }
202
199
 
203
- if (opts.writeDefaults === true || (obj.value != null && obj.value.byteLength > 0)) {
200
+ if ((obj.value != null && obj.value.byteLength > 0)) {
204
201
  w.uint32(18)
205
202
  w.bytes(obj.value)
206
203
  }
@@ -239,7 +236,7 @@ export namespace Metadata {
239
236
  return _codec
240
237
  }
241
238
 
242
- export const encode = (obj: Metadata): Uint8Array => {
239
+ export const encode = (obj: Partial<Metadata>): Uint8Array => {
243
240
  return encodeMessage(obj, Metadata.codec())
244
241
  }
245
242
 
package/src/pb/tags.ts CHANGED
@@ -2,10 +2,11 @@
2
2
  /* eslint-disable complexity */
3
3
  /* eslint-disable @typescript-eslint/no-namespace */
4
4
  /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
5
6
 
6
7
  import { encodeMessage, decodeMessage, message } from 'protons-runtime'
7
- import type { Uint8ArrayList } from 'uint8arraylist'
8
8
  import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
9
10
 
10
11
  export interface Tags {
11
12
  tags: Tag[]
@@ -24,9 +25,7 @@ export namespace Tags {
24
25
  if (obj.tags != null) {
25
26
  for (const value of obj.tags) {
26
27
  w.uint32(10)
27
- Tag.codec().encode(value, w, {
28
- writeDefaults: true
29
- })
28
+ Tag.codec().encode(value, w)
30
29
  }
31
30
  }
32
31
 
@@ -60,7 +59,7 @@ export namespace Tags {
60
59
  return _codec
61
60
  }
62
61
 
63
- export const encode = (obj: Tags): Uint8Array => {
62
+ export const encode = (obj: Partial<Tags>): Uint8Array => {
64
63
  return encodeMessage(obj, Tags.codec())
65
64
  }
66
65
 
@@ -85,7 +84,7 @@ export namespace Tag {
85
84
  w.fork()
86
85
  }
87
86
 
88
- if (opts.writeDefaults === true || obj.name !== '') {
87
+ if ((obj.name != null && obj.name !== '')) {
89
88
  w.uint32(10)
90
89
  w.string(obj.name)
91
90
  }
@@ -136,7 +135,7 @@ export namespace Tag {
136
135
  return _codec
137
136
  }
138
137
 
139
- export const encode = (obj: Tag): Uint8Array => {
138
+ export const encode = (obj: Partial<Tag>): Uint8Array => {
140
139
  return encodeMessage(obj, Tag.codec())
141
140
  }
142
141
 
package/src/proto-book.ts CHANGED
@@ -24,7 +24,7 @@ export class PeerStoreProtoBook implements ProtoBook {
24
24
  this.store = store
25
25
  }
26
26
 
27
- async get (peerId: PeerId) {
27
+ async get (peerId: PeerId): Promise<string[]> {
28
28
  log.trace('get wait for read lock')
29
29
  const release = await this.store.lock.readLock()
30
30
  log.trace('get got read lock')
@@ -45,7 +45,7 @@ export class PeerStoreProtoBook implements ProtoBook {
45
45
  return []
46
46
  }
47
47
 
48
- async set (peerId: PeerId, protocols: string[]) {
48
+ async set (peerId: PeerId, protocols: string[]): Promise<void> {
49
49
  peerId = peerIdFromPeerId(peerId)
50
50
 
51
51
  if (!Array.isArray(protocols)) {
@@ -94,7 +94,7 @@ export class PeerStoreProtoBook implements ProtoBook {
94
94
  }))
95
95
  }
96
96
 
97
- async add (peerId: PeerId, protocols: string[]) {
97
+ async add (peerId: PeerId, protocols: string[]): Promise<void> {
98
98
  peerId = peerIdFromPeerId(peerId)
99
99
 
100
100
  if (!Array.isArray(protocols)) {
@@ -144,7 +144,7 @@ export class PeerStoreProtoBook implements ProtoBook {
144
144
  }))
145
145
  }
146
146
 
147
- async remove (peerId: PeerId, protocols: string[]) {
147
+ async remove (peerId: PeerId, protocols: string[]): Promise<void> {
148
148
  peerId = peerIdFromPeerId(peerId)
149
149
 
150
150
  if (!Array.isArray(protocols)) {
@@ -196,7 +196,7 @@ export class PeerStoreProtoBook implements ProtoBook {
196
196
  }))
197
197
  }
198
198
 
199
- async delete (peerId: PeerId) {
199
+ async delete (peerId: PeerId): Promise<void> {
200
200
  peerId = peerIdFromPeerId(peerId)
201
201
 
202
202
  log.trace('delete await write lock')
package/src/store.ts CHANGED
@@ -45,7 +45,7 @@ export class PersistentStore {
45
45
  })
46
46
  }
47
47
 
48
- _peerIdToDatastoreKey (peerId: PeerId) {
48
+ _peerIdToDatastoreKey (peerId: PeerId): Key {
49
49
  if (peerId.type == null) {
50
50
  log.error('peerId must be an instance of peer-id to store data')
51
51
  throw new CodeError('peerId must be an instance of peer-id', codes.ERR_INVALID_PARAMETERS)
@@ -55,11 +55,11 @@ export class PersistentStore {
55
55
  return new Key(`${NAMESPACE_COMMON}${b32key}`)
56
56
  }
57
57
 
58
- async has (peerId: PeerId) {
58
+ async has (peerId: PeerId): Promise<boolean> {
59
59
  return await this.components.datastore.has(this._peerIdToDatastoreKey(peerId))
60
60
  }
61
61
 
62
- async delete (peerId: PeerId) {
62
+ async delete (peerId: PeerId): Promise<void> {
63
63
  await this.components.datastore.delete(this._peerIdToDatastoreKey(peerId))
64
64
  }
65
65
 
@@ -87,7 +87,7 @@ export class PersistentStore {
87
87
  }
88
88
  }
89
89
 
90
- async save (peer: Peer) {
90
+ async save (peer: Peer): Promise<Peer> {
91
91
  if (peer.pubKey != null && peer.id.publicKey != null && !uint8arrayEquals(peer.pubKey, peer.id.publicKey)) {
92
92
  log.error('peer publicKey bytes do not match peer id publicKey bytes')
93
93
  throw new CodeError('publicKey bytes do not match peer id publicKey bytes', codes.ERR_INVALID_PARAMETERS)
@@ -135,13 +135,13 @@ export class PersistentStore {
135
135
  return await this.load(peer.id)
136
136
  }
137
137
 
138
- async patch (peerId: PeerId, data: Partial<Peer>) {
138
+ async patch (peerId: PeerId, data: Partial<Peer>): Promise<Peer> {
139
139
  const peer = await this.load(peerId)
140
140
 
141
141
  return await this._patch(peerId, data, peer)
142
142
  }
143
143
 
144
- async patchOrCreate (peerId: PeerId, data: Partial<Peer>) {
144
+ async patchOrCreate (peerId: PeerId, data: Partial<Peer>): Promise<Peer> {
145
145
  let peer: Peer
146
146
 
147
147
  try {
@@ -157,7 +157,7 @@ export class PersistentStore {
157
157
  return await this._patch(peerId, data, peer)
158
158
  }
159
159
 
160
- async _patch (peerId: PeerId, data: Partial<Peer>, peer: Peer) {
160
+ async _patch (peerId: PeerId, data: Partial<Peer>, peer: Peer): Promise<Peer> {
161
161
  return await this.save({
162
162
  ...peer,
163
163
  ...data,
@@ -165,13 +165,13 @@ export class PersistentStore {
165
165
  })
166
166
  }
167
167
 
168
- async merge (peerId: PeerId, data: Partial<Peer>) {
168
+ async merge (peerId: PeerId, data: Partial<Peer>): Promise<Peer> {
169
169
  const peer = await this.load(peerId)
170
170
 
171
171
  return await this._merge(peerId, data, peer)
172
172
  }
173
173
 
174
- async mergeOrCreate (peerId: PeerId, data: Partial<Peer>) {
174
+ async mergeOrCreate (peerId: PeerId, data: Partial<Peer>): Promise<Peer> {
175
175
  /** @type {Peer} */
176
176
  let peer
177
177
 
@@ -188,7 +188,7 @@ export class PersistentStore {
188
188
  return await this._merge(peerId, data, peer)
189
189
  }
190
190
 
191
- async _merge (peerId: PeerId, data: Partial<Peer>, peer: Peer) {
191
+ async _merge (peerId: PeerId, data: Partial<Peer>, peer: Peer): Promise<Peer> {
192
192
  // if the peer has certified addresses, use those in
193
193
  // favour of the supplied versions
194
194
  const addresses = new Map<string, boolean>()
@@ -227,7 +227,7 @@ export class PersistentStore {
227
227
  })
228
228
  }
229
229
 
230
- async * all () {
230
+ async * all (): AsyncGenerator<Peer, void, unknown> {
231
231
  for await (const key of this.components.datastore.queryKeys({
232
232
  prefix: NAMESPACE_COMMON
233
233
  })) {