@jayfong/x-server 2.112.0 → 2.113.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
@@ -22,6 +22,7 @@ class Handler {
22
22
  this.options = options;
23
23
  this.requestDataSchema = void 0;
24
24
  this.requestDataSchemaVae = void 0;
25
+ this.requestHandle = void 0;
25
26
  this.handle = async (data, ctx) => {
26
27
  // 前置 hook
27
28
  if (Handler.hooks.length) {
@@ -72,7 +73,7 @@ class Handler {
72
73
  }
73
74
  }
74
75
  try {
75
- let res = await this.options.handle(data, ctx);
76
+ let res = await this.requestHandle(data, ctx);
76
77
  if (res instanceof _http_redirect.HttpRedirect) {
77
78
  ctx.redirect(res.url, res.permanently);
78
79
  return {};
@@ -188,6 +189,24 @@ class Handler {
188
189
  } else if (options.requestDataSchema) {
189
190
  this.requestDataSchema = options.requestDataSchema(_validator.yup);
190
191
  }
192
+ if (options.concurrency != null) {
193
+ if (typeof options.concurrency === 'number') {
194
+ this.requestHandle = (0, _vtils.asyncLimit)(async (data, ctx) => options.handle(data, ctx), {
195
+ concurrency: options.concurrency
196
+ });
197
+ } else {
198
+ const {
199
+ count,
200
+ groupBy
201
+ } = options.concurrency;
202
+ this.requestHandle = (0, _vtils.asyncLimit)(async (data, ctx) => options.handle(data, ctx), {
203
+ concurrency: count,
204
+ groupBy: groupBy
205
+ });
206
+ }
207
+ } else {
208
+ this.requestHandle = options.handle;
209
+ }
191
210
  }
192
211
  static addHook(hook) {
193
212
  this.hooks.push(hook);
@@ -6,6 +6,7 @@ export declare class Handler<TReqData extends any = void, TResData extends any =
6
6
  readonly options: XHandler.Options<TReqData, TResData, TReqMethod>;
7
7
  private requestDataSchema;
8
8
  readonly requestDataSchemaVae: vae.VaeSchemaOf<TReqDataRequired>;
9
+ private requestHandle;
9
10
  constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
10
11
  handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
11
12
  private handleHttp;
@@ -1,6 +1,6 @@
1
1
  import { HTTPError as GotHTTPError, ParseError as GotParseError, RequestError as GotRequestError, TimeoutError as GotTimeoutError } from 'got';
2
2
  import LZString from 'lz-string';
3
- import { base64UrlEncode, DataPacker, rot13, toSingleLineString } from 'vtils';
3
+ import { asyncLimit, base64UrlEncode, DataPacker, rot13, toSingleLineString } from 'vtils';
4
4
  import * as vae from 'vtils/vae';
5
5
  import { getZhCN, yup } from 'vtils/validator';
6
6
  import { HttpError } from "../core/http_error";
@@ -16,6 +16,7 @@ export class Handler {
16
16
  this.options = options;
17
17
  this.requestDataSchema = void 0;
18
18
  this.requestDataSchemaVae = void 0;
19
+ this.requestHandle = void 0;
19
20
  this.handle = async (data, ctx) => {
20
21
  // 前置 hook
21
22
  if (Handler.hooks.length) {
@@ -66,7 +67,7 @@ export class Handler {
66
67
  }
67
68
  }
68
69
  try {
69
- let res = await this.options.handle(data, ctx);
70
+ let res = await this.requestHandle(data, ctx);
70
71
  if (res instanceof HttpRedirect) {
71
72
  ctx.redirect(res.url, res.permanently);
72
73
  return {};
@@ -182,6 +183,24 @@ export class Handler {
182
183
  } else if (options.requestDataSchema) {
183
184
  this.requestDataSchema = options.requestDataSchema(yup);
184
185
  }
186
+ if (options.concurrency != null) {
187
+ if (typeof options.concurrency === 'number') {
188
+ this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
189
+ concurrency: options.concurrency
190
+ });
191
+ } else {
192
+ const {
193
+ count,
194
+ groupBy
195
+ } = options.concurrency;
196
+ this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
197
+ concurrency: count,
198
+ groupBy: groupBy
199
+ });
200
+ }
201
+ } else {
202
+ this.requestHandle = options.handle;
203
+ }
185
204
  }
186
205
  static addHook(hook) {
187
206
  this.hooks.push(hook);
@@ -167,6 +167,15 @@ export declare namespace XHandler {
167
167
  * @default 默认为服务的设置
168
168
  */
169
169
  useAsyncContext?: boolean;
170
+ /**
171
+ * 并发控制
172
+ */
173
+ concurrency?: number | {
174
+ /** 并发数量 */
175
+ count?: number;
176
+ /** 分组函数 */
177
+ groupBy?: (data: TReqData, ctx: Context) => string;
178
+ };
170
179
  /**
171
180
  * 处理器
172
181
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.112.0",
3
+ "version": "2.113.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -79,7 +79,7 @@
79
79
  "unplugin-macros": "^0.16.0",
80
80
  "utf-8-validate": "^5.0.9",
81
81
  "vscode-generate-index-standalone": "^1.7.1",
82
- "vtils": "^4.136.0",
82
+ "vtils": "^4.138.0",
83
83
  "yaml": "^2.3.1",
84
84
  "yargs": "^17.4.1"
85
85
  },