{"name":"@tybys/wasm-util","version":"0.10.2","keywords":["wasm","webassembly","wasi","polyfill"],"author":{"name":"toyobayashi"},"license":"MIT","_id":"@tybys/wasm-util@0.10.2","maintainers":[{"name":"toyobayashi","email":"lifenglin314@outlook.com"}],"homepage":"https://github.com/toyobayashi/wasm-util#readme","bugs":{"url":"https://github.com/toyobayashi/wasm-util/issues"},"dist":{"shasum":"12b3a1b33db1f9cad4ddff1f604ab7dd00bf464e","tarball":"http://registry-npm.ywwl.com/@tybys/wasm-util/download/@tybys/wasm-util-0.10.2.tgz","fileCount":39,"integrity":"sha512-RoBvJ2X0wuKlWFIjrwffGw1IqZHKQqzIchKaadZZfnNpsAYp2mM0h36JtPCjNDAHGgYez/15uMBpfGwchhiMgg==","signatures":[{"sig":"MEQCIFAsKh7dI7l6rVFO0IEqhgKhfHqpdjPJ5/dnpFswXhocAiAWOtluz5vwT3HEA/nZEdMQ2+wpyU7VBZZSYIKsEM9O0A==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":812994,"size":137612,"noattachment":false,"key":"/@tybys/wasm-util/-/@tybys/wasm-util-0.10.2.tgz"},"main":"./lib/cjs/index.js","types":"./dist/wasm-util.d.ts","module":"./dist/wasm-util.esm-bundler.js","exports":{".":{"types":"./dist/wasm-util.d.ts","import":"./lib/mjs/index.mjs","module":"./dist/wasm-util.esm-bundler.js","require":"./lib/cjs/index.js"}},"gitHead":"cded8e894fe40c7c6200a3fdb2bd33e328b7bb65","scripts":{"lint":"eslint ./src/**/*.{ts,js} --fix","test":"jest","build":"tsgo build","watch":"tsgo watch","prepare":"npm run build"},"_npmUser":{"name":"toyobayashi","email":"lifenglin314@outlook.com"},"repository":{"url":"git+https://github.com/toyobayashi/wasm-util.git","type":"git"},"_npmVersion":"11.6.2","description":"WASI polyfill for browser and some wasm util","directories":{},"_nodeVersion":"24.12.0","dependencies":{"tslib":"^2.4.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^10.1.0","eslint":"^8.25.0","ts-node":"^10.9.1","typescript":"~4.8.3","@tybys/tsgo":"^1.1.0","@types/node":"^14.14.31","memfs-browser":"^3.4.13000","eslint-plugin-n":"^15.3.0","eslint-plugin-import":"^2.26.0","eslint-plugin-promise":"^6.1.0","@typescript-eslint/parser":"^5.40.1","@tybys/ts-transform-pure-class":"^0.1.1","@typescript-eslint/eslint-plugin":"^5.40.1","@tybys/ts-transform-module-specifier":"^0.0.2","eslint-config-standard-with-typescript":"^23.0.0"},"_npmOperationalInternal":{"tmp":"tmp/wasm-util_0.10.2_1777868229323_0.9384414672017809","host":"s3://npm-registry-packages-npm-production"},"readme":"# @tybys/wasm-util\n\nWebAssembly related utils for browser environment\n\n**The output code is ES2019**\n\n## Features\n\nAll example code below need to be bundled by ES module bundlers like `webpack` / `rollup`, or specify import map in browser native ES module runtime.\n\n### WASI polyfill for browser\n\nThe API is similar to the `require('wasi').WASI` in Node.js.\n\nYou can use `memfs-browser` to provide filesystem capability.\n\n- Example: [https://github.com/toyobayashi/wasi-wabt](https://github.com/toyobayashi/wasi-wabt)\n- Demo: [https://toyobayashi.github.io/wasi-wabt/](https://toyobayashi.github.io/wasi-wabt/)\n\n```js\nimport { load, WASI } from '@tybys/wasm-util'\nimport { Volume, createFsFromVolume } from 'memfs-browser'\n\nconst fs = createFsFromVolume(Volume.fromJSON({\n  '/home/wasi': null\n}))\n\nconst wasi = new WASI({\n  args: ['chrome', 'file.wasm'],\n  env: {\n    NODE_ENV: 'development',\n    WASI_SDK_PATH: '/opt/wasi-sdk'\n  },\n  preopens: {\n    '/': '/'\n  },\n  fs,\n\n  // redirect stdout / stderr\n\n  // print (text) { console.log(text) },\n  // printErr (text) { console.error(text) }\n})\n\nconst imports = {\n  wasi_snapshot_preview1: wasi.wasiImport\n}\n\nconst { module, instance } = await load('/path/to/file.wasm', imports)\nwasi.start(instance)\n// wasi.initialize(instance)\n```\n\nImplemented syscalls: [wasi_snapshot_preview1](#wasi_snapshot_preview1)\n\n### `load` / `loadSync`\n\n`loadSync` has 4KB wasm size limit in browser.\n\n```js\n// bundler\nimport { load, loadSync } from '@tybys/wasm-util'\n\nconst imports = { /* ... */ }\n\n// using path\nconst { module, instance } = await load('/path/to/file.wasm', imports)\nconst { module, instance } = loadSync('/path/to/file.wasm', imports)\n\n// using URL\nconst { module, instance } = await load(new URL('./file.wasm', import.meta.url), imports)\nconst { module, instance } = loadSync(new URL('./file.wasm', import.meta.url), imports)\n\n// using Uint8Array\nconst buffer = new Uint8Array([\n  0x00, 0x61, 0x73, 0x6d,\n  0x01, 0x00, 0x00, 0x00\n])\nconst { module, instance } = await load(buffer, imports)\nconst { module, instance } = loadSync(buffer, imports)\n\n// auto asyncify\nconst {\n  module,\n  instance: asyncifiedInstance\n} = await load(buffer, imports, { /* asyncify options */})\nasyncifiedInstance.exports.fn() // => return Promise\n```\n\n### Extend Memory instance\n\n```js\nimport { Memory, extendMemory } from '@tybys/wasm-util'\n\nconst memory = new WebAssembly.Memory({ initial: 256 })\n// const memory = instance.exports.memory\n\nextendMemory(memory)\nconsole.log(memory instanceof Memory)\nconsole.log(memory instanceof WebAssembly.Memory)\n// expose memory view getters like Emscripten\nconst { HEAPU8, HEAPU32, view } = memory\n```\n\n### Asyncify wrap\n\nBuild the C code using `clang`, `wasm-ld` and `wasm-opt`\n\n```c\nvoid async_sleep(int ms);\n\nint main() {\n  async_sleep(200);\n  return 0;\n}\n```\n\n```js\nimport { Asyncify } from '@tybys/wasm-util'\n\nconst asyncify = new Asyncify()\n\nconst imports = {\n  env: {\n    async_sleep: asyncify.wrapImportFunction(function (ms) {\n      return new Promise((resolve) => {\n        setTimeout(resolve, ms)\n      })\n    })\n  }\n}\n\n// async_sleep(200)\nconst bytes = await (await fetch('/asyncfied_by_wasm-opt.wasm')).arrayBuffer()\nconst { instance } = await WebAssembly.instantiate(bytes, imports)\nconst asyncifiedInstance = asyncify.init(instance.exports.memory, instance, {\n  wrapExports: ['_start']\n})\n\nconst p = asyncifedInstance._start()\nconsole.log(typeof p.then === 'function')\nconst now = Date.now()\nawait p\nconsole.log(Date.now() - now >= 200)\n```\n\n### wasi_snapshot_preview1\n\n- [x] args_get\n- [x] args_sizes_get\n- [x] environ_get\n- [x] environ_sizes_get\n- [x] clock_res_get\n- [x] clock_time_get\n- [ ] ~~fd_advise~~\n- [x] fd_allocate\n- [x] fd_close\n- [x] fd_datasync\n- [x] fd_fdstat_get\n- [ ] ~~fd_fdstat_set_flags~~\n- [x] fd_fdstat_set_rights\n- [x] fd_filestat_get\n- [x] fd_filestat_set_size\n- [x] fd_filestat_set_times\n- [x] fd_pread\n- [x] fd_prestat_get\n- [x] fd_prestat_dir_name\n- [x] fd_pwrite\n- [x] fd_read\n- [x] fd_readdir\n- [x] fd_renumber\n- [x] fd_seek\n- [x] fd_sync\n- [x] fd_tell\n- [x] fd_write\n- [x] path_create_directory\n- [x] path_filestat_get\n- [x] path_filestat_set_times\n- [x] path_link\n- [x] path_open\n- [x] path_readlink\n- [x] path_remove_directory\n- [x] path_rename\n- [x] path_symlink\n- [x] path_unlink_file\n- [x] poll_oneoff (timer only)\n- [x] proc_exit\n- [ ] ~~proc_raise~~\n- [x] sched_yield\n- [x] random_get\n- [ ] ~~sock_recv~~\n- [ ] ~~sock_send~~\n- [ ] ~~sock_shutdown~~\n","publish_time":1777868229547,"_cnpm_publish_time":1777868229547,"dist-tags":{"latest":"0.10.4"}}