@naturalcycles/backend-lib 7.1.0 → 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.
- package/dist/bin/deploy-gae.js +1 -3
- package/dist/bin/deploy-health-check.js +1 -3
- package/dist/bin/deploy-prepare.js +1 -3
- package/dist/bin/undeploy-gae.js +1 -3
- package/dist/server/logMiddleware.js +35 -27
- package/package.json +9 -10
- package/src/bin/deploy-gae.ts +2 -3
- package/src/bin/deploy-health-check.ts +2 -3
- package/src/bin/deploy-prepare.ts +2 -3
- package/src/bin/undeploy-gae.ts +2 -3
- package/src/server/logMiddleware.ts +36 -28
package/dist/bin/deploy-gae.js
CHANGED
|
@@ -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 =
|
|
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 } =
|
|
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 =
|
|
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
|
package/dist/bin/undeploy-gae.js
CHANGED
|
@@ -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 } =
|
|
7
|
+
const { branch } = (0, nodejs_lib_1._yargs)().options({
|
|
10
8
|
branch: {
|
|
11
9
|
type: 'string',
|
|
12
10
|
demandOption: true,
|
|
@@ -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,
|
|
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 (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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 (
|
|
90
|
+
if (isDev) {
|
|
68
91
|
// Local machine, return "simple" logToDev middleware with request numbering
|
|
69
|
-
return function
|
|
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,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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.
|
|
3
|
+
"version": "7.1.2",
|
|
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
|
|
15
|
-
"deploy-prepare": "yarn
|
|
16
|
-
"deploy-prepare-debug": "AA=AA1 BB=BB1 yarn
|
|
17
|
-
"deploy-health-check-debug1": "yarn
|
|
18
|
-
"deploy-health-check-debug2": "yarn
|
|
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,18 +37,17 @@
|
|
|
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",
|
|
45
|
-
"@naturalcycles/dev-lib": "^
|
|
44
|
+
"@naturalcycles/dev-lib": "^17",
|
|
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": [
|
package/src/bin/deploy-gae.ts
CHANGED
|
@@ -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 =
|
|
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 } =
|
|
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 =
|
|
13
|
+
const opt = _yargs().options(deployPrepareYargsOptions).argv
|
|
15
14
|
|
|
16
15
|
await deployPrepare(opt)
|
|
17
16
|
})
|
package/src/bin/undeploy-gae.ts
CHANGED
|
@@ -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 } =
|
|
7
|
+
const { branch } = _yargs().options({
|
|
9
8
|
branch: {
|
|
10
9
|
type: 'string',
|
|
11
10
|
demandOption: true,
|
|
@@ -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,
|
|
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 (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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 (
|
|
103
|
+
if (isDev) {
|
|
80
104
|
// Local machine, return "simple" logToDev middleware with request numbering
|
|
81
|
-
return function
|
|
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,
|
|
90
|
-
|
|
91
|
-
return function
|
|
92
|
-
|
|
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
|
}
|