@naturalcycles/backend-lib 9.10.1 → 9.12.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.
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import type { BackendRequestHandler } from '../index.js';
|
|
1
|
+
import type { BackendRequest, BackendRequestHandler } from '../index.js';
|
|
2
2
|
export interface RequestLoggerMiddlewareCfg {
|
|
3
3
|
/**
|
|
4
4
|
* If set - this prefix will be removed from the request url before logging.
|
|
5
5
|
*/
|
|
6
6
|
removeUrlPrefix?: string;
|
|
7
|
+
/**
|
|
8
|
+
* If set - will be run to determine whether to log the request or not.
|
|
9
|
+
* Predicate should return true to log the request.
|
|
10
|
+
*/
|
|
11
|
+
predicate?: (req: BackendRequest) => boolean;
|
|
7
12
|
}
|
|
8
13
|
/**
|
|
9
14
|
* Experimental request logger for Cloud Run.
|
|
@@ -6,9 +6,12 @@ import { onFinished } from '../index.js';
|
|
|
6
6
|
* @experimental
|
|
7
7
|
*/
|
|
8
8
|
export function requestLoggerMiddleware(cfg = {}) {
|
|
9
|
-
const { removeUrlPrefix } = cfg;
|
|
9
|
+
const { removeUrlPrefix, predicate } = cfg;
|
|
10
10
|
const removeUrlPrefixLength = removeUrlPrefix?.length;
|
|
11
11
|
return (req, res, next) => {
|
|
12
|
+
if (predicate && !predicate(req)) {
|
|
13
|
+
return next();
|
|
14
|
+
}
|
|
12
15
|
const started = Date.now();
|
|
13
16
|
let url = req.originalUrl.split('?')[0];
|
|
14
17
|
if (removeUrlPrefix && url.startsWith(removeUrlPrefix)) {
|
|
@@ -2,22 +2,20 @@ import { _filterNullishValues, localTime } from '@naturalcycles/js-lib';
|
|
|
2
2
|
import { memoryUsageFull, processSharedUtil } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { getDeployInfo } from './deployInfo.util.js';
|
|
4
4
|
const { versions, arch, platform } = process;
|
|
5
|
-
const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, GOOGLE_CLOUD_PROJECT, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS, } = process.env;
|
|
5
|
+
const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, GOOGLE_CLOUD_PROJECT, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS, DEPLOY_BUILD_TIME, } = process.env;
|
|
6
6
|
export function serverStatusMiddleware(projectDir, extra) {
|
|
7
7
|
return async (_req, res) => {
|
|
8
8
|
res.json(getServerStatusData(projectDir, extra));
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
export function getServerStatusData(projectDir = process.cwd(), extra) {
|
|
12
|
-
const {
|
|
12
|
+
const { ts } = getDeployInfo(projectDir);
|
|
13
13
|
const t = localTime(ts);
|
|
14
|
-
const deployBuildTime = t.toPretty();
|
|
15
|
-
const buildInfo = [t.toStringCompact(), gitBranch, gitRev].filter(Boolean).join('_');
|
|
14
|
+
const deployBuildTime = DEPLOY_BUILD_TIME || t.toPretty();
|
|
16
15
|
return _filterNullishValues({
|
|
17
16
|
started: getStartedStr(),
|
|
18
17
|
deployBuildTime,
|
|
19
18
|
APP_ENV,
|
|
20
|
-
buildInfo,
|
|
21
19
|
GOOGLE_CLOUD_PROJECT,
|
|
22
20
|
GAE_APPLICATION,
|
|
23
21
|
GAE_SERVICE,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { UnixTimestampMillis } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _since } from '@naturalcycles/js-lib'
|
|
3
|
-
import type { BackendRequestHandler } from '../index.js'
|
|
3
|
+
import type { BackendRequest, BackendRequestHandler } from '../index.js'
|
|
4
4
|
import { onFinished } from '../index.js'
|
|
5
5
|
|
|
6
6
|
export interface RequestLoggerMiddlewareCfg {
|
|
@@ -8,6 +8,12 @@ export interface RequestLoggerMiddlewareCfg {
|
|
|
8
8
|
* If set - this prefix will be removed from the request url before logging.
|
|
9
9
|
*/
|
|
10
10
|
removeUrlPrefix?: string
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* If set - will be run to determine whether to log the request or not.
|
|
14
|
+
* Predicate should return true to log the request.
|
|
15
|
+
*/
|
|
16
|
+
predicate?: (req: BackendRequest) => boolean
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
/**
|
|
@@ -18,10 +24,14 @@ export interface RequestLoggerMiddlewareCfg {
|
|
|
18
24
|
export function requestLoggerMiddleware(
|
|
19
25
|
cfg: RequestLoggerMiddlewareCfg = {},
|
|
20
26
|
): BackendRequestHandler {
|
|
21
|
-
const { removeUrlPrefix } = cfg
|
|
27
|
+
const { removeUrlPrefix, predicate } = cfg
|
|
22
28
|
const removeUrlPrefixLength = removeUrlPrefix?.length
|
|
23
29
|
|
|
24
30
|
return (req, res, next) => {
|
|
31
|
+
if (predicate && !predicate(req)) {
|
|
32
|
+
return next()
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
const started = Date.now() as UnixTimestampMillis
|
|
26
36
|
|
|
27
37
|
let url = req.originalUrl.split('?')[0]!
|
|
@@ -13,6 +13,7 @@ const {
|
|
|
13
13
|
K_REVISION,
|
|
14
14
|
APP_ENV,
|
|
15
15
|
NODE_OPTIONS,
|
|
16
|
+
DEPLOY_BUILD_TIME,
|
|
16
17
|
} = process.env
|
|
17
18
|
|
|
18
19
|
export function serverStatusMiddleware(projectDir?: string, extra?: any): BackendRequestHandler {
|
|
@@ -25,16 +26,14 @@ export function getServerStatusData(
|
|
|
25
26
|
projectDir: string = process.cwd(),
|
|
26
27
|
extra?: any,
|
|
27
28
|
): Record<string, any> {
|
|
28
|
-
const {
|
|
29
|
+
const { ts } = getDeployInfo(projectDir)
|
|
29
30
|
const t = localTime(ts)
|
|
30
|
-
const deployBuildTime = t.toPretty()
|
|
31
|
-
const buildInfo = [t.toStringCompact(), gitBranch, gitRev].filter(Boolean).join('_')
|
|
31
|
+
const deployBuildTime = DEPLOY_BUILD_TIME || t.toPretty()
|
|
32
32
|
|
|
33
33
|
return _filterNullishValues({
|
|
34
34
|
started: getStartedStr(),
|
|
35
35
|
deployBuildTime,
|
|
36
36
|
APP_ENV,
|
|
37
|
-
buildInfo,
|
|
38
37
|
GOOGLE_CLOUD_PROJECT,
|
|
39
38
|
GAE_APPLICATION,
|
|
40
39
|
GAE_SERVICE,
|