Open weekens opened 7 years ago
From readme: "Note: If you would like to require() native modules in a worker, please consider using the process-based tiny-worker instead. It does not use threads, but the WebWorker API is compatible."
Why cannot require()
be implemented in threads? Is this a fundamental limitation? Could you explain?
@Reltik native modules
@weekens require calls through node's native codebase, which is very much not thread safe, and would end up breaking things pretty badly.
@devsnek But maybe it is possible to polyfill require
call inside thread code and implement some kind of a global "require lock" to avoid callling require
from several threads simultaneously?
@weekens no, that's not possible. v8 is not thread safe.
even if you could somehow manage to call require (which v8::Isolate will not allow under any circumstance), then getting the resulting value from that back into your thread would be doubly impossible (again v8::Isolate will not allow it)
additionally normal importing is not part of the WebWorker API specification and therefore I doubt this will be changed. you can use ImportScripts()
for your own scripts.
If require could be passed into threads as arguments, that would do.
like this:
const id = new Function('args', 'require', 'const uuid = require("uuid");return uuid();')({}, require);
For the moment, I cannot require modules from inside a thread. The following code would print an error: