@libp2p/utils 5.3.2 → 5.4.0-43046b9ae

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.
Files changed (82) hide show
  1. package/dist/src/abort-options.d.ts +7 -0
  2. package/dist/src/abort-options.d.ts.map +1 -0
  3. package/dist/src/abort-options.js +14 -0
  4. package/dist/src/abort-options.js.map +1 -0
  5. package/dist/src/adaptive-timeout.d.ts +35 -0
  6. package/dist/src/adaptive-timeout.d.ts.map +1 -0
  7. package/dist/src/adaptive-timeout.js +63 -0
  8. package/dist/src/adaptive-timeout.js.map +1 -0
  9. package/dist/src/close.d.ts +21 -0
  10. package/dist/src/close.d.ts.map +1 -0
  11. package/dist/src/close.js +49 -0
  12. package/dist/src/close.js.map +1 -0
  13. package/dist/src/filters/bloom-filter.d.ts +34 -0
  14. package/dist/src/filters/bloom-filter.d.ts.map +1 -0
  15. package/dist/src/filters/bloom-filter.js +113 -0
  16. package/dist/src/filters/bloom-filter.js.map +1 -0
  17. package/dist/src/filters/bucket.d.ts +10 -0
  18. package/dist/src/filters/bucket.d.ts.map +1 -0
  19. package/dist/src/filters/bucket.js +53 -0
  20. package/dist/src/filters/bucket.js.map +1 -0
  21. package/dist/src/filters/cuckoo-filter.d.ts +41 -0
  22. package/dist/src/filters/cuckoo-filter.d.ts.map +1 -0
  23. package/dist/src/filters/cuckoo-filter.js +134 -0
  24. package/dist/src/filters/cuckoo-filter.js.map +1 -0
  25. package/dist/src/filters/fingerprint.d.ts +11 -0
  26. package/dist/src/filters/fingerprint.d.ts.map +1 -0
  27. package/dist/src/filters/fingerprint.js +34 -0
  28. package/dist/src/filters/fingerprint.js.map +1 -0
  29. package/dist/src/filters/hashes.d.ts +8 -0
  30. package/dist/src/filters/hashes.d.ts.map +1 -0
  31. package/dist/src/filters/hashes.js +29 -0
  32. package/dist/src/filters/hashes.js.map +1 -0
  33. package/dist/src/filters/index.d.ts +9 -0
  34. package/dist/src/filters/index.d.ts.map +1 -0
  35. package/dist/src/filters/index.js +4 -0
  36. package/dist/src/filters/index.js.map +1 -0
  37. package/dist/src/filters/scalable-cuckoo-filter.d.ts +24 -0
  38. package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -0
  39. package/dist/src/filters/scalable-cuckoo-filter.js +87 -0
  40. package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -0
  41. package/dist/src/filters/utils.d.ts +2 -0
  42. package/dist/src/filters/utils.d.ts.map +1 -0
  43. package/dist/src/filters/utils.js +4 -0
  44. package/dist/src/filters/utils.js.map +1 -0
  45. package/dist/src/moving-average.d.ts +18 -0
  46. package/dist/src/moving-average.d.ts.map +1 -0
  47. package/dist/src/moving-average.js +43 -0
  48. package/dist/src/moving-average.js.map +1 -0
  49. package/dist/src/peer-queue.d.ts +3 -3
  50. package/dist/src/peer-queue.d.ts.map +1 -1
  51. package/dist/src/peer-queue.js +0 -1
  52. package/dist/src/peer-queue.js.map +1 -1
  53. package/dist/src/priority-queue.d.ts +10 -0
  54. package/dist/src/priority-queue.d.ts.map +1 -0
  55. package/dist/src/priority-queue.js +18 -0
  56. package/dist/src/priority-queue.js.map +1 -0
  57. package/dist/src/queue/index.d.ts +14 -14
  58. package/dist/src/queue/index.d.ts.map +1 -1
  59. package/dist/src/queue/index.js +6 -24
  60. package/dist/src/queue/index.js.map +1 -1
  61. package/dist/src/queue/job.d.ts +1 -2
  62. package/dist/src/queue/job.d.ts.map +1 -1
  63. package/dist/src/queue/job.js +1 -3
  64. package/dist/src/queue/job.js.map +1 -1
  65. package/package.json +36 -7
  66. package/src/abort-options.ts +20 -0
  67. package/src/adaptive-timeout.ts +94 -0
  68. package/src/close.ts +65 -0
  69. package/src/filters/bloom-filter.ts +142 -0
  70. package/src/filters/bucket.ts +64 -0
  71. package/src/filters/cuckoo-filter.ts +197 -0
  72. package/src/filters/fingerprint.ts +44 -0
  73. package/src/filters/hashes.ts +38 -0
  74. package/src/filters/index.ts +9 -0
  75. package/src/filters/scalable-cuckoo-filter.ts +111 -0
  76. package/src/filters/utils.ts +3 -0
  77. package/src/moving-average.ts +45 -0
  78. package/src/peer-queue.ts +3 -5
  79. package/src/priority-queue.ts +26 -0
  80. package/src/queue/index.ts +21 -45
  81. package/src/queue/job.ts +1 -3
  82. package/dist/typedoc-urls.json +0 -76
@@ -0,0 +1,197 @@
1
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
2
+ import { Bucket } from './bucket.js'
3
+ import { Fingerprint, MAX_FINGERPRINT_SIZE } from './fingerprint.js'
4
+ import { fnv1a, type Hash } from './hashes.js'
5
+ import { getRandomInt } from './utils.js'
6
+ import type { Filter } from './index.js'
7
+
8
+ const maxCuckooCount = 500
9
+
10
+ export interface CuckooFilterInit {
11
+ /**
12
+ * How many items the filter is expected to contain
13
+ */
14
+ filterSize: number
15
+
16
+ /**
17
+ * How many items to put in each bucket
18
+ */
19
+ bucketSize?: number
20
+
21
+ /**
22
+ * How many bytes the fingerprint is expected to be
23
+ */
24
+ fingerprintSize?: number
25
+
26
+ /**
27
+ * A non-cryptographic hash implementation
28
+ */
29
+ hash?: Hash
30
+
31
+ /**
32
+ * A number used to seed the hash
33
+ */
34
+ seed?: number
35
+ }
36
+
37
+ export class CuckooFilter implements Filter {
38
+ private readonly bucketSize: number
39
+ private readonly filterSize: number
40
+ private readonly fingerprintSize: number
41
+ private readonly buckets: Bucket[]
42
+ public count: number
43
+ private readonly hash: Hash
44
+ private readonly seed: number
45
+
46
+ constructor (init: CuckooFilterInit) {
47
+ this.filterSize = init.filterSize
48
+ this.bucketSize = init.bucketSize ?? 4
49
+ this.fingerprintSize = init.fingerprintSize ?? 2
50
+ this.count = 0
51
+ this.buckets = []
52
+ this.hash = init.hash ?? fnv1a
53
+ this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10))
54
+ }
55
+
56
+ add (item: Uint8Array | string): boolean {
57
+ if (typeof item === 'string') {
58
+ item = uint8ArrayFromString(item)
59
+ }
60
+
61
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
62
+ const j = this.hash.hash(item, this.seed) % this.filterSize
63
+ const k = (j ^ fingerprint.hash()) % this.filterSize
64
+
65
+ if (this.buckets[j] == null) {
66
+ this.buckets[j] = new Bucket(this.bucketSize)
67
+ }
68
+
69
+ if (this.buckets[k] == null) {
70
+ this.buckets[k] = new Bucket(this.bucketSize)
71
+ }
72
+
73
+ if (this.buckets[j].add(fingerprint) || this.buckets[k].add(fingerprint)) {
74
+ this.count++
75
+ return true
76
+ }
77
+
78
+ const rand = [j, k]
79
+ let i = rand[getRandomInt(0, rand.length - 1)]
80
+
81
+ if (this.buckets[i] == null) {
82
+ this.buckets[i] = new Bucket(this.bucketSize)
83
+ }
84
+
85
+ for (let n = 0; n < maxCuckooCount; n++) {
86
+ const swapped = this.buckets[i].swap(fingerprint)
87
+
88
+ if (swapped == null) {
89
+ continue
90
+ }
91
+
92
+ i = (i ^ swapped.hash()) % this.filterSize
93
+
94
+ if (this.buckets[i] == null) {
95
+ this.buckets[i] = new Bucket(this.bucketSize)
96
+ }
97
+
98
+ if (this.buckets[i].add(swapped)) {
99
+ this.count++
100
+
101
+ return true
102
+ } else {
103
+ continue
104
+ }
105
+ }
106
+
107
+ return false
108
+ }
109
+
110
+ has (item: Uint8Array | string): boolean {
111
+ if (typeof item === 'string') {
112
+ item = uint8ArrayFromString(item)
113
+ }
114
+
115
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
116
+ const j = this.hash.hash(item, this.seed) % this.filterSize
117
+ const inJ = this.buckets[j]?.has(fingerprint) ?? false
118
+
119
+ if (inJ) {
120
+ return inJ
121
+ }
122
+
123
+ const k = (j ^ fingerprint.hash()) % this.filterSize
124
+
125
+ return this.buckets[k]?.has(fingerprint) ?? false
126
+ }
127
+
128
+ remove (item: Uint8Array | string): boolean {
129
+ if (typeof item === 'string') {
130
+ item = uint8ArrayFromString(item)
131
+ }
132
+
133
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
134
+ const j = this.hash.hash(item, this.seed) % this.filterSize
135
+ const inJ = this.buckets[j]?.remove(fingerprint) ?? false
136
+
137
+ if (inJ) {
138
+ this.count--
139
+ return inJ
140
+ }
141
+
142
+ const k = (j ^ fingerprint.hash()) % this.filterSize
143
+ const inK = this.buckets[k]?.remove(fingerprint) ?? false
144
+
145
+ if (inK) {
146
+ this.count--
147
+ }
148
+
149
+ return inK
150
+ }
151
+
152
+ get reliable (): boolean {
153
+ return Math.floor(100 * (this.count / this.filterSize)) <= 95
154
+ }
155
+ }
156
+
157
+ // max load constants, defined in the cuckoo paper
158
+ const MAX_LOAD = {
159
+ 1: 0.5,
160
+ 2: 0.84,
161
+ 4: 0.95,
162
+ 8: 0.98
163
+ }
164
+
165
+ function calculateBucketSize (errorRate: number = 0.001): 2 | 4 | 8 {
166
+ if (errorRate > 0.002) {
167
+ return 2
168
+ }
169
+
170
+ if (errorRate > 0.00001) {
171
+ return 4
172
+ }
173
+
174
+ return 8
175
+ }
176
+
177
+ export function optimize (maxItems: number, errorRate: number = 0.001): CuckooFilterInit {
178
+ // https://www.eecs.harvard.edu/~michaelm/postscripts/cuckoo-conext2014.pdf
179
+ // Section 5.1 Optimal Bucket Size
180
+ const bucketSize = calculateBucketSize(errorRate)
181
+ const load = MAX_LOAD[bucketSize]
182
+
183
+ // https://stackoverflow.com/questions/57555236/how-to-size-a-cuckoo-filter/57617208#57617208
184
+ const filterSize = Math.round(maxItems / load)
185
+ const fingerprintSize = Math.min(Math.ceil(Math.log(filterSize / bucketSize)) + 2, MAX_FINGERPRINT_SIZE)
186
+
187
+ return {
188
+ filterSize,
189
+ bucketSize,
190
+ fingerprintSize
191
+ }
192
+ }
193
+
194
+ export function createCuckooFilter (maxItems: number, errorRate: number = 0.005): Filter {
195
+ const opts = optimize(maxItems, errorRate)
196
+ return new CuckooFilter(opts)
197
+ }
@@ -0,0 +1,44 @@
1
+ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
2
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
3
+ import type { Hash } from './hashes'
4
+
5
+ export const MAX_FINGERPRINT_SIZE = 64
6
+
7
+ export class Fingerprint {
8
+ private readonly fp: Uint8Array
9
+ private readonly h: Hash
10
+ private readonly seed: number
11
+
12
+ constructor (buf: Uint8Array, hash: Hash, seed: number, fingerprintSize: number = 2) {
13
+ if (fingerprintSize > MAX_FINGERPRINT_SIZE) {
14
+ throw new TypeError('Invalid Fingerprint Size')
15
+ }
16
+
17
+ const fnv = hash.hashV(buf, seed)
18
+ const fp = uint8ArrayAlloc(fingerprintSize)
19
+
20
+ for (let i = 0; i < fp.length; i++) {
21
+ fp[i] = fnv[i]
22
+ }
23
+
24
+ if (fp.length === 0) {
25
+ fp[0] = 7
26
+ }
27
+
28
+ this.fp = fp
29
+ this.h = hash
30
+ this.seed = seed
31
+ }
32
+
33
+ hash (): number {
34
+ return this.h.hash(this.fp, this.seed)
35
+ }
36
+
37
+ equals (other?: any): boolean {
38
+ if (!(other?.fp instanceof Uint8Array)) {
39
+ return false
40
+ }
41
+
42
+ return uint8ArrayEquals(this.fp, other.fp)
43
+ }
44
+ }
@@ -0,0 +1,38 @@
1
+ import fnv1aHash from '@sindresorhus/fnv1a'
2
+ import mur from 'murmurhash3js-revisited'
3
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
+
5
+ export interface Hash {
6
+ hash(input: Uint8Array, seed: number): number
7
+ hashV(input: Uint8Array, seed: number): Uint8Array
8
+ }
9
+
10
+ export const murmur3: Hash = {
11
+ hash: (input, seed) => {
12
+ return mur.x86.hash32(input, seed)
13
+ },
14
+ hashV: (input, seed) => {
15
+ return numberToBuffer(murmur3.hash(input, seed))
16
+ }
17
+ }
18
+
19
+ export const fnv1a: Hash = {
20
+ hash: (input) => {
21
+ return Number(fnv1aHash(input, {
22
+ size: 32
23
+ }))
24
+ },
25
+ hashV: (input, seed) => {
26
+ return numberToBuffer(fnv1a.hash(input, seed))
27
+ }
28
+ }
29
+
30
+ export function numberToBuffer (num: bigint | number): Uint8Array {
31
+ let hex = num.toString(16)
32
+
33
+ if (hex.length % 2 === 1) {
34
+ hex = `0${hex}`
35
+ }
36
+
37
+ return uint8ArrayFromString(hex, 'base16')
38
+ }
@@ -0,0 +1,9 @@
1
+ export { BloomFilter, createBloomFilter, type BloomFilterOptions } from './bloom-filter.js'
2
+ export { CuckooFilter, createCuckooFilter, type CuckooFilterInit } from './cuckoo-filter.js'
3
+ export { ScalableCuckooFilter, createScalableCuckooFilter, type ScalableCuckooFilterInit } from './scalable-cuckoo-filter.js'
4
+
5
+ export interface Filter {
6
+ add(item: Uint8Array | string): void
7
+ has(item: Uint8Array | string): boolean
8
+ remove?(buf: Uint8Array | string): boolean
9
+ }
@@ -0,0 +1,111 @@
1
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
2
+ import { CuckooFilter, optimize, type CuckooFilterInit } from './cuckoo-filter.js'
3
+ import { fnv1a, type Hash } from './hashes.js'
4
+ import { getRandomInt } from './utils.js'
5
+ import type { Filter } from './index.js'
6
+
7
+ export interface ScalableCuckooFilterInit extends CuckooFilterInit {
8
+ /**
9
+ * A number to multiply maxItems by when adding new sub-filters
10
+ */
11
+ scale?: number
12
+ }
13
+
14
+ export class ScalableCuckooFilter implements Filter {
15
+ private readonly filterSize: number
16
+ private readonly bucketSize: number
17
+ private readonly fingerprintSize: number
18
+ private readonly scale: number
19
+ private readonly filterSeries: CuckooFilter[]
20
+ private readonly hash: Hash
21
+ private readonly seed: number
22
+
23
+ constructor (init: ScalableCuckooFilterInit) {
24
+ this.bucketSize = init.bucketSize ?? 4
25
+ this.filterSize = init.filterSize ?? (1 << 18) / this.bucketSize
26
+ this.fingerprintSize = init.fingerprintSize ?? 2
27
+ this.scale = init.scale ?? 2
28
+ this.hash = init.hash ?? fnv1a
29
+ this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10))
30
+ this.filterSeries = [
31
+ new CuckooFilter({
32
+ filterSize: this.filterSize,
33
+ bucketSize: this.bucketSize,
34
+ fingerprintSize: this.fingerprintSize,
35
+ hash: this.hash,
36
+ seed: this.seed
37
+ })
38
+ ]
39
+ }
40
+
41
+ add (item: Uint8Array | string): boolean {
42
+ if (typeof item === 'string') {
43
+ item = uint8ArrayFromString(item)
44
+ }
45
+
46
+ if (this.has(item)) {
47
+ return true
48
+ }
49
+
50
+ let current = this.filterSeries.find((cuckoo) => {
51
+ return cuckoo.reliable
52
+ })
53
+
54
+ if (current == null) {
55
+ const curSize = this.filterSize * Math.pow(this.scale, this.filterSeries.length)
56
+
57
+ current = new CuckooFilter({
58
+ filterSize: curSize,
59
+ bucketSize: this.bucketSize,
60
+ fingerprintSize: this.fingerprintSize,
61
+ hash: this.hash,
62
+ seed: this.seed
63
+ })
64
+
65
+ this.filterSeries.push(current)
66
+ }
67
+
68
+ return current.add(item)
69
+ }
70
+
71
+ has (item: Uint8Array | string): boolean {
72
+ if (typeof item === 'string') {
73
+ item = uint8ArrayFromString(item)
74
+ }
75
+
76
+ for (let i = 0; i < this.filterSeries.length; i++) {
77
+ if (this.filterSeries[i].has(item)) {
78
+ return true
79
+ }
80
+ }
81
+
82
+ return false
83
+ }
84
+
85
+ remove (item: Uint8Array | string): boolean {
86
+ if (typeof item === 'string') {
87
+ item = uint8ArrayFromString(item)
88
+ }
89
+
90
+ for (let i = 0; i < this.filterSeries.length; i++) {
91
+ if (this.filterSeries[i].remove(item)) {
92
+ return true
93
+ }
94
+ }
95
+
96
+ return false
97
+ }
98
+
99
+ get count (): number {
100
+ return this.filterSeries.reduce((acc, curr) => {
101
+ return acc + curr.count
102
+ }, 0)
103
+ }
104
+ }
105
+
106
+ export function createScalableCuckooFilter (maxItems: number, errorRate: number = 0.001, options?: Pick<ScalableCuckooFilterInit, 'hash' | 'seed' | 'scale'>): Filter {
107
+ return new ScalableCuckooFilter({
108
+ ...optimize(maxItems, errorRate),
109
+ ...(options ?? {})
110
+ })
111
+ }
@@ -0,0 +1,3 @@
1
+ export function getRandomInt (min: number, max: number): number {
2
+ return Math.floor(Math.random() * (max - min)) + min
3
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Implements exponential moving average. Ported from `moving-average`.
3
+ *
4
+ * @see https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
5
+ * @see https://www.npmjs.com/package/moving-average
6
+ */
7
+ export class MovingAverage {
8
+ public movingAverage: number
9
+ public variance: number
10
+ public deviation: number
11
+ public forecast: number
12
+ private readonly timespan: number
13
+ private previousTime?: number
14
+
15
+ constructor (timespan: number) {
16
+ this.timespan = timespan
17
+ this.movingAverage = 0
18
+ this.variance = 0
19
+ this.deviation = 0
20
+ this.forecast = 0
21
+ }
22
+
23
+ alpha (t: number, pt: number): number {
24
+ return 1 - (Math.exp(-(t - pt) / this.timespan))
25
+ }
26
+
27
+ push (value: number, time: number = Date.now()): void {
28
+ if (this.previousTime != null) {
29
+ // calculate moving average
30
+ const a = this.alpha(time, this.previousTime)
31
+ const diff = value - this.movingAverage
32
+ const incr = a * diff
33
+ this.movingAverage = a * value + (1 - a) * this.movingAverage
34
+ // calculate variance & deviation
35
+ this.variance = (1 - a) * (this.variance + diff * incr)
36
+ this.deviation = Math.sqrt(this.variance)
37
+ // calculate forecast
38
+ this.forecast = this.movingAverage + a * diff
39
+ } else {
40
+ this.movingAverage = value
41
+ }
42
+
43
+ this.previousTime = time
44
+ }
45
+ }
package/src/peer-queue.ts CHANGED
@@ -1,10 +1,8 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
-
3
- import { Queue, type QueueAddOptions } from './queue/index.js'
1
+ import { Queue } from './queue/index.js'
4
2
  import type { Job } from './queue/job.js'
5
- import type { PeerId } from '@libp2p/interface'
3
+ import type { AbortOptions, PeerId } from '@libp2p/interface'
6
4
 
7
- export interface PeerQueueJobOptions extends QueueAddOptions {
5
+ export interface PeerQueueJobOptions extends AbortOptions {
8
6
  peerId: PeerId
9
7
  }
10
8
 
@@ -0,0 +1,26 @@
1
+ import { Queue } from './queue/index.js'
2
+ import type { QueueInit } from './queue/index.js'
3
+ import type { AbortOptions } from '@libp2p/interface'
4
+
5
+ export interface PriorityQueueJobOptions extends AbortOptions {
6
+ priority: number
7
+ }
8
+
9
+ export class PriorityQueue <JobReturnType = void, JobOptions extends PriorityQueueJobOptions = PriorityQueueJobOptions> extends Queue<JobReturnType, JobOptions> {
10
+ constructor (init: QueueInit<JobReturnType, JobOptions> = {}) {
11
+ super({
12
+ ...init,
13
+ sort: (a, b) => {
14
+ if (a.options.priority > b.options.priority) {
15
+ return -1
16
+ }
17
+
18
+ if (a.options.priority < b.options.priority) {
19
+ return 1
20
+ }
21
+
22
+ return 0
23
+ }
24
+ })
25
+ }
26
+ }
@@ -4,16 +4,11 @@ import { raceEvent } from 'race-event'
4
4
  import { Job } from './job.js'
5
5
  import type { AbortOptions, Metrics } from '@libp2p/interface'
6
6
 
7
- export interface QueueAddOptions extends AbortOptions {
8
- /**
9
- * Priority of operation. Operations with greater priority will be scheduled first.
10
- *
11
- * @default 0
12
- */
13
- priority?: number
7
+ export interface Comparator<T> {
8
+ (a: T, b: T): -1 | 0 | 1
14
9
  }
15
10
 
16
- export interface QueueInit {
11
+ export interface QueueInit<JobReturnType, JobOptions extends AbortOptions = AbortOptions> {
17
12
  /**
18
13
  * Concurrency limit.
19
14
  *
@@ -32,6 +27,11 @@ export interface QueueInit {
32
27
  * An implementation of the libp2p Metrics interface
33
28
  */
34
29
  metrics?: Metrics
30
+
31
+ /**
32
+ * An optional function that will sort the queue after a job has been added
33
+ */
34
+ sort?: Comparator<Job<JobOptions, JobReturnType>>
35
35
  }
36
36
 
37
37
  export type JobStatus = 'queued' | 'running' | 'errored' | 'complete'
@@ -40,21 +40,21 @@ export interface RunFunction<Options = AbortOptions, ReturnType = void> {
40
40
  (opts?: Options): Promise<ReturnType>
41
41
  }
42
42
 
43
- export interface JobMatcher<JobOptions extends QueueAddOptions = QueueAddOptions> {
43
+ export interface JobMatcher<JobOptions extends AbortOptions = AbortOptions> {
44
44
  (options?: Partial<JobOptions>): boolean
45
45
  }
46
46
 
47
- export interface QueueJobSuccess<JobReturnType, JobOptions extends QueueAddOptions = QueueAddOptions> {
47
+ export interface QueueJobSuccess<JobReturnType, JobOptions extends AbortOptions = AbortOptions> {
48
48
  job: Job<JobOptions, JobReturnType>
49
49
  result: JobReturnType
50
50
  }
51
51
 
52
- export interface QueueJobFailure<JobReturnType, JobOptions extends QueueAddOptions = QueueAddOptions> {
52
+ export interface QueueJobFailure<JobReturnType, JobOptions extends AbortOptions = AbortOptions> {
53
53
  job: Job<JobOptions, JobReturnType>
54
54
  error: Error
55
55
  }
56
56
 
57
- export interface QueueEvents<JobReturnType, JobOptions extends QueueAddOptions = QueueAddOptions> {
57
+ export interface QueueEvents<JobReturnType, JobOptions extends AbortOptions = AbortOptions> {
58
58
  /**
59
59
  * A job is about to start running
60
60
  */
@@ -103,39 +103,19 @@ export interface QueueEvents<JobReturnType, JobOptions extends QueueAddOptions =
103
103
  'failure': CustomEvent<QueueJobFailure<JobReturnType, JobOptions>>
104
104
  }
105
105
 
106
- // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
107
- // Used to compute insertion index to keep queue sorted after insertion
108
- function lowerBound<T> (array: readonly T[], value: T, comparator: (a: T, b: T) => number): number {
109
- let first = 0
110
- let count = array.length
111
-
112
- while (count > 0) {
113
- const step = Math.trunc(count / 2)
114
- let it = first + step
115
-
116
- if (comparator(array[it], value) <= 0) {
117
- first = ++it
118
- count -= step + 1
119
- } else {
120
- count = step
121
- }
122
- }
123
-
124
- return first
125
- }
126
-
127
106
  /**
128
107
  * Heavily influence by `p-queue` with the following differences:
129
108
  *
130
109
  * 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
131
110
  * 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
132
111
  */
133
- export class Queue<JobReturnType = unknown, JobOptions extends QueueAddOptions = QueueAddOptions> extends TypedEventEmitter<QueueEvents<JobReturnType, JobOptions>> {
112
+ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = AbortOptions> extends TypedEventEmitter<QueueEvents<JobReturnType, JobOptions>> {
134
113
  public concurrency: number
135
114
  public queue: Array<Job<JobOptions, JobReturnType>>
136
115
  private pending: number
116
+ private readonly sort?: Comparator<Job<JobOptions, JobReturnType>>
137
117
 
138
- constructor (init: QueueInit = {}) {
118
+ constructor (init: QueueInit<JobReturnType, JobOptions> = {}) {
139
119
  super()
140
120
 
141
121
  this.concurrency = init.concurrency ?? Number.POSITIVE_INFINITY
@@ -153,6 +133,7 @@ export class Queue<JobReturnType = unknown, JobOptions extends QueueAddOptions =
153
133
  })
154
134
  }
155
135
 
136
+ this.sort = init.sort
156
137
  this.queue = []
157
138
  }
158
139
 
@@ -215,16 +196,11 @@ export class Queue<JobReturnType = unknown, JobOptions extends QueueAddOptions =
215
196
  }
216
197
 
217
198
  private enqueue (job: Job<JobOptions, JobReturnType>): void {
218
- if (this.queue[this.size - 1]?.priority >= job.priority) {
219
- this.queue.push(job)
220
- return
221
- }
199
+ this.queue.push(job)
222
200
 
223
- const index = lowerBound(
224
- this.queue, job,
225
- (a: Readonly< Job<JobOptions, JobReturnType>>, b: Readonly< Job<JobOptions, JobReturnType>>) => b.priority - a.priority
226
- )
227
- this.queue.splice(index, 0, job)
201
+ if (this.sort != null) {
202
+ this.queue.sort(this.sort)
203
+ }
228
204
  }
229
205
 
230
206
  /**
@@ -233,7 +209,7 @@ export class Queue<JobReturnType = unknown, JobOptions extends QueueAddOptions =
233
209
  async add (fn: RunFunction<JobOptions, JobReturnType>, options?: JobOptions): Promise<JobReturnType> {
234
210
  options?.signal?.throwIfAborted()
235
211
 
236
- const job = new Job<JobOptions, JobReturnType>(fn, options, options?.priority)
212
+ const job = new Job<JobOptions, JobReturnType>(fn, options)
237
213
 
238
214
  const p = job.join(options)
239
215
  .then(result => {
package/src/queue/job.ts CHANGED
@@ -21,17 +21,15 @@ export class Job <JobOptions extends AbortOptions = AbortOptions, JobReturnType
21
21
  public id: string
22
22
  public fn: (options: JobOptions) => Promise<JobReturnType>
23
23
  public options: JobOptions
24
- public priority: number
25
24
  public recipients: Array<JobRecipient<JobReturnType>>
26
25
  public status: JobStatus
27
26
  public readonly timeline: JobTimeline
28
27
  private readonly controller: AbortController
29
28
 
30
- constructor (fn: (options: JobOptions) => Promise<JobReturnType>, options: any, priority: number = 0) {
29
+ constructor (fn: (options: JobOptions) => Promise<JobReturnType>, options: any) {
31
30
  this.id = randomId()
32
31
  this.status = 'queued'
33
32
  this.fn = fn
34
- this.priority = priority
35
33
  this.options = options
36
34
  this.recipients = []
37
35
  this.timeline = {