{"id":27070291,"url":"https://github.com/paulmillr/micro-wrkr","last_synced_at":"2025-04-10T22:48:41.755Z","repository":{"id":286265021,"uuid":"960409125","full_name":"paulmillr/micro-wrkr","owner":"paulmillr","description":"Wrappers for built-in Web Workers enabling easy parallel data processing","archived":false,"fork":false,"pushed_at":"2025-04-05T10:34:03.000Z","size":12,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T22:48:35.773Z","etag":null,"topics":["bun","cluster","concurrent","deno","nodejs","parallel","web-worker","worker"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulmillr.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"paulmillr"}},"created_at":"2025-04-04T11:39:02.000Z","updated_at":"2025-04-06T13:19:59.000Z","dependencies_parsed_at":"2025-04-09T17:46:07.507Z","dependency_job_id":null,"html_url":"https://github.com/paulmillr/micro-wrkr","commit_stats":null,"previous_names":["paulmillr/micro-wrkr"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-wrkr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-wrkr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-wrkr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-wrkr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulmillr","download_url":"https://codeload.github.com/paulmillr/micro-wrkr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312197,"owners_count":21082637,"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":["bun","cluster","concurrent","deno","nodejs","parallel","web-worker","worker"],"created_at":"2025-04-05T22:20:20.229Z","updated_at":"2025-04-10T22:48:41.744Z","avatar_url":"https://github.com/paulmillr.png","language":"TypeScript","funding_links":["https://github.com/sponsors/paulmillr"],"categories":[],"sub_categories":[],"readme":"# micro-wrkr\n\nWrappers for built-in Web Workers enabling easy parallel data processing.\n\n- 🔒 CSP-friendly: no evals, static file name\n- 🔍 Tested in browsers, node, deno, bun\n- 📦 Can be bundled using esbuild, rollup, webpack, parcel\n- 🏭 High-level type-safe helpers for batch processing\n- ⛓ Sync: much simpler than async, no queues / locks\n\n## Why\n\nBrowser Web Workers work fine, but have terrible APIs (just like most \"web APIs\").\nNode.js doesn't have workers, while polyfilling them using node APIs breaks bundlers.\n\nHow could one pass a code to a worker?\n\n- eval: stringify function, then `eval`. Would break CSP and imports\n- wasm: much easier, just send binary blob of code. Would not work in envs without wasm\n- re-run module with if-workercode-else-maincode: fragile, need to track everything done before workers are initialized (IO such as HTTP, DOM)\n- build static file before publishing: works if wrkr is directly used, but not inside of other library\n\nCheck out [webpack docs on webworkers](https://webpack.js.org/guides/web-workers/).\n\nThe library could also be used in single-threaded manner: provide `threads` option to `initBatch`.\nThen slow functions can be ran outside of main thread, with async API.\n\n## Usage\n\n\u003e `npm install micro-wrkr`\n\n\u003e `deno add jsr:@paulmillr/micro-wrkr`\n\n\u003e `deno doc jsr:@paulmillr/micro-wrkr` # command-line documentation\n\n### Main file `main.js`\n\n```ts\nimport { bn254 } from '@noble/curves/bn254';\nimport { type ProjConstructor, type ProjPointType } from '@noble/curves/abstract/weierstrass';\nimport wrkr from 'micro-wrkr';\nimport { type Handlers } from './msm-worker.js';\n\nfunction reducePoint\u003cT\u003e(p: ProjConstructor\u003cT\u003e) {\n  return (lst: ProjPointType\u003cT\u003e[]) =\u003e\n    lst.map((i) =\u003e new p(i.px, i.py, i.pz)).reduce((acc, i) =\u003e acc.add(i), p.ZERO);\n}\n\nexport function initMSM() {\n  // Type-safe\n  // worker should be in same directory as main thread code\n  const { methods, terminate } = wrkr.initBatch\u003cHandlers\u003e(\n    () =\u003e new Worker(new URL('./msm-worker.js', import.meta.url), { type: 'module' }),\n    {\n      // optional reducers\n      bn254_msmG1: reducePoint(bn254.G1.ProjectivePoint),\n      bn254_msmG2: reducePoint(bn254.G2.ProjectivePoint),\n    }\n  );\n  // Use `terminate` to stop workers when app is paused or exported from library.\n  // Otherwise, it won't terminate.\n  return { methods, terminate };\n}\n```\n\n### Worker file `msm-worker.js`\n\n```ts\nimport { bn254 } from '@noble/curves/bn254';\nimport wrkr from 'micro-wrkr';\nimport { type ProjConstructor, type ProjPointType } from '@noble/curves/abstract/weierstrass';\n\ntype MSMInput\u003cT\u003e = { point: ProjPointType\u003cT\u003e; scalar: T };\n\nfunction buildMSM\u003cT\u003e(point: ProjConstructor\u003cT\u003e) {\n  return (lst: MSMInput\u003cT\u003e[]): ProjPointType\u003cT\u003e =\u003e {\n    if (!lst.length) return point.ZERO;\n    const points = lst.map((i: any) =\u003e new point(i.point.px, i.point.py, i.point.pz));\n    const scalars = lst.map((i: any) =\u003e i.scalar);\n    return point.msm(points, scalars);\n  };\n}\n\nconst handlers = {\n  bn254_msmG1: buildMSM(bn254.G1.ProjectivePoint),\n  bn254_msmG2: buildMSM(bn254.G2.ProjectivePoint),\n};\n// Export Handlers type for type-safety\nexport type Handlers = typeof handlers;\nwrkr.initWorker(handlers);\n```\n\n## Testing\n\n- Browserify isn't supported\n- Webpack sometimes breaks CSP by encoding workers as data:url\n    - Example: `new Worker(new URL(e.p+e.u(44),e.b),{type:void 0})`\n\n\n```sh\n# when no google chrome, thorium can also be used\nexport CHROME_BIN='/Applications/Thorium.app/Contents/MacOS/Thorium'\nnpm run build \u0026\u0026 npm run test:full\n```\n\n## License\n\nMIT (c) Paul Miller [(https://paulmillr.com)](https://paulmillr.com), see LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmillr%2Fmicro-wrkr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulmillr%2Fmicro-wrkr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmillr%2Fmicro-wrkr/lists"}