{"name":"tiny-worker","version":"0.1.0","description":"Tiny WebWorker for Server","main":"lib/index.js","scripts":{"test":"grunt test"},"repository":{"type":"git","url":"git+https://github.com/avoidwork/tiny-worker.git"},"keywords":["web","worker","ps","webworker"],"author":{"name":"Jason Mulligan","email":"jason.mulligan@avoidwork.com"},"license":"BSD-3-Clause","bugs":{"url":"https://github.com/avoidwork/tiny-worker/issues"},"homepage":"https://github.com/avoidwork/tiny-worker","devDependencies":{"babel-eslint":"^4.1.0","grunt":"^0.4.5","grunt-babel":"^5.0.0","grunt-cli":"^0.1.13","grunt-contrib-watch":"^0.6.1","grunt-eslint":"^17.1.0"},"dependencies":{"tiny-defer":"^1.0.2"},"gitHead":"4b4309889b13032092ddd81f4f9222b1d75cec1b","_id":"tiny-worker@0.1.0","_shasum":"fcf80c9e57449dd6721041740d4f79f1a76ae85f","_from":"tiny-worker","_resolved":"file:tiny-worker","_npmVersion":"2.14.4","_nodeVersion":"4.1.1","_npmUser":{"name":"avoidwork","email":"jason.mulligan@avoidwork.com"},"maintainers":[{"name":"avoidwork","email":"jason.mulligan@avoidwork.com"}],"dist":{"shasum":"fcf80c9e57449dd6721041740d4f79f1a76ae85f","tarball":"http://registry-npm.ywwl.com/tiny-worker/download/tiny-worker-0.1.0.tgz","integrity":"sha512-v2oMcUJ03H3KKADjnxfEmrShAKyKkdOJ5hvS+RwiEuTlXyfDHbCycAcrnPv3cOze9w5J0+MEPU1xaiTCqc5i4A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGS+ot3hTvftEKFjQD/CvO5wuYGwfFyjaxmdi6RjrximAiBhLbQ2/B/80diwgJfU6x8x1dl9JVTZDh4Vp3XN+k+wNQ=="}],"size":1910,"noattachment":false,"key":"/tiny-worker/-/tiny-worker-0.1.0.tgz"},"directories":{},"readme":"# tiny-worker\r\nTiny WebWorker for Server\r\n\r\n`require()` is available for flexible inline Worker scripts. Optional parameters `args` Array & `options` Object; see `child_process.fork()` documentation.\r\n\r\n[![build status](https://secure.travis-ci.org/avoidwork/tiny-worker.svg)](http://travis-ci.org/avoidwork/tiny-worker)\r\n\r\n## Example\r\n#### Creating a Worker from a file\r\nThe worker script:\r\n```javascript\r\nonmessage = function (ev) {\r\n\tpostMessage(ev.data);\r\n};\r\n```\r\n\r\nThe core script:\r\n```javascript\r\nvar Worker = require(\"tiny-worker\");\r\nvar worker = new Worker(\"repeat.js\");\r\n\r\nworker.onmessage = function (ev) {\r\n\tconsole.log(ev.data);\r\n\tworker.terminate();\r\n};\r\n\r\nworker.postMessage(\"Hello World!\");\r\n```\r\n\r\n#### Enable ES6 import/export within Worker file\r\nThe worker helper script (helper.js):\r\n```javascript\r\nexport const dataFormatter = (data) => {\r\n\treturn `${data} World!`;\r\n};\r\n```\r\n\r\nThe worker script (repeat.js):\r\n```javascript\r\nimport { dataFormatter } from \"./helper\";\r\n\r\nonmessage = function (ev) {\r\n\tconst data = dataFormatter(ev.data);\r\n\tpostMessage(data);\r\n};\r\n```\r\n\r\nThe core script:\r\n```javascript\r\nvar Worker = require(\"tiny-worker\");\r\nvar worker = new Worker(\"repeat.js\", [], {esm: true});\r\n\r\nworker.onmessage = function (ev) {\r\n\tconsole.log(ev.data);\r\n\tworker.terminate();\r\n};\r\n\r\nworker.postMessage(\"Hello\");\r\n```\r\n\r\n#### Creating a Worker from a Function\r\n```javascript\r\nvar Worker = require(\"tiny-worker\");\r\nvar worker = new Worker(function () {\r\n\tself.onmessage = function (ev) {\r\n\t\tpostMessage(ev.data);\r\n\t};\r\n});\r\n\r\nworker.onmessage = function (ev) {\r\n\tconsole.log(ev.data);\r\n\tworker.terminate();\r\n};\r\n\r\nworker.postMessage(\"Hello World!\");\r\n```\r\n\r\n# Debugging\r\nTo be able to debug a child process, it must have a differnt debug port than the parent. \r\nTiny worker does this by adding a random port within a range to the parents debug port.\r\nThe default Range is `[1, 300]`, it can be changed with the `setRange(min, max)` method.\r\nTo disable any automatic port redirection set `options.noDebugRedirection = true`.\r\n\r\n### automatic redirection\r\n```javascript\r\n//parent is started with '--debug=1234'\r\nvar Worker = require(\"tiny-worker\");\r\nWorker.setRange(2, 20);\r\n\r\nvar worker = new Worker(function () {\r\n\tpostMessage(process.debugPort); \r\n});\r\n\r\nworker.onmessage = function (ev) {\r\n\tconsole.log(ev.data); //prints any number between 1236 and 1254\r\n\tworker.terminate();\r\n}\r\n```\r\n\r\n### manual redirection\r\n```javascript\r\n//parent is started with '--debug=1234'\r\nvar Worker = require(\"tiny-worker\");\r\n\r\nvar worker = new Worker(function () {\r\n\tpostMessage(process.debugPort); \r\n}, [], {noDebugRedirection: true, execArgv: [\"--debug=1235\"]});\r\n\r\nworker.onmessage = function (ev) {\r\n\tconsole.log(ev.data); //prints 1235\r\n\tworker.terminate();\r\n}\r\n```\r\n\r\n## Properties\r\n#### onmessage\r\nMessage handler, accepts an `Event`\r\n\r\n#### onerror\r\nError handler, accepts an `Event`\r\n\r\n## API\r\n#### addEventListener(event, fn)\r\nAdds an event listener\r\n\r\n#### postMessage()\r\nBroadcasts a message to the `Worker`\r\n\r\n#### terminate()\r\nTerminates the `Worker`\r\n\r\n#### static setRange(min, max)\r\nSets range for debug ports, only affects current process.\r\nReturns true if successful.\r\n\r\n## FAQ\r\n1. I have an orphaned child process that lives on past the parent process' lifespan\r\n  * Most likely a `SIGTERM` or `SIGINT` is not reaching the child process\r\n2. How do I insure all process are terminated?\r\n  * In your core script register a listener for `SIGTERM` or `SIGINT` via `process.on()` which terminates (all) worker process(es) and then gracefully shutdowns via `process.exit(0);`\r\n3. Why `SIGTERM` or `SIGINT`?\r\n  * Unix/BSD will work with `SIGTERM`, but if you also need to support Windows use `SIGINT`\r\n\r\n## License\r\nCopyright (c) 2019 Jason Mulligan\r\nLicensed under the BSD-3 license\r\n","publish_time":1443565695636,"_cnpm_publish_time":1443565695636,"dist-tags":{"latest":"2.3.0"}}