2019-03-04 11:04:19 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as dispatch from "./dispatch.ts";
|
|
|
|
import { sendSync } from "./dispatch_json.ts";
|
2019-03-04 11:04:19 -05:00
|
|
|
|
|
|
|
/** Permissions as granted by the caller */
|
2019-03-09 12:30:38 -05:00
|
|
|
export interface Permissions {
|
2019-03-04 11:04:19 -05:00
|
|
|
read: boolean;
|
|
|
|
write: boolean;
|
|
|
|
net: boolean;
|
|
|
|
env: boolean;
|
|
|
|
run: boolean;
|
2019-05-23 12:28:29 -04:00
|
|
|
hrtime: boolean;
|
2019-03-04 11:04:19 -05:00
|
|
|
// NOTE: Keep in sync with src/permissions.rs
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
2019-03-04 11:04:19 -05:00
|
|
|
|
|
|
|
export type Permission = keyof Permissions;
|
|
|
|
|
|
|
|
/** Inspect granted permissions for the current program.
|
|
|
|
*
|
|
|
|
* if (Deno.permissions().read) {
|
|
|
|
* const file = await Deno.readFile("example.test");
|
|
|
|
* // ...
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
export function permissions(): Permissions {
|
2019-08-26 08:50:21 -04:00
|
|
|
return sendSync(dispatch.OP_PERMISSIONS) as Permissions;
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
|
|
|
|
2019-03-04 11:04:19 -05:00
|
|
|
/** Revoke a permission. When the permission was already revoked nothing changes
|
|
|
|
*
|
|
|
|
* if (Deno.permissions().read) {
|
|
|
|
* const file = await Deno.readFile("example.test");
|
|
|
|
* Deno.revokePermission('read');
|
|
|
|
* }
|
|
|
|
* Deno.readFile("example.test"); // -> error or permission prompt
|
|
|
|
*/
|
|
|
|
export function revokePermission(permission: Permission): void {
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSync(dispatch.OP_REVOKE_PERMISSION, { permission });
|
2019-03-04 11:04:19 -05:00
|
|
|
}
|