tiny-worker icon indicating copy to clipboard operation
tiny-worker copied to clipboard

Cannot load emscripten modules in node because of importScripts global definition

Open cancerberoSgx opened this issue 6 years ago • 0 comments

I'm trying to load emscripten (WebAssembly) modules in node.js with this library but I conclude is not possible without hacking (hard and ugly). The idea is to use native Node.js workers to load but since they are only availbale in latest using this library as a fallback made sense (as other libs)[https://github.com/andywer/threads.js] are doing

This library defines the global importScripts here

On the other side, emscripten use this same global to detect current script environment (browser, worker, node.js, etc. Then it assumes is a web worker and that breaks the application since it try to access the DOM for other stuff. Also it disables Node.js features like native filesystem.

Both behaviors are correct but but nevertheless after some though I concluded makes more sense to act on this project since:

  • Node.js workers (don't define the global in their contexts)
  • solutions are relatively simple and safe
  • solves a real user issue of this project (being able to load .wasm/asm.js which I don't think is a edge case
  • emscripten already support both kind of workers (node's and browser) and is unlikely a PR will be accepted just to be compatible with this particular project

I have two proposals would be very interested in PR here, but first I wanted to know your opinion:

The safer: an option so users can prevent importScripts to be defined at all if they are working with Node.js workers (new Worker('foo.js', {nodeWorkers: true})

The simpler: Don't declare the global importScriptsif in Node.js. Perhaps we could contemplate the exception of node.js + jsdom (for those who want to simulate web workers in node on tests ?

I Wait for any comment, thanks.I know probably this is not an issue since I'm assuming (and mentioned libraries too) that web and Node workers are the same standard while they probably not . But I think this is a valid case for this project, what do you think?. Thanks, awesome library!

cancerberoSgx avatar Sep 19 '19 02:09 cancerberoSgx