{"id":13838838,"url":"https://github.com/avoidwork/tiny-worker","last_synced_at":"2025-05-16T18:06:45.403Z","repository":{"id":53956742,"uuid":"43386607","full_name":"avoidwork/tiny-worker","owner":"avoidwork","description":"Tiny WebWorker for the Server","archived":false,"fork":false,"pushed_at":"2022-10-22T21:17:35.000Z","size":208,"stargazers_count":249,"open_issues_count":0,"forks_count":23,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-16T18:06:41.923Z","etag":null,"topics":["node","nodejs","worker-script","workers"],"latest_commit_sha":null,"homepage":"http://avoidwork.github.io/tiny-worker","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/avoidwork.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["avoidwork"]}},"created_at":"2015-09-29T18:35:00.000Z","updated_at":"2025-03-23T03:06:29.000Z","dependencies_parsed_at":"2022-08-13T05:20:21.169Z","dependency_job_id":null,"html_url":"https://github.com/avoidwork/tiny-worker","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avoidwork%2Ftiny-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avoidwork%2Ftiny-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avoidwork%2Ftiny-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avoidwork%2Ftiny-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/avoidwork","download_url":"https://codeload.github.com/avoidwork/tiny-worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254582905,"owners_count":22095518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node","nodejs","worker-script","workers"],"created_at":"2024-08-04T16:00:35.095Z","updated_at":"2025-05-16T18:06:45.383Z","avatar_url":"https://github.com/avoidwork.png","language":"JavaScript","funding_links":["https://github.com/sponsors/avoidwork"],"categories":["JavaScript"],"sub_categories":[],"readme":"# tiny-worker\nTiny WebWorker for Server\n\n`require()` is available for flexible inline Worker scripts. Optional parameters `args` Array \u0026 `options` Object; see `child_process.fork()` documentation.\n\n[![build status](https://secure.travis-ci.org/avoidwork/tiny-worker.svg)](http://travis-ci.org/avoidwork/tiny-worker)\n\n## Example\n#### Creating a Worker from a file\nThe worker script:\n```javascript\nonmessage = function (ev) {\n\tpostMessage(ev.data);\n};\n```\n\nThe core script:\n```javascript\nvar Worker = require(\"tiny-worker\");\nvar worker = new Worker(\"repeat.js\");\n\nworker.onmessage = function (ev) {\n\tconsole.log(ev.data);\n\tworker.terminate();\n};\n\nworker.postMessage(\"Hello World!\");\n```\n\n#### Enable ES6 import/export within Worker file\nThe worker helper script (helper.js):\n```javascript\nexport const dataFormatter = (data) =\u003e {\n\treturn `${data} World!`;\n};\n```\n\nThe worker script (repeat.js):\n```javascript\nimport { dataFormatter } from \"./helper\";\n\nonmessage = function (ev) {\n\tconst data = dataFormatter(ev.data);\n\tpostMessage(data);\n};\n```\n\nThe core script:\n```javascript\nvar Worker = require(\"tiny-worker\");\nvar worker = new Worker(\"repeat.js\", [], {esm: true});\n\nworker.onmessage = function (ev) {\n\tconsole.log(ev.data);\n\tworker.terminate();\n};\n\nworker.postMessage(\"Hello\");\n```\n\n#### Creating a Worker from a Function\n```javascript\nvar Worker = require(\"tiny-worker\");\nvar worker = new Worker(function () {\n\tself.onmessage = function (ev) {\n\t\tpostMessage(ev.data);\n\t};\n});\n\nworker.onmessage = function (ev) {\n\tconsole.log(ev.data);\n\tworker.terminate();\n};\n\nworker.postMessage(\"Hello World!\");\n```\n\n# Debugging\nTo be able to debug a child process, it must have a differnt debug port than the parent. \nTiny worker does this by adding a random port within a range to the parents debug port.\nThe default Range is `[1, 300]`, it can be changed with the `setRange(min, max)` method.\nTo disable any automatic port redirection set `options.noDebugRedirection = true`.\n\n### automatic redirection\n```javascript\n//parent is started with '--debug=1234'\nvar Worker = require(\"tiny-worker\");\nWorker.setRange(2, 20);\n\nvar worker = new Worker(function () {\n\tpostMessage(process.debugPort); \n});\n\nworker.onmessage = function (ev) {\n\tconsole.log(ev.data); //prints any number between 1236 and 1254\n\tworker.terminate();\n}\n```\n\n### manual redirection\n```javascript\n//parent is started with '--debug=1234'\nvar Worker = require(\"tiny-worker\");\n\nvar worker = new Worker(function () {\n\tpostMessage(process.debugPort); \n}, [], {noDebugRedirection: true, execArgv: [\"--debug=1235\"]});\n\nworker.onmessage = function (ev) {\n\tconsole.log(ev.data); //prints 1235\n\tworker.terminate();\n}\n```\n\n## Properties\n#### onmessage\nMessage handler, accepts an `Event`\n\n#### onerror\nError handler, accepts an `Event`\n\n## API\n#### addEventListener(event, fn)\nAdds an event listener\n\n#### postMessage()\nBroadcasts a message to the `Worker`\n\n#### terminate()\nTerminates the `Worker`\n\n#### static setRange(min, max)\nSets range for debug ports, only affects current process.\nReturns true if successful.\n\n## FAQ\n1. I have an orphaned child process that lives on past the parent process' lifespan\n  * Most likely a `SIGTERM` or `SIGINT` is not reaching the child process\n2. How do I insure all process are terminated?\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);`\n3. Why `SIGTERM` or `SIGINT`?\n  * Unix/BSD will work with `SIGTERM`, but if you also need to support Windows use `SIGINT`\n\n## License\nCopyright (c) 2019 Jason Mulligan\nLicensed under the BSD-3 license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favoidwork%2Ftiny-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Favoidwork%2Ftiny-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favoidwork%2Ftiny-worker/lists"}