mirror of
https://github.com/denoland/deno.git
synced 2025-10-02 23:24:37 +00:00
Use web standard Permissions API (#3200)
This commit is contained in:
parent
2598f9c68d
commit
efd7e78af3
15 changed files with 362 additions and 306 deletions
|
@ -29,11 +29,34 @@ interface TestPermissions {
|
|||
hrtime?: boolean;
|
||||
}
|
||||
|
||||
const processPerms = Deno.permissions();
|
||||
export interface Permissions {
|
||||
read: boolean;
|
||||
write: boolean;
|
||||
net: boolean;
|
||||
env: boolean;
|
||||
run: boolean;
|
||||
hrtime: boolean;
|
||||
}
|
||||
|
||||
const isGranted = async (name: Deno.PermissionName): Promise<boolean> =>
|
||||
(await Deno.permissions.query({ name })).state === "granted";
|
||||
|
||||
async function getProcessPermissions(): Promise<Permissions> {
|
||||
return {
|
||||
run: await isGranted("run"),
|
||||
read: await isGranted("read"),
|
||||
write: await isGranted("write"),
|
||||
net: await isGranted("net"),
|
||||
env: await isGranted("env"),
|
||||
hrtime: await isGranted("hrtime")
|
||||
};
|
||||
}
|
||||
|
||||
const processPerms = await getProcessPermissions();
|
||||
|
||||
function permissionsMatch(
|
||||
processPerms: Deno.Permissions,
|
||||
requiredPerms: Deno.Permissions
|
||||
processPerms: Permissions,
|
||||
requiredPerms: Permissions
|
||||
): boolean {
|
||||
for (const permName in processPerms) {
|
||||
if (processPerms[permName] !== requiredPerms[permName]) {
|
||||
|
@ -44,9 +67,9 @@ function permissionsMatch(
|
|||
return true;
|
||||
}
|
||||
|
||||
export const permissionCombinations: Map<string, Deno.Permissions> = new Map();
|
||||
export const permissionCombinations: Map<string, Permissions> = new Map();
|
||||
|
||||
function permToString(perms: Deno.Permissions): string {
|
||||
function permToString(perms: Permissions): string {
|
||||
const r = perms.read ? 1 : 0;
|
||||
const w = perms.write ? 1 : 0;
|
||||
const n = perms.net ? 1 : 0;
|
||||
|
@ -56,14 +79,14 @@ function permToString(perms: Deno.Permissions): string {
|
|||
return `permR${r}W${w}N${n}E${e}U${u}H${h}`;
|
||||
}
|
||||
|
||||
function registerPermCombination(perms: Deno.Permissions): void {
|
||||
function registerPermCombination(perms: Permissions): void {
|
||||
const key = permToString(perms);
|
||||
if (!permissionCombinations.has(key)) {
|
||||
permissionCombinations.set(key, perms);
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeTestPermissions(perms: TestPermissions): Deno.Permissions {
|
||||
function normalizeTestPermissions(perms: TestPermissions): Permissions {
|
||||
return {
|
||||
read: !!perms.read,
|
||||
write: !!perms.write,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue