Skip to content

Commit dd30ced

Browse files
authored
feat: accept a URL instance as filename (#141)
1 parent a067b09 commit dd30ced

3 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _In case you need more tiny libraries like tinypool or tinyspy, please consider
2727
import Tinypool from 'tinypool'
2828

2929
const pool = new Tinypool({
30-
filename: new URL('./worker.mjs', import.meta.url).href,
30+
filename: new URL('./worker.mjs', import.meta.url),
3131
})
3232
const result = await pool.run({ a: 4, b: 6 })
3333
console.log(result) // Prints 10
@@ -55,7 +55,7 @@ import Tinypool from 'tinypool'
5555
import { MessageChannel } from 'node:worker_threads'
5656

5757
const pool = new Tinypool({
58-
filename: new URL('./worker.mjs', import.meta.url).href,
58+
filename: new URL('./worker.mjs', import.meta.url),
5959
})
6060
const { port1, port2 } = new MessageChannel()
6161
const promise = pool.run({ port: port1 }, { transferList: [port1] })
@@ -98,7 +98,7 @@ import Tinypool from 'tinypool'
9898

9999
const pool = new Tinypool({
100100
runtime: 'child_process',
101-
filename: new URL('./worker.mjs', import.meta.url).href,
101+
filename: new URL('./worker.mjs', import.meta.url),
102102
})
103103
const result = await pool.run({ a: 4, b: 6 })
104104
console.log(result) // Prints 10
@@ -124,7 +124,7 @@ import Tinypool from 'tinypool'
124124

125125
const pool = new Tinypool({
126126
runtime: 'child_process',
127-
filename: new URL('./worker.mjs', import.meta.url).href,
127+
filename: new URL('./worker.mjs', import.meta.url),
128128
})
129129

130130
const messages = []

‎src/index.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class ArrayTaskQueue implements TaskQueue {
134134
}
135135

136136
interface Options {
137-
filename?: string | null
137+
filename?: string | URL | null
138138
runtime?: 'worker_threads' | 'child_process'
139139
name?: string
140140
minThreads?: number
@@ -187,15 +187,15 @@ const kDefaultOptions: FilledOptions = {
187187
interface RunOptions {
188188
transferList?: TransferList
189189
channel?: TinypoolChannel
190-
filename?: string | null
190+
filename?: string | URL | null
191191
signal?: AbortSignalAny | null
192192
name?: string | null
193193
runtime?: Options['runtime']
194194
}
195195

196196
interface FilledRunOptions extends RunOptions {
197197
transferList: TransferList | never
198-
filename: string | null
198+
filename: string | URL | null
199199
signal: AbortSignalAny | null
200200
name: string | null
201201
}
@@ -250,7 +250,14 @@ type TransferList = MessagePort extends {
250250
: never
251251
type TransferListItem = TransferList extends (infer T)[] ? T : never
252252

253-
function maybeFileURLToPath(filename: string): string {
253+
function maybeFileURLToPath(filename: string | URL): string {
254+
// A URL instance is normalised the same way the equivalent string is: a file: URL
255+
// becomes a path, and any other scheme is passed through for the worker to import().
256+
if (typeof filename !== 'string') {
257+
return filename.protocol === 'file:'
258+
? fileURLToPath(filename)
259+
: filename.href
260+
}
254261
return filename.startsWith('file:')
255262
? fileURLToPath(new URL(filename))
256263
: filename
@@ -929,7 +936,7 @@ class ThreadPool {
929936
if (name == null) {
930937
name = this.options.name
931938
}
932-
if (typeof filename !== 'string') {
939+
if (typeof filename !== 'string' && !(filename instanceof URL)) {
933940
return Promise.reject(Errors.FilenameNotProvided())
934941
}
935942
filename = maybeFileURLToPath(filename)

‎test/simple.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,65 @@ test('filename can be a file:// URL to an ESM module', async () => {
143143
expect(result).toBe(42)
144144
})
145145

146+
test('filename can be a URL instance', async () => {
147+
const worker = new Tinypool({
148+
filename: pathToFileURL(resolve(__dirname, 'fixtures/eval.js')),
149+
})
150+
const result = await worker.run('42')
151+
expect(result).toBe(42)
152+
})
153+
154+
test('filename can be a URL instance to an ESM module', async () => {
155+
const worker = new Tinypool({
156+
filename: pathToFileURL(resolve(__dirname, 'fixtures/esm-export.mjs')),
157+
})
158+
const result = await worker.run('42')
159+
expect(result).toBe(42)
160+
})
161+
162+
test('run() accepts a URL instance as filename', async () => {
163+
const worker = new Tinypool()
164+
const result = await worker.run('42', {
165+
filename: pathToFileURL(resolve(__dirname, 'fixtures/eval.js')),
166+
})
167+
expect(result).toBe(42)
168+
})
169+
170+
test('a URL instance overrides the pool filename per task', async () => {
171+
const worker = new Tinypool({
172+
filename: resolve(__dirname, 'fixtures/eval.js'),
173+
})
174+
const result = await worker.run('42', {
175+
filename: pathToFileURL(resolve(__dirname, 'fixtures/esm-export.mjs')),
176+
})
177+
expect(result).toBe(42)
178+
})
179+
180+
test('filename can be a URL instance with runtime child_process', async () => {
181+
const worker = new Tinypool({
182+
filename: pathToFileURL(resolve(__dirname, 'fixtures/eval.js')),
183+
runtime: 'child_process',
184+
})
185+
const result = await worker.run('42')
186+
expect(result).toBe(42)
187+
})
188+
189+
test('a non-file: URL instance is passed through to the worker', async () => {
190+
const worker = new Tinypool({
191+
filename: new URL('data:text/javascript,export default () => 42'),
192+
})
193+
const result = await worker.run(null)
194+
expect(result).toBe(42)
195+
})
196+
197+
test('run() rejects a filename that is neither string nor URL', async () => {
198+
const worker = new Tinypool()
199+
await expect(
200+
// @ts-expect-error -- deliberately wrong type
201+
worker.run('42', { filename: 42 })
202+
).rejects.toThrow('filename must be provided to run() or in options object')
203+
})
204+
146205
test('named tasks work', async () => {
147206
const worker = new Tinypool({
148207
filename: resolve(__dirname, 'fixtures/multiple.js'),

0 commit comments

Comments
 (0)