deno.land / std@0.224.0 / permissions / mod.ts

View Documentation
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license./** * Helpers for interacting with Deno's permissions system. * * @deprecated Use the * {@link https://deno.land/api?s=Deno.Permissions | Deno Permissions API} * directly instead. This module will be removed once the Standard Library * migrates to {@link https://jsr.io/ | JSR}. * * @module */
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. * * ```ts * import { grant } from "https://deno.land/std@$STD_VERSION/permissions/mod.ts"; * 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. * * @deprecated Use the * {@link https://deno.land/api?s=Deno.Permissions | Deno Permissions API} * directly instead. This module will be removed once the Standard Library * migrates to {@link https://jsr.io/ | JSR}. */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. * * ```ts * import { grant } from "https://deno.land/std@$STD_VERSION/permissions/mod.ts"; * 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. * * @deprecated Use the * {@link https://deno.land/api?s=Deno.Permissions | Deno Permissions API} * directly instead. This module will be removed once the Standard Library * migrates to {@link https://jsr.io/ | JSR}. */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. * * ```ts * import { grantOrThrow } from "https://deno.land/std@$STD_VERSION/permissions/mod.ts"; * 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. * * @deprecated Use the * {@link https://deno.land/api?s=Deno.Permissions | Deno Permissions API} * directly instead. This module will be removed once the Standard Library * migrates to {@link https://jsr.io/ | JSR}. */export async function grantOrThrow( ...descriptors: Deno.PermissionDescriptor[]): Promise<void>;/** Attempts to grant a set of permissions or rejects. * * ```ts * import { grantOrThrow } from "https://deno.land/std@$STD_VERSION/permissions/mod.ts"; * 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. * * @deprecated Use the * {@link https://deno.land/api?s=Deno.Permissions | Deno Permissions API} * directly instead. This module will be removed once the Standard Library * migrates to {@link https://jsr.io/ | JSR}. */export async function grantOrThrow( descriptors: Deno.PermissionDescriptor[],): Promise<void>;export async function grantOrThrow( descriptor: Deno.PermissionDescriptor[] | Deno.PermissionDescriptor, ...descriptors: Deno.PermissionDescriptor[]) { 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, ) }`, ); }}
std

Version Info

Tagged at
6 months ago