@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/index.ts
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { PeerStoreKeyBook } from './key-book.js'
|
|
5
|
-
import { PeerStoreMetadataBook } from './metadata-book.js'
|
|
6
|
-
import { PeerStoreProtoBook } from './proto-book.js'
|
|
7
|
-
import { PersistentStore, Store } from './store.js'
|
|
8
|
-
import type { PeerStore, AddressBook, KeyBook, MetadataBook, ProtoBook, PeerStoreEvents, PeerStoreInit, Peer, TagOptions } from '@libp2p/interface-peer-store'
|
|
1
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
2
|
+
import { PersistentStore, PeerUpdate } from './store.js'
|
|
3
|
+
import type { PeerStore, Peer, PeerData } from '@libp2p/interface-peer-store'
|
|
9
4
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
10
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
11
|
-
import { Tag, Tags } from './pb/tags.js'
|
|
12
5
|
import type { Datastore } from 'interface-datastore'
|
|
6
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
8
|
+
import { logger } from '@libp2p/logger'
|
|
13
9
|
|
|
14
10
|
const log = logger('libp2p:peer-store')
|
|
15
11
|
|
|
16
12
|
export interface PersistentPeerStoreComponents {
|
|
17
13
|
peerId: PeerId
|
|
18
14
|
datastore: Datastore
|
|
15
|
+
events: EventEmitter<Libp2pEvents>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Return true to allow storing the passed multiaddr for the passed peer
|
|
20
|
+
*/
|
|
21
|
+
export interface AddressFilter {
|
|
22
|
+
(peerId: PeerId, multiaddr: Multiaddr): Promise<boolean>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PersistentPeerStoreInit {
|
|
26
|
+
addressFilter?: AddressFilter
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
/**
|
|
22
30
|
* An implementation of PeerStore that stores data in a Datastore
|
|
23
31
|
*/
|
|
24
|
-
export class PersistentPeerStore
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
constructor (components: PersistentPeerStoreComponents, init: PeerStoreInit = {}) {
|
|
34
|
-
super()
|
|
35
|
-
|
|
36
|
-
this.components = components
|
|
37
|
-
this.store = new PersistentStore(components)
|
|
38
|
-
this.addressBook = new PeerStoreAddressBook(this.dispatchEvent.bind(this), this.store, init.addressFilter)
|
|
39
|
-
this.keyBook = new PeerStoreKeyBook(this.dispatchEvent.bind(this), this.store)
|
|
40
|
-
this.metadataBook = new PeerStoreMetadataBook(this.dispatchEvent.bind(this), this.store)
|
|
41
|
-
this.protoBook = new PeerStoreProtoBook(this.dispatchEvent.bind(this), this.store)
|
|
32
|
+
export class PersistentPeerStore implements PeerStore {
|
|
33
|
+
private readonly store: PersistentStore
|
|
34
|
+
private readonly events: EventEmitter<Libp2pEvents>
|
|
35
|
+
private readonly peerId: PeerId
|
|
36
|
+
|
|
37
|
+
constructor (components: PersistentPeerStoreComponents, init: PersistentPeerStoreInit = {}) {
|
|
38
|
+
this.events = components.events
|
|
39
|
+
this.peerId = components.peerId
|
|
40
|
+
this.store = new PersistentStore(components, init)
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
async forEach (fn: (peer: Peer) => void): Promise<void> {
|
|
45
|
-
log.trace('
|
|
44
|
+
log.trace('forEach await read lock')
|
|
46
45
|
const release = await this.store.lock.readLock()
|
|
47
|
-
log.trace('
|
|
46
|
+
log.trace('forEach got read lock')
|
|
48
47
|
|
|
49
48
|
try {
|
|
50
49
|
for await (const peer of this.store.all()) {
|
|
51
|
-
if (peer.id.equals(this.components.peerId)) {
|
|
52
|
-
// Skip self peer if present
|
|
53
|
-
continue
|
|
54
|
-
}
|
|
55
|
-
|
|
56
50
|
fn(peer)
|
|
57
51
|
}
|
|
58
52
|
} finally {
|
|
59
|
-
log.trace('
|
|
53
|
+
log.trace('forEach release read lock')
|
|
60
54
|
release()
|
|
61
55
|
}
|
|
62
56
|
}
|
|
63
57
|
|
|
64
58
|
async all (): Promise<Peer[]> {
|
|
65
|
-
|
|
59
|
+
log.trace('all await read lock')
|
|
60
|
+
const release = await this.store.lock.readLock()
|
|
61
|
+
log.trace('all got read lock')
|
|
66
62
|
|
|
67
|
-
|
|
68
|
-
output
|
|
69
|
-
|
|
63
|
+
try {
|
|
64
|
+
const output: Peer[] = []
|
|
65
|
+
|
|
66
|
+
for await (const peer of this.store.all()) {
|
|
67
|
+
output.push(peer)
|
|
68
|
+
}
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
return output
|
|
71
|
+
} finally {
|
|
72
|
+
log.trace('all release read lock')
|
|
73
|
+
release()
|
|
74
|
+
}
|
|
72
75
|
}
|
|
73
76
|
|
|
74
|
-
/**
|
|
75
|
-
* Delete the information of the given peer in every book
|
|
76
|
-
*/
|
|
77
77
|
async delete (peerId: PeerId): Promise<void> {
|
|
78
78
|
log.trace('delete await write lock')
|
|
79
79
|
const release = await this.store.lock.writeLock()
|
|
@@ -87,9 +87,19 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> implement
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
async has (peerId: PeerId): Promise<boolean> {
|
|
91
|
+
log.trace('has await read lock')
|
|
92
|
+
const release = await this.store.lock.readLock()
|
|
93
|
+
log.trace('has got read lock')
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
return await this.store.has(peerId)
|
|
97
|
+
} finally {
|
|
98
|
+
log.trace('has release read lock')
|
|
99
|
+
release()
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
93
103
|
async get (peerId: PeerId): Promise<Peer> {
|
|
94
104
|
log.trace('get await read lock')
|
|
95
105
|
const release = await this.store.lock.readLock()
|
|
@@ -103,82 +113,66 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> implement
|
|
|
103
113
|
}
|
|
104
114
|
}
|
|
105
115
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
log.trace('has await read lock')
|
|
111
|
-
const release = await this.store.lock.readLock()
|
|
112
|
-
log.trace('has got read lock')
|
|
116
|
+
async save (id: PeerId, data: PeerData): Promise<Peer> {
|
|
117
|
+
log.trace('save await write lock')
|
|
118
|
+
const release = await this.store.lock.writeLock()
|
|
119
|
+
log.trace('save got write lock')
|
|
113
120
|
|
|
114
121
|
try {
|
|
115
|
-
|
|
122
|
+
const result = await this.store.save(id, data)
|
|
123
|
+
|
|
124
|
+
this.#emitIfUpdated(id, result)
|
|
125
|
+
|
|
126
|
+
return result.peer
|
|
116
127
|
} finally {
|
|
117
|
-
log.trace('
|
|
128
|
+
log.trace('save release write lock')
|
|
118
129
|
release()
|
|
119
130
|
}
|
|
120
131
|
}
|
|
121
132
|
|
|
122
|
-
async
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
133
|
+
async patch (id: PeerId, data: PeerData): Promise<Peer> {
|
|
134
|
+
log.trace('patch await write lock')
|
|
135
|
+
const release = await this.store.lock.writeLock()
|
|
136
|
+
log.trace('patch got write lock')
|
|
126
137
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
138
|
+
try {
|
|
139
|
+
const result = await this.store.patch(id, data)
|
|
130
140
|
|
|
131
|
-
|
|
132
|
-
let tags: Tag[] = []
|
|
141
|
+
this.#emitIfUpdated(id, result)
|
|
133
142
|
|
|
134
|
-
|
|
135
|
-
|
|
143
|
+
return result.peer
|
|
144
|
+
} finally {
|
|
145
|
+
log.trace('patch release write lock')
|
|
146
|
+
release()
|
|
136
147
|
}
|
|
137
|
-
|
|
138
|
-
// do not allow duplicate tags
|
|
139
|
-
tags = tags.filter(t => t.name !== tag)
|
|
140
|
-
|
|
141
|
-
tags.push({
|
|
142
|
-
name: tag,
|
|
143
|
-
value,
|
|
144
|
-
expiry: ttl == null ? undefined : BigInt(Date.now() + ttl)
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
await this.metadataBook.setValue(peerId, 'tags', Tags.encode({ tags }).subarray())
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
async
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
async merge (id: PeerId, data: PeerData): Promise<Peer> {
|
|
151
|
+
log.trace('merge await write lock')
|
|
152
|
+
const release = await this.store.lock.writeLock()
|
|
153
|
+
log.trace('merge got write lock')
|
|
153
154
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
155
|
+
try {
|
|
156
|
+
const result = await this.store.merge(id, data)
|
|
157
157
|
|
|
158
|
-
|
|
158
|
+
this.#emitIfUpdated(id, result)
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
return result.peer
|
|
161
|
+
} finally {
|
|
162
|
+
log.trace('merge release write lock')
|
|
163
|
+
release()
|
|
164
|
+
}
|
|
161
165
|
}
|
|
162
166
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (buf != null) {
|
|
168
|
-
tags = Tags.decode(buf).tags
|
|
167
|
+
#emitIfUpdated (id: PeerId, result: PeerUpdate): void {
|
|
168
|
+
if (!result.updated) {
|
|
169
|
+
return
|
|
169
170
|
}
|
|
170
171
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
// remove any expired tags
|
|
176
|
-
await this.metadataBook.setValue(peerId, 'tags', Tags.encode({ tags: unexpiredTags }).subarray())
|
|
172
|
+
if (this.peerId.equals(id)) {
|
|
173
|
+
this.events.safeDispatchEvent('self:peer:update', { detail: result })
|
|
174
|
+
} else {
|
|
175
|
+
this.events.safeDispatchEvent('peer:update', { detail: result })
|
|
177
176
|
}
|
|
178
|
-
|
|
179
|
-
return unexpiredTags.map(t => ({
|
|
180
|
-
name: t.name,
|
|
181
|
-
value: t.value ?? 0
|
|
182
|
-
}))
|
|
183
177
|
}
|
|
184
178
|
}
|
package/src/pb/peer.proto
CHANGED
|
@@ -7,14 +7,17 @@ message Peer {
|
|
|
7
7
|
// The protocols the peer supports
|
|
8
8
|
repeated string protocols = 2;
|
|
9
9
|
|
|
10
|
-
// Any peer metadata
|
|
11
|
-
repeated Metadata metadata = 3;
|
|
12
|
-
|
|
13
10
|
// The public key of the peer
|
|
14
|
-
optional bytes
|
|
11
|
+
optional bytes public_key = 4;
|
|
15
12
|
|
|
16
13
|
// The most recently received signed PeerRecord
|
|
17
14
|
optional bytes peer_record_envelope = 5;
|
|
15
|
+
|
|
16
|
+
// Any peer metadata
|
|
17
|
+
map<string, bytes> metadata = 6;
|
|
18
|
+
|
|
19
|
+
// Any tags the peer has
|
|
20
|
+
map<string, Tag> tags = 7;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
// Address represents a single multiaddr
|
|
@@ -25,7 +28,7 @@ message Address {
|
|
|
25
28
|
optional bool isCertified = 2;
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
message
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
message Tag {
|
|
32
|
+
uint32 value = 1; // tag value 0-100
|
|
33
|
+
optional uint64 expiry = 2; // ms timestamp after which the tag is no longer valid
|
|
31
34
|
}
|
package/src/pb/peer.ts
CHANGED
|
@@ -11,12 +11,148 @@ import type { Uint8ArrayList } from 'uint8arraylist'
|
|
|
11
11
|
export interface Peer {
|
|
12
12
|
addresses: Address[]
|
|
13
13
|
protocols: string[]
|
|
14
|
-
|
|
15
|
-
pubKey?: Uint8Array
|
|
14
|
+
publicKey?: Uint8Array
|
|
16
15
|
peerRecordEnvelope?: Uint8Array
|
|
16
|
+
metadata: Map<string, Uint8Array>
|
|
17
|
+
tags: Map<string, Tag>
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export namespace Peer {
|
|
21
|
+
export interface Peer$metadataEntry {
|
|
22
|
+
key: string
|
|
23
|
+
value: Uint8Array
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export namespace Peer$metadataEntry {
|
|
27
|
+
let _codec: Codec<Peer$metadataEntry>
|
|
28
|
+
|
|
29
|
+
export const codec = (): Codec<Peer$metadataEntry> => {
|
|
30
|
+
if (_codec == null) {
|
|
31
|
+
_codec = message<Peer$metadataEntry>((obj, w, opts = {}) => {
|
|
32
|
+
if (opts.lengthDelimited !== false) {
|
|
33
|
+
w.fork()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if ((obj.key != null && obj.key !== '')) {
|
|
37
|
+
w.uint32(10)
|
|
38
|
+
w.string(obj.key)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if ((obj.value != null && obj.value.byteLength > 0)) {
|
|
42
|
+
w.uint32(18)
|
|
43
|
+
w.bytes(obj.value)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (opts.lengthDelimited !== false) {
|
|
47
|
+
w.ldelim()
|
|
48
|
+
}
|
|
49
|
+
}, (reader, length) => {
|
|
50
|
+
const obj: any = {
|
|
51
|
+
key: '',
|
|
52
|
+
value: new Uint8Array(0)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const end = length == null ? reader.len : reader.pos + length
|
|
56
|
+
|
|
57
|
+
while (reader.pos < end) {
|
|
58
|
+
const tag = reader.uint32()
|
|
59
|
+
|
|
60
|
+
switch (tag >>> 3) {
|
|
61
|
+
case 1:
|
|
62
|
+
obj.key = reader.string()
|
|
63
|
+
break
|
|
64
|
+
case 2:
|
|
65
|
+
obj.value = reader.bytes()
|
|
66
|
+
break
|
|
67
|
+
default:
|
|
68
|
+
reader.skipType(tag & 7)
|
|
69
|
+
break
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return obj
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return _codec
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const encode = (obj: Partial<Peer$metadataEntry>): Uint8Array => {
|
|
81
|
+
return encodeMessage(obj, Peer$metadataEntry.codec())
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): Peer$metadataEntry => {
|
|
85
|
+
return decodeMessage(buf, Peer$metadataEntry.codec())
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface Peer$tagsEntry {
|
|
90
|
+
key: string
|
|
91
|
+
value?: Tag
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export namespace Peer$tagsEntry {
|
|
95
|
+
let _codec: Codec<Peer$tagsEntry>
|
|
96
|
+
|
|
97
|
+
export const codec = (): Codec<Peer$tagsEntry> => {
|
|
98
|
+
if (_codec == null) {
|
|
99
|
+
_codec = message<Peer$tagsEntry>((obj, w, opts = {}) => {
|
|
100
|
+
if (opts.lengthDelimited !== false) {
|
|
101
|
+
w.fork()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if ((obj.key != null && obj.key !== '')) {
|
|
105
|
+
w.uint32(10)
|
|
106
|
+
w.string(obj.key)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (obj.value != null) {
|
|
110
|
+
w.uint32(18)
|
|
111
|
+
Tag.codec().encode(obj.value, w)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (opts.lengthDelimited !== false) {
|
|
115
|
+
w.ldelim()
|
|
116
|
+
}
|
|
117
|
+
}, (reader, length) => {
|
|
118
|
+
const obj: any = {
|
|
119
|
+
key: ''
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const end = length == null ? reader.len : reader.pos + length
|
|
123
|
+
|
|
124
|
+
while (reader.pos < end) {
|
|
125
|
+
const tag = reader.uint32()
|
|
126
|
+
|
|
127
|
+
switch (tag >>> 3) {
|
|
128
|
+
case 1:
|
|
129
|
+
obj.key = reader.string()
|
|
130
|
+
break
|
|
131
|
+
case 2:
|
|
132
|
+
obj.value = Tag.codec().decode(reader, reader.uint32())
|
|
133
|
+
break
|
|
134
|
+
default:
|
|
135
|
+
reader.skipType(tag & 7)
|
|
136
|
+
break
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return obj
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return _codec
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const encode = (obj: Partial<Peer$tagsEntry>): Uint8Array => {
|
|
148
|
+
return encodeMessage(obj, Peer$tagsEntry.codec())
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): Peer$tagsEntry => {
|
|
152
|
+
return decodeMessage(buf, Peer$tagsEntry.codec())
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
20
156
|
let _codec: Codec<Peer>
|
|
21
157
|
|
|
22
158
|
export const codec = (): Codec<Peer> => {
|
|
@@ -40,16 +176,9 @@ export namespace Peer {
|
|
|
40
176
|
}
|
|
41
177
|
}
|
|
42
178
|
|
|
43
|
-
if (obj.
|
|
44
|
-
for (const value of obj.metadata) {
|
|
45
|
-
w.uint32(26)
|
|
46
|
-
Metadata.codec().encode(value, w)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (obj.pubKey != null) {
|
|
179
|
+
if (obj.publicKey != null) {
|
|
51
180
|
w.uint32(34)
|
|
52
|
-
w.bytes(obj.
|
|
181
|
+
w.bytes(obj.publicKey)
|
|
53
182
|
}
|
|
54
183
|
|
|
55
184
|
if (obj.peerRecordEnvelope != null) {
|
|
@@ -57,6 +186,20 @@ export namespace Peer {
|
|
|
57
186
|
w.bytes(obj.peerRecordEnvelope)
|
|
58
187
|
}
|
|
59
188
|
|
|
189
|
+
if (obj.metadata != null && obj.metadata.size !== 0) {
|
|
190
|
+
for (const [key, value] of obj.metadata.entries()) {
|
|
191
|
+
w.uint32(50)
|
|
192
|
+
Peer.Peer$metadataEntry.codec().encode({ key, value }, w)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (obj.tags != null && obj.tags.size !== 0) {
|
|
197
|
+
for (const [key, value] of obj.tags.entries()) {
|
|
198
|
+
w.uint32(58)
|
|
199
|
+
Peer.Peer$tagsEntry.codec().encode({ key, value }, w)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
60
203
|
if (opts.lengthDelimited !== false) {
|
|
61
204
|
w.ldelim()
|
|
62
205
|
}
|
|
@@ -64,7 +207,8 @@ export namespace Peer {
|
|
|
64
207
|
const obj: any = {
|
|
65
208
|
addresses: [],
|
|
66
209
|
protocols: [],
|
|
67
|
-
metadata:
|
|
210
|
+
metadata: new Map<string, Uint8Array>(),
|
|
211
|
+
tags: new Map<string, undefined>()
|
|
68
212
|
}
|
|
69
213
|
|
|
70
214
|
const end = length == null ? reader.len : reader.pos + length
|
|
@@ -79,15 +223,22 @@ export namespace Peer {
|
|
|
79
223
|
case 2:
|
|
80
224
|
obj.protocols.push(reader.string())
|
|
81
225
|
break
|
|
82
|
-
case 3:
|
|
83
|
-
obj.metadata.push(Metadata.codec().decode(reader, reader.uint32()))
|
|
84
|
-
break
|
|
85
226
|
case 4:
|
|
86
|
-
obj.
|
|
227
|
+
obj.publicKey = reader.bytes()
|
|
87
228
|
break
|
|
88
229
|
case 5:
|
|
89
230
|
obj.peerRecordEnvelope = reader.bytes()
|
|
90
231
|
break
|
|
232
|
+
case 6: {
|
|
233
|
+
const entry = Peer.Peer$metadataEntry.codec().decode(reader, reader.uint32())
|
|
234
|
+
obj.metadata.set(entry.key, entry.value)
|
|
235
|
+
break
|
|
236
|
+
}
|
|
237
|
+
case 7: {
|
|
238
|
+
const entry = Peer.Peer$tagsEntry.codec().decode(reader, reader.uint32())
|
|
239
|
+
obj.tags.set(entry.key, entry.value)
|
|
240
|
+
break
|
|
241
|
+
}
|
|
91
242
|
default:
|
|
92
243
|
reader.skipType(tag & 7)
|
|
93
244
|
break
|
|
@@ -177,29 +328,29 @@ export namespace Address {
|
|
|
177
328
|
}
|
|
178
329
|
}
|
|
179
330
|
|
|
180
|
-
export interface
|
|
181
|
-
|
|
182
|
-
|
|
331
|
+
export interface Tag {
|
|
332
|
+
value: number
|
|
333
|
+
expiry?: bigint
|
|
183
334
|
}
|
|
184
335
|
|
|
185
|
-
export namespace
|
|
186
|
-
let _codec: Codec<
|
|
336
|
+
export namespace Tag {
|
|
337
|
+
let _codec: Codec<Tag>
|
|
187
338
|
|
|
188
|
-
export const codec = (): Codec<
|
|
339
|
+
export const codec = (): Codec<Tag> => {
|
|
189
340
|
if (_codec == null) {
|
|
190
|
-
_codec = message<
|
|
341
|
+
_codec = message<Tag>((obj, w, opts = {}) => {
|
|
191
342
|
if (opts.lengthDelimited !== false) {
|
|
192
343
|
w.fork()
|
|
193
344
|
}
|
|
194
345
|
|
|
195
|
-
if ((obj.
|
|
196
|
-
w.uint32(
|
|
197
|
-
w.
|
|
346
|
+
if ((obj.value != null && obj.value !== 0)) {
|
|
347
|
+
w.uint32(8)
|
|
348
|
+
w.uint32(obj.value)
|
|
198
349
|
}
|
|
199
350
|
|
|
200
|
-
if (
|
|
201
|
-
w.uint32(
|
|
202
|
-
w.
|
|
351
|
+
if (obj.expiry != null) {
|
|
352
|
+
w.uint32(16)
|
|
353
|
+
w.uint64(obj.expiry)
|
|
203
354
|
}
|
|
204
355
|
|
|
205
356
|
if (opts.lengthDelimited !== false) {
|
|
@@ -207,8 +358,7 @@ export namespace Metadata {
|
|
|
207
358
|
}
|
|
208
359
|
}, (reader, length) => {
|
|
209
360
|
const obj: any = {
|
|
210
|
-
|
|
211
|
-
value: new Uint8Array(0)
|
|
361
|
+
value: 0
|
|
212
362
|
}
|
|
213
363
|
|
|
214
364
|
const end = length == null ? reader.len : reader.pos + length
|
|
@@ -218,10 +368,10 @@ export namespace Metadata {
|
|
|
218
368
|
|
|
219
369
|
switch (tag >>> 3) {
|
|
220
370
|
case 1:
|
|
221
|
-
obj.
|
|
371
|
+
obj.value = reader.uint32()
|
|
222
372
|
break
|
|
223
373
|
case 2:
|
|
224
|
-
obj.
|
|
374
|
+
obj.expiry = reader.uint64()
|
|
225
375
|
break
|
|
226
376
|
default:
|
|
227
377
|
reader.skipType(tag & 7)
|
|
@@ -236,11 +386,11 @@ export namespace Metadata {
|
|
|
236
386
|
return _codec
|
|
237
387
|
}
|
|
238
388
|
|
|
239
|
-
export const encode = (obj: Partial<
|
|
240
|
-
return encodeMessage(obj,
|
|
389
|
+
export const encode = (obj: Partial<Tag>): Uint8Array => {
|
|
390
|
+
return encodeMessage(obj, Tag.codec())
|
|
241
391
|
}
|
|
242
392
|
|
|
243
|
-
export const decode = (buf: Uint8Array | Uint8ArrayList):
|
|
244
|
-
return decodeMessage(buf,
|
|
393
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): Tag => {
|
|
394
|
+
return decodeMessage(buf, Tag.codec())
|
|
245
395
|
}
|
|
246
396
|
}
|