summaryrefslogtreecommitdiff
path: root/runtime/js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2022-11-28 12:33:51 +0100
committerGitHub <noreply@github.com>2022-11-28 12:33:51 +0100
commit1dd4843b62085a2aeb3134573adf9a7c47c154e2 (patch)
tree534a7b210655530ffce89b3c124690330867f8bc /runtime/js
parentfb04e87387e04053bf41a1512b4850adf62202c6 (diff)
feat(unstable): rework Deno.Command (#16812)
Refactors the `Deno.Command` class to not handle any state, but only being an intermediary to calling its methods, and as such any methods and properties besides `output`, `outputSync` & `spawn` have been removed. Interracting with a `spawn`ed subprocess now works by using the methods and properties on the returned class of the `spawn` method.
Diffstat (limited to 'runtime/js')
-rw-r--r--runtime/js/40_spawn.js136
-rw-r--r--runtime/js/90_deno_ns.js1
-rw-r--r--runtime/js/99_main.js31
3 files changed, 54 insertions, 114 deletions
diff --git a/runtime/js/40_spawn.js b/runtime/js/40_spawn.js
index 8f44c8929..863063e3f 100644
--- a/runtime/js/40_spawn.js
+++ b/runtime/js/40_spawn.js
@@ -277,136 +277,44 @@
};
}
- class Command {
- #command;
- #options;
-
- #child;
-
- #consumed;
-
- constructor(command, options) {
- this.#command = command;
- this.#options = options;
- }
+ function createCommand(spawn, spawnSync, spawnChild) {
+ return class Command {
+ #command;
+ #options;
+
+ constructor(command, options) {
+ this.#command = command;
+ this.#options = options;
+ }
- output() {
- if (this.#child) {
- return this.#child.output();
- } else {
- if (this.#consumed) {
+ output() {
+ if (this.#options?.stdin === "piped") {
throw new TypeError(
- "Command instance is being or has already been consumed.",
+ "Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
);
}
+ return spawn(this.#command, this.#options);
+ }
+
+ outputSync() {
if (this.#options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
);
}
-
- this.#consumed = true;
- return Deno.spawn(this.#command, this.#options);
+ return spawnSync(this.#command, this.#options);
}
- }
- outputSync() {
- if (this.#consumed) {
- throw new TypeError(
- "Command instance is being or has already been consumed.",
- );
- }
- if (this.#child) {
- throw new TypeError("Was spawned");
- }
- if (this.#options?.stdin === "piped") {
- throw new TypeError(
- "Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
- );
- }
-
- this.#consumed = true;
- return Deno.spawnSync(this.#command, this.#options);
- }
-
- spawn() {
- if (this.#consumed) {
- throw new TypeError(
- "Command instance is being or has already been consumed.",
- );
- }
-
- this.#consumed = true;
- this.#child = Deno.spawnChild(this.#command, this.#options);
- }
-
- get stdin() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
-
- return this.#child.stdin;
- }
-
- get stdout() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
+ spawn() {
+ return spawnChild(this.#command, this.#options);
}
-
- return this.#child.stdout;
- }
-
- get stderr() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
-
- return this.#child.stderr;
- }
-
- get status() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
-
- return this.#child.status;
- }
-
- get pid() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
-
- return this.#child.pid;
- }
-
- kill(signo = "SIGTERM") {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
- this.#child.kill(signo);
- }
-
- ref() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
-
- this.#child.ref();
- }
-
- unref() {
- if (!this.#child) {
- throw new TypeError("Wasn't spawned");
- }
-
- this.#child.unref();
- }
+ };
}
window.__bootstrap.spawn = {
Child,
- Command,
+ ChildProcess: Child,
+ createCommand,
createSpawn,
createSpawnChild,
createSpawnSync,
diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js
index cd6c07464..e3ccf1b6f 100644
--- a/runtime/js/90_deno_ns.js
+++ b/runtime/js/90_deno_ns.js
@@ -144,6 +144,7 @@
funlock: __bootstrap.fs.funlock,
funlockSync: __bootstrap.fs.funlockSync,
Child: __bootstrap.spawn.Child,
+ ChildProcess: __bootstrap.spawn.ChildProcess,
spawnChild: __bootstrap.spawn.spawnChild,
spawn: __bootstrap.spawn.spawn,
spawnSync: __bootstrap.spawn.spawnSync,
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 2ea3504e2..adfc0d360 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -482,6 +482,14 @@ delete Intl.v8BreakIterator;
},
});
+ ObjectAssign(internals.nodeUnstable, {
+ Command: __bootstrap.spawn.createCommand(
+ internals.nodeUnstable.spawn,
+ internals.nodeUnstable.spawnSync,
+ internals.nodeUnstable.spawnChild,
+ ),
+ });
+
const finalDenoNs = {
core,
internal: internalSymbol,
@@ -513,6 +521,14 @@ delete Intl.v8BreakIterator;
ops.op_net_listen_unixpacket,
),
});
+
+ ObjectAssign(finalDenoNs, {
+ Command: __bootstrap.spawn.createCommand(
+ finalDenoNs.spawn,
+ finalDenoNs.spawnSync,
+ finalDenoNs.spawnChild,
+ ),
+ });
}
// Setup `Deno` global - we're actually overriding already existing global
@@ -617,6 +633,14 @@ delete Intl.v8BreakIterator;
},
});
+ ObjectAssign(internals.nodeUnstable, {
+ Command: __bootstrap.spawn.createCommand(
+ internals.nodeUnstable.spawn,
+ internals.nodeUnstable.spawnSync,
+ internals.nodeUnstable.spawnChild,
+ ),
+ });
+
const finalDenoNs = {
core,
internal: internalSymbol,
@@ -640,6 +664,13 @@ delete Intl.v8BreakIterator;
ops.op_net_listen_unixpacket,
),
});
+ ObjectAssign(finalDenoNs, {
+ Command: __bootstrap.spawn.createCommand(
+ finalDenoNs.spawn,
+ finalDenoNs.spawnSync,
+ finalDenoNs.spawnChild,
+ ),
+ });
}
ObjectDefineProperties(finalDenoNs, {
pid: util.readOnly(runtimeOptions.pid),