Files
compiler-explorer/lib/node-graceful.ts
T
Mats Jun LarsenandMatt Godbolt 633eb82d18 Transition to ECMAScript Modules (#4780)
Makes the Compiler Explorer app, and all the tooling ESM compatible.
Things that have been done:

1. The package.json has `type: module` now
2. All relative imports have a .js ending
3. All directory imports are now directory/index.js to comply with ESM
standards
4. Dependency node-graceful is now imported into tree, because the
package is broken under esm
5. Dependency p-queue has been bumped to 7.x with ESM support
6. Dependency profanities has been bumped to 3.x with ESM support
7. Webpack config is now both ESM and CommonJS compatible
8. Non-ESM compatible imports have been rewritten
9. ESLint configuration has been tweaked to not fail on .js imports
10. Mocha is now hacked together and ran with ts-node-esm
11. Webpack is now hacked together and ran with ts-node-esm
12. Webpack config is now ESM compatible, so that it can be used in the
dev server
13. Cypress code still runs commonjs, and has been excluded from the
tsconfig
14. All sinon mock tests have been commented out, because sinon module
mocks do not work with ESModules (because ESModules are immutable)

A lot of tests are now giving warnings/errors to stdout, yet still pass.
Docenizer codegenerator scripts have been updated, but I did not re-run
them, and instead just changed their code.

---------

Co-authored-by: Matt Godbolt <matt@godbolt.org>
2023-02-27 18:06:38 -06:00

168 lines
5.7 KiB
TypeScript

// +----------------------------------------------------------------------+
// | node-graceful v3 (https://github.com/mrbar42/node-graceful) |
// | Graceful process exit manager. |
// |----------------------------------------------------------------------|
export type GracefulListener = (
signal: string,
details?: object,
) => void | any | Promise<any> | Promise<void> | Promise<Error>;
export type GracefulSubscription = () => void;
export class Graceful {
private static DEADLY_SIGNALS = ['SIGTERM', 'SIGINT', 'SIGBREAK', 'SIGHUP'];
public static exitOnDouble = true;
public static timeout = 30000;
private static _captureExceptions = false;
private static _captureRejections = false;
private static listeners: GracefulListener[] = [];
private static isRegistered = false;
private static isExiting = false;
private static exceptionListener = (event: any) => {
process.exitCode = 1;
Graceful.onDeadlyEvent('uncaughtException', event);
};
private static rejectionListener = (event: any) => {
process.exitCode = 1;
Graceful.onDeadlyEvent('unhandledRejection', event);
};
private static signalsListeners: {[signal: string]: (event: any) => void} = {};
public static get captureExceptions() {
return Graceful._captureExceptions;
}
public static set captureExceptions(newValue: boolean) {
if (Graceful._captureExceptions === newValue) return;
Graceful._captureExceptions = newValue;
if (Graceful._captureExceptions) {
process.on('uncaughtException' as any, Graceful.exceptionListener);
} else {
process.removeListener('uncaughtException', Graceful.exceptionListener);
}
}
public static get captureRejections() {
return Graceful._captureRejections;
}
public static set captureRejections(newValue: boolean) {
if (Graceful._captureRejections === newValue) return;
Graceful._captureRejections = newValue;
if (Graceful._captureRejections) {
process.on('unhandledRejection' as any, Graceful.rejectionListener);
} else {
process.removeListener('unhandledRejection', Graceful.rejectionListener);
}
}
public static on(signal: 'exit', listener: GracefulListener): GracefulSubscription {
if (signal !== 'exit') throw new Error("Only supports 'exit' signal");
Graceful.listeners.push(listener);
Graceful.updateRegistration();
return () => Graceful.off('exit', listener);
}
public static off(signal: 'exit', listener: GracefulListener) {
if (signal !== 'exit') throw new Error("Only supports 'exit' signal");
const index = Graceful.listeners.indexOf(listener);
if (index !== -1) Graceful.listeners.splice(index, 1);
Graceful.updateRegistration();
}
public static clear() {
Graceful.listeners.splice(0, Infinity);
Graceful.updateRegistration();
}
public static exit(code?: number | string, signal = 'SIGTERM') {
const exitSignal = typeof code === 'string' ? code : signal;
if (typeof code === 'number') {
process.exitCode = code;
}
Graceful.onDeadlyEvent(exitSignal, {reason: 'Manual call to Graceful.exit()'});
}
private static onDeadlyEvent(signal: string, details?: object) {
// console.log(signal, details);
if (Graceful.isExiting) {
if (Graceful.exitOnDouble) Graceful.killProcess(true);
return;
}
const listeners = Graceful.listeners.slice(0);
Graceful.isExiting = true;
let completedListeners = 0;
const done = () => {
completedListeners++;
if (completedListeners === listeners.length) {
Graceful.killProcess(false);
}
};
if (Number(Graceful.timeout)) {
const timeoutRef = setTimeout(() => Graceful.killProcess(true), Graceful.timeout);
if (timeoutRef && timeoutRef.unref) timeoutRef.unref();
}
for (const listener of listeners) {
Graceful.invokeListener(listener, done, signal, details);
}
}
private static invokeListener(listener: GracefulListener, done: () => void, signal: string, details?: object) {
let invoked = false;
const listenerDone = () => {
if (!invoked) {
invoked = true;
done();
}
};
const retVal: any = listener(signal, details);
// allow returning a promise
if (retVal && typeof retVal.then === 'function') {
retVal.then(listenerDone, listenerDone);
} else {
listenerDone();
}
}
private static updateRegistration() {
if (Graceful.listeners.length && !Graceful.isRegistered) {
for (const deadlySignal of Graceful.DEADLY_SIGNALS) {
const listener = () => Graceful.onDeadlyEvent(deadlySignal);
Graceful.signalsListeners[deadlySignal] = listener;
process.on(deadlySignal as any, listener);
}
Graceful.isRegistered = true;
} else if (!Graceful.listeners.length && Graceful.isRegistered) {
for (const deadlySignal of Graceful.DEADLY_SIGNALS) {
const listener = Graceful.signalsListeners[deadlySignal];
if (listener) {
process.removeListener(deadlySignal, listener);
delete Graceful.signalsListeners[deadlySignal];
}
}
Graceful.isRegistered = false;
}
}
private static killProcess(force: boolean) {
process.exit(process.exitCode || (force ? 1 : 0));
}
}