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;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2022-04-20 18:20:33 -04:00
|
|
|
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,
|
2022-12-19 21:37:50 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2022-04-20 18:20:33 -04:00
|
|
|
String,
|
|
|
|
TypeError,
|
2022-10-29 05:25:23 -04:00
|
|
|
PromisePrototypeThen,
|
|
|
|
SafePromiseAll,
|
2022-07-18 16:24:35 -04:00
|
|
|
SymbolFor,
|
2022-04-20 18:20:33 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2022-09-29 18:42:33 -04:00
|
|
|
const {
|
2022-11-15 08:06:52 -05:00
|
|
|
readableStreamCollectIntoUint8Array,
|
2022-09-29 18:42:33 -04:00
|
|
|
readableStreamForRidUnrefable,
|
|
|
|
readableStreamForRidUnrefableRef,
|
|
|
|
readableStreamForRidUnrefableUnref,
|
2022-12-19 21:37:50 -05:00
|
|
|
ReadableStreamPrototype,
|
2022-10-10 04:28:35 -04:00
|
|
|
writableStreamForRid,
|
2022-09-29 18:42:33 -04:00
|
|
|
} = window.__bootstrap.streams;
|
2022-04-20 18:20:33 -04:00
|
|
|
|
2022-07-18 16:24:35 -04:00
|
|
|
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
function spawnChildInner(opFn, command, apiName, {
|
2022-04-20 18:20:33 -04:00
|
|
|
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-10-17 06:51:25 -04:00
|
|
|
windowsRawArguments = false,
|
2022-04-20 18:20:33 -04:00
|
|
|
} = {}) {
|
2022-11-10 16:03:28 -05:00
|
|
|
const child = opFn({
|
2022-04-20 18:20:33 -04:00
|
|
|
cmd: pathFromURL(command),
|
|
|
|
args: ArrayPrototypeMap(args, String),
|
|
|
|
cwd: pathFromURL(cwd),
|
|
|
|
clearEnv,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
2022-10-17 06:51:25 -04:00
|
|
|
windowsRawArguments,
|
2022-09-27 16:36:33 -04:00
|
|
|
}, apiName);
|
2022-05-11 01:59:39 -04:00
|
|
|
return new Child(illegalConstructorKey, {
|
|
|
|
...child,
|
|
|
|
signal,
|
|
|
|
});
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
function createSpawnChild(opFn) {
|
|
|
|
return function spawnChild(command, options = {}) {
|
2022-12-09 10:43:36 -05:00
|
|
|
return spawnChildInner(opFn, command, "Deno.Command().spawn()", options);
|
2022-11-10 16:03:28 -05:00
|
|
|
};
|
2022-09-27 16:36:33 -04:00
|
|
|
}
|
|
|
|
|
2022-11-15 08:06:52 -05:00
|
|
|
function collectOutput(readableStream) {
|
2022-12-19 21:37:50 -05:00
|
|
|
if (
|
|
|
|
!(ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, readableStream))
|
|
|
|
) {
|
2022-04-20 18:20:33 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-15 08:06:52 -05:00
|
|
|
return readableStreamCollectIntoUint8Array(readableStream);
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Child {
|
|
|
|
#rid;
|
2022-07-18 16:24:35 -04:00
|
|
|
#waitPromiseId;
|
2022-07-22 12:07:20 -04:00
|
|
|
#unrefed = false;
|
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;
|
2022-09-29 18:42:33 -04:00
|
|
|
this.#stdout = readableStreamForRidUnrefable(stdoutRid);
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stderrRid !== null) {
|
2022-07-18 16:24:35 -04:00
|
|
|
this.#stderrRid = stderrRid;
|
2022-09-29 18:42:33 -04:00
|
|
|
this.#stderr = readableStreamForRidUnrefable(stderrRid);
|
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];
|
2022-10-29 05:25:23 -04:00
|
|
|
this.#status = PromisePrototypeThen(waitPromise, (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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-29 05:25:23 -04:00
|
|
|
const [status, stdout, stderr] = await SafePromiseAll([
|
2022-04-20 18:20:33 -04:00
|
|
|
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.");
|
|
|
|
}
|
2022-09-27 16:36:33 -04:00
|
|
|
ops.op_kill(this.#pid, signo, "Deno.Child.kill()");
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
2022-07-18 16:24:35 -04:00
|
|
|
|
|
|
|
ref() {
|
2022-07-22 12:07:20 -04:00
|
|
|
this.#unrefed = false;
|
2022-07-18 16:24:35 -04:00
|
|
|
core.refOp(this.#waitPromiseId);
|
2022-09-29 18:42:33 -04:00
|
|
|
if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout);
|
|
|
|
if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr);
|
2022-07-18 16:24:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
2022-07-22 12:07:20 -04:00
|
|
|
this.#unrefed = true;
|
2022-07-18 16:24:35 -04:00
|
|
|
core.unrefOp(this.#waitPromiseId);
|
2022-09-29 18:42:33 -04:00
|
|
|
if (this.#stdout) readableStreamForRidUnrefableUnref(this.#stdout);
|
|
|
|
if (this.#stderr) readableStreamForRidUnrefableUnref(this.#stderr);
|
2022-07-18 16:24:35 -04:00
|
|
|
}
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
function createSpawn(opFn) {
|
|
|
|
return function spawn(command, options) {
|
|
|
|
if (options?.stdin === "piped") {
|
|
|
|
throw new TypeError(
|
2022-12-09 10:43:36 -05:00
|
|
|
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
|
2022-11-10 16:03:28 -05:00
|
|
|
);
|
|
|
|
}
|
2022-12-09 10:43:36 -05:00
|
|
|
return spawnChildInner(opFn, command, "Deno.Command().output()", options)
|
|
|
|
.output();
|
2022-11-10 16:03:28 -05:00
|
|
|
};
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
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(
|
2022-12-09 10:43:36 -05:00
|
|
|
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
|
2022-11-10 16:03:28 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
};
|
2022-07-18 09:16:12 -04:00
|
|
|
};
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 06:33:51 -05:00
|
|
|
function createCommand(spawn, spawnSync, spawnChild) {
|
|
|
|
return class Command {
|
|
|
|
#command;
|
|
|
|
#options;
|
|
|
|
|
|
|
|
constructor(command, options) {
|
|
|
|
this.#command = command;
|
|
|
|
this.#options = options;
|
|
|
|
}
|
2022-11-13 14:00:24 -05:00
|
|
|
|
2022-11-28 06:33:51 -05:00
|
|
|
output() {
|
|
|
|
if (this.#options?.stdin === "piped") {
|
2022-11-13 14:00:24 -05:00
|
|
|
throw new TypeError(
|
2022-11-28 06:33:51 -05:00
|
|
|
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
|
2022-11-13 14:00:24 -05:00
|
|
|
);
|
|
|
|
}
|
2022-11-28 06:33:51 -05:00
|
|
|
return spawn(this.#command, this.#options);
|
|
|
|
}
|
|
|
|
|
|
|
|
outputSync() {
|
2022-11-13 14:00:24 -05:00
|
|
|
if (this.#options?.stdin === "piped") {
|
|
|
|
throw new TypeError(
|
|
|
|
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
|
|
|
|
);
|
|
|
|
}
|
2022-11-28 06:33:51 -05:00
|
|
|
return spawnSync(this.#command, this.#options);
|
2022-11-13 14:00:24 -05:00
|
|
|
}
|
|
|
|
|
2022-11-28 06:33:51 -05:00
|
|
|
spawn() {
|
2022-12-12 23:12:19 -05:00
|
|
|
const options = {
|
|
|
|
...(this.#options ?? {}),
|
|
|
|
stdout: this.#options?.stdout ?? "inherit",
|
|
|
|
stderr: this.#options?.stderr ?? "inherit",
|
|
|
|
};
|
|
|
|
return spawnChild(this.#command, options);
|
2022-11-13 14:00:24 -05:00
|
|
|
}
|
2022-11-28 06:33:51 -05:00
|
|
|
};
|
2022-11-13 14:00:24 -05:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:20:33 -04:00
|
|
|
window.__bootstrap.spawn = {
|
|
|
|
Child,
|
2022-11-28 06:33:51 -05:00
|
|
|
ChildProcess: Child,
|
|
|
|
createCommand,
|
2022-11-10 16:03:28 -05:00
|
|
|
createSpawn,
|
|
|
|
createSpawnChild,
|
|
|
|
createSpawnSync,
|
2022-04-20 18:20:33 -04:00
|
|
|
};
|
|
|
|
})(this);
|