2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2022-02-15 07:59:04 -05:00
|
|
|
const { FsFile } = window.__bootstrap.files;
|
2021-04-05 18:05:36 -04:00
|
|
|
const { readAll } = window.__bootstrap.io;
|
2022-03-19 08:57:37 -04:00
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
|
|
|
const { assert } = window.__bootstrap.infra;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
|
|
|
ArrayPrototypeMap,
|
2022-03-25 08:17:13 -04:00
|
|
|
ArrayPrototypeSlice,
|
2021-07-03 18:17:52 -04:00
|
|
|
TypeError,
|
|
|
|
ObjectEntries,
|
|
|
|
String,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
function opKill(pid, signo, apiName) {
|
|
|
|
ops.op_kill(pid, signo, apiName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function kill(pid, signo) {
|
|
|
|
opKill(pid, signo, "Deno.kill()");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opRunStatus(rid) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_run_status", rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opRun(request) {
|
|
|
|
assert(request.cmd.length > 0);
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_run(request);
|
2020-07-19 13:49:44 -04: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 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Process {
|
|
|
|
constructor(res) {
|
|
|
|
this.rid = res.rid;
|
|
|
|
this.pid = res.pid;
|
|
|
|
|
|
|
|
if (res.stdinRid && res.stdinRid > 0) {
|
2022-02-15 07:59:04 -05:00
|
|
|
this.stdin = new FsFile(res.stdinRid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res.stdoutRid && res.stdoutRid > 0) {
|
2022-02-15 07:59:04 -05:00
|
|
|
this.stdout = new FsFile(res.stdoutRid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res.stderrRid && res.stderrRid > 0) {
|
2022-02-15 07:59:04 -05:00
|
|
|
this.stderr = new FsFile(res.stderrRid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status() {
|
|
|
|
return runStatus(this.rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
async output() {
|
|
|
|
if (!this.stdout) {
|
|
|
|
throw new TypeError("stdout was not piped");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await readAll(this.stdout);
|
|
|
|
} finally {
|
|
|
|
this.stdout.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async stderrOutput() {
|
|
|
|
if (!this.stderr) {
|
|
|
|
throw new TypeError("stderr was not piped");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await readAll(this.stderr);
|
|
|
|
} finally {
|
|
|
|
this.stderr.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
kill(signo) {
|
2022-09-27 16:36:33 -04:00
|
|
|
opKill(this.pid, signo, "Deno.Process.kill()");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function run({
|
|
|
|
cmd,
|
|
|
|
cwd = undefined,
|
2021-08-04 15:47:43 -04:00
|
|
|
clearEnv = false,
|
2020-07-19 13:49:44 -04:00
|
|
|
env = {},
|
2021-09-13 13:26:23 -04:00
|
|
|
gid = undefined,
|
|
|
|
uid = undefined,
|
2020-07-19 13:49:44 -04:00
|
|
|
stdout = "inherit",
|
|
|
|
stderr = "inherit",
|
|
|
|
stdin = "inherit",
|
|
|
|
}) {
|
2020-08-12 14:20:34 -04:00
|
|
|
if (cmd[0] != null) {
|
2022-03-25 08:17:13 -04:00
|
|
|
cmd = [pathFromURL(cmd[0]), ...ArrayPrototypeSlice(cmd, 1)];
|
2020-08-12 14:20:34 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
const res = opRun({
|
2021-07-03 18:17:52 -04:00
|
|
|
cmd: ArrayPrototypeMap(cmd, String),
|
2020-07-19 13:49:44 -04:00
|
|
|
cwd,
|
2021-08-04 15:47:43 -04:00
|
|
|
clearEnv,
|
2021-07-03 18:17:52 -04:00
|
|
|
env: ObjectEntries(env),
|
2021-09-13 13:26:23 -04:00
|
|
|
gid,
|
|
|
|
uid,
|
2022-04-26 09:26:05 -04:00
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
return new Process(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.process = {
|
|
|
|
run,
|
|
|
|
Process,
|
2022-09-27 16:36:33 -04:00
|
|
|
kill,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|