From a63db63a3a8437315fa70e56bbb99ccca18e378f Mon Sep 17 00:00:00 2001 From: Jason Mulligan <jason.mulligan@avoidwork.com> Date: Wed, 4 Jan 2017 20:29:04 -0500 Subject: [PATCH] Exposing `args` & `options` parameters of `child_process.fork()` --- README.md | 2 +- lib/index.js | 13 ++++++++----- lib/noop.js | 4 +++- lib/worker.js | 24 ++++++++++-------------- package.json | 2 +- src/index.js | 12 ++++++------ src/noop.js | 2 +- src/worker.js | 26 ++++++++++---------------- 8 files changed, 40 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index c4f0171..4fc73b4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # 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. [](http://travis-ci.org/avoidwork/tiny-worker) diff --git a/lib/index.js b/lib/index.js index 0c0b473..3336bd0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; diff --git a/lib/noop.js b/lib/noop.js index 7f0f4e8..312fce8 100644 --- a/lib/noop.js +++ b/lib/noop.js @@ -1,3 +1,5 @@ "use strict"; -module.exports = function () {}; +module.exports = function () { + return void 0; +}; diff --git a/lib/worker.js b/lib/worker.js index 71addb1..51c1a40 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -1,10 +1,10 @@ "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]; }); diff --git a/package.json b/package.json index 9ecc360..e23d783 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-worker", - "version": "1.1.7", + "version": "2.0.0", "description": "Tiny WebWorker for Server", "main": "lib/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index dd53ae5..27adfe2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,14 @@ -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; diff --git a/src/noop.js b/src/noop.js index ea41b01..0e0cebb 100644 --- a/src/noop.js +++ b/src/noop.js @@ -1 +1 @@ -module.exports = function () {}; +module.exports = () => void 0; diff --git a/src/worker.js b/src/worker.js index 0219f99..5ee73ff 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,8 +1,8 @@ -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]; }); -- GitLab