@libp2p/utils 7.0.15 → 7.0.17
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 -9
- package/dist/index.min.js.map +4 -4
- package/dist/src/abstract-stream.d.ts +2 -2
- package/dist/src/abstract-stream.js +1 -1
- package/dist/src/adaptive-timeout.js +1 -1
- package/dist/src/errors.d.ts +1 -1
- package/dist/src/filters/bloom-filter.d.ts +1 -1
- package/dist/src/filters/bloom-filter.js +1 -1
- package/dist/src/filters/bucket.d.ts +1 -1
- package/dist/src/filters/bucket.js +2 -2
- package/dist/src/filters/cuckoo-filter.d.ts +2 -2
- package/dist/src/filters/cuckoo-filter.js +4 -4
- package/dist/src/filters/fingerprint.d.ts +1 -1
- package/dist/src/filters/index.d.ts +6 -6
- package/dist/src/filters/index.js +3 -3
- package/dist/src/filters/scalable-cuckoo-filter.d.ts +2 -2
- package/dist/src/filters/scalable-cuckoo-filter.js +3 -3
- package/dist/src/get-thin-waist-addresses.js +1 -1
- package/dist/src/index.d.ts +31 -31
- package/dist/src/index.js +31 -31
- package/dist/src/message-queue.js +1 -1
- package/dist/src/mock-muxer.js +1 -1
- package/dist/src/multiaddr/index.d.ts +5 -5
- package/dist/src/multiaddr/index.js +5 -5
- package/dist/src/peer-queue.d.ts +2 -2
- package/dist/src/peer-queue.js +1 -1
- package/dist/src/priority-queue.d.ts +2 -2
- package/dist/src/priority-queue.js +1 -1
- package/dist/src/queue/index.d.ts +5 -4
- package/dist/src/queue/index.d.ts.map +1 -1
- package/dist/src/queue/index.js +4 -3
- package/dist/src/queue/index.js.map +1 -1
- package/dist/src/queue/job.d.ts +5 -4
- package/dist/src/queue/job.d.ts.map +1 -1
- package/dist/src/queue/job.js +10 -5
- package/dist/src/queue/job.js.map +1 -1
- package/dist/src/queue/recipient.d.ts +5 -2
- package/dist/src/queue/recipient.d.ts.map +1 -1
- package/dist/src/queue/recipient.js +4 -2
- package/dist/src/queue/recipient.js.map +1 -1
- package/dist/src/rate-limiter.js +1 -1
- package/dist/src/stream-utils.js +1 -1
- package/package.json +9 -7
- package/src/abstract-stream.ts +2 -2
- package/src/adaptive-timeout.ts +1 -1
- package/src/errors.ts +1 -1
- package/src/filters/bloom-filter.ts +2 -2
- package/src/filters/bucket.ts +2 -2
- package/src/filters/cuckoo-filter.ts +6 -6
- package/src/filters/fingerprint.ts +1 -1
- package/src/filters/index.ts +6 -6
- package/src/filters/scalable-cuckoo-filter.ts +6 -6
- package/src/get-thin-waist-addresses.ts +1 -1
- package/src/index.ts +31 -31
- package/src/message-queue.ts +1 -1
- package/src/mock-muxer.ts +1 -1
- package/src/multiaddr/index.ts +5 -5
- package/src/peer-queue.ts +2 -2
- package/src/priority-queue.ts +2 -2
- package/src/queue/index.ts +10 -6
- package/src/queue/job.ts +13 -7
- package/src/queue/recipient.ts +7 -3
- package/src/rate-limiter.ts +1 -1
- package/src/stream-utils.ts +1 -1
package/dist/src/rate-limiter.js
CHANGED
package/dist/src/stream-utils.js
CHANGED
|
@@ -5,7 +5,7 @@ import { pEvent } from 'p-event';
|
|
|
5
5
|
import { raceSignal } from 'race-signal';
|
|
6
6
|
import * as varint from 'uint8-varint';
|
|
7
7
|
import { Uint8ArrayList } from 'uint8arraylist';
|
|
8
|
-
import { UnexpectedEOFError } from
|
|
8
|
+
import { UnexpectedEOFError } from "./errors.js";
|
|
9
9
|
const DEFAULT_MAX_BUFFER_SIZE = 4_194_304;
|
|
10
10
|
export class UnwrappedError extends Error {
|
|
11
11
|
static name = 'UnwrappedError';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.17",
|
|
4
4
|
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
28
|
"types": "./dist/src/index.d.ts",
|
|
29
|
-
"import": "./dist/src/index.js"
|
|
29
|
+
"import": "./dist/src/index.js",
|
|
30
|
+
"module-sync": "./dist/src/index.js"
|
|
30
31
|
}
|
|
31
32
|
},
|
|
32
33
|
"scripts": {
|
|
@@ -46,13 +47,13 @@
|
|
|
46
47
|
"dependencies": {
|
|
47
48
|
"@chainsafe/is-ip": "^2.1.0",
|
|
48
49
|
"@chainsafe/netmask": "^2.0.0",
|
|
49
|
-
"@libp2p/crypto": "^5.1.
|
|
50
|
-
"@libp2p/interface": "^3.2.
|
|
51
|
-
"@libp2p/logger": "^6.2.
|
|
50
|
+
"@libp2p/crypto": "^5.1.17",
|
|
51
|
+
"@libp2p/interface": "^3.2.2",
|
|
52
|
+
"@libp2p/logger": "^6.2.6",
|
|
52
53
|
"@multiformats/multiaddr": "^13.0.1",
|
|
53
54
|
"@sindresorhus/fnv1a": "^3.1.0",
|
|
54
55
|
"any-signal": "^4.1.1",
|
|
55
|
-
"cborg": "^
|
|
56
|
+
"cborg": "^5.1.0",
|
|
56
57
|
"delay": "^7.0.0",
|
|
57
58
|
"is-loopback-addr": "^2.0.2",
|
|
58
59
|
"it-length-prefixed": "^10.0.1",
|
|
@@ -63,13 +64,14 @@
|
|
|
63
64
|
"netmask": "^2.0.2",
|
|
64
65
|
"p-defer": "^4.0.1",
|
|
65
66
|
"p-event": "^7.0.0",
|
|
67
|
+
"progress-events": "^1.1.0",
|
|
66
68
|
"race-signal": "^2.0.0",
|
|
67
69
|
"uint8-varint": "^2.0.4",
|
|
68
70
|
"uint8arraylist": "^2.4.8",
|
|
69
71
|
"uint8arrays": "^5.1.0"
|
|
70
72
|
},
|
|
71
73
|
"devDependencies": {
|
|
72
|
-
"@libp2p/peer-id": "^6.0.
|
|
74
|
+
"@libp2p/peer-id": "^6.0.8",
|
|
73
75
|
"@types/netmask": "^2.0.5",
|
|
74
76
|
"aegir": "^47.0.22",
|
|
75
77
|
"benchmark": "^2.1.4",
|
package/src/abstract-stream.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { pEvent } from 'p-event'
|
|
2
|
-
import { AbstractMessageStream } from './abstract-message-stream.
|
|
3
|
-
import type { MessageStreamInit } from './abstract-message-stream.
|
|
2
|
+
import { AbstractMessageStream } from './abstract-message-stream.ts'
|
|
3
|
+
import type { MessageStreamInit } from './abstract-message-stream.ts'
|
|
4
4
|
import type { AbortOptions, Stream } from '@libp2p/interface'
|
|
5
5
|
|
|
6
6
|
export interface AbstractStreamInit extends MessageStreamInit {
|
package/src/adaptive-timeout.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { anySignal } from 'any-signal'
|
|
2
2
|
import { setMaxListeners } from 'main-event'
|
|
3
|
-
import { MovingAverage } from './moving-average.
|
|
3
|
+
import { MovingAverage } from './moving-average.ts'
|
|
4
4
|
import type { MetricGroup, Metrics } from '@libp2p/interface'
|
|
5
5
|
import type { ClearableSignal } from 'any-signal'
|
|
6
6
|
|
package/src/errors.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { randomBytes } from '@libp2p/crypto'
|
|
|
3
3
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
4
4
|
import { alloc } from 'uint8arrays/alloc'
|
|
5
5
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
|
-
import { fnv1a } from './hashes.
|
|
7
|
-
import type { Filter } from './index.
|
|
6
|
+
import { fnv1a } from './hashes.ts'
|
|
7
|
+
import type { Filter } from './index.ts'
|
|
8
8
|
|
|
9
9
|
const LN2_SQUARED = Math.LN2 * Math.LN2
|
|
10
10
|
|
package/src/filters/bucket.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
2
|
-
import { Bucket } from './bucket.
|
|
3
|
-
import { Fingerprint, MAX_FINGERPRINT_SIZE } from './fingerprint.
|
|
4
|
-
import { fnv1a } from './hashes.
|
|
5
|
-
import { getRandomInt } from './utils.
|
|
6
|
-
import type { Hash } from './hashes.
|
|
7
|
-
import type { Filter } from './index.
|
|
2
|
+
import { Bucket } from './bucket.ts'
|
|
3
|
+
import { Fingerprint, MAX_FINGERPRINT_SIZE } from './fingerprint.ts'
|
|
4
|
+
import { fnv1a } from './hashes.ts'
|
|
5
|
+
import { getRandomInt } from './utils.ts'
|
|
6
|
+
import type { Hash } from './hashes.ts'
|
|
7
|
+
import type { Filter } from './index.ts'
|
|
8
8
|
|
|
9
9
|
const maxCuckooCount = 500
|
|
10
10
|
|
package/src/filters/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { BloomFilter, createBloomFilter, type BloomFilterOptions } from './bloom-filter.
|
|
2
|
-
export { CuckooFilter, createCuckooFilter, type CuckooFilterInit } from './cuckoo-filter.
|
|
3
|
-
export { ScalableCuckooFilter, createScalableCuckooFilter, type ScalableCuckooFilterInit } from './scalable-cuckoo-filter.
|
|
4
|
-
export type { Bucket } from './bucket.
|
|
5
|
-
export type { Fingerprint } from './fingerprint.
|
|
6
|
-
export type { Hash } from './hashes.
|
|
1
|
+
export { BloomFilter, createBloomFilter, type BloomFilterOptions } from './bloom-filter.ts'
|
|
2
|
+
export { CuckooFilter, createCuckooFilter, type CuckooFilterInit } from './cuckoo-filter.ts'
|
|
3
|
+
export { ScalableCuckooFilter, createScalableCuckooFilter, type ScalableCuckooFilterInit } from './scalable-cuckoo-filter.ts'
|
|
4
|
+
export type { Bucket } from './bucket.ts'
|
|
5
|
+
export type { Fingerprint } from './fingerprint.ts'
|
|
6
|
+
export type { Hash } from './hashes.ts'
|
|
7
7
|
|
|
8
8
|
export interface Filter {
|
|
9
9
|
add(item: Uint8Array | string): void
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
2
|
-
import { CuckooFilter, optimize } from './cuckoo-filter.
|
|
3
|
-
import { fnv1a } from './hashes.
|
|
4
|
-
import { getRandomInt } from './utils.
|
|
5
|
-
import type { CuckooFilterInit } from './cuckoo-filter.
|
|
6
|
-
import type { Hash } from './hashes.
|
|
7
|
-
import type { Filter } from './index.
|
|
2
|
+
import { CuckooFilter, optimize } from './cuckoo-filter.ts'
|
|
3
|
+
import { fnv1a } from './hashes.ts'
|
|
4
|
+
import { getRandomInt } from './utils.ts'
|
|
5
|
+
import type { CuckooFilterInit } from './cuckoo-filter.ts'
|
|
6
|
+
import type { Hash } from './hashes.ts'
|
|
7
|
+
import type { Filter } from './index.ts'
|
|
8
8
|
|
|
9
9
|
export interface ScalableCuckooFilterInit extends CuckooFilterInit {
|
|
10
10
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import os from 'node:os'
|
|
2
|
-
import { isLinkLocalIp } from './link-local-ip.
|
|
2
|
+
import { isLinkLocalIp } from './link-local-ip.ts'
|
|
3
3
|
import { getNetConfig } from './multiaddr/get-net-config.ts'
|
|
4
4
|
import { netConfigToMultiaddr } from './multiaddr/utils.ts'
|
|
5
5
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
package/src/index.ts
CHANGED
|
@@ -4,34 +4,34 @@
|
|
|
4
4
|
* This module contains utility functions used by libp2p modules.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
export * from './filters/index.
|
|
8
|
-
export * from './multiaddr/index.
|
|
9
|
-
export * from './queue/index.
|
|
10
|
-
export * from './abstract-message-stream.
|
|
11
|
-
export * from './abstract-multiaddr-connection.
|
|
12
|
-
export * from './abstract-stream-muxer.
|
|
13
|
-
export * from './abstract-stream.
|
|
14
|
-
export * from './adaptive-timeout.
|
|
15
|
-
export * from './debounce.
|
|
16
|
-
export * from './errors.
|
|
17
|
-
export * from './get-thin-waist-addresses.
|
|
18
|
-
export * from './global-unicast-ip.
|
|
19
|
-
export * from './ip-port-to-multiaddr.
|
|
20
|
-
export * from './is-async-generator.
|
|
21
|
-
export * from './is-generator.
|
|
22
|
-
export * from './is-promise.
|
|
23
|
-
export * from './length-prefixed-decoder.
|
|
24
|
-
export * from './link-local-ip.
|
|
25
|
-
export * from './mock-muxer.
|
|
26
|
-
export * from './mock-stream.
|
|
27
|
-
export * from './moving-average.
|
|
28
|
-
export * from './multiaddr-connection-pair.
|
|
29
|
-
export * from './peer-queue.
|
|
30
|
-
export * from './priority-queue.
|
|
31
|
-
export * from './private-ip.
|
|
32
|
-
export * from './rate-limiter.
|
|
33
|
-
export * from './repeating-task.
|
|
34
|
-
export * from './stream-pair.
|
|
35
|
-
export * from './stream-utils.
|
|
36
|
-
export * from './tracked-list.
|
|
37
|
-
export * from './tracked-map.
|
|
7
|
+
export * from './filters/index.ts'
|
|
8
|
+
export * from './multiaddr/index.ts'
|
|
9
|
+
export * from './queue/index.ts'
|
|
10
|
+
export * from './abstract-message-stream.ts'
|
|
11
|
+
export * from './abstract-multiaddr-connection.ts'
|
|
12
|
+
export * from './abstract-stream-muxer.ts'
|
|
13
|
+
export * from './abstract-stream.ts'
|
|
14
|
+
export * from './adaptive-timeout.ts'
|
|
15
|
+
export * from './debounce.ts'
|
|
16
|
+
export * from './errors.ts'
|
|
17
|
+
export * from './get-thin-waist-addresses.ts'
|
|
18
|
+
export * from './global-unicast-ip.ts'
|
|
19
|
+
export * from './ip-port-to-multiaddr.ts'
|
|
20
|
+
export * from './is-async-generator.ts'
|
|
21
|
+
export * from './is-generator.ts'
|
|
22
|
+
export * from './is-promise.ts'
|
|
23
|
+
export * from './length-prefixed-decoder.ts'
|
|
24
|
+
export * from './link-local-ip.ts'
|
|
25
|
+
export * from './mock-muxer.ts'
|
|
26
|
+
export * from './mock-stream.ts'
|
|
27
|
+
export * from './moving-average.ts'
|
|
28
|
+
export * from './multiaddr-connection-pair.ts'
|
|
29
|
+
export * from './peer-queue.ts'
|
|
30
|
+
export * from './priority-queue.ts'
|
|
31
|
+
export * from './private-ip.ts'
|
|
32
|
+
export * from './rate-limiter.ts'
|
|
33
|
+
export * from './repeating-task.ts'
|
|
34
|
+
export * from './stream-pair.ts'
|
|
35
|
+
export * from './stream-utils.ts'
|
|
36
|
+
export * from './tracked-list.ts'
|
|
37
|
+
export * from './tracked-map.ts'
|
package/src/message-queue.ts
CHANGED
|
@@ -3,7 +3,7 @@ import delay from 'delay'
|
|
|
3
3
|
import { TypedEventEmitter } from 'main-event'
|
|
4
4
|
import { raceSignal } from 'race-signal'
|
|
5
5
|
import { isUint8ArrayList, Uint8ArrayList } from 'uint8arraylist'
|
|
6
|
-
import { Queue } from './queue/index.
|
|
6
|
+
import { Queue } from './queue/index.ts'
|
|
7
7
|
import type { AbortOptions, Logger } from '@libp2p/interface'
|
|
8
8
|
|
|
9
9
|
const DEFAULT_CHUNK_SIZE = 1024 * 64
|
package/src/mock-muxer.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { pushable } from 'it-pushable'
|
|
|
4
4
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
5
|
import { AbstractStreamMuxer } from './abstract-stream-muxer.ts'
|
|
6
6
|
import { AbstractStream } from './abstract-stream.ts'
|
|
7
|
-
import { Queue } from './queue/index.
|
|
7
|
+
import { Queue } from './queue/index.ts'
|
|
8
8
|
import type { SendResult } from './abstract-message-stream.ts'
|
|
9
9
|
import type { AbstractStreamInit } from './abstract-stream.ts'
|
|
10
10
|
import type { AbortOptions, MessageStreamDirection, CreateStreamOptions, StreamMuxerFactory, StreamMuxer, MultiaddrConnection, StreamMuxerOptions } from '@libp2p/interface'
|
package/src/multiaddr/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './get-net-config.ts'
|
|
2
|
-
export * from './is-global-unicast.
|
|
3
|
-
export * from './is-link-local.
|
|
4
|
-
export * from './is-loopback.
|
|
5
|
-
export * from './is-network-address.
|
|
6
|
-
export * from './is-private.
|
|
2
|
+
export * from './is-global-unicast.ts'
|
|
3
|
+
export * from './is-link-local.ts'
|
|
4
|
+
export * from './is-loopback.ts'
|
|
5
|
+
export * from './is-network-address.ts'
|
|
6
|
+
export * from './is-private.ts'
|
package/src/peer-queue.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Queue } from './queue/index.
|
|
2
|
-
import type { Job } from './queue/job.
|
|
1
|
+
import { Queue } from './queue/index.ts'
|
|
2
|
+
import type { Job } from './queue/job.ts'
|
|
3
3
|
import type { AbortOptions, PeerId } from '@libp2p/interface'
|
|
4
4
|
|
|
5
5
|
export interface PeerQueueJobOptions extends AbortOptions {
|
package/src/priority-queue.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Queue } from './queue/index.
|
|
2
|
-
import type { QueueInit } from './queue/index.
|
|
1
|
+
import { Queue } from './queue/index.ts'
|
|
2
|
+
import type { QueueInit } from './queue/index.ts'
|
|
3
3
|
import type { AbortOptions } from '@libp2p/interface'
|
|
4
4
|
|
|
5
5
|
export interface PriorityQueueJobOptions extends AbortOptions {
|
package/src/queue/index.ts
CHANGED
|
@@ -4,11 +4,12 @@ import { TypedEventEmitter } from 'main-event'
|
|
|
4
4
|
import { pEvent } from 'p-event'
|
|
5
5
|
import { debounce } from '../debounce.js'
|
|
6
6
|
import { QueueFullError } from '../errors.js'
|
|
7
|
-
import { Job } from './job.
|
|
7
|
+
import { Job } from './job.ts'
|
|
8
8
|
import type { AbortOptions, Metrics } from '@libp2p/interface'
|
|
9
|
+
import type { ProgressOptions } from 'progress-events'
|
|
9
10
|
|
|
10
|
-
export type { Job, JobTimeline } from './job.
|
|
11
|
-
export type { JobRecipient } from './recipient.
|
|
11
|
+
export type { Job, JobTimeline } from './job.ts'
|
|
12
|
+
export type { JobRecipient } from './recipient.ts'
|
|
12
13
|
|
|
13
14
|
export interface Comparator<T> {
|
|
14
15
|
(a: T, b: T): -1 | 0 | 1
|
|
@@ -118,7 +119,7 @@ export interface QueueEvents<JobReturnType, JobOptions extends AbortOptions = Ab
|
|
|
118
119
|
* 1. Items remain at the head of the queue while they are running so `queue.size` includes `queue.pending` items - this is so interested parties can join the results of a queue item while it is running
|
|
119
120
|
* 2. The options for a job are stored separately to the job in order for them to be modified while they are still in the queue
|
|
120
121
|
*/
|
|
121
|
-
export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = AbortOptions> extends TypedEventEmitter<QueueEvents<JobReturnType, JobOptions>> {
|
|
122
|
+
export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions & ProgressOptions = AbortOptions> extends TypedEventEmitter<QueueEvents<JobReturnType, JobOptions>> {
|
|
122
123
|
public concurrency: number
|
|
123
124
|
public maxSize: number
|
|
124
125
|
public queue: Array<Job<JobOptions, JobReturnType>>
|
|
@@ -257,9 +258,8 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
257
258
|
const job = new Job<JobOptions, JobReturnType>(fn, options)
|
|
258
259
|
this.enqueue(job)
|
|
259
260
|
this.safeDispatchEvent('add')
|
|
260
|
-
this.tryToStartAnother()
|
|
261
261
|
|
|
262
|
-
|
|
262
|
+
const result = job.join(options)
|
|
263
263
|
.then(result => {
|
|
264
264
|
this.safeDispatchEvent('completed', { detail: result })
|
|
265
265
|
this.safeDispatchEvent('success', { detail: { job, result } })
|
|
@@ -281,6 +281,10 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
281
281
|
|
|
282
282
|
throw err
|
|
283
283
|
})
|
|
284
|
+
|
|
285
|
+
this.tryToStartAnother()
|
|
286
|
+
|
|
287
|
+
return result
|
|
284
288
|
}
|
|
285
289
|
|
|
286
290
|
/**
|
package/src/queue/job.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { AbortError } from '@libp2p/interface'
|
|
2
2
|
import { setMaxListeners } from 'main-event'
|
|
3
3
|
import { raceSignal } from 'race-signal'
|
|
4
|
-
import { JobRecipient } from './recipient.
|
|
5
|
-
import type { JobStatus } from './index.
|
|
4
|
+
import { JobRecipient } from './recipient.ts'
|
|
5
|
+
import type { JobStatus } from './index.ts'
|
|
6
6
|
import type { AbortOptions } from '@libp2p/interface'
|
|
7
|
+
import type { ProgressOptions } from 'progress-events'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Returns a random string
|
|
@@ -18,7 +19,7 @@ export interface JobTimeline {
|
|
|
18
19
|
finished?: number
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
export class Job <JobOptions extends AbortOptions = AbortOptions, JobReturnType = unknown> {
|
|
22
|
+
export class Job <JobOptions extends AbortOptions & ProgressOptions = AbortOptions, JobReturnType = unknown> {
|
|
22
23
|
public id: string
|
|
23
24
|
public fn: (options: JobOptions) => Promise<JobReturnType>
|
|
24
25
|
public options: JobOptions
|
|
@@ -59,11 +60,11 @@ export class Job <JobOptions extends AbortOptions = AbortOptions, JobReturnType
|
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
async join (options
|
|
63
|
-
const recipient = new JobRecipient<JobReturnType>(options
|
|
63
|
+
async join (options?: Partial<Pick<JobOptions, 'signal' | 'onProgress'>>): Promise<JobReturnType> {
|
|
64
|
+
const recipient = new JobRecipient<JobReturnType>(options)
|
|
64
65
|
this.recipients.push(recipient)
|
|
65
66
|
|
|
66
|
-
options
|
|
67
|
+
options?.signal?.addEventListener('abort', this.onAbort)
|
|
67
68
|
|
|
68
69
|
return recipient.deferred.promise
|
|
69
70
|
}
|
|
@@ -77,7 +78,12 @@ export class Job <JobOptions extends AbortOptions = AbortOptions, JobReturnType
|
|
|
77
78
|
|
|
78
79
|
const result = await raceSignal(this.fn({
|
|
79
80
|
...(this.options ?? {}),
|
|
80
|
-
signal: this.controller.signal
|
|
81
|
+
signal: this.controller.signal,
|
|
82
|
+
onProgress: (evt: any): void => {
|
|
83
|
+
this.recipients.forEach(recipient => {
|
|
84
|
+
recipient.onProgress?.(evt)
|
|
85
|
+
})
|
|
86
|
+
}
|
|
81
87
|
}), this.controller.signal)
|
|
82
88
|
|
|
83
89
|
this.recipients.forEach(recipient => {
|
package/src/queue/recipient.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { AbortError } from '@libp2p/interface'
|
|
2
2
|
import pDefer from 'p-defer'
|
|
3
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
3
4
|
import type { DeferredPromise } from 'p-defer'
|
|
5
|
+
import type { ProgressOptions, ProgressEventListener } from 'progress-events'
|
|
4
6
|
|
|
5
|
-
export class JobRecipient<JobReturnType> {
|
|
7
|
+
export class JobRecipient<JobReturnType, JobOptions extends AbortOptions & ProgressOptions = any> {
|
|
6
8
|
public deferred: DeferredPromise<JobReturnType>
|
|
7
9
|
public signal?: AbortSignal
|
|
10
|
+
public onProgress?: ProgressEventListener
|
|
8
11
|
|
|
9
|
-
constructor (
|
|
10
|
-
this.signal = signal
|
|
12
|
+
constructor (options?: Partial<Pick<JobOptions, 'signal' | 'onProgress'>>) {
|
|
13
|
+
this.signal = options?.signal
|
|
14
|
+
this.onProgress = options?.onProgress
|
|
11
15
|
this.deferred = pDefer()
|
|
12
16
|
|
|
13
17
|
this.onAbort = this.onAbort.bind(this)
|
package/src/rate-limiter.ts
CHANGED
package/src/stream-utils.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { pEvent } from 'p-event'
|
|
|
5
5
|
import { raceSignal } from 'race-signal'
|
|
6
6
|
import * as varint from 'uint8-varint'
|
|
7
7
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
8
|
-
import { UnexpectedEOFError } from './errors.
|
|
8
|
+
import { UnexpectedEOFError } from './errors.ts'
|
|
9
9
|
import type { MessageStream, MultiaddrConnection, Stream, AbortOptions } from '@libp2p/interface'
|
|
10
10
|
import type { Duplex, Source, Transform, Sink } from 'it-stream-types'
|
|
11
11
|
|