2022-04-20 18:20:33 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
|
|
|
const { illegalConstructorKey } = window.__bootstrap.webUtil;
|
2022-05-11 01:59:39 -04:00
|
|
|
const { add, remove } = window.__bootstrap.abortSignal;
|
2022-04-20 18:20:33 -04:00
|
|
|
const {
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ObjectEntries,
|
|
|
|
String,
|
|
|
|
TypeError,
|
|
|
|
Uint8Array,
|
|
|
|
PromiseAll,
|
2022-07-18 16:24:35 -04:00
|
|
|
SymbolFor,
|
2022-04-20 18:20:33 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
|
|
|
const { readableStreamForRid, writableStreamForRid } =
|
|
|
|
window.__bootstrap.streamUtils;
|
|
|
|
|
2022-07-18 16:24:35 -04:00
|
|
|
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
|
|
|
|
|
2022-04-20 18:20:33 -04:00
|
|
|
function spawnChild(command, {
|
|
|
|
args = [],
|
|
|
|
cwd = undefined,
|
|
|
|
clearEnv = false,
|
|
|
|
env = {},
|
|
|
|
uid = undefined,
|
|
|
|
gid = undefined,
|
|
|
|
stdin = "null",
|
|
|
|
stdout = "piped",
|
|
|
|
stderr = "piped",
|
2022-05-11 01:59:39 -04:00
|
|
|
signal = undefined,
|
2022-04-20 18:20:33 -04:00
|
|
|
} = {}) {
|
|
|
|
const child = core.opSync("op_spawn_child", {
|
|
|
|
cmd: pathFromURL(command),
|
|
|
|
args: ArrayPrototypeMap(args, String),
|
|
|
|
cwd: pathFromURL(cwd),
|
|
|
|
clearEnv,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
});
|
2022-05-11 01:59:39 -04:00
|
|
|
return new Child(illegalConstructorKey, {
|
|
|
|
...child,
|
|
|
|
signal,
|
|
|
|
});
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function collectOutput(readableStream) {
|
|
|
|
if (!(readableStream instanceof ReadableStream)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufs = [];
|
|
|
|
let size = 0;
|
|
|
|
for await (const chunk of readableStream) {
|
|
|
|
bufs.push(chunk);
|
|
|
|
size += chunk.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
const buffer = new Uint8Array(size);
|
|
|
|
let offset = 0;
|
|
|
|
for (const chunk of bufs) {
|
|
|
|
buffer.set(chunk, offset);
|
|
|
|
offset += chunk.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Child {
|
|
|
|
#rid;
|
2022-07-18 16:24:35 -04:00
|
|
|
#waitPromiseId;
|
2022-04-20 18:20:33 -04:00
|
|
|
|
|
|
|
#pid;
|
|
|
|
get pid() {
|
|
|
|
return this.#pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
#stdin = null;
|
|
|
|
get stdin() {
|
2022-07-18 09:16:12 -04:00
|
|
|
if (this.#stdin == null) {
|
|
|
|
throw new TypeError("stdin is not piped");
|
|
|
|
}
|
2022-04-20 18:20:33 -04:00
|
|
|
return this.#stdin;
|
|
|
|
}
|
|
|
|
|
2022-07-18 16:24:35 -04:00
|
|
|
#stdoutPromiseId;
|
|
|
|
#stdoutRid;
|
2022-04-20 18:20:33 -04:00
|
|
|
#stdout = null;
|
|
|
|
get stdout() {
|
2022-07-18 09:16:12 -04:00
|
|
|
if (this.#stdout == null) {
|
|
|
|
throw new TypeError("stdout is not piped");
|
|
|
|
}
|
2022-04-20 18:20:33 -04:00
|
|
|
return this.#stdout;
|
|
|
|
}
|
|
|
|
|
2022-07-18 16:24:35 -04:00
|
|
|
#stderrPromiseId;
|
|
|
|
#stderrRid;
|
2022-04-20 18:20:33 -04:00
|
|
|
#stderr = null;
|
|
|
|
get stderr() {
|
2022-07-18 09:16:12 -04:00
|
|
|
if (this.#stderr == null) {
|
|
|
|
throw new TypeError("stderr is not piped");
|
|
|
|
}
|
2022-04-20 18:20:33 -04:00
|
|
|
return this.#stderr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(key = null, {
|
2022-05-11 01:59:39 -04:00
|
|
|
signal,
|
2022-04-20 18:20:33 -04:00
|
|
|
rid,
|
|
|
|
pid,
|
|
|
|
stdinRid,
|
|
|
|
stdoutRid,
|
|
|
|
stderrRid,
|
|
|
|
} = null) {
|
|
|
|
if (key !== illegalConstructorKey) {
|
|
|
|
throw new TypeError("Illegal constructor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#rid = rid;
|
|
|
|
this.#pid = pid;
|
|
|
|
|
|
|
|
if (stdinRid !== null) {
|
|
|
|
this.#stdin = writableStreamForRid(stdinRid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stdoutRid !== null) {
|
2022-07-18 16:24:35 -04:00
|
|
|
this.#stdoutRid = stdoutRid;
|
|
|
|
this.#stdout = readableStreamForRid(stdoutRid, (promise) => {
|
|
|
|
this.#stdoutPromiseId = promise[promiseIdSymbol];
|
|
|
|
});
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stderrRid !== null) {
|
2022-07-18 16:24:35 -04:00
|
|
|
this.#stderrRid = stderrRid;
|
|
|
|
this.#stderr = readableStreamForRid(stderrRid, (promise) => {
|
|
|
|
this.#stderrPromiseId = promise[promiseIdSymbol];
|
|
|
|
});
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-05-11 01:59:39 -04:00
|
|
|
const onAbort = () => this.kill("SIGTERM");
|
|
|
|
signal?.[add](onAbort);
|
|
|
|
|
2022-07-18 16:24:35 -04:00
|
|
|
const waitPromise = core.opAsync("op_spawn_wait", this.#rid);
|
|
|
|
this.#waitPromiseId = waitPromise[promiseIdSymbol];
|
|
|
|
this.#status = waitPromise.then((res) => {
|
2022-04-20 18:20:33 -04:00
|
|
|
this.#rid = null;
|
2022-05-11 01:59:39 -04:00
|
|
|
signal?.[remove](onAbort);
|
2022-04-20 18:20:33 -04: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 [status, stdout, stderr] = await PromiseAll([
|
|
|
|
this.#status,
|
|
|
|
collectOutput(this.#stdout),
|
|
|
|
collectOutput(this.#stderr),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
2022-07-18 09:16:12 -04:00
|
|
|
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;
|
|
|
|
},
|
2022-04-20 18:20:33 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:05:57 -04:00
|
|
|
kill(signo = "SIGTERM") {
|
2022-04-20 18:20:33 -04:00
|
|
|
if (this.#rid === null) {
|
|
|
|
throw new TypeError("Child process has already terminated.");
|
|
|
|
}
|
|
|
|
core.opSync("op_kill", this.#pid, signo);
|
|
|
|
}
|
2022-07-18 16:24:35 -04:00
|
|
|
|
|
|
|
ref() {
|
|
|
|
core.refOp(this.#waitPromiseId);
|
|
|
|
if (this.#stdoutPromiseId) core.refOp(this.#stdoutPromiseId);
|
|
|
|
if (this.#stderrPromiseId) core.refOp(this.#stderrPromiseId);
|
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
|
|
|
core.unrefOp(this.#waitPromiseId);
|
|
|
|
if (this.#stdoutPromiseId) core.unrefOp(this.#stdoutPromiseId);
|
|
|
|
if (this.#stderrPromiseId) core.unrefOp(this.#stderrPromiseId);
|
|
|
|
}
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-04-24 11:21:22 -04:00
|
|
|
function spawn(command, options) {
|
|
|
|
if (options?.stdin === "piped") {
|
|
|
|
throw new TypeError(
|
|
|
|
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return spawnChild(command, options).output();
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function spawnSync(command, {
|
|
|
|
args = [],
|
|
|
|
cwd = undefined,
|
|
|
|
clearEnv = false,
|
|
|
|
env = {},
|
|
|
|
uid = undefined,
|
|
|
|
gid = undefined,
|
2022-04-24 11:21:22 -04:00
|
|
|
stdin = "null",
|
|
|
|
stdout = "piped",
|
|
|
|
stderr = "piped",
|
|
|
|
} = {}) {
|
|
|
|
if (stdin === "piped") {
|
|
|
|
throw new TypeError(
|
|
|
|
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
|
|
|
|
);
|
|
|
|
}
|
2022-07-18 09:16:12 -04:00
|
|
|
const result = core.opSync("op_spawn_sync", {
|
2022-04-20 18:20:33 -04:00
|
|
|
cmd: pathFromURL(command),
|
|
|
|
args: ArrayPrototypeMap(args, String),
|
|
|
|
cwd: pathFromURL(cwd),
|
|
|
|
clearEnv,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
uid,
|
|
|
|
gid,
|
2022-04-24 11:21:22 -04:00
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
2022-04-20 18:20:33 -04:00
|
|
|
});
|
2022-07-18 09:16:12 -04: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;
|
|
|
|
},
|
|
|
|
};
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.spawn = {
|
|
|
|
Child,
|
|
|
|
spawnChild,
|
|
|
|
spawn,
|
|
|
|
spawnSync,
|
|
|
|
};
|
|
|
|
})(this);
|