-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 #409).
- Loading branch information
1 parent
a73e32f
commit 66d53d0
Showing
10 changed files
with
823 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`). |
10 changes: 10 additions & 0 deletions
10
packages/example-wasm-app/worker/service-worker-module/export_wasm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
5 changes: 5 additions & 0 deletions
5
packages/example-wasm-app/worker/service-worker-module/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
Oops, something went wrong.