{"name":"tiny-worker","version":"2.0.3","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-preset-es2015":"~6.22.0","grunt":"~1.0.1","grunt-babel":"~6.0.0","grunt-cli":"~1.2.0","grunt-contrib-nodeunit":"~1.0.0","grunt-contrib-watch":"~1.0.0","grunt-eslint":"~19.0.0"},"dependencies":{},"gitHead":"ef0b0f8fcf0619b5b9df62ac1a52034f537df2b5","_id":"tiny-worker@2.0.3","_shasum":"b8ea0ec003b413643311290265461a76735ed0ab","_from":"tiny-worker","_resolved":"file:tiny-worker","_npmVersion":"4.4.2","_nodeVersion":"7.7.2","_npmUser":{"name":"avoidwork","email":"jason.mulligan@avoidwork.com"},"dist":{"shasum":"b8ea0ec003b413643311290265461a76735ed0ab","tarball":"http://registry-npm.ywwl.com/tiny-worker/download/tiny-worker-2.0.3.tgz","integrity":"sha512-77sNPd4YIpsrB3E1+SpPp0L2oUlzx/S27dh8rc+Yji1wmi/MdlfxEU5eIpvMzb7JEzUtmpgIDWKidCfcHV4zNA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCJCcaW/5UqfGIRbs1Qq2l6LPAV1rLLWXXdr8jGZst7DQIhAJQVFXNE391SBQCWnjIkJyRQBypar7DlgL0/zKcsXSDf"}],"size":3561,"noattachment":false,"key":"/tiny-worker/-/tiny-worker-2.0.3.tgz"},"maintainers":[{"name":"avoidwork","email":"jason.mulligan@avoidwork.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/tiny-worker-2.0.3.tgz_1489368802785_0.8644417873583734"},"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":1489368804686,"_cnpm_publish_time":1489368804686,"dist-tags":{"latest":"2.3.0"}}