Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Deno Compatibility #486

Open
maliknajjar opened this issue Jan 30, 2025 · 7 comments
Open

Support for Deno Compatibility #486

maliknajjar opened this issue Jan 30, 2025 · 7 comments

Comments

@maliknajjar
Copy link

First of all, I want to say this library is amazing!

Since Deno supports the Web Worker API, I attempted to initialize the pool with the web worker type to configure Deno's permissions accordingly. However, when I try to run it, I encounter the following error (see the image below):

Image

@josdejong
Copy link
Owner

Thanks :)

A worker is always a separate script: https://github.com/josdejong/workerpool/blob/master/src/WorkerHandler.js#L66 maybe Deno doesn't like that?

@joshLong145
Copy link
Contributor

joshLong145 commented Jan 31, 2025

What resolve this issue is using a Blob URL when the resolved worker type is a WebWorker. This should work in Deno. However, it will require users of NodeJS to shim if not >=18.

I did a quick test and the tests do pass if we use a Blob URL in node environments. Still trying to get a test working in Deno to confirm this will resolve the issue. But in theory it should as Blob is supported through URL: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static

Would it be possible to provide your testing environment?

@maliknajjar
Copy link
Author

maliknajjar commented Jan 31, 2025

yes sure. this is my testing environment

deno 2.1.4 (stable, release, x86_64-unknown-linux-gnu)
v8 13.0.245.12-rusty
typescript 5.6.2

Using a Blob URL would be a reliable solution since it is expected to be supported across all runtimes.

@maliknajjar
Copy link
Author

It will likely depend on how you obtain the Blob. I don't think using fs to fetch the worker.js file from within the package will work. In Deno, you need to use the import statement to ensure the file is included.

In fact, there's an open issue in Deno about supporting the import of binary or text files:
denoland/deno#25354 (comment)

Unlike npm, which imports everything from the remote package into your repository, Deno only downloads the files that are explicitly imported in the package's JavaScript code.

Currently, Deno supports importing only JSON files in addition to JS and TS files.

@joshLong145
Copy link
Contributor

joshLong145 commented Feb 1, 2025

It will likely depend on how you obtain the Blob. I don't think using fs to fetch the worker.js file from within the package will work. In Deno, you need to use the import statement to ensure the file is included.

This is true, Deno also only supports module type Workers at this time, so you will want to supply module: true see here.

In fact, there's an open issue in Deno about supporting the import of binary or text files:
denoland/deno#25354 (comment)

It also may be possible to use a URL wrapper over a Blob as Deno seems to support file URL which would then allow Deno to load the worker script as a file url path.

@maliknajjar
Copy link
Author

It also may be possible to use a URL wrapper over a Blob as Deno seems to support file URL which would then allow Deno to load the worker script as a file url path.

Actually, this is correct. It's their recommended approach, and I tested it both directly and by running it from another package. It appears to work properly in Deno.
I assume it should function correctly when imported as a package using the npm: prefix, like npm:workerpool.

@joshLong145
Copy link
Contributor

joshLong145 commented Feb 3, 2025

It may be best to use Blob for consistency as we already wrap in cases where we load the embeddedworker in cases where a script is not provided here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants