Skip to main content
Module

std/node/process.ts

Deno standard library
Go to Latest
File
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.import { warnNotImplemented } from "./_utils.ts";import { EventEmitter } from "./events.ts";import { fromFileUrl } from "../path/mod.ts";import { isWindows } from "../_util/os.ts";import { Readable, Writable } from "./stream.ts";import { Buffer } from "./buffer.ts";import { validateString } from "./_validators.ts";import { ERR_INVALID_ARG_TYPE } from "./_errors.ts";import { getOptionValue } from "./_options.ts";import { assert } from "../_util/assert.ts";
const notImplementedEvents = [ "beforeExit", "disconnect", "message", "multipleResolves", "rejectionHandled", "uncaughtException", "uncaughtExceptionMonitor", "unhandledRejection",];
/** Returns the operating system CPU architecture for which the Deno binary was compiled */function _arch(): string { if (Deno.build.arch == "x86_64") { return "x64"; } else if (Deno.build.arch == "aarch64") { return "arm64"; } else { throw Error("unreachable"); }}
/** https://nodejs.org/api/process.html#process_process_arch */export const arch = _arch();
// The first 2 items are placeholders.// They will be overwritten by the below Object.defineProperty calls.const argv = ["", "", ...Deno.args];// Overwrites the 1st item with getter.Object.defineProperty(argv, "0", { get: Deno.execPath });// Overwrites the 2st item with getter.Object.defineProperty(argv, "1", { get: () => fromFileUrl(Deno.mainModule) });
/** https://nodejs.org/api/process.html#process_process_chdir_directory */export const chdir = Deno.chdir;
/** https://nodejs.org/api/process.html#process_process_cwd */export const cwd = Deno.cwd;
/** * https://nodejs.org/api/process.html#process_process_env * Requires env permissions */export const env: Record<string, string> = new Proxy({}, { get(_target, prop) { return Deno.env.get(String(prop)); }, ownKeys: () => Reflect.ownKeys(Deno.env.toObject()), getOwnPropertyDescriptor: () => ({ enumerable: true, configurable: true }), set(_target, prop, value) { Deno.env.set(String(prop), String(value)); return value; },});
/** https://nodejs.org/api/process.html#process_process_exit_code */export const exit = Deno.exit;
/** https://nodejs.org/api/process.html#process_process_nexttick_callback_args */export function nextTick(this: unknown, cb: () => void): void;export function nextTick<T extends Array<unknown>>( this: unknown, cb: (...args: T) => void, ...args: T): void;export function nextTick<T extends Array<unknown>>( this: unknown, cb: (...args: T) => void, ...args: T) { if (args) { queueMicrotask(() => cb.call(this, ...args)); } else { queueMicrotask(cb); }}
/** https://nodejs.org/api/process.html#process_process_pid */export const pid = Deno.pid;
/** https://nodejs.org/api/process.html#process_process_platform */export const platform = isWindows ? "win32" : Deno.build.os;
/** * https://nodejs.org/api/process.html#process_process_version * * This value is hard coded to latest stable release of Node, as * some packages are checking it for compatibility. Previously * it pointed to Deno version, but that led to incompability * with some packages. */export const version = "v16.11.1";
/** * https://nodejs.org/api/process.html#process_process_versions * * This value is hard coded to latest stable release of Node, as * some packages are checking it for compatibility. Previously * it contained only output of `Deno.version`, but that led to incompability * with some packages. Value of `v8` field is still taken from `Deno.version`. */export const versions = { node: "16.11.1", uv: "1.42.0", zlib: "1.2.11", brotli: "1.0.9", ares: "1.17.2", modules: "93", nghttp2: "1.45.1", napi: "8", llhttp: "6.0.4", openssl: "1.1.1l", cldr: "39.0", icu: "69.1", tz: "2021a", unicode: "13.0", ...Deno.version,};
interface _Readable extends Readable { get isTTY(): true | undefined; destroySoon: Readable["destroy"]; fd: number; _isStdio: undefined;}
interface _Writable extends Writable { get isTTY(): true | undefined; get columns(): number | undefined; get rows(): number | undefined; getWindowSize(): [columns: number, rows: number] | undefined; destroySoon: Writable["destroy"]; fd: number; _isStdio: true;}
// https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41function createWritableStdioStream(writer: typeof Deno.stdout): _Writable { const stream = new Writable({ write(buf: Uint8Array, enc: string, cb) { writer.writeSync(buf instanceof Uint8Array ? buf : Buffer.from(buf, enc)); cb(); }, destroy(err, cb) { cb(err); this._undestroy(); if (!this._writableState.emitClose) { queueMicrotask(() => this.emit("close")); } }, }) as _Writable; stream.fd = writer.rid; stream.destroySoon = stream.destroy; stream._isStdio = true; stream.once("close", () => writer.close()); Object.defineProperties(stream, { columns: { enumerable: true, configurable: true, get: () => Deno.isatty(writer.rid) ? Deno.consoleSize(writer.rid).columns : undefined, }, rows: { enumerable: true, configurable: true, get: () => Deno.isatty(writer.rid) ? Deno.consoleSize(writer.rid).rows : undefined, }, isTTY: { enumerable: true, configurable: true, get: () => Deno.isatty(writer.rid), }, getWindowSize: { enumerable: true, configurable: true, value: () => Deno.isatty(writer.rid) ? Object.values(Deno.consoleSize(writer.rid)) : undefined, }, }); return stream;}
/** https://nodejs.org/api/process.html#process_process_stderr */export const stderr = createWritableStdioStream(Deno.stderr);
/** https://nodejs.org/api/process.html#process_process_stdin */export const stdin = new Readable({ read(this: Readable, size: number) { const p = Buffer.alloc(size || 16 * 1024); const length = Deno.stdin.readSync(p); this.push(length === null ? null : p.slice(0, length)); },}) as _Readable;stdin.on("close", () => Deno.stdin.close());stdin.fd = Deno.stdin.rid;Object.defineProperty(stdin, "isTTY", { enumerable: true, configurable: true, get() { return Deno.isatty(Deno.stdin.rid); },});
/** https://nodejs.org/api/process.html#process_process_stdout */export const stdout = createWritableStdioStream(Deno.stdout);
function addReadOnlyProcessAlias( name: string, option: string, enumerable = true,) { const value = getOptionValue(option);
if (value) { Object.defineProperty(process, name, { writable: false, configurable: true, enumerable, value, }); }}
function createWarningObject( warning: string, type: string, code?: string, // deno-lint-ignore ban-types ctor?: Function, detail?: string,): Error { assert(typeof warning === "string");
// deno-lint-ignore no-explicit-any const warningErr: any = new Error(warning); warningErr.name = String(type || "Warning");
if (code !== undefined) { warningErr.code = code; } if (detail !== undefined) { warningErr.detail = detail; }
// @ts-ignore this function is not available in lib.dom.d.ts Error.captureStackTrace(warningErr, ctor || process.emitWarning);
return warningErr;}
function doEmitWarning(warning: Error) { process.emit("warning", warning);}
/** https://nodejs.org/api/process.html#process_process_emitwarning_warning_options */export function emitWarning( warning: string | Error, type: // deno-lint-ignore ban-types | { type: string; detail: string; code: string; ctor: Function } | string | null, code?: string, // deno-lint-ignore ban-types ctor?: Function,) { let detail;
if (type !== null && typeof type === "object" && !Array.isArray(type)) { ctor = type.ctor; code = type.code;
if (typeof type.detail === "string") { detail = type.detail; }
type = type.type || "Warning"; } else if (typeof type === "function") { ctor = type; code = undefined; type = "Warning"; }
if (type !== undefined) { validateString(type, "type"); }
if (typeof code === "function") { ctor = code; code = undefined; } else if (code !== undefined) { validateString(code, "code"); }
if (typeof warning === "string") { warning = createWarningObject(warning, type as string, code, ctor, detail); } else if (!(warning instanceof Error)) { throw new ERR_INVALID_ARG_TYPE("warning", ["Error", "string"], warning); }
if (warning.name === "DeprecationWarning") { // deno-lint-ignore no-explicit-any if ((process as any).noDeprecation) { return; }
// deno-lint-ignore no-explicit-any if ((process as any).throwDeprecation) { // Delay throwing the error to guarantee that all former warnings were // properly logged. return process.nextTick(() => { throw warning; }); } }
process.nextTick(doEmitWarning, warning);}
class Process extends EventEmitter { constructor() { super();
//This causes the exit event to be binded to the unload event // deno-lint-ignore no-window-prefix window.addEventListener("unload", () => { //TODO(Soremwar) //Get the exit code from the unload event super.emit("exit", 0); }); }
/** https://nodejs.org/api/process.html#process_process_arch */ arch = arch;
/** * https://nodejs.org/api/process.html#process_process_argv * Read permissions are required in order to get the executable route */ argv = argv;
/** https://nodejs.org/api/process.html#process_process_execargv */ execArgv = [];
/** https://nodejs.org/api/process.html#process_process_chdir_directory */ chdir = chdir;
/** https://nodejs.org/api/process.html#process_process_cwd */ cwd = cwd;
/** https://nodejs.org/api/process.html#process_process_exit_code */ exit = exit;
/** * https://nodejs.org/api/process.html#process_process_env * Requires env permissions */ env = env;
// Typed as any to avoid importing "module" module for types // deno-lint-ignore no-explicit-any mainModule: any = undefined;
/** https://nodejs.org/api/process.html#process_process_nexttick_callback_args */ nextTick = nextTick;
/** https://nodejs.org/api/process.html#process_process_events */ on(event: "exit", listener: (code: number) => void): this; // deno-lint-ignore no-explicit-any on(event: string, listener: (...args: any[]) => void): this; // deno-lint-ignore ban-types on(event: typeof notImplementedEvents[number], listener: Function): this; // deno-lint-ignore no-explicit-any on(event: string, listener: (...args: any[]) => void): this { if (notImplementedEvents.includes(event)) { warnNotImplemented(`process.on("${event}")`); } else if (event.startsWith("SIG")) { Deno.addSignalListener(event as Deno.Signal, listener); } else { super.on(event, listener); }
return this; }
off(event: "exit", listener: (code: number) => void): this; // deno-lint-ignore no-explicit-any off(event: string, listener: (...args: any[]) => void): this; // deno-lint-ignore ban-types off(event: typeof notImplementedEvents[number], listener: Function): this; // deno-lint-ignore no-explicit-any off(event: string, listener: (...args: any[]) => void): this { if (notImplementedEvents.includes(event)) { warnNotImplemented(`process.off("${event}")`); } else if (event.startsWith("SIG")) { Deno.removeSignalListener(event as Deno.Signal, listener); } else { super.off(event, listener); }
return this; }
/** https://nodejs.org/api/process.html#process_process_pid */ pid = pid;
/** https://nodejs.org/api/process.html#process_process_platform */ platform = platform;
removeAllListeners(eventName?: string | symbol): this { return super.removeAllListeners(eventName); }
removeListener( event: typeof notImplementedEvents[number], //deno-lint-ignore ban-types listener: Function, ): this; removeListener(event: "exit", listener: (code: number) => void): this; //deno-lint-ignore no-explicit-any removeListener(event: string, listener: (...args: any[]) => void): this { if (notImplementedEvents.includes(event)) { warnNotImplemented(`process.removeListener("${event}")`); return this; }
super.removeListener("exit", listener);
return this; }
/** * Returns the current high-resolution real time in a [seconds, nanoseconds] * tuple. * * Note: You need to give --allow-hrtime permission to Deno to actually get * nanoseconds precision values. If you don't give 'hrtime' permission, the returned * values only have milliseconds precision. * * `time` is an optional parameter that must be the result of a previous process.hrtime() call to diff with the current time. * * These times are relative to an arbitrary time in the past, and not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals. * https://nodejs.org/api/process.html#process_process_hrtime_time */ hrtime(time?: [number, number]): [number, number] { const milli = performance.now(); const sec = Math.floor(milli / 1000); const nano = Math.floor(milli * 1_000_000 - sec * 1_000_000_000); if (!time) { return [sec, nano]; } const [prevSec, prevNano] = time; return [sec - prevSec, nano - prevNano]; }
/** https://nodejs.org/api/process.html#process_process_stderr */ stderr = stderr;
/** https://nodejs.org/api/process.html#process_process_stdin */ stdin = stdin;
/** https://nodejs.org/api/process.html#process_process_stdout */ stdout = stdout;
/** https://nodejs.org/api/process.html#process_process_version */ version = version;
/** https://nodejs.org/api/process.html#process_process_versions */ versions = versions;
/** https://nodejs.org/api/process.html#process_process_emitwarning_warning_options */ emitWarning = emitWarning;}
/** https://nodejs.org/api/process.html#process_process */const process = new Process();
Object.defineProperty(process, Symbol.toStringTag, { enumerable: false, writable: true, configurable: false, value: "process",});
addReadOnlyProcessAlias("noDeprecation", "--no-deprecation");addReadOnlyProcessAlias("throwDeprecation", "--throw-deprecation");
export const removeListener = process.removeListener;export const removeAllListeners = process.removeAllListeners;
export default process;
//TODO(Soremwar)//Remove on 1.0//Kept for backwards compatibility with stdexport { process };