{"record":{"id":"13a96a7bd44b3752","repo":"BoundaryML/baml","slug":"missing-window-object","errorCode":null,"errorMessage":"Missing window object","messagePattern":"Missing window object","errorType":"exception","errorClass":"JwtError","httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/internal/wasm_jwt.rs","lineNumber":31,"sourceCode":"    Engine,\n};\nuse js_sys::{Array, Object, Uint8Array};\nuse serde::{Deserialize, Serialize};\nuse serde_json::json;\nuse thiserror::Error;\nuse wasm_bindgen::JsValue;\nuse wasm_bindgen_futures::JsFuture;\nuse web_sys::{window, CryptoKey, SubtleCrypto};\n\n#[derive(Error, Debug)]\npub enum JwtError {\n    #[error(\"JavaScript error: {0:?}\")]\n    JsError(JsValue),\n    #[error(\"Base64 decode error: {0}\")]\n    Base64Error(#[from] base64::DecodeError),\n    #[error(\"JSON error: {0}\")]\n    JsonError(#[from] serde_json::Error),\n    #[error(\"Missing window object\")]\n    NoWindow,\n    #[error(\"Missing crypto API\")]\n    NoCrypto,\n}\n\nimpl From<JsValue> for JwtError {\n    fn from(err: JsValue) -> Self {\n        JwtError::JsError(err)\n    }\n}\n\npub async fn encode_jwt(\n    claims: &serde_json::Value,\n    private_key_pem: &str,\n) -> Result<String, JwtError> {\n    // Extract the crypto.subtle API\n    let window = window().ok_or(JwtError::NoWindow)?;\n    let crypto = window.crypto()?;","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/internal/wasm_jwt.rs#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"Unit-testing WASM code in Node where window is undefined; executing during SSR; offscreen/worker contexts; misconfigured bundler polyfills.","solutions":["Run the JWT path only in a browser main thread where window exists.","Guard with a feature check: skip JWT signing in tests/SSR or mock the crypto dependency.","If using workers, route crypto work to the main thread or use self.crypto directly.","Use a server-side JWT implementation instead of the wasm module in Node."],"exampleFix":"// before\nlet window = window().unwrap();\n// after\nlet window = window().ok_or(JwtError::NoWindow)?;","handlingStrategy":"type-guard","validationCode":"if (typeof window === \"undefined\") {\n  throw new Error(\"NoWindow: run in a browser context\");\n}","typeGuard":"function inBrowser() {\n  return typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n}","tryCatchPattern":"match get_jwt(...) {\n    Err(JwtError::NoWindow) => fallback_to_server_signing(),\n    other => other,\n}","preventionTips":["Only call WASM JWT code from browser main-thread code paths.","Guard SSR and Node test suites so they never execute this module.","Use server-side signing for non-browser runtimes."],"tags":["wasm","browser","jwt","environment"],"backgroundTag":"unsupported-platform","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}