Skip to content

Commit e604297

Browse files
fix: worker specifier can be a string or an URL instance (#95)
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent 576ce92 commit e604297

File tree

8 files changed

+56
-44
lines changed

8 files changed

+56
-44
lines changed

docs/api.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Table of contents
44

55
- [Pool](#pool)
6-
- [`pool = new FixedThreadPool(numberOfThreads, fileURL, opts)`](#pool--new-fixedthreadpoolnumberofthreads-fileurl-opts)
7-
- [`pool = new DynamicThreadPool(min, max, fileURL, opts)`](#pool--new-dynamicthreadpoolmin-max-fileurl-opts)
6+
- [`pool = new FixedThreadPool(numberOfThreads, specifier, opts)`](#pool--new-fixedthreadpoolnumberofthreads-specifier-opts)
7+
- [`pool = new DynamicThreadPool(min, max, specifier, opts)`](#pool--new-dynamicthreadpoolmin-max-specifier-opts)
88
- [`pool.execute(data, name, abortSignal, transferList)`](#poolexecutedata-name-abortsignal-transferlist)
99
- [`pool.mapExecute(data, name, abortSignals, transferList)`](#poolmapexecutedata-name-abortsignals-transferlist)
1010
- [`pool.start()`](#poolstart)
@@ -25,20 +25,20 @@
2525

2626
## Pool
2727

28-
### `pool = new FixedThreadPool(numberOfThreads, fileURL, opts)`
28+
### `pool = new FixedThreadPool(numberOfThreads, specifier, opts)`
2929

3030
`numberOfThreads` (mandatory) Number of workers for this pool.\
31-
`fileURL` (mandatory) URL to a file with a worker implementation.\
31+
`specifier` (mandatory) Specifier to a file with a worker implementation.\
3232
`opts` (optional) An object with the pool options properties described below.
3333

34-
### `pool = new DynamicThreadPool(min, max, fileURL, opts)`
34+
### `pool = new DynamicThreadPool(min, max, specifier, opts)`
3535

3636
`min` (mandatory) Same as _FixedThreadPool_ numberOfThreads, this number of
3737
workers will be always active.\
3838
`max` (mandatory) Max number of workers that this pool can contain, the newly
3939
created workers will die after a threshold (default is 1 minute, you can
4040
override it in your worker implementation).\
41-
`fileURL` (mandatory) URL to a file with a worker implementation.\
41+
`specifier` (mandatory) Specifier to a file with a worker implementation.\
4242
`opts` (optional) An object with the pool options properties described below.
4343

4444
### `pool.execute(data, name, abortSignal, transferList)`

src/pools/abstract-pool.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
} from './selection-strategies/selection-strategies-types.ts'
4242
import { WorkerChoiceStrategiesContext } from './selection-strategies/worker-choice-strategies-context.ts'
4343
import {
44-
checkFileURL,
44+
checkSpecifier,
4545
checkValidPriority,
4646
checkValidTasksQueueOptions,
4747
checkValidWorkerChoiceStrategy,
@@ -152,13 +152,13 @@ export abstract class AbstractPool<
152152
* Constructs a new poolifier pool.
153153
*
154154
* @param minimumNumberOfWorkers - Minimum number of workers that this pool manages.
155-
* @param fileURL - URL to the worker file.
155+
* @param specifier - Specifier to the worker file.
156156
* @param opts - Options for the pool.
157157
* @param maximumNumberOfWorkers - Maximum number of workers that this pool manages.
158158
*/
159159
public constructor(
160160
protected readonly minimumNumberOfWorkers: number,
161-
protected readonly fileURL: URL,
161+
protected readonly specifier: URL | string,
162162
protected readonly opts: PoolOptions,
163163
protected readonly maximumNumberOfWorkers?: number,
164164
) {
@@ -168,7 +168,7 @@ export abstract class AbstractPool<
168168
)
169169
}
170170
this.checkPoolType()
171-
checkFileURL(this.fileURL)
171+
checkSpecifier(this.specifier)
172172
this.checkMinimumNumberOfWorkers(this.minimumNumberOfWorkers)
173173
this.checkPoolOptions(this.opts)
174174

@@ -208,7 +208,7 @@ export abstract class AbstractPool<
208208
private checkPoolType(): void {
209209
if (this.type === PoolTypes.fixed && this.maximumNumberOfWorkers != null) {
210210
throw new Error(
211-
'Cannot instantiate a fixed pool with a maximum number of workers specified at initialization',
211+
'Cannot instantiate a fixed pool with a maximum number of workers defined at initialization',
212212
)
213213
}
214214
}
@@ -2292,15 +2292,19 @@ export abstract class AbstractPool<
22922292
* @returns The created worker node.
22932293
*/
22942294
private createWorkerNode(): IWorkerNode<Worker, Data> {
2295-
const workerNode = new WorkerNode<Worker, Data>(this.worker, this.fileURL, {
2296-
workerOptions: this.opts.workerOptions,
2297-
tasksQueueBackPressureSize: this.opts.tasksQueueOptions?.size ??
2298-
getDefaultTasksQueueOptions(
2299-
this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers,
2300-
).size,
2301-
tasksQueueBucketSize: defaultBucketSize,
2302-
tasksQueuePriority: this.getTasksQueuePriority(),
2303-
})
2295+
const workerNode = new WorkerNode<Worker, Data>(
2296+
this.worker,
2297+
this.specifier,
2298+
{
2299+
workerOptions: this.opts.workerOptions,
2300+
tasksQueueBackPressureSize: this.opts.tasksQueueOptions?.size ??
2301+
getDefaultTasksQueueOptions(
2302+
this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers,
2303+
).size,
2304+
tasksQueueBucketSize: defaultBucketSize,
2305+
tasksQueuePriority: this.getTasksQueuePriority(),
2306+
},
2307+
)
23042308
// Flag the worker node as ready at pool startup.
23052309
if (this.starting) {
23062310
workerNode.info.ready = true

src/pools/thread/dynamic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export class DynamicThreadPool<
3232
*
3333
* @param min - Minimum number of threads which are always active.
3434
* @param max - Maximum number of threads that can be created by this pool.
35-
* @param fileURL - URL to an implementation of a `ThreadWorker` file.
35+
* @param specifier - Specifier to an implementation of a `ThreadWorker` file.
3636
* @param opts - Options for this dynamic thread pool.
3737
*/
3838
public constructor(
3939
min: number,
4040
max: number,
41-
fileURL: URL,
41+
specifier: URL | string,
4242
opts: ThreadPoolOptions = {},
4343
) {
44-
super(min, fileURL, opts, max)
44+
super(min, specifier, opts, max)
4545
checkDynamicPoolSize(
4646
this.minimumNumberOfWorkers,
4747
this.maximumNumberOfWorkers!,

src/pools/thread/fixed.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ export class FixedThreadPool<
2626
* Constructs a new poolifier fixed thread pool.
2727
*
2828
* @param numberOfThreads - Number of threads for this pool.
29-
* @param fileURL - URL to an implementation of a `ThreadWorker` file.
29+
* @param specifier - Specifier to an implementation of a `ThreadWorker` file.
3030
* @param opts - Options for this fixed thread pool.
3131
*/
3232
public constructor(
3333
numberOfThreads: number,
34-
fileURL: URL,
34+
specifier: URL | string,
3535
opts: ThreadPoolOptions = {},
3636
maximumNumberOfThreads?: number,
3737
) {
38-
super(numberOfThreads, fileURL, opts, maximumNumberOfThreads)
38+
super(numberOfThreads, specifier, opts, maximumNumberOfThreads)
3939
}
4040
/** @inheritDoc */
4141
protected isMain(): boolean {

src/pools/utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ export const getDefaultTasksQueueOptions = (
5858
})
5959
}
6060

61-
export const checkFileURL = (fileURL: URL | undefined): void => {
62-
if (fileURL == null) {
63-
throw new TypeError('The worker URL must be specified')
61+
export const checkSpecifier = (specifier: URL | string | undefined): void => {
62+
if (specifier == null) {
63+
throw new TypeError('The worker specifier must be defined')
6464
}
65-
if (fileURL instanceof URL === false) {
66-
throw new TypeError('The worker URL must be an instance of URL')
65+
if (typeof specifier !== 'string' && !(specifier instanceof URL)) {
66+
throw new TypeError(
67+
'The worker specifier must be a string or an instance of URL',
68+
)
6769
}
6870
}
6971

@@ -178,7 +180,7 @@ export const checkValidTasksQueueOptions = (
178180

179181
export const checkWorkerNodeArguments = (
180182
type: WorkerType | undefined,
181-
fileURL: URL | undefined,
183+
specifier: URL | string | undefined,
182184
opts: WorkerNodeOptions | undefined,
183185
): void => {
184186
if (type == null) {
@@ -189,7 +191,7 @@ export const checkWorkerNodeArguments = (
189191
`Cannot construct a worker node with an invalid worker type '${type}'`,
190192
)
191193
}
192-
checkFileURL(fileURL)
194+
checkSpecifier(specifier)
193195
if (opts == null) {
194196
throw new TypeError(
195197
'Cannot construct a worker node without worker node options',
@@ -396,12 +398,12 @@ export const messageListenerToEventListener = <Message = unknown>(
396398

397399
export const createWorker = <Worker extends IWorker>(
398400
type: WorkerType,
399-
fileURL: URL,
401+
specifier: URL | string,
400402
opts: { workerOptions?: WorkerOptions },
401403
): Worker => {
402404
switch (type) {
403405
case WorkerTypes.web:
404-
return new Worker(fileURL, {
406+
return new Worker(specifier, {
405407
...(runtime === JSRuntime.bun && { smol: true }),
406408
...opts.workerOptions,
407409
type: 'module',

src/pools/worker-node.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ export class WorkerNode<Worker extends IWorker, Data = unknown>
4848
* Constructs a new worker node.
4949
*
5050
* @param type - The worker type.
51-
* @param fileURL - URL to the worker file.
51+
* @param specifier - Specifier to the worker file.
5252
* @param opts - The worker node options.
5353
*/
54-
constructor(type: WorkerType, fileURL: URL, opts: WorkerNodeOptions) {
54+
constructor(
55+
type: WorkerType,
56+
specifier: URL | string,
57+
opts: WorkerNodeOptions,
58+
) {
5559
super()
56-
checkWorkerNodeArguments(type, fileURL, opts)
57-
this.worker = createWorker<Worker>(type, fileURL, {
60+
checkWorkerNodeArguments(type, specifier, opts)
61+
this.worker = createWorker<Worker>(type, specifier, {
5862
workerOptions: opts.workerOptions,
5963
})
6064
this.info = initWorkerInfo(this.worker)

tests/pools/abstract-pool.test.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ describe({
6565
expect(pool.destroying).toBe(false)
6666
})
6767

68-
it('Verify that fileURL is checked', () => {
68+
it('Verify that specifier is checked', () => {
6969
expect(() => new FixedThreadPool(numberOfWorkers)).toThrow(
70-
new TypeError('The worker URL must be specified'),
70+
new TypeError('The worker specifier must be defined'),
7171
)
7272
expect(() => new FixedThreadPool(numberOfWorkers, 0)).toThrow(
73-
new TypeError('The worker URL must be an instance of URL'),
73+
new TypeError(
74+
'The worker specifier must be a string or an instance of URL',
75+
),
7476
)
7577
})
7678

@@ -127,7 +129,7 @@ describe({
127129
),
128130
).toThrow(
129131
new Error(
130-
'Cannot instantiate a fixed pool with a maximum number of workers specified at initialization',
132+
'Cannot instantiate a fixed pool with a maximum number of workers defined at initialization',
131133
),
132134
)
133135
})

tests/pools/thread/dynamic.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe({
104104

105105
it('Validation of inputs test', () => {
106106
expect(() => new DynamicThreadPool(min)).toThrow(
107-
'The worker URL must be specified',
107+
'The worker specifier must be defined',
108108
)
109109
})
110110

0 commit comments

Comments
 (0)