From d16337cc9c59732fe81655482e08b72d844472e6 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Wed, 24 Jun 2020 06:00:47 +1000 Subject: [PATCH] fix(std/node): global process should usable (#6392) --- std/node/os.ts | 2 +- std/node/process.ts | 83 ++++++++++++++++++++++++++++++++-------- std/node/process_test.ts | 28 ++++++++++++-- 3 files changed, 94 insertions(+), 19 deletions(-) diff --git a/std/node/os.ts b/std/node/os.ts index 7d072cac89..afb472629b 100644 --- a/std/node/os.ts +++ b/std/node/os.ts @@ -21,7 +21,7 @@ import { notImplemented } from "./_utils.ts"; import { validateIntegerRange } from "./util.ts"; import { EOL as fsEOL } from "../fs/eol.ts"; -import { process } from "./process.ts"; +import process from "./process.ts"; const SEE_GITHUB_ISSUE = "See https://github.com/denoland/deno/issues/3802"; diff --git a/std/node/process.ts b/std/node/process.ts index cad72a00ab..12cd1b4a94 100644 --- a/std/node/process.ts +++ b/std/node/process.ts @@ -1,33 +1,82 @@ import { notImplemented } from "./_utils.ts"; -function on(_event: string, _callback: Function): void { - // TODO(rsp): to be implemented - notImplemented(); -} +/** https://nodejs.org/api/process.html#process_process_arch */ +export const arch = Deno.build.arch; +/** 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_exit_code */ +export const exit = Deno.exit; + +/** 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 = Deno.build.os === "windows" ? "win32" : Deno.build.os; + +/** https://nodejs.org/api/process.html#process_process_version */ +export const version = `v${Deno.version.deno}`; + +/** https://nodejs.org/api/process.html#process_process_versions */ +export const versions = { + node: Deno.version.deno, + ...Deno.version, +}; + +/** https://nodejs.org/api/process.html#process_process */ +// @deprecated exported only for backwards compatibility with old deno versions export const process = { - version: `v${Deno.version.deno}`, - versions: { - node: Deno.version.deno, - ...Deno.version, + arch, + chdir, + cwd, + exit, + pid, + platform, + version, + versions, + + /** https://nodejs.org/api/process.html#process_process_events */ + // node --input-type=module -e "import {on} from 'process'; console.log(on)" + // on is not exported by node, it is only available within process + on(_event: string, _callback: Function): void { + // TODO(rsp): to be implemented + notImplemented(); }, - platform: Deno.build.os === "windows" ? "win32" : Deno.build.os, - arch: Deno.build.arch, - pid: Deno.pid, - cwd: Deno.cwd, - chdir: Deno.chdir, - exit: Deno.exit, - on, + + /** https://nodejs.org/api/process.html#process_process_env */ get env(): { [index: string]: string } { // using getter to avoid --allow-env unless it's used return Deno.env.toObject(); }, + + /** https://nodejs.org/api/process.html#process_process_argv */ get argv(): string[] { // Deno.execPath() also requires --allow-env return [Deno.execPath(), ...Deno.args]; }, }; +// define the type for configuring the env and argv promises +// as well as for the global.process declaration +type Process = typeof process; + +/** requires the use of await for compatibility with deno */ +export const env = new Promise((resolve) => + resolve(process.env) +); + +/** requires the use of await for compatibility with deno */ +export const argv = new Promise((resolve) => + resolve(process.argv) +); + +/** use this for access to `process.env` and `process.argv` without the need for await */ +export default process; + Object.defineProperty(process, Symbol.toStringTag, { enumerable: false, writable: true, @@ -41,3 +90,7 @@ Object.defineProperty(globalThis, "process", { writable: true, configurable: true, }); + +declare global { + const process: Process; +} diff --git a/std/node/process_test.ts b/std/node/process_test.ts index 058105a4a5..043a954f8c 100644 --- a/std/node/process_test.ts +++ b/std/node/process_test.ts @@ -1,9 +1,29 @@ import { assert, assertThrows, assertEquals } from "../testing/asserts.ts"; -import { process } from "./process.ts"; +import * as all from "./process.ts"; +import { env, argv } from "./process.ts"; // NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env // (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious) +Deno.test({ + name: "process exports are as they should be", + fn() { + // * should be the same as process, default, and globalThis.process + // without the export aliases, and with properties that are not standalone + const allKeys = new Set(Object.keys(all)); + // without { process } for deno b/c + allKeys.delete("process"); + // without esm default + allKeys.delete("default"); + // with on, which is not exported via * + allKeys.add("on"); + const allStr = Array.from(allKeys).sort().join(" "); + assertEquals(Object.keys(all.default).sort().join(" "), allStr); + assertEquals(Object.keys(all.process).sort().join(" "), allStr); + assertEquals(Object.keys(process).sort().join(" "), allStr); + }, +}); + Deno.test({ name: "process.cwd and process.chdir success", fn() { @@ -82,8 +102,9 @@ Deno.test({ Deno.test({ name: "process.argv", - fn() { + async fn() { assert(Array.isArray(process.argv)); + assert(Array.isArray(await argv)); assert( process.argv[0].match(/[^/\\]*deno[^/\\]*$/), "deno included in the file name of argv[0]" @@ -94,7 +115,8 @@ Deno.test({ Deno.test({ name: "process.env", - fn() { + async fn() { assertEquals(typeof process.env.PATH, "string"); + assertEquals(typeof (await env).PATH, "string"); }, });