@idlebox/node 1.4.13 → 1.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/autoindex.d.ts +14 -14
- package/lib/autoindex.d.ts.map +1 -1
- package/lib/autoindex.js +25 -27
- package/lib/autoindex.js.map +1 -1
- package/lib/lifecycle/register.d.ts.map +1 -1
- package/lib/lifecycle/register.js +59 -44
- package/lib/lifecycle/register.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
- package/src/autoindex.ts +29 -31
- package/src/lifecycle/register.ts +62 -45
- package/lib/preload.d.ts +0 -2
- package/lib/preload.d.ts.map +0 -1
- package/lib/preload.js +0 -2
- package/lib/preload.js.map +0 -1
- package/src/preload.ts +0 -1
|
@@ -8,7 +8,7 @@ import { basename } from 'node:path';
|
|
|
8
8
|
import process from 'node:process';
|
|
9
9
|
import { inspect } from 'node:util';
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const shutdown_immediate: (code?: number) => never = process.exit;
|
|
12
12
|
const prefix = process.stderr.isTTY ? '' : `<${title()} ${process.pid}> `;
|
|
13
13
|
|
|
14
14
|
function title() {
|
|
@@ -31,16 +31,16 @@ export function setExitCodeIfNot(exitCode: number) {
|
|
|
31
31
|
|
|
32
32
|
let shuttingDown = 0;
|
|
33
33
|
export function shutdown(exitCode: number): never {
|
|
34
|
-
|
|
34
|
+
_shutdown_graceful(exitCode);
|
|
35
35
|
throw new Exit(getCurrentCode());
|
|
36
36
|
}
|
|
37
|
-
function
|
|
37
|
+
function _shutdown_graceful(exitCode: number) {
|
|
38
38
|
setExitCodeIfNot(exitCode);
|
|
39
39
|
|
|
40
40
|
if (!shuttingDown) {
|
|
41
41
|
shuttingDown = 1;
|
|
42
42
|
ensureDisposeGlobal().finally(() => {
|
|
43
|
-
|
|
43
|
+
shutdown_immediate(getCurrentCode());
|
|
44
44
|
});
|
|
45
45
|
} else {
|
|
46
46
|
shuttingDown++;
|
|
@@ -67,32 +67,41 @@ export function registerNodejsGlobalTypedErrorHandler(ErrorCls: ErrorConstructor
|
|
|
67
67
|
typed_error_handlers.set(ErrorCls, fn);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
function
|
|
70
|
+
function _root_cause(e: Error) {
|
|
71
|
+
if (e.cause instanceof Error) {
|
|
72
|
+
return _root_cause(e.cause);
|
|
73
|
+
}
|
|
74
|
+
return e;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function uniqueErrorHandler(currentError: unknown, logger: IDebugOutput) {
|
|
71
78
|
if (!isProductionMode) logger.verbose?.(`uniqueErrorHandler:`);
|
|
72
|
-
if (!(
|
|
73
|
-
prettyPrintError(
|
|
74
|
-
|
|
79
|
+
if (!(currentError instanceof Error)) {
|
|
80
|
+
prettyPrintError(
|
|
81
|
+
`${prefix}catch unexpect object`,
|
|
82
|
+
new Error(`error object is ${typeof currentError} ${currentError ? (currentError as any).constructor?.name : 'unknown'}`),
|
|
83
|
+
);
|
|
84
|
+
throw shutdown_immediate(ExitCode.PROGRAM);
|
|
75
85
|
}
|
|
76
86
|
|
|
77
|
-
|
|
87
|
+
const rootCause = _root_cause(currentError);
|
|
88
|
+
if (rootCause instanceof Exit) {
|
|
78
89
|
if (!isProductionMode) logger.verbose?.(` - skip exit object`);
|
|
79
|
-
if (!shuttingDown)
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
throw e;
|
|
90
|
+
if (!shuttingDown) _shutdown_graceful(rootCause.code);
|
|
91
|
+
throw rootCause;
|
|
83
92
|
}
|
|
84
93
|
|
|
85
94
|
try {
|
|
86
|
-
const catcher = typed_error_handlers.get(
|
|
95
|
+
const catcher = typed_error_handlers.get(rootCause.constructor as ErrorConstructor);
|
|
87
96
|
if (catcher) {
|
|
88
97
|
if (!isProductionMode) logger.verbose?.(` - call catcher ${functionName(catcher)}`);
|
|
89
|
-
catcher(
|
|
98
|
+
catcher(rootCause);
|
|
90
99
|
return;
|
|
91
100
|
}
|
|
92
101
|
for (const [Cls, fn] of inherit_error_handlers) {
|
|
93
102
|
if (!isProductionMode) logger.verbose?.(` - call inherited catcher ${functionName(fn)}`);
|
|
94
|
-
if (
|
|
95
|
-
fn(
|
|
103
|
+
if (rootCause instanceof Cls) {
|
|
104
|
+
fn(rootCause);
|
|
96
105
|
return;
|
|
97
106
|
}
|
|
98
107
|
}
|
|
@@ -100,37 +109,45 @@ function uniqueErrorHandler(e: unknown, logger: IDebugOutput) {
|
|
|
100
109
|
prettyPrintError(`${prefix}error while handle error`, {
|
|
101
110
|
message: ee.message,
|
|
102
111
|
stack: ee.stack,
|
|
103
|
-
cause:
|
|
112
|
+
cause: rootCause,
|
|
104
113
|
});
|
|
105
114
|
return;
|
|
106
115
|
}
|
|
107
116
|
|
|
108
|
-
if (
|
|
117
|
+
if (rootCause instanceof InterruptError) {
|
|
109
118
|
if (!isProductionMode) logger.verbose?.(` - shuttingDown = ${shuttingDown}`);
|
|
110
|
-
if (shuttingDown > 5) {
|
|
111
|
-
logger.output(`${prefix}Exiting immediately.`);
|
|
112
|
-
originalExit(ExitCode.INTERRUPT);
|
|
113
|
-
}
|
|
114
119
|
|
|
120
|
+
const signal = rootCause.signal;
|
|
121
|
+
if (signal === 'SIGINT') {
|
|
122
|
+
process.stderr.write(shuttingDown === 0 ? '\n' : '\r');
|
|
123
|
+
}
|
|
124
|
+
if (shuttingDown > 4) {
|
|
125
|
+
logger.output(`${prefix}Received ${signal} more than 5 times. Exiting immediately.`);
|
|
126
|
+
shutdown_immediate(ExitCode.INTERRUPT);
|
|
127
|
+
} else if (shuttingDown > 0) {
|
|
128
|
+
logger.output(`${prefix}Received ${signal} ${shuttingDown + 1} times.`);
|
|
129
|
+
} else {
|
|
130
|
+
logger.output(`${prefix}Received ${signal}. Exiting gracefully...`);
|
|
131
|
+
}
|
|
115
132
|
shutdown(ExitCode.INTERRUPT);
|
|
116
133
|
}
|
|
117
|
-
if (
|
|
134
|
+
if (currentError instanceof UnhandledRejection) {
|
|
118
135
|
if (!isProductionMode) logger.verbose?.(` - UnhandledRejection`);
|
|
119
|
-
if (
|
|
120
|
-
prettyPrintError(`${prefix}Unhandled Rejection`,
|
|
136
|
+
if (rootCause !== currentError) {
|
|
137
|
+
prettyPrintError(`${prefix}Unhandled Rejection`, currentError.cause);
|
|
121
138
|
} else {
|
|
122
|
-
logger.output(`${prefix}Unhandled Rejection / error type unknown: ${inspect(
|
|
139
|
+
logger.output(`${prefix}Unhandled Rejection / error type unknown: ${inspect(currentError.cause)}`);
|
|
123
140
|
}
|
|
124
141
|
return;
|
|
125
142
|
}
|
|
126
|
-
if (
|
|
143
|
+
if (currentError instanceof UncaughtException) {
|
|
127
144
|
if (!isProductionMode) logger.verbose?.(` - UncaughtException`);
|
|
128
|
-
prettyPrintError(`${prefix}Uncaught Exception`,
|
|
145
|
+
prettyPrintError(`${prefix}Uncaught Exception`, rootCause);
|
|
129
146
|
return;
|
|
130
147
|
}
|
|
131
148
|
|
|
132
149
|
if (!isProductionMode) logger.verbose?.(` - common error`);
|
|
133
|
-
prettyPrintError(`${prefix}unhandled global exception`,
|
|
150
|
+
prettyPrintError(`${prefix}unhandled global exception`, currentError);
|
|
134
151
|
shutdown(ExitCode.PROGRAM);
|
|
135
152
|
}
|
|
136
153
|
|
|
@@ -147,15 +164,15 @@ export function registerNodejsExitHandler(logger: IDebugOutput = { output: conso
|
|
|
147
164
|
}
|
|
148
165
|
function _real_register(logger: IDebugOutput) {
|
|
149
166
|
logger.verbose?.(`register nodejs exit handler: production=${isProductionMode}`);
|
|
150
|
-
process.on('SIGINT', () => {
|
|
151
|
-
logger.output(`\n${prefix}Received SIGINT. Exiting gracefully...`);
|
|
152
|
-
uniqueErrorHandler(new InterruptError('SIGINT'), logger);
|
|
153
|
-
});
|
|
154
167
|
|
|
155
|
-
process.on('
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
168
|
+
process.on('SIGINT', () => signal_handler('SIGINT'));
|
|
169
|
+
process.on('SIGTERM', () => signal_handler('SIGTERM'));
|
|
170
|
+
|
|
171
|
+
function signal_handler(signal: 'SIGINT' | 'SIGTERM') {
|
|
172
|
+
setImmediate(() => {
|
|
173
|
+
uniqueErrorHandler(new InterruptError(signal, signal_handler), logger);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
159
176
|
|
|
160
177
|
process.on('beforeExit', (code) => {
|
|
161
178
|
// empty handler prevent real exit
|
|
@@ -164,7 +181,7 @@ function _real_register(logger: IDebugOutput) {
|
|
|
164
181
|
code = ExitCode.EXECUTION;
|
|
165
182
|
logger.output(`${prefix}beforeExit called, but process.exitCode has not been set, switch to ${code}`);
|
|
166
183
|
}
|
|
167
|
-
|
|
184
|
+
_shutdown_graceful(code);
|
|
168
185
|
});
|
|
169
186
|
|
|
170
187
|
function finalThrow(e: UnhandledRejection | UncaughtException) {
|
|
@@ -173,17 +190,17 @@ function _real_register(logger: IDebugOutput) {
|
|
|
173
190
|
|
|
174
191
|
if (e.cause instanceof ErrorWithCode) {
|
|
175
192
|
if (!isProductionMode) logger.verbose?.(`finalThrow: got code: ${e.cause.code}`);
|
|
176
|
-
|
|
193
|
+
_shutdown_graceful(e.cause.code);
|
|
177
194
|
} else {
|
|
178
195
|
if (!isProductionMode) logger.verbose?.(`finalThrow: not got code: ${e.cause} `);
|
|
179
|
-
|
|
196
|
+
_shutdown_graceful(ExitCode.PROGRAM);
|
|
180
197
|
}
|
|
181
|
-
} catch (
|
|
182
|
-
if (
|
|
198
|
+
} catch (ee: any) {
|
|
199
|
+
if (ee instanceof Exit) {
|
|
183
200
|
return;
|
|
184
201
|
}
|
|
185
|
-
prettyPrintError('Exception while handling error',
|
|
186
|
-
|
|
202
|
+
prettyPrintError('Exception while handling error', ee);
|
|
203
|
+
shutdown_immediate(ExitCode.PROGRAM);
|
|
187
204
|
}
|
|
188
205
|
}
|
|
189
206
|
|
package/lib/preload.d.ts
DELETED
package/lib/preload.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"preload.d.ts","sourceRoot":"","sources":["../src/preload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC"}
|
package/lib/preload.js
DELETED
package/lib/preload.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"preload.js","sourceRoot":"","sources":["../src/preload.ts"],"names":[],"mappings":""}
|
package/src/preload.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|