mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
c62615bfe5
This commit introduces deprecation warnings for "Deno.*" APIs. This is gonna be quite noisy, but should tremendously help with user code updates to ensure smooth migration to Deno 2.0. The warning is printed at each unique call site to help quickly identify where code needs to be adjusted. There's some stack frame filtering going on to remove frames that are not useful to the user and would only cause confusion. The warning can be silenced using "--quiet" flag or "DENO_NO_DEPRECATION_WARNINGS" env var. "Deno.run()" API is now using this warning. Other deprecated APIs will start warning in follow up PRs. Example: ```js import { runEcho as runEcho2 } from "http://localhost:4545/run/warn_on_deprecated_api/mod.ts"; const p = Deno.run({ cmd: [ Deno.execPath(), "eval", "console.log('hello world')", ], }); await p.status(); p.close(); async function runEcho() { const p = Deno.run({ cmd: [ Deno.execPath(), "eval", "console.log('hello world')", ], }); await p.status(); p.close(); } await runEcho(); await runEcho(); for (let i = 0; i < 10; i++) { await runEcho(); } await runEcho2(); ``` ``` $ deno run --allow-read foo.js Warning ├ Use of deprecated "Deno.run()" API. │ ├ This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then. │ ├ Suggestion: Use "Deno.Command()" API instead. │ └ Stack trace: └─ at file:///Users/ib/dev/deno/foo.js:3:16 hello world Warning ├ Use of deprecated "Deno.run()" API. │ ├ This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then. │ ├ Suggestion: Use "Deno.Command()" API instead. │ └ Stack trace: ├─ at runEcho (file:///Users/ib/dev/deno/foo.js:8:18) └─ at file:///Users/ib/dev/deno/foo.js:13:7 hello world Warning ├ Use of deprecated "Deno.run()" API. │ ├ This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then. │ ├ Suggestion: Use "Deno.Command()" API instead. │ └ Stack trace: ├─ at runEcho (file:///Users/ib/dev/deno/foo.js:8:18) └─ at file:///Users/ib/dev/deno/foo.js:14:7 hello world Warning ├ Use of deprecated "Deno.run()" API. │ ├ This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then. │ ├ Suggestion: Use "Deno.Command()" API instead. │ └ Stack trace: ├─ at runEcho (file:///Users/ib/dev/deno/foo.js:8:18) └─ at file:///Users/ib/dev/deno/foo.js:17:9 hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world Warning ├ Use of deprecated "Deno.run()" API. │ ├ This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then. │ ├ Suggestion: Use "Deno.Command()" API instead. │ ├ Suggestion: It appears this API is used by a remote dependency. │ Try upgrading to the latest version of that dependency. │ └ Stack trace: ├─ at runEcho (http://localhost:4545/run/warn_on_deprecated_api/mod.ts:2:18) └─ at file:///Users/ib/dev/deno/foo.js:20:7 hello world ``` Closes #21839
471 lines
9.8 KiB
JavaScript
471 lines
9.8 KiB
JavaScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
|
const {
|
|
op_kill,
|
|
op_run,
|
|
op_run_status,
|
|
op_spawn_child,
|
|
op_spawn_kill,
|
|
op_spawn_sync,
|
|
op_spawn_wait,
|
|
} = core.ensureFastOps();
|
|
const {
|
|
ArrayPrototypeMap,
|
|
ArrayPrototypeSlice,
|
|
TypeError,
|
|
ObjectEntries,
|
|
SafeArrayIterator,
|
|
String,
|
|
ObjectPrototypeIsPrototypeOf,
|
|
PromisePrototypeThen,
|
|
SafePromiseAll,
|
|
Symbol,
|
|
} = primordials;
|
|
|
|
import { FsFile } from "ext:deno_fs/30_fs.js";
|
|
import { readAll } from "ext:deno_io/12_io.js";
|
|
import {
|
|
assert,
|
|
pathFromURL,
|
|
SymbolAsyncDispose,
|
|
} from "ext:deno_web/00_infra.js";
|
|
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
|
|
import {
|
|
readableStreamCollectIntoUint8Array,
|
|
readableStreamForRidUnrefable,
|
|
readableStreamForRidUnrefableRef,
|
|
readableStreamForRidUnrefableUnref,
|
|
ReadableStreamPrototype,
|
|
writableStreamForRid,
|
|
} from "ext:deno_web/06_streams.js";
|
|
|
|
function opKill(pid, signo, apiName) {
|
|
op_kill(pid, signo, apiName);
|
|
}
|
|
|
|
function kill(pid, signo = "SIGTERM") {
|
|
opKill(pid, signo, "Deno.kill()");
|
|
}
|
|
|
|
function opRunStatus(rid) {
|
|
return op_run_status(rid);
|
|
}
|
|
|
|
function opRun(request) {
|
|
assert(request.cmd.length > 0);
|
|
return 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 };
|
|
}
|
|
}
|
|
|
|
class Process {
|
|
constructor(res) {
|
|
this.rid = res.rid;
|
|
this.pid = res.pid;
|
|
|
|
if (res.stdinRid && res.stdinRid > 0) {
|
|
this.stdin = new FsFile(res.stdinRid);
|
|
}
|
|
|
|
if (res.stdoutRid && res.stdoutRid > 0) {
|
|
this.stdout = new FsFile(res.stdoutRid);
|
|
}
|
|
|
|
if (res.stderrRid && res.stderrRid > 0) {
|
|
this.stderr = new FsFile(res.stderrRid);
|
|
}
|
|
}
|
|
|
|
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() {
|
|
core.close(this.rid);
|
|
}
|
|
|
|
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)),
|
|
];
|
|
}
|
|
internals.warnOnDeprecatedApi(
|
|
"Deno.run()",
|
|
(new Error()).stack,
|
|
`Use "Deno.Command()" API instead.`,
|
|
);
|
|
const res = opRun({
|
|
cmd: ArrayPrototypeMap(cmd, String),
|
|
cwd,
|
|
clearEnv,
|
|
env: ObjectEntries(env),
|
|
gid,
|
|
uid,
|
|
stdin,
|
|
stdout,
|
|
stderr,
|
|
});
|
|
return new Process(res);
|
|
}
|
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
function spawnChildInner(opFn, command, apiName, {
|
|
args = [],
|
|
cwd = undefined,
|
|
clearEnv = false,
|
|
env = {},
|
|
uid = undefined,
|
|
gid = undefined,
|
|
stdin = "null",
|
|
stdout = "piped",
|
|
stderr = "piped",
|
|
signal = undefined,
|
|
windowsRawArguments = false,
|
|
ipc = -1,
|
|
} = {}) {
|
|
const child = opFn({
|
|
cmd: pathFromURL(command),
|
|
args: ArrayPrototypeMap(args, String),
|
|
cwd: pathFromURL(cwd),
|
|
clearEnv,
|
|
env: ObjectEntries(env),
|
|
uid,
|
|
gid,
|
|
stdin,
|
|
stdout,
|
|
stderr,
|
|
windowsRawArguments,
|
|
ipc,
|
|
}, apiName);
|
|
return new ChildProcess(illegalConstructorKey, {
|
|
...child,
|
|
signal,
|
|
});
|
|
}
|
|
|
|
function spawnChild(command, options = {}) {
|
|
return spawnChildInner(
|
|
op_spawn_child,
|
|
command,
|
|
"Deno.Command().spawn()",
|
|
options,
|
|
);
|
|
}
|
|
|
|
function collectOutput(readableStream) {
|
|
if (
|
|
!(ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, readableStream))
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return readableStreamCollectIntoUint8Array(readableStream);
|
|
}
|
|
|
|
const _pipeFd = Symbol("[[pipeFd]]");
|
|
|
|
internals.getPipeFd = (process) => process[_pipeFd];
|
|
|
|
class ChildProcess {
|
|
#rid;
|
|
#waitPromise;
|
|
#waitComplete = false;
|
|
|
|
[_pipeFd];
|
|
|
|
#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,
|
|
pipeFd, // internal
|
|
} = null) {
|
|
if (key !== illegalConstructorKey) {
|
|
throw new TypeError("Illegal constructor.");
|
|
}
|
|
|
|
this.#rid = rid;
|
|
this.#pid = pid;
|
|
this[_pipeFd] = pipeFd;
|
|
|
|
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);
|
|
|
|
const waitPromise = op_spawn_wait(this.#rid);
|
|
this.#waitPromise = waitPromise;
|
|
this.#status = PromisePrototypeThen(waitPromise, (res) => {
|
|
signal?.[abortSignal.remove](onAbort);
|
|
this.#waitComplete = true;
|
|
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") {
|
|
if (this.#waitComplete) {
|
|
throw new TypeError("Child process has already terminated.");
|
|
}
|
|
op_spawn_kill(this.#rid, signo);
|
|
}
|
|
|
|
async [SymbolAsyncDispose]() {
|
|
try {
|
|
op_spawn_kill(this.#rid, "SIGTERM");
|
|
} catch {
|
|
// ignore errors from killing the process (such as ESRCH or BadResource)
|
|
}
|
|
await this.#status;
|
|
}
|
|
|
|
ref() {
|
|
core.refOpPromise(this.#waitPromise);
|
|
if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout);
|
|
if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr);
|
|
}
|
|
|
|
unref() {
|
|
core.unrefOpPromise(this.#waitPromise);
|
|
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(
|
|
op_spawn_child,
|
|
command,
|
|
"Deno.Command().output()",
|
|
options,
|
|
)
|
|
.output();
|
|
}
|
|
|
|
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(
|
|
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
|
|
);
|
|
}
|
|
const result = op_spawn_sync({
|
|
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;
|
|
},
|
|
};
|
|
}
|
|
|
|
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 };
|