Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.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>
function worker_url(source) {
  return "data:text/javascript," + encodeURIComponent(source);
}

function assert_throws_name(expectedName, callback, description) {
  let error = null;
  try {
    callback();
  } catch (thrown) {
    error = thrown;
  }
  assert_true(error !== null, (description || "assert_throws_name") + ": expected an exception");
  assert_equals(error.name, expectedName, (description || "assert_throws_name") + ": unexpected exception name");
}

function next_message(target) {
  return new Promise(function (resolve) {
    target.onmessage = resolve;
  });
}

promise_test(async function () {
  const worker = new Worker(worker_url("postMessage('ready');"));
  const probe = new MessageChannel();

  try {
    assert_throws_name(
      "DataCloneError",
      function () {
        worker.postMessage({ port: probe.port1 });
      },
      "nested MessagePort without transfer list should throw",
    );

    const delivered = next_message(probe.port2);
    probe.port1.postMessage("still-live");
    assert_equals((await delivered).data, "still-live", "failed worker postMessage should not detach the nested port");
  } finally {
    probe.port1.close();
    probe.port2.close();
    worker.terminate();
  }
}, "Worker.postMessage rejects nested MessagePorts that are not transferred");

test(function () {
  const worker = new Worker(worker_url("onmessage = function () {}"));

  try {
    const buffer = new ArrayBuffer(4);
    worker.postMessage(buffer, [buffer]);
    assert_equals(buffer.byteLength, 0, "first transfer should detach the sender buffer");

    assert_throws_name(
      "DataCloneError",
      function () {
        worker.postMessage(buffer, [buffer]);
      },
      "detached ArrayBuffer transfer should throw",
    );
  } finally {
    worker.terminate();
  }
}, "Worker.postMessage rejects detached ArrayBuffers in transfer lists");

promise_test(async function () {
  const channel = new MessageChannel();
  const probe = new MessageChannel();

  try {
    assert_throws_name(
      "DataCloneError",
      function () {
        channel.port1.postMessage({ port: probe.port1 });
      },
      "nested MessagePort without transfer list should throw",
    );

    const delivered = next_message(probe.port2);
    probe.port1.postMessage("still-live");
    assert_equals((await delivered).data, "still-live", "failed MessagePort.postMessage should not detach the nested port");
  } finally {
    channel.port1.close();
    channel.port2.close();
    probe.port1.close();
    probe.port2.close();
  }
}, "MessagePort.postMessage rejects nested MessagePorts that are not transferred");

test(function () {
  const channel = new MessageChannel();

  try {
    const buffer = new ArrayBuffer(4);
    channel.port1.postMessage(buffer, [buffer]);
    assert_equals(buffer.byteLength, 0, "first MessagePort transfer should detach the sender buffer");

    assert_throws_name(
      "DataCloneError",
      function () {
        channel.port1.postMessage(buffer, [buffer]);
      },
      "detached ArrayBuffer transfer should throw",
    );
  } finally {
    channel.port1.close();
    channel.port2.close();
  }
}, "MessagePort.postMessage rejects detached ArrayBuffers in transfer lists");
</script>