All files / testing / permissions.ts

100.00% Branches 11/11
100.00% Lines 23/23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
x53
x64
x64
x66
x66
x64
x700
x118
x121
x121
x121
x118
x120
x120
x120
x118
x120
x120
x120
x118
x70
x64
x64





















/** Compute deno permissions flag from a `Deno.PermissionOptions`-like object. */
export function flags(permissions: Deno.PermissionOptions | boolean | null): string {
  const flags = []
  if ((permissions === true) || (permissions === "inherit")) {
    flags.push("--allow-all")
  }
  if ((typeof permissions === "object") && permissions) {
    for (const type of ["env", "ffi", "import", "net", "read", "run", "sys", "write"] as const) {
      if ((permissions[type] === true) || (permissions[type] === "inherit")) {
        flags.push(`--allow-${type}`)
        continue
      }
      if (permissions[type] === false) {
        flags.push(`--deny-${type}`)
        continue
      }
      if (Array.isArray(permissions[type])) {
        flags.push(`--allow-${type}=${permissions[type].map(String).join(",")}`)
        continue
      }
    }
  }
  return flags.join(" ")
}