summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/scripting-1/the-script-element/module/dynamic-import/blob-url.any.js
blob: 0719a18bf16674f83e397b0e14b40675255a0e8a (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
61
62
63
64
65
66
// META: global=window,dedicatedworker,sharedworker,dedicatedworker-module,sharedworker-module

function objectUrlFromModule(module) {
  const blob = new Blob([module], { type: "text/javascript" });
  return URL.createObjectURL(blob);
}

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

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

  const module = await import(moduleBlobUrl);
  assert_equals(module.foo, "bar");
}, "Blob URLs are supported in dynamic imports");

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

  const module1 = await import(moduleBlobUrl);
  const module2 = await import(moduleBlobUrl);
  assert_equals(module1, module2);
}, "Identical blob URLs resolve to the same module");

promise_test(async (t) => {
  const moduleBlob = new Blob([moduleText], { type: "text/javascript" });
  const moduleBlobUrl1 = URL.createObjectURL(moduleBlob);
  const moduleBlobUrl2 = URL.createObjectURL(moduleBlob);
  t.add_cleanup(() => {
    URL.revokeObjectURL(moduleBlobUrl1);
    URL.revokeObjectURL(moduleBlobUrl2);
  });

  const module1 = await import(moduleBlobUrl1);
  const module2 = await import(moduleBlobUrl2);
  assert_not_equals(module1, module2);
}, "Different blob URLs pointing to the same blob resolve to different modules");

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

  await promise_rejects_js(t, TypeError, import(moduleBlobUrl));
}, "A revoked blob URL will not resolve");

promise_test(async () => {
  const moduleBlobUrl = objectUrlFromModule(moduleText);
  const module1 = await import(moduleBlobUrl);

  URL.revokeObjectURL(moduleBlobUrl);

  const module2 = await import(moduleBlobUrl);
  assert_equals(module1, module2);
}, "A revoked blob URL will resolve if it's already in the module graph");

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

  const importPromise = import(moduleBlobUrl);
  URL.revokeObjectURL(moduleBlobUrl);

  const module = await importPromise;
  assert_equals(module.foo, "bar");
}, "Revoking a blob URL immediately after calling import will not fail");