summaryrefslogtreecommitdiff
path: root/js/process.ts
diff options
context:
space:
mode:
Diffstat (limited to 'js/process.ts')
-rw-r--r--js/process.ts132
1 files changed, 71 insertions, 61 deletions
diff --git a/js/process.ts b/js/process.ts
index dd4f70103..b2b6d4734 100644
--- a/js/process.ts
+++ b/js/process.ts
@@ -1,6 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { sendSync, sendAsync } from "./dispatch_json";
-import * as dispatch from "./dispatch";
+import { sendSync, sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers";
+
import { File, close } from "./files";
import { ReadCloser, WriteCloser } from "./io";
import { readAll } from "./buffer";
@@ -31,22 +31,21 @@ export interface RunOptions {
stdin?: ProcessStdio | number;
}
-interface RunStatusResponse {
- gotSignal: boolean;
- exitCode: number;
- exitSignal: number;
-}
-
async function runStatus(rid: number): Promise<ProcessStatus> {
- const res = (await sendAsync(dispatch.OP_RUN_STATUS, {
- rid
- })) as RunStatusResponse;
+ const builder = flatbuffers.createBuilder();
+ const inner = msg.RunStatus.createRunStatus(builder, rid);
- if (res.gotSignal) {
- const signal = res.exitSignal;
+ const baseRes = await sendAsync(builder, msg.Any.RunStatus, inner);
+ assert(baseRes != null);
+ assert(msg.Any.RunStatusRes === baseRes!.innerType());
+ const res = new msg.RunStatusRes();
+ assert(baseRes!.inner(res) != null);
+
+ if (res.gotSignal()) {
+ const signal = res.exitSignal();
return { signal, success: false };
} else {
- const code = res.exitCode;
+ const code = res.exitCode();
return { code, success: code === 0 };
}
}
@@ -57,7 +56,9 @@ async function runStatus(rid: number): Promise<ProcessStatus> {
* Requires the `--allow-run` flag.
*/
export function kill(pid: number, signo: number): void {
- sendSync(dispatch.OP_KILL, { pid, signo });
+ const builder = flatbuffers.createBuilder();
+ const inner = msg.Kill.createKill(builder, pid, signo);
+ sendSync(builder, msg.Any.Kill, inner);
}
export class Process {
@@ -68,20 +69,20 @@ export class Process {
readonly stderr?: ReadCloser;
// @internal
- constructor(res: RunResponse) {
- this.rid = res.rid;
- this.pid = res.pid;
+ constructor(res: msg.RunRes) {
+ this.rid = res.rid();
+ this.pid = res.pid();
- if (res.stdinRid && res.stdinRid > 0) {
- this.stdin = new File(res.stdinRid);
+ if (res.stdinRid() > 0) {
+ this.stdin = new File(res.stdinRid());
}
- if (res.stdoutRid && res.stdoutRid > 0) {
- this.stdout = new File(res.stdoutRid);
+ if (res.stdoutRid() > 0) {
+ this.stdout = new File(res.stdoutRid());
}
- if (res.stderrRid && res.stderrRid > 0) {
- this.stderr = new File(res.stderrRid);
+ if (res.stderrRid() > 0) {
+ this.stderr = new File(res.stderrRid());
}
}
@@ -134,13 +135,14 @@ export interface ProcessStatus {
signal?: number; // TODO: Make this a string, e.g. 'SIGTERM'.
}
-// TODO: this method is only used to validate proper option, probably can be renamed
-function stdioMap(s: string): string {
+function stdioMap(s: ProcessStdio): msg.ProcessStdio {
switch (s) {
case "inherit":
+ return msg.ProcessStdio.Inherit;
case "piped":
+ return msg.ProcessStdio.Piped;
case "null":
- return s;
+ return msg.ProcessStdio.Null;
default:
return unreachable();
}
@@ -150,13 +152,6 @@ function isRid(arg: unknown): arg is number {
return !isNaN(arg as number);
}
-interface RunResponse {
- rid: number;
- pid: number;
- stdinRid: number | null;
- stdoutRid: number | null;
- stderrRid: number | null;
-}
/**
* Spawns new subprocess.
*
@@ -171,56 +166,71 @@ interface RunResponse {
* they can be set to either `ProcessStdio` or `rid` of open file.
*/
export function run(opt: RunOptions): Process {
- assert(opt.args.length > 0);
- let env: Array<[string, string]> = [];
+ const builder = flatbuffers.createBuilder();
+ const argsOffset = msg.Run.createArgsVector(
+ builder,
+ opt.args.map((a): number => builder.createString(a))
+ );
+ const cwdOffset = opt.cwd == null ? 0 : builder.createString(opt.cwd);
+ const kvOffset: flatbuffers.Offset[] = [];
if (opt.env) {
- env = Array.from(Object.entries(opt.env));
+ for (const [key, val] of Object.entries(opt.env)) {
+ const keyOffset = builder.createString(key);
+ const valOffset = builder.createString(String(val));
+ kvOffset.push(msg.KeyValue.createKeyValue(builder, keyOffset, valOffset));
+ }
}
+ const envOffset = msg.Run.createEnvVector(builder, kvOffset);
- let stdin = stdioMap("inherit");
- let stdout = stdioMap("inherit");
- let stderr = stdioMap("inherit");
- let stdinRid = 0;
- let stdoutRid = 0;
- let stderrRid = 0;
+ let stdInOffset = stdioMap("inherit");
+ let stdOutOffset = stdioMap("inherit");
+ let stdErrOffset = stdioMap("inherit");
+ let stdinRidOffset = 0;
+ let stdoutRidOffset = 0;
+ let stderrRidOffset = 0;
if (opt.stdin) {
if (isRid(opt.stdin)) {
- stdinRid = opt.stdin;
+ stdinRidOffset = opt.stdin;
} else {
- stdin = stdioMap(opt.stdin);
+ stdInOffset = stdioMap(opt.stdin);
}
}
if (opt.stdout) {
if (isRid(opt.stdout)) {
- stdoutRid = opt.stdout;
+ stdoutRidOffset = opt.stdout;
} else {
- stdout = stdioMap(opt.stdout);
+ stdOutOffset = stdioMap(opt.stdout);
}
}
if (opt.stderr) {
if (isRid(opt.stderr)) {
- stderrRid = opt.stderr;
+ stderrRidOffset = opt.stderr;
} else {
- stderr = stdioMap(opt.stderr);
+ stdErrOffset = stdioMap(opt.stderr);
}
}
- const req = {
- args: opt.args.map(String),
- cwd: opt.cwd,
- env,
- stdin,
- stdout,
- stderr,
- stdinRid,
- stdoutRid,
- stderrRid
- };
+ const inner = msg.Run.createRun(
+ builder,
+ argsOffset,
+ cwdOffset,
+ envOffset,
+ stdInOffset,
+ stdOutOffset,
+ stdErrOffset,
+ stdinRidOffset,
+ stdoutRidOffset,
+ stderrRidOffset
+ );
+ const baseRes = sendSync(builder, msg.Any.Run, inner);
+ assert(baseRes != null);
+ assert(msg.Any.RunRes === baseRes!.innerType());
+ const res = new msg.RunRes();
+ assert(baseRes!.inner(res) != null);
- const res = sendSync(dispatch.OP_RUN, req) as RunResponse;
return new Process(res);
}