summaryrefslogtreecommitdiff
path: root/cli/tests/workers/parent_read_check_granular_worker.js
blob: 474b8a61bb6b43d96448b8c01e838279b2889279 (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
import { fromFileUrl } from "../../../test_util/std/path/mod.ts";

const worker = new Worker(
  new URL("./read_check_granular_worker.js", import.meta.url).href,
  {
    type: "module",
    deno: {
      namespace: true,
      permissions: {
        read: [],
      },
    },
  },
);

let received = 0;
const messages = [];

worker.onmessage = ({ data: childResponse }) => {
  received++;
  postMessage({
    childHasPermission: childResponse.hasPermission,
    index: childResponse.index,
    parentHasPermission: messages[childResponse.index],
  });
  if (received === messages.length) {
    worker.terminate();
  }
};

onmessage = async ({ data }) => {
  const { state } = await Deno.permissions.query({
    name: "read",
    path: fromFileUrl(new URL(data.route, import.meta.url)),
  });

  messages[data.index] = state === "granted";

  worker.postMessage({
    index: data.index,
    route: data.route,
  });
};