diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-03-09 15:18:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-09 15:18:02 +0100 |
commit | 886f330ec8110a3eb72feb14353f353962179d2e (patch) | |
tree | f6b458afac30a18d5120396bed9b1e63ae6d8529 /cli/js/ops | |
parent | 1b6fc87b7188118896f797e5f0dab309775def71 (diff) |
reorg: move JS ops implementations to cli/js/ops/, part 2 (#4283)
Following JS ops were moved to separate files in cli/js/ops directory:
- io
- process
- worker_host
- web_worker
- plugins
- timers
- signal
- permissions
Diffstat (limited to 'cli/js/ops')
-rw-r--r-- | cli/js/ops/io.ts | 105 | ||||
-rw-r--r-- | cli/js/ops/permissions.ts | 24 | ||||
-rw-r--r-- | cli/js/ops/plugins.ts | 12 | ||||
-rw-r--r-- | cli/js/ops/process.ts | 47 | ||||
-rw-r--r-- | cli/js/ops/signal.ts | 14 | ||||
-rw-r--r-- | cli/js/ops/timers.ts | 10 | ||||
-rw-r--r-- | cli/js/ops/web_worker.ts | 11 | ||||
-rw-r--r-- | cli/js/ops/worker_host.ts | 29 |
8 files changed, 252 insertions, 0 deletions
diff --git a/cli/js/ops/io.ts b/cli/js/ops/io.ts new file mode 100644 index 000000000..9f53707b7 --- /dev/null +++ b/cli/js/ops/io.ts @@ -0,0 +1,105 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts"; +import { EOF } from "../io.ts"; +// TODO(bartlomieju): remove this import and maybe lazy-initialize +// OPS_CACHE that belongs only to this module +import { OPS_CACHE } from "../runtime.ts"; + +// This is done because read/write are extremely performance sensitive. +let OP_READ = -1; +let OP_WRITE = -1; + +/** Synchronously read from a file ID into an array buffer. + * + * Returns `number | EOF` for the operation. + * + * const file = Deno.openSync("/foo/bar.txt"); + * const buf = new Uint8Array(100); + * const nread = Deno.readSync(file.rid, buf); + * const text = new TextDecoder().decode(buf); + */ +export function readSync(rid: number, p: Uint8Array): number | EOF { + if (p.length == 0) { + return 0; + } + if (OP_READ < 0) { + OP_READ = OPS_CACHE["op_read"]; + } + const nread = sendSyncMinimal(OP_READ, rid, p); + if (nread < 0) { + throw new Error("read error"); + } else if (nread == 0) { + return EOF; + } else { + return nread; + } +} + +/** Read from a resource ID into an array buffer. + * + * Resolves to the `number | EOF` for the operation. + * + * const file = await Deno.open("/foo/bar.txt"); + * const buf = new Uint8Array(100); + * const nread = await Deno.read(file.rid, buf); + * const text = new TextDecoder().decode(buf); + */ +export async function read(rid: number, p: Uint8Array): Promise<number | EOF> { + if (p.length == 0) { + return 0; + } + if (OP_READ < 0) { + OP_READ = OPS_CACHE["op_read"]; + } + const nread = await sendAsyncMinimal(OP_READ, rid, p); + if (nread < 0) { + throw new Error("read error"); + } else if (nread == 0) { + return EOF; + } else { + return nread; + } +} + +/** Synchronously write to the resource ID the contents of the array buffer. + * + * Resolves to the number of bytes written. + * + * const encoder = new TextEncoder(); + * const data = encoder.encode("Hello world\n"); + * const file = Deno.openSync("/foo/bar.txt", {create: true, write: true}); + * Deno.writeSync(file.rid, data); + */ +export function writeSync(rid: number, p: Uint8Array): number { + if (OP_WRITE < 0) { + OP_WRITE = OPS_CACHE["op_write"]; + } + const result = sendSyncMinimal(OP_WRITE, rid, p); + if (result < 0) { + throw new Error("write error"); + } else { + return result; + } +} + +/** Write to the resource ID the contents of the array buffer. + * + * Resolves to the number of bytes written. + * + * const encoder = new TextEncoder(); + * const data = encoder.encode("Hello world\n"); + * const file = await Deno.open("/foo/bar.txt", {create: true, write: true}); + * await Deno.write(file.rid, data); + */ +export async function write(rid: number, p: Uint8Array): Promise<number> { + if (OP_WRITE < 0) { + OP_WRITE = OPS_CACHE["op_write"]; + } + const result = await sendAsyncMinimal(OP_WRITE, rid, p); + if (result < 0) { + throw new Error("write error"); + } else { + return result; + } +} diff --git a/cli/js/ops/permissions.ts b/cli/js/ops/permissions.ts new file mode 100644 index 000000000..783b1b297 --- /dev/null +++ b/cli/js/ops/permissions.ts @@ -0,0 +1,24 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync } from "./dispatch_json.ts"; + +// TODO(bartlomieju): duplicated in `cli/js/permissions.ts` as +// `PermissionState +export type PermissionResponse = "granted" | "denied" | "prompt"; + +interface PermissionRequest { + name: string; + url?: string; + path?: string; +} + +export function query(desc: PermissionRequest): PermissionResponse { + return sendSync("op_query_permission", desc).state; +} + +export function revoke(desc: PermissionRequest): PermissionResponse { + return sendSync("op_revoke_permission", desc).state; +} + +export function request(desc: PermissionRequest): PermissionResponse { + return sendSync("op_request_permission", desc).state; +} diff --git a/cli/js/ops/plugins.ts b/cli/js/ops/plugins.ts new file mode 100644 index 000000000..878ea1c66 --- /dev/null +++ b/cli/js/ops/plugins.ts @@ -0,0 +1,12 @@ +import { sendSync } from "./dispatch_json.ts"; + +interface OpenPluginResponse { + rid: number; + ops: { + [name: string]: number; + }; +} + +export function openPlugin(filename: string): OpenPluginResponse { + return sendSync("op_open_plugin", { filename }); +} diff --git a/cli/js/ops/process.ts b/cli/js/ops/process.ts new file mode 100644 index 000000000..31ce0b33e --- /dev/null +++ b/cli/js/ops/process.ts @@ -0,0 +1,47 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "./dispatch_json.ts"; +import { assert } from "../util.ts"; + +/** Send a signal to process under given PID. Unix only at this moment. + * If pid is negative, the signal will be sent to the process group identified + * by -pid. + * Requires the `--allow-run` flag. + */ +export function kill(pid: number, signo: number): void { + sendSync("op_kill", { pid, signo }); +} + +interface RunStatusResponse { + gotSignal: boolean; + exitCode: number; + exitSignal: number; +} + +export async function runStatus(rid: number): Promise<RunStatusResponse> { + return await sendAsync("op_run_status", { rid }); +} + +interface RunRequest { + args: string[]; + cwd?: string; + env?: Array<[string, string]>; + stdin: string; + stdout: string; + stderr: string; + stdinRid: number; + stdoutRid: number; + stderrRid: number; +} + +interface RunResponse { + rid: number; + pid: number; + stdinRid: number | null; + stdoutRid: number | null; + stderrRid: number | null; +} + +export function run(request: RunRequest): RunResponse { + assert(request.args.length > 0); + return sendSync("op_run", request); +} diff --git a/cli/js/ops/signal.ts b/cli/js/ops/signal.ts new file mode 100644 index 000000000..7f9304a82 --- /dev/null +++ b/cli/js/ops/signal.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "./dispatch_json.ts"; + +export function bindSignal(signo: number): { rid: number } { + return sendSync("op_signal_bind", { signo }); +} + +export async function pollSignal(rid: number): Promise<{ done: boolean }> { + return await sendAsync("op_signal_poll", { rid }); +} + +export function unbindSignal(rid: number): void { + sendSync("op_signal_unbind", { rid }); +} diff --git a/cli/js/ops/timers.ts b/cli/js/ops/timers.ts new file mode 100644 index 000000000..bc7fe6453 --- /dev/null +++ b/cli/js/ops/timers.ts @@ -0,0 +1,10 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "./dispatch_json.ts"; + +export function stopGlobalTimer(): void { + sendSync("op_global_timer_stop"); +} + +export async function startGlobalTimer(timeout: number): Promise<void> { + await sendAsync("op_global_timer", { timeout }); +} diff --git a/cli/js/ops/web_worker.ts b/cli/js/ops/web_worker.ts new file mode 100644 index 000000000..329323e2e --- /dev/null +++ b/cli/js/ops/web_worker.ts @@ -0,0 +1,11 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { sendSync } from "./dispatch_json.ts"; + +export function postMessage(data: Uint8Array): void { + sendSync("op_worker_post_message", {}, data); +} + +export function close(): void { + sendSync("op_worker_close"); +} diff --git a/cli/js/ops/worker_host.ts b/cli/js/ops/worker_host.ts new file mode 100644 index 000000000..a409d2c77 --- /dev/null +++ b/cli/js/ops/worker_host.ts @@ -0,0 +1,29 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { sendAsync, sendSync } from "./dispatch_json.ts"; + +export function createWorker( + specifier: string, + hasSourceCode: boolean, + sourceCode: string, + name?: string +): { id: number } { + return sendSync("op_create_worker", { + specifier, + hasSourceCode, + sourceCode, + name + }); +} + +export function hostTerminateWorker(id: number): void { + sendSync("op_host_terminate_worker", { id }); +} + +export function hostPostMessage(id: number, data: Uint8Array): void { + sendSync("op_host_post_message", { id }, data); +} + +export async function hostGetMessage(id: number): Promise<any> { + return await sendAsync("op_host_get_message", { id }); +} |