@naturalcycles/backend-lib 7.0.0 → 7.1.0
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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/server/asyncLocalStorageMiddleware.js +3 -2
- package/dist/server/createDefaultApp.js +2 -2
- package/dist/server/{appEngineLogMiddleware.d.ts → logMiddleware.d.ts} +1 -1
- package/dist/server/{appEngineLogMiddleware.js → logMiddleware.js} +11 -3
- package/dist/server/serverStatusMiddleware.js +4 -3
- package/dist/util.d.ts +2 -0
- package/dist/{gae/appEngine.util.js → util.js} +5 -1
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/server/asyncLocalStorageMiddleware.ts +5 -2
- package/src/server/createDefaultApp.ts +2 -2
- package/src/server/{appEngineLogMiddleware.ts → logMiddleware.ts} +11 -2
- package/src/server/serverStatusMiddleware.ts +5 -4
- package/src/util.ts +9 -0
- package/dist/gae/appEngine.util.d.ts +0 -1
- package/src/gae/appEngine.util.ts +0 -5
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
|
|
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 ?
|
|
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,
|
|
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
|
|
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.
|
|
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
|
|
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 = logToCI;
|
|
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
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isGAE = isGAE;
|
|
4
|
-
|
|
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
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 './
|
|
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
|
|
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(
|
|
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
|
|
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 = logToCI
|
|
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 } =
|
|
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
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isGAE(): boolean;
|