@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.
@@ -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 originalExit: (code?: number) => never = process.exit;
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
- _shutdown(exitCode);
34
+ _shutdown_graceful(exitCode);
35
35
  throw new Exit(getCurrentCode());
36
36
  }
37
- function _shutdown(exitCode: number) {
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
- originalExit(getCurrentCode());
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 uniqueErrorHandler(e: unknown, logger: IDebugOutput) {
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 (!(e instanceof Error)) {
73
- prettyPrintError(`${prefix}catch unexpect object`, new Error(`error object is ${typeof e} ${e ? (e as any).constructor?.name : 'unknown'}`));
74
- throw originalExit(ExitCode.PROGRAM);
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
- if (e instanceof Exit) {
87
+ const rootCause = _root_cause(currentError);
88
+ if (rootCause instanceof Exit) {
78
89
  if (!isProductionMode) logger.verbose?.(` - skip exit object`);
79
- if (!shuttingDown) {
80
- _shutdown(e.code);
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(e.constructor as ErrorConstructor);
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(e);
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 (e instanceof Cls) {
95
- fn(e);
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: e,
112
+ cause: rootCause,
104
113
  });
105
114
  return;
106
115
  }
107
116
 
108
- if (e instanceof InterruptError) {
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 (e instanceof UnhandledRejection) {
134
+ if (currentError instanceof UnhandledRejection) {
118
135
  if (!isProductionMode) logger.verbose?.(` - UnhandledRejection`);
119
- if (e.cause instanceof Error) {
120
- prettyPrintError(`${prefix}Unhandled Rejection`, e.cause);
136
+ if (rootCause !== currentError) {
137
+ prettyPrintError(`${prefix}Unhandled Rejection`, currentError.cause);
121
138
  } else {
122
- logger.output(`${prefix}Unhandled Rejection / error type unknown: ${inspect(e.cause)}`);
139
+ logger.output(`${prefix}Unhandled Rejection / error type unknown: ${inspect(currentError.cause)}`);
123
140
  }
124
141
  return;
125
142
  }
126
- if (e instanceof UncaughtException) {
143
+ if (currentError instanceof UncaughtException) {
127
144
  if (!isProductionMode) logger.verbose?.(` - UncaughtException`);
128
- prettyPrintError(`${prefix}Uncaught Exception`, e.cause);
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`, e);
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('SIGTERM', () => {
156
- logger.output(`${prefix}Received SIGTERM. Exiting gracefully...`);
157
- uniqueErrorHandler(new InterruptError('SIGTERM'), logger);
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
- _shutdown(code);
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
- _shutdown(e.cause.code);
193
+ _shutdown_graceful(e.cause.code);
177
194
  } else {
178
195
  if (!isProductionMode) logger.verbose?.(`finalThrow: not got code: ${e.cause} `);
179
- _shutdown(ExitCode.PROGRAM);
196
+ _shutdown_graceful(ExitCode.PROGRAM);
180
197
  }
181
- } catch (e: any) {
182
- if (e instanceof Exit) {
198
+ } catch (ee: any) {
199
+ if (ee instanceof Exit) {
183
200
  return;
184
201
  }
185
- prettyPrintError('Exception while handling error', e);
186
- _shutdown(ExitCode.PROGRAM);
202
+ prettyPrintError('Exception while handling error', ee);
203
+ shutdown_immediate(ExitCode.PROGRAM);
187
204
  }
188
205
  }
189
206
 
package/lib/preload.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=preload.d.ts.map
@@ -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
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=preload.js.map
@@ -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 {};