diff options
Diffstat (limited to 'js/permissions.ts')
-rw-r--r-- | js/permissions.ts | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/js/permissions.ts b/js/permissions.ts index bc969f3a8..822ae8cbd 100644 --- a/js/permissions.ts +++ b/js/permissions.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as dispatch from "./dispatch"; -import { sendSync } from "./dispatch_json"; +import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; +import { assert } from "./util"; /** Permissions as granted by the caller */ export interface Permissions { @@ -15,6 +15,23 @@ export interface Permissions { export type Permission = keyof Permissions; +function getReq(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { + const builder = flatbuffers.createBuilder(); + const inner = msg.Permissions.createPermissions(builder); + return [builder, msg.Any.Permissions, inner]; +} + +function createPermissions(inner: msg.PermissionsRes): Permissions { + return { + read: inner.read(), + write: inner.write(), + net: inner.net(), + env: inner.env(), + run: inner.run(), + hrtime: inner.hrtime() + }; +} + /** Inspect granted permissions for the current program. * * if (Deno.permissions().read) { @@ -23,7 +40,24 @@ export type Permission = keyof Permissions; * } */ export function permissions(): Permissions { - return sendSync(dispatch.OP_PERMISSIONS) as Permissions; + const baseRes = sendSync(...getReq())!; + assert(msg.Any.PermissionsRes === baseRes.innerType()); + const res = new msg.PermissionsRes(); + assert(baseRes.inner(res) != null); + // TypeScript cannot track assertion above, therefore not null assertion + return createPermissions(res); +} + +function revokeReq( + permission: string +): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { + const builder = flatbuffers.createBuilder(); + const permission_ = builder.createString(permission); + const inner = msg.PermissionRevoke.createPermissionRevoke( + builder, + permission_ + ); + return [builder, msg.Any.PermissionRevoke, inner]; } /** Revoke a permission. When the permission was already revoked nothing changes @@ -35,5 +69,5 @@ export function permissions(): Permissions { * Deno.readFile("example.test"); // -> error or permission prompt */ export function revokePermission(permission: Permission): void { - sendSync(dispatch.OP_REVOKE_PERMISSION, { permission }); + sendSync(...revokeReq(permission)); } |