2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2024-01-26 17:46:46 -05:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_query_permission,
|
|
|
|
op_request_permission,
|
|
|
|
op_revoke_permission,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
MapPrototypeGet,
|
|
|
|
MapPrototypeHas,
|
|
|
|
MapPrototypeSet,
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
PromiseResolve,
|
|
|
|
PromiseReject,
|
|
|
|
ReflectHas,
|
|
|
|
SafeArrayIterator,
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeMap,
|
2023-02-07 14:22:46 -05:00
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import { pathFromURL } from "ext:deno_web/00_infra.js";
|
|
|
|
import { Event, EventTarget } from "ext:deno_web/02_event.js";
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef StatusCacheValue
|
|
|
|
* @property {PermissionState} state
|
|
|
|
* @property {PermissionStatus} status
|
2023-08-03 07:19:19 -04:00
|
|
|
* @property {boolean} partial
|
2023-02-07 14:22:46 -05:00
|
|
|
*/
|
|
|
|
|
2024-09-03 05:24:25 -04:00
|
|
|
/** @type {ReadonlyArray<"read" | "write" | "net" | "env" | "sys" | "run" | "ffi">} */
|
2023-02-07 14:22:46 -05:00
|
|
|
const permissionNames = [
|
|
|
|
"read",
|
|
|
|
"write",
|
|
|
|
"net",
|
|
|
|
"env",
|
|
|
|
"sys",
|
|
|
|
"run",
|
|
|
|
"ffi",
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Deno.PermissionDescriptor} desc
|
|
|
|
* @returns {Deno.PermissionState}
|
|
|
|
*/
|
|
|
|
function opQuery(desc) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_query_permission(desc);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Deno.PermissionDescriptor} desc
|
|
|
|
* @returns {Deno.PermissionState}
|
|
|
|
*/
|
|
|
|
function opRevoke(desc) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_revoke_permission(desc);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Deno.PermissionDescriptor} desc
|
|
|
|
* @returns {Deno.PermissionState}
|
|
|
|
*/
|
|
|
|
function opRequest(desc) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_request_permission(desc);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class PermissionStatus extends EventTarget {
|
2023-08-03 07:19:19 -04:00
|
|
|
/** @type {{ state: Deno.PermissionState, partial: boolean }} */
|
|
|
|
#status;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
/** @type {((this: PermissionStatus, event: Event) => any) | null} */
|
|
|
|
onchange = null;
|
|
|
|
|
|
|
|
/** @returns {Deno.PermissionState} */
|
|
|
|
get state() {
|
2023-08-03 07:19:19 -04:00
|
|
|
return this.#status.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {boolean} */
|
|
|
|
get partial() {
|
|
|
|
return this.#status.partial;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-02-24 22:33:09 -05:00
|
|
|
/**
|
2023-08-03 07:19:19 -04:00
|
|
|
* @param {{ state: Deno.PermissionState, partial: boolean }} status
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {unknown} key
|
2021-02-24 22:33:09 -05:00
|
|
|
*/
|
2023-08-03 07:19:19 -04:00
|
|
|
constructor(status = null, key = null) {
|
2023-02-07 14:22:46 -05:00
|
|
|
if (key != illegalConstructorKey) {
|
2024-09-13 05:38:45 -04:00
|
|
|
throw new TypeError("Illegal constructor");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
super();
|
2023-08-03 07:19:19 -04:00
|
|
|
this.#status = status;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-02-24 22:33:09 -05:00
|
|
|
/**
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {Event} event
|
|
|
|
* @returns {boolean}
|
2021-02-24 22:33:09 -05:00
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
dispatchEvent(event) {
|
|
|
|
let dispatched = super.dispatchEvent(event);
|
|
|
|
if (dispatched && this.onchange) {
|
|
|
|
FunctionPrototypeCall(this.onchange, this, event);
|
|
|
|
dispatched = !event.defaultPrevented;
|
2021-02-24 22:33:09 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return dispatched;
|
2021-02-24 22:33:09 -05:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:13:38 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
2023-08-03 07:19:19 -04:00
|
|
|
const object = { state: this.state, onchange: this.onchange };
|
|
|
|
if (this.partial) object.partial = this.partial;
|
2023-11-19 03:13:38 -05:00
|
|
|
return `${this.constructor.name} ${inspect(object, inspectOptions)}`;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {Map<string, StatusCacheValue>} */
|
2023-04-14 16:23:28 -04:00
|
|
|
const statusCache = new SafeMap();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Deno.PermissionDescriptor} desc
|
2023-08-03 07:19:19 -04:00
|
|
|
* @param {{ state: Deno.PermissionState, partial: boolean }} rawStatus
|
2023-02-07 14:22:46 -05:00
|
|
|
* @returns {PermissionStatus}
|
|
|
|
*/
|
2023-08-03 07:19:19 -04:00
|
|
|
function cache(desc, rawStatus) {
|
2023-02-07 14:22:46 -05:00
|
|
|
let { name: key } = desc;
|
|
|
|
if (
|
|
|
|
(desc.name === "read" || desc.name === "write" || desc.name === "ffi") &&
|
|
|
|
ReflectHas(desc, "path")
|
|
|
|
) {
|
|
|
|
key += `-${desc.path}&`;
|
|
|
|
} else if (desc.name === "net" && desc.host) {
|
|
|
|
key += `-${desc.host}&`;
|
|
|
|
} else if (desc.name === "run" && desc.command) {
|
|
|
|
key += `-${desc.command}&`;
|
|
|
|
} else if (desc.name === "env" && desc.variable) {
|
|
|
|
key += `-${desc.variable}&`;
|
|
|
|
} else if (desc.name === "sys" && desc.kind) {
|
|
|
|
key += `-${desc.kind}&`;
|
|
|
|
} else {
|
|
|
|
key += "$";
|
|
|
|
}
|
|
|
|
if (MapPrototypeHas(statusCache, key)) {
|
2023-08-03 07:19:19 -04:00
|
|
|
const cachedObj = MapPrototypeGet(statusCache, key);
|
|
|
|
if (
|
|
|
|
cachedObj.state !== rawStatus.state ||
|
|
|
|
cachedObj.partial !== rawStatus.partial
|
|
|
|
) {
|
|
|
|
cachedObj.state = rawStatus.state;
|
|
|
|
cachedObj.partial = rawStatus.partial;
|
|
|
|
cachedObj.status.dispatchEvent(
|
|
|
|
new Event("change", { cancelable: false }),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
return cachedObj.status;
|
2021-02-24 22:33:09 -05:00
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
/** @type {{ state: Deno.PermissionState, partial: boolean, status?: PermissionStatus }} */
|
|
|
|
const obj = rawStatus;
|
|
|
|
obj.status = new PermissionStatus(obj, illegalConstructorKey);
|
|
|
|
MapPrototypeSet(statusCache, key, obj);
|
|
|
|
return obj.status;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {unknown} desc
|
|
|
|
* @returns {desc is Deno.PermissionDescriptor}
|
|
|
|
*/
|
|
|
|
function isValidDescriptor(desc) {
|
|
|
|
return typeof desc === "object" && desc !== null &&
|
|
|
|
ArrayPrototypeIncludes(permissionNames, desc.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Deno.PermissionDescriptor} desc
|
|
|
|
* @returns {desc is Deno.PermissionDescriptor}
|
|
|
|
*/
|
|
|
|
function formDescriptor(desc) {
|
|
|
|
if (
|
|
|
|
desc.name === "read" || desc.name === "write" || desc.name === "ffi"
|
|
|
|
) {
|
|
|
|
desc.path = pathFromURL(desc.path);
|
|
|
|
} else if (desc.name === "run") {
|
|
|
|
desc.command = pathFromURL(desc.command);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Permissions {
|
|
|
|
constructor(key = null) {
|
|
|
|
if (key != illegalConstructorKey) {
|
2024-09-13 05:38:45 -04:00
|
|
|
throw new TypeError("Illegal constructor");
|
2023-01-03 15:50:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
query(desc) {
|
|
|
|
try {
|
|
|
|
return PromiseResolve(this.querySync(desc));
|
|
|
|
} catch (error) {
|
|
|
|
return PromiseReject(error);
|
2020-09-19 17:30:59 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-19 17:30:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
querySync(desc) {
|
|
|
|
if (!isValidDescriptor(desc)) {
|
|
|
|
throw new TypeError(
|
2024-09-13 05:38:45 -04:00
|
|
|
`The provided value "${desc?.name}" is not a valid permission name`,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2023-01-24 18:42:44 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
formDescriptor(desc);
|
2021-08-06 09:04:00 -04:00
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
const status = opQuery(desc);
|
|
|
|
return cache(desc, status);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-06 09:04:00 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
revoke(desc) {
|
|
|
|
try {
|
|
|
|
return PromiseResolve(this.revokeSync(desc));
|
|
|
|
} catch (error) {
|
|
|
|
return PromiseReject(error);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
revokeSync(desc) {
|
|
|
|
if (!isValidDescriptor(desc)) {
|
|
|
|
throw new TypeError(
|
2024-09-13 05:38:45 -04:00
|
|
|
`The provided value "${desc?.name}" is not a valid permission name`,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2023-01-24 18:42:44 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
formDescriptor(desc);
|
2021-08-06 09:04:00 -04:00
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
const status = opRevoke(desc);
|
|
|
|
return cache(desc, status);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-06 09:04:00 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
request(desc) {
|
|
|
|
try {
|
|
|
|
return PromiseResolve(this.requestSync(desc));
|
|
|
|
} catch (error) {
|
|
|
|
return PromiseReject(error);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
requestSync(desc) {
|
|
|
|
if (!isValidDescriptor(desc)) {
|
|
|
|
throw new TypeError(
|
|
|
|
`The provided value "${desc?.name}" is not a valid permission name.`,
|
|
|
|
);
|
2023-01-24 18:42:44 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
formDescriptor(desc);
|
2021-08-06 09:04:00 -04:00
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
const status = opRequest(desc);
|
|
|
|
return cache(desc, status);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const permissions = new Permissions(illegalConstructorKey);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** Converts all file URLs in FS allowlists to paths. */
|
|
|
|
function serializePermissions(permissions) {
|
|
|
|
if (typeof permissions == "object" && permissions != null) {
|
2024-05-22 18:03:35 -04:00
|
|
|
const serializedPermissions = { __proto__: null };
|
2023-02-07 14:22:46 -05:00
|
|
|
for (
|
|
|
|
const key of new SafeArrayIterator(["read", "write", "run", "ffi"])
|
|
|
|
) {
|
|
|
|
if (ArrayIsArray(permissions[key])) {
|
|
|
|
serializedPermissions[key] = ArrayPrototypeMap(
|
|
|
|
permissions[key],
|
|
|
|
(path) => pathFromURL(path),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
serializedPermissions[key] = permissions[key];
|
2021-10-13 13:04:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
for (
|
2024-09-03 05:24:25 -04:00
|
|
|
const key of new SafeArrayIterator(["env", "net", "sys"])
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
if (ArrayIsArray(permissions[key])) {
|
|
|
|
serializedPermissions[key] = ArrayPrototypeSlice(permissions[key]);
|
|
|
|
} else {
|
|
|
|
serializedPermissions[key] = permissions[key];
|
2021-10-13 13:04:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return serializedPermissions;
|
2021-10-13 13:04:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return permissions;
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { Permissions, permissions, PermissionStatus, serializePermissions };
|