tiny-worker
tiny-worker copied to clipboard
TinyWorker doesn't accept ArrayBuffer as data of a message
Broken example in node:
var Worker = require("tiny-worker");
var worker = new Worker(function () {
self.onmessage = function (ev) {
postMessage(ev.data instanceof ArrayBuffer);
};
});
worker.onmessage = function (ev) {
console.log(ev.data);
worker.terminate();
};
var buf = new ArrayBuffer(2);
var bufView = new Uint8Array(buf);
bufView[0] = 1;
bufView[1] = 1;
worker.postMessage(buf);
Working example in javascript (the function needs to be added by blob):
https://jsfiddle.net/nraq2zdm/2/
See also https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
and linked documents there
Not sure if that can be done using node!
I see this could be working (use instead of JSON parse/stringify in passing messages) - seems unmaintained with 2 rotting issues, but still better than no support
https://www.npmjs.com/package/structured-clone
Pretty sure you'd need to take this to the core, since this lib just setups a js vm via child_process. I can't change how it works from downstream.
btw Blob is absent afaik.
I was thinking it could be changed here
- https://github.com/avoidwork/tiny-worker/blob/master/src/index.js#L102
- https://github.com/avoidwork/tiny-worker/blob/master/src/index.js#L67
- https://github.com/avoidwork/tiny-worker/blob/master/src/worker.js#L31
- https://github.com/avoidwork/tiny-worker/blob/master/src/worker.js#L60
- instead of JSON.parse/JSON.stringify, using this. (Maybe somehow connected with stringification of the resulting buffer.)
But I didn't have time to do it and test it yet.
changing to it to be buffer aware is likely good, kinda breaks everything though. if you could make it work consistently it should be ok 👍
(I never got around to actually doing this :( )