Skip to content

armgit / moli

Public Imported from GitHub
0

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

2.5 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 src="/wpt/ported/worker/resources/assert-bootstrap-error.js"></script>
<script>
function worker_url(source) {
  return "data:text/javascript," + encodeURIComponent(source);
}

function wait(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

function wait_for(promise, description) {
  return Promise.race([
    promise,
    wait(250).then(function () {
      throw new Error("timed out waiting for " + description);
    }),
  ]);
}

async function assert_module_link_failure(source, description) {
  const previousOnerror = window.onerror;
  let messageSeen = false;
  const worker = new Worker(worker_url(source), { type: "module" });

  try {
    window.onerror = function () {
      return true;
    };
    worker.onmessage = function () {
      messageSeen = true;
    };

    const event = await wait_for(
      new Promise(function (resolve) {
        worker.onerror = function (event) {
          resolve(event);
          return true;
        };
      }),
      "module worker " + description + " link error",
    );

    assert_worker_bootstrap_error(event, worker);
    assert_false(messageSeen, "module evaluation should not continue after the link failure");
  } finally {
    window.onerror = previousOnerror;
    worker.terminate();
  }
}

promise_test(async function () {
  const depUrl = worker_url("export const present = 1;");

  const missingNamedSource = [
    "import { absent } from " + JSON.stringify(depUrl) + ";",
    "postMessage('unexpected');",
  ].join("\n");
  await assert_module_link_failure(
    missingNamedSource,
    "missing named import",
  );

  const missingDefaultSource = [
    "import missingDefault from " + JSON.stringify(depUrl) + ";",
    "postMessage('unexpected');",
  ].join("\n");
  await assert_module_link_failure(
    missingDefaultSource,
    "missing default import",
  );

  const reexportUrl = worker_url([
    "export { absent as forwarded } from " + JSON.stringify(depUrl) + ";",
  ].join("\n"));
  const missingReexportSource = [
    "import * as reexported from " + JSON.stringify(reexportUrl) + ";",
    "postMessage(reexported.forwarded);",
  ].join("\n");
  await assert_module_link_failure(
    missingReexportSource,
    "missing re-export",
  );
}, "Dedicated module workers reject missing static import exports before evaluation");
</script>