Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

2.4 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
let uniqueNameCounter = 0;

function unique_name(prefix) {
  uniqueNameCounter++;
  return prefix + "-" + Date.now() + "-" + uniqueNameCounter;
}

function module_data_url(source) {
  return "data:text/javascript," + encodeURIComponent(source);
}

function next_worker_outcome(worker) {
  return new Promise(function (resolve) {
    worker.onerror = function (event) {
      resolve({ type: "error", message: String(event.message || "") });
    };
    worker.port.onmessage = function (event) {
      resolve({ type: "message", data: event.data });
    };
    worker.port.start();
  });
}

promise_test(async function () {
  const staticURL = new URL(
    "/wpt/ported/sharedworker/resources/module-dataurl/static-dependency.js",
    location.href
  ).href;
  const source = [
    "import " + JSON.stringify(staticURL) + ";",
    "onconnect = event => event.ports[0].postMessage(globalThis.staticDependencyValue);",
  ].join("\n");
  const worker = new SharedWorker(module_data_url(source), {
    type: "module",
    name: unique_name("data-module-static-http"),
  });
  const outcome = await next_worker_outcome(worker);

  assert_equals(outcome.type, "message");
  assert_equals(outcome.data, "static-dependency");
}, "Data URL module SharedWorker static import uses the creator origin settings");

promise_test(async function () {
  const dynamicURL = new URL(
    "/wpt/ported/sharedworker/resources/module-dataurl/dynamic-dependency.js",
    location.href
  ).href;
  const source = [
    "onconnect = async event => {",
    "  const port = event.ports[0];",
    "  try {",
    "    const imported = await import(" + JSON.stringify(dynamicURL) + ");",
    "    port.postMessage(['LOADED', imported.value]);",
    "  } catch (error) {",
    "    port.postMessage(['ERROR', error && error.name]);",
    "  }",
    "};",
  ].join("\n");
  const worker = new SharedWorker(module_data_url(source), {
    type: "module",
    name: unique_name("data-module-dynamic-http"),
  });
  const outcome = await next_worker_outcome(worker);

  assert_equals(outcome.type, "message");
  assert_array_equals(outcome.data, ["ERROR", "TypeError"]);
}, "Data URL module SharedWorker dynamic import uses the worker opaque origin settings");
</script>