@naturalcycles/backend-lib 5.7.0 → 5.7.2

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.
@@ -21,8 +21,9 @@ function requireSecureHeaderOrAdmin(cfg, reqPermissions) {
21
21
  return next();
22
22
  // Header provided - don't check for Admin
23
23
  if (providedHeader) {
24
- if (!secureHeaderValue || (0, nodejs_lib_1.timingSafeStringEqual)(providedHeader, secureHeaderValue))
24
+ if (!secureHeaderValue || (0, nodejs_lib_1.timingSafeStringEqual)(providedHeader, secureHeaderValue)) {
25
25
  return next();
26
+ }
26
27
  return next(new js_lib_1.AppError('secureHeader or adminToken is required', {
27
28
  backendResponseStatusCode: 401,
28
29
  adminAuthRequired: true,
@@ -68,7 +68,7 @@ async function createDeployInfo(backendCfg, overrideBranch) {
68
68
  serviceUrl,
69
69
  gitBranch,
70
70
  gitRev,
71
- ts: now.unix(),
71
+ ts: now.unix,
72
72
  };
73
73
  console.log({ deployInfo });
74
74
  return deployInfo;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deployGae = deployGae;
4
4
  exports.undeployGae = undeployGae;
5
- const dev_lib_1 = require("@naturalcycles/dev-lib");
6
5
  const js_lib_1 = require("@naturalcycles/js-lib");
7
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
7
  const backend_cfg_util_1 = require("./backend.cfg.util");
@@ -11,8 +10,8 @@ const deployHealthCheck_1 = require("./deployHealthCheck");
11
10
  const deployPrepare_1 = require("./deployPrepare");
12
11
  async function deployGae(opt = {}) {
13
12
  const { logOnFailure, logOnSuccess } = opt;
14
- // 1. build-prod
15
- (0, dev_lib_1.buildProdCommand)();
13
+ // 1. build
14
+ (0, nodejs_lib_1.execVoidCommandSync)('yarn', ['build']);
16
15
  // 2. deploy-prepare
17
16
  const deployInfo = await (0, deployPrepare_1.deployPrepare)();
18
17
  const targetDir = './tmp/deploy';
@@ -4,5 +4,5 @@ import { createAppYaml, createDeployInfo } from './deploy.util';
4
4
  import { deployGae } from './deployGae';
5
5
  import { deployHealthCheck, DeployHealthCheckOptions } from './deployHealthCheck';
6
6
  import { deployPrepare } from './deployPrepare';
7
- export type { BackendCfg, DeployInfo, DeployHealthCheckOptions };
8
- export { getBackendCfg, createDeployInfo, createAppYaml, deployGae, deployPrepare, deployHealthCheck, };
7
+ export type { BackendCfg, DeployHealthCheckOptions, DeployInfo };
8
+ export { createAppYaml, createDeployInfo, deployGae, deployHealthCheck, deployPrepare, getBackendCfg, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deployHealthCheck = exports.deployPrepare = exports.deployGae = exports.createAppYaml = exports.createDeployInfo = exports.getBackendCfg = void 0;
3
+ exports.getBackendCfg = exports.deployPrepare = exports.deployHealthCheck = exports.deployGae = exports.createDeployInfo = exports.createAppYaml = void 0;
4
4
  const backend_cfg_util_1 = require("./backend.cfg.util");
5
5
  Object.defineProperty(exports, "getBackendCfg", { enumerable: true, get: function () { return backend_cfg_util_1.getBackendCfg; } });
6
6
  const deploy_util_1 = require("./deploy.util");
package/dist/index.d.ts CHANGED
@@ -7,30 +7,30 @@ export * from './env/env.model';
7
7
  export * from './env/env.shared.service';
8
8
  export * from './gae/appEngine.util';
9
9
  export * from './sentry/sentry.shared.service';
10
+ export * from './server/appEngineLogMiddleware';
11
+ export * from './server/asyncLocalStorageMiddleware';
12
+ export * from './server/basicAuthMiddleware';
13
+ export * from './server/bodyParserTimeoutMiddleware';
10
14
  export * from './server/catchWrapper';
11
15
  export * from './server/createDefaultApp';
12
16
  export * from './server/createDefaultApp.model';
13
17
  export * from './server/deployInfo.util';
14
- export * from './server/getDefaultRouter';
15
- export * from './server/bodyParserTimeoutMiddleware';
16
18
  export * from './server/genericErrorMiddleware';
17
- export * from './server/serverStatsMiddleware';
19
+ export * from './server/getDefaultRouter';
18
20
  export * from './server/methodOverrideMiddleware';
19
21
  export * from './server/notFoundMiddleware';
20
22
  export * from './server/okMiddleware';
21
- export * from './server/basicAuthMiddleware';
23
+ export * from './server/request.log.util';
24
+ export * from './server/request.util';
22
25
  export * from './server/requestTimeoutMiddleware';
23
- export * from './server/validation/validateRequest';
24
- export * from './server/simpleRequestLoggerMiddleware';
26
+ export * from './server/safeJsonMiddleware';
27
+ export * from './server/server.model';
28
+ export * from './server/serverStatsMiddleware';
25
29
  export * from './server/serverStatusMiddleware';
26
- export * from './server/validation/validateMiddleware';
27
- export * from './server/validation/zodValidateMiddleware';
28
- export * from './server/request.log.util';
30
+ export * from './server/simpleRequestLoggerMiddleware';
29
31
  export * from './server/startServer';
30
32
  export * from './server/startServer.model';
31
- export * from './server/asyncLocalStorageMiddleware';
32
- export * from './server/server.model';
33
- export * from './server/appEngineLogMiddleware';
34
- export * from './server/safeJsonMiddleware';
35
- export * from './server/request.util';
33
+ export * from './server/validation/validateMiddleware';
34
+ export * from './server/validation/validateRequest';
35
+ export * from './server/validation/zodValidateMiddleware';
36
36
  export { onFinished };
package/dist/index.js CHANGED
@@ -12,29 +12,29 @@ tslib_1.__exportStar(require("./env/env.model"), exports);
12
12
  tslib_1.__exportStar(require("./env/env.shared.service"), exports);
13
13
  tslib_1.__exportStar(require("./gae/appEngine.util"), exports);
14
14
  tslib_1.__exportStar(require("./sentry/sentry.shared.service"), exports);
15
+ tslib_1.__exportStar(require("./server/appEngineLogMiddleware"), exports);
16
+ tslib_1.__exportStar(require("./server/asyncLocalStorageMiddleware"), exports);
17
+ tslib_1.__exportStar(require("./server/basicAuthMiddleware"), exports);
18
+ tslib_1.__exportStar(require("./server/bodyParserTimeoutMiddleware"), exports);
15
19
  tslib_1.__exportStar(require("./server/catchWrapper"), exports);
16
20
  tslib_1.__exportStar(require("./server/createDefaultApp"), exports);
17
21
  tslib_1.__exportStar(require("./server/createDefaultApp.model"), exports);
18
22
  tslib_1.__exportStar(require("./server/deployInfo.util"), exports);
19
- tslib_1.__exportStar(require("./server/getDefaultRouter"), exports);
20
- tslib_1.__exportStar(require("./server/bodyParserTimeoutMiddleware"), exports);
21
23
  tslib_1.__exportStar(require("./server/genericErrorMiddleware"), exports);
22
- tslib_1.__exportStar(require("./server/serverStatsMiddleware"), exports);
24
+ tslib_1.__exportStar(require("./server/getDefaultRouter"), exports);
23
25
  tslib_1.__exportStar(require("./server/methodOverrideMiddleware"), exports);
24
26
  tslib_1.__exportStar(require("./server/notFoundMiddleware"), exports);
25
27
  tslib_1.__exportStar(require("./server/okMiddleware"), exports);
26
- tslib_1.__exportStar(require("./server/basicAuthMiddleware"), exports);
28
+ tslib_1.__exportStar(require("./server/request.log.util"), exports);
29
+ tslib_1.__exportStar(require("./server/request.util"), exports);
27
30
  tslib_1.__exportStar(require("./server/requestTimeoutMiddleware"), exports);
28
- tslib_1.__exportStar(require("./server/validation/validateRequest"), exports);
29
- tslib_1.__exportStar(require("./server/simpleRequestLoggerMiddleware"), exports);
31
+ tslib_1.__exportStar(require("./server/safeJsonMiddleware"), exports);
32
+ tslib_1.__exportStar(require("./server/server.model"), exports);
33
+ tslib_1.__exportStar(require("./server/serverStatsMiddleware"), exports);
30
34
  tslib_1.__exportStar(require("./server/serverStatusMiddleware"), exports);
31
- tslib_1.__exportStar(require("./server/validation/validateMiddleware"), exports);
32
- tslib_1.__exportStar(require("./server/validation/zodValidateMiddleware"), exports);
33
- tslib_1.__exportStar(require("./server/request.log.util"), exports);
35
+ tslib_1.__exportStar(require("./server/simpleRequestLoggerMiddleware"), exports);
34
36
  tslib_1.__exportStar(require("./server/startServer"), exports);
35
37
  tslib_1.__exportStar(require("./server/startServer.model"), exports);
36
- tslib_1.__exportStar(require("./server/asyncLocalStorageMiddleware"), exports);
37
- tslib_1.__exportStar(require("./server/server.model"), exports);
38
- tslib_1.__exportStar(require("./server/appEngineLogMiddleware"), exports);
39
- tslib_1.__exportStar(require("./server/safeJsonMiddleware"), exports);
40
- tslib_1.__exportStar(require("./server/request.util"), exports);
38
+ tslib_1.__exportStar(require("./server/validation/validateMiddleware"), exports);
39
+ tslib_1.__exportStar(require("./server/validation/validateRequest"), exports);
40
+ tslib_1.__exportStar(require("./server/validation/zodValidateMiddleware"), exports);
@@ -98,8 +98,9 @@ class SentrySharedService {
98
98
  return;
99
99
  }
100
100
  if (data?.reportRate && // E.g rate of 0.1 means 10% of errors are reported
101
- Math.random() > data.reportRate)
101
+ Math.random() > data.reportRate) {
102
102
  return;
103
+ }
103
104
  // This is to avoid Sentry cutting err.message to 253 characters
104
105
  // It will log additional "breadcrumb object" before the error
105
106
  // It's a Breadcrumb, not a console.log, because console.log are NOT automatically attached as Breadcrumbs in cron-job environments (outside of Express)
@@ -1,5 +1,5 @@
1
1
  import type { CommonLogFunction, Promisable } from '@naturalcycles/js-lib';
2
- import type { IRouter, Request, NextFunction, Response, Application } from 'express';
2
+ import type { Application, IRouter, NextFunction, Request, Response } from 'express';
3
3
  /**
4
4
  * Use this interface instead of express.Request in cases when TypeScript gives an error, because it haven't "included" this very file.
5
5
  *
@@ -36,5 +36,5 @@ function getServerStatusData(projectDir = process.cwd(), extra) {
36
36
  }
37
37
  function getStartedStr() {
38
38
  const started = js_lib_1.localTime.now().minus(process.uptime(), 'second');
39
- return `${started.toPretty()} (${started.fromNow()})`;
39
+ return `${started.toPretty()} (${started.toFromNowString()})`;
40
40
  }
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "5.7.0",
3
+ "version": "5.7.2",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
+ "build": "dev-lib build",
7
+ "test": "dev-lib test",
8
+ "lint": "dev-lib lint",
9
+ "bt": "dev-lib bt",
10
+ "lbt": "dev-lib lbt",
6
11
  "dev": "APP_ENV=dev node -r ts-node/register --watch ./src/test/server/server.ts",
7
12
  "docs-serve": "NODE_OPTIONS=--openssl-legacy-provider vuepress dev docs",
8
13
  "docs-build": "NODE_OPTIONS=--openssl-legacy-provider vuepress build docs",
@@ -17,7 +22,7 @@
17
22
  },
18
23
  "dependencies": {
19
24
  "@naturalcycles/db-lib": "^9.1.0",
20
- "@naturalcycles/js-lib": "^14.27.0",
25
+ "@naturalcycles/js-lib": "^14.244.0",
21
26
  "@naturalcycles/nodejs-lib": "^13.1.0",
22
27
  "@types/cookie-parser": "^1.4.1",
23
28
  "@types/cors": "^2.8.4",
@@ -37,7 +42,7 @@
37
42
  },
38
43
  "devDependencies": {
39
44
  "@naturalcycles/bench-lib": "^3.0.0",
40
- "@naturalcycles/dev-lib": "^13.0.0",
45
+ "@naturalcycles/dev-lib": "^15.0.4",
41
46
  "@sentry/node": "^7.0.0",
42
47
  "@types/ejs": "^3.0.0",
43
48
  "@types/node": "^20.1.0",
@@ -71,7 +76,7 @@
71
76
  "url": "https://github.com/NaturalCycles/backend-lib"
72
77
  },
73
78
  "engines": {
74
- "node": ">=18.12.0"
79
+ "node": ">=20.12.0"
75
80
  },
76
81
  "type": "commonjs",
77
82
  "description": "Standard library for making Express.js / AppEngine based backend services",
@@ -42,8 +42,9 @@ function requireSecureHeaderOrAdmin(
42
42
 
43
43
  // Header provided - don't check for Admin
44
44
  if (providedHeader) {
45
- if (!secureHeaderValue || timingSafeStringEqual(providedHeader, secureHeaderValue))
45
+ if (!secureHeaderValue || timingSafeStringEqual(providedHeader, secureHeaderValue)) {
46
46
  return next()
47
+ }
47
48
 
48
49
  return next(
49
50
  new AppError('secureHeader or adminToken is required', {
@@ -90,7 +90,7 @@ export async function createDeployInfo(
90
90
  serviceUrl,
91
91
  gitBranch,
92
92
  gitRev,
93
- ts: now.unix(),
93
+ ts: now.unix,
94
94
  }
95
95
 
96
96
  console.log({ deployInfo })
@@ -1,4 +1,3 @@
1
- import { buildProdCommand } from '@naturalcycles/dev-lib'
2
1
  import { _anyToError, _objectAssign, pRetry } from '@naturalcycles/js-lib'
3
2
  import { appendToGithubSummary, execVoidCommandSync } from '@naturalcycles/nodejs-lib'
4
3
  import { getBackendCfg } from './backend.cfg.util'
@@ -11,9 +10,9 @@ export interface DeployGaeOptions extends DeployPrepareOptions, DeployHealthChec
11
10
  export async function deployGae(opt: DeployGaeOptions = {}): Promise<void> {
12
11
  const { logOnFailure, logOnSuccess } = opt
13
12
 
14
- // 1. build-prod
13
+ // 1. build
15
14
 
16
- buildProdCommand()
15
+ execVoidCommandSync('yarn', ['build'])
17
16
 
18
17
  // 2. deploy-prepare
19
18
 
@@ -1,6 +1,6 @@
1
1
  import { inspect, InspectOptions } from 'node:util'
2
- import { pDelay, _filterFalsyValues, _ms, _since, getFetcher } from '@naturalcycles/js-lib'
3
- import { execVoidCommandSync, dimGrey, red } from '@naturalcycles/nodejs-lib'
2
+ import { _filterFalsyValues, _ms, _since, getFetcher, pDelay } from '@naturalcycles/js-lib'
3
+ import { dimGrey, execVoidCommandSync, red } from '@naturalcycles/nodejs-lib'
4
4
  import { coloredHttpCode } from '../server/request.log.util'
5
5
 
6
6
  export interface DeployHealthCheckOptions {
@@ -5,13 +5,13 @@ import { deployGae } from './deployGae'
5
5
  import { deployHealthCheck, DeployHealthCheckOptions } from './deployHealthCheck'
6
6
  import { deployPrepare } from './deployPrepare'
7
7
 
8
- export type { BackendCfg, DeployInfo, DeployHealthCheckOptions }
8
+ export type { BackendCfg, DeployHealthCheckOptions, DeployInfo }
9
9
 
10
10
  export {
11
- getBackendCfg,
12
- createDeployInfo,
13
11
  createAppYaml,
12
+ createDeployInfo,
14
13
  deployGae,
15
- deployPrepare,
16
14
  deployHealthCheck,
15
+ deployPrepare,
16
+ getBackendCfg,
17
17
  }
package/src/index.ts CHANGED
@@ -7,31 +7,31 @@ export * from './env/env.model'
7
7
  export * from './env/env.shared.service'
8
8
  export * from './gae/appEngine.util'
9
9
  export * from './sentry/sentry.shared.service'
10
+ export * from './server/appEngineLogMiddleware'
11
+ export * from './server/asyncLocalStorageMiddleware'
12
+ export * from './server/basicAuthMiddleware'
13
+ export * from './server/bodyParserTimeoutMiddleware'
10
14
  export * from './server/catchWrapper'
11
15
  export * from './server/createDefaultApp'
12
16
  export * from './server/createDefaultApp.model'
13
17
  export * from './server/deployInfo.util'
14
- export * from './server/getDefaultRouter'
15
- export * from './server/bodyParserTimeoutMiddleware'
16
18
  export * from './server/genericErrorMiddleware'
17
- export * from './server/serverStatsMiddleware'
19
+ export * from './server/getDefaultRouter'
18
20
  export * from './server/methodOverrideMiddleware'
19
21
  export * from './server/notFoundMiddleware'
20
22
  export * from './server/okMiddleware'
21
- export * from './server/basicAuthMiddleware'
23
+ export * from './server/request.log.util'
24
+ export * from './server/request.util'
22
25
  export * from './server/requestTimeoutMiddleware'
23
- export * from './server/validation/validateRequest'
24
- export * from './server/simpleRequestLoggerMiddleware'
26
+ export * from './server/safeJsonMiddleware'
27
+ export * from './server/server.model'
28
+ export * from './server/serverStatsMiddleware'
25
29
  export * from './server/serverStatusMiddleware'
26
- export * from './server/validation/validateMiddleware'
27
- export * from './server/validation/zodValidateMiddleware'
28
- export * from './server/request.log.util'
30
+ export * from './server/simpleRequestLoggerMiddleware'
29
31
  export * from './server/startServer'
30
32
  export * from './server/startServer.model'
31
- export * from './server/asyncLocalStorageMiddleware'
32
- export * from './server/server.model'
33
- export * from './server/appEngineLogMiddleware'
34
- export * from './server/safeJsonMiddleware'
35
- export * from './server/request.util'
33
+ export * from './server/validation/validateMiddleware'
34
+ export * from './server/validation/validateRequest'
35
+ export * from './server/validation/zodValidateMiddleware'
36
36
 
37
37
  export { onFinished }
@@ -126,8 +126,9 @@ export class SentrySharedService {
126
126
  if (
127
127
  data?.reportRate && // E.g rate of 0.1 means 10% of errors are reported
128
128
  Math.random() > data.reportRate
129
- )
129
+ ) {
130
130
  return
131
+ }
131
132
 
132
133
  // This is to avoid Sentry cutting err.message to 253 characters
133
134
  // It will log additional "breadcrumb object" before the error
@@ -1,6 +1,6 @@
1
1
  import { inspect } from 'node:util'
2
2
  import { AnyObject, CommonLogger } from '@naturalcycles/js-lib'
3
- import { dimGrey, _inspect } from '@naturalcycles/nodejs-lib'
3
+ import { _inspect, dimGrey } from '@naturalcycles/nodejs-lib'
4
4
  import { BackendRequestHandler } from './server.model'
5
5
 
6
6
  const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE } = process.env
@@ -1,6 +1,6 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks'
2
2
  import { _lazyValue, CommonLogger } from '@naturalcycles/js-lib'
3
- import { gaeLogger, devLogger, ciLogger } from './appEngineLogMiddleware'
3
+ import { ciLogger, devLogger, gaeLogger } from './appEngineLogMiddleware'
4
4
  import { BackendRequest, BackendRequestHandler } from './server.model'
5
5
 
6
6
  const { GAE_INSTANCE, CI } = process.env
@@ -5,9 +5,9 @@ import { BackendApplication, isGAE, methodOverrideMiddleware } from '..'
5
5
  import { appEngineLogMiddleware } from './appEngineLogMiddleware'
6
6
  import { asyncLocalStorageMiddleware } from './asyncLocalStorageMiddleware'
7
7
  import {
8
- DefaultAppCfg,
9
8
  BackendRequestHandlerCfg,
10
9
  BackendRequestHandlerWithPath,
10
+ DefaultAppCfg,
11
11
  } from './createDefaultApp.model'
12
12
  import { genericErrorMiddleware } from './genericErrorMiddleware'
13
13
  import { notFoundMiddleware } from './notFoundMiddleware'
@@ -1,5 +1,5 @@
1
1
  import type { CommonLogFunction, Promisable } from '@naturalcycles/js-lib'
2
- import type { IRouter, Request, NextFunction, Response, Application } from 'express'
2
+ import type { Application, IRouter, NextFunction, Request, Response } from 'express'
3
3
 
4
4
  /**
5
5
  * Use this interface instead of express.Request in cases when TypeScript gives an error, because it haven't "included" this very file.
@@ -41,5 +41,5 @@ export function getServerStatusData(
41
41
 
42
42
  function getStartedStr(): string {
43
43
  const started = localTime.now().minus(process.uptime(), 'second')
44
- return `${started.toPretty()} (${started.fromNow()})`
44
+ return `${started.toPretty()} (${started.toFromNowString()})`
45
45
  }
@@ -1,4 +1,4 @@
1
- import { JsonSchema, JsonSchemaBuilder, _get, AppError } from '@naturalcycles/js-lib'
1
+ import { _get, AppError, JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib'
2
2
  import { AjvSchema, AjvValidationError } from '@naturalcycles/nodejs-lib'
3
3
  import { BackendRequestHandler } from '../server.model'
4
4
  import { ReqValidationOptions } from './validateRequest'