summaryrefslogtreecommitdiff
path: root/runtime/js/40_spawn.js
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2022-07-18 14:16:12 +0100
committerGitHub <noreply@github.com>2022-07-18 15:16:12 +0200
commit45c49034a7ea20f27287cd8559ea050d8973bfae (patch)
tree4566c3ca296f49e6bb564693f0e18e141304a3d4 /runtime/js/40_spawn.js
parent0f6b455c964933692f4c82476692ab66eba242c2 (diff)
BREAKING(unstable): Improve Deno.spawn() stdio API (#14919)
- "SpawnOutput" extends "ChildStatus" instead of composing it - "SpawnOutput::stdout", "SpawnOutput::stderr", "Child::stdin", "Child::stdout" and "Child::stderr" are no longer optional, instead made them getters that throw at runtime if that stream wasn't set to "piped". - Remove the complicated "<T extends SpawnOptions = SpawnOptions>" which we currently need to give proper type hints for the availability of these fields. Their typings for these would get increasingly complex if it became dependent on more options (e.g. "SpawnOptions::pty" which if set should make the stdio streams unavailable)
Diffstat (limited to 'runtime/js/40_spawn.js')
-rw-r--r--runtime/js/40_spawn.js52
1 files changed, 42 insertions, 10 deletions
diff --git a/runtime/js/40_spawn.js b/runtime/js/40_spawn.js
index 7a2f06b6a..54914dc09 100644
--- a/runtime/js/40_spawn.js
+++ b/runtime/js/40_spawn.js
@@ -77,21 +77,27 @@
return this.#pid;
}
- #stdinRid;
#stdin = null;
get stdin() {
+ if (this.#stdin == null) {
+ throw new TypeError("stdin is not piped");
+ }
return this.#stdin;
}
- #stdoutRid;
#stdout = null;
get stdout() {
+ if (this.#stdout == null) {
+ throw new TypeError("stdout is not piped");
+ }
return this.#stdout;
}
- #stderrRid;
#stderr = null;
get stderr() {
+ if (this.#stderr == null) {
+ throw new TypeError("stderr is not piped");
+ }
return this.#stderr;
}
@@ -111,17 +117,14 @@
this.#pid = pid;
if (stdinRid !== null) {
- this.#stdinRid = stdinRid;
this.#stdin = writableStreamForRid(stdinRid);
}
if (stdoutRid !== null) {
- this.#stdoutRid = stdoutRid;
this.#stdout = readableStreamForRid(stdoutRid);
}
if (stderrRid !== null) {
- this.#stderrRid = stderrRid;
this.#stderr = readableStreamForRid(stderrRid);
}
@@ -159,9 +162,21 @@
]);
return {
- status,
- stdout,
- stderr,
+ success: status.success,
+ code: status.code,
+ signal: status.signal,
+ get stdout() {
+ if (stdout == null) {
+ throw new TypeError("stdout is not piped");
+ }
+ return stdout;
+ },
+ get stderr() {
+ if (stderr == null) {
+ throw new TypeError("stderr is not piped");
+ }
+ return stderr;
+ },
};
}
@@ -198,7 +213,7 @@
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
);
}
- return core.opSync("op_spawn_sync", {
+ const result = core.opSync("op_spawn_sync", {
cmd: pathFromURL(command),
args: ArrayPrototypeMap(args, String),
cwd: pathFromURL(cwd),
@@ -210,6 +225,23 @@
stdout,
stderr,
});
+ return {
+ success: result.status.success,
+ code: result.status.code,
+ signal: result.status.signal,
+ get stdout() {
+ if (result.stdout == null) {
+ throw new TypeError("stdout is not piped");
+ }
+ return result.stdout;
+ },
+ get stderr() {
+ if (result.stderr == null) {
+ throw new TypeError("stderr is not piped");
+ }
+ return result.stderr;
+ },
+ };
}
window.__bootstrap.spawn = {