2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-12-30 00:32:30 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
2024-01-26 17:46:46 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_kill,
|
|
|
|
op_run,
|
|
|
|
op_run_status,
|
|
|
|
op_spawn_child,
|
|
|
|
op_spawn_kill,
|
|
|
|
op_spawn_sync,
|
|
|
|
op_spawn_wait,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
TypeError,
|
|
|
|
ObjectEntries,
|
|
|
|
SafeArrayIterator,
|
|
|
|
String,
|
2023-03-05 07:19:34 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
SafePromiseAll,
|
|
|
|
Symbol,
|
2024-01-25 17:51:29 -05:00
|
|
|
SymbolFor,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import { FsFile } from "ext:deno_fs/30_fs.js";
|
|
|
|
import { readAll } from "ext:deno_io/12_io.js";
|
2023-11-01 15:26:12 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
pathFromURL,
|
|
|
|
SymbolAsyncDispose,
|
|
|
|
} from "ext:deno_web/00_infra.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
|
2023-03-05 07:19:34 -05:00
|
|
|
import {
|
|
|
|
readableStreamCollectIntoUint8Array,
|
|
|
|
readableStreamForRidUnrefable,
|
|
|
|
readableStreamForRidUnrefableRef,
|
|
|
|
readableStreamForRidUnrefableUnref,
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
writableStreamForRid,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/06_streams.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
function opKill(pid, signo, apiName) {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_kill(pid, signo, apiName);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function kill(pid, signo = "SIGTERM") {
|
|
|
|
opKill(pid, signo, "Deno.kill()");
|
|
|
|
}
|
|
|
|
|
|
|
|
function opRunStatus(rid) {
|
2023-12-26 20:30:26 -05:00
|
|
|
return op_run_status(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function opRun(request) {
|
|
|
|
assert(request.cmd.length > 0);
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_run(request);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function runStatus(rid) {
|
|
|
|
const res = await opRunStatus(rid);
|
|
|
|
|
|
|
|
if (res.gotSignal) {
|
|
|
|
const signal = res.exitSignal;
|
|
|
|
return { success: false, code: 128 + signal, signal };
|
|
|
|
} else if (res.exitCode != 0) {
|
|
|
|
return { success: false, code: res.exitCode };
|
|
|
|
} else {
|
|
|
|
return { success: true, code: 0 };
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Process {
|
|
|
|
constructor(res) {
|
|
|
|
this.rid = res.rid;
|
|
|
|
this.pid = res.pid;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (res.stdinRid && res.stdinRid > 0) {
|
2024-01-25 17:51:29 -05:00
|
|
|
this.stdin = new FsFile(res.stdinRid, SymbolFor("Deno.internal.FsFile"));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (res.stdoutRid && res.stdoutRid > 0) {
|
2024-01-25 17:51:29 -05:00
|
|
|
this.stdout = new FsFile(
|
|
|
|
res.stdoutRid,
|
|
|
|
SymbolFor("Deno.internal.FsFile"),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (res.stderrRid && res.stderrRid > 0) {
|
2024-01-25 17:51:29 -05:00
|
|
|
this.stderr = new FsFile(
|
|
|
|
res.stderrRid,
|
|
|
|
SymbolFor("Deno.internal.FsFile"),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
status() {
|
|
|
|
return runStatus(this.rid);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async output() {
|
|
|
|
if (!this.stdout) {
|
|
|
|
throw new TypeError("stdout was not piped");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
return await readAll(this.stdout);
|
|
|
|
} finally {
|
|
|
|
this.stdout.close();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async stderrOutput() {
|
|
|
|
if (!this.stderr) {
|
|
|
|
throw new TypeError("stderr was not piped");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await readAll(this.stderr);
|
|
|
|
} finally {
|
|
|
|
this.stderr.close();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
kill(signo = "SIGTERM") {
|
|
|
|
opKill(this.pid, signo, "Deno.Process.kill()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 18:45:55 -04:00
|
|
|
// Note: This function was soft-removed in Deno 2. Its types have been removed,
|
|
|
|
// but its implementation has been kept to avoid breaking changes.
|
2023-02-07 14:22:46 -05:00
|
|
|
function run({
|
|
|
|
cmd,
|
|
|
|
cwd = undefined,
|
2024-05-22 18:03:35 -04:00
|
|
|
env = { __proto__: null },
|
2023-02-07 14:22:46 -05:00
|
|
|
stdout = "inherit",
|
|
|
|
stderr = "inherit",
|
|
|
|
stdin = "inherit",
|
|
|
|
}) {
|
|
|
|
if (cmd[0] != null) {
|
|
|
|
cmd = [
|
|
|
|
pathFromURL(cmd[0]),
|
|
|
|
...new SafeArrayIterator(ArrayPrototypeSlice(cmd, 1)),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
const res = opRun({
|
|
|
|
cmd: ArrayPrototypeMap(cmd, String),
|
|
|
|
cwd,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
});
|
|
|
|
return new Process(res);
|
|
|
|
}
|
|
|
|
|
2023-03-05 07:19:34 -05:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
2024-08-15 12:38:46 -04:00
|
|
|
function spawnChildInner(command, apiName, {
|
2023-03-05 07:19:34 -05:00
|
|
|
args = [],
|
|
|
|
cwd = undefined,
|
|
|
|
clearEnv = false,
|
2024-05-22 18:03:35 -04:00
|
|
|
env = { __proto__: null },
|
2023-03-05 07:19:34 -05:00
|
|
|
uid = undefined,
|
|
|
|
gid = undefined,
|
|
|
|
stdin = "null",
|
|
|
|
stdout = "piped",
|
|
|
|
stderr = "piped",
|
|
|
|
signal = undefined,
|
|
|
|
windowsRawArguments = false,
|
2023-12-13 05:14:16 -05:00
|
|
|
ipc = -1,
|
2024-08-15 12:38:46 -04:00
|
|
|
extraStdio = [],
|
2024-05-22 18:03:35 -04:00
|
|
|
} = { __proto__: null }) {
|
2024-08-15 12:38:46 -04:00
|
|
|
const child = op_spawn_child({
|
2023-03-05 07:19:34 -05:00
|
|
|
cmd: pathFromURL(command),
|
|
|
|
args: ArrayPrototypeMap(args, String),
|
|
|
|
cwd: pathFromURL(cwd),
|
|
|
|
clearEnv,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
windowsRawArguments,
|
2023-12-13 05:14:16 -05:00
|
|
|
ipc,
|
2024-08-15 12:38:46 -04:00
|
|
|
extraStdio,
|
2023-03-05 07:19:34 -05:00
|
|
|
}, apiName);
|
|
|
|
return new ChildProcess(illegalConstructorKey, {
|
|
|
|
...child,
|
|
|
|
signal,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:03:35 -04:00
|
|
|
function spawnChild(command, options = { __proto__: null }) {
|
2023-03-05 07:19:34 -05:00
|
|
|
return spawnChildInner(
|
|
|
|
command,
|
|
|
|
"Deno.Command().spawn()",
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function collectOutput(readableStream) {
|
|
|
|
if (
|
|
|
|
!(ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, readableStream))
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return readableStreamCollectIntoUint8Array(readableStream);
|
|
|
|
}
|
|
|
|
|
2024-08-15 12:38:46 -04:00
|
|
|
const _ipcPipeRid = Symbol("[[ipcPipeRid]]");
|
|
|
|
const _extraPipeRids = Symbol("[[_extraPipeRids]]");
|
2023-12-30 00:32:30 -05:00
|
|
|
|
2024-08-15 12:38:46 -04:00
|
|
|
internals.getIpcPipeRid = (process) => process[_ipcPipeRid];
|
|
|
|
internals.getExtraPipeRids = (process) => process[_extraPipeRids];
|
2023-12-30 00:32:30 -05:00
|
|
|
|
2023-03-05 07:19:34 -05:00
|
|
|
class ChildProcess {
|
|
|
|
#rid;
|
2023-11-09 15:57:26 -05:00
|
|
|
#waitPromise;
|
2023-05-11 08:53:45 -04:00
|
|
|
#waitComplete = false;
|
2023-03-05 07:19:34 -05:00
|
|
|
|
2024-08-15 12:38:46 -04:00
|
|
|
[_ipcPipeRid];
|
|
|
|
[_extraPipeRids];
|
2023-12-13 05:14:16 -05:00
|
|
|
|
2023-03-05 07:19:34 -05:00
|
|
|
#pid;
|
|
|
|
get pid() {
|
|
|
|
return this.#pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
#stdin = null;
|
|
|
|
get stdin() {
|
|
|
|
if (this.#stdin == null) {
|
|
|
|
throw new TypeError("stdin is not piped");
|
|
|
|
}
|
|
|
|
return this.#stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
#stdout = null;
|
|
|
|
get stdout() {
|
|
|
|
if (this.#stdout == null) {
|
|
|
|
throw new TypeError("stdout is not piped");
|
|
|
|
}
|
|
|
|
return this.#stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
#stderr = null;
|
|
|
|
get stderr() {
|
|
|
|
if (this.#stderr == null) {
|
|
|
|
throw new TypeError("stderr is not piped");
|
|
|
|
}
|
|
|
|
return this.#stderr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(key = null, {
|
|
|
|
signal,
|
|
|
|
rid,
|
|
|
|
pid,
|
|
|
|
stdinRid,
|
|
|
|
stdoutRid,
|
|
|
|
stderrRid,
|
2024-08-15 12:38:46 -04:00
|
|
|
ipcPipeRid, // internal
|
|
|
|
extraPipeRids,
|
2023-03-05 07:19:34 -05:00
|
|
|
} = null) {
|
|
|
|
if (key !== illegalConstructorKey) {
|
|
|
|
throw new TypeError("Illegal constructor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#rid = rid;
|
|
|
|
this.#pid = pid;
|
2024-08-15 12:38:46 -04:00
|
|
|
this[_ipcPipeRid] = ipcPipeRid;
|
|
|
|
this[_extraPipeRids] = extraPipeRids;
|
2023-03-05 07:19:34 -05:00
|
|
|
|
|
|
|
if (stdinRid !== null) {
|
|
|
|
this.#stdin = writableStreamForRid(stdinRid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stdoutRid !== null) {
|
|
|
|
this.#stdout = readableStreamForRidUnrefable(stdoutRid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stderrRid !== null) {
|
|
|
|
this.#stderr = readableStreamForRidUnrefable(stderrRid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const onAbort = () => this.kill("SIGTERM");
|
|
|
|
signal?.[abortSignal.add](onAbort);
|
|
|
|
|
2023-12-26 20:30:26 -05:00
|
|
|
const waitPromise = op_spawn_wait(this.#rid);
|
2023-11-09 15:57:26 -05:00
|
|
|
this.#waitPromise = waitPromise;
|
2023-03-05 07:19:34 -05:00
|
|
|
this.#status = PromisePrototypeThen(waitPromise, (res) => {
|
|
|
|
signal?.[abortSignal.remove](onAbort);
|
2023-05-11 08:53:45 -04:00
|
|
|
this.#waitComplete = true;
|
2023-03-05 07:19:34 -05:00
|
|
|
return res;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#status;
|
|
|
|
get status() {
|
|
|
|
return this.#status;
|
|
|
|
}
|
|
|
|
|
|
|
|
async output() {
|
|
|
|
if (this.#stdout?.locked) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Can't collect output because stdout is locked",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this.#stderr?.locked) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Can't collect output because stderr is locked",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { 0: status, 1: stdout, 2: stderr } = await SafePromiseAll([
|
|
|
|
this.#status,
|
|
|
|
collectOutput(this.#stdout),
|
|
|
|
collectOutput(this.#stderr),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
kill(signo = "SIGTERM") {
|
2023-05-11 08:53:45 -04:00
|
|
|
if (this.#waitComplete) {
|
2023-03-05 07:19:34 -05:00
|
|
|
throw new TypeError("Child process has already terminated.");
|
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
op_spawn_kill(this.#rid, signo);
|
2023-03-05 07:19:34 -05:00
|
|
|
}
|
|
|
|
|
2023-11-01 15:26:12 -04:00
|
|
|
async [SymbolAsyncDispose]() {
|
|
|
|
try {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_spawn_kill(this.#rid, "SIGTERM");
|
2023-11-01 15:26:12 -04:00
|
|
|
} catch {
|
|
|
|
// ignore errors from killing the process (such as ESRCH or BadResource)
|
|
|
|
}
|
|
|
|
await this.#status;
|
|
|
|
}
|
|
|
|
|
2023-03-05 07:19:34 -05:00
|
|
|
ref() {
|
2023-11-09 15:57:26 -05:00
|
|
|
core.refOpPromise(this.#waitPromise);
|
2023-03-05 07:19:34 -05:00
|
|
|
if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout);
|
|
|
|
if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
2023-11-09 15:57:26 -05:00
|
|
|
core.unrefOpPromise(this.#waitPromise);
|
2023-03-05 07:19:34 -05:00
|
|
|
if (this.#stdout) readableStreamForRidUnrefableUnref(this.#stdout);
|
|
|
|
if (this.#stderr) readableStreamForRidUnrefableUnref(this.#stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
command,
|
|
|
|
"Deno.Command().output()",
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
.output();
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawnSync(command, {
|
|
|
|
args = [],
|
|
|
|
cwd = undefined,
|
|
|
|
clearEnv = false,
|
2024-05-22 18:03:35 -04:00
|
|
|
env = { __proto__: null },
|
2023-03-05 07:19:34 -05:00
|
|
|
uid = undefined,
|
|
|
|
gid = undefined,
|
|
|
|
stdin = "null",
|
|
|
|
stdout = "piped",
|
|
|
|
stderr = "piped",
|
|
|
|
windowsRawArguments = false,
|
2024-05-22 18:03:35 -04:00
|
|
|
} = { __proto__: null }) {
|
2023-03-05 07:19:34 -05:00
|
|
|
if (stdin === "piped") {
|
|
|
|
throw new TypeError(
|
|
|
|
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
|
|
|
|
);
|
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
const result = op_spawn_sync({
|
2023-03-05 07:19:34 -05:00
|
|
|
cmd: pathFromURL(command),
|
|
|
|
args: ArrayPrototypeMap(args, String),
|
|
|
|
cwd: pathFromURL(cwd),
|
|
|
|
clearEnv,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
windowsRawArguments,
|
2024-08-15 12:38:46 -04:00
|
|
|
extraStdio: [],
|
2023-03-05 07:19:34 -05:00
|
|
|
});
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Command {
|
|
|
|
#command;
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { ChildProcess, Command, kill, Process, run };
|