@libp2p/kad-dht 10.0.14 → 10.0.15-0f5c305af
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 +7 -7
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query/query-path.js +2 -59
- package/dist/src/query/query-path.js.map +1 -1
- package/dist/src/query/utils.d.ts +7 -0
- package/dist/src/query/utils.d.ts.map +1 -0
- package/dist/src/query/utils.js +53 -0
- package/dist/src/query/utils.js.map +1 -0
- package/package.json +12 -10
- package/src/query/query-path.ts +2 -71
- package/src/query/utils.ts +65 -0
- package/dist/typedoc-urls.json +0 -55
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-path.d.ts","sourceRoot":"","sources":["../../../src/query/query-path.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"query-path.d.ts","sourceRoot":"","sources":["../../../src/query/query-path.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAIvD,MAAM,WAAW,gBAAiB,SAAQ,YAAY;IACpD;;OAEG;IACH,GAAG,EAAE,UAAU,CAAA;IAEf;;OAEG;IACH,YAAY,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IAEjB;;OAEG;IACH,MAAM,EAAE,WAAW,CAAA;IAEnB;;OAEG;IACH,KAAK,EAAE,SAAS,CAAA;IAEhB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IAEjB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAEhB;;OAEG;IACH,OAAO,EAAE,gBAAgB,CAAC,aAAa,CAAC,CAAA;IAExC;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;IAEzB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IAEX;;OAEG;IACH,SAAS,EAAE,OAAO,CAAA;CACnB;AAED;;;GAGG;AACH,wBAAwB,SAAS,CAAE,OAAO,EAAE,gBAAgB,GAAG,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,CAAC,CAqGzG"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface/errors';
|
|
2
1
|
import { anySignal } from 'any-signal';
|
|
3
|
-
import defer from 'p-defer';
|
|
4
2
|
import Queue from 'p-queue';
|
|
5
3
|
import { toString } from 'uint8arrays/to-string';
|
|
6
4
|
import { xor } from 'uint8arrays/xor';
|
|
7
5
|
import { convertPeerId, convertBuffer } from '../utils.js';
|
|
8
6
|
import { queryErrorEvent } from './events.js';
|
|
7
|
+
import { queueToGenerator } from './utils.js';
|
|
9
8
|
const MAX_XOR = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF');
|
|
10
9
|
/**
|
|
11
10
|
* Walks a path through the DHT, calling the passed query function for
|
|
@@ -97,62 +96,6 @@ export async function* queryPath(options) {
|
|
|
97
96
|
// begin the query with the starting peer
|
|
98
97
|
queryPeer(startingPeer, await convertPeerId(startingPeer));
|
|
99
98
|
// yield results as they come in
|
|
100
|
-
yield*
|
|
101
|
-
}
|
|
102
|
-
async function* toGenerator(queue, signal, cleanUp, log) {
|
|
103
|
-
let deferred = defer();
|
|
104
|
-
let running = true;
|
|
105
|
-
const results = [];
|
|
106
|
-
const cleanup = () => {
|
|
107
|
-
if (!running) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
log('clean up queue, results %d, queue size %d, pending tasks %d', results.length, queue.size, queue.pending);
|
|
111
|
-
running = false;
|
|
112
|
-
queue.clear();
|
|
113
|
-
results.splice(0, results.length);
|
|
114
|
-
};
|
|
115
|
-
queue.on('completed', result => {
|
|
116
|
-
results.push(result);
|
|
117
|
-
deferred.resolve();
|
|
118
|
-
});
|
|
119
|
-
queue.on('error', err => {
|
|
120
|
-
log('queue error', err);
|
|
121
|
-
cleanup();
|
|
122
|
-
deferred.reject(err);
|
|
123
|
-
});
|
|
124
|
-
queue.on('idle', () => {
|
|
125
|
-
log('queue idle');
|
|
126
|
-
running = false;
|
|
127
|
-
deferred.resolve();
|
|
128
|
-
});
|
|
129
|
-
// clear the queue and throw if the query is aborted
|
|
130
|
-
signal.addEventListener('abort', () => {
|
|
131
|
-
log('abort queue');
|
|
132
|
-
const wasRunning = running;
|
|
133
|
-
cleanup();
|
|
134
|
-
if (wasRunning) {
|
|
135
|
-
deferred.reject(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'));
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
// the user broke out of the loop early, ensure we resolve the deferred result
|
|
139
|
-
// promise and clear the queue of any remaining jobs
|
|
140
|
-
cleanUp.addEventListener('cleanup', () => {
|
|
141
|
-
cleanup();
|
|
142
|
-
deferred.resolve();
|
|
143
|
-
});
|
|
144
|
-
while (running) { // eslint-disable-line no-unmodified-loop-condition
|
|
145
|
-
await deferred.promise;
|
|
146
|
-
deferred = defer();
|
|
147
|
-
// yield all available results
|
|
148
|
-
while (results.length > 0) {
|
|
149
|
-
const result = results.shift();
|
|
150
|
-
if (result != null) {
|
|
151
|
-
yield result;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
// yield any remaining results
|
|
156
|
-
yield* results;
|
|
99
|
+
yield* queueToGenerator(queue, signal, cleanUp, log);
|
|
157
100
|
}
|
|
158
101
|
//# sourceMappingURL=query-path.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-path.js","sourceRoot":"","sources":["../../../src/query/query-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"query-path.js","sourceRoot":"","sources":["../../../src/query/query-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,KAAK,MAAM,SAAS,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACrC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAS7C,MAAM,OAAO,GAAG,MAAM,CAAC,oEAAoE,CAAC,CAAA;AAgE5F;;;GAGG;AACH,MAAM,CAAC,KAAK,SAAU,CAAC,CAAC,SAAS,CAAE,OAAyB;IAC1D,MAAM,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;IACtI,+EAA+E;IAC/E,yFAAyF;IACzF,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC;QACtB,WAAW,EAAE,KAAK;KACnB,CAAC,CAAA;IAEF,iDAAiD;IACjD,MAAM,KAAK,GAAG,MAAM,aAAa,CAAC,GAAG,CAAC,CAAA;IAEtC;;;OAGG;IACH,SAAS,SAAS,CAAE,IAAY,EAAE,SAAqB;QACrD,IAAI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@@
|
|
|
1
|
+
import type { CleanUpEvents } from './manager.js';
|
|
2
|
+
import type { QueryEvent } from '../index.js';
|
|
3
|
+
import type { TypedEventTarget } from '@libp2p/interface/events';
|
|
4
|
+
import type { Logger } from '@libp2p/logger';
|
|
5
|
+
import type Queue from 'p-queue';
|
|
6
|
+
export declare function queueToGenerator(queue: Queue, signal: AbortSignal, cleanUp: TypedEventTarget<CleanUpEvents>, log: Logger): AsyncGenerator<QueryEvent, void, undefined>;
|
|
7
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/query/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,KAAK,MAAM,SAAS,CAAA;AAEhC,wBAAwB,gBAAgB,CAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,gBAAgB,CAAC,aAAa,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,CAAC,CAwD/K"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
|
2
|
+
import { pushable } from 'it-pushable';
|
|
3
|
+
export async function* queueToGenerator(queue, signal, cleanUp, log) {
|
|
4
|
+
const stream = pushable({
|
|
5
|
+
objectMode: true
|
|
6
|
+
});
|
|
7
|
+
const cleanup = (err) => {
|
|
8
|
+
log('clean up queue, results %d, queue size %d, pending tasks %d', stream.readableLength, queue.size, queue.pending);
|
|
9
|
+
queue.clear();
|
|
10
|
+
stream.end(err);
|
|
11
|
+
};
|
|
12
|
+
const onQueueJobComplete = (result) => {
|
|
13
|
+
if (result != null) {
|
|
14
|
+
stream.push(result);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const onQueueError = (err) => {
|
|
18
|
+
log('queue error', err);
|
|
19
|
+
cleanup(err);
|
|
20
|
+
};
|
|
21
|
+
const onQueueIdle = () => {
|
|
22
|
+
log('queue idle');
|
|
23
|
+
cleanup();
|
|
24
|
+
};
|
|
25
|
+
// clear the queue and throw if the query is aborted
|
|
26
|
+
const onSignalAbort = () => {
|
|
27
|
+
log('abort queue');
|
|
28
|
+
cleanup(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'));
|
|
29
|
+
};
|
|
30
|
+
// the user broke out of the loop early, ensure we resolve the deferred result
|
|
31
|
+
// promise and clear the queue of any remaining jobs
|
|
32
|
+
const onCleanUp = () => {
|
|
33
|
+
cleanup();
|
|
34
|
+
};
|
|
35
|
+
// add listeners
|
|
36
|
+
queue.on('completed', onQueueJobComplete);
|
|
37
|
+
queue.on('error', onQueueError);
|
|
38
|
+
queue.on('idle', onQueueIdle);
|
|
39
|
+
signal.addEventListener('abort', onSignalAbort);
|
|
40
|
+
cleanUp.addEventListener('cleanup', onCleanUp);
|
|
41
|
+
try {
|
|
42
|
+
yield* stream;
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
// remove listeners
|
|
46
|
+
queue.removeListener('completed', onQueueJobComplete);
|
|
47
|
+
queue.removeListener('error', onQueueError);
|
|
48
|
+
queue.removeListener('idle', onQueueIdle);
|
|
49
|
+
signal.removeEventListener('abort', onSignalAbort);
|
|
50
|
+
cleanUp.removeEventListener('cleanup', onCleanUp);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/query/utils.ts"],"names":[],"mappings":"AAAA,O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CAAC,CAAA;IAED,gBAAgB;IAChB,KAAK,CAAC,EAAE,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;IACzC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;IAC/B,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IAC7B,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAA;IAC/C,OAAO,CAAC,gBAAgB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;IAE9C,IAAI;QACF,KAAM,CAAC,CAAC,MAAM,CAAA;KACf;YAAS;QACR,mBAAmB;QACnB,KAAK,CAAC,cAAc,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QACrD,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;QAC3C,KAAK,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;QACzC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAA;QAClD,OAAO,CAAC,mBAAmB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;KAClD;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "10.0.
|
|
3
|
+
"version": "10.0.15-0f5c305af",
|
|
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/master/packages/kad-dht#readme",
|
|
@@ -52,12 +52,12 @@
|
|
|
52
52
|
"dep-check": "aegir dep-check"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@libp2p/crypto": "
|
|
56
|
-
"@libp2p/interface": "
|
|
57
|
-
"@libp2p/interface-internal": "
|
|
58
|
-
"@libp2p/logger": "
|
|
59
|
-
"@libp2p/peer-collections": "
|
|
60
|
-
"@libp2p/peer-id": "
|
|
55
|
+
"@libp2p/crypto": "2.0.8-0f5c305af",
|
|
56
|
+
"@libp2p/interface": "0.1.6-0f5c305af",
|
|
57
|
+
"@libp2p/interface-internal": "0.1.9-0f5c305af",
|
|
58
|
+
"@libp2p/logger": "3.1.0-0f5c305af",
|
|
59
|
+
"@libp2p/peer-collections": "4.0.8-0f5c305af",
|
|
60
|
+
"@libp2p/peer-id": "3.0.6-0f5c305af",
|
|
61
61
|
"@multiformats/multiaddr": "^12.1.5",
|
|
62
62
|
"@types/sinon": "^17.0.0",
|
|
63
63
|
"abortable-iterator": "^5.0.1",
|
|
@@ -74,6 +74,7 @@
|
|
|
74
74
|
"it-merge": "^3.0.0",
|
|
75
75
|
"it-parallel": "^3.0.0",
|
|
76
76
|
"it-pipe": "^3.0.1",
|
|
77
|
+
"it-pushable": "^3.2.1",
|
|
77
78
|
"it-stream-types": "^2.0.1",
|
|
78
79
|
"it-take": "^3.0.1",
|
|
79
80
|
"multiformats": "^12.0.1",
|
|
@@ -88,9 +89,9 @@
|
|
|
88
89
|
"uint8arrays": "^4.0.6"
|
|
89
90
|
},
|
|
90
91
|
"devDependencies": {
|
|
91
|
-
"@libp2p/interface-compliance-tests": "
|
|
92
|
-
"@libp2p/peer-id-factory": "
|
|
93
|
-
"@libp2p/peer-store": "
|
|
92
|
+
"@libp2p/interface-compliance-tests": "4.1.5-0f5c305af",
|
|
93
|
+
"@libp2p/peer-id-factory": "3.0.8-0f5c305af",
|
|
94
|
+
"@libp2p/peer-store": "9.0.9-0f5c305af",
|
|
94
95
|
"@types/lodash.random": "^3.2.6",
|
|
95
96
|
"@types/lodash.range": "^3.2.6",
|
|
96
97
|
"@types/which": "^3.0.0",
|
|
@@ -107,6 +108,7 @@
|
|
|
107
108
|
"protons": "^7.0.2",
|
|
108
109
|
"sinon": "^17.0.0",
|
|
109
110
|
"ts-sinon": "^2.0.2",
|
|
111
|
+
"wherearewe": "^2.0.1",
|
|
110
112
|
"which": "^4.0.0"
|
|
111
113
|
},
|
|
112
114
|
"browser": {
|
package/src/query/query-path.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface/errors'
|
|
2
1
|
import { anySignal } from 'any-signal'
|
|
3
|
-
import defer from 'p-defer'
|
|
4
2
|
import Queue from 'p-queue'
|
|
5
3
|
import { toString } from 'uint8arrays/to-string'
|
|
6
4
|
import { xor } from 'uint8arrays/xor'
|
|
7
5
|
import { convertPeerId, convertBuffer } from '../utils.js'
|
|
8
6
|
import { queryErrorEvent } from './events.js'
|
|
7
|
+
import { queueToGenerator } from './utils.js'
|
|
9
8
|
import type { CleanUpEvents } from './manager.js'
|
|
10
9
|
import type { QueryEvent, QueryOptions } from '../index.js'
|
|
11
10
|
import type { QueryFunc } from '../query/types.js'
|
|
@@ -182,73 +181,5 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
|
|
|
182
181
|
queryPeer(startingPeer, await convertPeerId(startingPeer))
|
|
183
182
|
|
|
184
183
|
// yield results as they come in
|
|
185
|
-
yield *
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async function * toGenerator (queue: Queue, signal: AbortSignal, cleanUp: TypedEventTarget<CleanUpEvents>, log: Logger): AsyncGenerator<QueryEvent, void, undefined> {
|
|
189
|
-
let deferred = defer()
|
|
190
|
-
let running = true
|
|
191
|
-
const results: QueryEvent[] = []
|
|
192
|
-
|
|
193
|
-
const cleanup = (): void => {
|
|
194
|
-
if (!running) {
|
|
195
|
-
return
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
log('clean up queue, results %d, queue size %d, pending tasks %d', results.length, queue.size, queue.pending)
|
|
199
|
-
|
|
200
|
-
running = false
|
|
201
|
-
queue.clear()
|
|
202
|
-
results.splice(0, results.length)
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
queue.on('completed', result => {
|
|
206
|
-
results.push(result)
|
|
207
|
-
deferred.resolve()
|
|
208
|
-
})
|
|
209
|
-
queue.on('error', err => {
|
|
210
|
-
log('queue error', err)
|
|
211
|
-
cleanup()
|
|
212
|
-
deferred.reject(err)
|
|
213
|
-
})
|
|
214
|
-
queue.on('idle', () => {
|
|
215
|
-
log('queue idle')
|
|
216
|
-
running = false
|
|
217
|
-
deferred.resolve()
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
// clear the queue and throw if the query is aborted
|
|
221
|
-
signal.addEventListener('abort', () => {
|
|
222
|
-
log('abort queue')
|
|
223
|
-
const wasRunning = running
|
|
224
|
-
cleanup()
|
|
225
|
-
|
|
226
|
-
if (wasRunning) {
|
|
227
|
-
deferred.reject(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'))
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
// the user broke out of the loop early, ensure we resolve the deferred result
|
|
232
|
-
// promise and clear the queue of any remaining jobs
|
|
233
|
-
cleanUp.addEventListener('cleanup', () => {
|
|
234
|
-
cleanup()
|
|
235
|
-
deferred.resolve()
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
while (running) { // eslint-disable-line no-unmodified-loop-condition
|
|
239
|
-
await deferred.promise
|
|
240
|
-
deferred = defer()
|
|
241
|
-
|
|
242
|
-
// yield all available results
|
|
243
|
-
while (results.length > 0) {
|
|
244
|
-
const result = results.shift()
|
|
245
|
-
|
|
246
|
-
if (result != null) {
|
|
247
|
-
yield result
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// yield any remaining results
|
|
253
|
-
yield * results
|
|
184
|
+
yield * queueToGenerator(queue, signal, cleanUp, log)
|
|
254
185
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { pushable } from 'it-pushable'
|
|
3
|
+
import type { CleanUpEvents } from './manager.js'
|
|
4
|
+
import type { QueryEvent } from '../index.js'
|
|
5
|
+
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
6
|
+
import type { Logger } from '@libp2p/logger'
|
|
7
|
+
import type Queue from 'p-queue'
|
|
8
|
+
|
|
9
|
+
export async function * queueToGenerator (queue: Queue, signal: AbortSignal, cleanUp: TypedEventTarget<CleanUpEvents>, log: Logger): AsyncGenerator<QueryEvent, void, undefined> {
|
|
10
|
+
const stream = pushable<QueryEvent>({
|
|
11
|
+
objectMode: true
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const cleanup = (err?: Error): void => {
|
|
15
|
+
log('clean up queue, results %d, queue size %d, pending tasks %d', stream.readableLength, queue.size, queue.pending)
|
|
16
|
+
queue.clear()
|
|
17
|
+
stream.end(err)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const onQueueJobComplete = (result: QueryEvent): void => {
|
|
21
|
+
if (result != null) {
|
|
22
|
+
stream.push(result)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const onQueueError = (err: Error): void => {
|
|
27
|
+
log('queue error', err)
|
|
28
|
+
cleanup(err)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const onQueueIdle = (): void => {
|
|
32
|
+
log('queue idle')
|
|
33
|
+
cleanup()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// clear the queue and throw if the query is aborted
|
|
37
|
+
const onSignalAbort = (): void => {
|
|
38
|
+
log('abort queue')
|
|
39
|
+
cleanup(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// the user broke out of the loop early, ensure we resolve the deferred result
|
|
43
|
+
// promise and clear the queue of any remaining jobs
|
|
44
|
+
const onCleanUp = (): void => {
|
|
45
|
+
cleanup()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// add listeners
|
|
49
|
+
queue.on('completed', onQueueJobComplete)
|
|
50
|
+
queue.on('error', onQueueError)
|
|
51
|
+
queue.on('idle', onQueueIdle)
|
|
52
|
+
signal.addEventListener('abort', onSignalAbort)
|
|
53
|
+
cleanUp.addEventListener('cleanup', onCleanUp)
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
yield * stream
|
|
57
|
+
} finally {
|
|
58
|
+
// remove listeners
|
|
59
|
+
queue.removeListener('completed', onQueueJobComplete)
|
|
60
|
+
queue.removeListener('error', onQueueError)
|
|
61
|
+
queue.removeListener('idle', onQueueIdle)
|
|
62
|
+
signal.removeEventListener('abort', onSignalAbort)
|
|
63
|
+
cleanUp.removeEventListener('cleanup', onCleanUp)
|
|
64
|
+
}
|
|
65
|
+
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"EventTypes": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.EventTypes.html",
|
|
3
|
-
".:EventTypes": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.EventTypes.html",
|
|
4
|
-
"MessageType": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.MessageType.html",
|
|
5
|
-
".:MessageType": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.MessageType.html",
|
|
6
|
-
"Record": "https://libp2p.github.io/js-libp2p/classes/_libp2p_kad_dht.Record.html",
|
|
7
|
-
"AddPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.AddPeerEvent.html",
|
|
8
|
-
".:AddPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.AddPeerEvent.html",
|
|
9
|
-
"DHTRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DHTRecord.html",
|
|
10
|
-
".:DHTRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DHTRecord.html",
|
|
11
|
-
"DialPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DialPeerEvent.html",
|
|
12
|
-
".:DialPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DialPeerEvent.html",
|
|
13
|
-
"DualKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DualKadDHT.html",
|
|
14
|
-
".:DualKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DualKadDHT.html",
|
|
15
|
-
"FinalPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.FinalPeerEvent.html",
|
|
16
|
-
".:FinalPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.FinalPeerEvent.html",
|
|
17
|
-
"KadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHT.html",
|
|
18
|
-
".:KadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHT.html",
|
|
19
|
-
"KadDHTComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTComponents.html",
|
|
20
|
-
".:KadDHTComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTComponents.html",
|
|
21
|
-
"KadDHTInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTInit.html",
|
|
22
|
-
".:KadDHTInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTInit.html",
|
|
23
|
-
"PeerResponseEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerResponseEvent.html",
|
|
24
|
-
".:PeerResponseEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerResponseEvent.html",
|
|
25
|
-
"ProviderEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ProviderEvent.html",
|
|
26
|
-
".:ProviderEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ProviderEvent.html",
|
|
27
|
-
"QueryErrorEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryErrorEvent.html",
|
|
28
|
-
".:QueryErrorEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryErrorEvent.html",
|
|
29
|
-
"QueryOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryOptions.html",
|
|
30
|
-
".:QueryOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryOptions.html",
|
|
31
|
-
"RoutingTable": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.RoutingTable.html",
|
|
32
|
-
".:RoutingTable": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.RoutingTable.html",
|
|
33
|
-
"SelectFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SelectFn.html",
|
|
34
|
-
".:SelectFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SelectFn.html",
|
|
35
|
-
"SendQueryEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SendQueryEvent.html",
|
|
36
|
-
".:SendQueryEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SendQueryEvent.html",
|
|
37
|
-
"SingleKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SingleKadDHT.html",
|
|
38
|
-
".:SingleKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SingleKadDHT.html",
|
|
39
|
-
"ValidateFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValidateFn.html",
|
|
40
|
-
".:ValidateFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValidateFn.html",
|
|
41
|
-
"ValueEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValueEvent.html",
|
|
42
|
-
".:ValueEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValueEvent.html",
|
|
43
|
-
"DHTProgressEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.DHTProgressEvents.html",
|
|
44
|
-
".:DHTProgressEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.DHTProgressEvents.html",
|
|
45
|
-
"MessageName": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.MessageName.html",
|
|
46
|
-
".:MessageName": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.MessageName.html",
|
|
47
|
-
"QueryEvent": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.QueryEvent.html",
|
|
48
|
-
".:QueryEvent": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.QueryEvent.html",
|
|
49
|
-
"Selectors": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Selectors.html",
|
|
50
|
-
".:Selectors": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Selectors.html",
|
|
51
|
-
"Validators": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Validators.html",
|
|
52
|
-
".:Validators": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Validators.html",
|
|
53
|
-
"kadDHT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.kadDHT-1.html",
|
|
54
|
-
".:kadDHT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.kadDHT-1.html"
|
|
55
|
-
}
|