Skip to content

Commit

Permalink
feat: import .wasm modules in service worker format workers
Browse files Browse the repository at this point in the history
This allows importing `.wasm` modules in service worker format workers. We do this by hijacking imports to `.wasm` modules, and instead registering them under `[wasm_modules]` (building on the work from #409).
  • Loading branch information
threepointone committed Feb 10, 2022
1 parent a73e32f commit 66d53d0
Show file tree
Hide file tree
Showing 10 changed files with 823 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .changeset/ten-moons-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

feat: import `.wasm` modules in service worker format workers

This allows importing `.wasm` modules in service worker format workers. We do this by hijacking imports to `.wasm` modules, and instead registering them under `[wasm_modules]` (building on the work from https://github.com/cloudflare/wrangler2/pull/409).
12 changes: 10 additions & 2 deletions packages/example-wasm-app/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
## example-wasm-app

There are 2 workers in this package. They were both created with a [`workers-rs`](https://github.com/cloudflare/workers-rs) project, running a build, removing unneeded artifacts, and copying the output folders.
There are 3 workers in this package. They were created with a [`workers-rs`](https://github.com/cloudflare/workers-rs) project, running a build, removing unneeded artifacts, and copying the output folders.

The wasm file generated, `index_bg.wasm` is copied into `./worker`, and shared by the 2 workers. `./worker/module` contains a "modules" format worker and imports the wasm module as a regular es module, while `./worker/service-worker` contains a "service-worker" format worker and uses wrangler.toml to bind the wasm module as a global `MYWASM`. They're otherwise identical.
The wasm file generated, `index_bg.wasm` is copied into `./worker`, and shared by the 3 workers.

- `./worker/module` contains a "modules" format worker and imports the wasm module as a regular es module.
- `./worker/service-worker` contains a "service-worker" format worker and uses `wrangler.toml` to bind the wasm module as a global `MYWASM`.
- `./worker/service-worker-module` contains a "service-worker" format worker and imports the wasm module as a regular es module.

They're otherwise identical.

You can run the module worker with `npx wrangler dev worker/module/index.js` (or from the `wrangler` package directory with `npm start -- dev ../example-wasm-app/worker/module/index.js`).

You can run the service-worker worker with `npx wrangler dev worker/service-worker/index.js --config worker/service-worker/wrangler.toml` (or from the `wrangler` package directory with `npm start -- dev ../example-wasm-app/worker/service-worker/index.js --config ../example-wasm-app/worker/service-worker/wrangler.toml`).

You can run the service-worker-module worker with `npx wrangler dev worker/service-worker-module/index.js` (or from the `wrangler` package directory with `npm start -- dev ../example-wasm-app/worker/service-worker-module/index.js`).
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as index_bg from "./index_bg.js";
import _wasm from "../index_bg.wasm";

const _wasm_memory = new WebAssembly.Memory({ initial: 512 });
let importsObject = {
env: { memory: _wasm_memory },
"./index_bg.js": index_bg,
};

export default new WebAssembly.Instance(_wasm, importsObject).exports;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as worker from "./index_bg.js";

addEventListener("fetch", (event) => {
event.respondWith(worker.fetch(event.request));
});
Loading

0 comments on commit 66d53d0

Please sign in to comment.