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
No related branches found
No related tags found
No related merge requests found
# tiny-worker # tiny-worker
Tiny WebWorker for Server 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) [![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 ...@@ -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"); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var path = require("path"); var path = require("path"),
var fork = require("child_process").fork; fork = require("child_process").fork,
var worker = path.join(__dirname, "worker.js"); worker = path.join(__dirname, "worker.js"),
var events = /^(error|message)$/; events = /^(error|message)$/;
var Worker = (function () { var Worker = (function () {
function Worker(arg) { function Worker(arg) {
var _this = this; 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); _classCallCheck(this, Worker);
var isfn = typeof arg === "function", var isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg; input = isfn ? arg.toString() : arg;
this.child = fork(worker); this.child = fork(worker, args, options);
this.onerror = undefined; this.onerror = undefined;
this.onmessage = undefined; this.onmessage = undefined;
......
"use strict"; "use strict";
module.exports = function () {}; module.exports = function () {
return void 0;
};
"use strict"; "use strict";
var fs = require("fs"); var fs = require("fs"),
var path = require("path"); path = require("path"),
var vm = require("vm"); vm = require("vm"),
var noop = require(path.join(__dirname, "noop.js")); noop = require(path.join(__dirname, "noop.js")),
var events = /^(error|message)$/; events = /^(error|message)$/;
function trim(arg) { function trim(arg) {
return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, ""); return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
...@@ -30,11 +30,11 @@ process.once("message", function (obj) { ...@@ -30,11 +30,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 })); process.send(JSON.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 })); process.send(JSON.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)) {
...@@ -52,18 +52,14 @@ process.once("message", function (obj) { ...@@ -52,18 +52,14 @@ process.once("message", function (obj) {
files[_key] = arguments[_key]; files[_key] = arguments[_key];
} }
var scripts = undefined;
if (files.length > 0) { if (files.length > 0) {
scripts = files.map(function (file) { vm.createScript(files.map(function (file) {
return fs.readFileSync(file, "utf8"); return fs.readFileSync(file, "utf8");
}).join("\n"); }).join("\n")).runInThisContext();
vm.createScript(scripts).runInThisContext();
} }
}; };
Object.keys(global.self).forEach(function (key) { Reflect.ownKeys(global.self).forEach(function (key) {
global[key] = global.self[key]; global[key] = global.self[key];
}); });
......
{ {
"name": "tiny-worker", "name": "tiny-worker",
"version": "1.1.7", "version": "2.0.0",
"description": "Tiny WebWorker for Server", "description": "Tiny WebWorker for Server",
"main": "lib/index.js", "main": "lib/index.js",
"scripts": { "scripts": {
......
const path = require("path"); const path = require("path"),
const fork = require("child_process").fork; fork = require("child_process").fork,
const worker = path.join(__dirname, "worker.js"); worker = path.join(__dirname, "worker.js"),
const events = /^(error|message)$/; events = /^(error|message)$/;
class Worker { class Worker {
constructor (arg) { constructor (arg, args = undefined, options = undefined) {
let isfn = typeof arg === "function", let isfn = typeof arg === "function",
input = isfn ? arg.toString() : arg; input = isfn ? arg.toString() : arg;
this.child = fork(worker); this.child = fork(worker, args, options);
this.onerror = undefined; this.onerror = undefined;
this.onmessage = undefined; this.onmessage = undefined;
......
module.exports = function () {}; module.exports = () => void 0;
const fs = require("fs"); const fs = require("fs"),
const path = require("path"); path = require("path"),
const vm = require("vm"); vm = require("vm"),
const noop = require(path.join(__dirname, "noop.js")); noop = require(path.join(__dirname, "noop.js")),
const events = /^(error|message)$/; events = /^(error|message)$/;
function trim (arg) { function trim (arg) {
return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, ""); return arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
...@@ -13,7 +13,7 @@ function explode (arg) { ...@@ -13,7 +13,7 @@ function explode (arg) {
} }
function toFunction (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(/\}$/, "")); body = trim(arg.replace(/^.*\{/, "").replace(/\}$/, ""));
return Function.apply(Function, explode(args).concat([body])); return Function.apply(Function, explode(args).concat([body]));
...@@ -28,11 +28,11 @@ process.once("message", obj => { ...@@ -28,11 +28,11 @@ process.once("message", obj => {
process.exit(0); process.exit(0);
}, },
postMessage: msg => { postMessage: msg => {
process.send(JSON.stringify({data: msg})); process.send(JSON.stringify({data: msg}, null, 0));
}, },
onmessage: void 0, onmessage: void 0,
onerror: err => { 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) => { addEventListener: (event, fn) => {
if (events.test(event)) { if (events.test(event)) {
...@@ -46,18 +46,12 @@ process.once("message", obj => { ...@@ -46,18 +46,12 @@ process.once("message", obj => {
global.require = require; global.require = require;
global.importScripts = (...files) => { global.importScripts = (...files) => {
let scripts;
if (files.length > 0) { if (files.length > 0) {
scripts = files.map(file => { vm.createScript(files.map(file => fs.readFileSync(file, "utf8")).join("\n")).runInThisContext();
return fs.readFileSync(file, "utf8");
}).join("\n");
vm.createScript(scripts).runInThisContext();
} }
}; };
Object.keys(global.self).forEach(key => { Reflect.ownKeys(global.self).forEach(key => {
global[key] = global.self[key]; global[key] = global.self[key];
}); });
......
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