BoundaryML/baml · error · JwtError

Missing window object

Error message

Missing window object

What it means

JwtError::NoWindow is returned when the WASM JWT code cannot obtain the browser window object via web_sys::window(). All WebCrypto operations (SubtleCrypto access) depend on it, so JWT signing cannot proceed outside a browser context.

Source

Thrown at engine/baml-runtime/src/internal/wasm_jwt.rs:31

    Engine,
};
use js_sys::{Array, Object, Uint8Array};
use serde::{Deserialize, Serialize};
use serde_json::json;
use thiserror::Error;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
use web_sys::{window, CryptoKey, SubtleCrypto};

#[derive(Error, Debug)]
pub enum JwtError {
    #[error("JavaScript error: {0:?}")]
    JsError(JsValue),
    #[error("Base64 decode error: {0}")]
    Base64Error(#[from] base64::DecodeError),
    #[error("JSON error: {0}")]
    JsonError(#[from] serde_json::Error),
    #[error("Missing window object")]
    NoWindow,
    #[error("Missing crypto API")]
    NoCrypto,
}

impl From<JsValue> for JwtError {
    fn from(err: JsValue) -> Self {
        JwtError::JsError(err)
    }
}

pub async fn encode_jwt(
    claims: &serde_json::Value,
    private_key_pem: &str,
) -> Result<String, JwtError> {
    // Extract the crypto.subtle API
    let window = window().ok_or(JwtError::NoWindow)?;
    let crypto = window.crypto()?;

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Run the JWT path only in a browser main thread where window exists.
  2. Guard with a feature check: skip JWT signing in tests/SSR or mock the crypto dependency.
  3. If using workers, route crypto work to the main thread or use self.crypto directly.
  4. Use a server-side JWT implementation instead of the wasm module in Node.

Example fix

// before
let window = window().unwrap();
// after
let window = window().ok_or(JwtError::NoWindow)?;
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof window === "undefined") {
  throw new Error("NoWindow: run in a browser context");
}

Type guard

function inBrowser() {
  return typeof window !== "undefined" && typeof window.document !== "undefined";
}

Try / catch

match get_jwt(...) {
    Err(JwtError::NoWindow) => fallback_to_server_signing(),
    other => other,
}

Prevention

When it happens

Trigger: Running the wasm32 build in a non-browser environment: Node.js tests without DOM globals, web workers without the window handle expected, or calling the function before DOM availability.

Common situations: Unit-testing WASM code in Node where window is undefined; executing during SSR; offscreen/worker contexts; misconfigured bundler polyfills.

Understand the failure class

Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/13a96a7bd44b3752. Report an issue: GitHub.