@bgord/bun 0.29.3 → 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.3",
3
+ "version": "0.29.5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": "Bartosz Gordon",
@@ -29,7 +29,7 @@
29
29
  "@types/nodemailer": "7.0.1",
30
30
  "@types/yazl": "3.3.0",
31
31
  "cspell": "9.2.1",
32
- "knip": "5.64.0",
32
+ "knip": "5.64.1",
33
33
  "lefthook": "1.13.4",
34
34
  "only-allow": "1.2.1",
35
35
  "shellcheck": "4.1.0",
@@ -45,7 +45,7 @@
45
45
  "croner": "9.1.0",
46
46
  "csv": "6.4.1",
47
47
  "hcaptcha": "0.2.0",
48
- "hono": "4.9.8",
48
+ "hono": "4.9.9",
49
49
  "lodash": "4.17.21",
50
50
  "mime-types": "3.0.1",
51
51
  "node-cache": "5.1.2",
@@ -5,45 +5,54 @@ import { formatError } from "./logger-format-error.service";
5
5
  type ServerType = ReturnType<typeof Bun.serve>;
6
6
 
7
7
  export class GracefulShutdown {
8
- constructor(private readonly logger: LoggerPort) {}
9
-
10
8
  private readonly base = { operation: "shutdown", component: "infra" } as const;
9
+ private isShuttingDown = false;
11
10
 
12
- private async shutdown(server: ServerType, cleanup: () => any = tools.noop) {
13
- server.stop();
11
+ constructor(
12
+ private readonly logger: LoggerPort,
13
+ private readonly exitFn: (code: number) => never = ((code: number) => process.exit(code)) as never,
14
+ ) {}
14
15
 
15
- try {
16
- await cleanup();
16
+ private shutdown(server: ServerType, cleanup: () => any, exitCode: number) {
17
+ if (this.isShuttingDown) return;
18
+ this.isShuttingDown = true;
17
19
 
18
- this.logger.info({ message: "HTTP server closed", ...this.base });
20
+ try {
21
+ server.stop();
19
22
  } catch (error) {
20
- this.logger.error({ message: "Cleanup hook failed", error: formatError(error), ...this.base });
23
+ this.logger.error({ message: "Server stop failed", error: formatError(error), ...this.base });
21
24
  }
25
+
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
+ });
22
35
  }
23
36
 
24
37
  applyTo(server: ServerType, cleanup: () => any = tools.noop) {
25
- process.once("SIGTERM", async () => {
38
+ process.once("SIGTERM", () => {
26
39
  this.logger.info({ message: "SIGTERM received", ...this.base });
27
- await this.shutdown(server, cleanup);
28
- process.exitCode = 0;
40
+ this.shutdown(server, cleanup, 0);
29
41
  });
30
42
 
31
- process.once("SIGINT", async () => {
43
+ process.once("SIGINT", () => {
32
44
  this.logger.info({ message: "SIGINT received", ...this.base });
33
- await this.shutdown(server, cleanup);
34
- process.exitCode = 0;
45
+ this.shutdown(server, cleanup, 0);
35
46
  });
36
47
 
37
- process.once("unhandledRejection", async (event) => {
38
- this.logger.error({ message: "UnhandledRejection received", error: formatError(event), ...this.base });
39
- await this.shutdown(server, cleanup);
40
- process.exitCode = 1;
48
+ process.once("unhandledRejection", (reason) => {
49
+ this.logger.error({ message: "UnhandledRejection received", error: formatError(reason), ...this.base });
50
+ this.shutdown(server, cleanup, 1);
41
51
  });
42
52
 
43
- process.once("uncaughtException", async (error) => {
53
+ process.once("uncaughtException", (error) => {
44
54
  this.logger.error({ message: "UncaughtException received", error: formatError(error), ...this.base });
45
- await this.shutdown(server, cleanup);
46
- process.exitCode = 1;
55
+ this.shutdown(server, cleanup, 1);
47
56
  });
48
57
  }
49
58
  }