{"name":"tiny-worker","version":"2.3.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-core":"^6.26.3","babel-preset-env":"^1.7.0","grunt":"^1.0.4","grunt-babel":"^7.0.0","grunt-cli":"^1.3.2","grunt-contrib-nodeunit":"^2.0.0","grunt-contrib-watch":"^1.1.0","grunt-eslint":"^22.0.0"},"dependencies":{"esm":"^3.2.25"},"gitHead":"1939cf6339daa23ddfd8cf45e98402af730d53b7","_id":"tiny-worker@2.3.0","_nodeVersion":"12.9.1","_npmVersion":"6.10.2","dist":{"integrity":"sha512-pJ70wq5EAqTAEl9IkGzA+fN0836rycEuz2Cn6yeZ6FRzlVS5IDOkFHpIoEsksPRQV34GDqXm65+OlnZqUSyK2g==","shasum":"715ae34304c757a9af573ae9a8e3967177e6011e","tarball":"http://registry-npm.ywwl.com/tiny-worker/download/tiny-worker-2.3.0.tgz","fileCount":6,"unpackedSize":12662,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdhQy3CRA9TVsSAnZWagAAvmgP/1fjElMP7aW30y3k0fBZ\nTFwjGsTOFFZeZx7bzLJKC3zhW1VDiNzAlTKZIjaMp3WyFNgcjodfUmhq2Wox\nWeaVth250w6aMynoWtV+wVbsLF3G2NDDbxh2CuzyYxyBy6ecca6TahM499By\nvXG480vx4sKSS5a2IGXMRU6T5lSLzwNTz1OFUNlMB/UoXOWFRlh4ABpZE9L+\nwh0K8wsaksrj6OOUXHMa7F26v9mseuRkOiuKDOnNUdyx2A7kZiw6oHrdZYPt\nws7xSfVsgQpxaxTGxKi6iKox/tQARo9dbKDRiT0GfOasczGYAk8PuDomHWF0\nmPeVt3Jt7nCcpkizxXobOJyzzBvGsUO2nHH9hKLWoBYX04lN4JC/j/4K9aYB\nV5BQsnq86lWRDVMxT/+A1GwXoG8+Ghujrj85TU7zBmBDBGMjB8ufoYh3SEB3\ndfdKa+EQdUcmWk15QJhQ1M3JUL1TZJ+T/LgtX+t+W8VKkB7npray579lI9+Q\nHOZlE0jTSrVUY9xbN/7h88SgmcGK7zGK95KZqobd+szjP/9sa0kQGebEXHmU\nGHvJOWsqASVPrWGn9Uc5jhDfECERCZ9EwWcugh00M7ii1E8oPXKBoDX0UZ1q\nuAlKLQz/m2p82veHtN4N5uHey3LJRppt4A08IXus3xQ8ZdfPdPMgMhKEJ3iM\nUhyC\r\n=rx0j\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDcjMx9VkxozRymy8D/BArCluqvghoWVCuRjeiHFK07JgIgW1PPE6lR/3jf/LiuichQf3v/cTzQLfXC+YEXAemwojU="}],"size":4833,"noattachment":false,"key":"/tiny-worker/-/tiny-worker-2.3.0.tgz"},"maintainers":[{"name":"avoidwork","email":"jason.mulligan@avoidwork.com"}],"_npmUser":{"name":"avoidwork","email":"jason.mulligan@avoidwork.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/tiny-worker_2.3.0_1569000630541_0.5485104119551711"},"_hasShrinkwrap":false,"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":1569000630678,"_cnpm_publish_time":1569000630678,"dist-tags":{"latest":"2.3.0"}}