@libp2p/kad-dht 12.0.15 → 12.0.16-1cd5aae11
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 +4 -4
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +3 -2
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/index.d.ts +34 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/network.d.ts +3 -0
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +33 -8
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-list/peer-distance-list.d.ts +13 -4
- package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -1
- package/dist/src/peer-list/peer-distance-list.js +29 -21
- package/dist/src/peer-list/peer-distance-list.js.map +1 -1
- package/dist/src/peer-routing/index.d.ts +5 -5
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +15 -24
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/query/manager.d.ts +3 -0
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/manager.js +14 -5
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query/query-path.d.ts +6 -6
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query/query-path.js +32 -20
- package/dist/src/query/query-path.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +11 -5
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +84 -42
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/k-bucket.d.ts +80 -115
- package/dist/src/routing-table/k-bucket.d.ts.map +1 -1
- package/dist/src/routing-table/k-bucket.js +165 -311
- package/dist/src/routing-table/k-bucket.js.map +1 -1
- package/dist/src/routing-table/refresh.d.ts.map +1 -1
- package/dist/src/routing-table/refresh.js +9 -4
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/package.json +14 -16
- package/src/constants.ts +1 -1
- package/src/content-routing/index.ts +3 -2
- package/src/index.ts +37 -1
- package/src/network.ts +38 -9
- package/src/peer-list/peer-distance-list.ts +36 -25
- package/src/peer-routing/index.ts +19 -28
- package/src/query/manager.ts +18 -5
- package/src/query/query-path.ts +46 -30
- package/src/routing-table/index.ts +100 -46
- package/src/routing-table/k-bucket.ts +214 -359
- package/src/routing-table/refresh.ts +10 -4
- package/dist/src/query/utils.d.ts +0 -6
- package/dist/src/query/utils.d.ts.map +0 -1
- package/dist/src/query/utils.js +0 -53
- package/dist/src/query/utils.js.map +0 -1
- package/dist/typedoc-urls.json +0 -55
- package/src/query/utils.ts +0 -64
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto'
|
|
2
|
+
import { setMaxListeners } from '@libp2p/interface'
|
|
2
3
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
3
4
|
import length from 'it-length'
|
|
4
5
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
@@ -136,7 +137,12 @@ export class RoutingTableRefresh {
|
|
|
136
137
|
|
|
137
138
|
this.log('starting refreshing cpl %s with key %p (routing table size was %s)', cpl, peerId, this.routingTable.size)
|
|
138
139
|
|
|
139
|
-
const
|
|
140
|
+
const signal = AbortSignal.timeout(this.refreshQueryTimeout)
|
|
141
|
+
setMaxListeners(Infinity, signal)
|
|
142
|
+
|
|
143
|
+
const peers = await length(this.peerRouting.getClosestPeers(peerId.toBytes(), {
|
|
144
|
+
signal
|
|
145
|
+
}))
|
|
140
146
|
|
|
141
147
|
this.log(`found ${peers} peers that were close to imaginary peer %p`, peerId)
|
|
142
148
|
this.log('finished refreshing cpl %s with key %p (routing table size is now %s)', cpl, peerId, this.routingTable.size)
|
|
@@ -165,7 +171,7 @@ export class RoutingTableRefresh {
|
|
|
165
171
|
const randomData = randomBytes(2)
|
|
166
172
|
const randomUint16 = (randomData[1] << 8) + randomData[0]
|
|
167
173
|
|
|
168
|
-
const key = await this._makePeerId(this.routingTable.kb.
|
|
174
|
+
const key = await this._makePeerId(this.routingTable.kb.localPeer.kadId, randomUint16, targetCommonPrefixLength)
|
|
169
175
|
|
|
170
176
|
return peerIdFromBytes(key)
|
|
171
177
|
}
|
|
@@ -241,8 +247,8 @@ export class RoutingTableRefresh {
|
|
|
241
247
|
return
|
|
242
248
|
}
|
|
243
249
|
|
|
244
|
-
for (const {
|
|
245
|
-
const distance = uint8ArrayXor(this.routingTable.kb.
|
|
250
|
+
for (const { kadId } of this.routingTable.kb.toIterable()) {
|
|
251
|
+
const distance = uint8ArrayXor(this.routingTable.kb.localPeer.kadId, kadId)
|
|
246
252
|
let leadingZeros = 0
|
|
247
253
|
|
|
248
254
|
for (const byte of distance) {
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { CleanUpEvents } from './manager.js';
|
|
2
|
-
import type { QueryEvent } from '../index.js';
|
|
3
|
-
import type { Logger, TypedEventTarget } from '@libp2p/interface';
|
|
4
|
-
import type Queue from 'p-queue';
|
|
5
|
-
export declare function queueToGenerator(queue: Queue, signal: AbortSignal, cleanUp: TypedEventTarget<CleanUpEvents>, log: Logger): AsyncGenerator<QueryEvent, void, undefined>;
|
|
6
|
-
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,MAAM,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACjE,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"}
|
package/dist/src/query/utils.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface';
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/query/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAMtC,MAAM,CAAC,KAAK,SAAU,CAAC,CAAC,gBAAgB,CAAE,KAAY,EAAE,MAAmB,EAAE,OAAwC,EAAE,GAAW;IAChI,MAAM,MAAM,GAAG,QAAQ,CAAa;QAClC,UAAU,EAAE,IAAI;KACjB,CAAC,CAAA;IAEF,MAAM,OAAO,GAAG,CAAC,GAAW,EAAQ,EAAE;QACpC,GAAG,CAAC,6DAA6D,EAAE,MAAM,CAAC,cAAc,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;QACpH,KAAK,CAAC,KAAK,EAAE,CAAA;QACb,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACjB,CAAC,CAAA;IAED,MAAM,kBAAkB,GAAG,CAAC,MAAkB,EAAQ,EAAE;QACtD,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACrB,CAAC;IACH,CAAC,CAAA;IAED,MAAM,YAAY,GAAG,CAAC,GAAU,EAAQ,EAAE;QACxC,GAAG,CAAC,aAAa,EAAE,GAAG,CAAC,CAAA;QACvB,OAAO,CAAC,GAAG,CAAC,CAAA;IACd,CAAC,CAAA;IAED,MAAM,WAAW,GAAG,GAAS,EAAE;QAC7B,GAAG,CAAC,YAAY,CAAC,CAAA;QACjB,OAAO,EAAE,CAAA;IACX,CAAC,CAAA;IAED,oDAAoD;IACpD,MAAM,aAAa,GAAG,GAAS,EAAE;QAC/B,GAAG,CAAC,aAAa,CAAC,CAAA;QAClB,OAAO,CAAC,IAAI,SAAS,CAAC,eAAe,EAAE,mBAAmB,CAAC,CAAC,CAAA;IAC9D,CAAC,CAAA;IAED,8EAA8E;IAC9E,oDAAoD;IACpD,MAAM,SAAS,GAAG,GAAS,EAAE;QAC3B,OAAO,EAAE,CAAA;IACX,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,CAAC;QACH,KAAM,CAAC,CAAC,MAAM,CAAA;IAChB,CAAC;YAAS,CAAC;QACT,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;IACnD,CAAC;AACH,CAAC"}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"codec": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.MessageType.codec.html",
|
|
3
|
-
"EventTypes": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.EventTypes.html",
|
|
4
|
-
".:EventTypes": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.EventTypes.html",
|
|
5
|
-
"MessageType": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.MessageType-1.html",
|
|
6
|
-
"AddPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.AddPeerEvent.html",
|
|
7
|
-
".:AddPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.AddPeerEvent.html",
|
|
8
|
-
"DHTRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DHTRecord.html",
|
|
9
|
-
".:DHTRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DHTRecord.html",
|
|
10
|
-
"DialPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DialPeerEvent.html",
|
|
11
|
-
".:DialPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DialPeerEvent.html",
|
|
12
|
-
"FinalPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.FinalPeerEvent.html",
|
|
13
|
-
".:FinalPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.FinalPeerEvent.html",
|
|
14
|
-
"KadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHT.html",
|
|
15
|
-
".:KadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHT.html",
|
|
16
|
-
"KadDHTComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTComponents.html",
|
|
17
|
-
".:KadDHTComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTComponents.html",
|
|
18
|
-
"KadDHTInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTInit.html",
|
|
19
|
-
".:KadDHTInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTInit.html",
|
|
20
|
-
"PeerInfoMapper": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerInfoMapper.html",
|
|
21
|
-
".:PeerInfoMapper": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerInfoMapper.html",
|
|
22
|
-
"PeerResponseEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerResponseEvent.html",
|
|
23
|
-
".:PeerResponseEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerResponseEvent.html",
|
|
24
|
-
"ProviderEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ProviderEvent.html",
|
|
25
|
-
".:ProviderEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ProviderEvent.html",
|
|
26
|
-
"QueryErrorEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryErrorEvent.html",
|
|
27
|
-
".:QueryErrorEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryErrorEvent.html",
|
|
28
|
-
"RoutingTable": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.RoutingTable.html",
|
|
29
|
-
".:RoutingTable": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.RoutingTable.html",
|
|
30
|
-
"SelectFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SelectFn.html",
|
|
31
|
-
".:SelectFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SelectFn.html",
|
|
32
|
-
"SendQueryEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SendQueryEvent.html",
|
|
33
|
-
".:SendQueryEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SendQueryEvent.html",
|
|
34
|
-
"SingleKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SingleKadDHT.html",
|
|
35
|
-
".:SingleKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SingleKadDHT.html",
|
|
36
|
-
"ValidateFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValidateFn.html",
|
|
37
|
-
".:ValidateFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValidateFn.html",
|
|
38
|
-
"ValueEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValueEvent.html",
|
|
39
|
-
".:ValueEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValueEvent.html",
|
|
40
|
-
"DHTProgressEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.DHTProgressEvents.html",
|
|
41
|
-
".:DHTProgressEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.DHTProgressEvents.html",
|
|
42
|
-
"MessageName": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.MessageName.html",
|
|
43
|
-
".:MessageName": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.MessageName.html",
|
|
44
|
-
"QueryEvent": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.QueryEvent.html",
|
|
45
|
-
".:QueryEvent": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.QueryEvent.html",
|
|
46
|
-
"Selectors": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Selectors.html",
|
|
47
|
-
".:Selectors": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Selectors.html",
|
|
48
|
-
"Validators": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Validators.html",
|
|
49
|
-
".:Validators": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Validators.html",
|
|
50
|
-
"kadDHT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.kadDHT-1.html",
|
|
51
|
-
".:kadDHT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.kadDHT-1.html",
|
|
52
|
-
"passthroughMapper": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.passthroughMapper.html",
|
|
53
|
-
"removePrivateAddressesMapper": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.removePrivateAddressesMapper.html",
|
|
54
|
-
"removePublicAddressesMapper": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.removePublicAddressesMapper.html"
|
|
55
|
-
}
|
package/src/query/utils.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface'
|
|
2
|
-
import { pushable } from 'it-pushable'
|
|
3
|
-
import type { CleanUpEvents } from './manager.js'
|
|
4
|
-
import type { QueryEvent } from '../index.js'
|
|
5
|
-
import type { Logger, TypedEventTarget } from '@libp2p/interface'
|
|
6
|
-
import type Queue from 'p-queue'
|
|
7
|
-
|
|
8
|
-
export async function * queueToGenerator (queue: Queue, signal: AbortSignal, cleanUp: TypedEventTarget<CleanUpEvents>, log: Logger): AsyncGenerator<QueryEvent, void, undefined> {
|
|
9
|
-
const stream = pushable<QueryEvent>({
|
|
10
|
-
objectMode: true
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
const cleanup = (err?: Error): void => {
|
|
14
|
-
log('clean up queue, results %d, queue size %d, pending tasks %d', stream.readableLength, queue.size, queue.pending)
|
|
15
|
-
queue.clear()
|
|
16
|
-
stream.end(err)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const onQueueJobComplete = (result: QueryEvent): void => {
|
|
20
|
-
if (result != null) {
|
|
21
|
-
stream.push(result)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const onQueueError = (err: Error): void => {
|
|
26
|
-
log('queue error', err)
|
|
27
|
-
cleanup(err)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const onQueueIdle = (): void => {
|
|
31
|
-
log('queue idle')
|
|
32
|
-
cleanup()
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// clear the queue and throw if the query is aborted
|
|
36
|
-
const onSignalAbort = (): void => {
|
|
37
|
-
log('abort queue')
|
|
38
|
-
cleanup(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'))
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// the user broke out of the loop early, ensure we resolve the deferred result
|
|
42
|
-
// promise and clear the queue of any remaining jobs
|
|
43
|
-
const onCleanUp = (): void => {
|
|
44
|
-
cleanup()
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// add listeners
|
|
48
|
-
queue.on('completed', onQueueJobComplete)
|
|
49
|
-
queue.on('error', onQueueError)
|
|
50
|
-
queue.on('idle', onQueueIdle)
|
|
51
|
-
signal.addEventListener('abort', onSignalAbort)
|
|
52
|
-
cleanUp.addEventListener('cleanup', onCleanUp)
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
yield * stream
|
|
56
|
-
} finally {
|
|
57
|
-
// remove listeners
|
|
58
|
-
queue.removeListener('completed', onQueueJobComplete)
|
|
59
|
-
queue.removeListener('error', onQueueError)
|
|
60
|
-
queue.removeListener('idle', onQueueIdle)
|
|
61
|
-
signal.removeEventListener('abort', onSignalAbort)
|
|
62
|
-
cleanUp.removeEventListener('cleanup', onCleanUp)
|
|
63
|
-
}
|
|
64
|
-
}
|