init#

The default export of the browser build (pkg/plen_wasm.js). It fetches and instantiates the WebAssembly module. Await it once before the first new JsonProject or schedule call. The Node build (pkg-node/) has no init: require loads and instantiates the module synchronously.

Signature#

init(module_or_path?: { module_or_path: InitInput | Promise<InitInput> }): Promise<InitOutput>

type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

Asynchronous. It's the only async step of the raw surface; every other export is synchronous.

Parameters#

Name Type Description
module_or_path { module_or_path: InitInput | Promise<InitInput> } Optional. Where to load the module from. Omitted: plen_wasm_bg.wasm in the same folder as plen_wasm.js.

Accepted values for module_or_path.module_or_path:

Value What init does
URL string, URL, Request Fetches it, then instantiates the response.
Response Instantiates the response body. Streams it when the server sends Content-Type: application/wasm; otherwise logs a warning and falls back to reading the whole body.
BufferSource (bytes) Instantiates the bytes.
WebAssembly.Module Instantiates the compiled module.
A promise of any of these Awaits it, then handles the value as above.

Passing the value directly, without the { module_or_path } wrapper, still works. wasm-bindgen marks that form as deprecated and logs a warning.

Behavior#

  • Runs once per page or worker. When the module is already instantiated, a later init or initSync call returns the existing instance and ignores its argument.
  • The exports read the module that init installs. Calling new JsonProject(...) or schedule(...) before the promise resolves throws a TypeError.

Returns#

A promise of the instantiated module's raw exports (InitOutput). These are binding-generator internals. Call the named exports (JsonProject, schedule) instead, and treat the resolved value only as a "ready" signal.

Errors#

Error When
Rejects with Error (failed to fetch Wasm: ...) The fetched Response has a non-2xx status.
Rejects (fetch error) The URL can't be fetched.
Rejects (WebAssembly.CompileError / LinkError) The bytes aren't a valid module for this build.

See Error handling for the other host-level failures.

initSync#

initSync(module: { module: SyncInitInput }): InitOutput

type SyncInitInput = BufferSource | WebAssembly.Module;

Instantiates the module synchronously from bytes or a compiled WebAssembly.Module. Bytes are compiled on the calling thread. It fetches nothing, so it fits hosts that already hold the module, such as a worker that receives a WebAssembly.Module from the main thread. Like init, it returns the existing instance when the module is already instantiated. Passing the value without the { module } wrapper is deprecated and logs a warning. Invalid bytes throw synchronously.

Example#

import init, { schedule } from "./pkg/plen_wasm.js";

await init({ module_or_path: new URL("/static/plen_wasm_bg.wasm", location.origin) });

const data = { tasks: [{ id: 1, start_date: "2026-01-05", duration: 2 }] };
const envelope = JSON.parse(schedule(JSON.stringify(data), "{}"));

Synchronous loading in a worker:

import { initSync, JsonProject } from "./pkg/plen_wasm.js";

self.onmessage = ({ data }) => {
  initSync({ module: data.wasmModule }); // WebAssembly.Module sent by the main thread
  const project = new JsonProject("{}");
  // ...
  project.free();
};

Build targets and the Node loading path are in Module and handles.

Note

The package entry (plen-wasm) also has a default export init, used with the Project class. In the browser build, await init() before creating a Project; it loads the WebAssembly module and resolves to the module's exports. See JavaScript usage.