phecda-server 7.0.0-alpha.8 → 7.0.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/README.md +10 -10
- package/assets/schema.json +12 -9
- package/bin/cli.mjs +96 -37
- package/dist/chunk-5SUY5UVC.js +258 -0
- package/dist/chunk-5SUY5UVC.js.map +1 -0
- package/dist/chunk-6JAHCTOT.mjs +334 -0
- package/dist/chunk-6JAHCTOT.mjs.map +1 -0
- package/dist/{chunk-BBNJVYVX.js → chunk-EDAWJVJ4.js} +42 -25
- package/dist/chunk-EDAWJVJ4.js.map +1 -0
- package/dist/{chunk-7EMAZGYO.js → chunk-H7OCHDZT.js} +65 -95
- package/dist/chunk-H7OCHDZT.js.map +1 -0
- package/dist/{chunk-QJIAXSLZ.js → chunk-KRUOHWFR.js} +79 -68
- package/dist/chunk-KRUOHWFR.js.map +1 -0
- package/dist/chunk-KUGU3UGO.js +334 -0
- package/dist/chunk-KUGU3UGO.js.map +1 -0
- package/dist/{chunk-SGMQZRP6.mjs → chunk-LBH4RXQQ.mjs} +12 -42
- package/dist/chunk-LBH4RXQQ.mjs.map +1 -0
- package/dist/{chunk-YKP4ODZA.mjs → chunk-MDPW6WGF.mjs} +32 -6
- package/dist/chunk-MDPW6WGF.mjs.map +1 -0
- package/dist/{chunk-VSGYTGHY.js → chunk-RLFCI3RG.js} +31 -5
- package/dist/chunk-RLFCI3RG.js.map +1 -0
- package/dist/{chunk-7NWDKK7D.mjs → chunk-TTHYUNOT.mjs} +18 -1
- package/dist/chunk-TTHYUNOT.mjs.map +1 -0
- package/dist/chunk-VDOICQ2U.mjs +258 -0
- package/dist/chunk-VDOICQ2U.mjs.map +1 -0
- package/dist/{chunk-B45BN7LY.mjs → chunk-WTJKTICN.mjs} +65 -54
- package/dist/chunk-WTJKTICN.mjs.map +1 -0
- package/dist/{core-BGEsBtTG.d.ts → core-BOo3uGFX.d.ts} +5 -4
- package/dist/{core-BcKywWLd.d.mts → core-CF45G79J.d.mts} +5 -4
- package/dist/core-DbqWh47p.d.mts +50 -0
- package/dist/core-Rq7BhPHE.d.ts +50 -0
- package/dist/helper.d.mts +7 -8
- package/dist/helper.d.ts +7 -8
- package/dist/helper.js +5 -3
- package/dist/helper.js.map +1 -0
- package/dist/helper.mjs +9 -7
- package/dist/helper.mjs.map +1 -0
- package/dist/http/elysia/index.d.mts +4 -4
- package/dist/http/elysia/index.d.ts +4 -4
- package/dist/http/elysia/index.js +43 -45
- package/dist/http/elysia/index.js.map +1 -0
- package/dist/http/elysia/index.mjs +6 -8
- package/dist/http/elysia/index.mjs.map +1 -0
- package/dist/http/express/index.d.mts +3 -3
- package/dist/http/express/index.d.ts +3 -3
- package/dist/http/express/index.js +40 -42
- package/dist/http/express/index.js.map +1 -0
- package/dist/http/express/index.mjs +6 -8
- package/dist/http/express/index.mjs.map +1 -0
- package/dist/http/fastify/index.d.mts +3 -3
- package/dist/http/fastify/index.d.ts +3 -3
- package/dist/http/fastify/index.js +41 -43
- package/dist/http/fastify/index.js.map +1 -0
- package/dist/http/fastify/index.mjs +6 -8
- package/dist/http/fastify/index.mjs.map +1 -0
- package/dist/http/h3/index.d.mts +3 -3
- package/dist/http/h3/index.d.ts +3 -3
- package/dist/http/h3/index.js +41 -43
- package/dist/http/h3/index.js.map +1 -0
- package/dist/http/h3/index.mjs +5 -7
- package/dist/http/h3/index.mjs.map +1 -0
- package/dist/http/hono/index.d.mts +3 -3
- package/dist/http/hono/index.d.ts +3 -3
- package/dist/http/hono/index.js +37 -39
- package/dist/http/hono/index.js.map +1 -0
- package/dist/http/hono/index.mjs +5 -7
- package/dist/http/hono/index.mjs.map +1 -0
- package/dist/http/hyper-express/index.d.mts +3 -3
- package/dist/http/hyper-express/index.d.ts +3 -3
- package/dist/http/hyper-express/index.js +38 -40
- package/dist/http/hyper-express/index.js.map +1 -0
- package/dist/http/hyper-express/index.mjs +5 -7
- package/dist/http/hyper-express/index.mjs.map +1 -0
- package/dist/http/koa/index.d.mts +3 -3
- package/dist/http/koa/index.d.ts +3 -3
- package/dist/http/koa/index.js +40 -42
- package/dist/http/koa/index.js.map +1 -0
- package/dist/http/koa/index.mjs +6 -8
- package/dist/http/koa/index.mjs.map +1 -0
- package/dist/index.d.mts +76 -12
- package/dist/index.d.ts +76 -12
- package/dist/index.js +103 -49
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +68 -14
- package/dist/index.mjs.map +1 -0
- package/dist/{meta-B_HW7cWh.d.ts → meta-BoS1E-Nz.d.mts} +6 -24
- package/dist/{meta-B_HW7cWh.d.mts → meta-BoS1E-Nz.d.ts} +6 -24
- package/dist/rpc/bullmq/index.d.mts +4 -6
- package/dist/rpc/bullmq/index.d.ts +4 -6
- package/dist/rpc/bullmq/index.js +13 -80
- package/dist/rpc/bullmq/index.js.map +1 -0
- package/dist/rpc/bullmq/index.mjs +6 -73
- package/dist/rpc/bullmq/index.mjs.map +1 -0
- package/dist/rpc/electron/index.d.mts +13 -0
- package/dist/rpc/electron/index.d.ts +13 -0
- package/dist/rpc/electron/index.js +73 -0
- package/dist/rpc/electron/index.js.map +1 -0
- package/dist/rpc/electron/index.mjs +73 -0
- package/dist/rpc/electron/index.mjs.map +1 -0
- package/dist/rpc/kafka/index.d.mts +4 -9
- package/dist/rpc/kafka/index.d.ts +4 -9
- package/dist/rpc/kafka/index.js +12 -90
- package/dist/rpc/kafka/index.js.map +1 -0
- package/dist/rpc/kafka/index.mjs +5 -83
- package/dist/rpc/kafka/index.mjs.map +1 -0
- package/dist/rpc/nats/index.d.mts +4 -6
- package/dist/rpc/nats/index.d.ts +4 -6
- package/dist/rpc/nats/index.js +13 -72
- package/dist/rpc/nats/index.js.map +1 -0
- package/dist/rpc/nats/index.mjs +5 -64
- package/dist/rpc/nats/index.mjs.map +1 -0
- package/dist/rpc/rabbitmq/index.d.mts +4 -6
- package/dist/rpc/rabbitmq/index.d.ts +4 -6
- package/dist/rpc/rabbitmq/index.js +14 -79
- package/dist/rpc/rabbitmq/index.js.map +1 -0
- package/dist/rpc/rabbitmq/index.mjs +6 -71
- package/dist/rpc/rabbitmq/index.mjs.map +1 -0
- package/dist/rpc/redis/index.d.mts +4 -9
- package/dist/rpc/redis/index.d.ts +4 -9
- package/dist/rpc/redis/index.js +12 -78
- package/dist/rpc/redis/index.js.map +1 -0
- package/dist/rpc/redis/index.mjs +6 -72
- package/dist/rpc/redis/index.mjs.map +1 -0
- package/dist/rpc/web-ext/index.d.mts +2 -0
- package/dist/rpc/web-ext/index.d.ts +2 -0
- package/dist/rpc/web-ext/index.js +1 -0
- package/dist/rpc/web-ext/index.js.map +1 -0
- package/dist/rpc/web-ext/index.mjs +1 -0
- package/dist/rpc/web-ext/index.mjs.map +1 -0
- package/dist/rpc/ws/index.d.mts +14 -0
- package/dist/rpc/ws/index.d.ts +14 -0
- package/dist/rpc/ws/index.js +74 -0
- package/dist/rpc/ws/index.js.map +1 -0
- package/dist/rpc/ws/index.mjs +74 -0
- package/dist/rpc/ws/index.mjs.map +1 -0
- package/dist/test.d.mts +8 -4
- package/dist/test.d.ts +8 -4
- package/dist/test.js +7 -6
- package/dist/test.js.map +1 -0
- package/dist/test.mjs +3 -2
- package/dist/test.mjs.map +1 -0
- package/dist/types-CInz3bD0.d.ts +14 -0
- package/dist/types-E-rqlg3-.d.mts +14 -0
- package/dist/{types-C2SQ9U9V.d.mts → types-SCJZFCYt.d.mts} +1 -1
- package/dist/{types-PFPIQWVF.d.ts → types-xruyYmyu.d.ts} +1 -1
- package/package.json +24 -8
- package/register/export.mjs +30 -0
- package/register/index.mjs +20 -12
- package/register/loader.mjs +94 -116
- package/register/utils.mjs +36 -6
- package/assets/ps.json +0 -24
- package/assets/tsconfig.json +0 -21
- package/dist/types-CYF0vha6.d.ts +0 -19
- package/dist/types-DEQb_e59.d.mts +0 -19
package/dist/index.mjs
CHANGED
|
@@ -6,9 +6,10 @@ import {
|
|
|
6
6
|
emitter,
|
|
7
7
|
phecdaNamespace,
|
|
8
8
|
useS
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-WTJKTICN.mjs";
|
|
10
10
|
import {
|
|
11
11
|
Addon,
|
|
12
|
+
ApiDoc,
|
|
12
13
|
Arg,
|
|
13
14
|
BaseParam,
|
|
14
15
|
Body,
|
|
@@ -21,6 +22,8 @@ import {
|
|
|
21
22
|
Guard,
|
|
22
23
|
Head,
|
|
23
24
|
Header,
|
|
25
|
+
ManyFiles,
|
|
26
|
+
OneFile,
|
|
24
27
|
Param,
|
|
25
28
|
Patch,
|
|
26
29
|
Pipe,
|
|
@@ -31,7 +34,7 @@ import {
|
|
|
31
34
|
Route,
|
|
32
35
|
Rpc,
|
|
33
36
|
Search
|
|
34
|
-
} from "./chunk-
|
|
37
|
+
} from "./chunk-VDOICQ2U.mjs";
|
|
35
38
|
import {
|
|
36
39
|
BadGatewayException,
|
|
37
40
|
BadRequestException,
|
|
@@ -57,12 +60,12 @@ import {
|
|
|
57
60
|
addPipe,
|
|
58
61
|
defaultPipe,
|
|
59
62
|
joinUrl
|
|
60
|
-
} from "./chunk-
|
|
63
|
+
} from "./chunk-LBH4RXQQ.mjs";
|
|
61
64
|
import {
|
|
62
65
|
ERROR_SYMBOL,
|
|
63
66
|
IS_DEV,
|
|
64
67
|
IS_ONLY_GENERATE,
|
|
65
|
-
|
|
68
|
+
IS_PURE,
|
|
66
69
|
IS_STRICT,
|
|
67
70
|
LOG_LEVEL,
|
|
68
71
|
Mixin,
|
|
@@ -72,15 +75,14 @@ import {
|
|
|
72
75
|
log,
|
|
73
76
|
runMiddleware,
|
|
74
77
|
setLogger
|
|
75
|
-
} from "./chunk-
|
|
78
|
+
} from "./chunk-MDPW6WGF.mjs";
|
|
76
79
|
|
|
77
80
|
// src/types.ts
|
|
78
|
-
var ResponseSymbol = Symbol("response");
|
|
79
81
|
var CustomResponse = class {
|
|
80
82
|
static {
|
|
81
83
|
__name(this, "CustomResponse");
|
|
82
84
|
}
|
|
83
|
-
|
|
85
|
+
_ps_response;
|
|
84
86
|
};
|
|
85
87
|
|
|
86
88
|
// src/modules/base.ts
|
|
@@ -249,7 +251,7 @@ var Generator = class {
|
|
|
249
251
|
return this._path || `.ps/${this.name.toLowerCase()}.js`;
|
|
250
252
|
}
|
|
251
253
|
async output(meta) {
|
|
252
|
-
await fse.outputFile(this.path, this.generateCode(meta
|
|
254
|
+
await fse.outputFile(this.path, this.generateCode(meta));
|
|
253
255
|
}
|
|
254
256
|
};
|
|
255
257
|
|
|
@@ -282,9 +284,9 @@ var RPCGenerator = class extends Generator {
|
|
|
282
284
|
`;
|
|
283
285
|
}
|
|
284
286
|
generateCode(meta) {
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
}
|
|
287
|
+
meta.forEach(({ data }) => {
|
|
288
|
+
if (data.controller === "rpc") this.addMethod(data);
|
|
289
|
+
});
|
|
288
290
|
return this.getContent();
|
|
289
291
|
}
|
|
290
292
|
};
|
|
@@ -323,9 +325,56 @@ return ret
|
|
|
323
325
|
`;
|
|
324
326
|
}
|
|
325
327
|
generateCode(meta) {
|
|
326
|
-
|
|
327
|
-
if (
|
|
328
|
+
meta.forEach(({ data }) => {
|
|
329
|
+
if (data.controller === "http") this.addMethod(data);
|
|
330
|
+
});
|
|
331
|
+
return this.getContent();
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// src/generator/openapi.ts
|
|
336
|
+
import { getMergedMeta } from "phecda-core";
|
|
337
|
+
var OpenAPIGenerator = class extends Generator {
|
|
338
|
+
static {
|
|
339
|
+
__name(this, "OpenAPIGenerator");
|
|
340
|
+
}
|
|
341
|
+
name = "OpenAPI";
|
|
342
|
+
paths = {};
|
|
343
|
+
getContent() {
|
|
344
|
+
return JSON.stringify({
|
|
345
|
+
openapi: "3.0.0",
|
|
346
|
+
info: {
|
|
347
|
+
title: "API Documentation",
|
|
348
|
+
version: "1.0.0",
|
|
349
|
+
description: "API documentation generated by phecda-server"
|
|
350
|
+
},
|
|
351
|
+
paths: this.paths
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
addMethod(args) {
|
|
355
|
+
const { http, tag, func } = args;
|
|
356
|
+
if (!http?.type) return;
|
|
357
|
+
const path = joinUrl(http.prefix, http.route);
|
|
358
|
+
if (!this.paths[path]) {
|
|
359
|
+
this.paths[path] = {};
|
|
328
360
|
}
|
|
361
|
+
const config = getMergedMeta(useS().getModel(tag), func)["openapi"];
|
|
362
|
+
this.paths[path][http.type] = {
|
|
363
|
+
summary: config.summary,
|
|
364
|
+
description: config.description,
|
|
365
|
+
tags: config.tags || [
|
|
366
|
+
tag
|
|
367
|
+
],
|
|
368
|
+
deprecated: config.deprecated,
|
|
369
|
+
parameters: config.parameters,
|
|
370
|
+
requestBody: config.requestBody,
|
|
371
|
+
responses: config.responses
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
generateCode(meta) {
|
|
375
|
+
meta.forEach(({ data }) => {
|
|
376
|
+
if (data.controller === "http") this.addMethod(data);
|
|
377
|
+
});
|
|
329
378
|
return this.getContent();
|
|
330
379
|
}
|
|
331
380
|
};
|
|
@@ -334,6 +383,7 @@ return ret
|
|
|
334
383
|
export * from "phecda-core";
|
|
335
384
|
export {
|
|
336
385
|
Addon,
|
|
386
|
+
ApiDoc,
|
|
337
387
|
Arg,
|
|
338
388
|
BadGatewayException,
|
|
339
389
|
BadRequestException,
|
|
@@ -361,13 +411,16 @@ export {
|
|
|
361
411
|
HttpBase,
|
|
362
412
|
IS_DEV,
|
|
363
413
|
IS_ONLY_GENERATE,
|
|
364
|
-
|
|
414
|
+
IS_PURE,
|
|
365
415
|
IS_STRICT,
|
|
366
416
|
InvalidInputException,
|
|
367
417
|
LOG_LEVEL,
|
|
418
|
+
ManyFiles,
|
|
368
419
|
Meta,
|
|
369
420
|
Mixin,
|
|
370
421
|
NotFoundException,
|
|
422
|
+
OneFile,
|
|
423
|
+
OpenAPIGenerator,
|
|
371
424
|
PAddon,
|
|
372
425
|
PExtension,
|
|
373
426
|
PFilter,
|
|
@@ -411,3 +464,4 @@ export {
|
|
|
411
464
|
setLogger,
|
|
412
465
|
useS
|
|
413
466
|
};
|
|
467
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/modules/base.ts","../src/modules/filter.ts","../src/modules/guard.ts","../src/modules/pipe.ts","../src/modules/addon.ts","../src/modules/extension.ts","../src/generator/utils.ts","../src/generator/rpc.ts","../src/generator/http.ts","../src/generator/openapi.ts","../src/index.ts"],"sourcesContent":["import type { Events } from 'phecda-core'\r\nimport type { ControllerMeta } from './meta'\r\nimport type { ERROR_SYMBOL } from './common'\r\nexport interface Emitter {\r\n on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void\r\n once<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void\r\n off<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void\r\n removeAllListeners<N extends keyof Events>(eventName: N): void\r\n emit<N extends keyof Events>(eventName: N, param: Events[N]): void\r\n}\r\n\r\nexport interface BaseCtx {\r\n meta: ControllerMeta\r\n moduleMap: Record<string, any>\r\n type: string\r\n tag: string\r\n func: string\r\n category: string\r\n [key: string]: any\r\n\r\n}\r\n\r\nexport interface DefaultOptions {\r\n globalGuards?: string[]\r\n globalFilter?: string\r\n globalPipe?: string\r\n globalAddons?: string[]\r\n}\r\n\r\nexport interface BaseError {\r\n // as a symbol\r\n [ERROR_SYMBOL]: true\r\n status: number\r\n message: string\r\n description: string\r\n}\r\n// export type RetOrErr<R> = { [K in keyof R]: Awaited<R[K]> | Error }\r\n\r\nexport type BaseRequestType = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options'\r\n\r\nexport class CustomResponse<Value> {\r\n _ps_response: Value\r\n}\r\n\r\nexport type ExtractResponse<Class extends CustomResponse<any>> = Class extends CustomResponse<infer Value> ? Value : never\r\n","import { Base } from 'phecda-core'\r\nimport { type LogLevel, log } from '../../src/utils'\r\nimport type { HttpCtx } from '../http/types'\r\nimport type { RpcCtx } from '../rpc/types'\r\nimport { Ctx } from '../decorators'\r\nimport { emitter } from '../core'\r\n\r\nexport class ServerBase extends Base {\r\n emitter = emitter\r\n\r\n log(msg: unknown, level: LogLevel = 'log') {\r\n log(msg, level, this.tag)\r\n }\r\n}\r\n\r\nexport class HttpBase<Ctx extends HttpCtx = HttpCtx> extends ServerBase {\r\n @Ctx\r\n context: Ctx\r\n}\r\nexport class RpcBase<Ctx extends RpcCtx = RpcCtx> extends ServerBase {\r\n @Ctx\r\n context: Ctx\r\n}\r\n","import { getTag } from 'phecda-core'\r\nimport { Context, addFilter } from '../context'\r\nimport type { Exception } from '../exception'\r\nimport type { BaseCtx, BaseError } from '../types'\r\nimport { ServerBase } from './base'\r\n\r\nexport abstract class PFilter<Ctx extends BaseCtx = any, E extends Exception = Exception > extends ServerBase {\r\n readonly key: PropertyKey\r\n constructor(tag?: string) {\r\n super()\r\n this.key = tag || getTag(this)\r\n\r\n addFilter(this.key, this.use.bind(this))\r\n this.onUnmount(() => {\r\n delete Context.filterRecord[this.key]\r\n })\r\n }\r\n\r\n abstract use(error: Error | E, ctx?: Ctx): BaseError\r\n}\r\n","import { getTag } from 'phecda-core'\r\nimport { Context, addGuard } from '../context'\r\nimport type { BaseCtx } from '../types'\r\nimport { ServerBase } from './base'\r\n\r\nexport abstract class PGuard<Ctx extends BaseCtx = any> extends ServerBase {\r\n abstract use(ctx: Ctx, next: () => Promise<any>): any\r\n readonly key: PropertyKey\r\n priority = 0\r\n\r\n constructor(tag?: string) {\r\n super()\r\n this.key = tag || getTag(this)\r\n\r\n addGuard(this.key, this.use.bind(this), this.priority)\r\n\r\n this.onUnmount(() => {\r\n delete Context.guardRecord[this.key]\r\n })\r\n }\r\n}\r\n","import { getTag } from 'phecda-core'\r\nimport { Context, addPipe } from '../context'\r\nimport type { BaseCtx } from '../types'\r\nimport { ServerBase } from './base'\r\nexport abstract class PPipe<Ctx extends BaseCtx = any> extends ServerBase {\r\n readonly key: PropertyKey\r\n\r\n constructor(tag?: string) {\r\n super()\r\n\r\n this.key = tag || getTag(this)\r\n addPipe(this.key, this.use.bind(this))\r\n this.onUnmount(() => {\r\n delete Context.pipeRecord[this.key]\r\n })\r\n }\r\n\r\n abstract use(param: { arg: any; option?: any; key: string; type: string; index: number; reflect: any }, ctx: Ctx): any\r\n}\r\n","import { getTag } from 'phecda-core'\r\nimport { Context, addAddon } from '../context'\r\nimport { ServerBase } from './base'\r\n\r\nexport abstract class PAddon extends ServerBase {\r\n readonly key: PropertyKey\r\n\r\n priority = 0\r\n constructor(tag?: string) {\r\n super()\r\n this.key = tag || getTag(this)\r\n\r\n addAddon(this.key, this.use.bind(this), this.priority)\r\n\r\n this.onUnmount(() => {\r\n delete Context.addonRecord[this.key]\r\n })\r\n }\r\n\r\n abstract use(router: any, framework: string): undefined\r\n}\r\n","import { getTag } from 'phecda-core'\r\nimport type { BaseCtx, BaseError } from '../types'\r\nimport { Context, addAddon, addFilter, addGuard, addPipe } from '../context'\r\nimport type { Exception } from '../exception'\r\nimport { ServerBase } from './base'\r\n\r\nexport interface PExtension<Ctx extends BaseCtx = any, E extends Exception = Exception> {\r\n\r\n guard(ctx: Ctx): Promise<boolean> | boolean\r\n\r\n pipe(param: { arg: any; option?: any; key: string; type: string; index: number; reflect: any }, ctx: Ctx): any\r\n\r\n filter(error: Error | E, ctx?: Ctx): BaseError\r\n\r\n addon<Addon = any>(framework: string): Addon\r\n}\r\n\r\nexport class PExtension extends ServerBase {\r\n readonly key: PropertyKey\r\n\r\n guardPriority: number\r\n addonPriority: number\r\n\r\n constructor(tag?: string) {\r\n super()\r\n\r\n const key = this.key = tag || getTag(this)\r\n\r\n if (this.pipe) {\r\n addPipe(key, this.pipe.bind(this))\r\n this.onUnmount(() => {\r\n // no safe enough\r\n delete Context.pipeRecord[key]\r\n })\r\n }\r\n\r\n if (this.addon) {\r\n addAddon(key, this.addon.bind(this), this.addonPriority)\r\n\r\n this.onUnmount(() => {\r\n delete Context.addonRecord[key]\r\n })\r\n }\r\n\r\n if (this.guard) {\r\n addGuard(key, this.guard.bind(this), this.guardPriority)\r\n\r\n this.onUnmount(() => {\r\n delete Context.guardRecord[key]\r\n })\r\n }\r\n\r\n if (this.filter) {\r\n addFilter(key, this.filter.bind(this))\r\n this.onUnmount(() => {\r\n delete Context.filterRecord[key]\r\n })\r\n }\r\n }\r\n}\r\n","import fse from 'fs-extra'\r\nimport type { Meta } from '../meta'\r\nexport abstract class Generator {\r\n private _path: string\r\n constructor(\r\n path?: string,\r\n ) {\r\n if (path)\r\n this._path = path\r\n }\r\n\r\n abstract name: string\r\n\r\n get path() {\r\n return this._path || `.ps/${this.name.toLowerCase()}.js`\r\n }\r\n\r\n abstract generateCode(meta: Meta[]): string\r\n\r\n async output(meta: Meta[]) {\r\n await fse.outputFile(this.path, this.generateCode(meta))\r\n }\r\n}\r\n","import type { ControllerMetaData, Meta } from '../meta'\r\nimport { Generator } from './utils'\r\n\r\nexport class RPCGenerator extends Generator {\r\n name = 'RPC'\r\n classMap: Record<string, { [key: string]: string }> = {}\r\n\r\n getContent() {\r\n let content = ''\r\n\r\n for (const name in this.classMap) {\r\n content += `\r\n export class ${name}{\r\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\r\n }`\r\n }\r\n return content\r\n }\r\n\r\n addMethod(args: ControllerMetaData) {\r\n const {\r\n rpc, name, func, tag,\r\n } = args\r\n\r\n if (!rpc)\r\n return\r\n if (!this.classMap[name])\r\n this.classMap[name] = {}\r\n this.classMap[name][func] = `\r\n ${func}(){\r\n return {tag:'${tag as string}',func:\"${func}\",isEvent:${!!rpc.isEvent},queue:\"${rpc.queue || ''}\"}\r\n\r\n }\r\n `\r\n }\r\n\r\n generateCode(meta: Meta[]): string {\r\n // for (const i of meta) {\r\n // if (i.controller === 'rpc')\r\n // this.addMethod(i as ControllerMetaData)\r\n // }\r\n meta.forEach(({ data }) => {\r\n if (data.controller === 'rpc')\r\n this.addMethod(data as ControllerMetaData)\r\n })\r\n return this.getContent()\r\n }\r\n}\r\n","import { joinUrl } from '../helper'\r\nimport type { ControllerMetaData, Meta } from '../meta'\r\nimport { Generator } from './utils'\r\n\r\nexport class HTTPGenerator extends Generator {\r\n name = 'HTTP'\r\n classMap: Record<string, { [key: string]: string }> = {}\r\n\r\n getContent() {\r\n let content = ''\r\n\r\n for (const name in this.classMap) {\r\n content += `\r\n export class ${name}{\r\n ${Object.values(this.classMap[name]).reduce((p, c) => p + c)}\r\n }`\r\n }\r\n return content\r\n }\r\n\r\n addMethod(args: ControllerMetaData) {\r\n const {\r\n http, name, func, params, tag,\r\n } = args\r\n if (!http?.type)\r\n return\r\n const url = joinUrl(http.prefix, http.route).replace(/\\/\\:([^\\/]*)/g, (_, js) => `/{{${js}}}`)\r\n if (!this.classMap[name])\r\n this.classMap[name] = {}\r\n this.classMap[name][func] = `\r\n ${func}(...args){\r\nconst ret={tag:\"${tag as string}\",func:\"${func}\",body:{},headers:{},query:{},params:{},method:\"${http.type}\",url:\"${url}\"}\r\n\r\n${params.reduce((p, c, i) => `${p}ret.${c.type}${c.key ? `['${c.key}']` : ''}=args[${i}]\\n${c.type === 'params' ? `ret.url=ret.url.replace('{{${c.key}}}',args[${i}])` : ''}\\n`, '')}\r\nreturn ret\r\n }\r\n `\r\n }\r\n\r\n generateCode(meta: Meta[]): string {\r\n meta.forEach(({ data }) => {\r\n if (data.controller === 'http')\r\n this.addMethod(data as ControllerMetaData)\r\n })\r\n return this.getContent()\r\n }\r\n}\r\n","import { useS } from '../core'\r\nimport { joinUrl } from '../helper'\r\nimport type { ControllerMetaData, Meta } from '../meta'\r\nimport { Generator } from './utils'\r\nimport { getMergedMeta } from 'phecda-core'\r\n\r\nexport class OpenAPIGenerator extends Generator {\r\n name = 'OpenAPI'\r\n paths: Record<string, any> = {}\r\n getContent() {\r\n return JSON.stringify({\r\n openapi: '3.0.0',\r\n info: {\r\n title: 'API Documentation',\r\n version: '1.0.0',\r\n description: 'API documentation generated by phecda-server'\r\n },\r\n paths: this.paths\r\n })\r\n }\r\n\r\n addMethod(args: ControllerMetaData) {\r\n const {\r\n http, tag, func\r\n } = args\r\n if (!http?.type)\r\n return\r\n\r\n const path = joinUrl(http.prefix, http.route)\r\n if (!this.paths[path]) {\r\n this.paths[path] = {}\r\n }\r\n\r\n const config = getMergedMeta(useS().getModel(tag), func)['openapi']\r\n this.paths[path][http.type as string] = {\r\n summary: config.summary,\r\n description: config.description,\r\n tags: config.tags || [tag],\r\n deprecated: config.deprecated,\r\n parameters: config.parameters,\r\n requestBody: config.requestBody,\r\n responses: config.responses\r\n };\r\n }\r\n\r\n generateCode(meta: Meta[]): string {\r\n meta.forEach(({ data }) => {\r\n if (data.controller === 'http')\r\n this.addMethod(data as ControllerMetaData)\r\n })\r\n return this.getContent()\r\n }\r\n}\r\n","export * from './context'\r\nexport * from './types'\r\nexport * from './core'\r\nexport * from './decorators'\r\nexport * from './exception'\r\nexport * from './pipe'\r\nexport * from './meta'\r\nexport * from './common'\r\nexport * from './modules'\r\nexport * from './generator'\r\nexport * from './utils'\r\n\r\nexport * from 'phecda-core'\r\n\r\nexport * from './http/types'\r\nexport * from './rpc/types'\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCO,IAAMA,iBAAN,MAAMA;EAAb,OAAaA;;;EACXC;AACF;;;AC1CA,SAASC,YAAY;;;;;;;;;;;;AAOd,IAAMC,aAAN,cAAyBC,KAAAA;SAAAA;;;EAC9BC,UAAUA;EAEVC,IAAIC,KAAcC,QAAkB,OAAO;AACzCF,QAAIC,KAAKC,OAAO,KAAKC,GAAG;EAC1B;AACF;AAEO,IAAMC,WAAN,cAAsDP,WAAAA;SAAAA;;;EAE3DQ;AACF;;;;;AACO,IAAMC,UAAN,cAAmDT,WAAAA;SAAAA;;;EAExDQ;AACF;;;;;;;ACtBA,SAASE,cAAc;AAMhB,IAAe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xB;AAEA,UAAMI,SAASC,cAAcC,KAAAA,EAAOC,SAASV,GAAAA,GAAMC,IAAAA,EAAM,SAAA;AACzD,SAAKb,MAAMe,IAAAA,EAAMJ,KAAKG,IAAI,IAAc;MACpCS,SAASJ,OAAOI;MAChBf,aAAaW,OAAOX;MACpBgB,MAAML,OAAOK,QAAQ;QAACZ;;MACtBa,YAAYN,OAAOM;MACnBC,YAAYP,OAAOO;MACnBC,aAAaR,OAAOQ;MACpBC,WAAWT,OAAOS;IACtB;EACJ;EAEAC,aAAaC,MAAsB;AAC/BA,SAAKC,QAAQ,CAAC,EAAEC,KAAI,MAAE;AAClB,UAAIA,KAAKC,eAAe,OACpB,MAAKxB,UAAUuB,IAAAA;IACvB,CAAA;AACA,WAAO,KAAK/B,WAAU;EAC1B;AACJ;;;ACxCA,cAAc;","names":["CustomResponse","_ps_response","Base","ServerBase","Base","emitter","log","msg","level","tag","HttpBase","context","RpcBase","getTag","PFilter","ServerBase","key","constructor","tag","getTag","addFilter","use","bind","onUnmount","Context","filterRecord","getTag","PGuard","ServerBase","key","priority","constructor","tag","getTag","addGuard","use","bind","onUnmount","Context","guardRecord","getTag","PPipe","ServerBase","key","constructor","tag","getTag","addPipe","use","bind","onUnmount","Context","pipeRecord","getTag","PAddon","ServerBase","key","priority","constructor","tag","getTag","addAddon","use","bind","onUnmount","Context","addonRecord","getTag","PExtension","ServerBase","key","guardPriority","addonPriority","constructor","tag","getTag","pipe","addPipe","bind","onUnmount","Context","pipeRecord","addon","addAddon","addonRecord","guard","addGuard","guardRecord","filter","addFilter","filterRecord","fse","Generator","_path","constructor","path","name","toLowerCase","output","meta","fse","outputFile","generateCode","RPCGenerator","Generator","name","classMap","getContent","content","Object","values","reduce","p","c","addMethod","args","rpc","func","tag","isEvent","queue","generateCode","meta","forEach","data","controller","HTTPGenerator","Generator","name","classMap","getContent","content","Object","values","reduce","p","c","addMethod","args","http","func","params","tag","type","url","joinUrl","prefix","route","replace","_","js","i","key","generateCode","meta","forEach","data","controller","getMergedMeta","OpenAPIGenerator","Generator","name","paths","getContent","JSON","stringify","openapi","info","title","version","description","addMethod","args","http","tag","func","type","path","joinUrl","prefix","route","config","getMergedMeta","useS","getModel","summary","tags","deprecated","parameters","requestBody","responses","generateCode","meta","forEach","data","controller"]}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { Events
|
|
1
|
+
import { Events } from 'phecda-core';
|
|
2
2
|
|
|
3
3
|
declare const ERROR_SYMBOL = "__PS_ERROR__";
|
|
4
4
|
declare const IS_DEV: boolean;
|
|
5
5
|
declare const IS_ONLY_GENERATE: boolean;
|
|
6
6
|
declare const IS_STRICT: boolean;
|
|
7
|
+
declare const IS_PURE: boolean;
|
|
7
8
|
declare const LOG_LEVEL: number;
|
|
8
|
-
declare const IS_READY: unique symbol;
|
|
9
9
|
declare enum PS_EXIT_CODE {
|
|
10
|
-
RELAUNCH =
|
|
11
|
-
|
|
10
|
+
RELAUNCH = 4171,
|
|
11
|
+
EXIT = 4172
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
interface Emitter {
|
|
@@ -18,20 +18,6 @@ interface Emitter {
|
|
|
18
18
|
removeAllListeners<N extends keyof Events>(eventName: N): void;
|
|
19
19
|
emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
|
|
20
20
|
}
|
|
21
|
-
type AnyFunction = (...args: any) => any;
|
|
22
|
-
type ParseInstance<Instance extends Record<string, AnyFunction>> = {
|
|
23
|
-
[Key in keyof Instance]: ToClientFn<Instance[Key]>;
|
|
24
|
-
};
|
|
25
|
-
type PickFuncKeys<Type> = {
|
|
26
|
-
[Key in keyof Type]: Type[Key] extends (...args: any) => any ? (ReturnType<Type[Key]> extends CustomResponse<any> ? never : Key) : never;
|
|
27
|
-
}[keyof Type];
|
|
28
|
-
type ToClientMap<ControllerMap extends Record<string, Construct>> = {
|
|
29
|
-
[Key in keyof ControllerMap]: ToClientInstance<InstanceType<ControllerMap[Key]>>;
|
|
30
|
-
};
|
|
31
|
-
type ToClientInstance<Instance extends Record<string, any>> = ParseInstance<PickFunc<Instance>>;
|
|
32
|
-
type ToClientFn<Func extends AnyFunction> = (...p: Parameters<Func>) => Promise<BaseReturn<ReturnType<Func>>>;
|
|
33
|
-
type PickFunc<Instance> = Pick<Instance, PickFuncKeys<Instance>>;
|
|
34
|
-
type OmitFunction<Instance> = Omit<Instance, PickFuncKeys<Instance>>;
|
|
35
21
|
interface BaseCtx {
|
|
36
22
|
meta: ControllerMeta;
|
|
37
23
|
moduleMap: Record<string, any>;
|
|
@@ -53,13 +39,9 @@ interface BaseError {
|
|
|
53
39
|
message: string;
|
|
54
40
|
description: string;
|
|
55
41
|
}
|
|
56
|
-
type BaseReturn<T> = Awaited<T> extends {
|
|
57
|
-
toJSON(): infer R;
|
|
58
|
-
} ? R : Awaited<T>;
|
|
59
42
|
type BaseRequestType = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options';
|
|
60
|
-
declare const ResponseSymbol: unique symbol;
|
|
61
43
|
declare class CustomResponse<Value> {
|
|
62
|
-
|
|
44
|
+
_ps_response: Value;
|
|
63
45
|
}
|
|
64
46
|
type ExtractResponse<Class extends CustomResponse<any>> = Class extends CustomResponse<infer Value> ? Value : never;
|
|
65
47
|
|
|
@@ -105,4 +87,4 @@ interface ControllerMeta extends Meta {
|
|
|
105
87
|
data: ControllerMetaData;
|
|
106
88
|
}
|
|
107
89
|
|
|
108
|
-
export { type BaseCtx as B,
|
|
90
|
+
export { type BaseCtx as B, CustomResponse as C, type DefaultOptions as D, type Emitter as E, IS_DEV as I, LOG_LEVEL as L, Meta as M, PS_EXIT_CODE as P, type ServiceMetaData as S, type ControllerMeta as a, type ControllerMetaData as b, type BaseError as c, type BaseRequestType as d, type ExtractResponse as e, type MetaData as f, ERROR_SYMBOL as g, IS_ONLY_GENERATE as h, IS_STRICT as i, IS_PURE as j };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { Events
|
|
1
|
+
import { Events } from 'phecda-core';
|
|
2
2
|
|
|
3
3
|
declare const ERROR_SYMBOL = "__PS_ERROR__";
|
|
4
4
|
declare const IS_DEV: boolean;
|
|
5
5
|
declare const IS_ONLY_GENERATE: boolean;
|
|
6
6
|
declare const IS_STRICT: boolean;
|
|
7
|
+
declare const IS_PURE: boolean;
|
|
7
8
|
declare const LOG_LEVEL: number;
|
|
8
|
-
declare const IS_READY: unique symbol;
|
|
9
9
|
declare enum PS_EXIT_CODE {
|
|
10
|
-
RELAUNCH =
|
|
11
|
-
|
|
10
|
+
RELAUNCH = 4171,
|
|
11
|
+
EXIT = 4172
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
interface Emitter {
|
|
@@ -18,20 +18,6 @@ interface Emitter {
|
|
|
18
18
|
removeAllListeners<N extends keyof Events>(eventName: N): void;
|
|
19
19
|
emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
|
|
20
20
|
}
|
|
21
|
-
type AnyFunction = (...args: any) => any;
|
|
22
|
-
type ParseInstance<Instance extends Record<string, AnyFunction>> = {
|
|
23
|
-
[Key in keyof Instance]: ToClientFn<Instance[Key]>;
|
|
24
|
-
};
|
|
25
|
-
type PickFuncKeys<Type> = {
|
|
26
|
-
[Key in keyof Type]: Type[Key] extends (...args: any) => any ? (ReturnType<Type[Key]> extends CustomResponse<any> ? never : Key) : never;
|
|
27
|
-
}[keyof Type];
|
|
28
|
-
type ToClientMap<ControllerMap extends Record<string, Construct>> = {
|
|
29
|
-
[Key in keyof ControllerMap]: ToClientInstance<InstanceType<ControllerMap[Key]>>;
|
|
30
|
-
};
|
|
31
|
-
type ToClientInstance<Instance extends Record<string, any>> = ParseInstance<PickFunc<Instance>>;
|
|
32
|
-
type ToClientFn<Func extends AnyFunction> = (...p: Parameters<Func>) => Promise<BaseReturn<ReturnType<Func>>>;
|
|
33
|
-
type PickFunc<Instance> = Pick<Instance, PickFuncKeys<Instance>>;
|
|
34
|
-
type OmitFunction<Instance> = Omit<Instance, PickFuncKeys<Instance>>;
|
|
35
21
|
interface BaseCtx {
|
|
36
22
|
meta: ControllerMeta;
|
|
37
23
|
moduleMap: Record<string, any>;
|
|
@@ -53,13 +39,9 @@ interface BaseError {
|
|
|
53
39
|
message: string;
|
|
54
40
|
description: string;
|
|
55
41
|
}
|
|
56
|
-
type BaseReturn<T> = Awaited<T> extends {
|
|
57
|
-
toJSON(): infer R;
|
|
58
|
-
} ? R : Awaited<T>;
|
|
59
42
|
type BaseRequestType = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options';
|
|
60
|
-
declare const ResponseSymbol: unique symbol;
|
|
61
43
|
declare class CustomResponse<Value> {
|
|
62
|
-
|
|
44
|
+
_ps_response: Value;
|
|
63
45
|
}
|
|
64
46
|
type ExtractResponse<Class extends CustomResponse<any>> = Class extends CustomResponse<infer Value> ? Value : never;
|
|
65
47
|
|
|
@@ -105,4 +87,4 @@ interface ControllerMeta extends Meta {
|
|
|
105
87
|
data: ControllerMetaData;
|
|
106
88
|
}
|
|
107
89
|
|
|
108
|
-
export { type BaseCtx as B,
|
|
90
|
+
export { type BaseCtx as B, CustomResponse as C, type DefaultOptions as D, type Emitter as E, IS_DEV as I, LOG_LEVEL as L, Meta as M, PS_EXIT_CODE as P, type ServiceMetaData as S, type ControllerMeta as a, type ControllerMetaData as b, type BaseError as c, type BaseRequestType as d, type ExtractResponse as e, type MetaData as f, ERROR_SYMBOL as g, IS_ONLY_GENERATE as h, IS_STRICT as i, IS_PURE as j };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { WorkerOptions, QueueOptions, Worker, Queue } from 'bullmq';
|
|
2
|
-
import { F as Factory } from '../../core-
|
|
3
|
-
import { R as RpcCtx, a as RpcServerOptions
|
|
4
|
-
import { T as ToClientMap } from '../../meta-B_HW7cWh.mjs';
|
|
2
|
+
import { F as Factory } from '../../core-DbqWh47p.mjs';
|
|
3
|
+
import { R as RpcCtx, a as RpcServerOptions } from '../../types-E-rqlg3-.mjs';
|
|
5
4
|
import 'phecda-core';
|
|
5
|
+
import '../../meta-BoS1E-Nz.mjs';
|
|
6
6
|
|
|
7
7
|
interface BullmqCtx extends RpcCtx {
|
|
8
8
|
type: 'bullmq';
|
|
@@ -16,6 +16,4 @@ declare function create({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>
|
|
|
16
16
|
queueMap: Record<string, Queue<any, any, string, any, any, string>>;
|
|
17
17
|
}>;
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export { type BullmqCtx, type BullmqOptions, create, createClient };
|
|
19
|
+
export { type BullmqCtx, type BullmqOptions, create };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { WorkerOptions, QueueOptions, Worker, Queue } from 'bullmq';
|
|
2
|
-
import { F as Factory } from '../../core-
|
|
3
|
-
import { R as RpcCtx, a as RpcServerOptions
|
|
4
|
-
import { T as ToClientMap } from '../../meta-B_HW7cWh.js';
|
|
2
|
+
import { F as Factory } from '../../core-Rq7BhPHE.js';
|
|
3
|
+
import { R as RpcCtx, a as RpcServerOptions } from '../../types-CInz3bD0.js';
|
|
5
4
|
import 'phecda-core';
|
|
5
|
+
import '../../meta-BoS1E-Nz.js';
|
|
6
6
|
|
|
7
7
|
interface BullmqCtx extends RpcCtx {
|
|
8
8
|
type: 'bullmq';
|
|
@@ -16,6 +16,4 @@ declare function create({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>
|
|
|
16
16
|
queueMap: Record<string, Queue<any, any, string, any, any, string>>;
|
|
17
17
|
}>;
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export { type BullmqCtx, type BullmqOptions, create, createClient };
|
|
19
|
+
export { type BullmqCtx, type BullmqOptions, create };
|
package/dist/rpc/bullmq/index.js
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
+
var _chunkH7OCHDZTjs = require('../../chunk-H7OCHDZT.js');
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
var _chunk7EMAZGYOjs = require('../../chunk-7EMAZGYO.js');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
var _chunkVSGYTGHYjs = require('../../chunk-VSGYTGHY.js');
|
|
9
|
+
var _chunkRLFCI3RGjs = require('../../chunk-RLFCI3RG.js');
|
|
11
10
|
|
|
12
11
|
// src/rpc/bullmq/create.ts
|
|
13
12
|
var _bullmq = require('bullmq');
|
|
@@ -18,14 +17,14 @@ async function create({ moduleMap, meta }, opts = {}) {
|
|
|
18
17
|
const workerMap = {};
|
|
19
18
|
const queueMap = {};
|
|
20
19
|
const existQueue = /* @__PURE__ */ new Set();
|
|
21
|
-
const metaMap =
|
|
20
|
+
const metaMap = _chunkH7OCHDZTjs.createControllerMetaMap.call(void 0, meta, (meta2) => {
|
|
22
21
|
const { controller, rpc, func, tag } = meta2.data;
|
|
23
22
|
if (controller === "rpc" && _optionalChain([rpc, 'optionalAccess', _ => _.queue]) !== void 0) {
|
|
24
23
|
debug(`register method "${func}" in module "${tag}"`);
|
|
25
24
|
return true;
|
|
26
25
|
}
|
|
27
26
|
});
|
|
28
|
-
|
|
27
|
+
_chunkH7OCHDZTjs.detectAopDep.call(void 0, meta, {
|
|
29
28
|
guards: globalGuards
|
|
30
29
|
}, "rpc");
|
|
31
30
|
async function subscribeQueues() {
|
|
@@ -39,12 +38,12 @@ async function create({ moduleMap, meta }, opts = {}) {
|
|
|
39
38
|
if (existQueue.has(queue)) continue;
|
|
40
39
|
existQueue.add(queue);
|
|
41
40
|
workerMap[queue] = new (0, _bullmq.Worker)(queue, handleRequest, workerOpts);
|
|
42
|
-
|
|
41
|
+
_chunkH7OCHDZTjs.Context.applyAddons(addons, workerMap[queue], "bullmq");
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
|
-
|
|
46
|
+
_chunkRLFCI3RGjs.__name.call(void 0, subscribeQueues, "subscribeQueues");
|
|
48
47
|
async function handleRequest(job) {
|
|
49
48
|
const { data } = job;
|
|
50
49
|
const { tag, func, args, id, queue: clientQueue, _ps } = data;
|
|
@@ -53,12 +52,12 @@ async function create({ moduleMap, meta }, opts = {}) {
|
|
|
53
52
|
const meta2 = metaMap.get(tag)[func];
|
|
54
53
|
const { data: { rpc: { isEvent } = {} } } = meta2;
|
|
55
54
|
if (!isEvent && !(clientQueue in queueMap)) queueMap[clientQueue] = new (0, _bullmq.Queue)(clientQueue, queueOpts);
|
|
56
|
-
const aop =
|
|
55
|
+
const aop = _chunkH7OCHDZTjs.Context.getAop(meta2, {
|
|
57
56
|
globalFilter,
|
|
58
57
|
globalGuards,
|
|
59
58
|
globalPipe
|
|
60
59
|
});
|
|
61
|
-
const context = new (0,
|
|
60
|
+
const context = new (0, _chunkH7OCHDZTjs.Context)({
|
|
62
61
|
type: "bullmq",
|
|
63
62
|
category: "rpc",
|
|
64
63
|
moduleMap,
|
|
@@ -86,9 +85,9 @@ async function create({ moduleMap, meta }, opts = {}) {
|
|
|
86
85
|
}
|
|
87
86
|
});
|
|
88
87
|
}
|
|
89
|
-
|
|
88
|
+
_chunkRLFCI3RGjs.__name.call(void 0, handleRequest, "handleRequest");
|
|
90
89
|
subscribeQueues();
|
|
91
|
-
|
|
90
|
+
_chunkRLFCI3RGjs.HMR.call(void 0, async () => {
|
|
92
91
|
for (const i in workerMap) await workerMap[i].close(true);
|
|
93
92
|
for (const i in queueMap) await queueMap[i].close();
|
|
94
93
|
await subscribeQueues();
|
|
@@ -98,74 +97,8 @@ async function create({ moduleMap, meta }, opts = {}) {
|
|
|
98
97
|
queueMap
|
|
99
98
|
};
|
|
100
99
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// src/rpc/bullmq/client.ts
|
|
104
|
-
var _events = require('events');
|
|
105
|
-
|
|
106
|
-
async function createClient(controllers, opts = {}) {
|
|
107
|
-
let eventId = 1;
|
|
108
|
-
let eventCount = 0;
|
|
109
|
-
const { max, workerOpts, queueOpts, timeout, key } = opts;
|
|
110
|
-
const ret = {};
|
|
111
|
-
const emitter = new (0, _events.EventEmitter)();
|
|
112
|
-
const clientQueue = _chunk7EMAZGYOjs.genClientQueue.call(void 0, key);
|
|
113
|
-
const queueMap = {};
|
|
114
|
-
new (0, _bullmq.Worker)(clientQueue, async (job) => {
|
|
115
|
-
const { data, id, error } = job.data;
|
|
116
|
-
emitter.emit(id, data, error);
|
|
117
|
-
}, workerOpts);
|
|
118
|
-
for (const i in controllers) {
|
|
119
|
-
ret[i] = new Proxy(new controllers[i](), {
|
|
120
|
-
get(target, p) {
|
|
121
|
-
if (typeof target[p] !== "function") throw new Error(`"${p}" in "${i}" is not an exposed rpc `);
|
|
122
|
-
let { tag, queue, isEvent } = target[p]();
|
|
123
|
-
return async (...args) => {
|
|
124
|
-
if (!queue) queue = tag;
|
|
125
|
-
if (!(queue in queueMap)) queueMap[queue] = new (0, _bullmq.Queue)(queue, queueOpts);
|
|
126
|
-
const id = `${eventId++}`;
|
|
127
|
-
queueMap[queue].add(`${tag}-${p}`, {
|
|
128
|
-
_ps: 1,
|
|
129
|
-
id,
|
|
130
|
-
args,
|
|
131
|
-
tag,
|
|
132
|
-
queue: clientQueue,
|
|
133
|
-
func: p
|
|
134
|
-
});
|
|
135
|
-
if (isEvent) return null;
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
if (max && eventCount >= max) reject({
|
|
138
|
-
type: "exceeded"
|
|
139
|
-
});
|
|
140
|
-
let isEnd = false;
|
|
141
|
-
const timer = setTimeout(() => {
|
|
142
|
-
if (!isEnd) {
|
|
143
|
-
eventCount--;
|
|
144
|
-
emitter.off(id, listener);
|
|
145
|
-
reject({
|
|
146
|
-
type: "timeout"
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}, timeout || 5e3);
|
|
150
|
-
function listener(data, error) {
|
|
151
|
-
eventCount--;
|
|
152
|
-
isEnd = true;
|
|
153
|
-
clearTimeout(timer);
|
|
154
|
-
if (error) reject(data);
|
|
155
|
-
else resolve(data);
|
|
156
|
-
}
|
|
157
|
-
_chunkVSGYTGHYjs.__name.call(void 0, listener, "listener");
|
|
158
|
-
eventCount++;
|
|
159
|
-
emitter.once(id, listener);
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
return ret;
|
|
166
|
-
}
|
|
167
|
-
_chunkVSGYTGHYjs.__name.call(void 0, createClient, "createClient");
|
|
168
|
-
|
|
100
|
+
_chunkRLFCI3RGjs.__name.call(void 0, create, "create");
|
|
169
101
|
|
|
170
102
|
|
|
171
|
-
exports.create = create;
|
|
103
|
+
exports.create = create;
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["d:\\myproject\\phecda\\packages\\server\\dist\\rpc\\bullmq\\index.js"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACF,0DAAgC;AAChC;AACE;AACA;AACF,0DAAgC;AAChC;AACA;AACA,gCAAsC;AACtC,4EAAyB;AACzB,IAAI,MAAM,EAAE,6BAAK,sBAAuB,CAAC;AACzC,MAAM,SAAS,MAAM,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE;AACtD,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,EAAE,IAAI;AAC9F,EAAE,MAAM,UAAU,EAAE,CAAC,CAAC;AACtB,EAAE,MAAM,SAAS,EAAE,CAAC,CAAC;AACrB,EAAE,MAAM,WAAW,kBAAkB,IAAI,GAAG,CAAC,CAAC;AAC9C,EAAE,MAAM,QAAQ,EAAE,sDAAuB,IAAK,EAAE,CAAC,KAAK,EAAE,GAAG;AAC3D,IAAI,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,KAAK,CAAC,IAAI;AACrD,IAAI,GAAG,CAAC,WAAW,IAAI,MAAM,mB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GAAG;AAC3C,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,EAAA;AACA,QAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,MAAA;AACA,QAAA;AACA,UAAA;AACA,UAAA;AACA,UAAA;AACA,QAAA;AACA,MAAA;AACA,IAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA,EAAA;AACA,IAAA;AACA,IAAA;AACA,EAAA;AACA;AACA;AACA;AACA;AACA","file":"D:\\myproject\\phecda\\packages\\server\\dist\\rpc\\bullmq\\index.js","sourcesContent":[null]}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Context,
|
|
3
|
-
HMR,
|
|
4
3
|
createControllerMetaMap,
|
|
5
|
-
detectAopDep
|
|
6
|
-
|
|
7
|
-
} from "../../chunk-SGMQZRP6.mjs";
|
|
4
|
+
detectAopDep
|
|
5
|
+
} from "../../chunk-LBH4RXQQ.mjs";
|
|
8
6
|
import {
|
|
7
|
+
HMR,
|
|
9
8
|
__name
|
|
10
|
-
} from "../../chunk-
|
|
9
|
+
} from "../../chunk-MDPW6WGF.mjs";
|
|
11
10
|
|
|
12
11
|
// src/rpc/bullmq/create.ts
|
|
13
12
|
import { Queue, Worker } from "bullmq";
|
|
@@ -99,73 +98,7 @@ async function create({ moduleMap, meta }, opts = {}) {
|
|
|
99
98
|
};
|
|
100
99
|
}
|
|
101
100
|
__name(create, "create");
|
|
102
|
-
|
|
103
|
-
// src/rpc/bullmq/client.ts
|
|
104
|
-
import { EventEmitter } from "events";
|
|
105
|
-
import { Queue as Queue2, Worker as Worker2 } from "bullmq";
|
|
106
|
-
async function createClient(controllers, opts = {}) {
|
|
107
|
-
let eventId = 1;
|
|
108
|
-
let eventCount = 0;
|
|
109
|
-
const { max, workerOpts, queueOpts, timeout, key } = opts;
|
|
110
|
-
const ret = {};
|
|
111
|
-
const emitter = new EventEmitter();
|
|
112
|
-
const clientQueue = genClientQueue(key);
|
|
113
|
-
const queueMap = {};
|
|
114
|
-
new Worker2(clientQueue, async (job) => {
|
|
115
|
-
const { data, id, error } = job.data;
|
|
116
|
-
emitter.emit(id, data, error);
|
|
117
|
-
}, workerOpts);
|
|
118
|
-
for (const i in controllers) {
|
|
119
|
-
ret[i] = new Proxy(new controllers[i](), {
|
|
120
|
-
get(target, p) {
|
|
121
|
-
if (typeof target[p] !== "function") throw new Error(`"${p}" in "${i}" is not an exposed rpc `);
|
|
122
|
-
let { tag, queue, isEvent } = target[p]();
|
|
123
|
-
return async (...args) => {
|
|
124
|
-
if (!queue) queue = tag;
|
|
125
|
-
if (!(queue in queueMap)) queueMap[queue] = new Queue2(queue, queueOpts);
|
|
126
|
-
const id = `${eventId++}`;
|
|
127
|
-
queueMap[queue].add(`${tag}-${p}`, {
|
|
128
|
-
_ps: 1,
|
|
129
|
-
id,
|
|
130
|
-
args,
|
|
131
|
-
tag,
|
|
132
|
-
queue: clientQueue,
|
|
133
|
-
func: p
|
|
134
|
-
});
|
|
135
|
-
if (isEvent) return null;
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
if (max && eventCount >= max) reject({
|
|
138
|
-
type: "exceeded"
|
|
139
|
-
});
|
|
140
|
-
let isEnd = false;
|
|
141
|
-
const timer = setTimeout(() => {
|
|
142
|
-
if (!isEnd) {
|
|
143
|
-
eventCount--;
|
|
144
|
-
emitter.off(id, listener);
|
|
145
|
-
reject({
|
|
146
|
-
type: "timeout"
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}, timeout || 5e3);
|
|
150
|
-
function listener(data, error) {
|
|
151
|
-
eventCount--;
|
|
152
|
-
isEnd = true;
|
|
153
|
-
clearTimeout(timer);
|
|
154
|
-
if (error) reject(data);
|
|
155
|
-
else resolve(data);
|
|
156
|
-
}
|
|
157
|
-
__name(listener, "listener");
|
|
158
|
-
eventCount++;
|
|
159
|
-
emitter.once(id, listener);
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
return ret;
|
|
166
|
-
}
|
|
167
|
-
__name(createClient, "createClient");
|
|
168
101
|
export {
|
|
169
|
-
create
|
|
170
|
-
createClient
|
|
102
|
+
create
|
|
171
103
|
};
|
|
104
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/rpc/bullmq/create.ts"],"sourcesContent":["import type { Job, QueueOptions, WorkerOptions } from 'bullmq'\r\nimport { Queue, Worker } from 'bullmq'\r\nimport Debug from 'debug'\r\nimport type { Factory } from '../../core'\r\nimport { Context } from '../../context'\r\nimport type { RpcCtx, RpcServerOptions } from '../types'\r\nimport { HMR } from '../../hmr'\r\n\r\nimport { createControllerMetaMap, detectAopDep } from '../../helper'\r\n\r\nconst debug = Debug('phecda-server/bullmq')\r\n\r\nexport interface BullmqCtx extends RpcCtx {\r\n type: 'bullmq'\r\n}\r\n\r\nexport type BullmqOptions = Omit<RpcServerOptions, 'globalAddons'> & {\r\n workerOpts?: WorkerOptions\r\n queueOpts?: QueueOptions\r\n}\r\n\r\nexport async function create({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, opts: BullmqOptions = {}) {\r\n const { globalGuards, globalFilter, globalPipe, workerOpts, queueOpts, defaultQueue } = opts\r\n\r\n const workerMap: Record<string, Worker> = {}\r\n const queueMap: Record<string, Queue> = {}\r\n const existQueue = new Set<string>()\r\n const metaMap = createControllerMetaMap(meta, (meta) => {\r\n const { controller, rpc, func, tag } = meta.data\r\n if (controller === 'rpc' && rpc?.queue !== undefined) {\r\n debug(`register method \"${func}\" in module \"${tag}\"`)\r\n return true\r\n }\r\n })\r\n\r\n detectAopDep(meta, {\r\n guards: globalGuards,\r\n }, 'rpc')\r\n\r\n async function subscribeQueues() {\r\n existQueue.clear()\r\n for (const [tag, record] of metaMap) {\r\n for (const func in record) {\r\n const meta = metaMap.get(tag)![func]\r\n\r\n const {\r\n data: {\r\n rpc,\r\n addons,\r\n },\r\n } = meta\r\n\r\n if (rpc) {\r\n const queue = rpc.queue || defaultQueue || tag\r\n if (existQueue.has(queue))\r\n continue\r\n existQueue.add(queue)\r\n workerMap[queue] = new Worker(queue, handleRequest, workerOpts)\r\n\r\n Context.applyAddons(addons, workerMap[queue], 'bullmq')\r\n }\r\n }\r\n }\r\n }\r\n\r\n async function handleRequest(job: Job) {\r\n const { data } = job\r\n const { tag, func, args, id, queue: clientQueue, _ps } = data\r\n if (_ps !== 1)\r\n return\r\n debug(`invoke method \"${func}\" in module \"${tag}\"`)\r\n const meta = metaMap.get(tag)![func]\r\n\r\n const {\r\n data: {\r\n rpc: { isEvent } = {},\r\n },\r\n } = meta\r\n\r\n if (!isEvent && !(clientQueue in queueMap))\r\n queueMap[clientQueue] = new Queue(clientQueue, queueOpts)\r\n\r\n const aop = Context.getAop(meta, {\r\n globalFilter,\r\n globalGuards,\r\n globalPipe,\r\n })\r\n const context = new Context<BullmqCtx>({\r\n type: 'bullmq',\r\n category: 'rpc',\r\n moduleMap,\r\n meta,\r\n tag,\r\n func,\r\n data,\r\n args,\r\n id,\r\n queue: job.queueName,\r\n isEvent,\r\n })\r\n\r\n await context.run(aop, (returnData) => {\r\n if (!isEvent)\r\n\r\n queueMap[clientQueue].add(`${tag}-${func}`, { data: returnData, id })\r\n }, (err) => {\r\n if (!isEvent) {\r\n queueMap[clientQueue].add(`${tag}-${func}`, {\r\n data: err,\r\n error: true,\r\n id,\r\n })\r\n }\r\n })\r\n }\r\n\r\n subscribeQueues()\r\n\r\n HMR(async () => {\r\n for (const i in workerMap)\r\n await workerMap[i].close(true)\r\n for (const i in queueMap)\r\n await queueMap[i].close()\r\n\r\n await subscribeQueues()\r\n })\r\n\r\n return { workerMap, queueMap }\r\n}\r\n"],"mappings":";;;;;;;;;;;AACA,SAASA,OAAOC,c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debug","create","moduleMap","meta","opts","globalGuards","globalFilter","globalPipe","workerOpts","queueOpts","defaultQueue","workerMap","queueMap","existQueue","Set","metaMap","createControllerMetaMap","controller","rpc","func","tag","data","queue","undefined","detectAopDep","guards","subscribeQueues","clear","record","get","addons","has","add","Worker","handleRequest","Context","applyAddons","job","args","id","clientQueue","_ps","isEvent","Queue","aop","getAop","context","type","category","queueName","run","returnData","err","error","HMR","i","close"]}
|