@bgord/bun 0.29.4 → 0.29.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/bun",
3
- "version": "0.29.4",
3
+ "version": "0.29.5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": "Bartosz Gordon",
@@ -4,21 +4,16 @@ import { formatError } from "./logger-format-error.service";
4
4
 
5
5
  type ServerType = ReturnType<typeof Bun.serve>;
6
6
 
7
- type GracefulShutdownOptions = { exitFn?: (code: number) => never };
8
-
9
7
  export class GracefulShutdown {
10
8
  private readonly base = { operation: "shutdown", component: "infra" } as const;
11
- private readonly exitFn: (code: number) => never;
12
9
  private isShuttingDown = false;
13
10
 
14
11
  constructor(
15
12
  private readonly logger: LoggerPort,
16
- options: GracefulShutdownOptions = {},
17
- ) {
18
- this.exitFn = options.exitFn ?? ((code: number) => process.exit(code));
19
- }
13
+ private readonly exitFn: (code: number) => never = ((code: number) => process.exit(code)) as never,
14
+ ) {}
20
15
 
21
- private async shutdown(server: ServerType, cleanup: () => any, exitCode: number) {
16
+ private shutdown(server: ServerType, cleanup: () => any, exitCode: number) {
22
17
  if (this.isShuttingDown) return;
23
18
  this.isShuttingDown = true;
24
19
 
@@ -28,35 +23,36 @@ export class GracefulShutdown {
28
23
  this.logger.error({ message: "Server stop failed", error: formatError(error), ...this.base });
29
24
  }
30
25
 
31
- try {
32
- await cleanup();
33
- this.logger.info({ message: "HTTP server closed", ...this.base });
34
- } catch (error) {
35
- this.logger.error({ message: "Cleanup hook failed", error: formatError(error), ...this.base });
36
- } finally {
37
- this.exitFn(exitCode);
38
- }
26
+ Promise.resolve()
27
+ .then(() => cleanup())
28
+ .then(() => this.logger.info({ message: "HTTP server closed", ...this.base }))
29
+ .catch((error) =>
30
+ this.logger.error({ message: "Cleanup hook failed", error: formatError(error), ...this.base }),
31
+ )
32
+ .finally(() => {
33
+ this.exitFn(exitCode);
34
+ });
39
35
  }
40
36
 
41
37
  applyTo(server: ServerType, cleanup: () => any = tools.noop) {
42
- process.once("SIGTERM", async () => {
38
+ process.once("SIGTERM", () => {
43
39
  this.logger.info({ message: "SIGTERM received", ...this.base });
44
- await this.shutdown(server, cleanup, 0);
40
+ this.shutdown(server, cleanup, 0);
45
41
  });
46
42
 
47
- process.once("SIGINT", async () => {
43
+ process.once("SIGINT", () => {
48
44
  this.logger.info({ message: "SIGINT received", ...this.base });
49
- await this.shutdown(server, cleanup, 0);
45
+ this.shutdown(server, cleanup, 0);
50
46
  });
51
47
 
52
- process.once("unhandledRejection", async (reason /*, promise*/) => {
48
+ process.once("unhandledRejection", (reason) => {
53
49
  this.logger.error({ message: "UnhandledRejection received", error: formatError(reason), ...this.base });
54
- await this.shutdown(server, cleanup, 1);
50
+ this.shutdown(server, cleanup, 1);
55
51
  });
56
52
 
57
- process.once("uncaughtException", async (error) => {
53
+ process.once("uncaughtException", (error) => {
58
54
  this.logger.error({ message: "UncaughtException received", error: formatError(error), ...this.base });
59
- await this.shutdown(server, cleanup, 1);
55
+ this.shutdown(server, cleanup, 1);
60
56
  });
61
57
  }
62
58
  }