@jayfong/x-server 2.58.1 → 2.59.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.
File without changes
@@ -26,11 +26,15 @@ class Handler {
26
26
  if (Handler.hooks.length) {
27
27
  await Promise.all(Handler.hooks.map(hook => hook(this.options, data, ctx)));
28
28
  }
29
+ let useAsyncContext = _server.Server.options.useAsyncContext !== false;
30
+ if (this.options.useAsyncContext != null) {
31
+ useAsyncContext = this.options.useAsyncContext;
32
+ }
29
33
  ctx.setHeader('x-trace-id', ctx.traceId);
30
34
  if (this.options.requestMethod === 'WS') {
31
- return _ctx_storage.ctxStorage.run(ctx, () => this.handleWs(data, ctx));
35
+ return useAsyncContext ? _ctx_storage.ctxStorage.run(ctx, () => this.handleWs(data, ctx)) : this.handleWs(data, ctx);
32
36
  }
33
- return _ctx_storage.ctxStorage.run(ctx, () => this.handleHttp(data, ctx));
37
+ return useAsyncContext ? _ctx_storage.ctxStorage.run(ctx, () => this.handleHttp(data, ctx)) : this.handleHttp(data, ctx);
34
38
  };
35
39
  this.handleHttp = async (data, ctx) => {
36
40
  // 请求数据解析
@@ -16,6 +16,9 @@ class Server {
16
16
  this.options = options;
17
17
  this.fastify = void 0;
18
18
  this.routes = [];
19
+ if (options.useAsyncContext == null) {
20
+ options.useAsyncContext = true;
21
+ }
19
22
  Server.options = options;
20
23
  _nodeProcess.default.on('unhandledRejection', (reason, promise) => {
21
24
  console.error('未捕获的错误', reason, promise);
@@ -20,11 +20,15 @@ export class Handler {
20
20
  if (Handler.hooks.length) {
21
21
  await Promise.all(Handler.hooks.map(hook => hook(this.options, data, ctx)));
22
22
  }
23
+ let useAsyncContext = Server.options.useAsyncContext !== false;
24
+ if (this.options.useAsyncContext != null) {
25
+ useAsyncContext = this.options.useAsyncContext;
26
+ }
23
27
  ctx.setHeader('x-trace-id', ctx.traceId);
24
28
  if (this.options.requestMethod === 'WS') {
25
- return ctxStorage.run(ctx, () => this.handleWs(data, ctx));
29
+ return useAsyncContext ? ctxStorage.run(ctx, () => this.handleWs(data, ctx)) : this.handleWs(data, ctx);
26
30
  }
27
- return ctxStorage.run(ctx, () => this.handleHttp(data, ctx));
31
+ return useAsyncContext ? ctxStorage.run(ctx, () => this.handleHttp(data, ctx)) : this.handleHttp(data, ctx);
28
32
  };
29
33
  this.handleHttp = async (data, ctx) => {
30
34
  // 请求数据解析
@@ -10,6 +10,9 @@ export class Server {
10
10
  this.options = options;
11
11
  this.fastify = void 0;
12
12
  this.routes = [];
13
+ if (options.useAsyncContext == null) {
14
+ options.useAsyncContext = true;
15
+ }
13
16
  Server.options = options;
14
17
  process.on('unhandledRejection', (reason, promise) => {
15
18
  console.error('未捕获的错误', reason, promise);
@@ -44,6 +44,12 @@ export declare namespace XServer {
44
44
  * 响应加密密钥
45
45
  */
46
46
  responseEncryptKey?: string;
47
+ /**
48
+ * 是否使用异步上下文
49
+ *
50
+ * @default true
51
+ */
52
+ useAsyncContext?: boolean;
47
53
  /**
48
54
  * 透传给 Fastify 的选项
49
55
  */
@@ -158,6 +164,12 @@ export declare namespace XHandler {
158
164
  * 请求数据验证结构(vae)
159
165
  */
160
166
  requestDataSchemaVae?: vae.VaeSchemaOf<TReqData> | ((_: vae.VaeSchemaBuilder<TReqData>) => vae.VaeSchemaOf<TReqData> | vae.VaeObjectSchemaShapeOf<TReqData>);
167
+ /**
168
+ * 是否使用异步上下文
169
+ *
170
+ * @default 默认为服务的设置
171
+ */
172
+ useAsyncContext?: boolean;
161
173
  /**
162
174
  * 处理器
163
175
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.58.1",
3
+ "version": "2.59.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -9,7 +9,9 @@
9
9
  "bin": {
10
10
  "xs": "lib/_cjs/cli/cli.js"
11
11
  },
12
- "files": ["lib"],
12
+ "files": [
13
+ "lib"
14
+ ],
13
15
  "scripts": {
14
16
  "build": "haoma compile",
15
17
  "build_test_pkg": "tyn build && rm -rf ./lib_test && mkdir -p ./lib_test && cp -r ./lib ./lib_test/lib && cp ./package.json ./lib_test/package.json && cd ./tests/app && tyn add file:../../lib_test",
@@ -24,7 +26,7 @@
24
26
  "@fastify/formbody": "^7.4.0",
25
27
  "@fastify/multipart": "^7.7.0",
26
28
  "@fastify/websocket": "^8.1.0",
27
- "@prisma/client": "^5.8.0",
29
+ "@prisma/client": "^6.0.1",
28
30
  "@types/busboy": "^0.3.2",
29
31
  "@types/http-errors": "^1.8.2",
30
32
  "@types/jsonwebtoken": "^8.5.8",
@@ -58,7 +60,7 @@
58
60
  "nodemailer": "^6.7.3",
59
61
  "pino-pretty": "^10.0.1",
60
62
  "pirates": "^4.0.6",
61
- "prisma": "^5.8.0",
63
+ "prisma": "^6.0.1",
62
64
  "select-run": "^1.1.2",
63
65
  "supports-color": "^8",
64
66
  "svg-captcha": "^1.4.0",