@naturalcycles/backend-lib 2.60.4 → 2.61.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [2.61.0](https://github.com/NaturalCycles/backend-lib/compare/v2.60.4...v2.61.0) (2021-10-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * clarify startServer cfg, use process.uptime() ([9185603](https://github.com/NaturalCycles/backend-lib/commit/9185603bec23f5bcccfe764125d8af1efcfc3c11))
7
+
1
8
  ## [2.60.4](https://github.com/NaturalCycles/backend-lib/compare/v2.60.3...v2.60.4) (2021-10-21)
2
9
 
3
10
 
@@ -10,7 +10,7 @@ class BackendServer {
10
10
  this.cfg = cfg;
11
11
  }
12
12
  async start() {
13
- const { bootstrapStartedAt = Date.now(), port: cfgPort, expressApp } = this.cfg;
13
+ const { port: cfgPort, expressApp } = this.cfg;
14
14
  // 1. Register error handlers, etc.
15
15
  process.on('uncaughtException', err => {
16
16
  log_1.log.error('uncaughtException:', err);
@@ -32,15 +32,21 @@ class BackendServer {
32
32
  });
33
33
  // This is to fix GCP LoadBalancer race condition
34
34
  this.server.keepAliveTimeout = 600 * 1000; // 10 minutes
35
- const serverStartedAt = Date.now();
36
- const bootstrapMillis = serverStartedAt - bootstrapStartedAt;
37
- (0, log_1.log)(`serverStarted on port ${(0, colors_1.white)(String(port))}, bootstrapTime ${(0, colors_1.dimGrey)((0, js_lib_1._ms)(bootstrapMillis))}`);
35
+ let address = `http://localhost:${port}`; // default
36
+ const addr = this.server.address();
37
+ if (addr) {
38
+ if (typeof addr === 'string') {
39
+ address = addr;
40
+ }
41
+ else if (addr.address !== '::') {
42
+ address = `http://${addr.address}:${port}`;
43
+ }
44
+ }
45
+ (0, log_1.log)(`serverStarted on ${(0, colors_1.white)(address)} in ${(0, colors_1.dimGrey)((0, js_lib_1._ms)(process.uptime() * 1000))}`);
38
46
  return {
39
47
  port,
40
- bootstrapStartedAt,
41
- serverStartedAt,
42
- bootstrapMillis,
43
48
  server: this.server,
49
+ address,
44
50
  };
45
51
  }
46
52
  /**
@@ -53,9 +59,7 @@ class BackendServer {
53
59
  (0, log_1.log)((0, colors_1.boldGrey)('Forceful shutdown after timeout'));
54
60
  process.exit(1);
55
61
  }, this.cfg.forceShutdownTimeout || 3000);
56
- if (this.cfg.onShutdown) {
57
- void this.cfg.onShutdown();
58
- }
62
+ void this.cfg.onShutdown?.();
59
63
  try {
60
64
  if (this.server) {
61
65
  await new Promise(r => this.server.close(r));
@@ -64,7 +68,7 @@ class BackendServer {
64
68
  process.exit(0);
65
69
  }
66
70
  catch (err) {
67
- log_1.log.error(err);
71
+ console.error(err);
68
72
  process.exit(1);
69
73
  }
70
74
  }
@@ -6,12 +6,6 @@ export interface StartServerCfg {
6
6
  * @default process.env.PORT || 8080
7
7
  */
8
8
  port?: number;
9
- /**
10
- * Unix millisecond timestamp of when bootstrap has started.
11
- *
12
- * @default to Date.now()
13
- */
14
- bootstrapStartedAt?: number;
15
9
  expressApp: Application;
16
10
  /**
17
11
  * Server will wait for promise to resolve until shutting down.
@@ -25,8 +19,9 @@ export interface StartServerCfg {
25
19
  }
26
20
  export interface StartServerData {
27
21
  port: number;
28
- bootstrapStartedAt: number;
29
- serverStartedAt: number;
30
- bootstrapMillis: number;
31
22
  server: Server;
23
+ /**
24
+ * "Processed" server.address() as a string, ready to Cmd+click in MacOS Terminal
25
+ */
26
+ address: string;
32
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "2.60.4",
3
+ "version": "2.61.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -7,13 +7,6 @@ export interface StartServerCfg {
7
7
  */
8
8
  port?: number
9
9
 
10
- /**
11
- * Unix millisecond timestamp of when bootstrap has started.
12
- *
13
- * @default to Date.now()
14
- */
15
- bootstrapStartedAt?: number
16
-
17
10
  expressApp: Application
18
11
 
19
12
  /**
@@ -30,8 +23,9 @@ export interface StartServerCfg {
30
23
 
31
24
  export interface StartServerData {
32
25
  port: number
33
- bootstrapStartedAt: number
34
- serverStartedAt: number
35
- bootstrapMillis: number
36
26
  server: Server
27
+ /**
28
+ * "Processed" server.address() as a string, ready to Cmd+click in MacOS Terminal
29
+ */
30
+ address: string
37
31
  }
@@ -10,7 +10,7 @@ export class BackendServer {
10
10
  server?: Server
11
11
 
12
12
  async start(): Promise<StartServerData> {
13
- const { bootstrapStartedAt = Date.now(), port: cfgPort, expressApp } = this.cfg
13
+ const { port: cfgPort, expressApp } = this.cfg
14
14
 
15
15
  // 1. Register error handlers, etc.
16
16
  process.on('uncaughtException', err => {
@@ -39,21 +39,23 @@ export class BackendServer {
39
39
  // This is to fix GCP LoadBalancer race condition
40
40
  this.server.keepAliveTimeout = 600 * 1000 // 10 minutes
41
41
 
42
- const serverStartedAt = Date.now()
42
+ let address = `http://localhost:${port}` // default
43
43
 
44
- const bootstrapMillis = serverStartedAt - bootstrapStartedAt
45
- log(
46
- `serverStarted on port ${white(String(port))}, bootstrapTime ${dimGrey(
47
- _ms(bootstrapMillis),
48
- )}`,
49
- )
44
+ const addr = this.server.address()
45
+ if (addr) {
46
+ if (typeof addr === 'string') {
47
+ address = addr
48
+ } else if (addr.address !== '::') {
49
+ address = `http://${addr.address}:${port}`
50
+ }
51
+ }
52
+
53
+ log(`serverStarted on ${white(address)} in ${dimGrey(_ms(process.uptime() * 1000))}`)
50
54
 
51
55
  return {
52
56
  port,
53
- bootstrapStartedAt,
54
- serverStartedAt,
55
- bootstrapMillis,
56
57
  server: this.server,
58
+ address,
57
59
  }
58
60
  }
59
61
 
@@ -70,9 +72,7 @@ export class BackendServer {
70
72
  process.exit(1)
71
73
  }, this.cfg.forceShutdownTimeout || 3000)
72
74
 
73
- if (this.cfg.onShutdown) {
74
- void this.cfg.onShutdown()
75
- }
75
+ void this.cfg.onShutdown?.()
76
76
 
77
77
  try {
78
78
  if (this.server) {
@@ -81,7 +81,7 @@ export class BackendServer {
81
81
  log(dimGrey('Shutdown completed.'))
82
82
  process.exit(0)
83
83
  } catch (err) {
84
- log.error(err)
84
+ console.error(err)
85
85
  process.exit(1)
86
86
  }
87
87
  }