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

TinyWorker doesn't accept ArrayBuffer as data of a message

Open karelbilek opened this issue 8 years ago • 7 comments

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/

karelbilek avatar Jul 26 '17 02:07 karelbilek

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!

karelbilek avatar Jul 26 '17 02:07 karelbilek

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

karelbilek avatar Jul 26 '17 02:07 karelbilek

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.

avoidwork avatar Jul 26 '17 11:07 avoidwork

btw Blob is absent afaik.

avoidwork avatar Jul 26 '17 11:07 avoidwork

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.

karelbilek avatar Jul 26 '17 15:07 karelbilek

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 👍

avoidwork avatar Jul 26 '17 16:07 avoidwork

(I never got around to actually doing this :( )

karelbilek avatar Dec 07 '18 08:12 karelbilek