@dofe/infra-common 0.1.10 → 0.1.11
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,8 +1,18 @@
|
|
|
1
1
|
import { NestMiddleware } from '@nestjs/common';
|
|
2
2
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
4
|
import { Logger } from 'winston';
|
|
4
5
|
export declare const TRACE_ID_HEADER = "x-trace-id";
|
|
5
|
-
export
|
|
6
|
+
export interface RequestContext {
|
|
7
|
+
traceID: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const asyncLocalStorage: AsyncLocalStorage<RequestContext>;
|
|
10
|
+
/** @deprecated Use asyncLocalStorage instead */
|
|
11
|
+
export declare const clsNamespace: {
|
|
12
|
+
run: (fn: () => void) => void;
|
|
13
|
+
set: (key: string, value: any) => void;
|
|
14
|
+
get: (key: string) => string;
|
|
15
|
+
};
|
|
6
16
|
/**
|
|
7
17
|
* RequestMiddleware
|
|
8
18
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.middleware.d.ts","sourceRoot":"","sources":["../../../../packages/common/src/middleware/request.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,cAAc,EAEf,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"request.middleware.d.ts","sourceRoot":"","sources":["../../../../packages/common/src/middleware/request.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,cAAc,EAEf,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAQjC,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,iBAAiB,mCAA0C,CAAC;AAEzE,gDAAgD;AAChD,eAAO,MAAM,YAAY;cACb,MAAM,IAAI;eACT,MAAM,SAAS,GAAG;eAIlB,MAAM;CAClB,CAAC;AAcF;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,CAAC,OAAO,OAAO,iBAAkB,YAAW,cAAc,CAC9D,eAAe,EACf,cAAc,CACf;IAEoC,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAG5D,GAAG,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,IAAI;CAwCtE"}
|
|
@@ -48,10 +48,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
48
48
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
49
49
|
};
|
|
50
50
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
exports.clsNamespace = exports.TRACE_ID_HEADER = void 0;
|
|
51
|
+
exports.clsNamespace = exports.asyncLocalStorage = exports.TRACE_ID_HEADER = void 0;
|
|
52
52
|
const common_1 = require("@nestjs/common");
|
|
53
53
|
const uuid_1 = require("uuid");
|
|
54
|
-
const
|
|
54
|
+
const async_hooks_1 = require("async_hooks");
|
|
55
55
|
const nest_winston_1 = require("nest-winston");
|
|
56
56
|
const winston_1 = require("winston");
|
|
57
57
|
const infra_utils_1 = require("@dofe/infra-utils");
|
|
@@ -60,7 +60,17 @@ const ip_util_1 = __importDefault(require("@dofe/infra-utils/ip.util"));
|
|
|
60
60
|
const environment_util_1 = __importDefault(require("@dofe/infra-utils/environment.util"));
|
|
61
61
|
// Trace ID 请求头名称
|
|
62
62
|
exports.TRACE_ID_HEADER = 'x-trace-id';
|
|
63
|
-
exports.
|
|
63
|
+
exports.asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
|
|
64
|
+
/** @deprecated Use asyncLocalStorage instead */
|
|
65
|
+
exports.clsNamespace = {
|
|
66
|
+
run: (fn) => exports.asyncLocalStorage.run({ traceID: '' }, fn),
|
|
67
|
+
set: (key, value) => {
|
|
68
|
+
const store = exports.asyncLocalStorage.getStore();
|
|
69
|
+
if (store)
|
|
70
|
+
store[key] = value;
|
|
71
|
+
},
|
|
72
|
+
get: (key) => exports.asyncLocalStorage.getStore()?.[key],
|
|
73
|
+
};
|
|
64
74
|
/**
|
|
65
75
|
* 获取或生成 Trace ID
|
|
66
76
|
* 优先从请求头获取 (支持分布式追踪),否则生成新的 UUID
|
|
@@ -94,8 +104,6 @@ let RequestMiddleware = class RequestMiddleware {
|
|
|
94
104
|
this.logger = logger;
|
|
95
105
|
}
|
|
96
106
|
async use(req, res, next) {
|
|
97
|
-
exports.clsNamespace.bind(req);
|
|
98
|
-
exports.clsNamespace.bind(res);
|
|
99
107
|
// 获取或生成 Trace ID (支持分布式追踪)
|
|
100
108
|
const traceId = getOrCreateTraceId(req);
|
|
101
109
|
// 添加到请求对象 (便于 Controller/Service 访问)
|
|
@@ -117,8 +125,7 @@ let RequestMiddleware = class RequestMiddleware {
|
|
|
117
125
|
// 这里使用类型断言,因为 IncomingMessage 和 FastifyRequest 在 headers 属性上兼容
|
|
118
126
|
const realIp = ip_util_1.default.extractIp(req);
|
|
119
127
|
req.realIp = realIp;
|
|
120
|
-
exports.
|
|
121
|
-
exports.clsNamespace.set('traceID', traceId);
|
|
128
|
+
exports.asyncLocalStorage.run({ traceID: traceId }, () => {
|
|
122
129
|
next();
|
|
123
130
|
// 记录日志 (包含 traceId)
|
|
124
131
|
// 注意:getReqMainInfo 期望 FastifyRequest/FastifyReply,但中间件中是原生对象
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.middleware.js","sourceRoot":"","sources":["../../../../packages/common/src/middleware/request.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAKwB;AAGxB,+BAAoC;AACpC,
|
|
1
|
+
{"version":3,"file":"request.middleware.js","sourceRoot":"","sources":["../../../../packages/common/src/middleware/request.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAKwB;AAGxB,+BAAoC;AACpC,6CAAgD;AAChD,+CAAuD;AACvD,qCAAiC;AAEjC,mDAAmD;AACnD,+DAAiD;AACjD,wEAA+C;AAC/C,0FAA4D;AAE5D,iBAAiB;AACJ,QAAA,eAAe,GAAG,YAAY,CAAC;AAM/B,QAAA,iBAAiB,GAAG,IAAI,+BAAiB,EAAkB,CAAC;AAEzE,gDAAgD;AACnC,QAAA,YAAY,GAAG;IAC1B,GAAG,EAAE,CAAC,EAAc,EAAE,EAAE,CAAC,yBAAiB,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;IACnE,GAAG,EAAE,CAAC,GAAW,EAAE,KAAU,EAAE,EAAE;QAC/B,MAAM,KAAK,GAAG,yBAAiB,CAAC,QAAQ,EAAE,CAAC;QAC3C,IAAI,KAAK;YAAG,KAAa,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACzC,CAAC;IACD,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,yBAAiB,CAAC,QAAQ,EAAE,EAAE,CAAC,GAA2B,CAAC;CAClF,CAAC;AAEF;;;GAGG;AACH,SAAS,kBAAkB,CAAC,GAAqC;IAC/D,MAAM,aAAa,GAAG,GAAG,CAAC,OAAO,CAAC,uBAAe,CAAC,CAAC;IACnD,IAAI,aAAa,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE,CAAC;QACvD,OAAO,aAAa,CAAC;IACvB,CAAC;IACD,OAAO,IAAA,SAAM,GAAE,CAAC;AAClB,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AAEY,IAAM,iBA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AAO;oBACP,GAAG,IAAA,4BAAc,EAAC,GAAU,EAAE,GAAU,CAAC;iBAC1C,CAAC,CAAC;YACL,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAhDoB,iBAAiB;IADrC,IAAA,mBAAU,GAAE;IAMR,WAAA,IAAA,eAAM,EAAC,sCAAuB,CAAC,CAAA;qCAA0B,gBAAM;GAL/C,iBAAiB,CAgDrC;kBAhDoB,iBAAiB"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dofe/infra-common",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"peerDependencies": {
|
|
7
|
-
"@dofe/infra-
|
|
7
|
+
"@dofe/infra-contracts": "^0.1.11",
|
|
8
|
+
"@dofe/infra-redis": "^0.1.11",
|
|
8
9
|
"@nestjs/common": "^11.0.0",
|
|
9
10
|
"@nestjs/config": "^4.0.0",
|
|
10
11
|
"@nestjs/core": "^11.0.0",
|
|
11
12
|
"@nestjs/event-emitter": "^3.0.0",
|
|
12
13
|
"@nestjs/platform-fastify": "^11.0.0",
|
|
13
|
-
"@
|
|
14
|
-
"@ts-rest/core": "^3.53.0-rc.1",
|
|
15
|
-
"@ts-rest/nest": "^3.53.0-rc.1",
|
|
14
|
+
"@prisma/client": "^7.0.0",
|
|
16
15
|
"accept-language-parser": "^1.5.0",
|
|
17
16
|
"class-validator": "^0.14.1",
|
|
18
|
-
"cls-hooked": "^4.2.2",
|
|
19
17
|
"crypto-js": "^4.2.0",
|
|
20
18
|
"fastify": "^5.2.0",
|
|
21
19
|
"ioredis": "^5.0.0",
|
|
@@ -24,19 +22,18 @@
|
|
|
24
22
|
"nestjs-i18n": "^10.4.5",
|
|
25
23
|
"reflect-metadata": "^0.2.0",
|
|
26
24
|
"rxjs": "^7.8.0",
|
|
27
|
-
"uuid": "^
|
|
25
|
+
"uuid": "^14.0.0",
|
|
28
26
|
"winston": "^3.13.0",
|
|
29
27
|
"zod": "^4.3.6"
|
|
30
28
|
},
|
|
31
29
|
"dependencies": {
|
|
32
|
-
"@dofe/infra-i18n": "^0.1.
|
|
33
|
-
"@dofe/infra-utils": "^0.1.
|
|
30
|
+
"@dofe/infra-i18n": "^0.1.11",
|
|
31
|
+
"@dofe/infra-utils": "^0.1.11"
|
|
34
32
|
},
|
|
35
33
|
"devDependencies": {
|
|
36
34
|
"@types/accept-language-parser": "^1.5.8",
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"typescript": "^5.4.5"
|
|
35
|
+
"rimraf": "^6.1.3",
|
|
36
|
+
"typescript": "^6.0.3"
|
|
40
37
|
},
|
|
41
38
|
"type": "commonjs",
|
|
42
39
|
"publishConfig": {
|