@naturalcycles/backend-lib 7.0.0 → 7.1.1

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.
@@ -1,14 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- const tslib_1 = require("tslib");
5
4
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
- const yargs_1 = tslib_1.__importDefault(require("yargs"));
7
5
  const deployGae_1 = require("../deploy/deployGae");
8
6
  const deployHealthCheck_1 = require("../deploy/deployHealthCheck");
9
7
  const deployPrepare_1 = require("../deploy/deployPrepare");
10
8
  (0, nodejs_lib_1.runScript)(async () => {
11
- const opt = yargs_1.default.options({
9
+ const opt = (0, nodejs_lib_1._yargs)().options({
12
10
  ...deployPrepare_1.deployPrepareYargsOptions,
13
11
  ...deployHealthCheck_1.deployHealthCheckYargsOptions,
14
12
  }).argv;
@@ -9,12 +9,10 @@ yarn deploy-health-check --url https://service-dot-yourproject.appspot.com
9
9
 
10
10
  */
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- const tslib_1 = require("tslib");
13
12
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
14
- const yargs_1 = tslib_1.__importDefault(require("yargs"));
15
13
  const deployHealthCheck_1 = require("../deploy/deployHealthCheck");
16
14
  (0, nodejs_lib_1.runScript)(async () => {
17
- const { url, ...opt } = yargs_1.default.options({
15
+ const { url, ...opt } = (0, nodejs_lib_1._yargs)().options({
18
16
  ...deployHealthCheck_1.deployHealthCheckYargsOptions,
19
17
  url: {
20
18
  type: 'string',
@@ -6,12 +6,10 @@ yarn deploy-prepare
6
6
 
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- const tslib_1 = require("tslib");
10
9
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
11
- const yargs_1 = tslib_1.__importDefault(require("yargs"));
12
10
  const deployPrepare_1 = require("../deploy/deployPrepare");
13
11
  (0, nodejs_lib_1.runScript)(async () => {
14
- const opt = yargs_1.default.options(deployPrepare_1.deployPrepareYargsOptions).argv;
12
+ const opt = (0, nodejs_lib_1._yargs)().options(deployPrepare_1.deployPrepareYargsOptions).argv;
15
13
  await (0, deployPrepare_1.deployPrepare)(opt);
16
14
  });
17
15
  // deploy strategy
@@ -1,12 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- const tslib_1 = require("tslib");
5
4
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
- const yargs_1 = tslib_1.__importDefault(require("yargs"));
7
5
  const deployGae_1 = require("../deploy/deployGae");
8
6
  (0, nodejs_lib_1.runScript)(async () => {
9
- const { branch } = yargs_1.default.options({
7
+ const { branch } = (0, nodejs_lib_1._yargs)().options({
10
8
  branch: {
11
9
  type: 'string',
12
10
  demandOption: true,
package/dist/index.d.ts CHANGED
@@ -4,9 +4,7 @@ export * from './admin/base.admin.service';
4
4
  export * from './admin/firebase.shared.service';
5
5
  export * from './admin/secureHeaderMiddleware';
6
6
  export * from './env/env.shared.service';
7
- export * from './gae/appEngine.util';
8
7
  export * from './sentry/sentry.shared.service';
9
- export * from './server/appEngineLogMiddleware';
10
8
  export * from './server/asyncLocalStorageMiddleware';
11
9
  export * from './server/basicAuthMiddleware';
12
10
  export * from './server/bodyParserTimeoutMiddleware';
@@ -16,6 +14,7 @@ export * from './server/createDefaultApp.model';
16
14
  export * from './server/deployInfo.util';
17
15
  export * from './server/genericErrorMiddleware';
18
16
  export * from './server/getDefaultRouter';
17
+ export * from './server/logMiddleware';
19
18
  export * from './server/methodOverrideMiddleware';
20
19
  export * from './server/notFoundMiddleware';
21
20
  export * from './server/okMiddleware';
@@ -32,4 +31,5 @@ export * from './server/startServer.model';
32
31
  export * from './server/validation/validateMiddleware';
33
32
  export * from './server/validation/validateRequest';
34
33
  export * from './server/validation/zodValidateMiddleware';
34
+ export * from './util';
35
35
  export { onFinished };
package/dist/index.js CHANGED
@@ -9,9 +9,7 @@ tslib_1.__exportStar(require("./admin/base.admin.service"), exports);
9
9
  tslib_1.__exportStar(require("./admin/firebase.shared.service"), exports);
10
10
  tslib_1.__exportStar(require("./admin/secureHeaderMiddleware"), exports);
11
11
  tslib_1.__exportStar(require("./env/env.shared.service"), exports);
12
- tslib_1.__exportStar(require("./gae/appEngine.util"), exports);
13
12
  tslib_1.__exportStar(require("./sentry/sentry.shared.service"), exports);
14
- tslib_1.__exportStar(require("./server/appEngineLogMiddleware"), exports);
15
13
  tslib_1.__exportStar(require("./server/asyncLocalStorageMiddleware"), exports);
16
14
  tslib_1.__exportStar(require("./server/basicAuthMiddleware"), exports);
17
15
  tslib_1.__exportStar(require("./server/bodyParserTimeoutMiddleware"), exports);
@@ -21,6 +19,7 @@ tslib_1.__exportStar(require("./server/createDefaultApp.model"), exports);
21
19
  tslib_1.__exportStar(require("./server/deployInfo.util"), exports);
22
20
  tslib_1.__exportStar(require("./server/genericErrorMiddleware"), exports);
23
21
  tslib_1.__exportStar(require("./server/getDefaultRouter"), exports);
22
+ tslib_1.__exportStar(require("./server/logMiddleware"), exports);
24
23
  tslib_1.__exportStar(require("./server/methodOverrideMiddleware"), exports);
25
24
  tslib_1.__exportStar(require("./server/notFoundMiddleware"), exports);
26
25
  tslib_1.__exportStar(require("./server/okMiddleware"), exports);
@@ -37,3 +36,4 @@ tslib_1.__exportStar(require("./server/startServer.model"), exports);
37
36
  tslib_1.__exportStar(require("./server/validation/validateMiddleware"), exports);
38
37
  tslib_1.__exportStar(require("./server/validation/validateRequest"), exports);
39
38
  tslib_1.__exportStar(require("./server/validation/zodValidateMiddleware"), exports);
39
+ tslib_1.__exportStar(require("./util"), exports);
@@ -6,10 +6,11 @@ exports.getRequest = getRequest;
6
6
  exports.getRequestLogger = getRequestLogger;
7
7
  const node_async_hooks_1 = require("node:async_hooks");
8
8
  const js_lib_1 = require("@naturalcycles/js-lib");
9
- const appEngineLogMiddleware_1 = require("./appEngineLogMiddleware");
9
+ const logMiddleware_1 = require("./logMiddleware");
10
10
  const { GAE_INSTANCE, CI } = process.env;
11
11
  const isGAE = !!GAE_INSTANCE;
12
12
  const isCI = !!CI;
13
+ const isCloudRun = !!CI;
13
14
  // Singleton, for simplicity
14
15
  // Create it lazily (on demand)
15
16
  const storage = (0, js_lib_1._lazyValue)(() => new node_async_hooks_1.AsyncLocalStorage());
@@ -30,7 +31,7 @@ function getRequest() {
30
31
  * @experimental
31
32
  */
32
33
  function getRequestLogger() {
33
- return storage().getStore()?.req || (isGAE ? appEngineLogMiddleware_1.gaeLogger : isCI ? appEngineLogMiddleware_1.ciLogger : appEngineLogMiddleware_1.devLogger);
34
+ return (storage().getStore()?.req || (isGAE ? logMiddleware_1.gaeLogger : isCI || isCloudRun ? logMiddleware_1.ciLogger : logMiddleware_1.devLogger));
34
35
  }
35
36
  /**
36
37
  * CommonLogger implementation that is Request-bound.
@@ -5,9 +5,9 @@ const cookieParser = require("cookie-parser");
5
5
  const cors = require("cors");
6
6
  const express = require("express");
7
7
  const __1 = require("..");
8
- const appEngineLogMiddleware_1 = require("./appEngineLogMiddleware");
9
8
  const asyncLocalStorageMiddleware_1 = require("./asyncLocalStorageMiddleware");
10
9
  const genericErrorMiddleware_1 = require("./genericErrorMiddleware");
10
+ const logMiddleware_1 = require("./logMiddleware");
11
11
  const notFoundMiddleware_1 = require("./notFoundMiddleware");
12
12
  const requestTimeoutMiddleware_1 = require("./requestTimeoutMiddleware");
13
13
  const simpleRequestLoggerMiddleware_1 = require("./simpleRequestLoggerMiddleware");
@@ -20,7 +20,7 @@ function createDefaultApp(cfg) {
20
20
  app.set('trust proxy', true);
21
21
  // preHandlers
22
22
  useHandlers(app, cfg.preHandlers);
23
- app.use((0, appEngineLogMiddleware_1.appEngineLogMiddleware)());
23
+ app.use((0, logMiddleware_1.logMiddleware)());
24
24
  if (!isTest) {
25
25
  app.use((0, asyncLocalStorageMiddleware_1.asyncLocalStorageMiddleware)());
26
26
  }
@@ -14,4 +14,4 @@ export declare const devLogger: CommonLogger;
14
14
  * Same as devLogger, but without colors (e.g to not confuse Sentry).
15
15
  */
16
16
  export declare const ciLogger: CommonLogger;
17
- export declare function appEngineLogMiddleware(): BackendRequestHandler;
17
+ export declare function logMiddleware(): BackendRequestHandler;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ciLogger = exports.devLogger = exports.gaeLogger = void 0;
4
- exports.appEngineLogMiddleware = appEngineLogMiddleware;
4
+ exports.logMiddleware = logMiddleware;
5
5
  const node_util_1 = require("node:util");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
- const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE } = process.env;
7
+ const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, K_SERVICE } = process.env;
8
8
  const isGAE = !!GAE_INSTANCE;
9
+ const isCloudRun = !!K_SERVICE;
9
10
  // Simple "request counter" (poor man's "correlation id") counter, to use on dev machine (not in the cloud)
10
11
  let reqCounter = 0;
11
12
  /**
@@ -55,7 +56,14 @@ function logToDev(requestId, args) {
55
56
  function logToCI(args) {
56
57
  console.log(args.map(a => (0, nodejs_lib_1._inspect)(a, { includeErrorStack: true, colors: false })).join(' '));
57
58
  }
58
- function appEngineLogMiddleware() {
59
+ function logMiddleware() {
60
+ if (isCloudRun) {
61
+ // Cloud Run, return simple logger, similar to CI logger
62
+ return function cloudRunLogHandler(req, _res, next) {
63
+ req.log = req.warn = req.error = (...args) => logToCI(args);
64
+ next();
65
+ };
66
+ }
59
67
  if (!isGAE || !GOOGLE_CLOUD_PROJECT) {
60
68
  // Local machine, return "simple" logToDev middleware with request numbering
61
69
  return function gaeLogMiddlewareDev(req, _res, next) {
@@ -5,8 +5,8 @@ exports.getServerStatusData = getServerStatusData;
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
7
  const deployInfo_util_1 = require("./deployInfo.util");
8
- const { versions } = process;
9
- const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, APP_ENV, NODE_OPTIONS } = process.env;
8
+ const { versions, arch, platform } = process;
9
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS } = process.env;
10
10
  function serverStatusMiddleware(projectDir, extra) {
11
11
  return async (_req, res) => {
12
12
  res.json(getServerStatusData(projectDir, extra));
@@ -17,7 +17,6 @@ function getServerStatusData(projectDir = process.cwd(), extra) {
17
17
  const t = (0, js_lib_1.localTime)(ts);
18
18
  const deployBuildTime = t.toPretty();
19
19
  const buildInfo = [t.toStringCompact(), gitBranch, gitRev].filter(Boolean).join('_');
20
- const { arch, platform } = process;
21
20
  return (0, js_lib_1._filterNullishValues)({
22
21
  started: getStartedStr(),
23
22
  deployBuildTime,
@@ -26,6 +25,8 @@ function getServerStatusData(projectDir = process.cwd(), extra) {
26
25
  GAE_APPLICATION,
27
26
  GAE_SERVICE,
28
27
  GAE_VERSION,
28
+ K_SERVICE,
29
+ K_REVISION,
29
30
  processInfo: {
30
31
  arch,
31
32
  platform,
package/dist/util.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function isGAE(): boolean;
2
+ export declare function isCloudRun(): boolean;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isGAE = isGAE;
4
- const { GAE_INSTANCE } = process.env;
4
+ exports.isCloudRun = isCloudRun;
5
+ const { GAE_INSTANCE, K_SERVICE } = process.env;
5
6
  function isGAE() {
6
7
  return !!GAE_INSTANCE;
7
8
  }
9
+ function isCloudRun() {
10
+ return !!K_SERVICE;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "7.0.0",
3
+ "version": "7.1.1",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build",
@@ -11,11 +11,11 @@
11
11
  "dev": "APP_ENV=dev node -r ts-node/register --watch ./src/test/server/server.ts",
12
12
  "docs-serve": "NODE_OPTIONS=--openssl-legacy-provider vuepress dev docs",
13
13
  "docs-build": "NODE_OPTIONS=--openssl-legacy-provider vuepress build docs",
14
- "deploy-gae": "yarn tsn ./src/bin/deploy-gae.ts",
15
- "deploy-prepare": "yarn tsn ./src/bin/deploy-prepare.ts",
16
- "deploy-prepare-debug": "AA=AA1 BB=BB1 yarn tsn ./src/bin/deploy-prepare.ts --projectDir ./src/test/project",
17
- "deploy-health-check-debug1": "yarn tsn ./src/bin/deploy-health-check.ts --url https://api-master.naturalcycles.com",
18
- "deploy-health-check-debug2": "yarn tsn ./src/bin/deploy-health-check.ts --url https://api-master2.naturalcycles.com --thresholdUnhealthy 5"
14
+ "deploy-gae": "yarn tsx ./src/bin/deploy-gae.ts",
15
+ "deploy-prepare": "yarn tsx ./src/bin/deploy-prepare.ts",
16
+ "deploy-prepare-debug": "AA=AA1 BB=BB1 yarn tsx ./src/bin/deploy-prepare.ts --projectDir ./src/test/project",
17
+ "deploy-health-check-debug1": "yarn tsx ./src/bin/deploy-health-check.ts --url https://api-master.naturalcycles.com",
18
+ "deploy-health-check-debug2": "yarn tsx ./src/bin/deploy-health-check.ts --url https://api-master2.naturalcycles.com --thresholdUnhealthy 5"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "@sentry/node": "^9"
@@ -37,8 +37,7 @@
37
37
  "firebase-admin": "^13",
38
38
  "helmet": "^8",
39
39
  "on-finished": "^2",
40
- "simple-git": "^3",
41
- "yargs": "^17"
40
+ "simple-git": "^3"
42
41
  },
43
42
  "devDependencies": {
44
43
  "@naturalcycles/bench-lib": "^3",
@@ -46,9 +45,9 @@
46
45
  "@sentry/node": "^9",
47
46
  "@types/ejs": "^3",
48
47
  "@types/node": "^22",
49
- "@types/yargs": "^16",
50
48
  "@vitest/coverage-v8": "^3",
51
49
  "fastify": "^5",
50
+ "tsx": "^4",
52
51
  "vitest": "^3"
53
52
  },
54
53
  "files": [
@@ -1,13 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { runScript } from '@naturalcycles/nodejs-lib'
4
- import yargs from 'yargs'
3
+ import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
5
4
  import { deployGae } from '../deploy/deployGae'
6
5
  import { deployHealthCheckYargsOptions } from '../deploy/deployHealthCheck'
7
6
  import { deployPrepareYargsOptions } from '../deploy/deployPrepare'
8
7
 
9
8
  runScript(async () => {
10
- const opt = yargs.options({
9
+ const opt = _yargs().options({
11
10
  ...deployPrepareYargsOptions,
12
11
  ...deployHealthCheckYargsOptions,
13
12
  }).argv
@@ -9,12 +9,11 @@ yarn deploy-health-check --url https://service-dot-yourproject.appspot.com
9
9
 
10
10
  */
11
11
 
12
- import { runScript } from '@naturalcycles/nodejs-lib'
13
- import yargs from 'yargs'
12
+ import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
14
13
  import { deployHealthCheck, deployHealthCheckYargsOptions } from '../deploy/deployHealthCheck'
15
14
 
16
15
  runScript(async () => {
17
- const { url, ...opt } = yargs.options({
16
+ const { url, ...opt } = _yargs().options({
18
17
  ...deployHealthCheckYargsOptions,
19
18
  url: {
20
19
  type: 'string',
@@ -6,12 +6,11 @@ yarn deploy-prepare
6
6
 
7
7
  */
8
8
 
9
- import { runScript } from '@naturalcycles/nodejs-lib'
10
- import yargs from 'yargs'
9
+ import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
11
10
  import { deployPrepare, deployPrepareYargsOptions } from '../deploy/deployPrepare'
12
11
 
13
12
  runScript(async () => {
14
- const opt = yargs.options(deployPrepareYargsOptions).argv
13
+ const opt = _yargs().options(deployPrepareYargsOptions).argv
15
14
 
16
15
  await deployPrepare(opt)
17
16
  })
@@ -1,11 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { runScript } from '@naturalcycles/nodejs-lib'
4
- import yargs from 'yargs'
3
+ import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
5
4
  import { undeployGae } from '../deploy/deployGae'
6
5
 
7
6
  runScript(async () => {
8
- const { branch } = yargs.options({
7
+ const { branch } = _yargs().options({
9
8
  branch: {
10
9
  type: 'string',
11
10
  demandOption: true,
package/src/index.ts CHANGED
@@ -4,9 +4,7 @@ export * from './admin/base.admin.service'
4
4
  export * from './admin/firebase.shared.service'
5
5
  export * from './admin/secureHeaderMiddleware'
6
6
  export * from './env/env.shared.service'
7
- export * from './gae/appEngine.util'
8
7
  export * from './sentry/sentry.shared.service'
9
- export * from './server/appEngineLogMiddleware'
10
8
  export * from './server/asyncLocalStorageMiddleware'
11
9
  export * from './server/basicAuthMiddleware'
12
10
  export * from './server/bodyParserTimeoutMiddleware'
@@ -16,6 +14,7 @@ export * from './server/createDefaultApp.model'
16
14
  export * from './server/deployInfo.util'
17
15
  export * from './server/genericErrorMiddleware'
18
16
  export * from './server/getDefaultRouter'
17
+ export * from './server/logMiddleware'
19
18
  export * from './server/methodOverrideMiddleware'
20
19
  export * from './server/notFoundMiddleware'
21
20
  export * from './server/okMiddleware'
@@ -32,5 +31,6 @@ export * from './server/startServer.model'
32
31
  export * from './server/validation/validateMiddleware'
33
32
  export * from './server/validation/validateRequest'
34
33
  export * from './server/validation/zodValidateMiddleware'
34
+ export * from './util'
35
35
 
36
36
  export { onFinished }
@@ -1,11 +1,12 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks'
2
2
  import { _lazyValue, CommonLogger } from '@naturalcycles/js-lib'
3
- import { ciLogger, devLogger, gaeLogger } from './appEngineLogMiddleware'
3
+ import { ciLogger, devLogger, gaeLogger } from './logMiddleware'
4
4
  import { BackendRequest, BackendRequestHandler } from './server.model'
5
5
 
6
6
  const { GAE_INSTANCE, CI } = process.env
7
7
  const isGAE = !!GAE_INSTANCE
8
8
  const isCI = !!CI
9
+ const isCloudRun = !!CI
9
10
 
10
11
  export interface RequestLocalStorage {
11
12
  req: BackendRequest
@@ -35,7 +36,9 @@ export function getRequest(): BackendRequest | undefined {
35
36
  * @experimental
36
37
  */
37
38
  export function getRequestLogger(): CommonLogger {
38
- return storage().getStore()?.req || (isGAE ? gaeLogger : isCI ? ciLogger : devLogger)
39
+ return (
40
+ storage().getStore()?.req || (isGAE ? gaeLogger : isCI || isCloudRun ? ciLogger : devLogger)
41
+ )
39
42
  }
40
43
 
41
44
  /**
@@ -2,7 +2,6 @@ import cookieParser = require('cookie-parser')
2
2
  import cors = require('cors')
3
3
  import express = require('express')
4
4
  import { BackendApplication, isGAE, methodOverrideMiddleware } from '..'
5
- import { appEngineLogMiddleware } from './appEngineLogMiddleware'
6
5
  import { asyncLocalStorageMiddleware } from './asyncLocalStorageMiddleware'
7
6
  import {
8
7
  BackendRequestHandlerCfg,
@@ -10,6 +9,7 @@ import {
10
9
  DefaultAppCfg,
11
10
  } from './createDefaultApp.model'
12
11
  import { genericErrorMiddleware } from './genericErrorMiddleware'
12
+ import { logMiddleware } from './logMiddleware'
13
13
  import { notFoundMiddleware } from './notFoundMiddleware'
14
14
  import { requestTimeoutMiddleware } from './requestTimeoutMiddleware'
15
15
  import { simpleRequestLoggerMiddleware } from './simpleRequestLoggerMiddleware'
@@ -28,7 +28,7 @@ export function createDefaultApp(cfg: DefaultAppCfg): BackendApplication {
28
28
  // preHandlers
29
29
  useHandlers(app, cfg.preHandlers)
30
30
 
31
- app.use(appEngineLogMiddleware())
31
+ app.use(logMiddleware())
32
32
 
33
33
  if (!isTest) {
34
34
  app.use(asyncLocalStorageMiddleware())
@@ -3,8 +3,9 @@ import { AnyObject, CommonLogger } from '@naturalcycles/js-lib'
3
3
  import { _inspect, dimGrey } from '@naturalcycles/nodejs-lib'
4
4
  import { BackendRequestHandler } from './server.model'
5
5
 
6
- const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE } = process.env
6
+ const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, K_SERVICE } = process.env
7
7
  const isGAE = !!GAE_INSTANCE
8
+ const isCloudRun = !!K_SERVICE
8
9
 
9
10
  // Simple "request counter" (poor man's "correlation id") counter, to use on dev machine (not in the cloud)
10
11
  let reqCounter = 0
@@ -66,7 +67,15 @@ function logToCI(args: any[]): void {
66
67
  console.log(args.map(a => _inspect(a, { includeErrorStack: true, colors: false })).join(' '))
67
68
  }
68
69
 
69
- export function appEngineLogMiddleware(): BackendRequestHandler {
70
+ export function logMiddleware(): BackendRequestHandler {
71
+ if (isCloudRun) {
72
+ // Cloud Run, return simple logger, similar to CI logger
73
+ return function cloudRunLogHandler(req, _res, next) {
74
+ req.log = req.warn = req.error = (...args: any[]) => logToCI(args)
75
+ next()
76
+ }
77
+ }
78
+
70
79
  if (!isGAE || !GOOGLE_CLOUD_PROJECT) {
71
80
  // Local machine, return "simple" logToDev middleware with request numbering
72
81
  return function gaeLogMiddlewareDev(req, _res, next) {
@@ -3,8 +3,9 @@ import { memoryUsageFull, processSharedUtil } from '@naturalcycles/nodejs-lib'
3
3
  import { getDeployInfo } from './deployInfo.util'
4
4
  import { BackendRequestHandler } from './server.model'
5
5
 
6
- const { versions } = process
7
- const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, APP_ENV, NODE_OPTIONS } = process.env
6
+ const { versions, arch, platform } = process
7
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS } =
8
+ process.env
8
9
 
9
10
  export function serverStatusMiddleware(projectDir?: string, extra?: any): BackendRequestHandler {
10
11
  return async (_req, res) => {
@@ -21,8 +22,6 @@ export function getServerStatusData(
21
22
  const deployBuildTime = t.toPretty()
22
23
  const buildInfo = [t.toStringCompact(), gitBranch, gitRev].filter(Boolean).join('_')
23
24
 
24
- const { arch, platform } = process
25
-
26
25
  return _filterNullishValues({
27
26
  started: getStartedStr(),
28
27
  deployBuildTime,
@@ -31,6 +30,8 @@ export function getServerStatusData(
31
30
  GAE_APPLICATION,
32
31
  GAE_SERVICE,
33
32
  GAE_VERSION,
33
+ K_SERVICE,
34
+ K_REVISION,
34
35
  processInfo: {
35
36
  arch,
36
37
  platform,
package/src/util.ts ADDED
@@ -0,0 +1,9 @@
1
+ const { GAE_INSTANCE, K_SERVICE } = process.env
2
+
3
+ export function isGAE(): boolean {
4
+ return !!GAE_INSTANCE
5
+ }
6
+
7
+ export function isCloudRun(): boolean {
8
+ return !!K_SERVICE
9
+ }
@@ -1 +0,0 @@
1
- export declare function isGAE(): boolean;
@@ -1,5 +0,0 @@
1
- const { GAE_INSTANCE } = process.env
2
-
3
- export function isGAE(): boolean {
4
- return !!GAE_INSTANCE
5
- }