0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-12 16:59:32 -05:00

fix(node): instantiating process class without new (#23865)

Popular test runners like Jest instantiate a new `Process` object
themselves and expect the class constructor to be callable without the
`new` keyword. This PR refactors our `Process` class implementation from
a proper ES2015 class to an ES5-style class which can be invoked both
with and without the `new` keyword like in Node.

Fixes https://github.com/denoland/deno/issues/23863
This commit is contained in:
Marvin Hagemeister 2024-05-17 16:24:07 +02:00 committed by Bartek Iwańczuk
parent eec8800513
commit 3d3b9a7566
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 292 additions and 273 deletions

View file

@ -311,6 +311,8 @@ if (!isWindows) {
export { geteuid, getgid, getuid };
const ALLOWED_FLAGS = buildAllowedFlags();
// deno-lint-ignore no-explicit-any
function uncaughtExceptionHandler(err: any, origin: string) {
// The origin parameter can be 'unhandledRejection' or 'uncaughtException'
@ -326,13 +328,21 @@ function uncaughtExceptionHandler(err: any, origin: string) {
let execPath: string | null = null;
class Process extends EventEmitter {
constructor() {
super();
}
// The process class needs to be an ES5 class because it can be instantiated
// in Node without the `new` keyword. It's not a true class in Node. Popular
// test runners like Jest rely on this.
// deno-lint-ignore no-explicit-any
function Process(this: any) {
// deno-lint-ignore no-explicit-any
if (!(this instanceof Process)) return new (Process as any)();
/** https://nodejs.org/api/process.html#processrelease */
get release() {
EventEmitter.call(this);
}
Process.prototype = Object.create(EventEmitter.prototype);
/** https://nodejs.org/api/process.html#processrelease */
Object.defineProperty(Process.prototype, "release", {
get() {
return {
name: "node",
sourceUrl:
@ -340,109 +350,115 @@ class Process extends EventEmitter {
headersUrl:
`https://nodejs.org/download/release/${version}/node-${version}-headers.tar.gz`,
};
}
},
});
/** https://nodejs.org/api/process.html#process_process_arch */
get arch() {
/** https://nodejs.org/api/process.html#process_process_arch */
Object.defineProperty(Process.prototype, "arch", {
get() {
return arch;
}
},
});
get report() {
Object.defineProperty(Process.prototype, "report", {
get() {
return report;
}
},
});
get title() {
Object.defineProperty(Process.prototype, "title", {
get() {
return "deno";
}
set title(_value) {
},
set(_value) {
// NOTE(bartlomieju): this is a noop. Node.js doesn't guarantee that the
// process name will be properly set and visible from other tools anyway.
// Might revisit in the future.
}
},
});
/**
/**
* https://nodejs.org/api/process.html#process_process_argv
* Read permissions are required in order to get the executable route
*/
argv = argv;
Process.prototype.argv = argv;
get argv0() {
Object.defineProperty(Process.prototype, "argv0", {
get() {
return argv0;
}
},
set(_val) {},
});
set argv0(_val) {}
/** https://nodejs.org/api/process.html#process_process_chdir_directory */
Process.prototype.chdir = chdir;
/** https://nodejs.org/api/process.html#process_process_chdir_directory */
chdir = chdir;
/** https://nodejs.org/api/process.html#processconfig */
config = {
/** https://nodejs.org/api/process.html#processconfig */
Process.prototype.config = {
target_defaults: {},
variables: {},
};
};
/** https://nodejs.org/api/process.html#process_process_cwd */
cwd = cwd;
/** https://nodejs.org/api/process.html#process_process_cwd */
Process.prototype.cwd = cwd;
/**
/**
* https://nodejs.org/api/process.html#process_process_env
* Requires env permissions
*/
env = env;
Process.prototype.env = env;
/** https://nodejs.org/api/process.html#process_process_execargv */
execArgv: string[] = [];
/** https://nodejs.org/api/process.html#process_process_execargv */
Process.prototype.execArgv = [];
/** https://nodejs.org/api/process.html#process_process_exit_code */
exit = exit;
/** https://nodejs.org/api/process.html#process_process_exit_code */
Process.prototype.exit = exit;
/** https://nodejs.org/api/process.html#processabort */
abort = abort;
/** https://nodejs.org/api/process.html#processabort */
Process.prototype.abort = abort;
// Undocumented Node API that is used by `signal-exit` which in turn
// is used by `node-tap`. It was marked for removal a couple of years
// ago. See https://github.com/nodejs/node/blob/6a6b3c54022104cc110ab09044a2a0cecb8988e7/lib/internal/bootstrap/node.js#L172
reallyExit = (code: number) => {
// Undocumented Node API that is used by `signal-exit` which in turn
// is used by `node-tap`. It was marked for removal a couple of years
// ago. See https://github.com/nodejs/node/blob/6a6b3c54022104cc110ab09044a2a0cecb8988e7/lib/internal/bootstrap/node.js#L172
Process.prototype.reallyExit = (code: number) => {
return Deno.exit(code || 0);
};
};
_exiting = _exiting;
Process.prototype._exiting = _exiting;
/** https://nodejs.org/api/process.html#processexitcode_1 */
get exitCode() {
/** https://nodejs.org/api/process.html#processexitcode_1 */
Object.defineProperty(Process.prototype, "exitCode", {
get() {
return globalProcessExitCode;
}
set exitCode(code: number | undefined) {
},
set(code: number | undefined) {
globalProcessExitCode = code;
code = parseInt(code) || 0;
if (!isNaN(code)) {
op_set_exit_code(code);
}
}
},
});
// Typed as any to avoid importing "module" module for types
// Typed as any to avoid importing "module" module for types
Process.prototype.mainModule = undefined;
/** https://nodejs.org/api/process.html#process_process_nexttick_callback_args */
Process.prototype.nextTick = _nextTick;
Process.prototype.dlopen = dlopen;
/** https://nodejs.org/api/process.html#process_process_events */
Process.prototype.on = function (
// deno-lint-ignore no-explicit-any
mainModule: any = undefined;
/** https://nodejs.org/api/process.html#process_process_nexttick_callback_args */
nextTick = _nextTick;
dlopen = dlopen;
/** https://nodejs.org/api/process.html#process_process_events */
override on(event: "exit", listener: (code: number) => void): this;
override on(
event: typeof notImplementedEvents[number],
// deno-lint-ignore ban-types
listener: Function,
): this;
this: any,
event: string,
// deno-lint-ignore no-explicit-any
override on(event: string, listener: (...args: any[]) => void): this {
listener: (...args: any[]) => void,
) {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.on("${event}")`);
super.on(event, listener);
EventEmitter.prototype.on.call(this, event, listener);
} else if (event.startsWith("SIG")) {
if (event === "SIGBREAK" && Deno.build.os !== "windows") {
// Ignores SIGBREAK if the platform is not windows.
@ -452,23 +468,22 @@ class Process extends EventEmitter {
Deno.addSignalListener(event as Deno.Signal, listener);
}
} else {
super.on(event, listener);
EventEmitter.prototype.on.call(this, event, listener);
}
return this;
}
};
override off(event: "exit", listener: (code: number) => void): this;
override off(
event: typeof notImplementedEvents[number],
// deno-lint-ignore ban-types
listener: Function,
): this;
Process.prototype.off = function (
// deno-lint-ignore no-explicit-any
override off(event: string, listener: (...args: any[]) => void): this {
this: any,
event: string,
// deno-lint-ignore no-explicit-any
listener: (...args: any[]) => void,
) {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.off("${event}")`);
super.off(event, listener);
EventEmitter.prototype.off.call(this, event, listener);
} else if (event.startsWith("SIG")) {
if (event === "SIGBREAK" && Deno.build.os !== "windows") {
// Ignores SIGBREAK if the platform is not windows.
@ -478,14 +493,19 @@ class Process extends EventEmitter {
Deno.removeSignalListener(event as Deno.Signal, listener);
}
} else {
super.off(event, listener);
EventEmitter.prototype.off.call(this, event, listener);
}
return this;
}
};
Process.prototype.emit = function (
// deno-lint-ignore no-explicit-any
override emit(event: string, ...args: any[]): boolean {
this: any,
event: string,
// deno-lint-ignore no-explicit-any
...args: any[]
): boolean {
if (event.startsWith("SIG")) {
if (event === "SIGBREAK" && Deno.build.os !== "windows") {
// Ignores SIGBREAK if the platform is not windows.
@ -493,29 +513,22 @@ class Process extends EventEmitter {
Deno.kill(Deno.pid, event as Deno.Signal);
}
} else {
return super.emit(event, ...args);
return EventEmitter.prototype.emit.call(this, event, ...args);
}
return true;
}
};
override prependListener(
event: "exit",
listener: (code: number) => void,
): this;
override prependListener(
event: typeof notImplementedEvents[number],
// deno-lint-ignore ban-types
listener: Function,
): this;
override prependListener(
Process.prototype.prependListener = function (
// deno-lint-ignore no-explicit-any
this: any,
event: string,
// deno-lint-ignore no-explicit-any
listener: (...args: any[]) => void,
): this {
) {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.prependListener("${event}")`);
super.prependListener(event, listener);
EventEmitter.prototype.prependListener.call(this, event, listener);
} else if (event.startsWith("SIG")) {
if (event === "SIGBREAK" && Deno.build.os !== "windows") {
// Ignores SIGBREAK if the platform is not windows.
@ -523,73 +536,67 @@ class Process extends EventEmitter {
Deno.addSignalListener(event as Deno.Signal, listener);
}
} else {
super.prependListener(event, listener);
EventEmitter.prototype.prependListener.call(this, event, listener);
}
return this;
}
};
/** https://nodejs.org/api/process.html#process_process_pid */
get pid() {
/** https://nodejs.org/api/process.html#process_process_pid */
Object.defineProperty(Process.prototype, "pid", {
get() {
return pid;
}
},
});
/** https://nodejs.org/api/process.html#processppid */
get ppid() {
/** https://nodejs.org/api/process.html#processppid */
Object.defineProperty(Process.prototype, "ppid", {
get() {
return Deno.ppid;
}
},
});
/** https://nodejs.org/api/process.html#process_process_platform */
get platform() {
/** https://nodejs.org/api/process.html#process_process_platform */
Object.defineProperty(Process.prototype, "platform", {
get() {
return platform;
}
},
});
// https://nodejs.org/api/process.html#processsetsourcemapsenabledval
setSourceMapsEnabled(_val: boolean) {
// https://nodejs.org/api/process.html#processsetsourcemapsenabledval
Process.prototype.setSourceMapsEnabled = (_val: boolean) => {
// This is a no-op in Deno. Source maps are always enabled.
// TODO(@satyarohith): support disabling source maps if needed.
}
};
override addListener(event: "exit", listener: (code: number) => void): this;
override addListener(
event: typeof notImplementedEvents[number],
// deno-lint-ignore ban-types
listener: Function,
): this;
override addListener(
Process.prototype.addListener = function (
// deno-lint-ignore no-explicit-any
this: any,
event: string,
// deno-lint-ignore no-explicit-any
listener: (...args: any[]) => void,
): this {
) {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.addListener("${event}")`);
}
return this.on(event, listener);
}
};
override removeListener(
event: "exit",
listener: (code: number) => void,
): this;
override removeListener(
event: typeof notImplementedEvents[number],
// deno-lint-ignore ban-types
listener: Function,
): this;
override removeListener(
event: string,
Process.prototype.removeListener = function (
// deno-lint-ignore no-explicit-any
this: any,
event: string, // deno-lint-ignore no-explicit-any
listener: (...args: any[]) => void,
): this {
) {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.removeListener("${event}")`);
}
return this.off(event, listener);
}
};
/**
/**
* Returns the current high-resolution real time in a [seconds, nanoseconds]
* tuple.
*
@ -602,92 +609,94 @@ class Process extends EventEmitter {
* 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 = hrtime;
Process.prototype.hrtime = hrtime;
/**
/**
* @private
*
* NodeJS internal, use process.kill instead
*/
_kill = _kill;
Process.prototype._kill = _kill;
/** https://nodejs.org/api/process.html#processkillpid-signal */
kill = kill;
/** https://nodejs.org/api/process.html#processkillpid-signal */
Process.prototype.kill = kill;
memoryUsage = memoryUsage;
Process.prototype.memoryUsage = memoryUsage;
/** https://nodejs.org/api/process.html#process_process_stderr */
stderr = stderr;
/** https://nodejs.org/api/process.html#process_process_stderr */
Process.prototype.stderr = stderr;
/** https://nodejs.org/api/process.html#process_process_stdin */
stdin = stdin;
/** https://nodejs.org/api/process.html#process_process_stdin */
Process.prototype.stdin = stdin;
/** https://nodejs.org/api/process.html#process_process_stdout */
stdout = stdout;
/** https://nodejs.org/api/process.html#process_process_stdout */
Process.prototype.stdout = stdout;
/** https://nodejs.org/api/process.html#process_process_version */
version = version;
/** https://nodejs.org/api/process.html#process_process_version */
Process.prototype.version = version;
/** https://nodejs.org/api/process.html#process_process_versions */
versions = versions;
/** https://nodejs.org/api/process.html#process_process_versions */
Process.prototype.versions = versions;
/** https://nodejs.org/api/process.html#process_process_emitwarning_warning_options */
emitWarning = emitWarning;
/** https://nodejs.org/api/process.html#process_process_emitwarning_warning_options */
Process.prototype.emitWarning = emitWarning;
binding(name: BindingName) {
Process.prototype.binding = (name: BindingName) => {
return getBinding(name);
}
};
/** https://nodejs.org/api/process.html#processumaskmask */
umask() {
/** https://nodejs.org/api/process.html#processumaskmask */
Process.prototype.umask = () => {
// Always return the system default umask value.
// We don't use Deno.umask here because it has a race
// condition bug.
// See https://github.com/denoland/deno_std/issues/1893#issuecomment-1032897779
return 0o22;
}
};
/** This method is removed on Windows */
getgid = getgid;
/** This method is removed on Windows */
Process.prototype.getgid = getgid;
/** This method is removed on Windows */
getuid = getuid;
/** This method is removed on Windows */
Process.prototype.getuid = getuid;
/** This method is removed on Windows */
geteuid = geteuid;
/** This method is removed on Windows */
Process.prototype.geteuid = geteuid;
// TODO(kt3k): Implement this when we added -e option to node compat mode
_eval: string | undefined = undefined;
// TODO(kt3k): Implement this when we added -e option to node compat mode
Process.prototype._eval = undefined;
/** https://nodejs.org/api/process.html#processexecpath */
get execPath() {
/** https://nodejs.org/api/process.html#processexecpath */
Object.defineProperty(Process.prototype, "execPath", {
get() {
if (execPath) {
return execPath;
}
execPath = Deno.execPath();
return execPath;
}
set execPath(path: string) {
},
set(path: string) {
execPath = path;
}
},
});
/** https://nodejs.org/api/process.html#processuptime */
uptime() {
/** https://nodejs.org/api/process.html#processuptime */
Process.prototype.uptime = () => {
return Number((performance.now() / 1000).toFixed(9));
}
};
#allowedFlags = buildAllowedFlags();
/** https://nodejs.org/api/process.html#processallowednodeenvironmentflags */
get allowedNodeEnvironmentFlags() {
return this.#allowedFlags;
}
/** https://nodejs.org/api/process.html#processallowednodeenvironmentflags */
Object.defineProperty(Process.prototype, "allowedNodeEnvironmentFlags", {
get() {
return ALLOWED_FLAGS;
},
});
features = { inspector: false };
Process.prototype.features = { inspector: false };
// TODO(kt3k): Get the value from --no-deprecation flag.
noDeprecation = false;
}
// TODO(kt3k): Get the value from --no-deprecation flag.
Process.prototype.noDeprecation = false;
if (isWindows) {
delete Process.prototype.getgid;
@ -696,6 +705,7 @@ if (isWindows) {
}
/** https://nodejs.org/api/process.html#process_process */
// @ts-ignore TS doesn't work well with ES5 classes
const process = new Process();
Object.defineProperty(process, Symbol.toStringTag, {

View file

@ -1094,3 +1094,12 @@ Deno.test({
assert(v >= 0);
},
});
// Test for https://github.com/denoland/deno/issues/23863
Deno.test({
name: "instantiate process constructor without 'new' keyword",
fn() {
// This would throw
process.constructor.call({});
},
});