{"id":13725912,"url":"https://github.com/mbasso/wasm-worker","last_synced_at":"2025-04-04T22:02:28.010Z","repository":{"id":54811614,"uuid":"148500760","full_name":"mbasso/wasm-worker","owner":"mbasso","description":"Move a WebAssembly module into its own thread","archived":false,"fork":false,"pushed_at":"2024-08-28T00:01:39.000Z","size":27,"stargazers_count":265,"open_issues_count":3,"forks_count":14,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-28T21:01:44.664Z","etag":null,"topics":["thread","wasm","web-worker","webassembly","webworkers"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/mbasso.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","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}},"created_at":"2018-09-12T15:20:08.000Z","updated_at":"2025-03-25T09:50:14.000Z","dependencies_parsed_at":"2024-11-16T18:01:03.120Z","dependency_job_id":"38c8f64e-6317-43c7-8f89-f12ad40150e4","html_url":"https://github.com/mbasso/wasm-worker","commit_stats":{"total_commits":14,"total_committers":2,"mean_commits":7.0,"dds":0.0714285714285714,"last_synced_commit":"c22ccf17ee2643255848233b095b1b28c1d99ddd"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbasso%2Fwasm-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbasso%2Fwasm-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbasso%2Fwasm-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbasso%2Fwasm-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mbasso","download_url":"https://codeload.github.com/mbasso/wasm-worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256103,"owners_count":20909240,"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":["thread","wasm","web-worker","webassembly","webworkers"],"created_at":"2024-08-03T01:02:40.783Z","updated_at":"2025-04-04T22:02:27.991Z","avatar_url":"https://github.com/mbasso.png","language":"JavaScript","funding_links":["https://paypal.me/BassoMatteo"],"categories":["JavaScript"],"sub_categories":[],"readme":"# wasm-worker\n\n[![Build Status](https://travis-ci.org/mbasso/wasm-worker.svg?branch=master)](https://travis-ci.org/mbasso/wasm-worker)\n[![npm version](https://img.shields.io/npm/v/wasm-worker.svg)](https://www.npmjs.com/package/wasm-worker)\n[![npm downloads](https://img.shields.io/npm/dm/wasm-worker.svg?maxAge=2592000)](https://www.npmjs.com/package/wasm-worker)\n[![MIT](https://img.shields.io/npm/l/wasm-worker.svg)](https://github.com/mbasso/wasm-worker/blob/master/LICENSE.md)\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/BassoMatteo)\n\n\u003e Move a WebAssembly module into its own thread\n\n\n_wasm-worker only supports browser environments, since it uses [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). For use in a NodeJS environment, Web Workers must be polyfilled using a library like [node-webworker](https://github.com/pgriess/node-webworker)._\n\n## Installation\n\nYou can install wasm-worker using [npm](https://www.npmjs.com/package/wasm-worker):\n\n```bash\nnpm install --save wasm-worker\n```\n\nIf you aren't using npm in your project, you can include wasmWorker using UMD build in the dist folder with `\u003cscript\u003e` tag.\n\n## Usage\n\nOnce you have installed wasm-worker, supposing a CommonJS environment, you can import and use it in this way:\n\n```js\nimport wasmWorker from 'wasm-worker';\n\n// supposing an \"add.wasm\" module that exports a single function \"add\"\nwasmWorker('add.wasm')\n  .then(module =\u003e {\n    return module.exports.add(1, 2);\n  })\n  .then(sum =\u003e {\n    console.log('1 + 2 = ' + sum);\n  })\n  .catch(ex =\u003e {\n    // ex is a string that represents the exception\n    console.error(ex);\n  });\n\n// you can also run js functions inside the worker\n// to access importObject for example\nwasmWorker('add.wasm')\n  .then(module =\u003e {\n    return module.run(({\n      // module,\n      // importObject,\n      instance,\n      params\n    }) =\u003e {\n      // here is sync\n      const sum = instance.exports.add(...params);\n      return '1 + 2 = ' + sum;\n    }, [1, 2]);\n  })\n  .then(result =\u003e {\n    console.log(result);\n  });\n```\n\n## API\n\n```js\ntype JsCallback = (context: {\n  module: WebAssembly.Module,\n  instance: WebAssembly.Instance,\n  importObject: importObject,\n  params: any,\n}) =\u003e any;\n\ntype WasmWorkerModule = {\n  exports: {\n    [export: string]: (...any: Array\u003cany\u003e) =\u003e Promise\u003cany\u003e\n  },\n  // run a js function inside the worker and provides it the given params\n  // ⚠️ Caveat: the function you pass cannot rely on its surrounding scope, since it is executed in an isolated context.\n  // Please use the \"params\" parameter to provide some values to the callback\n  run: (callback: JsCallback, params?: any) =\u003e Promise\u003cany\u003e\n};\n\ntype Options = {\n  // the first 3 properties are used to create the Web Worker\n  // https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker#Parameters\n  name: string,\n  type: 'classic' | 'module',\n  credentials: 'omit' | 'same-origin' | 'include',\n  \n  // the getImportObject function is used to get the options to instantiate the WebAssembly Module\n  // ⚠️ Caveat: the function you pass cannot rely on its surrounding scope, since it is executed in an isolated context.\n  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate#Primary_overload_%E2%80%94_taking_wasm_binary_code\n  getImportObject: () =\u003e importObject,\n};\n\nwasmWorker(url: string, options?: Options): Promise\u003cWasmWorkerModule\u003e // browser only\nwasmWorker(bufferSource: TypedArray | ArrayBuffer, options?: Options): Promise\u003cWasmWorkerModule\u003e\n```\n\n## Browser support\n\n`wasm-worker` uses [fetch](https://developer.mozilla.org/it/docs/Web/API/Fetch_API), [Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) and obviously [WebAssembly](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly) APIs, they are broadly supported by major browser engines but you would like to polyfill them to support old versions.\n\n```js\nif (!window.fetch || !window.Worker || !window.WebAssembly) {\n    ...\n} else {\n    ...\n}\n```\n\n### CSP\n\nIf your app has a [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy),\nwasm-worker require `worker-src data:` and `script-src data:` in your config.\n\n## Inspiration\n\nThis project is inspired by [greenlet](https://github.com/developit/greenlet).\n\n## Change Log\n\nThis project adheres to [Semantic Versioning](http://semver.org/).  \nEvery release, along with the migration instructions, is documented on the Github [Releases](https://github.com/mbasso/wasm-worker/releases) page.\n\n## Authors\n**Matteo Basso**\n- [github/mbasso](https://github.com/mbasso)\n- [@teo_basso](https://twitter.com/teo_basso)\n\n## Copyright and License\nCopyright (c) 2018, Matteo Basso.\n\nwasm-worker source code is licensed under the [MIT License](https://github.com/mbasso/wasm-worker/blob/master/LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbasso%2Fwasm-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmbasso%2Fwasm-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbasso%2Fwasm-worker/lists"}