summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/scripting-1/the-script-element/module/dynamic-import/blob-url-workers.window.js
blob: 70cb20fa57ad610f5ecd5d64141193a629208b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function objectUrlFromModule(module) {
  const blob = new Blob([module], { type: "text/javascript" });
  return URL.createObjectURL(blob);
}

const moduleText = `export const foo = "bar";`;

async_test((t) => {
  const moduleBlobUrl = objectUrlFromModule(moduleText);
  t.add_cleanup(() => URL.revokeObjectURL(moduleBlobUrl));

  const worker = new Worker("./resources/blob-url-worker.js");
  worker.postMessage(moduleBlobUrl);

  worker.addEventListener(
    "message",
    t.step_func_done((evt) => {
      assert_true(evt.data.importSucceeded);
      assert_equals(evt.data.module.foo, "bar");
    })
  );
}, "A blob URL created in a window agent can be imported from a worker");

async_test((t) => {
  const moduleBlobUrl = objectUrlFromModule(moduleText);
  URL.revokeObjectURL(moduleBlobUrl);

  const worker = new Worker("./resources/blob-url-worker.js");
  worker.postMessage(moduleBlobUrl);

  worker.addEventListener(
    "message",
    t.step_func_done((evt) => {
      assert_false(evt.data.importSucceeded);
      assert_equals(evt.data.errorName, "TypeError");
    })
  );
}, "A blob URL revoked in a window agent will not resolve in a worker");

promise_test(async (t) => {
  const moduleBlobUrl = objectUrlFromModule(moduleText);

  await import(moduleBlobUrl);

  URL.revokeObjectURL(moduleBlobUrl);

  const worker = new Worker("./resources/blob-url-worker.js");
  worker.postMessage(moduleBlobUrl);

  await new Promise((resolve) => {
    worker.addEventListener(
      "message",
      t.step_func((evt) => {
        assert_false(evt.data.importSucceeded);
        assert_equals(evt.data.errorName, "TypeError");
        resolve();
      })
    );
  });
}, "A revoked blob URL will not resolve in a worker even if it's in the window's module graph");