2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
2023-03-04 22:37:37 -05:00
|
|
|
import { FsFile } from "internal:runtime/30_fs.js";
|
2023-03-04 19:39:48 -05:00
|
|
|
import { readAll } from "internal:deno_io/12_io.js";
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import { pathFromURL } from "internal:runtime/06_util.js";
|
2023-02-07 16:09:50 -05:00
|
|
|
import { assert } from "internal:deno_web/00_infra.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
TypeError,
|
|
|
|
ObjectEntries,
|
|
|
|
SafeArrayIterator,
|
|
|
|
String,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
function opKill(pid, signo, apiName) {
|
|
|
|
ops.op_kill(pid, signo, apiName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function kill(pid, signo = "SIGTERM") {
|
|
|
|
opKill(pid, signo, "Deno.kill()");
|
|
|
|
}
|
|
|
|
|
|
|
|
function opRunStatus(rid) {
|
|
|
|
return core.opAsync("op_run_status", rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
function opRun(request) {
|
|
|
|
assert(request.cmd.length > 0);
|
|
|
|
return ops.op_run(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.stdin = new FsFile(res.stdinRid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (res.stdoutRid && res.stdoutRid > 0) {
|
|
|
|
this.stdout = new FsFile(res.stdoutRid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (res.stderrRid && res.stderrRid > 0) {
|
|
|
|
this.stderr = new FsFile(res.stderrRid);
|
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()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function run({
|
|
|
|
cmd,
|
|
|
|
cwd = undefined,
|
|
|
|
clearEnv = false,
|
|
|
|
env = {},
|
|
|
|
gid = undefined,
|
|
|
|
uid = undefined,
|
|
|
|
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,
|
|
|
|
clearEnv,
|
|
|
|
env: ObjectEntries(env),
|
|
|
|
gid,
|
|
|
|
uid,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
});
|
|
|
|
return new Process(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
export { kill, Process, run };
|