diff --git a/README.md b/README.md index 034c4832ffd9c089c6fa01b26de4fbdc6fd333e9..b73787334ced7e4f9bc48a5aba4f4cb5b1f7644e 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,43 @@ worker.onmessage = function (ev) { worker.postMessage("Hello World!"); ``` +# Debugging +To be able to debug a child process, it must have a differnt debug port than the parent. +Tiny worker does this by adding a random port within a range to the parents debug port. +The default Range is `[1, 300]`, it can be changed with the `setRange(min, max)` method. +To disable any automatic port redirection set `options.noDebugRedirection = true`. + +### automatic redirection +```javascript +//parent is started with '--debug=1234' +var Worker = require("tiny-worker"); +Worker.setRange(2, 20); + +var worker = new Worker(function () { + postMessage(process.debugPort); +}); + +worker.onmessage = function (ev) { + console.log(ev.data); //prints any number between 1236 and 1254 + worker.terminate(); +} +``` + +### manual redirection +```javascript +//parent is started with '--debug=1234' +var Worker = require("tiny-worker"); + +var worker = new Worker(function () { + postMessage(process.debugPort); +}, [], {noDebugRedirection: true, execArgv: ["--debug=1235"]}); + +worker.onmessage = function (ev) { + console.log(ev.data); //prints 1235 + worker.terminate(); +} +``` + ## Properties #### onmessage Message handler, accepts an `Event` @@ -61,6 +98,10 @@ Broadcasts a message to the `Worker` #### terminate() Terminates the `Worker` +#### static setRange(min, max) +Sets range for debug ports, only affects current process. +Returns true if successful. + ## FAQ 1. I have an orphaned child process that lives on past the parent process' lifespan * Most likely a `SIGTERM` or `SIGINT` is not reaching the child process diff --git a/lib/index.js b/lib/index.js index 7a0c8615411e7d608b92ad7bf336fbb5910dbae7..1cce88cfef9784e4fab795569e0adae26de4e7ac 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,12 +9,13 @@ var path = require("path"), worker = path.join(__dirname, "worker.js"), events = /^(error|message)$/, defaultPorts = { inspect: 9229, debug: 5858 }; +var range = { min: 1, max: 300 }; var Worker = function () { function Worker(arg) { var _this = this; - var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; + var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { cwd: process.cwd() }; _classCallCheck(this, Worker); @@ -34,7 +35,7 @@ var Worker = function () { if (debugVars.length > 0 && !options.noDebugRedirection) { if (!options.execArgv) { //if no execArgs are given copy all arguments - debugVars = process.execArgv; + debugVars = Array.from(process.execArgv); options.execArgv = []; } @@ -58,7 +59,7 @@ var Worker = function () { if (match[2]) { port = parseInt(match[2]); } - debugVars[portIndex] = "--" + match[1] + "=" + (port + 1); //new parameter + 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 @@ -117,6 +118,17 @@ var Worker = function () { value: function terminate() { this.child.kill("SIGINT"); } + }], [{ + key: "setRange", + value: function setRange(min, max) { + if (min >= max) { + return false; + } + range.min = min; + range.max = max; + + return true; + } }]); return Worker; diff --git a/src/index.js b/src/index.js index cd9e6f39d446b59fa9c7479963e80d3f5c0b2ec1..a3ee836c25ff9eaa580686d3947c8365ad8a8453 100644 --- a/src/index.js +++ b/src/index.js @@ -3,9 +3,10 @@ const path = require("path"), 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 = undefined, options = {cwd: process.cwd()}) { + constructor (arg, args = [], options = {cwd: process.cwd()}) { let isfn = typeof arg === "function", input = isfn ? arg.toString() : arg; @@ -19,7 +20,7 @@ class Worker { }); if (debugVars.length > 0 && !options.noDebugRedirection) { if (!options.execArgv) { //if no execArgs are given copy all arguments - debugVars = process.execArgv; + debugVars = Array.from(process.execArgv); options.execArgv = []; } @@ -39,7 +40,7 @@ class Worker { if (match[2]) { port = parseInt(match[2]); } - debugVars[portIndex] = "--" + match[1] + "=" + (port + 1); //new parameter + 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]); @@ -81,6 +82,16 @@ class Worker { this.child.send({input: input, isfn: isfn, cwd: options.cwd}); } + 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;