@libp2p/kad-dht 12.0.10-3ffecc5bf → 12.0.10-4fc0a7d30
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 +3 -3
- package/dist/src/content-fetching/index.d.ts +2 -1
- package/dist/src/content-fetching/index.d.ts.map +1 -1
- package/dist/src/content-fetching/index.js +5 -1
- package/dist/src/content-fetching/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +1 -1
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-routing/index.js +1 -1
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/query/events.d.ts +1 -1
- package/dist/src/query/events.d.ts.map +1 -1
- package/dist/src/record/index.d.ts +22 -0
- package/dist/src/record/index.d.ts.map +1 -0
- package/dist/src/record/index.js +53 -0
- package/dist/src/record/index.js.map +1 -0
- package/dist/src/record/record.d.ts +13 -0
- package/dist/src/record/record.d.ts.map +1 -0
- package/dist/src/record/record.js +67 -0
- package/dist/src/record/record.js.map +1 -0
- package/dist/src/record/utils.d.ts +11 -0
- package/dist/src/record/utils.d.ts.map +1 -0
- package/dist/src/record/utils.js +41 -0
- package/dist/src/record/utils.js.map +1 -0
- package/dist/src/record/validators.d.ts +1 -1
- package/dist/src/record/validators.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-value.js +1 -1
- package/dist/src/rpc/handlers/get-value.js.map +1 -1
- package/dist/src/rpc/handlers/put-value.js +1 -1
- package/dist/src/rpc/handlers/put-value.js.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +1 -1
- package/dist/src/utils.js.map +1 -1
- package/package.json +18 -19
- package/src/content-fetching/index.ts +6 -1
- package/src/index.ts +1 -1
- package/src/network.ts +1 -1
- package/src/peer-routing/index.ts +1 -1
- package/src/query/events.ts +1 -1
- package/src/record/index.ts +70 -0
- package/src/record/record.proto +20 -0
- package/src/record/record.ts +87 -0
- package/src/record/utils.ts +46 -0
- package/src/record/validators.ts +1 -1
- package/src/rpc/handlers/get-value.ts +1 -1
- package/src/rpc/handlers/put-value.ts +1 -1
- package/src/utils.ts +1 -1
package/dist/src/utils.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
2
|
-
import { Libp2pRecord } from '@libp2p/record';
|
|
3
2
|
import { isPrivateIp } from '@libp2p/utils/private-ip';
|
|
4
3
|
import { Key } from 'interface-datastore/key';
|
|
5
4
|
import { sha256 } from 'multiformats/hashes/sha2';
|
|
@@ -7,6 +6,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat';
|
|
|
7
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
8
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
9
8
|
import { RECORD_KEY_PREFIX } from './constants.js';
|
|
9
|
+
import { Libp2pRecord } from './record/index.js';
|
|
10
10
|
// const IPNS_PREFIX = uint8ArrayFromString('/ipns/')
|
|
11
11
|
const PK_PREFIX = uint8ArrayFromString('/pk/');
|
|
12
12
|
export function removePrivateAddressesMapper(peer) {
|
package/dist/src/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,WAA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,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,CAAA;QAE1C,OAAO,QAAQ,CAAA;IACjB,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "12.0.10-
|
|
3
|
+
"version": "12.0.10-4fc0a7d30",
|
|
4
4
|
"description": "JavaScript implementation of the Kad-DHT for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht#readme",
|
|
@@ -57,14 +57,14 @@
|
|
|
57
57
|
"doc-check": "aegir doc-check"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@libp2p/crypto": "4.0.4-
|
|
61
|
-
"@libp2p/interface": "1.1.5-
|
|
62
|
-
"@libp2p/interface-internal": "1.0.10-
|
|
63
|
-
"@libp2p/peer-collections": "5.1.8-
|
|
64
|
-
"@libp2p/peer-id": "4.0.8-
|
|
65
|
-
"@libp2p/
|
|
66
|
-
"@
|
|
67
|
-
"@
|
|
60
|
+
"@libp2p/crypto": "4.0.4-4fc0a7d30",
|
|
61
|
+
"@libp2p/interface": "1.1.5-4fc0a7d30",
|
|
62
|
+
"@libp2p/interface-internal": "1.0.10-4fc0a7d30",
|
|
63
|
+
"@libp2p/peer-collections": "5.1.8-4fc0a7d30",
|
|
64
|
+
"@libp2p/peer-id": "4.0.8-4fc0a7d30",
|
|
65
|
+
"@libp2p/utils": "5.2.7-4fc0a7d30",
|
|
66
|
+
"@multiformats/multiaddr": "^12.1.14",
|
|
67
|
+
"@types/sinon": "^17.0.3",
|
|
68
68
|
"any-signal": "^4.1.1",
|
|
69
69
|
"hashlru": "^2.3.0",
|
|
70
70
|
"interface-datastore": "^8.2.11",
|
|
@@ -79,27 +79,26 @@
|
|
|
79
79
|
"it-pushable": "^3.2.3",
|
|
80
80
|
"it-take": "^3.0.4",
|
|
81
81
|
"multiformats": "^13.1.0",
|
|
82
|
-
"p-defer": "^4.0.
|
|
83
|
-
"p-event": "^6.0.
|
|
82
|
+
"p-defer": "^4.0.0",
|
|
83
|
+
"p-event": "^6.0.0",
|
|
84
84
|
"p-queue": "^8.0.1",
|
|
85
85
|
"progress-events": "^1.0.0",
|
|
86
86
|
"protons-runtime": "^5.4.0",
|
|
87
87
|
"race-signal": "^1.0.2",
|
|
88
88
|
"uint8-varint": "^2.0.4",
|
|
89
89
|
"uint8arraylist": "^2.4.8",
|
|
90
|
-
"uint8arrays": "^5.0.
|
|
90
|
+
"uint8arrays": "^5.0.2"
|
|
91
91
|
},
|
|
92
92
|
"devDependencies": {
|
|
93
|
-
"@libp2p/interface-compliance-tests": "5.3.3-
|
|
94
|
-
"@libp2p/logger": "4.0.8-
|
|
95
|
-
"@libp2p/peer-id-factory": "4.0.8-
|
|
96
|
-
"@libp2p/peer-store": "10.0.12-
|
|
93
|
+
"@libp2p/interface-compliance-tests": "5.3.3-4fc0a7d30",
|
|
94
|
+
"@libp2p/logger": "4.0.8-4fc0a7d30",
|
|
95
|
+
"@libp2p/peer-id-factory": "4.0.8-4fc0a7d30",
|
|
96
|
+
"@libp2p/peer-store": "10.0.12-4fc0a7d30",
|
|
97
97
|
"@types/lodash.random": "^3.2.9",
|
|
98
98
|
"@types/lodash.range": "^3.2.9",
|
|
99
|
-
"@types/sinon": "^17.0.3",
|
|
100
99
|
"@types/which": "^3.0.3",
|
|
101
|
-
"aegir": "^42.2.
|
|
102
|
-
"datastore-core": "^9.2.
|
|
100
|
+
"aegir": "^42.2.4",
|
|
101
|
+
"datastore-core": "^9.2.8",
|
|
103
102
|
"datastore-level": "^10.1.7",
|
|
104
103
|
"delay": "^6.0.0",
|
|
105
104
|
"execa": "^8.0.1",
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface'
|
|
2
|
-
import { Libp2pRecord } from '@libp2p/record'
|
|
3
2
|
import map from 'it-map'
|
|
4
3
|
import parallel from 'it-parallel'
|
|
5
4
|
import { pipe } from 'it-pipe'
|
|
@@ -12,6 +11,7 @@ import {
|
|
|
12
11
|
valueEvent,
|
|
13
12
|
queryErrorEvent
|
|
14
13
|
} from '../query/events.js'
|
|
14
|
+
import { Libp2pRecord } from '../record/index.js'
|
|
15
15
|
import { bestRecord } from '../record/selectors.js'
|
|
16
16
|
import { verifyRecord } from '../record/validators.js'
|
|
17
17
|
import { createPutRecord, bufferToRecordKey } from '../utils.js'
|
|
@@ -53,6 +53,11 @@ export class ContentFetching {
|
|
|
53
53
|
this.network = network
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
async putLocal (key: Uint8Array, rec: Uint8Array): Promise<void> {
|
|
57
|
+
const dsKey = bufferToRecordKey(key)
|
|
58
|
+
await this.components.datastore.put(dsKey, rec)
|
|
59
|
+
}
|
|
60
|
+
|
|
56
61
|
/**
|
|
57
62
|
* Attempt to retrieve the value for the given key from
|
|
58
63
|
* the local datastore
|
package/src/index.ts
CHANGED
|
@@ -90,7 +90,7 @@ import type { Datastore } from 'interface-datastore'
|
|
|
90
90
|
import type { CID } from 'multiformats/cid'
|
|
91
91
|
import type { ProgressEvent } from 'progress-events'
|
|
92
92
|
|
|
93
|
-
export { Libp2pRecord as Record } from '
|
|
93
|
+
export { Libp2pRecord as Record } from './record/index.js'
|
|
94
94
|
export { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughMapper }
|
|
95
95
|
|
|
96
96
|
/**
|
package/src/network.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { TypedEventEmitter } from '@libp2p/interface'
|
|
2
|
-
import { Libp2pRecord } from '@libp2p/record'
|
|
3
2
|
import { pbStream } from 'it-protobuf-stream'
|
|
4
3
|
import { CodeError } from 'protons-runtime'
|
|
5
4
|
import { Message } from './message/dht.js'
|
|
@@ -10,6 +9,7 @@ import {
|
|
|
10
9
|
peerResponseEvent,
|
|
11
10
|
queryErrorEvent
|
|
12
11
|
} from './query/events.js'
|
|
12
|
+
import { Libp2pRecord } from './record/index.js'
|
|
13
13
|
import type { KadDHTComponents, QueryEvent } from './index.js'
|
|
14
14
|
import type { AbortOptions, Logger, Stream, PeerId, PeerInfo, Startable, RoutingOptions } from '@libp2p/interface'
|
|
15
15
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { keys } from '@libp2p/crypto'
|
|
2
2
|
import { CodeError } from '@libp2p/interface'
|
|
3
3
|
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
4
|
-
import { Libp2pRecord } from '@libp2p/record'
|
|
5
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
6
5
|
import { MessageType } from '../message/dht.js'
|
|
7
6
|
import { PeerDistanceList } from '../peer-list/peer-distance-list.js'
|
|
@@ -10,6 +9,7 @@ import {
|
|
|
10
9
|
finalPeerEvent,
|
|
11
10
|
valueEvent
|
|
12
11
|
} from '../query/events.js'
|
|
12
|
+
import { Libp2pRecord } from '../record/index.js'
|
|
13
13
|
import { verifyRecord } from '../record/validators.js'
|
|
14
14
|
import * as utils from '../utils.js'
|
|
15
15
|
import type { KadDHTComponents, DHTRecord, DialPeerEvent, FinalPeerEvent, QueryEvent, Validators } from '../index.js'
|
package/src/query/events.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CustomEvent } from '@libp2p/interface'
|
|
2
2
|
import type { MessageType, SendQueryEvent, PeerResponseEvent, DialPeerEvent, AddPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent } from '../index.js'
|
|
3
|
+
import type { Libp2pRecord } from '../record/index.js'
|
|
3
4
|
import type { PeerId, PeerInfo } from '@libp2p/interface'
|
|
4
|
-
import type { Libp2pRecord } from '@libp2p/record'
|
|
5
5
|
import type { ProgressOptions } from 'progress-events'
|
|
6
6
|
|
|
7
7
|
export interface QueryEventFields {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Record
|
|
3
|
+
} from './record.js'
|
|
4
|
+
import * as utils from './utils.js'
|
|
5
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
|
+
|
|
7
|
+
export class Libp2pRecord {
|
|
8
|
+
public key: Uint8Array
|
|
9
|
+
public value: Uint8Array
|
|
10
|
+
public timeReceived: Date
|
|
11
|
+
|
|
12
|
+
constructor (key: Uint8Array, value: Uint8Array, timeReceived: Date) {
|
|
13
|
+
if (!(key instanceof Uint8Array)) {
|
|
14
|
+
throw new Error('key must be a Uint8Array')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!(value instanceof Uint8Array)) {
|
|
18
|
+
throw new Error('value must be a Uint8Array')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.key = key
|
|
22
|
+
this.value = value
|
|
23
|
+
this.timeReceived = timeReceived
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
serialize (): Uint8Array {
|
|
27
|
+
return Record.encode(this.prepareSerialize())
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Return the object format ready to be given to the protobuf library.
|
|
32
|
+
*/
|
|
33
|
+
prepareSerialize (): Record {
|
|
34
|
+
return {
|
|
35
|
+
key: this.key,
|
|
36
|
+
value: this.value,
|
|
37
|
+
timeReceived: utils.toRFC3339(this.timeReceived)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Decode a protobuf encoded record
|
|
43
|
+
*/
|
|
44
|
+
static deserialize (raw: Uint8Array | Uint8ArrayList): Libp2pRecord {
|
|
45
|
+
const rec = Record.decode(raw)
|
|
46
|
+
|
|
47
|
+
return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Create a record from the raw object returned from the protobuf library
|
|
52
|
+
*/
|
|
53
|
+
static fromDeserialized (obj: Record): Libp2pRecord {
|
|
54
|
+
const recvtime = utils.parseRFC3339(obj.timeReceived)
|
|
55
|
+
|
|
56
|
+
if (obj.key == null) {
|
|
57
|
+
throw new Error('key missing from deserialized object')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (obj.value == null) {
|
|
61
|
+
throw new Error('value missing from deserialized object')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const rec = new Libp2pRecord(
|
|
65
|
+
obj.key, obj.value, recvtime
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
return rec
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
// Record represents a dht record that contains a value
|
|
4
|
+
// for a key value pair
|
|
5
|
+
message Record {
|
|
6
|
+
// The key that references this record
|
|
7
|
+
bytes key = 1;
|
|
8
|
+
|
|
9
|
+
// The actual value this record is storing
|
|
10
|
+
bytes value = 2;
|
|
11
|
+
|
|
12
|
+
// Note: These fields were removed from the Record message
|
|
13
|
+
// hash of the authors public key
|
|
14
|
+
// optional bytes author = 3;
|
|
15
|
+
// A PKI signature for the key+value+author
|
|
16
|
+
// optional bytes signature = 4;
|
|
17
|
+
|
|
18
|
+
// Time the record was received, set by receiver
|
|
19
|
+
string timeReceived = 5;
|
|
20
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable complexity */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
6
|
+
|
|
7
|
+
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
|
|
8
|
+
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
10
|
+
|
|
11
|
+
export interface Record {
|
|
12
|
+
key: Uint8Array
|
|
13
|
+
value: Uint8Array
|
|
14
|
+
timeReceived: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export namespace Record {
|
|
18
|
+
let _codec: Codec<Record>
|
|
19
|
+
|
|
20
|
+
export const codec = (): Codec<Record> => {
|
|
21
|
+
if (_codec == null) {
|
|
22
|
+
_codec = message<Record>((obj, w, opts = {}) => {
|
|
23
|
+
if (opts.lengthDelimited !== false) {
|
|
24
|
+
w.fork()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if ((obj.key != null && obj.key.byteLength > 0)) {
|
|
28
|
+
w.uint32(10)
|
|
29
|
+
w.bytes(obj.key)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if ((obj.value != null && obj.value.byteLength > 0)) {
|
|
33
|
+
w.uint32(18)
|
|
34
|
+
w.bytes(obj.value)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if ((obj.timeReceived != null && obj.timeReceived !== '')) {
|
|
38
|
+
w.uint32(42)
|
|
39
|
+
w.string(obj.timeReceived)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (opts.lengthDelimited !== false) {
|
|
43
|
+
w.ldelim()
|
|
44
|
+
}
|
|
45
|
+
}, (reader, length) => {
|
|
46
|
+
const obj: any = {
|
|
47
|
+
key: new Uint8Array(0),
|
|
48
|
+
value: new Uint8Array(0),
|
|
49
|
+
timeReceived: ''
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const end = length == null ? reader.len : reader.pos + length
|
|
53
|
+
|
|
54
|
+
while (reader.pos < end) {
|
|
55
|
+
const tag = reader.uint32()
|
|
56
|
+
|
|
57
|
+
switch (tag >>> 3) {
|
|
58
|
+
case 1:
|
|
59
|
+
obj.key = reader.bytes()
|
|
60
|
+
break
|
|
61
|
+
case 2:
|
|
62
|
+
obj.value = reader.bytes()
|
|
63
|
+
break
|
|
64
|
+
case 5:
|
|
65
|
+
obj.timeReceived = reader.string()
|
|
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<Record>): Uint8Array => {
|
|
81
|
+
return encodeMessage(obj, Record.codec())
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): Record => {
|
|
85
|
+
return decodeMessage(buf, Record.codec())
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a JavaScript date into an `RFC3339Nano` formatted
|
|
3
|
+
* string
|
|
4
|
+
*/
|
|
5
|
+
export function toRFC3339 (time: Date): string {
|
|
6
|
+
const year = time.getUTCFullYear()
|
|
7
|
+
const month = String(time.getUTCMonth() + 1).padStart(2, '0')
|
|
8
|
+
const day = String(time.getUTCDate()).padStart(2, '0')
|
|
9
|
+
const hour = String(time.getUTCHours()).padStart(2, '0')
|
|
10
|
+
const minute = String(time.getUTCMinutes()).padStart(2, '0')
|
|
11
|
+
const seconds = String(time.getUTCSeconds()).padStart(2, '0')
|
|
12
|
+
const milliseconds = time.getUTCMilliseconds()
|
|
13
|
+
const nanoseconds = String(milliseconds * 1000 * 1000).padStart(9, '0')
|
|
14
|
+
|
|
15
|
+
return `${year}-${month}-${day}T${hour}:${minute}:${seconds}.${nanoseconds}Z`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Parses a date string formatted as `RFC3339Nano` into a
|
|
20
|
+
* JavaScript Date object
|
|
21
|
+
*/
|
|
22
|
+
export function parseRFC3339 (time: string): Date {
|
|
23
|
+
const rfc3339Matcher = new RegExp(
|
|
24
|
+
// 2006-01-02T
|
|
25
|
+
'(\\d{4})-(\\d{2})-(\\d{2})T' +
|
|
26
|
+
// 15:04:05
|
|
27
|
+
'(\\d{2}):(\\d{2}):(\\d{2})' +
|
|
28
|
+
// .999999999Z
|
|
29
|
+
'\\.(\\d+)Z'
|
|
30
|
+
)
|
|
31
|
+
const m = String(time).trim().match(rfc3339Matcher)
|
|
32
|
+
|
|
33
|
+
if (m == null) {
|
|
34
|
+
throw new Error('Invalid format')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const year = parseInt(m[1], 10)
|
|
38
|
+
const month = parseInt(m[2], 10) - 1
|
|
39
|
+
const date = parseInt(m[3], 10)
|
|
40
|
+
const hour = parseInt(m[4], 10)
|
|
41
|
+
const minute = parseInt(m[5], 10)
|
|
42
|
+
const second = parseInt(m[6], 10)
|
|
43
|
+
const millisecond = parseInt(m[7].slice(0, -6), 10)
|
|
44
|
+
|
|
45
|
+
return new Date(Date.UTC(year, month, date, hour, minute, second, millisecond))
|
|
46
|
+
}
|
package/src/record/validators.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { CodeError } from '@libp2p/interface'
|
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
3
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
4
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
5
|
+
import type { Libp2pRecord } from './index.js'
|
|
5
6
|
import type { Validators } from '../index.js'
|
|
6
|
-
import type { Libp2pRecord } from '@libp2p/record'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Checks a record and ensures it is still valid.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface'
|
|
2
|
-
import { Libp2pRecord } from '@libp2p/record'
|
|
3
2
|
import {
|
|
4
3
|
MAX_RECORD_AGE
|
|
5
4
|
} from '../../constants.js'
|
|
6
5
|
import { MessageType } from '../../message/dht.js'
|
|
6
|
+
import { Libp2pRecord } from '../../record/index.js'
|
|
7
7
|
import { bufferToRecordKey, isPublicKeyKey, fromPublicKeyKey } from '../../utils.js'
|
|
8
8
|
import type { Message } from '../../message/dht.js'
|
|
9
9
|
import type { PeerRouting } from '../../peer-routing/index.js'
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface'
|
|
2
|
-
import { Libp2pRecord } from '
|
|
2
|
+
import { Libp2pRecord } from '../../record/index.js'
|
|
3
3
|
import { verifyRecord } from '../../record/validators.js'
|
|
4
4
|
import { bufferToRecordKey } from '../../utils.js'
|
|
5
5
|
import type { Validators } from '../../index.js'
|
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
2
|
-
import { Libp2pRecord } from '@libp2p/record'
|
|
3
2
|
import { isPrivateIp } from '@libp2p/utils/private-ip'
|
|
4
3
|
import { Key } from 'interface-datastore/key'
|
|
5
4
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
@@ -7,6 +6,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
|
7
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
9
8
|
import { RECORD_KEY_PREFIX } from './constants.js'
|
|
9
|
+
import { Libp2pRecord } from './record/index.js'
|
|
10
10
|
import type { PeerId, PeerInfo } from '@libp2p/interface'
|
|
11
11
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
12
12
|
|