tinypool icon indicating copy to clipboard operation
tinypool copied to clipboard

Accept `URL` as `filename`

Open fisker opened this issue 10 months ago • 2 comments

const pool = new Tinypool({
-  filename: new URL('./worker.mjs', import.meta.url).href,
+  filename: new URL('./worker.mjs', import.meta.url),
})
  1. Convenient to use
  2. Align with Worker constructor

fisker avatar Feb 17 '25 08:02 fisker

This should be doable, as child_process.fork() also supports it: https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options

Though we'll need to make sure it works on Windows too, as we've had URL issues there before.

AriPerkkio avatar Feb 19 '25 07:02 AriPerkkio

I was thinking change

https://github.com/tinylibs/tinypool/blob/ec78ce94b6621a0c1234fd5aec1a1400981a31ce/src/index.ts#L304

to

this.filename = filename instanceof URL ? filename.href : filename

I don't think it related to Node.js API support

fisker avatar Feb 19 '25 08:02 fisker

Would be very much interested in this as I have a need to run js code from a blob.

import Tinypool from "tinypool";

const sourceCode = `
export default ({a, b}) => {
  return a + b;
};
`

const sourceCodeBlob = new Blob([sourceCode], { type: 'application/javascript' });

const pool = new Tinypool({
    filename: URL.createObjectURL(sourceCodeBlob),
});

const result = await pool.run({ a: 1, b: 2 });

console.log(result); // Outputs: 3

await pool.destroy();

There are security features in place before this in my setup.

Currently i would need to write it to disk, which is not possible in my case.

schplitt avatar Jul 03 '25 07:07 schplitt