@libp2p/peer-store 6.0.0 → 6.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/peer-store",
3
- "version": "6.0.0",
3
+ "version": "6.0.2",
4
4
  "description": "Stores information about peers libp2p knows on the network",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p-peer-store#readme",
@@ -148,12 +148,11 @@
148
148
  "@libp2p/interface-peer-info": "^1.0.3",
149
149
  "@libp2p/interface-peer-store": "^1.2.2",
150
150
  "@libp2p/interface-record": "^2.0.1",
151
- "@libp2p/interfaces": "^3.0.3",
151
+ "@libp2p/interfaces": "^3.2.0",
152
152
  "@libp2p/logger": "^2.0.0",
153
153
  "@libp2p/peer-id": "^2.0.0",
154
154
  "@libp2p/peer-record": "^5.0.0",
155
155
  "@multiformats/multiaddr": "^11.0.0",
156
- "err-code": "^3.0.1",
157
156
  "interface-datastore": "^7.0.0",
158
157
  "it-all": "^2.0.0",
159
158
  "it-filter": "^2.0.0",
@@ -162,7 +161,7 @@
162
161
  "it-pipe": "^2.0.3",
163
162
  "mortice": "^3.0.0",
164
163
  "multiformats": "^11.0.0",
165
- "protons-runtime": "^4.0.1",
164
+ "protons-runtime": "^5.0.0",
166
165
  "uint8arraylist": "^2.1.1",
167
166
  "uint8arrays": "^4.0.2"
168
167
  },
@@ -175,7 +174,7 @@
175
174
  "delay": "^5.0.0",
176
175
  "p-defer": "^4.0.0",
177
176
  "p-wait-for": "^5.0.0",
178
- "protons": "^6.0.0",
177
+ "protons": "^7.0.2",
179
178
  "sinon": "^15.0.1"
180
179
  }
181
180
  }
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import { isMultiaddr } from '@multiformats/multiaddr'
4
4
  import { codes } from './errors.js'
5
5
  import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record'
@@ -174,7 +174,7 @@ export class PeerStoreAddressBook {
174
174
 
175
175
  if (!Array.isArray(multiaddrs)) {
176
176
  log.error('multiaddrs must be an array of Multiaddrs')
177
- throw errcode(new Error('multiaddrs must be an array of Multiaddrs'), codes.ERR_INVALID_PARAMETERS)
177
+ throw new CodeError('multiaddrs must be an array of Multiaddrs', codes.ERR_INVALID_PARAMETERS)
178
178
  }
179
179
 
180
180
  log.trace('set await write lock')
@@ -244,7 +244,7 @@ export class PeerStoreAddressBook {
244
244
 
245
245
  if (!Array.isArray(multiaddrs)) {
246
246
  log.error('multiaddrs must be an array of Multiaddrs')
247
- throw errcode(new Error('multiaddrs must be an array of Multiaddrs'), codes.ERR_INVALID_PARAMETERS)
247
+ throw new CodeError('multiaddrs must be an array of Multiaddrs', codes.ERR_INVALID_PARAMETERS)
248
248
  }
249
249
 
250
250
  log.trace('add await write lock')
@@ -351,7 +351,7 @@ async function filterMultiaddrs (peerId: PeerId, multiaddrs: Multiaddr[], addres
351
351
  (source) => each(source, (multiaddr) => {
352
352
  if (!isMultiaddr(multiaddr)) {
353
353
  log.error('multiaddr must be an instance of Multiaddr')
354
- throw errcode(new Error('multiaddr must be an instance of Multiaddr'), codes.ERR_INVALID_PARAMETERS)
354
+ throw new CodeError('multiaddr must be an instance of Multiaddr', codes.ERR_INVALID_PARAMETERS)
355
355
  }
356
356
  }),
357
357
  (source) => filter(source, async (multiaddr) => await addressFilter(peerId, multiaddr)),
package/src/index.ts CHANGED
@@ -7,7 +7,7 @@ import { PeerStoreProtoBook } from './proto-book.js'
7
7
  import { PersistentStore, Store } from './store.js'
8
8
  import type { PeerStore, AddressBook, KeyBook, MetadataBook, ProtoBook, PeerStoreEvents, PeerStoreInit, Peer, TagOptions } from '@libp2p/interface-peer-store'
9
9
  import type { PeerId } from '@libp2p/interface-peer-id'
10
- import errCode from 'err-code'
10
+ import { CodeError } from '@libp2p/interfaces/errors'
11
11
  import { Tag, Tags } from './pb/tags.js'
12
12
  import type { Datastore } from 'interface-datastore'
13
13
 
@@ -125,7 +125,7 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> implement
125
125
  const ttl = options.ttl ?? undefined
126
126
 
127
127
  if (value !== providedValue || value < 0 || value > 100) {
128
- throw errCode(new Error('Tag value must be between 0-100'), 'ERR_TAG_VALUE_OUT_OF_BOUNDS')
128
+ throw new CodeError('Tag value must be between 0-100', 'ERR_TAG_VALUE_OUT_OF_BOUNDS')
129
129
  }
130
130
 
131
131
  const buf = await this.metadataBook.getValue(peerId, 'tags')
@@ -135,11 +135,8 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> implement
135
135
  tags = Tags.decode(buf).tags
136
136
  }
137
137
 
138
- for (const t of tags) {
139
- if (t.name === tag) {
140
- throw errCode(new Error('Peer already tagged'), 'ERR_DUPLICATE_TAG')
141
- }
142
- }
138
+ // do not allow duplicate tags
139
+ tags = tags.filter(t => t.name !== tag)
143
140
 
144
141
  tags.push({
145
142
  name: tag,
package/src/key-book.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import { codes } from './errors.js'
4
4
  import { peerIdFromPeerId } from '@libp2p/peer-id'
5
5
  import { equals as uint8arrayEquals } from 'uint8arrays/equals'
@@ -32,7 +32,7 @@ export class PeerStoreKeyBook implements KeyBook {
32
32
 
33
33
  if (!(publicKey instanceof Uint8Array)) {
34
34
  log.error('publicKey must be an instance of Uint8Array to store data')
35
- throw errcode(new Error('publicKey must be an instance of PublicKey'), codes.ERR_INVALID_PARAMETERS)
35
+ throw new CodeError('publicKey must be an instance of PublicKey', codes.ERR_INVALID_PARAMETERS)
36
36
  }
37
37
 
38
38
  log.trace('set await write lock')
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import { codes } from './errors.js'
4
4
  import { peerIdFromPeerId } from '@libp2p/peer-id'
5
5
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
@@ -80,7 +80,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
80
80
 
81
81
  if (!(metadata instanceof Map)) {
82
82
  log.error('valid metadata must be provided to store data')
83
- throw errcode(new Error('valid metadata must be provided'), codes.ERR_INVALID_PARAMETERS)
83
+ throw new CodeError('valid metadata must be provided', codes.ERR_INVALID_PARAMETERS)
84
84
  }
85
85
 
86
86
  log.trace('set await write lock')
@@ -123,7 +123,7 @@ export class PeerStoreMetadataBook implements MetadataBook {
123
123
 
124
124
  if (typeof key !== 'string' || !(value instanceof Uint8Array)) {
125
125
  log.error('valid key and value must be provided to store data')
126
- throw errcode(new Error('valid key and value must be provided'), codes.ERR_INVALID_PARAMETERS)
126
+ throw new CodeError('valid key and value must be provided', codes.ERR_INVALID_PARAMETERS)
127
127
  }
128
128
 
129
129
  log.trace('setValue 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
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import { codes } from './errors.js'
4
4
  import { peerIdFromPeerId } from '@libp2p/peer-id'
5
5
  import { CustomEvent } from '@libp2p/interfaces/events'
@@ -50,7 +50,7 @@ export class PeerStoreProtoBook implements ProtoBook {
50
50
 
51
51
  if (!Array.isArray(protocols)) {
52
52
  log.error('protocols must be provided to store data')
53
- throw errcode(new Error('protocols must be provided'), codes.ERR_INVALID_PARAMETERS)
53
+ throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS)
54
54
  }
55
55
 
56
56
  log.trace('set await write lock')
@@ -99,7 +99,7 @@ export class PeerStoreProtoBook implements ProtoBook {
99
99
 
100
100
  if (!Array.isArray(protocols)) {
101
101
  log.error('protocols must be provided to store data')
102
- throw errcode(new Error('protocols must be provided'), codes.ERR_INVALID_PARAMETERS)
102
+ throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS)
103
103
  }
104
104
 
105
105
  log.trace('add await write lock')
@@ -149,7 +149,7 @@ export class PeerStoreProtoBook implements ProtoBook {
149
149
 
150
150
  if (!Array.isArray(protocols)) {
151
151
  log.error('protocols must be provided to store data')
152
- throw errcode(new Error('protocols must be provided'), codes.ERR_INVALID_PARAMETERS)
152
+ throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS)
153
153
  }
154
154
 
155
155
  log.trace('remove await write lock')
package/src/store.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import { peerIdFromBytes } from '@libp2p/peer-id'
3
- import errcode from 'err-code'
3
+ import { CodeError } from '@libp2p/interfaces/errors'
4
4
  import { codes } from './errors.js'
5
5
  import { Key } from 'interface-datastore/key'
6
6
  import { base32 } from 'multiformats/bases/base32'
@@ -48,7 +48,7 @@ export class PersistentStore {
48
48
  _peerIdToDatastoreKey (peerId: PeerId) {
49
49
  if (peerId.type == null) {
50
50
  log.error('peerId must be an instance of peer-id to store data')
51
- throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
51
+ throw new CodeError('peerId must be an instance of peer-id', codes.ERR_INVALID_PARAMETERS)
52
52
  }
53
53
 
54
54
  const b32key = peerId.toCID().toString()
@@ -90,7 +90,7 @@ export class PersistentStore {
90
90
  async save (peer: 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
- throw errcode(new Error('publicKey bytes do not match peer id publicKey bytes'), codes.ERR_INVALID_PARAMETERS)
93
+ throw new CodeError('publicKey bytes do not match peer id publicKey bytes', codes.ERR_INVALID_PARAMETERS)
94
94
  }
95
95
 
96
96
  // dedupe addresses