In some cases, ensuring a fault-tolerant program requires a way to interact with the permission system at runtime.

    On the CLI, read permission for /foo/bar is represented as --allow-read=/foo/bar. In runtime JS, it is represented as the following:

    Other examples:

    1. // Global write permission.
    2. const desc1 = { name: "write" } as const;
    3. // Write permission to `$PWD/foo/bar`.
    4. const desc2 = { name: "write", path: "foo/bar" } as const;
    5. // Global net permission.
    6. const desc3 = { name: "net" } as const;
    7. // Net permission to 127.0.0.1:8000.
    8. const desc4 = { name: "net", host: "127.0.0.1:8000" } as const;
    9. // High-resolution time permission.
    10. const desc5 = { name: "hrtime" } as const;

    Query permissions

    Check, by descriptor, if a permission is granted or not.

    1. const desc1 = { name: "read", path: "/foo" } as const;
    2. console.log(await Deno.permissions.query(desc1));
    3. // PermissionStatus { state: "granted" }
    4. const desc2 = { name: "read", path: "/foo/bar" } as const;
    5. console.log(await Deno.permissions.query(desc2));
    6. // PermissionStatus { state: "granted" }
    7. const desc3 = { name: "read", path: "/bar" } as const;
    8. console.log(await Deno.permissions.query(desc3));
    9. // PermissionStatus { state: "prompt" }

    A permission state can be either “granted”, “prompt” or “denied”. Permissions which have been granted from the CLI will query to { state: "granted" }. Those which have not been granted query to { state: "prompt" } by default, while { state: "denied" } reserved for those which have been explicitly refused. This will come up in Request permissions.

    Permission strength

    We can also say that desc1 is stronger than desc2. This means that for any set of CLI-granted permissions:

    1. If desc1 queries to { state: "granted" } then so must desc2.
    2. If desc2 queries to { state: "denied" } then so must desc1.

    More examples:

    Request an ungranted permission from the user via CLI prompt.

    1. // deno run main.ts
    2. const desc1 = { name: "read", path: "/foo" } as const;
    3. const status1 = await Deno.permissions.request(desc1);
    4. // ⚠️ Deno requests read access to "/foo". Grant? [y/n (y = yes allow, n = no deny)] y
    5. console.log(status1);
    6. const desc2 = { name: "read", path: "/bar" } as const;
    7. const status2 = await Deno.permissions.request(desc2);
    8. // ⚠️ Deno requests read access to "/bar". Grant? [y/n (y = yes allow, n = no deny)] n
    9. console.log(status2);
    10. // PermissionStatus { state: "denied" }

    If the current permission state is “prompt”, a prompt will appear on the user’s terminal asking them if they would like to grant the request. The request for desc1 was granted so its new status is returned and execution will continue as if --allow-read=/foo was specified on the CLI. The request for desc2 was denied so its permission state is downgraded from “prompt” to “denied”.

    If the current permission state is already either “granted” or “denied”, the request will behave like a query and just return the current status. This prevents prompts both for already granted permissions and previously denied requests.

    Revoke permissions

    1. // deno run --allow-read=/foo main.ts
    2. const desc = { name: "read", path: "/foo" } as const;
    3. console.log(await Deno.permissions.revoke(desc));
    4. // PermissionStatus { state: "prompt" }

    However, what happens when you try to revoke a permission which is partial to one granted on the CLI?

    It was not revoked.

    To understand this behaviour, imagine that Deno stores an internal set of explicitly granted permission descriptors. Specifying --allow-read=/foo,/bar on the CLI initializes this set to:

    1. [
    2. { name: "read", path: "/foo" },
    3. { name: "read", path: "/bar" },
    4. ];

    Granting a runtime request for { name: "write", path: "/foo" } updates the set to:

    1. [
    2. { name: "read", path: "/foo" },
    3. { name: "read", path: "/bar" },
    4. { name: "write", path: "/foo" },
    5. ];

    Deno’s permission revocation algorithm works by removing every element from this set which the argument permission descriptor is stronger than. So to ensure desc is no longer granted, pass an argument descriptor stronger than whichever explicitly granted permission descriptor is stronger than .