{"name":"@emnapi/wasi-threads","version":"1.2.0","author":{"name":"toyobayashi"},"license":"MIT","_id":"@emnapi/wasi-threads@1.2.0","maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"homepage":"https://github.com/toyobayashi/emnapi#readme","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"dist":{"shasum":"a19d9772cc3d195370bf6e2a805eec40aa75e18e","tarball":"http://registry-npm.ywwl.com/@emnapi/wasi-threads/download/@emnapi/wasi-threads-1.2.0.tgz","fileCount":15,"integrity":"sha512-N10dEJNSsUx41Z6pZsXU8FjPjpBEplgH24sfkmITrBED1/U2Esum9F3lfLrMjKHHjmi557zQn7kR9R+XWXu5Rg==","signatures":[{"sig":"MEUCIHESOiVtyLKZmLBII31l6KkyxhFxI20yggbgzumTPtfQAiEAmUqL4vT8PyXuOdRAhVLvwl1jnHLNljsgy2p572Rm8kw=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@emnapi%2fwasi-threads@1.2.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":227604,"size":39320,"noattachment":false,"key":"/@emnapi/wasi-threads/-/@emnapi/wasi-threads-1.2.0.tgz"},"main":"index.js","types":"./dist/wasi-threads.d.ts","module":"./dist/wasi-threads.esm-bundler.js","exports":{".":{"types":{"import":"./dist/wasi-threads.d.mts","module":"./dist/wasi-threads.d.ts","default":"./dist/wasi-threads.d.ts"},"import":"./dist/wasi-threads.mjs","module":"./dist/wasi-threads.esm-bundler.js","default":"./index.js"},"./dist/wasi-threads.cjs.min":{"types":"./dist/wasi-threads.d.ts","default":"./dist/wasi-threads.cjs.min.js"},"./dist/wasi-threads.min.mjs":{"types":"./dist/wasi-threads.d.mts","default":"./dist/wasi-threads.min.mjs"}},"gitHead":"c9743cf573e52c5b4d31d7369c752b652b29fe9e","scripts":{"test":"node ./test/index.js","build":"node ./script/build.js","build:test":"node ./test/build.js"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:05470976-075f-4b7b-8d48-b3daf030dc03"}},"repository":{"url":"git+https://github.com/toyobayashi/emnapi.git","type":"git"},"_npmVersion":"11.6.2","description":"WASI threads proposal implementation in JavaScript","directories":{},"sideEffects":false,"_nodeVersion":"24.11.1","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/wasi-threads_1.2.0_1773305275085_0.6219428515424414","host":"s3://npm-registry-packages-npm-production"},"readme":"# @emnapi/wasi-threads\n\nThis package makes [wasi-threads proposal](https://github.com/WebAssembly/wasi-threads) based WASI modules work in Node.js and browser.\n\n## Quick Start\n\n`index.html`\n\n```html\n<script src=\"./node_modules/@tybys/wasm-util/dist/wasm-util.js\"></script>\n<script src=\"./node_modules/@emnapi/wasi-threads/dist/wasi-threads.js\"></script>\n<script src=\"./index.js\"></script>\n```\n\nIf your application will block browser main thread (for example `pthread_join`), please run it in worker instead.\n\n```html\n<script>\n  // pthread_join (Atomics.wait) cannot be called in browser main thread\n  new Worker('./index.js')\n</script>\n```\n\n`index.js`\n\n```js\nconst ENVIRONMENT_IS_NODE =\n  typeof process === 'object' && process !== null &&\n  typeof process.versions === 'object' && process.versions !== null &&\n  typeof process.versions.node === 'string';\n\n(function (main) {\n  if (ENVIRONMENT_IS_NODE) {\n    main(require)\n  } else {\n    if (typeof importScripts === 'function') {\n      importScripts('./node_modules/@tybys/wasm-util/dist/wasm-util.js')\n      importScripts('./node_modules/@emnapi/wasi-threads/dist/wasi-threads.js')\n    }\n    const nodeWasi = { WASI: globalThis.wasmUtil.WASI }\n    const nodeWorkerThreads = {\n      Worker: globalThis.Worker\n    }\n    const _require = function (request) {\n      if (request === 'node:wasi' || request === 'wasi') return nodeWasi\n      if (request === 'node:worker_threads' || request === 'worker_threads') return nodeWorkerThreads\n      if (request === '@emnapi/wasi-threads') return globalThis.wasiThreads\n      throw new Error('Can not find module: ' + request)\n    }\n    main(_require)\n  }\n})(async function (require) {\n  const { WASI } = require('wasi')\n  const { Worker } = require('worker_threads')\n  const { WASIThreads } = require('@emnapi/wasi-threads')\n\n  const wasi = new WASI({\n    version: 'preview1'\n  })\n  const wasiThreads = new WASIThreads({\n    wasi,\n\n    /**\n     * avoid Atomics.wait() deadlock during thread creation in browser\n     * see https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size\n     */\n    reuseWorker: ENVIRONMENT_IS_NODE\n      ? false\n      : {\n          size: 4 /** greater than actual needs (2) */,\n          strict: true\n        },\n\n    /**\n     * Synchronous thread creation\n     * pthread_create will not return until thread worker actually starts\n     */\n    waitThreadStart: typeof window === 'undefined' ? 1000 : false,\n\n    onCreateWorker: () => {\n      return new Worker('./worker.js', {\n        execArgv: ['--experimental-wasi-unstable-preview1']\n      })\n    }\n  })\n  const memory = new WebAssembly.Memory({\n    initial: 16777216 / 65536,\n    maximum: 2147483648 / 65536,\n    shared: true\n  })\n  let input\n  const file = 'path/to/your/wasi-module.wasm'\n  try {\n    input = require('fs').readFileSync(require('path').join(__dirname, file))\n  } catch (err) {\n    const response = await fetch(file)\n    input = await response.arrayBuffer()\n  }\n  let { module, instance } = await WebAssembly.instantiate(input, {\n    env: { memory },\n    wasi_snapshot_preview1: wasi.wasiImport,\n    ...wasiThreads.getImportObject()\n  })\n\n  wasiThreads.setup(instance, module, memory)\n  await wasiThreads.preloadWorkers()\n\n  if (typeof instance.exports._start === 'function') {\n    return wasi.start(instance)\n  } else {\n    wasi.initialize(instance)\n    // instance.exports.exported_wasm_function()\n  }\n})\n```\n\n`worker.js`\n\n```js\n(function (main) {\n  const ENVIRONMENT_IS_NODE =\n    typeof process === 'object' && process !== null &&\n    typeof process.versions === 'object' && process.versions !== null &&\n    typeof process.versions.node === 'string'\n\n  if (ENVIRONMENT_IS_NODE) {\n    const _require = function (request) {\n      return require(request)\n    }\n\n    const _init = function () {\n      const nodeWorkerThreads = require('worker_threads')\n      const parentPort = nodeWorkerThreads.parentPort\n\n      parentPort.on('message', (data) => {\n        globalThis.onmessage({ data })\n      })\n\n      Object.assign(globalThis, {\n        self: globalThis,\n        require,\n        Worker: nodeWorkerThreads.Worker,\n        importScripts: function (f) {\n          (0, eval)(require('fs').readFileSync(f, 'utf8') + '//# sourceURL=' + f)\n        },\n        postMessage: function (msg) {\n          parentPort.postMessage(msg)\n        }\n      })\n    }\n\n    main(_require, _init)\n  } else {\n    importScripts('./node_modules/@tybys/wasm-util/dist/wasm-util.js')\n    importScripts('./node_modules/@emnapi/wasi-threads/dist/wasi-threads.js')\n\n    const nodeWasi = { WASI: globalThis.wasmUtil.WASI }\n    const _require = function (request) {\n      if (request === '@emnapi/wasi-threads') return globalThis.wasiThreads\n      if (request === 'node:wasi' || request === 'wasi') return nodeWasi\n      throw new Error('Can not find module: ' + request)\n    }\n    const _init = function () {}\n    main(_require, _init)\n  }\n})(function main (require, init) {\n  init()\n\n  const { WASI } = require('wasi')\n  const { ThreadMessageHandler, WASIThreads } = require('@emnapi/wasi-threads')\n\n  const handler = new ThreadMessageHandler({\n    async onLoad ({ wasmModule, wasmMemory }) {\n      const wasi = new WASI({\n        version: 'preview1'\n      })\n\n      const wasiThreads = new WASIThreads({\n        wasi,\n        childThread: true\n      })\n\n      const originalInstance = await WebAssembly.instantiate(wasmModule, {\n        env: {\n          memory: wasmMemory,\n        },\n        wasi_snapshot_preview1: wasi.wasiImport,\n        ...wasiThreads.getImportObject()\n      })\n\n      // must call `initialize` instead of `start` in child thread\n      const instance = wasiThreads.initialize(originalInstance, wasmModule, wasmMemory)\n\n      return { module: wasmModule, instance }\n    }\n  })\n\n  globalThis.onmessage = function (e) {\n    handler.handle(e)\n    // handle other messages\n  }\n})\n```\n","publish_time":1773305275227,"_cnpm_publish_time":1773305275227,"dist-tags":{"latest":"2.2.0","legacy":"1.2.3"}}