@naturalcycles/backend-lib 2.60.3 → 2.60.4

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.60.4](https://github.com/NaturalCycles/backend-lib/compare/v2.60.3...v2.60.4) (2021-10-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * deps, use process.uptime in statusHandler ([8d3389d](https://github.com/NaturalCycles/backend-lib/commit/8d3389d8908954af9a375a6d7fb95aefc1b08e5f))
7
+
1
8
  ## [2.60.3](https://github.com/NaturalCycles/backend-lib/compare/v2.60.2...v2.60.3) (2021-10-15)
2
9
 
3
10
 
@@ -1,5 +1,5 @@
1
- import { JsonSchemaRootObject } from '@naturalcycles/js-lib';
2
- import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBQuery, ObjectWithId, RunQueryResult } from '@naturalcycles/db-lib';
1
+ import { JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
2
+ import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
3
3
  import { GetGotOptions, ReadableTyped } from '@naturalcycles/nodejs-lib';
4
4
  export interface HttpDBCfg extends GetGotOptions {
5
5
  prefixUrl: string;
@@ -1,4 +1,5 @@
1
- import { CommonDB, CommonDBOptions, CommonDBSaveOptions, DBQuery, ObjectWithId } from '@naturalcycles/db-lib';
1
+ import { CommonDB, CommonDBOptions, CommonDBSaveOptions, DBQuery } from '@naturalcycles/db-lib';
2
+ import { ObjectWithId } from '@naturalcycles/js-lib';
2
3
  import { Router } from 'express';
3
4
  export interface GetByIdsInput {
4
5
  table: string;
package/dist/index.d.ts CHANGED
@@ -32,13 +32,3 @@ import { BackendServer, startServer } from './server/startServer';
32
32
  import { StartServerCfg, StartServerData } from './server/startServer.model';
33
33
  export type { MethodOverrideCfg, SentrySharedServiceCfg, RequestHandlerWithPath, RequestHandlerCfg, DefaultAppCfg, StartServerCfg, StartServerData, EnvSharedServiceCfg, BaseEnv, AdminMiddleware, AdminServiceCfg, AdminInfo, RequireAdminCfg, SecureHeaderMiddlewareCfg, BodyParserTimeoutCfg, RequestTimeoutCfg, SimpleRequestLoggerCfg, ReqValidationOptions, };
34
34
  export { BackendServer, SentrySharedService, EnvSharedService, reqValidation, notFoundHandler, genericErrorHandler, methodOverride, sentryErrorHandler, createDefaultApp, startServer, catchWrapper, getDefaultRouter, isGAE, statusHandler, statusHandlerData, okHandler, getDeployInfo, onFinished, respondWithError, logRequest, FirebaseSharedService, createAdminMiddleware, BaseAdminService, loginHtml, createSecureHeaderMiddleware, bodyParserTimeout, clearBodyParserTimeout, requestTimeout, simpleRequestLogger, coloredHttpCode, getRequestContextProperty, setRequestContextProperty, requestContextMiddleware, requestIdMiddleware, REQUEST_ID_KEY, validateBody, validateParams, validateQuery, };
35
- declare global {
36
- namespace NodeJS {
37
- interface ProcessEnv {
38
- PORT?: string;
39
- GAE_APPLICATION?: string;
40
- GAE_SERVICE?: string;
41
- GAE_VERSION?: string;
42
- }
43
- }
44
- }
@@ -11,6 +11,7 @@ function reqValidation(reqProperty, schema, opt = {}) {
11
11
  if (opt.redactPaths) {
12
12
  redact(opt.redactPaths, req[reqProperty], error);
13
13
  error.data.joiValidationErrorItems.length = 0; // clears the array
14
+ delete error.data.annotation;
14
15
  }
15
16
  return next(new js_lib_1.HttpError(error.message, {
16
17
  httpStatusCode: 400,
@@ -1,8 +1,3 @@
1
1
  import { RequestHandler } from 'express';
2
- /**
3
- * @returns unix timestamp in millis
4
- */
5
- declare type ServerStartedCallback = () => number | undefined;
6
- export declare function statusHandler(serverStartedCallback?: ServerStartedCallback, projectDir?: string, extra?: any): RequestHandler;
7
- export declare function statusHandlerData(serverStartedCallback?: ServerStartedCallback, projectDir?: string, extra?: any): Record<string, any>;
8
- export {};
2
+ export declare function statusHandler(projectDir?: string, extra?: any): RequestHandler;
3
+ export declare function statusHandlerData(projectDir?: string, extra?: any): Record<string, any>;
@@ -5,29 +5,28 @@ const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
6
  const time_lib_1 = require("@naturalcycles/time-lib");
7
7
  const deployInfo_util_1 = require("../deployInfo.util");
8
- const now = Date.now();
9
- const defaultServerStartedCallback = () => now;
10
8
  const { versions } = process;
11
- function statusHandler(serverStartedCallback, projectDir, extra) {
9
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION } = process.env;
10
+ function statusHandler(projectDir, extra) {
12
11
  return async (req, res) => {
13
- res.json(statusHandlerData(serverStartedCallback, projectDir, extra));
12
+ res.json(statusHandlerData(projectDir, extra));
14
13
  };
15
14
  }
16
15
  exports.statusHandler = statusHandler;
17
- function statusHandlerData(serverStartedCallback = defaultServerStartedCallback, projectDir = process.cwd(), extra) {
16
+ function statusHandlerData(projectDir = process.cwd(), extra) {
18
17
  const { APP_ENV } = process.env;
19
18
  const { gitRev, gitBranch, prod, ts } = (0, deployInfo_util_1.getDeployInfo)(projectDir);
20
19
  const deployBuildTimeUTC = time_lib_1.dayjs.unix(ts).toPretty();
21
20
  const buildInfo = [time_lib_1.dayjs.unix(ts).toCompactTime(), gitBranch, gitRev].filter(Boolean).join('_');
22
21
  return (0, js_lib_1._filterFalsyValues)({
23
- started: getStartedStr(serverStartedCallback()),
22
+ started: getStartedStr(),
24
23
  deployBuildTimeUTC,
25
24
  APP_ENV,
26
25
  prod,
27
26
  buildInfo,
28
- GAE_APPLICATION: process.env.GAE_APPLICATION,
29
- GAE_SERVICE: process.env.GAE_SERVICE,
30
- GAE_VERSION: process.env.GAE_VERSION,
27
+ GAE_APPLICATION,
28
+ GAE_SERVICE,
29
+ GAE_VERSION,
31
30
  mem: (0, nodejs_lib_1.memoryUsageFull)(),
32
31
  cpuAvg: nodejs_lib_1.processSharedUtil.cpuAvg(),
33
32
  // resourceUsage: process.resourceUsage?.(),
@@ -36,9 +35,8 @@ function statusHandlerData(serverStartedCallback = defaultServerStartedCallback,
36
35
  });
37
36
  }
38
37
  exports.statusHandlerData = statusHandlerData;
39
- function getStartedStr(serverStarted) {
40
- if (!serverStarted)
41
- return 'not started yet';
38
+ function getStartedStr() {
39
+ const serverStarted = time_lib_1.dayjs.utc().subtract(process.uptime(), 's');
42
40
  const s1 = (0, time_lib_1.dayjs)(serverStarted).toPretty();
43
41
  const s2 = (0, time_lib_1.dayjs)(serverStarted).fromNow();
44
42
  return `${s1} UTC (${s2})`;
@@ -22,7 +22,7 @@ class BackendServer {
22
22
  process.once('SIGTERM', () => this.stop());
23
23
  // sentryService.install()
24
24
  // 2. Start Express Server
25
- const port = Number(process.env.PORT) || cfgPort || 8080;
25
+ const port = Number(process.env['PORT']) || cfgPort || 8080;
26
26
  this.server = await new Promise((resolve, reject) => {
27
27
  const server = expressApp.listen(port, (err) => {
28
28
  if (err)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "2.60.3",
3
+ "version": "2.60.4",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
package/src/db/httpDB.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Readable } from 'stream'
2
- import { JsonSchemaRootObject } from '@naturalcycles/js-lib'
2
+ import { JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
3
3
  import {
4
4
  BaseCommonDB,
5
5
  CommonDB,
@@ -7,7 +7,6 @@ import {
7
7
  CommonDBSaveOptions,
8
8
  CommonDBStreamOptions,
9
9
  DBQuery,
10
- ObjectWithId,
11
10
  RunQueryResult,
12
11
  } from '@naturalcycles/db-lib'
13
12
  import { getGot, GetGotOptions, Got, ReadableTyped } from '@naturalcycles/nodejs-lib'
@@ -4,13 +4,13 @@ import {
4
4
  CommonDBSaveOptions,
5
5
  DBQuery,
6
6
  InMemoryDB,
7
- ObjectWithId,
8
7
  } from '@naturalcycles/db-lib'
9
8
  import {
10
9
  commonDBOptionsSchema,
11
10
  commonDBSaveOptionsSchema,
12
11
  dbQuerySchema,
13
12
  } from '@naturalcycles/db-lib/dist/validation'
13
+ import { ObjectWithId } from '@naturalcycles/js-lib'
14
14
  import { anyObjectSchema, arraySchema, objectSchema, stringSchema } from '@naturalcycles/nodejs-lib'
15
15
  import { Router } from 'express'
16
16
  import { getDefaultRouter, reqValidation } from '..'
package/src/index.ts CHANGED
@@ -112,14 +112,3 @@ export {
112
112
  validateParams,
113
113
  validateQuery,
114
114
  }
115
-
116
- declare global {
117
- namespace NodeJS {
118
- interface ProcessEnv {
119
- PORT?: string
120
- GAE_APPLICATION?: string
121
- GAE_SERVICE?: string
122
- GAE_VERSION?: string
123
- }
124
- }
125
- }
@@ -23,6 +23,7 @@ export function reqValidation(
23
23
  if (opt.redactPaths) {
24
24
  redact(opt.redactPaths, req[reqProperty], error)
25
25
  error.data.joiValidationErrorItems.length = 0 // clears the array
26
+ delete error.data.annotation
26
27
  }
27
28
 
28
29
  return next(
@@ -4,27 +4,16 @@ import { dayjs } from '@naturalcycles/time-lib'
4
4
  import { RequestHandler } from 'express'
5
5
  import { getDeployInfo } from '../deployInfo.util'
6
6
 
7
- /**
8
- * @returns unix timestamp in millis
9
- */
10
- type ServerStartedCallback = () => number | undefined
11
-
12
- const now = Date.now()
13
- const defaultServerStartedCallback = () => now
14
7
  const { versions } = process
8
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION } = process.env
15
9
 
16
- export function statusHandler(
17
- serverStartedCallback?: ServerStartedCallback,
18
- projectDir?: string,
19
- extra?: any,
20
- ): RequestHandler {
10
+ export function statusHandler(projectDir?: string, extra?: any): RequestHandler {
21
11
  return async (req, res) => {
22
- res.json(statusHandlerData(serverStartedCallback, projectDir, extra))
12
+ res.json(statusHandlerData(projectDir, extra))
23
13
  }
24
14
  }
25
15
 
26
16
  export function statusHandlerData(
27
- serverStartedCallback: ServerStartedCallback = defaultServerStartedCallback,
28
17
  projectDir: string = process.cwd(),
29
18
  extra?: any,
30
19
  ): Record<string, any> {
@@ -34,14 +23,14 @@ export function statusHandlerData(
34
23
  const buildInfo = [dayjs.unix(ts).toCompactTime(), gitBranch, gitRev].filter(Boolean).join('_')
35
24
 
36
25
  return _filterFalsyValues({
37
- started: getStartedStr(serverStartedCallback()),
26
+ started: getStartedStr(),
38
27
  deployBuildTimeUTC,
39
28
  APP_ENV,
40
29
  prod,
41
30
  buildInfo,
42
- GAE_APPLICATION: process.env.GAE_APPLICATION,
43
- GAE_SERVICE: process.env.GAE_SERVICE,
44
- GAE_VERSION: process.env.GAE_VERSION,
31
+ GAE_APPLICATION,
32
+ GAE_SERVICE,
33
+ GAE_VERSION,
45
34
  mem: memoryUsageFull(),
46
35
  cpuAvg: processSharedUtil.cpuAvg(),
47
36
  // resourceUsage: process.resourceUsage?.(),
@@ -50,8 +39,8 @@ export function statusHandlerData(
50
39
  })
51
40
  }
52
41
 
53
- function getStartedStr(serverStarted?: number): string {
54
- if (!serverStarted) return 'not started yet'
42
+ function getStartedStr(): string {
43
+ const serverStarted = dayjs.utc().subtract(process.uptime(), 's')
55
44
 
56
45
  const s1 = dayjs(serverStarted).toPretty()
57
46
  const s2 = dayjs(serverStarted).fromNow()
@@ -27,7 +27,7 @@ export class BackendServer {
27
27
  // sentryService.install()
28
28
 
29
29
  // 2. Start Express Server
30
- const port = Number(process.env.PORT) || cfgPort || 8080
30
+ const port = Number(process.env['PORT']) || cfgPort || 8080
31
31
 
32
32
  this.server = await new Promise<Server>((resolve, reject) => {
33
33
  const server = expressApp.listen(port, (err?: Error) => {