@libp2p/utils 6.6.2-f1de46607 → 6.6.3-aa25d38ab
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/src/abstract-stream.d.ts +1 -1
- package/dist/src/abstract-stream.d.ts.map +1 -1
- package/dist/src/abstract-stream.js +6 -7
- package/dist/src/abstract-stream.js.map +1 -1
- package/dist/src/adaptive-timeout.d.ts +2 -3
- package/dist/src/adaptive-timeout.d.ts.map +1 -1
- package/dist/src/adaptive-timeout.js +1 -1
- package/dist/src/adaptive-timeout.js.map +1 -1
- package/dist/src/debounce.js.map +1 -1
- package/dist/src/filters/cuckoo-filter.d.ts +1 -1
- package/dist/src/filters/cuckoo-filter.d.ts.map +1 -1
- package/dist/src/filters/cuckoo-filter.js.map +1 -1
- package/dist/src/filters/fingerprint.d.ts +1 -1
- package/dist/src/filters/fingerprint.d.ts.map +1 -1
- package/dist/src/filters/scalable-cuckoo-filter.d.ts +1 -1
- package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -1
- package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -1
- package/dist/src/ip-port-to-multiaddr.d.ts +1 -1
- package/dist/src/ip-port-to-multiaddr.d.ts.map +1 -1
- package/dist/src/ip-port-to-multiaddr.js +1 -1
- package/dist/src/ip-port-to-multiaddr.js.map +1 -1
- package/dist/src/merge-options.d.ts +7 -0
- package/dist/src/merge-options.d.ts.map +1 -0
- package/dist/src/merge-options.js +128 -0
- package/dist/src/merge-options.js.map +1 -0
- package/dist/src/private-ip.d.ts.map +1 -1
- package/dist/src/private-ip.js +12 -6
- package/dist/src/private-ip.js.map +1 -1
- package/dist/src/queue/index.d.ts +11 -9
- package/dist/src/queue/index.d.ts.map +1 -1
- package/dist/src/queue/index.js +17 -10
- package/dist/src/queue/index.js.map +1 -1
- package/dist/src/repeating-task.d.ts +1 -1
- package/dist/src/repeating-task.d.ts.map +1 -1
- package/dist/src/repeating-task.js +1 -3
- package/dist/src/repeating-task.js.map +1 -1
- package/dist/src/stream-to-ma-conn.js.map +1 -1
- package/package.json +20 -22
- package/src/abstract-stream.ts +11 -10
- package/src/adaptive-timeout.ts +3 -3
- package/src/debounce.ts +2 -2
- package/src/filters/cuckoo-filter.ts +2 -1
- package/src/filters/fingerprint.ts +1 -1
- package/src/filters/scalable-cuckoo-filter.ts +4 -2
- package/src/ip-port-to-multiaddr.ts +3 -2
- package/src/merge-options.ts +161 -0
- package/src/private-ip.ts +2 -6
- package/src/queue/index.ts +31 -19
- package/src/repeating-task.ts +6 -9
- package/src/stream-to-ma-conn.ts +4 -4
package/src/queue/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AbortError, TypedEventEmitter } from '@libp2p/interface'
|
|
2
2
|
import { pushable } from 'it-pushable'
|
|
3
3
|
import { raceEvent } from 'race-event'
|
|
4
|
+
import { debounce } from '../debounce.js'
|
|
4
5
|
import { QueueFullError } from '../errors.js'
|
|
5
6
|
import { Job } from './job.js'
|
|
6
7
|
import type { AbortOptions, Metrics } from '@libp2p/interface'
|
|
@@ -70,49 +71,49 @@ export interface QueueEvents<JobReturnType, JobOptions extends AbortOptions = Ab
|
|
|
70
71
|
/**
|
|
71
72
|
* A job is about to start running
|
|
72
73
|
*/
|
|
73
|
-
|
|
74
|
+
active: CustomEvent
|
|
74
75
|
|
|
75
76
|
/**
|
|
76
77
|
* All jobs have finished and the queue is empty
|
|
77
78
|
*/
|
|
78
|
-
|
|
79
|
+
idle: CustomEvent
|
|
79
80
|
|
|
80
81
|
/**
|
|
81
82
|
* The queue is empty, jobs may be running
|
|
82
83
|
*/
|
|
83
|
-
|
|
84
|
+
empty: CustomEvent
|
|
84
85
|
|
|
85
86
|
/**
|
|
86
87
|
* A job was added to the queue
|
|
87
88
|
*/
|
|
88
|
-
|
|
89
|
+
add: CustomEvent
|
|
89
90
|
|
|
90
91
|
/**
|
|
91
92
|
* A job has finished or failed
|
|
92
93
|
*/
|
|
93
|
-
|
|
94
|
+
next: CustomEvent
|
|
94
95
|
|
|
95
96
|
/**
|
|
96
97
|
* A job has finished successfully
|
|
97
98
|
*/
|
|
98
|
-
|
|
99
|
+
completed: CustomEvent<JobReturnType>
|
|
99
100
|
|
|
100
101
|
/**
|
|
101
102
|
* A job has failed
|
|
102
103
|
*/
|
|
103
|
-
|
|
104
|
+
error: CustomEvent<Error>
|
|
104
105
|
|
|
105
106
|
/**
|
|
106
107
|
* Emitted just after `"completed", a job has finished successfully - this
|
|
107
108
|
* event gives access to the job and it's result
|
|
108
109
|
*/
|
|
109
|
-
|
|
110
|
+
success: CustomEvent<QueueJobSuccess<JobReturnType, JobOptions>>
|
|
110
111
|
|
|
111
112
|
/**
|
|
112
113
|
* Emitted just after `"error", a job has failed - this event gives access to
|
|
113
114
|
* the job and the thrown error
|
|
114
115
|
*/
|
|
115
|
-
|
|
116
|
+
failure: CustomEvent<QueueJobFailure<JobReturnType, JobOptions>>
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
/**
|
|
@@ -149,22 +150,33 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
149
150
|
|
|
150
151
|
this.sort = init.sort
|
|
151
152
|
this.queue = []
|
|
153
|
+
|
|
154
|
+
this.emitEmpty = debounce(this.emitEmpty.bind(this), 1)
|
|
155
|
+
this.emitIdle = debounce(this.emitIdle.bind(this), 1)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
emitEmpty (): void {
|
|
159
|
+
if (this.size !== 0) {
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
this.safeDispatchEvent('empty')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
emitIdle (): void {
|
|
167
|
+
if (this.running !== 0) {
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.safeDispatchEvent('idle')
|
|
152
172
|
}
|
|
153
173
|
|
|
154
174
|
private tryToStartAnother (): boolean {
|
|
155
175
|
if (this.size === 0) {
|
|
156
|
-
|
|
157
|
-
// result before the "empty" event fires
|
|
158
|
-
queueMicrotask(() => {
|
|
159
|
-
this.safeDispatchEvent('empty')
|
|
160
|
-
})
|
|
176
|
+
this.emitEmpty()
|
|
161
177
|
|
|
162
178
|
if (this.running === 0) {
|
|
163
|
-
|
|
164
|
-
// result before the "idle" event fires
|
|
165
|
-
queueMicrotask(() => {
|
|
166
|
-
this.safeDispatchEvent('idle')
|
|
167
|
-
})
|
|
179
|
+
this.emitIdle()
|
|
168
180
|
}
|
|
169
181
|
|
|
170
182
|
return false
|
package/src/repeating-task.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface RepeatingTask {
|
|
|
18
18
|
* Update the amount of time a task will run before the passed abort signal
|
|
19
19
|
* will fire.
|
|
20
20
|
*
|
|
21
|
-
*
|
|
21
|
+
* This only affects the next iteration of the task, if it is currently
|
|
22
22
|
* running, that run will not be interrupted.
|
|
23
23
|
*/
|
|
24
24
|
setTimeout(ms: number): void
|
|
@@ -83,7 +83,7 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
|
|
|
83
83
|
let started = false
|
|
84
84
|
|
|
85
85
|
return {
|
|
86
|
-
setInterval: (ms) => {
|
|
86
|
+
setInterval: (ms): void => {
|
|
87
87
|
interval = ms
|
|
88
88
|
|
|
89
89
|
// maybe reschedule
|
|
@@ -92,14 +92,11 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
|
|
|
92
92
|
timeout = setTimeout(runTask, interval)
|
|
93
93
|
}
|
|
94
94
|
},
|
|
95
|
-
setTimeout: (ms) => {
|
|
96
|
-
|
|
97
|
-
options = {}
|
|
98
|
-
}
|
|
99
|
-
|
|
95
|
+
setTimeout: (ms): void => {
|
|
96
|
+
options ??= {}
|
|
100
97
|
options.timeout = ms
|
|
101
98
|
},
|
|
102
|
-
start: () => {
|
|
99
|
+
start: (): void => {
|
|
103
100
|
if (started) {
|
|
104
101
|
return
|
|
105
102
|
}
|
|
@@ -118,7 +115,7 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
|
|
|
118
115
|
timeout = setTimeout(runTask, interval)
|
|
119
116
|
}
|
|
120
117
|
},
|
|
121
|
-
stop: () => {
|
|
118
|
+
stop: (): void => {
|
|
122
119
|
clearTimeout(timeout)
|
|
123
120
|
shutdownController?.abort()
|
|
124
121
|
started = false
|
package/src/stream-to-ma-conn.ts
CHANGED
|
@@ -34,21 +34,21 @@ export function streamToMaConnection (props: StreamProperties): MultiaddrConnect
|
|
|
34
34
|
|
|
35
35
|
// piggyback on `stream.close` invocations to close multiaddr connection
|
|
36
36
|
const streamClose = stream.close.bind(stream)
|
|
37
|
-
stream.close = async (options) => {
|
|
37
|
+
stream.close = async (options): Promise<void> => {
|
|
38
38
|
await streamClose(options)
|
|
39
39
|
close(true)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// piggyback on `stream.abort` invocations to close multiaddr connection
|
|
43
43
|
const streamAbort = stream.abort.bind(stream)
|
|
44
|
-
stream.abort = (err) => {
|
|
44
|
+
stream.abort = (err): void => {
|
|
45
45
|
streamAbort(err)
|
|
46
46
|
close(true)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
// piggyback on `stream.sink` invocations to close multiaddr connection
|
|
50
50
|
const streamSink = stream.sink.bind(stream)
|
|
51
|
-
stream.sink = async (source) => {
|
|
51
|
+
stream.sink = async (source): Promise<void> => {
|
|
52
52
|
try {
|
|
53
53
|
await streamSink(
|
|
54
54
|
pipe(
|
|
@@ -73,7 +73,7 @@ export function streamToMaConnection (props: StreamProperties): MultiaddrConnect
|
|
|
73
73
|
const maConn: MultiaddrConnection = {
|
|
74
74
|
log,
|
|
75
75
|
sink: stream.sink,
|
|
76
|
-
source: (async function * () {
|
|
76
|
+
source: (async function * (): AsyncGenerator<Uint8ArrayList> {
|
|
77
77
|
try {
|
|
78
78
|
for await (const buf of stream.source) {
|
|
79
79
|
onDataRead?.(buf)
|