🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

unwasm

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unwasm - npm Package Compare versions

Comparing version

to
0.2.0

dist/plugin/index.d.mts

16

package.json
{
"name": "unwasm",
"version": "0.1.0",
"version": "0.2.0",
"description": "WebAssembly tools for JavaScript",

@@ -10,12 +10,16 @@ "repository": "unjs/unwasm",

"exports": {
"./examples/*": "./examples/*",
"./plugin": {
"types": "./dist/plugin.d.mts",
"import": "./dist/plugin.mjs"
"types": "./dist/plugin/index.d.mts",
"import": "./dist/plugin/index.mjs"
}
},
"files": [
"dist"
"dist",
"*.d.ts",
"examples/*.wasm"
],
"scripts": {
"build": "unbuild",
"build": "unbuild && pnpm build:examples",
"build:examples": "node ./examples/build.mjs",
"dev": "vitest dev",

@@ -39,2 +43,3 @@ "lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",

"@vitest/coverage-v8": "^1.1.0",
"assemblyscript": "^0.27.22",
"changelogen": "^0.5.5",

@@ -44,2 +49,3 @@ "eslint": "^8.56.0",

"jiti": "^1.21.0",
"miniflare": "^3.20231030.4",
"prettier": "^3.1.1",

@@ -46,0 +52,0 @@ "rollup": "^4.9.1",

@@ -1,3 +0,1 @@

# 🇼 unwasm
[![npm version][npm-version-src]][npm-version-href]

@@ -7,2 +5,4 @@ [![npm downloads][npm-downloads-src]][npm-downloads-href]

# unwasm
Universal [WebAssembly](https://webassembly.org/) tools for JavaScript.

@@ -12,3 +12,3 @@

This project aims to make a common and future-proof solution for WebAssembly modules support suitable for various JavaScript runtimes, Frameworks, and build Tools following [WebAssembly/ES Module Integration](https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration) proposal from WebAssembly Community Group as much as possible.
This project aims to make a common and future-proof solution for WebAssembly modules support suitable for various JavaScript runtimes, frameworks, and build Tools following [WebAssembly/ES Module Integration](https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration) proposal from WebAssembly Community Group as much as possible while also trying to keep compatibility with current ecosystem libraries.

@@ -20,31 +20,87 @@ ## Roadmap

> [!IMPORTANT]
> This Project is under development! Join the linked discussions to be involved!
> This Project is under development! See the linked discussions to be involved!
- [ ] Universal builder plugins ([unjs/unwasm#2](https://github.com/unjs/unwasm/issues/2)) built with [unjs/unplugin](https://github.com/unjs/unplugin)
- [ ] Universal builder plugins built with [unjs/unplugin](https://github.com/unjs/unplugin) ([unjs/unwasm#2](https://github.com/unjs/unwasm/issues/2))
- [x] Rollup
- [ ] Tools to operate and inspect `.wasm` files ([unjs/unwasm#3](https://github.com/unjs/unwasm/issues/3))
- [ ] Runtime utils ([unjs/unwasm#4](https://github.com/unjs/unwasm/issues/4))
- [ ] Tools to operate and inspect `.wasm` files ([unjs/unwasm#3](https://github.com/unjs/unwasm/issues/3))
- [ ] Runtime utils ([unjs/unwasm#4](https://github.com/unjs/unwasm/issues/4))
- [ ] ESM loader for Node.js and other JavaScript runtimes ([unjs/unwasm#5](https://github.com/unjs/unwasm/issues/5))
- [ ] Integration with [Wasmer](https://github.com/wasmerio) ([unjs/unwasm#6](https://github.com/unjs/unwasm/issues/6))
- [ ] Integration with [Wasmer](https://github.com/wasmerio) ([unjs/unwasm#6](https://github.com/unjs/unwasm/issues/6))
- [ ] Convention and tools for library authors exporting wasm modules ([unjs/unwasm#7](https://github.com/unjs/unwasm/issues/7))
## Install
## Bindings API
Install package from [npm](https://www.npmjs.com/package/unwasm):
When importing a `.wasm` module using unwasm, it will take steps to transform the binary and finally resolve to an ESM module that allows you to interact with the WASM module. The returned result is a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object. This proxy allows to use of an elegant API while also having both backward and forward compatibility with WASM modules as the ecosystem evolves.
WebAssembly modules that don't require any imports, can be imported simply like you import any other ESM module.
**Using static import:**
```js
import { sum } from "unwasm/examples/sum.wasm";
```
**Using dynamic import:**
```js
const { sum } = await import("unwasm/examples/sum.wasm").then(
(mod) => mod.default,
);
```
In case your WebAssembly module requires an import object (which is likely!), the usage syntax would be slightly different as we need to initate the module with an import object first.
**Using static import with imports object:**
```js
import { rand, $init } from "unwasm/examples/rand.wasm";
await $init({
env: {
seed: () => () => Math.random() * Date.now(),
},
});
```
**Using dynamic import with imports object:**
```js
const { rand } = await import("unwasm/examples/rand.wasm").then((mod) =>
mod.$init({
env: {
seed: () => () => Math.random() * Date.now(),
},
}),
);
```
> [!NOTE]
> When using **static import syntax**, and before calling `$init`, the named exports will be wrapped into a function by proxy that waits for the module initialization and before that, if called, will immediately try to call `$init()` and return a Promise that calls a function after init.
> [!NOTE]
> Named exports with the `$` prefix are reserved for unwasm. In case your module uses them, you can access them from the `$exports` property.
## Usage
Unwasm needs to transform the `.wasm` imports to the compatible bindings. Currently only method is using a rollup plugin. In the future, more usage methods will be introduced.
### Install
First, install the [`unwasm` npm package](https://www.npmjs.com/package/unwasm):
```sh
# npm
npm install unwasm
npm install --dev unwasm
# yarn
yarn add unwasm
yarn add -D unwasm
# pnpm
pnpm install unwasm
pnpm i -D unwasm
# bun
bun install unwasm
bun i -D unwasm
```
## Using build plugin
### Builder Plugins

@@ -54,2 +110,3 @@ ###### Rollup

```js
// rollup.config.js
import unwasmPlugin from "unwasm/plugin";

@@ -66,3 +123,3 @@

### Options
### Plugin Options

@@ -79,2 +136,3 @@ - `esmImport`: Direct import the wasm file instead of bundling, required in Cloudflare Workers (default is `false`)

- Run interactive tests using `pnpm dev`
- Optionally install [es6-string-html](https://marketplace.visualstudio.com/items?itemName=Tobermory.es6-string-html) extension to make it easier to work with string templates.

@@ -81,0 +139,0 @@ ## License