Skip to content
Snippets Groups Projects
Commit 60d95bad authored by Moritz Langenstein's avatar Moritz Langenstein
Browse files

(ml5717) Send Uint8Array as base64 string to worker

parent 577b8e79
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,8 @@ var _createClass = function () { function defineProperties(target, props) { for ...@@ -4,7 +4,8 @@ var _createClass = function () { function defineProperties(target, props) { for
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var path = require("path"), var BJSON = require("./uint8array-json.js"),
path = require("path"),
fork = require("child_process").fork, fork = require("child_process").fork,
worker = path.join(__dirname, "worker.js"), worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/, events = /^(error|message)$/,
...@@ -83,7 +84,7 @@ var Worker = function () { ...@@ -83,7 +84,7 @@ var Worker = function () {
}); });
this.child.on("message", function (msg) { this.child.on("message", function (msg) {
var message = JSON.parse(msg); var message = BJSON.parse(msg);
var error = void 0; var error = void 0;
if (!message.error && _this.onmessage) { if (!message.error && _this.onmessage) {
...@@ -111,7 +112,7 @@ var Worker = function () { ...@@ -111,7 +112,7 @@ var Worker = function () {
}, { }, {
key: "postMessage", key: "postMessage",
value: function postMessage(msg) { value: function postMessage(msg) {
this.child.send(JSON.stringify({ data: msg }, null, 0)); this.child.send(BJSON.stringify({ data: msg }, /*null,*/ 0));
} }
}, { }, {
key: "terminate", key: "terminate",
......
// Adapted from https://github.com/jprichardson/buffer-json (MIT license)
function stringify (value, space) {
return JSON.stringify(value, replacer, space)
}
function parse (text) {
return JSON.parse(text, reviver)
}
function replacer (key, value) {
if (value instanceof Uint8Array) {
return 'base64:' + Buffer.from(value).toString('base64')
}
return value
}
function reviver (key, value) {
if (typeof value == 'string' && value.startsWith('base64:')) {
return Uint8Array.from(Buffer.from(value.slice('base64:'.length), 'base64'))
}
return value
}
module.exports = {
stringify,
parse,
replacer,
reviver
}
"use strict"; "use strict";
var fs = require("fs"), var BJSON = require("./uint8array-json.js"),
fs = require("fs"),
path = require("path"), path = require("path"),
vm = require("vm"), vm = require("vm"),
noop = require(path.join(__dirname, "noop.js")), noop = require(path.join(__dirname, "noop.js")),
...@@ -27,11 +28,11 @@ process.once("message", function (obj) { ...@@ -27,11 +28,11 @@ process.once("message", function (obj) {
process.exit(0); process.exit(0);
}, },
postMessage: function postMessage(msg) { postMessage: function postMessage(msg) {
process.send(JSON.stringify({ data: msg }, null, 0)); process.send(BJSON.stringify({ data: msg }, /*null,*/ 0));
}, },
onmessage: void 0, onmessage: void 0,
onerror: function onerror(err) { onerror: function onerror(err) {
process.send(JSON.stringify({ error: err.message, stack: err.stack }, null, 0)); process.send(BJSON.stringify({ error: err.message, stack: err.stack }, /*null,*/ 0));
}, },
addEventListener: function addEventListener(event, fn) { addEventListener: function addEventListener(event, fn) {
if (events.test(event)) { if (events.test(event)) {
...@@ -62,7 +63,7 @@ process.once("message", function (obj) { ...@@ -62,7 +63,7 @@ process.once("message", function (obj) {
process.on("message", function (msg) { process.on("message", function (msg) {
try { try {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg)); (global.onmessage || global.self.onmessage || noop)(BJSON.parse(msg));
} catch (err) { } catch (err) {
(global.onerror || global.self.onerror || noop)(err); (global.onerror || global.self.onerror || noop)(err);
} }
......
This diff is collapsed.
const path = require("path"),
fork = require("child_process").fork,
worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/,
defaultPorts = {inspect: 9229, debug: 5858};
let range = {min: 1, max: 300};
class Worker {
constructor (arg, args = [], options = {cwd: process.cwd()}) {
let isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg;
if (!options.cwd) {
options.cwd = process.cwd();
}
//get all debug related parameters
var debugVars = process.execArgv.filter(execArg => {
return (/(debug|inspect)/).test(execArg);
});
if (debugVars.length > 0 && !options.noDebugRedirection) {
if (!options.execArgv) { //if no execArgs are given copy all arguments
debugVars = Array.from(process.execArgv);
options.execArgv = [];
}
let inspectIndex = debugVars.findIndex(debugArg => { //get index of inspect parameter
return (/^--inspect(-brk)?(=\d+)?$/).test(debugArg);
});
let debugIndex = debugVars.findIndex(debugArg => { //get index of debug parameter
return (/^--debug(-brk)?(=\d+)?$/).test(debugArg);
});
let portIndex = inspectIndex >= 0 ? inspectIndex : debugIndex; //get index of port, inspect has higher priority
if (portIndex >= 0) {
var match = (/^--(debug|inspect)(?:-brk)?(?:=(\d+))?$/).exec(debugVars[portIndex]); //get port
var port = defaultPorts[match[1]];
if (match[2]) {
port = parseInt(match[2]);
}
debugVars[portIndex] = "--" + match[1] + "=" + (port + range.min + Math.floor(Math.random() * (range.max - range.min))); //new parameter
if (debugIndex >= 0 && debugIndex !== portIndex) { //remove "-brk" from debug if there
match = (/^(--debug)(?:-brk)?(.*)/).exec(debugVars[debugIndex]);
debugVars[debugIndex] = match[1] + (match[2] ? match[2] : "");
}
}
options.execArgv = options.execArgv.concat(debugVars);
}
delete options.noDebugRedirection;
this.child = fork(worker, args, options);
this.onerror = undefined;
this.onmessage = undefined;
this.child.on("error", e => {
if (this.onerror) {
this.onerror.call(this, e);
}
});
this.child.on("message", msg => {
const message = JSON.parse(msg);
let error;
if (!message.error && this.onmessage) {
this.onmessage.call(this, message);
}
if (message.error && this.onerror) {
error = new Error(message.error);
error.stack = message.stack;
this.onerror.call(this, error);
}
});
this.child.send({ input: input, isfn: isfn, cwd: options.cwd, esm: options.esm });
}
static setRange (min, max) {
if (min >= max) {
return false;
}
range.min = min;
range.max = max;
return true;
}
addEventListener (event, fn) {
if (events.test(event)) {
this["on" + event] = fn;
}
}
postMessage (msg) {
this.child.send(JSON.stringify({data: msg}, null, 0));
}
terminate () {
this.child.kill("SIGINT");
}
}
module.exports = Worker;
module.exports = () => void 0;
const fs = require("fs"),
path = require("path"),
vm = require("vm"),
noop = require(path.join(__dirname, "noop.js")),
events = /^(error|message)$/;
function toFunction (arg) {
var __worker_evaluated_function_ = null;
eval("__worker_evaluated_function_ = (" + arg + ")"); // eslint-disable-line no-eval
return __worker_evaluated_function_;
}
// Bootstraps the Worker
process.once("message", obj => {
const { isfn, input, esm, cwd } = obj;
const exp = isfn ? toFunction(input) : esm ? `require("${input}");` : fs.readFileSync(input, "utf8");
global.self = {
close: () => {
process.exit(0);
},
postMessage: msg => {
process.send(JSON.stringify({ data: msg }, null, 0));
},
onmessage: void 0,
onerror: err => {
process.send(JSON.stringify({ error: err.message, stack: err.stack }, null, 0));
},
addEventListener: (event, fn) => {
if (events.test(event)) {
global["on" + event] = global.self["on" + event] = fn;
}
}
};
global.__dirname = cwd;
global.__filename = __filename;
global.require = esm ? require("esm")(module) : require;
global.importScripts = (...files) => {
if (files.length > 0) {
vm.createScript(files.map(file => fs.readFileSync(file, "utf8")).join("\n")).runInThisContext();
}
};
Object.keys(global.self).forEach(key => {
global[key] = global.self[key];
});
process.on("message", msg => {
try {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg));
} catch (err) {
(global.onerror || global.self.onerror || noop)(err);
}
});
process.on("error", err => {
(global.onerror || global.self.onerror || noop)(err);
});
if (typeof exp === "function") {
exp();
} else {
vm.createScript(exp).runInThisContext();
}
});
process.once("disconnect", () => process.exit());
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment