diff options
author | Casper Beyer <caspervonb@pm.me> | 2021-02-02 19:05:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 12:05:46 +0100 |
commit | 6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch) | |
tree | fd94c013a19fcb38954844085821ec1601c20e18 /std/permissions/mod.ts | |
parent | a2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff) |
chore: remove std directory (#9361)
This removes the std folder from the tree.
Various parts of the tests are pretty tightly dependent
on std (47 direct imports and 75 indirect imports, not
counting the cli tests that use them as fixtures) so I've
added std as a submodule for now.
Diffstat (limited to 'std/permissions/mod.ts')
-rw-r--r-- | std/permissions/mod.ts | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/std/permissions/mod.ts b/std/permissions/mod.ts deleted file mode 100644 index 3134589d2..000000000 --- a/std/permissions/mod.ts +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. - -const { PermissionDenied } = Deno.errors; - -function getPermissionString(descriptors: Deno.PermissionDescriptor[]): string { - return descriptors.length - ? ` ${ - descriptors - .map((pd) => { - switch (pd.name) { - case "read": - case "write": - return pd.path - ? `--allow-${pd.name}=${pd.path}` - : `--allow-${pd.name}`; - case "net": - return pd.host - ? `--allow-${pd.name}=${pd.host}` - : `--allow-${pd.name}`; - default: - return `--allow-${pd.name}`; - } - }) - .join("\n ") - }` - : ""; -} - -/** Attempts to grant a set of permissions, resolving with the descriptors of - * the permissions that are granted. - * - * const perms = await grant({ name: "net" }, { name: "read" }); - * if (perms && perms.length === 2) { - * // do something cool that connects to the net and reads files - * } else { - * // notify user of missing permissions - * } - * - * If one of the permissions requires a prompt, the function will attempt to - * prompt for it. The function resolves with all of the granted permissions. */ -export async function grant( - ...descriptors: Deno.PermissionDescriptor[] -): Promise<void | Deno.PermissionDescriptor[]>; -/** Attempts to grant a set of permissions, resolving with the descriptors of - * the permissions that are granted. - * - * const perms = await grant([{ name: "net" }, { name: "read" }]); - * if (perms && perms.length === 2) { - * // do something cool that connects to the net and reads files - * } else { - * // notify user of missing permissions - * } - * - * If one of the permissions requires a prompt, the function will attempt to - * prompt for it. The function resolves with all of the granted permissions. */ -export async function grant( - descriptors: Deno.PermissionDescriptor[], -): Promise<void | Deno.PermissionDescriptor[]>; -export async function grant( - descriptor: Deno.PermissionDescriptor[] | Deno.PermissionDescriptor, - ...descriptors: Deno.PermissionDescriptor[] -): Promise<void | Deno.PermissionDescriptor[]> { - const result: Deno.PermissionDescriptor[] = []; - descriptors = Array.isArray(descriptor) - ? descriptor - : [descriptor, ...descriptors]; - for (const descriptor of descriptors) { - let state = (await Deno.permissions.query(descriptor)).state; - if (state === "prompt") { - state = (await Deno.permissions.request(descriptor)).state; - } - if (state === "granted") { - result.push(descriptor); - } - } - return result.length ? result : undefined; -} - -/** Attempts to grant a set of permissions or rejects. - * - * await grantOrThrow({ name: "env" }, { name: "net" }); - * - * If the permission can be prompted for, the function will attempt to prompt. - * If any of the permissions are denied, the function will reject for the first - * permission that is denied. If all permissions are granted, the function - * will resolve. */ -export async function grantOrThrow( - ...descriptors: Deno.PermissionDescriptor[] -): Promise<void>; -/** Attempts to grant a set of permissions or rejects. - * - * await grantOrThrow([{ name: "env" }, { name: "net" }]); - * - * If the permission can be prompted for, the function will attempt to prompt. - * If any of the permissions are denied, the function will reject mentioning the - * the denied permissions. If all permissions are granted, the function will - * resolve. */ -export async function grantOrThrow( - descriptors: Deno.PermissionDescriptor[], -): Promise<void>; -export async function grantOrThrow( - descriptor: Deno.PermissionDescriptor[] | Deno.PermissionDescriptor, - ...descriptors: Deno.PermissionDescriptor[] -): Promise<void> { - const denied: Deno.PermissionDescriptor[] = []; - descriptors = Array.isArray(descriptor) - ? descriptor - : [descriptor, ...descriptors]; - for (const descriptor of descriptors) { - const { state } = await Deno.permissions.request(descriptor); - if (state !== "granted") { - denied.push(descriptor); - } - } - if (denied.length) { - throw new PermissionDenied( - `The following permissions have not been granted:\n${ - getPermissionString( - denied, - ) - }`, - ); - } -} |