@naturalcycles/backend-lib 7.1.1 → 7.1.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.
@@ -4,9 +4,11 @@ exports.ciLogger = exports.devLogger = exports.gaeLogger = void 0;
4
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, K_SERVICE } = process.env;
7
+ const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, APP_ENV } = process.env;
8
8
  const isGAE = !!GAE_INSTANCE;
9
- const isCloudRun = !!K_SERVICE;
9
+ // const isCloudRun = !!K_SERVICE
10
+ // const isTest = APP_ENV === 'test'
11
+ const isDev = APP_ENV === 'dev';
10
12
  // Simple "request counter" (poor man's "correlation id") counter, to use on dev machine (not in the cloud)
11
13
  let reqCounter = 0;
12
14
  /**
@@ -57,41 +59,47 @@ function logToCI(args) {
57
59
  console.log(args.map(a => (0, nodejs_lib_1._inspect)(a, { includeErrorStack: true, colors: false })).join(' '));
58
60
  }
59
61
  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);
62
+ if (isGAE) {
63
+ if (GOOGLE_CLOUD_PROJECT) {
64
+ return function appEngineLogHandler(req, _res, next) {
65
+ const traceHeader = req.header('x-cloud-trace-context');
66
+ if (traceHeader) {
67
+ const [trace] = traceHeader.split('/');
68
+ const meta = {
69
+ 'logging.googleapis.com/trace': `projects/${GOOGLE_CLOUD_PROJECT}/traces/${trace}`,
70
+ 'appengine.googleapis.com/request_id': req.header('x-appengine-request-log-id'),
71
+ };
72
+ Object.assign(req, {
73
+ log: (...args) => logToAppEngine({ ...meta, severity: 'INFO' }, args),
74
+ warn: (...args) => logToAppEngine({ ...meta, severity: 'WARNING' }, args),
75
+ error: (...args) => logToAppEngine({ ...meta, severity: 'ERROR' }, args),
76
+ });
77
+ req.requestId = trace;
78
+ }
79
+ else {
80
+ Object.assign(req, exports.gaeLogger);
81
+ }
82
+ next();
83
+ };
84
+ }
85
+ return function appEngineLogHandler(req, _res, next) {
86
+ Object.assign(req, exports.gaeLogger);
64
87
  next();
65
88
  };
66
89
  }
67
- if (!isGAE || !GOOGLE_CLOUD_PROJECT) {
90
+ if (isDev) {
68
91
  // Local machine, return "simple" logToDev middleware with request numbering
69
- return function gaeLogMiddlewareDev(req, _res, next) {
92
+ return function devLogHandler(req, _res, next) {
70
93
  // Local machine
71
94
  req.requestId = String(++reqCounter);
72
95
  req.log = req.warn = req.error = (...args) => logToDev(req.requestId, args);
73
96
  next();
74
97
  };
75
98
  }
76
- // Otherwise, we're in AppEngine
77
- return function appEngineLogHandler(req, _res, next) {
78
- const traceHeader = req.header('x-cloud-trace-context');
79
- if (traceHeader) {
80
- const [trace] = traceHeader.split('/');
81
- const meta = {
82
- 'logging.googleapis.com/trace': `projects/${GOOGLE_CLOUD_PROJECT}/traces/${trace}`,
83
- 'appengine.googleapis.com/request_id': req.header('x-appengine-request-log-id'),
84
- };
85
- Object.assign(req, {
86
- log: (...args) => logToAppEngine({ ...meta, severity: 'INFO' }, args),
87
- warn: (...args) => logToAppEngine({ ...meta, severity: 'WARNING' }, args),
88
- error: (...args) => logToAppEngine({ ...meta, severity: 'ERROR' }, args),
89
- });
90
- req.requestId = trace;
91
- }
92
- else {
93
- Object.assign(req, exports.gaeLogger);
94
- }
99
+ // Otherwise, return "simple" logger
100
+ // This includes: unit tests, CloudRun, CI environments
101
+ return function simpleLogHandler(req, _res, next) {
102
+ req.log = req.warn = req.error = (...args) => logToCI(args);
95
103
  next();
96
104
  };
97
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "7.1.1",
3
+ "version": "7.1.2",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@naturalcycles/bench-lib": "^3",
44
- "@naturalcycles/dev-lib": "^16",
44
+ "@naturalcycles/dev-lib": "^17",
45
45
  "@sentry/node": "^9",
46
46
  "@types/ejs": "^3",
47
47
  "@types/node": "^22",
@@ -3,9 +3,11 @@ 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, K_SERVICE } = process.env
6
+ const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, APP_ENV } = process.env
7
7
  const isGAE = !!GAE_INSTANCE
8
- const isCloudRun = !!K_SERVICE
8
+ // const isCloudRun = !!K_SERVICE
9
+ // const isTest = APP_ENV === 'test'
10
+ const isDev = APP_ENV === 'dev'
9
11
 
10
12
  // Simple "request counter" (poor man's "correlation id") counter, to use on dev machine (not in the cloud)
11
13
  let reqCounter = 0
@@ -68,17 +70,39 @@ function logToCI(args: any[]): void {
68
70
  }
69
71
 
70
72
  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)
73
+ if (isGAE) {
74
+ if (GOOGLE_CLOUD_PROJECT) {
75
+ return function appEngineLogHandler(req, _res, next) {
76
+ const traceHeader = req.header('x-cloud-trace-context')
77
+ if (traceHeader) {
78
+ const [trace] = traceHeader.split('/')
79
+ const meta = {
80
+ 'logging.googleapis.com/trace': `projects/${GOOGLE_CLOUD_PROJECT}/traces/${trace}`,
81
+ 'appengine.googleapis.com/request_id': req.header('x-appengine-request-log-id'),
82
+ }
83
+ Object.assign(req, {
84
+ log: (...args: any[]) => logToAppEngine({ ...meta, severity: 'INFO' }, args),
85
+ warn: (...args: any[]) => logToAppEngine({ ...meta, severity: 'WARNING' }, args),
86
+ error: (...args: any[]) => logToAppEngine({ ...meta, severity: 'ERROR' }, args),
87
+ })
88
+ req.requestId = trace
89
+ } else {
90
+ Object.assign(req, gaeLogger)
91
+ }
92
+
93
+ next()
94
+ }
95
+ }
96
+
97
+ return function appEngineLogHandler(req, _res, next) {
98
+ Object.assign(req, gaeLogger)
75
99
  next()
76
100
  }
77
101
  }
78
102
 
79
- if (!isGAE || !GOOGLE_CLOUD_PROJECT) {
103
+ if (isDev) {
80
104
  // Local machine, return "simple" logToDev middleware with request numbering
81
- return function gaeLogMiddlewareDev(req, _res, next) {
105
+ return function devLogHandler(req, _res, next) {
82
106
  // Local machine
83
107
  req.requestId = String(++reqCounter)
84
108
  req.log = req.warn = req.error = (...args: any[]) => logToDev(req.requestId!, args)
@@ -86,26 +110,10 @@ export function logMiddleware(): BackendRequestHandler {
86
110
  }
87
111
  }
88
112
 
89
- // Otherwise, we're in AppEngine
90
-
91
- return function appEngineLogHandler(req, _res, next) {
92
- const traceHeader = req.header('x-cloud-trace-context')
93
- if (traceHeader) {
94
- const [trace] = traceHeader.split('/')
95
- const meta = {
96
- 'logging.googleapis.com/trace': `projects/${GOOGLE_CLOUD_PROJECT}/traces/${trace}`,
97
- 'appengine.googleapis.com/request_id': req.header('x-appengine-request-log-id'),
98
- }
99
- Object.assign(req, {
100
- log: (...args: any[]) => logToAppEngine({ ...meta, severity: 'INFO' }, args),
101
- warn: (...args: any[]) => logToAppEngine({ ...meta, severity: 'WARNING' }, args),
102
- error: (...args: any[]) => logToAppEngine({ ...meta, severity: 'ERROR' }, args),
103
- })
104
- req.requestId = trace
105
- } else {
106
- Object.assign(req, gaeLogger)
107
- }
108
-
113
+ // Otherwise, return "simple" logger
114
+ // This includes: unit tests, CloudRun, CI environments
115
+ return function simpleLogHandler(req, _res, next) {
116
+ req.log = req.warn = req.error = (...args: any[]) => logToCI(args)
109
117
  next()
110
118
  }
111
119
  }