Skip to content

script-utils/node-web-globals

Puts back the Web API globals Node provides but a VM-backed Vitest pool’s jsdom context lacks.

Under the default pool a test sees Node’s global object with the jsdom window laid over it, so crypto.subtle, the Web Streams classes, CompressionStream, the Performance* classes and URLPattern are all there. Under vmThreads / vmForks the test’s global object IS the jsdom window, and Vitest copies only a fixed handful of Node globals into it (structuredClone, fetch, TextEncoder, …), so those are simply absent. Measured on Node 26 with jsdom: crypto exists without subtle, and 31 global names present under forks are missing under vmThreads — the ones restored here, plus CryptoKey and QuotaExceededError, which no Node module exports, and matchMedia, createPopup, scrollLeft and scrollTop, which are not Node’s to give.

Absence is the dangerous failure, not the loud one. A call such as crypto.subtle.digest throws and is noticed, but code that feature-detects (typeof CompressionStream !== 'undefined') silently takes its fallback branch, so the suite passes while testing a path Obsidian’s Electron renderer — which has every one of these — never runs. restoreNodeWebGlobals closes that gap by defining each missing name from Node’s own module that exports it, and leaves every name already present alone, so it is a no-op under the default pool.

Function Description
restoreNodeWebGlobals Defines every Node Web API global that is missing from target, plus crypto.subtle and SubtleCrypto, leaving whatever is already present untouched.
Variable Description
NODE_WEB_GLOBAL_NAMES The global names a VM-backed jsdom context lacks and a Node module exports under the same name.