{"name":"napi-wasm","version":"0.0.1-security","description":"security holding package","repository":{"type":"git","url":"git+https://github.com/npm/security-holder.git"},"bugs":{"url":"https://github.com/npm/security-holder/issues"},"homepage":"https://github.com/npm/security-holder#readme","_id":"napi-wasm@0.0.1-security","_nodeVersion":"12.19.0","_npmVersion":"8.19.1","dist":{"integrity":"sha512-Qs0mROjz6h1Acc546kSRexTn3G/y2j+a0Ja1bejGk+o9oaOd73BjVvYroqBskgDOGlPWqF3f86YE8R3VK4m4PQ==","shasum":"42b9edac076f5e73adb0338208fb1a776b1037eb","tarball":"http://registry-npm.ywwl.com/napi-wasm/download/napi-wasm-0.0.1-security.tgz","fileCount":2,"unpackedSize":496,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIApuqmp6xlRgk/d75ZTH7syDwZnzJYRHHv5DBX7lTeReAiEAxDLs4MPaCjoaCACErOYrpbXlEc0uV3i+X2+COwG1DOU="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjrXX3ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmpc2g//ZDAliDeqVfswlrse7ibQTtEbEG4GFZgZqxw5bW6H5XjoyuEY\r\nBNtT0P0JnN2BxL2RGud5ld46FMeLgvGJjj7PtRTlwZeLvHuaAnZtaCt56Aoa\r\nN+E5Wcks34W1hSLduJgVs+wOGP6mcLX9rCU/RAX1pbESMbIs9ybpbqan1s2c\r\n1Ogf00TRJFpcs4Xc8CK4syJbZj/8G8BMgnnqo1JAGDtsmdD1qBFBJw9dOwvL\r\nSkGC/ptTxmqUzkinOMGDsFjOqXv2ZwVYQtHLhM5kibwVNWkbilPJxLIJnxW/\r\nJSjQEzuWn6GRrCvvaop1mil1FD/3HMrnxSHt4cRY6LOMb0dYx3hAIWZJpaxE\r\nI6P7JFi2JivgMw+qA5f1i9tJxJBIEWcM7uLWIkYojaCeRvDwv85XrM6abpRN\r\nbpAoF67OdF/p/qdOol3pn8R/al78kwXdosTxhGKhYGt1Tlyrq0finioxK1GQ\r\nT9V8sEIjyGIMhzaeqvQHSy08ak0Riuj83oTLwhFEOnqG2qNm0c3WksfolpsY\r\ndVny1fFocGOTvaTu7l61jw0/Row6y6+iVUyiqnqgS8EKnkEPp8bNZQXFvO8V\r\n/ipSWh68DiL1e+tNqVClcPLkQw4gv4yapDJXzKI1S4Jv25BsqczZseebLZr3\r\nMNahWFjPAipDe7Ks1TH5+wL+aWHl64AEr3A=\r\n=HiUR\r\n-----END PGP SIGNATURE-----\r\n","size":419,"noattachment":false,"key":"/napi-wasm/-/napi-wasm-0.0.1-security.tgz"},"_npmUser":{"name":"lisayu","email":"lisa@npmjs.com"},"directories":{},"maintainers":[{"name":"lisayu","email":"lisa@npmjs.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-wasm_0.0.1-security_1672312311299_0.9159403851987413"},"_hasShrinkwrap":false,"readme":"# napi-wasm\n\nAn implementation of the [napi](https://nodejs.org/api/n-api.html) API for WASM. Enables using some native Node modules in browsers and other environments.\n\n## Setup\n\nTo use napi-wasm, there are a few requirements:\n\n1. Configure your linker to export an indirect function table. With ldd, this is the `--export-table` flag. This enables JavaScript to call callback functions registered by WASM. It is exposed in the WebAssembly exports as `__indirect_function_table`.\n2. Export a function from your WASM build named `napi_register_module_v1` (Node's default), or `napi_register_wasm_v1` for WASM-specific builds. This is called during initialization to setup the `exports` object for your module. It receives an environment and an exports object pointer as arguments, which you can add properties to.\n3. Include a function named `napi_wasm_malloc` in your WASM build. This is called from JavaScript by napi-wasm to allocate memory in the WASM heap. It should accept a `uint32` size argument indicating the number of bytes to allocate, and return a `uint8` pointer to allocated memory.\n4. Compile for the `wasm32-unknown-unknown` target.\n\n### In Rust\n\nThe above steps should apply for any programming language, but here's an example in Rust. First, define a `napi_wasm_malloc` function so JavaScript can allocate memory in the WASM heap using the default allocator.\n\n```rust\nuse std::alloc::{alloc, Layout};\n\n#[no_mangle]\npub extern \"C\" fn napi_wasm_malloc(size: usize) -> *mut u8 {\n  let align = std::mem::align_of::<usize>();\n  if let Ok(layout) = Layout::from_size_align(size, align) {\n    unsafe {\n      if layout.size() > 0 {\n        let ptr = alloc(layout);\n        if !ptr.is_null() {\n          return ptr;\n        }\n      } else {\n        return align as *mut u8;\n      }\n    }\n  }\n\n  std::process::abort();\n}\n```\n\nNext, implement `napi_register_wasm_v1` to register your module exports. We'll use the [napi-rs](https://github.com/napi-rs/napi-rs) bindings in this example to make it a bit nicer than calling C APIs directly. Note that the napi-rs `#[module_exports]` macro currently doesn't work in WASM because Rust doesn't support ctor setup functions in WASM targets yet, so we'll need to do this manually.\n\n```rust\nuse napi::{Env, JsObject, NapiValue};\n\n#[no_mangle]\npub unsafe extern \"C\" fn napi_register_wasm_v1(raw_env: napi::sys::napi_env, raw_exports: napi::sys::napi_value) {\n  let env = Env::from_raw(raw_env);\n  let exports = JsObject::from_raw_unchecked(raw_env, raw_exports);\n\n  exports.create_named_method(\"transform\", transform);\n}\n\n#[js_function(1)]\nfn transform(ctx: CallContext) -> napi::Result<JsUnknown> {\n  // ...\n}\n```\n\nTo compile, you need to export a function table and use the correct target.\n\n```shell\n RUSTFLAGS=\"-C link-arg=--export-table\" cargo build --target wasm32-unknown-unknown\n```\n\nThis will output a file in `target/wasm32-unknown-unknown/debug/YOUR_CRATE.wasm` which you can load in a JavaScript environment.\n\nYou can also put the rust flags in a `.cargo/config.toml` file so you don't need to provide the environment variable each time you run `cargo build`.\n\n```toml\n[target.wasm32-unknown-unknown]\nrustflags = [\"-C\", \"link-arg=--export-table\"]\n```\n\n### Loading\n\nTo load a WASM file and initialize a napi environment, you'll need to import the `napi-wasm` package. You instantiate a WASM module as usual, providing `napi` as the `env` import key. This provides the napi functions for your WASM module to use.\n\nThen, pass the WASM instance to the `Environment` constructor to setup a napi environment. This will call `napi_register_wasm_v1` or `napi_register_module_v1` to setup the exports object. Then you can call functions on the exports object as you would in Node.\n\n```js\nimport { Environment, napi } from 'napi-wasm';\n\n// Construct a URL and instantiate a WebAssembly module as usual.\nconst url = new URL('path/to/lib.wasm', import.meta.url);\nconst { instance } = await WebAssembly.instantiateStreaming(fetch(url), {\n  env: napi\n});\n\n// Create an environment.\nlet env = new Environment(instance);\nlet exports = env.exports;\n\n// Use exports as usual!\nexports.transform({\n  // ...\n});\n```\n\nWhen you are done with an `Environment`, call the `destroy()` function to clean up memory.\n","publish_time":1672312311435,"_cnpm_publish_time":1672312311435,"dist-tags":{"latest":"1.1.3"}}