Load The WebAssembly Binary From A CDN Or Your Own Bytes
By default the package loads muhammara-wasm.wasm from next to its own module
through import.meta.url. Bundlers such as Vite and webpack detect that
reference and emit the file, and under Node it is read from the installed
package, so most applications need no loading options at all.
Use the options below when the binary has to come from somewhere else. Both
createMuhammaraWasm() and createRecipe() accept them.
Serve The Binary From A CDN Or Another Path
Return the URL from locateFile. In pages and Workers the package fetches it
and compiles the module while it downloads:
import { createRecipe } from "@muhammara/wasm";
var wasmUrl =
"https://cdn.example.com/muhammara-wasm/1.1.0/muhammara-wasm.wasm";
var Recipe = await createRecipe({
locateFile: (path) => (path.endsWith(".wasm") ? wasmUrl : path),
});
locateFile receives the requested file name, muhammara-wasm.wasm, and the
directory the package would otherwise use. Return other paths unchanged.
The package always fetches with credentials: "same-origin", so cookies are not
sent to another origin. Use wasmBinary when the request needs cookies,
authorization headers, or other custom options.
Supply Bytes You Retrieved Yourself
Pass the binary as a Uint8Array or ArrayBuffer in wasmBinary. Nothing is
fetched or read, and locateFile's result for the binary is ignored:
import { createMuhammaraWasm } from "@muhammara/wasm";
var response = await fetch(wasmUrl, {
headers: { authorization: `Bearer ${token}` },
});
if (!response.ok) throw new Error(`Could not load ${wasmUrl}`);
var muhammara = await createMuhammaraWasm({
wasmBinary: await response.arrayBuffer(),
});
To keep the binary across visits, store it with the Cache API. Put the package version in the cache name so an upgrade never pairs new JavaScript with an old binary:
var cache = await caches.open("muhammara-wasm-1.1.0");
var cached = await cache.match(wasmUrl);
if (!cached) {
await cache.add(wasmUrl);
cached = await cache.match(wasmUrl);
}
var muhammara = await createMuhammaraWasm({
wasmBinary: await cached.arrayBuffer(),
});
A binary the user selects, such as one from <input type="file">, works the
same way. Read Blob and File objects with arrayBuffer() first:
Other typed arrays, DataView, Blob, and File objects are rejected with
TypeError: wasmBinary must be a Uint8Array or ArrayBuffer. The byte budgets in
limits apply to PDF and asset inputs, not to the binary.
Load A Remote Binary Under Node
Under Node, locateFile accepts file paths and file: URLs only. Fetch a
binary served over https: yourself and pass the bytes:
import { createMuhammaraWasm } from "@muhammara/wasm";
var response = await fetch(wasmUrl);
var muhammara = await createMuhammaraWasm({
wasmBinary: new Uint8Array(await response.arrayBuffer()),
});
Serve The Binary Correctly
- Use the same version. The binary must come from the same
@muhammara/wasmversion as the JavaScript that loads it. A mismatched binary can fail during instantiation or misbehave later. Copy it fromdist/muhammara-wasm.wasmin the installed package whenever you upgrade. - Allow cross-origin requests. A binary on another origin must be served
with
Access-Control-Allow-Origin, whether the package or your code fetches it. - Send the WebAssembly MIME type. Serve the file as
application/wasm. Otherwise the package logs a console error and downloads the whole file before compiling it, which is slower but still works. - Update the Content Security Policy. A strict policy must list the host
under
connect-srcand allow compilation with'wasm-unsafe-eval'inscript-src.
Handle A Binary That Fails To Load
When the binary cannot be fetched, read, compiled, or instantiated, Emscripten's
loader, which the package uses, fails the load. It calls an onAbort(reason)
option with the underlying error, logs the reason through a
printErr(message) option, or console.error when none is given, and rejects
with a WebAssembly.RuntimeError whose message starts with Aborted(:
try {
var muhammara = await createMuhammaraWasm({
locateFile: () => wasmUrl,
onAbort: (reason) => reportLoadFailure(reason),
});
} catch (error) {
// error instanceof WebAssembly.RuntimeError
// error.message: "Aborted(Error: 404 : https://...). Build with ..."
}
A failed streaming compile, such as one caused by a wrong MIME type, is logged
through printErr as well before the package downloads the file again.
See Browser Setup for installing the package and the
API Reference for the MuhammaraWasmOptions declaration.