Skip to content

spatial-monorepo


spatial-monorepo / worker-guard/src / isWorkerScope

Function: isWorkerScope()

isWorkerScope(): boolean

Defined in: packages/worker-guard/src/index.ts:19

isWorkerScope — the single guard used by every Comlink-exposed worker entry point in this monorepo (wayfinding, rtls, pmtiles) to decide whether the current module evaluation is happening inside a real dedicated Worker.

Why not typeof self !== 'undefined' && 'postMessage' in self? That check is also true on the main thread: window has a postMessage method too. A worker module imported directly on the main thread (e.g. by a test, a Node/SSR bundler evaluating it eagerly, or a consumer accidentally importing the worker file instead of its factory) would pass that guard and call Comlink.expose(...), registering a message listener on window that responds to any Comlink-shaped postMessage — including from other origins/iframes. See issue #452.

WorkerGlobalScope only exists as a global inside a genuine dedicated / shared / service worker context, so self instanceof WorkerGlobalScope is a scope check the main thread can never satisfy.

Returns

boolean

Released under commercial licensing.