@libp2p/utils 5.1.1 → 5.2.0
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/peer-queue.d.ts +14 -0
- package/dist/src/peer-queue.d.ts.map +1 -0
- package/dist/src/peer-queue.js +16 -0
- package/dist/src/peer-queue.js.map +1 -0
- package/dist/src/queue/index.d.ts +122 -0
- package/dist/src/queue/index.d.ts.map +1 -0
- package/dist/src/queue/index.js +262 -0
- package/dist/src/queue/index.js.map +1 -0
- package/dist/src/queue/job.d.ts +25 -0
- package/dist/src/queue/job.d.ts.map +1 -0
- package/dist/src/queue/job.js +82 -0
- package/dist/src/queue/job.js.map +1 -0
- package/dist/src/queue/recipient.d.ts +10 -0
- package/dist/src/queue/recipient.d.ts.map +1 -0
- package/dist/src/queue/recipient.js +21 -0
- package/dist/src/queue/recipient.js.map +1 -0
- package/dist/src/tracked-list.d.ts +13 -0
- package/dist/src/tracked-list.d.ts.map +1 -0
- package/dist/src/tracked-list.js +11 -0
- package/dist/src/tracked-list.js.map +1 -0
- package/dist/typedoc-urls.json +29 -5
- package/package.json +18 -11
- package/src/peer-queue.ts +24 -0
- package/src/queue/index.ts +365 -0
- package/src/queue/job.ts +105 -0
- package/src/queue/recipient.ts +26 -0
- package/src/tracked-list.ts +26 -0
- package/dist/src/peer-job-queue.d.ts +0 -42
- package/dist/src/peer-job-queue.d.ts.map +0 -1
- package/dist/src/peer-job-queue.js +0 -132
- package/dist/src/peer-job-queue.js.map +0 -1
- package/src/peer-job-queue.ts +0 -187
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import PQueue from 'p-queue';
|
|
2
|
-
import type { PeerId } from '@libp2p/interface';
|
|
3
|
-
import type { AbortOptions } from 'it-pushable';
|
|
4
|
-
import type { QueueAddOptions, Options, Queue } from 'p-queue';
|
|
5
|
-
interface RunFunction<T> {
|
|
6
|
-
(options?: AbortOptions): Promise<T>;
|
|
7
|
-
}
|
|
8
|
-
export interface PeerPriorityQueueOptions extends QueueAddOptions {
|
|
9
|
-
peerId: PeerId;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
13
|
-
* that adds support for filtering jobs by peer id
|
|
14
|
-
*/
|
|
15
|
-
declare class PeerPriorityQueue implements Queue<RunFunction<unknown>, PeerPriorityQueueOptions> {
|
|
16
|
-
#private;
|
|
17
|
-
enqueue(run: RunFunction<unknown>, options?: Partial<PeerPriorityQueueOptions>): void;
|
|
18
|
-
dequeue(): RunFunction<unknown> | undefined;
|
|
19
|
-
filter(options: Readonly<Partial<PeerPriorityQueueOptions>>): Array<RunFunction<unknown>>;
|
|
20
|
-
get size(): number;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Extends PQueue to add support for querying queued jobs by peer id
|
|
24
|
-
*/
|
|
25
|
-
export declare class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
|
|
26
|
-
private readonly results;
|
|
27
|
-
constructor(options?: Options<PeerPriorityQueue, PeerPriorityQueueOptions>);
|
|
28
|
-
/**
|
|
29
|
-
* Returns true if this queue has a job for the passed peer id that has not
|
|
30
|
-
* yet started to run
|
|
31
|
-
*/
|
|
32
|
-
hasJob(peerId: PeerId): boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Returns a promise for the result of the job in the queue for the passed
|
|
35
|
-
* peer id.
|
|
36
|
-
*/
|
|
37
|
-
joinJob<Result = void>(peerId: PeerId): Promise<Result>;
|
|
38
|
-
add<T>(fn: RunFunction<T>, opts: PeerPriorityQueueOptions): Promise<T>;
|
|
39
|
-
clear(): void;
|
|
40
|
-
}
|
|
41
|
-
export {};
|
|
42
|
-
//# sourceMappingURL=peer-job-queue.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"peer-job-queue.d.ts","sourceRoot":"","sources":["../../src/peer-job-queue.ts"],"names":[],"mappings":"AA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G,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,wBAAwB,GAAG,OAAO,CAAC,CAAC,CAAC;IAkC9E,KAAK,IAAK,IAAI;CAIf"}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface';
|
|
3
|
-
import { PeerMap } from '@libp2p/peer-collections';
|
|
4
|
-
import pDefer from 'p-defer';
|
|
5
|
-
import PQueue from 'p-queue';
|
|
6
|
-
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
7
|
-
// Used to compute insertion index to keep queue sorted after insertion
|
|
8
|
-
function lowerBound(array, value, comparator) {
|
|
9
|
-
let first = 0;
|
|
10
|
-
let count = array.length;
|
|
11
|
-
while (count > 0) {
|
|
12
|
-
const step = Math.trunc(count / 2);
|
|
13
|
-
let it = first + step;
|
|
14
|
-
if (comparator(array[it], value) <= 0) {
|
|
15
|
-
first = ++it;
|
|
16
|
-
count -= step + 1;
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
count = step;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return first;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
26
|
-
* that adds support for filtering jobs by peer id
|
|
27
|
-
*/
|
|
28
|
-
class PeerPriorityQueue {
|
|
29
|
-
#queue = [];
|
|
30
|
-
enqueue(run, options) {
|
|
31
|
-
const peerId = options?.peerId;
|
|
32
|
-
const priority = options?.priority ?? 0;
|
|
33
|
-
if (peerId == null) {
|
|
34
|
-
throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS);
|
|
35
|
-
}
|
|
36
|
-
const element = {
|
|
37
|
-
priority,
|
|
38
|
-
peerId,
|
|
39
|
-
run
|
|
40
|
-
};
|
|
41
|
-
if (this.size > 0 && this.#queue[this.size - 1].priority >= priority) {
|
|
42
|
-
this.#queue.push(element);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const index = lowerBound(this.#queue, element, (a, b) => b.priority - a.priority);
|
|
46
|
-
this.#queue.splice(index, 0, element);
|
|
47
|
-
}
|
|
48
|
-
dequeue() {
|
|
49
|
-
const item = this.#queue.shift();
|
|
50
|
-
return item?.run;
|
|
51
|
-
}
|
|
52
|
-
filter(options) {
|
|
53
|
-
if (options.peerId != null) {
|
|
54
|
-
const peerId = options.peerId;
|
|
55
|
-
return this.#queue.filter((element) => peerId.equals(element.peerId)).map((element) => element.run);
|
|
56
|
-
}
|
|
57
|
-
return this.#queue.filter((element) => element.priority === options.priority).map((element) => element.run);
|
|
58
|
-
}
|
|
59
|
-
get size() {
|
|
60
|
-
return this.#queue.length;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Extends PQueue to add support for querying queued jobs by peer id
|
|
65
|
-
*/
|
|
66
|
-
export class PeerJobQueue extends PQueue {
|
|
67
|
-
results;
|
|
68
|
-
constructor(options = {}) {
|
|
69
|
-
super({
|
|
70
|
-
...options,
|
|
71
|
-
queueClass: PeerPriorityQueue
|
|
72
|
-
});
|
|
73
|
-
this.results = new PeerMap();
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Returns true if this queue has a job for the passed peer id that has not
|
|
77
|
-
* yet started to run
|
|
78
|
-
*/
|
|
79
|
-
hasJob(peerId) {
|
|
80
|
-
return this.sizeBy({
|
|
81
|
-
peerId
|
|
82
|
-
}) > 0;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Returns a promise for the result of the job in the queue for the passed
|
|
86
|
-
* peer id.
|
|
87
|
-
*/
|
|
88
|
-
async joinJob(peerId) {
|
|
89
|
-
let deferred = this.results.get(peerId);
|
|
90
|
-
if (deferred == null) {
|
|
91
|
-
throw new CodeError('No job found for peer id', 'ERR_NO_JOB_FOR_PEER_ID');
|
|
92
|
-
}
|
|
93
|
-
if (deferred === true) {
|
|
94
|
-
// a job has been added but so far nothing has tried to join the job
|
|
95
|
-
deferred = pDefer();
|
|
96
|
-
this.results.set(peerId, deferred);
|
|
97
|
-
}
|
|
98
|
-
return deferred.promise;
|
|
99
|
-
}
|
|
100
|
-
async add(fn, opts) {
|
|
101
|
-
const peerId = opts?.peerId;
|
|
102
|
-
if (peerId == null) {
|
|
103
|
-
throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS);
|
|
104
|
-
}
|
|
105
|
-
this.results.set(opts.peerId, true);
|
|
106
|
-
return super.add(async (opts) => {
|
|
107
|
-
try {
|
|
108
|
-
const value = await fn(opts);
|
|
109
|
-
const deferred = this.results.get(peerId);
|
|
110
|
-
if (deferred != null && deferred !== true) {
|
|
111
|
-
deferred.resolve(value);
|
|
112
|
-
}
|
|
113
|
-
return value;
|
|
114
|
-
}
|
|
115
|
-
catch (err) {
|
|
116
|
-
const deferred = this.results.get(peerId);
|
|
117
|
-
if (deferred != null && deferred !== true) {
|
|
118
|
-
deferred.reject(err);
|
|
119
|
-
}
|
|
120
|
-
throw err;
|
|
121
|
-
}
|
|
122
|
-
finally {
|
|
123
|
-
this.results.delete(peerId);
|
|
124
|
-
}
|
|
125
|
-
}, opts);
|
|
126
|
-
}
|
|
127
|
-
clear() {
|
|
128
|
-
this.results.clear();
|
|
129
|
-
super.clear();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
//# sourceMappingURL=peer-job-queue.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"peer-job-queue.js","sourceRoot":"","sources":["../../src/peer-job-queue.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,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CAAC,CAAA;gBAEzC,IAAI,QAAQ,IAAI,IAAI,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;oBAC1C,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBACzB,CAAC;gBAED,OAAO,KAAK,CAAA;YACd,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;gBAEzC,IAAI,QAAQ,IAAI,IAAI,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;oBAC1C,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBACtB,CAAC;gBAED,MAAM,GAAG,CAAA;YACX,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YAC7B,CAAC;QACH,CAAC,EAAE,IAAI,CAAe,CAAA;IACxB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;QACpB,KAAK,CAAC,KAAK,EAAE,CAAA;IACf,CAAC;CACF"}
|
package/src/peer-job-queue.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
|
|
3
|
-
import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface'
|
|
4
|
-
import { PeerMap } from '@libp2p/peer-collections'
|
|
5
|
-
import pDefer from 'p-defer'
|
|
6
|
-
import PQueue from 'p-queue'
|
|
7
|
-
import type { PeerId } from '@libp2p/interface'
|
|
8
|
-
import type { AbortOptions } from 'it-pushable'
|
|
9
|
-
import type { DeferredPromise } from 'p-defer'
|
|
10
|
-
import type { QueueAddOptions, Options, Queue } from 'p-queue'
|
|
11
|
-
|
|
12
|
-
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
13
|
-
// Used to compute insertion index to keep queue sorted after insertion
|
|
14
|
-
function lowerBound<T> (array: readonly T[], value: T, comparator: (a: T, b: T) => number): number {
|
|
15
|
-
let first = 0
|
|
16
|
-
let count = array.length
|
|
17
|
-
|
|
18
|
-
while (count > 0) {
|
|
19
|
-
const step = Math.trunc(count / 2)
|
|
20
|
-
let it = first + step
|
|
21
|
-
|
|
22
|
-
if (comparator(array[it]!, value) <= 0) {
|
|
23
|
-
first = ++it
|
|
24
|
-
count -= step + 1
|
|
25
|
-
} else {
|
|
26
|
-
count = step
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return first
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface RunFunction<T> {
|
|
34
|
-
(options?: AbortOptions): Promise<T>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface PeerPriorityQueueOptions extends QueueAddOptions {
|
|
38
|
-
peerId: PeerId
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
interface PeerJob {
|
|
42
|
-
priority: number
|
|
43
|
-
peerId: PeerId
|
|
44
|
-
run: RunFunction<any>
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
49
|
-
* that adds support for filtering jobs by peer id
|
|
50
|
-
*/
|
|
51
|
-
class PeerPriorityQueue implements Queue<RunFunction<unknown>, PeerPriorityQueueOptions> {
|
|
52
|
-
readonly #queue: PeerJob[] = []
|
|
53
|
-
|
|
54
|
-
enqueue (run: RunFunction<unknown>, options?: Partial<PeerPriorityQueueOptions>): void {
|
|
55
|
-
const peerId = options?.peerId
|
|
56
|
-
const priority = options?.priority ?? 0
|
|
57
|
-
|
|
58
|
-
if (peerId == null) {
|
|
59
|
-
throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const element: PeerJob = {
|
|
63
|
-
priority,
|
|
64
|
-
peerId,
|
|
65
|
-
run
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (this.size > 0 && this.#queue[this.size - 1]!.priority >= priority) {
|
|
69
|
-
this.#queue.push(element)
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const index = lowerBound(
|
|
74
|
-
this.#queue, element,
|
|
75
|
-
(a: Readonly<PeerPriorityQueueOptions>, b: Readonly<PeerPriorityQueueOptions>) => b.priority! - a.priority!
|
|
76
|
-
)
|
|
77
|
-
this.#queue.splice(index, 0, element)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
dequeue (): RunFunction<unknown> | undefined {
|
|
81
|
-
const item = this.#queue.shift()
|
|
82
|
-
return item?.run
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
filter (options: Readonly<Partial<PeerPriorityQueueOptions>>): Array<RunFunction<unknown>> {
|
|
86
|
-
if (options.peerId != null) {
|
|
87
|
-
const peerId = options.peerId
|
|
88
|
-
|
|
89
|
-
return this.#queue.filter(
|
|
90
|
-
(element: Readonly<PeerPriorityQueueOptions>) => peerId.equals(element.peerId)
|
|
91
|
-
).map((element: Readonly<{ run: RunFunction<unknown> }>) => element.run)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return this.#queue.filter(
|
|
95
|
-
(element: Readonly<PeerPriorityQueueOptions>) => element.priority === options.priority
|
|
96
|
-
).map((element: Readonly<{ run: RunFunction<unknown> }>) => element.run)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
get size (): number {
|
|
100
|
-
return this.#queue.length
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Extends PQueue to add support for querying queued jobs by peer id
|
|
106
|
-
*/
|
|
107
|
-
export class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
|
|
108
|
-
private readonly results: PeerMap<DeferredPromise<any> | true>
|
|
109
|
-
|
|
110
|
-
constructor (options: Options<PeerPriorityQueue, PeerPriorityQueueOptions> = {}) {
|
|
111
|
-
super({
|
|
112
|
-
...options,
|
|
113
|
-
queueClass: PeerPriorityQueue
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
this.results = new PeerMap()
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Returns true if this queue has a job for the passed peer id that has not
|
|
121
|
-
* yet started to run
|
|
122
|
-
*/
|
|
123
|
-
hasJob (peerId: PeerId): boolean {
|
|
124
|
-
return this.sizeBy({
|
|
125
|
-
peerId
|
|
126
|
-
}) > 0
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Returns a promise for the result of the job in the queue for the passed
|
|
131
|
-
* peer id.
|
|
132
|
-
*/
|
|
133
|
-
async joinJob <Result = void> (peerId: PeerId): Promise<Result> {
|
|
134
|
-
let deferred = this.results.get(peerId)
|
|
135
|
-
|
|
136
|
-
if (deferred == null) {
|
|
137
|
-
throw new CodeError('No job found for peer id', 'ERR_NO_JOB_FOR_PEER_ID')
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (deferred === true) {
|
|
141
|
-
// a job has been added but so far nothing has tried to join the job
|
|
142
|
-
deferred = pDefer<Result>()
|
|
143
|
-
this.results.set(peerId, deferred)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return deferred.promise
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async add <T> (fn: RunFunction<T>, opts: PeerPriorityQueueOptions): Promise<T> {
|
|
150
|
-
const peerId = opts?.peerId
|
|
151
|
-
|
|
152
|
-
if (peerId == null) {
|
|
153
|
-
throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
this.results.set(opts.peerId, true)
|
|
157
|
-
|
|
158
|
-
return super.add(async (opts?: AbortOptions) => {
|
|
159
|
-
try {
|
|
160
|
-
const value = await fn(opts)
|
|
161
|
-
|
|
162
|
-
const deferred = this.results.get(peerId)
|
|
163
|
-
|
|
164
|
-
if (deferred != null && deferred !== true) {
|
|
165
|
-
deferred.resolve(value)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return value
|
|
169
|
-
} catch (err) {
|
|
170
|
-
const deferred = this.results.get(peerId)
|
|
171
|
-
|
|
172
|
-
if (deferred != null && deferred !== true) {
|
|
173
|
-
deferred.reject(err)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
throw err
|
|
177
|
-
} finally {
|
|
178
|
-
this.results.delete(peerId)
|
|
179
|
-
}
|
|
180
|
-
}, opts) as Promise<T>
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
clear (): void {
|
|
184
|
-
this.results.clear()
|
|
185
|
-
super.clear()
|
|
186
|
-
}
|
|
187
|
-
}
|