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
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as permissionsOps from "../ops/permissions.ts";
import { EventTargetImpl as EventTarget } from "./event_target.ts";
const permissionNames = [
"read",
"write",
"net",
"env",
"run",
"plugin",
"hrtime",
] as const;
type PermissionName = typeof permissionNames[number];
interface RunPermissionDescriptor {
name: "run";
}
interface ReadPermissionDescriptor {
name: "read";
path?: string;
}
interface WritePermissionDescriptor {
name: "write";
path?: string;
}
interface NetPermissionDescriptor {
name: "net";
url?: string;
}
interface EnvPermissionDescriptor {
name: "env";
}
interface PluginPermissionDescriptor {
name: "plugin";
}
interface HrtimePermissionDescriptor {
name: "hrtime";
}
type DenoPermissionDescriptor =
| RunPermissionDescriptor
| ReadPermissionDescriptor
| WritePermissionDescriptor
| NetPermissionDescriptor
| EnvPermissionDescriptor
| PluginPermissionDescriptor
| HrtimePermissionDescriptor;
interface StatusCacheValue {
state: PermissionState;
status: PermissionStatusImpl;
}
export class PermissionStatusImpl extends EventTarget
implements PermissionStatus {
#state: { state: PermissionState };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onchange: ((this: PermissionStatus, event: Event) => any) | null = null;
get state(): PermissionState {
return this.#state.state;
}
constructor(state: { state: PermissionState }) {
super();
this.#state = state;
}
dispatchEvent(event: Event): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let dispatched = super.dispatchEvent(event as any);
if (dispatched && this.onchange) {
this.onchange.call(this, event);
dispatched = !event.defaultPrevented;
}
return dispatched;
}
get [Symbol.toStringTag](): string {
return "PermissionStatus";
}
}
/** A cache of `PermissionStatus` objects and their last known state. */
const statusCache = new Map<string, StatusCacheValue>();
/** Cache the state of a descriptor and return its `PermissionStatus`. */
function cache(
desc: DenoPermissionDescriptor,
state: PermissionState
): PermissionStatusImpl {
let key = desc.name;
if ((desc.name === "read" || desc.name === "write") && desc.path) {
key += `-${desc.path}`;
} else if (desc.name === "net" && desc.url) {
key += `-${desc.url}`;
}
if (statusCache.has(key)) {
const status = statusCache.get(key)!;
if (status.state !== state) {
status.state = state;
status.status.dispatchEvent(new Event("change", { cancelable: false }));
}
return status.status;
}
const status: { state: PermissionState; status?: PermissionStatusImpl } = {
state,
};
status.status = new PermissionStatusImpl(status);
statusCache.set(key, status as StatusCacheValue);
return status.status;
}
function isValidDescriptor(
desc: PermissionDescriptor | DenoPermissionDescriptor
): desc is DenoPermissionDescriptor {
return permissionNames.includes(desc.name as PermissionName);
}
export class PermissionsImpl implements Permissions {
query(
desc: PermissionDescriptor | DenoPermissionDescriptor
): Promise<PermissionStatus> {
if (!isValidDescriptor(desc)) {
return Promise.reject(
new TypeError(
`The provided value "${desc.name}" is not a valid permission name.`
)
);
}
const state = permissionsOps.query(desc);
return Promise.resolve(cache(desc, state) as PermissionStatus);
}
revoke(
desc: PermissionDescriptor | DenoPermissionDescriptor
): Promise<PermissionStatus> {
if (!isValidDescriptor(desc)) {
return Promise.reject(
new TypeError(
`The provided value "${desc.name}" is not a valid permission name.`
)
);
}
const state = permissionsOps.revoke(desc);
return Promise.resolve(cache(desc, state) as PermissionStatus);
}
request(
desc: PermissionDescriptor | DenoPermissionDescriptor
): Promise<PermissionStatus> {
if (!isValidDescriptor(desc)) {
return Promise.reject(
new TypeError(
`The provided value "${desc.name}" is not a valid permission name.`
)
);
}
const state = permissionsOps.request(desc);
return Promise.resolve(cache(desc, state) as PermissionStatus);
}
}
Object.defineProperty(PermissionStatusImpl, "name", {
value: "PermissionStatus",
configurable: true,
});
Object.defineProperty(PermissionsImpl, "name", {
value: "Permissions",
configurable: true,
});
|