@libp2p/peer-store 11.0.21 → 11.0.22-2c8ecb455
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 +1 -71
- package/dist/src/constants.d.ts +3 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +3 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/index.d.ts +22 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/pb/peer.d.ts +2 -0
- package/dist/src/pb/peer.d.ts.map +1 -1
- package/dist/src/pb/peer.js +16 -0
- package/dist/src/pb/peer.js.map +1 -1
- package/dist/src/store.d.ts +7 -0
- package/dist/src/store.d.ts.map +1 -1
- package/dist/src/store.js +80 -43
- package/dist/src/store.js.map +1 -1
- package/dist/src/utils/bytes-to-peer.d.ts +3 -1
- package/dist/src/utils/bytes-to-peer.d.ts.map +1 -1
- package/dist/src/utils/bytes-to-peer.js +24 -7
- package/dist/src/utils/bytes-to-peer.js.map +1 -1
- package/dist/src/utils/dedupe-addresses.d.ts +1 -1
- 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-equals.d.ts +3 -0
- package/dist/src/utils/peer-equals.d.ts.map +1 -0
- package/dist/src/utils/peer-equals.js +71 -0
- package/dist/src/utils/peer-equals.js.map +1 -0
- package/dist/src/utils/to-peer-pb.d.ts +3 -2
- package/dist/src/utils/to-peer-pb.d.ts.map +1 -1
- package/dist/src/utils/to-peer-pb.js +8 -2
- package/dist/src/utils/to-peer-pb.js.map +1 -1
- package/package.json +8 -7
- package/src/constants.ts +2 -0
- package/src/index.ts +24 -0
- package/src/pb/peer.proto +6 -0
- package/src/pb/peer.ts +20 -0
- package/src/store.ts +95 -55
- package/src/utils/bytes-to-peer.ts +33 -12
- package/src/utils/dedupe-addresses.ts +1 -1
- package/src/utils/peer-equals.ts +91 -0
- package/src/utils/to-peer-pb.ts +17 -4
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts +0 -4
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts.map +0 -1
- package/dist/src/utils/peer-data-to-datastore-peer.js +0 -91
- package/dist/src/utils/peer-data-to-datastore-peer.js.map +0 -1
- package/dist/typedoc-urls.json +0 -10
- package/src/utils/peer-data-to-datastore-peer.ts +0 -113
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
2
|
+
import type { Peer, Address, Tag } from '../pb/peer.js'
|
|
3
|
+
|
|
4
|
+
export function peerEquals (peerA: Peer, peerB: Peer): boolean {
|
|
5
|
+
return addressesEqual(peerA.addresses, peerB.addresses) &&
|
|
6
|
+
protocolsEqual(peerA.protocols, peerB.protocols) &&
|
|
7
|
+
publicKeyEqual(peerA.publicKey, peerB.publicKey) &&
|
|
8
|
+
peerRecordEnvelope(peerA.peerRecordEnvelope, peerB.peerRecordEnvelope) &&
|
|
9
|
+
metadataEqual(peerA.metadata, peerB.metadata) &&
|
|
10
|
+
tagsEqual(peerA.tags, peerB.tags)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function addressesEqual (addressesA: Address[], addressesB: Address[]): boolean {
|
|
14
|
+
return compareArrays(addressesA, addressesB, (a, b) => {
|
|
15
|
+
if (a.isCertified !== b.isCertified) {
|
|
16
|
+
return false
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!uint8ArrayEquals(a.multiaddr, b.multiaddr)) {
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return true
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function protocolsEqual (protocolsA: string[], protocolsB: string[]): boolean {
|
|
28
|
+
return compareArrays(protocolsA, protocolsB, (a, b) => a === b)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function publicKeyEqual (publicKeyA?: Uint8Array, publicKeyB?: Uint8Array): boolean {
|
|
32
|
+
return compareOptionalUint8Arrays(publicKeyA, publicKeyB)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function peerRecordEnvelope (envelopeA?: Uint8Array, envelopeB?: Uint8Array): boolean {
|
|
36
|
+
return compareOptionalUint8Arrays(envelopeA, envelopeB)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function metadataEqual (metadataA: Map<string, Uint8Array>, metadataB: Map<string, Uint8Array>): boolean {
|
|
40
|
+
return compareMaps(metadataA, metadataB, (a, b) => uint8ArrayEquals(a, b))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function tagsEqual (metadataA: Map<string, Tag>, metadataB: Map<string, Tag>): boolean {
|
|
44
|
+
return compareMaps(metadataA, metadataB, (a, b) => a.value === b.value && a.expiry === b.expiry)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function compareOptionalUint8Arrays (arrA?: Uint8Array, arrB?: Uint8Array): boolean {
|
|
48
|
+
if (arrA == null && arrB == null) {
|
|
49
|
+
return true
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (arrA != null && arrB != null) {
|
|
53
|
+
return uint8ArrayEquals(arrA, arrB)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function compareArrays <T> (arrA: T[], arrB: T[], compare: (a: T, b: T) => boolean): boolean {
|
|
60
|
+
if (arrA.length !== arrB.length) {
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < arrA.length; i++) {
|
|
65
|
+
if (!compare(arrA[i], arrB[i])) {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function compareMaps <K, V> (mapA: Map<K, V>, mapB: Map<K, V>, compare: (a: V, b: V) => boolean): boolean {
|
|
74
|
+
if (mapA.size !== mapB.size) {
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (const [key, value] of mapA.entries()) {
|
|
79
|
+
const valueB = mapB.get(key)
|
|
80
|
+
|
|
81
|
+
if (valueB == null) {
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!compare(value, valueB)) {
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return true
|
|
91
|
+
}
|
package/src/utils/to-peer-pb.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
/* eslint-disable complexity */
|
|
1
2
|
import { publicKeyToProtobuf } from '@libp2p/crypto/keys'
|
|
2
3
|
import { InvalidParametersError } from '@libp2p/interface'
|
|
4
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
3
5
|
import { dedupeFilterAndSortAddresses } from './dedupe-addresses.js'
|
|
4
6
|
import type { AddressFilter } from '../index.js'
|
|
5
7
|
import type { Tag, Peer as PeerPB } from '../pb/peer.js'
|
|
6
|
-
import type {
|
|
8
|
+
import type { ExistingPeer } from '../store.js'
|
|
9
|
+
import type { PeerId, Address, PeerData, TagOptions } from '@libp2p/interface'
|
|
7
10
|
|
|
8
11
|
export interface ToPBPeerOptions {
|
|
9
12
|
addressFilter?: AddressFilter
|
|
10
|
-
existingPeer?:
|
|
13
|
+
existingPeer?: ExistingPeer
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strategy: 'merge' | 'patch', options: ToPBPeerOptions): Promise<PeerPB> {
|
|
@@ -19,7 +22,7 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
19
22
|
throw new InvalidParametersError('publicKey bytes do not match peer id publicKey bytes')
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
const existingPeer = options.existingPeer
|
|
25
|
+
const existingPeer = options.existingPeer?.peer
|
|
23
26
|
|
|
24
27
|
if (existingPeer != null && !peerId.equals(existingPeer.id)) {
|
|
25
28
|
throw new InvalidParametersError('peer id did not match existing peer id')
|
|
@@ -141,7 +144,12 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
141
144
|
}
|
|
142
145
|
|
|
143
146
|
const output: PeerPB = {
|
|
144
|
-
addresses: await dedupeFilterAndSortAddresses(
|
|
147
|
+
addresses: await dedupeFilterAndSortAddresses(
|
|
148
|
+
peerId,
|
|
149
|
+
options.addressFilter ?? (async () => true),
|
|
150
|
+
addresses,
|
|
151
|
+
options.existingPeer?.peerPB.addresses
|
|
152
|
+
),
|
|
145
153
|
protocols: [...protocols.values()].sort((a, b) => {
|
|
146
154
|
return a.localeCompare(b)
|
|
147
155
|
}),
|
|
@@ -151,6 +159,11 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
151
159
|
peerRecordEnvelope
|
|
152
160
|
}
|
|
153
161
|
|
|
162
|
+
// add observed addresses to multiaddrs
|
|
163
|
+
output.addresses.forEach(addr => {
|
|
164
|
+
addr.observed = options.existingPeer?.peerPB.addresses?.find(addr => uint8ArrayEquals(addr.multiaddr, addr.multiaddr))?.observed ?? Date.now()
|
|
165
|
+
})
|
|
166
|
+
|
|
154
167
|
// Ed25519 and secp256k1 have their public key embedded in them so no need to duplicate it
|
|
155
168
|
if (peerId.type !== 'RSA') {
|
|
156
169
|
delete output.publicKey
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"peer-data-to-datastore-peer.d.ts","sourceRoot":"","sources":["../../../src/utils/peer-data-to-datastore-peer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAEzD,wBAAgB,eAAe,CAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,CA0GvE"}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { publicKeyToProtobuf } from '@libp2p/crypto/keys';
|
|
2
|
-
import { InvalidParametersError } from '@libp2p/interface';
|
|
3
|
-
import { isMultiaddr } from '@multiformats/multiaddr';
|
|
4
|
-
export function toDatastorePeer(peerId, data) {
|
|
5
|
-
if (data == null) {
|
|
6
|
-
throw new InvalidParametersError('Invalid PeerData');
|
|
7
|
-
}
|
|
8
|
-
if (data.publicKey != null && peerId.publicKey != null && !data.publicKey.equals(peerId.publicKey)) {
|
|
9
|
-
throw new InvalidParametersError('publicKey bytes do not match peer id publicKey bytes');
|
|
10
|
-
}
|
|
11
|
-
// merge addresses and multiaddrs, and dedupe
|
|
12
|
-
const addressSet = new Set();
|
|
13
|
-
const output = {
|
|
14
|
-
addresses: (data.addresses ?? [])
|
|
15
|
-
.concat((data.multiaddrs ?? []).map(multiaddr => ({ multiaddr, isCertified: false })))
|
|
16
|
-
.filter(address => {
|
|
17
|
-
if (!isMultiaddr(address.multiaddr)) {
|
|
18
|
-
throw new InvalidParametersError('Invalid multiaddr');
|
|
19
|
-
}
|
|
20
|
-
if (addressSet.has(address.multiaddr.toString())) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
addressSet.add(address.multiaddr.toString());
|
|
24
|
-
return true;
|
|
25
|
-
})
|
|
26
|
-
.sort((a, b) => {
|
|
27
|
-
return a.multiaddr.toString().localeCompare(b.multiaddr.toString());
|
|
28
|
-
})
|
|
29
|
-
.map(({ multiaddr, isCertified }) => ({
|
|
30
|
-
multiaddr: multiaddr.bytes,
|
|
31
|
-
isCertified
|
|
32
|
-
})),
|
|
33
|
-
protocols: (data.protocols ?? []).sort(),
|
|
34
|
-
metadata: new Map(),
|
|
35
|
-
tags: new Map(),
|
|
36
|
-
publicKey: data.publicKey != null ? publicKeyToProtobuf(data.publicKey) : undefined,
|
|
37
|
-
peerRecordEnvelope: data.peerRecordEnvelope
|
|
38
|
-
};
|
|
39
|
-
// remove invalid metadata
|
|
40
|
-
if (data.metadata != null) {
|
|
41
|
-
const metadataEntries = data.metadata instanceof Map ? data.metadata.entries() : Object.entries(data.metadata);
|
|
42
|
-
for (const [key, value] of metadataEntries) {
|
|
43
|
-
if (typeof key !== 'string') {
|
|
44
|
-
throw new InvalidParametersError('Peer metadata keys must be strings');
|
|
45
|
-
}
|
|
46
|
-
if (value == null) {
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (!(value instanceof Uint8Array)) {
|
|
50
|
-
throw new InvalidParametersError('Peer metadata values must be Uint8Arrays');
|
|
51
|
-
}
|
|
52
|
-
output.metadata.set(key, value);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (data.tags != null) {
|
|
56
|
-
const tagsEntries = data.tags instanceof Map ? data.tags.entries() : Object.entries(data.tags);
|
|
57
|
-
for (const [key, value] of tagsEntries) {
|
|
58
|
-
if (typeof key !== 'string') {
|
|
59
|
-
throw new InvalidParametersError('Peer tag keys must be strings');
|
|
60
|
-
}
|
|
61
|
-
if (value == null) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
const tag = {
|
|
65
|
-
name: key,
|
|
66
|
-
ttl: value.ttl,
|
|
67
|
-
value: value.value ?? 0
|
|
68
|
-
};
|
|
69
|
-
if (tag.value < 0 || tag.value > 100) {
|
|
70
|
-
throw new InvalidParametersError('Tag value must be between 0-100');
|
|
71
|
-
}
|
|
72
|
-
if (parseInt(`${tag.value}`, 10) !== tag.value) {
|
|
73
|
-
throw new InvalidParametersError('Tag value must be an integer');
|
|
74
|
-
}
|
|
75
|
-
if (tag.ttl != null) {
|
|
76
|
-
if (tag.ttl < 0) {
|
|
77
|
-
throw new InvalidParametersError('Tag ttl must be between greater than 0');
|
|
78
|
-
}
|
|
79
|
-
if (parseInt(`${tag.ttl}`, 10) !== tag.ttl) {
|
|
80
|
-
throw new InvalidParametersError('Tag ttl must be an integer');
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
output.tags.set(tag.name, {
|
|
84
|
-
value: tag.value,
|
|
85
|
-
expiry: tag.ttl == null ? undefined : BigInt(Date.now() + tag.ttl)
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return output;
|
|
90
|
-
}
|
|
91
|
-
//# sourceMappingURL=peer-data-to-datastore-peer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"peer-data-to-datastore-peer.js","sourceRoot":"","sources":["../../../src/utils/peer-data-to-datastore-peer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAIrD,MAAM,UAAU,eAAe,CAAE,MAAc,EAAE,IAAc;IAC7D,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QACjB,MAAM,IAAI,sBAAsB,CAAC,kBAAkB,CAAC,CAAA;IACtD,CAAC;IAED,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,IAAI,MAAM,CAAC,SAAS,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;QACnG,MAAM,IAAI,sBAAsB,CAAC,sDAAsD,CAAC,CAAA;IAC1F,CAAC;IAED,6CAA6C;IAC7C,MAAM,UAAU,GAAG,IAAI,GAAG,EAAE,CAAA;IAE5B,MAAM,MAAM,GAAW;QACrB,SAAS,EAAE,CAAC,IAAI,CAAC,SAAS,IAAI,EAAE,CAAC;aAC9B,MAAM,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,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"https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.AddressFilter.html",
|
|
4
|
-
"PersistentPeerStoreComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreComponents.html",
|
|
5
|
-
".:PersistentPeerStoreComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreComponents.html",
|
|
6
|
-
"PersistentPeerStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreInit.html",
|
|
7
|
-
".:PersistentPeerStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreInit.html",
|
|
8
|
-
"persistentPeerStore": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_store.persistentPeerStore.html",
|
|
9
|
-
".:persistentPeerStore": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_store.persistentPeerStore.html"
|
|
10
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { publicKeyToProtobuf } from '@libp2p/crypto/keys'
|
|
2
|
-
import { InvalidParametersError } from '@libp2p/interface'
|
|
3
|
-
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
4
|
-
import type { Peer as PeerPB } from '../pb/peer.js'
|
|
5
|
-
import type { PeerId, PeerData } from '@libp2p/interface'
|
|
6
|
-
|
|
7
|
-
export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
8
|
-
if (data == null) {
|
|
9
|
-
throw new InvalidParametersError('Invalid PeerData')
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (data.publicKey != null && peerId.publicKey != null && !data.publicKey.equals(peerId.publicKey)) {
|
|
13
|
-
throw new InvalidParametersError('publicKey bytes do not match peer id publicKey bytes')
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// merge addresses and multiaddrs, and dedupe
|
|
17
|
-
const addressSet = new Set()
|
|
18
|
-
|
|
19
|
-
const output: PeerPB = {
|
|
20
|
-
addresses: (data.addresses ?? [])
|
|
21
|
-
.concat((data.multiaddrs ?? []).map(multiaddr => ({ multiaddr, isCertified: false })))
|
|
22
|
-
.filter(address => {
|
|
23
|
-
if (!isMultiaddr(address.multiaddr)) {
|
|
24
|
-
throw new InvalidParametersError('Invalid multiaddr')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (addressSet.has(address.multiaddr.toString())) {
|
|
28
|
-
return false
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
addressSet.add(address.multiaddr.toString())
|
|
32
|
-
return true
|
|
33
|
-
})
|
|
34
|
-
.sort((a, b) => {
|
|
35
|
-
return a.multiaddr.toString().localeCompare(b.multiaddr.toString())
|
|
36
|
-
})
|
|
37
|
-
.map(({ multiaddr, isCertified }) => ({
|
|
38
|
-
multiaddr: multiaddr.bytes,
|
|
39
|
-
isCertified
|
|
40
|
-
})),
|
|
41
|
-
protocols: (data.protocols ?? []).sort(),
|
|
42
|
-
metadata: new Map(),
|
|
43
|
-
tags: new Map(),
|
|
44
|
-
publicKey: data.publicKey != null ? publicKeyToProtobuf(data.publicKey) : undefined,
|
|
45
|
-
peerRecordEnvelope: data.peerRecordEnvelope
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// remove invalid metadata
|
|
49
|
-
if (data.metadata != null) {
|
|
50
|
-
const metadataEntries = data.metadata instanceof Map ? data.metadata.entries() : Object.entries(data.metadata)
|
|
51
|
-
|
|
52
|
-
for (const [key, value] of metadataEntries) {
|
|
53
|
-
if (typeof key !== 'string') {
|
|
54
|
-
throw new InvalidParametersError('Peer metadata keys must be strings')
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (value == null) {
|
|
58
|
-
continue
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (!(value instanceof Uint8Array)) {
|
|
62
|
-
throw new InvalidParametersError('Peer metadata values must be Uint8Arrays')
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
output.metadata.set(key, value)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (data.tags != null) {
|
|
70
|
-
const tagsEntries = data.tags instanceof Map ? data.tags.entries() : Object.entries(data.tags)
|
|
71
|
-
|
|
72
|
-
for (const [key, value] of tagsEntries) {
|
|
73
|
-
if (typeof key !== 'string') {
|
|
74
|
-
throw new InvalidParametersError('Peer tag keys must be strings')
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (value == null) {
|
|
78
|
-
continue
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const tag = {
|
|
82
|
-
name: key,
|
|
83
|
-
ttl: value.ttl,
|
|
84
|
-
value: value.value ?? 0
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (tag.value < 0 || tag.value > 100) {
|
|
88
|
-
throw new InvalidParametersError('Tag value must be between 0-100')
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (parseInt(`${tag.value}`, 10) !== tag.value) {
|
|
92
|
-
throw new InvalidParametersError('Tag value must be an integer')
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (tag.ttl != null) {
|
|
96
|
-
if (tag.ttl < 0) {
|
|
97
|
-
throw new InvalidParametersError('Tag ttl must be between greater than 0')
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (parseInt(`${tag.ttl}`, 10) !== tag.ttl) {
|
|
101
|
-
throw new InvalidParametersError('Tag ttl must be an integer')
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
output.tags.set(tag.name, {
|
|
106
|
-
value: tag.value,
|
|
107
|
-
expiry: tag.ttl == null ? undefined : BigInt(Date.now() + tag.ttl)
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return output
|
|
113
|
-
}
|