@libp2p/peer-store 9.0.8 → 9.0.9-0b4a2ee79
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 +1 -1
- package/dist/index.min.js +9 -9
- package/dist/src/index.d.ts +3 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +28 -28
- package/dist/src/index.js.map +1 -1
- package/package.json +12 -12
- package/src/index.ts +30 -30
- package/dist/typedoc-urls.json +0 -10
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Libp2pEvents } from '@libp2p/interface';
|
|
1
|
+
import type { ComponentLogger, Libp2pEvents } from '@libp2p/interface';
|
|
2
2
|
import type { TypedEventTarget } from '@libp2p/interface/events';
|
|
3
3
|
import type { PeerId } from '@libp2p/interface/peer-id';
|
|
4
4
|
import type { PeerStore, Peer, PeerData, PeerQuery } from '@libp2p/interface/peer-store';
|
|
@@ -8,6 +8,7 @@ export interface PersistentPeerStoreComponents {
|
|
|
8
8
|
peerId: PeerId;
|
|
9
9
|
datastore: Datastore;
|
|
10
10
|
events: TypedEventTarget<Libp2pEvents>;
|
|
11
|
+
logger: ComponentLogger;
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
14
|
* Return true to allow storing the passed multiaddr for the passed peer
|
|
@@ -26,6 +27,7 @@ export declare class PersistentPeerStore implements PeerStore {
|
|
|
26
27
|
private readonly store;
|
|
27
28
|
private readonly events;
|
|
28
29
|
private readonly peerId;
|
|
30
|
+
private readonly log;
|
|
29
31
|
constructor(components: PersistentPeerStoreComponents, init?: PersistentPeerStoreInit);
|
|
30
32
|
forEach(fn: (peer: Peer) => void, query?: PeerQuery): Promise<void>;
|
|
31
33
|
all(query?: PeerQuery): Promise<Peer[]>;
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":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G,OAAO,CAAC,IAAI,CAAC;IAerE,GAAG,CAAE,KAAK,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IAaxC,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAatC,GAAG,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAatC,GAAG,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAanC,IAAI,CAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBhD,KAAK,CAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBjD,KAAK,CAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBjD,iBAAiB,CAAE,GAAG,EAAE,UAAU,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAoDnF"}
|
package/dist/src/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger';
|
|
2
1
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record';
|
|
3
2
|
import all from 'it-all';
|
|
4
3
|
import { PersistentStore } from './store.js';
|
|
5
|
-
const log = logger('libp2p:peer-store');
|
|
6
4
|
/**
|
|
7
5
|
* An implementation of PeerStore that stores data in a Datastore
|
|
8
6
|
*/
|
|
@@ -10,119 +8,121 @@ export class PersistentPeerStore {
|
|
|
10
8
|
store;
|
|
11
9
|
events;
|
|
12
10
|
peerId;
|
|
11
|
+
log;
|
|
13
12
|
constructor(components, init = {}) {
|
|
13
|
+
this.log = components.logger.forComponent('libp2p:peer-store');
|
|
14
14
|
this.events = components.events;
|
|
15
15
|
this.peerId = components.peerId;
|
|
16
16
|
this.store = new PersistentStore(components, init);
|
|
17
17
|
}
|
|
18
18
|
async forEach(fn, query) {
|
|
19
|
-
log.trace('forEach await read lock');
|
|
19
|
+
this.log.trace('forEach await read lock');
|
|
20
20
|
const release = await this.store.lock.readLock();
|
|
21
|
-
log.trace('forEach got read lock');
|
|
21
|
+
this.log.trace('forEach got read lock');
|
|
22
22
|
try {
|
|
23
23
|
for await (const peer of this.store.all(query)) {
|
|
24
24
|
fn(peer);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
finally {
|
|
28
|
-
log.trace('forEach release read lock');
|
|
28
|
+
this.log.trace('forEach release read lock');
|
|
29
29
|
release();
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
async all(query) {
|
|
33
|
-
log.trace('all await read lock');
|
|
33
|
+
this.log.trace('all await read lock');
|
|
34
34
|
const release = await this.store.lock.readLock();
|
|
35
|
-
log.trace('all got read lock');
|
|
35
|
+
this.log.trace('all got read lock');
|
|
36
36
|
try {
|
|
37
37
|
return await all(this.store.all(query));
|
|
38
38
|
}
|
|
39
39
|
finally {
|
|
40
|
-
log.trace('all release read lock');
|
|
40
|
+
this.log.trace('all release read lock');
|
|
41
41
|
release();
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
async delete(peerId) {
|
|
45
|
-
log.trace('delete await write lock');
|
|
45
|
+
this.log.trace('delete await write lock');
|
|
46
46
|
const release = await this.store.lock.writeLock();
|
|
47
|
-
log.trace('delete got write lock');
|
|
47
|
+
this.log.trace('delete got write lock');
|
|
48
48
|
try {
|
|
49
49
|
await this.store.delete(peerId);
|
|
50
50
|
}
|
|
51
51
|
finally {
|
|
52
|
-
log.trace('delete release write lock');
|
|
52
|
+
this.log.trace('delete release write lock');
|
|
53
53
|
release();
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
async has(peerId) {
|
|
57
|
-
log.trace('has await read lock');
|
|
57
|
+
this.log.trace('has await read lock');
|
|
58
58
|
const release = await this.store.lock.readLock();
|
|
59
|
-
log.trace('has got read lock');
|
|
59
|
+
this.log.trace('has got read lock');
|
|
60
60
|
try {
|
|
61
61
|
return await this.store.has(peerId);
|
|
62
62
|
}
|
|
63
63
|
finally {
|
|
64
|
-
log.trace('has release read lock');
|
|
64
|
+
this.log.trace('has release read lock');
|
|
65
65
|
release();
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
async get(peerId) {
|
|
69
|
-
log.trace('get await read lock');
|
|
69
|
+
this.log.trace('get await read lock');
|
|
70
70
|
const release = await this.store.lock.readLock();
|
|
71
|
-
log.trace('get got read lock');
|
|
71
|
+
this.log.trace('get got read lock');
|
|
72
72
|
try {
|
|
73
73
|
return await this.store.load(peerId);
|
|
74
74
|
}
|
|
75
75
|
finally {
|
|
76
|
-
log.trace('get release read lock');
|
|
76
|
+
this.log.trace('get release read lock');
|
|
77
77
|
release();
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
async save(id, data) {
|
|
81
|
-
log.trace('save await write lock');
|
|
81
|
+
this.log.trace('save await write lock');
|
|
82
82
|
const release = await this.store.lock.writeLock();
|
|
83
|
-
log.trace('save got write lock');
|
|
83
|
+
this.log.trace('save got write lock');
|
|
84
84
|
try {
|
|
85
85
|
const result = await this.store.save(id, data);
|
|
86
86
|
this.#emitIfUpdated(id, result);
|
|
87
87
|
return result.peer;
|
|
88
88
|
}
|
|
89
89
|
finally {
|
|
90
|
-
log.trace('save release write lock');
|
|
90
|
+
this.log.trace('save release write lock');
|
|
91
91
|
release();
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
async patch(id, data) {
|
|
95
|
-
log.trace('patch await write lock');
|
|
95
|
+
this.log.trace('patch await write lock');
|
|
96
96
|
const release = await this.store.lock.writeLock();
|
|
97
|
-
log.trace('patch got write lock');
|
|
97
|
+
this.log.trace('patch got write lock');
|
|
98
98
|
try {
|
|
99
99
|
const result = await this.store.patch(id, data);
|
|
100
100
|
this.#emitIfUpdated(id, result);
|
|
101
101
|
return result.peer;
|
|
102
102
|
}
|
|
103
103
|
finally {
|
|
104
|
-
log.trace('patch release write lock');
|
|
104
|
+
this.log.trace('patch release write lock');
|
|
105
105
|
release();
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
async merge(id, data) {
|
|
109
|
-
log.trace('merge await write lock');
|
|
109
|
+
this.log.trace('merge await write lock');
|
|
110
110
|
const release = await this.store.lock.writeLock();
|
|
111
|
-
log.trace('merge got write lock');
|
|
111
|
+
this.log.trace('merge got write lock');
|
|
112
112
|
try {
|
|
113
113
|
const result = await this.store.merge(id, data);
|
|
114
114
|
this.#emitIfUpdated(id, result);
|
|
115
115
|
return result.peer;
|
|
116
116
|
}
|
|
117
117
|
finally {
|
|
118
|
-
log.trace('merge release write lock');
|
|
118
|
+
this.log.trace('merge release write lock');
|
|
119
119
|
release();
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
async consumePeerRecord(buf, expectedPeer) {
|
|
123
123
|
const envelope = await RecordEnvelope.openAndCertify(buf, PeerRecord.DOMAIN);
|
|
124
124
|
if (expectedPeer?.equals(envelope.peerId) === false) {
|
|
125
|
-
log('envelope peer id was not the expected peer id - expected: %p received: %p', expectedPeer, envelope.peerId);
|
|
125
|
+
this.log('envelope peer id was not the expected peer id - expected: %p received: %p', expectedPeer, envelope.peerId);
|
|
126
126
|
return false;
|
|
127
127
|
}
|
|
128
128
|
const peerRecord = PeerRecord.createFromProtobuf(envelope.payload);
|
|
@@ -140,7 +140,7 @@ export class PersistentPeerStore {
|
|
|
140
140
|
const storedEnvelope = await RecordEnvelope.createFromProtobuf(peer.peerRecordEnvelope);
|
|
141
141
|
const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload);
|
|
142
142
|
if (storedRecord.seqNumber >= peerRecord.seqNumber) {
|
|
143
|
-
log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber);
|
|
143
|
+
this.log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber);
|
|
144
144
|
return false;
|
|
145
145
|
}
|
|
146
146
|
}
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChE,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,eAAe,EAAmB,MAAM,YAAY,CAAA;AA0B7D;;GAEG;AACH,MAAM,OAAO,mBAAmB;IACb,KAAK,CAAiB;IACtB,MAAM,CAAgC;IACtC,MAAM,CAAQ;IACd,GAAG,CAAQ;IAE5B,YAAa,UAAyC,EAAE,OAAgC,EAAE;QACxF,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,mBAAmB,CAAC,CAAA;QAC9D,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC/B,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC/B,IAAI,CAAC,KAAK,GAAG,IAAI,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC;IAED,KAAK,CAAC,OAAO,CAAE,EAAyB,EAAE,KAAiB;QACzD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAA;QACzC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAA;QAChD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAA;QAEvC,IAAI;YACF,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;gBAC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AC,CAAC;gBACjD,WAAW,EAAE,IAAI;gBACjB,SAAS;aACV,CAAC,CAAC;SACJ,CAAC,CAAA;QAEF,OAAO,IAAI,CAAA;IACb,CAAC;IAED,cAAc,CAAE,EAAU,EAAE,MAAkB;QAC5C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;YACnB,OAAM;SACP;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;YAC1B,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,kBAAkB,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;SACtE;aAAM;YACL,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;SACjE;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/peer-store",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.9-0b4a2ee79",
|
|
4
4
|
"description": "Stores information about peers libp2p knows on the network",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
|
-
"homepage": "https://github.com/libp2p/js-libp2p/tree/
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/peer-store#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
@@ -54,29 +54,29 @@
|
|
|
54
54
|
"test:electron-main": "aegir test -t electron-main"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@libp2p/interface": "
|
|
58
|
-
"@libp2p/
|
|
59
|
-
"@libp2p/peer-
|
|
60
|
-
"@libp2p/peer-id": "
|
|
61
|
-
"@libp2p/peer-
|
|
62
|
-
"@
|
|
63
|
-
"@multiformats/multiaddr": "^12.1.5",
|
|
57
|
+
"@libp2p/interface": "0.1.6-0b4a2ee79",
|
|
58
|
+
"@libp2p/peer-collections": "4.0.8-0b4a2ee79",
|
|
59
|
+
"@libp2p/peer-id": "3.0.6-0b4a2ee79",
|
|
60
|
+
"@libp2p/peer-id-factory": "3.0.8-0b4a2ee79",
|
|
61
|
+
"@libp2p/peer-record": "6.0.9-0b4a2ee79",
|
|
62
|
+
"@multiformats/multiaddr": "^12.1.10",
|
|
64
63
|
"interface-datastore": "^8.2.0",
|
|
65
64
|
"it-all": "^3.0.2",
|
|
66
65
|
"mortice": "^3.0.1",
|
|
67
|
-
"multiformats": "^12.
|
|
66
|
+
"multiformats": "^12.1.3",
|
|
68
67
|
"protons-runtime": "^5.0.0",
|
|
69
68
|
"uint8arraylist": "^2.4.3",
|
|
70
69
|
"uint8arrays": "^4.0.6"
|
|
71
70
|
},
|
|
72
71
|
"devDependencies": {
|
|
73
|
-
"@
|
|
72
|
+
"@libp2p/logger": "3.1.0-0b4a2ee79",
|
|
73
|
+
"@types/sinon": "^17.0.0",
|
|
74
74
|
"aegir": "^41.0.2",
|
|
75
75
|
"datastore-core": "^9.1.1",
|
|
76
76
|
"delay": "^6.0.0",
|
|
77
77
|
"p-defer": "^4.0.0",
|
|
78
78
|
"p-event": "^6.0.0",
|
|
79
|
-
"protons": "^7.0
|
|
79
|
+
"protons": "^7.3.0",
|
|
80
80
|
"sinon": "^17.0.0"
|
|
81
81
|
}
|
|
82
82
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
1
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
3
2
|
import all from 'it-all'
|
|
4
3
|
import { PersistentStore, type PeerUpdate } from './store.js'
|
|
5
|
-
import type { Libp2pEvents } from '@libp2p/interface'
|
|
4
|
+
import type { ComponentLogger, Libp2pEvents, Logger } from '@libp2p/interface'
|
|
6
5
|
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
7
6
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
8
7
|
import type { PeerStore, Peer, PeerData, PeerQuery } from '@libp2p/interface/peer-store'
|
|
9
8
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
10
9
|
import type { Datastore } from 'interface-datastore'
|
|
11
10
|
|
|
12
|
-
const log = logger('libp2p:peer-store')
|
|
13
|
-
|
|
14
11
|
export interface PersistentPeerStoreComponents {
|
|
15
12
|
peerId: PeerId
|
|
16
13
|
datastore: Datastore
|
|
17
14
|
events: TypedEventTarget<Libp2pEvents>
|
|
15
|
+
logger: ComponentLogger
|
|
18
16
|
}
|
|
19
17
|
|
|
20
18
|
/**
|
|
@@ -35,84 +33,86 @@ export class PersistentPeerStore implements PeerStore {
|
|
|
35
33
|
private readonly store: PersistentStore
|
|
36
34
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
37
35
|
private readonly peerId: PeerId
|
|
36
|
+
private readonly log: Logger
|
|
38
37
|
|
|
39
38
|
constructor (components: PersistentPeerStoreComponents, init: PersistentPeerStoreInit = {}) {
|
|
39
|
+
this.log = components.logger.forComponent('libp2p:peer-store')
|
|
40
40
|
this.events = components.events
|
|
41
41
|
this.peerId = components.peerId
|
|
42
42
|
this.store = new PersistentStore(components, init)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
async forEach (fn: (peer: Peer,) => void, query?: PeerQuery): Promise<void> {
|
|
46
|
-
log.trace('forEach await read lock')
|
|
46
|
+
this.log.trace('forEach await read lock')
|
|
47
47
|
const release = await this.store.lock.readLock()
|
|
48
|
-
log.trace('forEach got read lock')
|
|
48
|
+
this.log.trace('forEach got read lock')
|
|
49
49
|
|
|
50
50
|
try {
|
|
51
51
|
for await (const peer of this.store.all(query)) {
|
|
52
52
|
fn(peer)
|
|
53
53
|
}
|
|
54
54
|
} finally {
|
|
55
|
-
log.trace('forEach release read lock')
|
|
55
|
+
this.log.trace('forEach release read lock')
|
|
56
56
|
release()
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
async all (query?: PeerQuery): Promise<Peer[]> {
|
|
61
|
-
log.trace('all await read lock')
|
|
61
|
+
this.log.trace('all await read lock')
|
|
62
62
|
const release = await this.store.lock.readLock()
|
|
63
|
-
log.trace('all got read lock')
|
|
63
|
+
this.log.trace('all got read lock')
|
|
64
64
|
|
|
65
65
|
try {
|
|
66
66
|
return await all(this.store.all(query))
|
|
67
67
|
} finally {
|
|
68
|
-
log.trace('all release read lock')
|
|
68
|
+
this.log.trace('all release read lock')
|
|
69
69
|
release()
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
async delete (peerId: PeerId): Promise<void> {
|
|
74
|
-
log.trace('delete await write lock')
|
|
74
|
+
this.log.trace('delete await write lock')
|
|
75
75
|
const release = await this.store.lock.writeLock()
|
|
76
|
-
log.trace('delete got write lock')
|
|
76
|
+
this.log.trace('delete got write lock')
|
|
77
77
|
|
|
78
78
|
try {
|
|
79
79
|
await this.store.delete(peerId)
|
|
80
80
|
} finally {
|
|
81
|
-
log.trace('delete release write lock')
|
|
81
|
+
this.log.trace('delete release write lock')
|
|
82
82
|
release()
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
async has (peerId: PeerId): Promise<boolean> {
|
|
87
|
-
log.trace('has await read lock')
|
|
87
|
+
this.log.trace('has await read lock')
|
|
88
88
|
const release = await this.store.lock.readLock()
|
|
89
|
-
log.trace('has got read lock')
|
|
89
|
+
this.log.trace('has got read lock')
|
|
90
90
|
|
|
91
91
|
try {
|
|
92
92
|
return await this.store.has(peerId)
|
|
93
93
|
} finally {
|
|
94
|
-
log.trace('has release read lock')
|
|
94
|
+
this.log.trace('has release read lock')
|
|
95
95
|
release()
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async get (peerId: PeerId): Promise<Peer> {
|
|
100
|
-
log.trace('get await read lock')
|
|
100
|
+
this.log.trace('get await read lock')
|
|
101
101
|
const release = await this.store.lock.readLock()
|
|
102
|
-
log.trace('get got read lock')
|
|
102
|
+
this.log.trace('get got read lock')
|
|
103
103
|
|
|
104
104
|
try {
|
|
105
105
|
return await this.store.load(peerId)
|
|
106
106
|
} finally {
|
|
107
|
-
log.trace('get release read lock')
|
|
107
|
+
this.log.trace('get release read lock')
|
|
108
108
|
release()
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
async save (id: PeerId, data: PeerData): Promise<Peer> {
|
|
113
|
-
log.trace('save await write lock')
|
|
113
|
+
this.log.trace('save await write lock')
|
|
114
114
|
const release = await this.store.lock.writeLock()
|
|
115
|
-
log.trace('save got write lock')
|
|
115
|
+
this.log.trace('save got write lock')
|
|
116
116
|
|
|
117
117
|
try {
|
|
118
118
|
const result = await this.store.save(id, data)
|
|
@@ -121,15 +121,15 @@ export class PersistentPeerStore implements PeerStore {
|
|
|
121
121
|
|
|
122
122
|
return result.peer
|
|
123
123
|
} finally {
|
|
124
|
-
log.trace('save release write lock')
|
|
124
|
+
this.log.trace('save release write lock')
|
|
125
125
|
release()
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
async patch (id: PeerId, data: PeerData): Promise<Peer> {
|
|
130
|
-
log.trace('patch await write lock')
|
|
130
|
+
this.log.trace('patch await write lock')
|
|
131
131
|
const release = await this.store.lock.writeLock()
|
|
132
|
-
log.trace('patch got write lock')
|
|
132
|
+
this.log.trace('patch got write lock')
|
|
133
133
|
|
|
134
134
|
try {
|
|
135
135
|
const result = await this.store.patch(id, data)
|
|
@@ -138,15 +138,15 @@ export class PersistentPeerStore implements PeerStore {
|
|
|
138
138
|
|
|
139
139
|
return result.peer
|
|
140
140
|
} finally {
|
|
141
|
-
log.trace('patch release write lock')
|
|
141
|
+
this.log.trace('patch release write lock')
|
|
142
142
|
release()
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
async merge (id: PeerId, data: PeerData): Promise<Peer> {
|
|
147
|
-
log.trace('merge await write lock')
|
|
147
|
+
this.log.trace('merge await write lock')
|
|
148
148
|
const release = await this.store.lock.writeLock()
|
|
149
|
-
log.trace('merge got write lock')
|
|
149
|
+
this.log.trace('merge got write lock')
|
|
150
150
|
|
|
151
151
|
try {
|
|
152
152
|
const result = await this.store.merge(id, data)
|
|
@@ -155,7 +155,7 @@ export class PersistentPeerStore implements PeerStore {
|
|
|
155
155
|
|
|
156
156
|
return result.peer
|
|
157
157
|
} finally {
|
|
158
|
-
log.trace('merge release write lock')
|
|
158
|
+
this.log.trace('merge release write lock')
|
|
159
159
|
release()
|
|
160
160
|
}
|
|
161
161
|
}
|
|
@@ -164,7 +164,7 @@ export class PersistentPeerStore implements PeerStore {
|
|
|
164
164
|
const envelope = await RecordEnvelope.openAndCertify(buf, PeerRecord.DOMAIN)
|
|
165
165
|
|
|
166
166
|
if (expectedPeer?.equals(envelope.peerId) === false) {
|
|
167
|
-
log('envelope peer id was not the expected peer id - expected: %p received: %p', expectedPeer, envelope.peerId)
|
|
167
|
+
this.log('envelope peer id was not the expected peer id - expected: %p received: %p', expectedPeer, envelope.peerId)
|
|
168
168
|
return false
|
|
169
169
|
}
|
|
170
170
|
|
|
@@ -185,7 +185,7 @@ export class PersistentPeerStore implements PeerStore {
|
|
|
185
185
|
const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload)
|
|
186
186
|
|
|
187
187
|
if (storedRecord.seqNumber >= peerRecord.seqNumber) {
|
|
188
|
-
log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber)
|
|
188
|
+
this.log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber)
|
|
189
189
|
return false
|
|
190
190
|
}
|
|
191
191
|
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"PersistentPeerStore": "https://libp2p.github.io/js-libp2p/classes/_libp2p_peer_store.PersistentPeerStore.html",
|
|
3
|
-
".:PersistentPeerStore": "https://libp2p.github.io/js-libp2p/classes/_libp2p_peer_store.PersistentPeerStore.html",
|
|
4
|
-
"AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.AddressFilter.html",
|
|
5
|
-
".:AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.AddressFilter.html",
|
|
6
|
-
"PersistentPeerStoreComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreComponents.html",
|
|
7
|
-
".:PersistentPeerStoreComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreComponents.html",
|
|
8
|
-
"PersistentPeerStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreInit.html",
|
|
9
|
-
".:PersistentPeerStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreInit.html"
|
|
10
|
-
}
|