{"name":"@emnapi/wasi-threads","versions":{"1.0.0":{"name":"@emnapi/wasi-threads","version":"1.0.0","author":{"name":"toyobayashi"},"license":"MIT","_id":"@emnapi/wasi-threads@1.0.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":"595309a3ccff2870635ee92e24d3013eb9ff34e6","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.0.0.tgz","fileCount":15,"integrity":"sha512-ess84fIThSLHq0dw4zBzffMdmL+RyuEwUS1tDP4SWsOZ5svpLOgCobodDiIQc6X/H8kYcYxFqK4sxRKfgZ6HlA==","signatures":[{"sig":"MEQCIFWLabYlno4lOig0PYN4v8rPWrBFA0pXWdAlMrpHqDT6AiAIvGXhRyUTCDWIooQEqGn4+sh1RrDkseLngFINVZqIMA==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":213855},"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":"696f3e8b74a6974b3ccbc1b8c6b78830a99b780f","scripts":{"test":"node ./test/index.js","build":"node ./script/build.js","build:test":"node ./test/build.js"},"_npmUser":{"name":"toyobayashi","email":"lifenglin314@outlook.com"},"repository":{"url":"git+https://github.com/toyobayashi/emnapi.git","type":"git"},"_npmVersion":"10.1.0","description":"WASI threads proposal implementation in JavaScript","directories":{},"sideEffects":false,"_nodeVersion":"20.9.0","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/wasi-threads_1.0.0_1716100160135_0.31306629634876315","host":"s3://npm-registry-packages"},"contributors":[]},"1.0.1":{"name":"@emnapi/wasi-threads","version":"1.0.1","author":{"name":"toyobayashi"},"license":"MIT","_id":"@emnapi/wasi-threads@1.0.1","maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"homepage":"https://github.com/toyobayashi/emnapi#readme","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"dist":{"shasum":"d7ae71fd2166b1c916c6cd2d0df2ef565a2e1a5b","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.0.1.tgz","fileCount":15,"integrity":"sha512-iIBu7mwkq4UQGeMEM8bLwNK962nXdhodeScX4slfQnRhEMMzvYivHhutCIk8uojvmASXXPC2WNEjwxFWk72Oqw==","signatures":[{"sig":"MEQCICafHu3ZoE02hI303Zc5qi+35hOc2/q0tLfW5OyI1oe9AiAMSs+gklXF4rkXnwJ0IBi6y9SeOQj24sFCHiZyH4uMvw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":219488},"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":"f1fe5df18be4f35968381bc9be5df71942fe7165","scripts":{"test":"node ./test/index.js","build":"node ./script/build.js","build:test":"node ./test/build.js"},"_npmUser":{"name":"toyobayashi","email":"lifenglin314@outlook.com"},"repository":{"url":"git+https://github.com/toyobayashi/emnapi.git","type":"git"},"_npmVersion":"10.1.0","description":"WASI threads proposal implementation in JavaScript","directories":{},"sideEffects":false,"_nodeVersion":"20.9.0","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/wasi-threads_1.0.1_1716106667079_0.9628386566243541","host":"s3://npm-registry-packages"},"contributors":[]},"1.0.2":{"name":"@emnapi/wasi-threads","version":"1.0.2","author":{"name":"toyobayashi"},"license":"MIT","_id":"@emnapi/wasi-threads@1.0.2","maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"homepage":"https://github.com/toyobayashi/emnapi#readme","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"dist":{"shasum":"977f44f844eac7d6c138a415a123818c655f874c","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.0.2.tgz","fileCount":15,"integrity":"sha512-5n3nTJblwRi8LlXkJ9eBzu+kZR8Yxcc7ubakyQTFzPMtIhFpUBRbsnc2Dv88IZDIbCDlBiWrknhB4Lsz7mg6BA==","signatures":[{"sig":"MEYCIQDeIkHWKQTthiztxbLpxEka26FPNT8txcK9NmXs08VoUQIhAPlC7DXKLfugnl/1WCNzHwEbPsjogeZZ6q/h2CkqtwGU","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":219364},"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":"90c4c1b1358851d401dd162c5d1925a2dcb2e397","scripts":{"test":"node ./test/index.js","build":"node ./script/build.js","build:test":"node ./test/build.js"},"_npmUser":{"name":"toyobayashi","email":"lifenglin314@outlook.com"},"repository":{"url":"git+https://github.com/toyobayashi/emnapi.git","type":"git"},"_npmVersion":"10.1.0","description":"WASI threads proposal implementation in JavaScript","directories":{},"sideEffects":false,"_nodeVersion":"20.9.0","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/wasi-threads_1.0.2_1744733223477_0.9346622564921898","host":"s3://npm-registry-packages-npm-production"},"contributors":[]},"1.0.3":{"name":"@emnapi/wasi-threads","version":"1.0.3","author":{"name":"toyobayashi"},"license":"MIT","_id":"@emnapi/wasi-threads@1.0.3","maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"homepage":"https://github.com/toyobayashi/emnapi#readme","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"dist":{"shasum":"83fa228bde0e71668aad6db1af4937473d1d3ab1","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.0.3.tgz","fileCount":15,"integrity":"sha512-8K5IFFsQqF9wQNJptGbS6FNKgUTsSRYnTqNCG1vPP8jFdjSv18n2mQfJpkt2Oibo9iBEzcDnDxNwKTzC7svlJw==","signatures":[{"sig":"MEUCIQCk9XAk8hBo4tbcYq55/Af/IMnG5NjMa5Ac+nNNPKe4/AIgFoOF0RpBCo9kfkQrwQnvDlleF3y3U1BqLsa6IKMHAuI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":220061},"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":"92d5bba8c7c3da32d9aa9d2571555f517b847d4e","scripts":{"test":"node ./test/index.js","build":"node ./script/build.js","build:test":"node ./test/build.js"},"_npmUser":{"name":"toyobayashi","actor":{"name":"toyobayashi","type":"user","email":"lifenglin314@outlook.com"},"email":"lifenglin314@outlook.com"},"repository":{"url":"git+https://github.com/toyobayashi/emnapi.git","type":"git"},"_npmVersion":"10.1.0","description":"WASI threads proposal implementation in JavaScript","directories":{},"sideEffects":false,"_nodeVersion":"20.9.0","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/wasi-threads_1.0.3_1751898822851_0.0903609685699478","host":"s3://npm-registry-packages-npm-production"},"contributors":[]},"1.0.4":{"name":"@emnapi/wasi-threads","version":"1.0.4","author":{"name":"toyobayashi"},"license":"MIT","_id":"@emnapi/wasi-threads@1.0.4","maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"homepage":"https://github.com/toyobayashi/emnapi#readme","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"dist":{"shasum":"703fc094d969e273b1b71c292523b2f792862bf4","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.0.4.tgz","fileCount":15,"integrity":"sha512-PJR+bOmMOPH8AtcTGAyYNiuJ3/Fcoj2XN/gBEWzDIKh254XO+mM9XoXHk5GNEhodxeMznbg7BlRojVbKN+gC6g==","signatures":[{"sig":"MEUCICJMoETze99XSch4rr2unK7rziFE53f7hymXlU7I0wV0AiEAtH4nRF1ehR3fD6Cmo389Wn74G+6bjkqh7I8sA30wEmw=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":219508},"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":"4afb0e72e28486a073e55d8fa2e00c9ea916d0f5","scripts":{"test":"node ./test/index.js","build":"node ./script/build.js","build:test":"node ./test/build.js"},"_npmUser":{"name":"toyobayashi","email":"lifenglin314@outlook.com"},"repository":{"url":"git+https://github.com/toyobayashi/emnapi.git","type":"git"},"_npmVersion":"10.1.0","description":"WASI threads proposal implementation in JavaScript","directories":{},"sideEffects":false,"_nodeVersion":"20.9.0","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/wasi-threads_1.0.4_1752828073367_0.4092440293040531","host":"s3://npm-registry-packages-npm-production"},"contributors":[]},"1.1.0":{"name":"@emnapi/wasi-threads","version":"1.1.0","description":"WASI threads proposal implementation in JavaScript","main":"index.js","module":"./dist/wasi-threads.esm-bundler.js","types":"./dist/wasi-threads.d.ts","sideEffects":false,"exports":{".":{"types":{"module":"./dist/wasi-threads.d.ts","import":"./dist/wasi-threads.d.mts","default":"./dist/wasi-threads.d.ts"},"module":"./dist/wasi-threads.esm-bundler.js","import":"./dist/wasi-threads.mjs","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"}},"dependencies":{"tslib":"^2.4.0"},"scripts":{"build":"node ./script/build.js","build:test":"node ./test/build.js","test":"node ./test/index.js"},"repository":{"type":"git","url":"git+https://github.com/toyobayashi/emnapi.git"},"author":{"name":"toyobayashi"},"license":"MIT","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"homepage":"https://github.com/toyobayashi/emnapi#readme","publishConfig":{"access":"public"},"_id":"@emnapi/wasi-threads@1.1.0","gitHead":"3a1cf8da7158477d52d3c298485a5b4a4db42d09","_nodeVersion":"20.9.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-WI0DdZ8xFSbgMjR1sFsKABJ/C5OnRrjT06JXbZKexJGrDuPTzZdDYfFlsgcCXCyf+suG5QU2e/y1Wo2V/OapLQ==","shasum":"60b2102fddc9ccb78607e4a3cf8403ea69be41bf","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.1.0.tgz","fileCount":15,"unpackedSize":227505,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIAqAosqMyXhaiWGL6hqnV1N7grRTY+1wuW4GpsOF8UM5AiA5ifzkTkkXNj3rCGyQRXYXiwnUVEyCbiKogsghc1P6YQ=="}]},"_npmUser":{"name":"toyobayashi","email":"lifenglin314@outlook.com"},"directories":{},"maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/wasi-threads_1.1.0_1756373548762_0.7307111935503441"},"_hasShrinkwrap":false,"contributors":[]},"1.2.0":{"name":"@emnapi/wasi-threads","version":"1.2.0","description":"WASI threads proposal implementation in JavaScript","main":"index.js","module":"./dist/wasi-threads.esm-bundler.js","types":"./dist/wasi-threads.d.ts","sideEffects":false,"exports":{".":{"types":{"module":"./dist/wasi-threads.d.ts","import":"./dist/wasi-threads.d.mts","default":"./dist/wasi-threads.d.ts"},"module":"./dist/wasi-threads.esm-bundler.js","import":"./dist/wasi-threads.mjs","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"}},"dependencies":{"tslib":"^2.4.0"},"scripts":{"build":"node ./script/build.js","build:test":"node ./test/build.js","test":"node ./test/index.js"},"repository":{"type":"git","url":"git+https://github.com/toyobayashi/emnapi.git"},"author":{"name":"toyobayashi"},"license":"MIT","bugs":{"url":"https://github.com/toyobayashi/emnapi/issues"},"homepage":"https://github.com/toyobayashi/emnapi#readme","publishConfig":{"access":"public"},"gitHead":"c9743cf573e52c5b4d31d7369c752b652b29fe9e","_id":"@emnapi/wasi-threads@1.2.0","_nodeVersion":"24.11.1","_npmVersion":"11.6.2","dist":{"integrity":"sha512-N10dEJNSsUx41Z6pZsXU8FjPjpBEplgH24sfkmITrBED1/U2Esum9F3lfLrMjKHHjmi557zQn7kR9R+XWXu5Rg==","shasum":"a19d9772cc3d195370bf6e2a805eec40aa75e18e","tarball":"http://mirrors.tencentyun.com/npm/@emnapi/wasi-threads/-/wasi-threads-1.2.0.tgz","fileCount":15,"unpackedSize":227604,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@emnapi%2fwasi-threads@1.2.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIHESOiVtyLKZmLBII31l6KkyxhFxI20yggbgzumTPtfQAiEAmUqL4vT8PyXuOdRAhVLvwl1jnHLNljsgy2p572Rm8kw="}]},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:05470976-075f-4b7b-8d48-b3daf030dc03"}},"directories":{},"maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/wasi-threads_1.2.0_1773305275085_0.6219428515424414"},"_hasShrinkwrap":false,"contributors":[]}},"time":{"created":"2024-05-19T06:29:20.038Z","modified":"2026-03-12T08:47:55.546Z","1.0.0":"2024-05-19T06:29:20.305Z","1.0.1":"2024-05-19T08:17:47.304Z","1.0.2":"2025-04-15T16:07:03.666Z","1.0.3":"2025-07-07T14:33:43.222Z","1.0.4":"2025-07-18T08:41:13.577Z","1.1.0":"2025-08-28T09:32:28.975Z","1.2.0":"2026-03-12T08:47:55.227Z"},"users":{},"dist-tags":{"latest":"1.2.0"},"_rev":"11866-7c84e7e4faa7803d","_id":"@emnapi/wasi-threads","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```","_attachments":{},"cachedAt":1774231974228}