Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

884 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

/** Load a module worker across origins, including from file:// pages. */
export function createModuleWorker(url: URL, options: WorkerOptions): Worker {
  if (url.origin === globalThis.origin) {
    return new Worker(url.href, { ...options, type: "module" });
  }
  const js = `import ${JSON.stringify(url.href)}`;
  // Blob workers preserve the page origin, but file:// needs a data URL.
  const source =
    globalThis.origin === "null"
      ? `data:application/javascript,${encodeURIComponent(js)}`
      : URL.createObjectURL(new Blob([js], { type: "application/javascript" }));
  try {
    return new Worker(source, { ...options, type: "module" });
  } finally {
    if (source.startsWith("blob:")) {
      // Worker() captures the Blob before returning, even while imports load.
      URL.revokeObjectURL(source);
    }
  }
}