diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-02-13 19:25:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-13 19:25:00 +0100 |
commit | f917d2e2c10e0a94e564a9016217e7ce27c8bbee (patch) | |
tree | dc13ab3e0acf5d28874c881ad4ea62f2a103bdff /runtime/js | |
parent | 9e3d433249b9259e3a04b4f68563a41455ac7efc (diff) |
feat: Stabilize Deno.Command API (#17628)
This commit stabilizes "Deno.Command" API with all its related APIs.
"--unstable" flag is no longer required to use this API.
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/40_spawn.js | 207 | ||||
-rw-r--r-- | runtime/js/90_deno_ns.js | 6 | ||||
-rw-r--r-- | runtime/js/99_main.js | 29 |
3 files changed, 102 insertions, 140 deletions
diff --git a/runtime/js/40_spawn.js b/runtime/js/40_spawn.js index f153b93a5..f75d83f9b 100644 --- a/runtime/js/40_spawn.js +++ b/runtime/js/40_spawn.js @@ -55,16 +55,19 @@ function spawnChildInner(opFn, command, apiName, { stderr, windowsRawArguments, }, apiName); - return new Child(illegalConstructorKey, { + return new ChildProcess(illegalConstructorKey, { ...child, signal, }); } -function createSpawnChild(opFn) { - return function spawnChild(command, options = {}) { - return spawnChildInner(opFn, command, "Deno.Command().spawn()", options); - }; +function spawnChild(command, options = {}) { + return spawnChildInner( + ops.op_spawn_child, + command, + "Deno.Command().spawn()", + options, + ); } function collectOutput(readableStream) { @@ -77,7 +80,7 @@ function collectOutput(readableStream) { return readableStreamCollectIntoUint8Array(readableStream); } -class Child { +class ChildProcess { #rid; #waitPromiseId; #unrefed = false; @@ -95,7 +98,6 @@ class Child { return this.#stdin; } - #stdoutPromiseId; #stdoutRid; #stdout = null; get stdout() { @@ -105,7 +107,6 @@ class Child { return this.#stdout; } - #stderrPromiseId; #stderrRid; #stderr = null; get stderr() { @@ -220,116 +221,106 @@ class Child { } } -function createSpawn(opFn) { - return function spawn(command, options) { - if (options?.stdin === "piped") { - throw new TypeError( - "Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead", - ); - } - return spawnChildInner(opFn, command, "Deno.Command().output()", options) - .output(); - }; +function spawn(command, options) { + if (options?.stdin === "piped") { + throw new TypeError( + "Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead", + ); + } + return spawnChildInner( + ops.op_spawn_child, + command, + "Deno.Command().output()", + options, + ) + .output(); } -function createSpawnSync(opFn) { - return function spawnSync(command, { - args = [], - cwd = undefined, - clearEnv = false, - env = {}, - uid = undefined, - gid = undefined, - stdin = "null", - stdout = "piped", - stderr = "piped", - windowsRawArguments = false, - } = {}) { - if (stdin === "piped") { - throw new TypeError( - "Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead", - ); - } - const result = opFn({ - cmd: pathFromURL(command), - args: ArrayPrototypeMap(args, String), - cwd: pathFromURL(cwd), - clearEnv, - env: ObjectEntries(env), - uid, - gid, - stdin, - stdout, - stderr, - windowsRawArguments, - }); - 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; - }, - }; +function spawnSync(command, { + args = [], + cwd = undefined, + clearEnv = false, + env = {}, + uid = undefined, + gid = undefined, + stdin = "null", + stdout = "piped", + stderr = "piped", + windowsRawArguments = false, +} = {}) { + if (stdin === "piped") { + throw new TypeError( + "Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead", + ); + } + const result = ops.op_spawn_sync({ + cmd: pathFromURL(command), + args: ArrayPrototypeMap(args, String), + cwd: pathFromURL(cwd), + clearEnv, + env: ObjectEntries(env), + uid, + gid, + stdin, + stdout, + stderr, + windowsRawArguments, + }); + 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; + }, }; } -function createCommand(spawn, spawnSync, spawnChild) { - return class Command { - #command; - #options; +class Command { + #command; + #options; - constructor(command, options) { - this.#command = command; - this.#options = options; - } + constructor(command, options) { + this.#command = command; + this.#options = options; + } - output() { - if (this.#options?.stdin === "piped") { - throw new TypeError( - "Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead", - ); - } - return spawn(this.#command, this.#options); + output() { + if (this.#options?.stdin === "piped") { + throw new TypeError( + "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", - ); - } - return spawnSync(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", + ); } + return spawnSync(this.#command, this.#options); + } - spawn() { - const options = { - ...(this.#options ?? {}), - stdout: this.#options?.stdout ?? "inherit", - stderr: this.#options?.stderr ?? "inherit", - stdin: this.#options?.stdin ?? "inherit", - }; - return spawnChild(this.#command, options); - } - }; + spawn() { + const options = { + ...(this.#options ?? {}), + stdout: this.#options?.stdout ?? "inherit", + stderr: this.#options?.stderr ?? "inherit", + stdin: this.#options?.stdin ?? "inherit", + }; + return spawnChild(this.#command, options); + } } -const ChildProcess = Child; - -export { - Child, - ChildProcess, - createCommand, - createSpawn, - createSpawnChild, - createSpawnSync, -}; +export { ChildProcess, Command }; diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index fe59ea1a7..7b9d8e6e6 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -152,6 +152,9 @@ const denoNs = { consoleSize: tty.consoleSize, gid: os.gid, uid: os.uid, + Command: spawn.Command, + // TODO(bartlomieju): why is this exported? + ChildProcess: spawn.ChildProcess, }; const denoNsUnstable = { @@ -171,9 +174,6 @@ const denoNsUnstable = { flockSync: fs.flockSync, funlock: fs.funlock, funlockSync: fs.funlockSync, - Child: spawn.Child, - ChildProcess: spawn.ChildProcess, - Command: spawn.Command, upgradeHttp: http.upgradeHttp, upgradeHttpRaw: flash.upgradeHttpRaw, }; diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index a4301ed3a..cfddb143d 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -62,7 +62,6 @@ import { errors } from "internal:runtime/js/01_errors.js"; import * as webidl from "internal:deno_webidl/00_webidl.js"; import DOMException from "internal:deno_web/01_dom_exception.js"; import * as flash from "internal:deno_flash/01_http.js"; -import * as spawn from "internal:runtime/js/40_spawn.js"; import { mainRuntimeGlobalProperties, setLanguage, @@ -466,15 +465,6 @@ function bootstrapMainRuntime(runtimeOptions) { // a snapshot ObjectAssign(internals, { nodeUnstable: { - Command: spawn.createCommand( - spawn.createSpawn(ops.op_node_unstable_spawn_child), - spawn.createSpawnSync( - ops.op_node_unstable_spawn_sync, - ), - spawn.createSpawnChild( - ops.op_node_unstable_spawn_child, - ), - ), serve: flash.createServe(ops.op_node_unstable_flash_serve), upgradeHttpRaw: flash.upgradeHttpRaw, listenDatagram: net.createListenDatagram( @@ -513,11 +503,6 @@ function bootstrapMainRuntime(runtimeOptions) { // the op function that needs to be passed will be invalidated by creating // a snapshot ObjectAssign(finalDenoNs, { - Command: spawn.createCommand( - spawn.createSpawn(ops.op_spawn_child), - spawn.createSpawnSync(ops.op_spawn_sync), - spawn.createSpawnChild(ops.op_spawn_child), - ), serve: flash.createServe(ops.op_flash_serve), listenDatagram: net.createListenDatagram( ops.op_net_listen_udp, @@ -611,15 +596,6 @@ function bootstrapWorkerRuntime( // a snapshot ObjectAssign(internals, { nodeUnstable: { - Command: spawn.createCommand( - spawn.createSpawn(ops.op_node_unstable_spawn_child), - spawn.createSpawnSync( - ops.op_node_unstable_spawn_sync, - ), - spawn.createSpawnChild( - ops.op_node_unstable_spawn_child, - ), - ), serve: flash.createServe(ops.op_node_unstable_flash_serve), upgradeHttpRaw: flash.upgradeHttpRaw, listenDatagram: net.createListenDatagram( @@ -650,11 +626,6 @@ function bootstrapWorkerRuntime( // the op function that needs to be passed will be invalidated by creating // a snapshot ObjectAssign(finalDenoNs, { - Command: spawn.createCommand( - spawn.createSpawn(ops.op_spawn_child), - spawn.createSpawnSync(ops.op_spawn_sync), - spawn.createSpawnChild(ops.op_spawn_child), - ), serve: flash.createServe(ops.op_flash_serve), listenDatagram: net.createListenDatagram( ops.op_net_listen_udp, |