Skip to content
Snippets Groups Projects
Commit a63db63a authored by Jason Mulligan's avatar Jason Mulligan
Browse files

Exposing `args` & `options` parameters of `child_process.fork()`

parent ffe8681c
Branches
No related tags found
No related merge requests found
# tiny-worker
Tiny WebWorker for Server
`require()` is available for flexible inline Worker scripts.
`require()` is available for flexible inline Worker scripts. Optional parameters `args` Array & `options` Object; see `child_process.fork()` documentation.
[![build status](https://secure.travis-ci.org/avoidwork/tiny-worker.svg)](http://travis-ci.org/avoidwork/tiny-worker)
......
......@@ -4,21 +4,24 @@ 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"); } }
var path = require("path");
var fork = require("child_process").fork;
var worker = path.join(__dirname, "worker.js");
var events = /^(error|message)$/;
var path = require("path"),
fork = require("child_process").fork,
worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/;
var Worker = (function () {
function Worker(arg) {
var _this = this;
var args = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? undefined : arguments[2];
_classCallCheck(this, Worker);
var isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg;
this.child = fork(worker);
this.child = fork(worker, args, options);
this.onerror = undefined;
this.onmessage = undefined;
......
"use strict";
module.exports = function () {};
module.exports = function () {
return void 0;
};
"use strict";
var fs = require("fs");
var path = require("path");
var vm = require("vm");
var noop = require(path.join(__dirname, "noop.js"));
var events = /^(error|message)$/;
var fs = require("fs"),
path = require("path"),
vm = require("vm"),
noop = require(path.join(__dirname, "noop.js")),
events = /^(error|message)$/;
function trim(arg) {
return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
......@@ -30,11 +30,11 @@ process.once("message", function (obj) {
process.exit(0);
},
postMessage: function postMessage(msg) {
process.send(JSON.stringify({ data: msg }));
process.send(JSON.stringify({ data: msg }, null, 0));
},
onmessage: void 0,
onerror: function onerror(err) {
process.send(JSON.stringify({ error: err.message, stack: err.stack }));
process.send(JSON.stringify({ error: err.message, stack: err.stack }, null, 0));
},
addEventListener: function addEventListener(event, fn) {
if (events.test(event)) {
......@@ -52,18 +52,14 @@ process.once("message", function (obj) {
files[_key] = arguments[_key];
}
var scripts = undefined;
if (files.length > 0) {
scripts = files.map(function (file) {
vm.createScript(files.map(function (file) {
return fs.readFileSync(file, "utf8");
}).join("\n");
vm.createScript(scripts).runInThisContext();
}).join("\n")).runInThisContext();
}
};
Object.keys(global.self).forEach(function (key) {
Reflect.ownKeys(global.self).forEach(function (key) {
global[key] = global.self[key];
});
......
{
"name": "tiny-worker",
"version": "1.1.7",
"version": "2.0.0",
"description": "Tiny WebWorker for Server",
"main": "lib/index.js",
"scripts": {
......
const path = require("path");
const fork = require("child_process").fork;
const worker = path.join(__dirname, "worker.js");
const events = /^(error|message)$/;
const path = require("path"),
fork = require("child_process").fork,
worker = path.join(__dirname, "worker.js"),
events = /^(error|message)$/;
class Worker {
constructor (arg) {
constructor (arg, args = undefined, options = undefined) {
let isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg;
this.child = fork(worker);
this.child = fork(worker, args, options);
this.onerror = undefined;
this.onmessage = undefined;
......
module.exports = function () {};
module.exports = () => void 0;
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const noop = require(path.join(__dirname, "noop.js"));
const events = /^(error|message)$/;
const fs = require("fs"),
path = require("path"),
vm = require("vm"),
noop = require(path.join(__dirname, "noop.js")),
events = /^(error|message)$/;
function trim (arg) {
return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
......@@ -13,7 +13,7 @@ function explode (arg) {
}
function toFunction (arg) {
let args = trim(arg.replace(/^.*\(/, "").replace(/[\t|\r|\n|\"|\']+/g, "").replace(/\).*/, "")),
const args = trim(arg.replace(/^.*\(/, "").replace(/[\t|\r|\n|\"|\']+/g, "").replace(/\).*/, "")),
body = trim(arg.replace(/^.*\{/, "").replace(/\}$/, ""));
return Function.apply(Function, explode(args).concat([body]));
......@@ -28,11 +28,11 @@ process.once("message", obj => {
process.exit(0);
},
postMessage: msg => {
process.send(JSON.stringify({data: msg}));
process.send(JSON.stringify({data: msg}, null, 0));
},
onmessage: void 0,
onerror: err => {
process.send(JSON.stringify({error: err.message, stack: err.stack}));
process.send(JSON.stringify({error: err.message, stack: err.stack}, null, 0));
},
addEventListener: (event, fn) => {
if (events.test(event)) {
......@@ -46,18 +46,12 @@ process.once("message", obj => {
global.require = require;
global.importScripts = (...files) => {
let scripts;
if (files.length > 0) {
scripts = files.map(file => {
return fs.readFileSync(file, "utf8");
}).join("\n");
vm.createScript(scripts).runInThisContext();
vm.createScript(files.map(file => fs.readFileSync(file, "utf8")).join("\n")).runInThisContext();
}
};
Object.keys(global.self).forEach(key => {
Reflect.ownKeys(global.self).forEach(key => {
global[key] = global.self[key];
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment