phecda-server 3.1.0 → 3.2.0-alpha.1
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/bin/cli.js +29 -8
- package/dist/index.d.ts +21 -14
- package/dist/index.js +182 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +181 -106
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -3
- package/register/compile.mjs +29 -0
- package/register/index.mjs +34 -0
- package/register/loader.mjs +154 -0
package/bin/cli.js
CHANGED
|
@@ -1,12 +1,33 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
1
|
+
const { exec } = require('child_process')
|
|
2
|
+
const pc = require('picocolors')
|
|
3
|
+
const cmd = process.argv.slice(2)[0]
|
|
4
|
+
let child
|
|
5
|
+
function startChild() {
|
|
6
|
+
child = exec(`node --import phecda-server/register ${cmd}`, {
|
|
7
|
+
env: process.env,
|
|
8
|
+
// cwd: process.cwd(),
|
|
9
|
+
})
|
|
10
|
+
child.stderr.pipe(process.stderr)
|
|
11
|
+
child.stdin.pipe(process.stdin)
|
|
12
|
+
child.stdout.pipe(process.stdout)
|
|
4
13
|
|
|
5
|
-
|
|
6
|
-
|
|
14
|
+
child.on('exit', (code) => {
|
|
15
|
+
if (code === 3) {
|
|
16
|
+
log('relunch...')
|
|
17
|
+
return startChild()
|
|
18
|
+
}
|
|
7
19
|
|
|
8
|
-
|
|
9
|
-
|
|
20
|
+
process.exit(0)
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
process.on('SIGINT', () => {
|
|
25
|
+
child.kill('SIGINT')
|
|
26
|
+
})
|
|
10
27
|
|
|
28
|
+
function log(msg, color = 'green') {
|
|
29
|
+
const date = new Date()
|
|
30
|
+
console.log(`${pc.gray(`${date.getHours()} ${date.getMinutes()} ${date.getSeconds()}`)} ${pc.magenta('[phecda-server]')} ${pc[color](msg)}`)
|
|
11
31
|
}
|
|
12
|
-
|
|
32
|
+
|
|
33
|
+
startChild()
|
package/dist/index.d.ts
CHANGED
|
@@ -149,7 +149,7 @@ declare namespace P {
|
|
|
149
149
|
interface Pipe {
|
|
150
150
|
transform(args: {
|
|
151
151
|
arg: any;
|
|
152
|
-
option?:
|
|
152
|
+
option?: any;
|
|
153
153
|
key: string;
|
|
154
154
|
type: string;
|
|
155
155
|
index: number;
|
|
@@ -158,7 +158,7 @@ declare namespace P {
|
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
declare class
|
|
161
|
+
declare class Histroy {
|
|
162
162
|
guard: string[];
|
|
163
163
|
interceptor: string[];
|
|
164
164
|
record(name: string, type: 'guard' | 'interceptor'): boolean;
|
|
@@ -171,11 +171,10 @@ declare abstract class Context<Data = any> {
|
|
|
171
171
|
params: string[];
|
|
172
172
|
static metaRecord: Record<string, Meta>;
|
|
173
173
|
static metaDataRecord: Record<string, ReturnType<typeof parseMeta>>;
|
|
174
|
-
static instanceRecord: Record<string, any>;
|
|
175
174
|
static guardsRecord: Record<string, any>;
|
|
176
175
|
static interceptorsRecord: Record<string, any>;
|
|
177
176
|
post: ((...params: any) => any)[];
|
|
178
|
-
history:
|
|
177
|
+
history: Histroy;
|
|
179
178
|
constructor(key: string, data: Data);
|
|
180
179
|
static registerGuard(key: string, handler: any): void;
|
|
181
180
|
static registerInterceptor(key: string, handler: any): void;
|
|
@@ -185,7 +184,6 @@ declare abstract class Context<Data = any> {
|
|
|
185
184
|
}
|
|
186
185
|
declare function addGuard(key: string, handler: P.Guard): void;
|
|
187
186
|
declare function addInterceptor(key: string, handler: P.Interceptor): void;
|
|
188
|
-
declare function getInstance(tag: string): any;
|
|
189
187
|
declare function parseMeta(meta: Meta): {
|
|
190
188
|
guards: string[];
|
|
191
189
|
reflect: any[];
|
|
@@ -202,7 +200,7 @@ declare function parseMeta(meta: Meta): {
|
|
|
202
200
|
|
|
203
201
|
declare class ServerContext extends Context<ServerCtx | ServerMergeCtx> {
|
|
204
202
|
static pipe: P.Pipe;
|
|
205
|
-
static filter: ServerFilter
|
|
203
|
+
static filter: ServerFilter;
|
|
206
204
|
static middlewareRecord: Record<string, (...params: any) => any>;
|
|
207
205
|
static useMiddleware(middlewares: string[]): ((...params: any) => any)[];
|
|
208
206
|
usePipe(args: {
|
|
@@ -220,21 +218,33 @@ declare function addMiddleware(key: string, handler: RequestHandler): void;
|
|
|
220
218
|
declare function useServerPipe(pipe: P.Pipe): void;
|
|
221
219
|
declare function useServerFilter(filter: ServerFilter): void;
|
|
222
220
|
|
|
221
|
+
declare const SERIES_SYMBOL = "__symbol_series__";
|
|
222
|
+
declare const MERGE_SYMBOL = "__symbol_req__";
|
|
223
|
+
declare const UNMOUNT_SYMBOL = "__symbol_unmount__";
|
|
224
|
+
|
|
223
225
|
declare const emitter: Emitter;
|
|
224
|
-
declare
|
|
225
|
-
|
|
226
|
+
declare function Factory(Modules: (new (...args: any) => any)[], opts?: {
|
|
227
|
+
dev?: boolean;
|
|
228
|
+
file?: string;
|
|
229
|
+
}): Promise<{
|
|
226
230
|
moduleMap: Map<string, any>;
|
|
227
231
|
meta: Meta[];
|
|
228
|
-
|
|
232
|
+
constructorMap: Map<any, any>;
|
|
233
|
+
update: (Module: Construct) => Promise<void>;
|
|
229
234
|
}>;
|
|
235
|
+
declare class Dev {
|
|
236
|
+
[UNMOUNT_SYMBOL]: (() => void)[];
|
|
237
|
+
onUnmount(cb: () => void): void;
|
|
238
|
+
}
|
|
230
239
|
|
|
231
240
|
interface Options {
|
|
241
|
+
dev?: boolean;
|
|
232
242
|
route?: string;
|
|
233
243
|
globalGuards?: string[];
|
|
234
244
|
globalInterceptors?: string[];
|
|
235
245
|
middlewares?: string[];
|
|
236
246
|
}
|
|
237
|
-
declare function bindApp(app: Express | Router, {
|
|
247
|
+
declare function bindApp(app: Express | Router, { moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options?: Options): void;
|
|
238
248
|
|
|
239
249
|
declare function BaseParam(type: string, key: string, option?: any): any;
|
|
240
250
|
declare function Body(key?: string, pipeOpts?: any): any;
|
|
@@ -260,7 +270,4 @@ declare const defaultPipe: P.Pipe;
|
|
|
260
270
|
declare function isMerge(data: ServerCtx | ServerMergeCtx): data is ServerMergeCtx;
|
|
261
271
|
declare function resolveDep(ret: any, key: string): any;
|
|
262
272
|
|
|
263
|
-
|
|
264
|
-
declare const MERGE_SYMBOL = "__symbol_req__";
|
|
265
|
-
|
|
266
|
-
export { BadGatewayException, BadRequestException, Base, BaseError, BaseParam, Body, ConflictException, Construct, Context, Controller, Define, Delete, Emitter, Factory, ForbiddenException, FrameworkException, Get, Guard, Header, HttpException, Interceptor, InvalidInputException, MERGE_SYMBOL, MergeType, Meta, Middle, NotFoundException, Options, P, Param, Patch, PayloadLargeException, Post, Put, Query, RequestType, Route, SERIES_SYMBOL, ServerContext, ServerCtx, ServerFilter, ServerMergeCtx, ServiceUnavailableException, TimeoutException, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, ValidateException, addGuard, addInterceptor, addMiddleware, bindApp, constructorMap, defaultPipe, emitter, getInstance, isMerge, parseMeta, resolveDep, useServerFilter, useServerPipe };
|
|
273
|
+
export { BadGatewayException, BadRequestException, Base, BaseError, BaseParam, Body, ConflictException, Construct, Context, Controller, Define, Delete, Dev, Emitter, Factory, ForbiddenException, FrameworkException, Get, Guard, Header, HttpException, Interceptor, InvalidInputException, MERGE_SYMBOL, MergeType, Meta, Middle, NotFoundException, Options, P, Param, Patch, PayloadLargeException, Post, Put, Query, RequestType, Route, SERIES_SYMBOL, ServerContext, ServerCtx, ServerFilter, ServerMergeCtx, ServiceUnavailableException, TimeoutException, UNMOUNT_SYMBOL, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, ValidateException, addGuard, addInterceptor, addMiddleware, bindApp, defaultPipe, emitter, isMerge, parseMeta, resolveDep, useServerFilter, useServerPipe };
|
package/dist/index.js
CHANGED
|
@@ -43,6 +43,7 @@ __export(src_exports, {
|
|
|
43
43
|
Controller: () => Controller,
|
|
44
44
|
Define: () => Define,
|
|
45
45
|
Delete: () => Delete,
|
|
46
|
+
Dev: () => Dev,
|
|
46
47
|
Factory: () => Factory,
|
|
47
48
|
ForbiddenException: () => ForbiddenException,
|
|
48
49
|
FrameworkException: () => FrameworkException,
|
|
@@ -67,6 +68,7 @@ __export(src_exports, {
|
|
|
67
68
|
ServerContext: () => ServerContext,
|
|
68
69
|
ServiceUnavailableException: () => ServiceUnavailableException,
|
|
69
70
|
TimeoutException: () => TimeoutException,
|
|
71
|
+
UNMOUNT_SYMBOL: () => UNMOUNT_SYMBOL,
|
|
70
72
|
UnauthorizedException: () => UnauthorizedException,
|
|
71
73
|
UndefinedException: () => UndefinedException,
|
|
72
74
|
UnsupportedMediaTypeException: () => UnsupportedMediaTypeException,
|
|
@@ -75,10 +77,8 @@ __export(src_exports, {
|
|
|
75
77
|
addInterceptor: () => addInterceptor,
|
|
76
78
|
addMiddleware: () => addMiddleware,
|
|
77
79
|
bindApp: () => bindApp,
|
|
78
|
-
constructorMap: () => constructorMap,
|
|
79
80
|
defaultPipe: () => defaultPipe,
|
|
80
81
|
emitter: () => emitter,
|
|
81
|
-
getInstance: () => getInstance,
|
|
82
82
|
isMerge: () => isMerge,
|
|
83
83
|
parseMeta: () => parseMeta,
|
|
84
84
|
resolveDep: () => resolveDep,
|
|
@@ -270,7 +270,7 @@ var serverFilter = /* @__PURE__ */ __name((e) => {
|
|
|
270
270
|
}, "serverFilter");
|
|
271
271
|
|
|
272
272
|
// src/history.ts
|
|
273
|
-
var
|
|
273
|
+
var Histroy = class {
|
|
274
274
|
guard = [];
|
|
275
275
|
interceptor = [];
|
|
276
276
|
record(name, type) {
|
|
@@ -281,7 +281,7 @@ var Phistroy = class {
|
|
|
281
281
|
return false;
|
|
282
282
|
}
|
|
283
283
|
};
|
|
284
|
-
__name(
|
|
284
|
+
__name(Histroy, "Histroy");
|
|
285
285
|
|
|
286
286
|
// src/utils.ts
|
|
287
287
|
var import_picocolors = __toESM(require("picocolors"));
|
|
@@ -304,7 +304,7 @@ var _Context = class {
|
|
|
304
304
|
constructor(key, data) {
|
|
305
305
|
this.key = key;
|
|
306
306
|
this.data = data;
|
|
307
|
-
this.history = new
|
|
307
|
+
this.history = new Histroy();
|
|
308
308
|
}
|
|
309
309
|
static registerGuard(key, handler) {
|
|
310
310
|
_Context.guardsRecord[key] = handler;
|
|
@@ -347,7 +347,6 @@ var Context = _Context;
|
|
|
347
347
|
__name(Context, "Context");
|
|
348
348
|
__publicField(Context, "metaRecord", {});
|
|
349
349
|
__publicField(Context, "metaDataRecord", {});
|
|
350
|
-
__publicField(Context, "instanceRecord", {});
|
|
351
350
|
__publicField(Context, "guardsRecord", {});
|
|
352
351
|
__publicField(Context, "interceptorsRecord", {});
|
|
353
352
|
function addGuard(key, handler) {
|
|
@@ -358,10 +357,6 @@ function addInterceptor(key, handler) {
|
|
|
358
357
|
Context.registerInterceptor(key, handler);
|
|
359
358
|
}
|
|
360
359
|
__name(addInterceptor, "addInterceptor");
|
|
361
|
-
function getInstance(tag) {
|
|
362
|
-
return Context.instanceRecord[tag];
|
|
363
|
-
}
|
|
364
|
-
__name(getInstance, "getInstance");
|
|
365
360
|
function parseMeta(meta) {
|
|
366
361
|
const { data: { params, guards, interceptors, middlewares }, reflect, handlers } = meta;
|
|
367
362
|
params.forEach(({ index, key }, i) => {
|
|
@@ -437,17 +432,17 @@ __name(resolveDep, "resolveDep");
|
|
|
437
432
|
// src/common.ts
|
|
438
433
|
var SERIES_SYMBOL = "__symbol_series__";
|
|
439
434
|
var MERGE_SYMBOL = "__symbol_req__";
|
|
435
|
+
var UNMOUNT_SYMBOL = "__symbol_unmount__";
|
|
440
436
|
|
|
441
437
|
// src/server/express.ts
|
|
442
|
-
function bindApp(app, {
|
|
443
|
-
const { globalGuards, globalInterceptors, route, middlewares: proMiddle } = {
|
|
438
|
+
function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
439
|
+
const { dev = process.env.NODE_ENV !== "production", globalGuards, globalInterceptors, route, middlewares: proMiddle } = {
|
|
444
440
|
route: "/__PHECDA_SERVER__",
|
|
445
441
|
globalGuards: [],
|
|
446
442
|
globalInterceptors: [],
|
|
447
443
|
middlewares: [],
|
|
448
444
|
...options
|
|
449
445
|
};
|
|
450
|
-
const methodMap = {};
|
|
451
446
|
const contextMeta = {};
|
|
452
447
|
app.post(route, (req, _res, next) => {
|
|
453
448
|
req[MERGE_SYMBOL] = true;
|
|
@@ -489,7 +484,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
489
484
|
if (category === "series") {
|
|
490
485
|
for (const item of data) {
|
|
491
486
|
const { tag } = item;
|
|
492
|
-
const [name] = tag.split("-");
|
|
487
|
+
const [name, method] = tag.split("-");
|
|
493
488
|
const { reflect, params } = Context.metaDataRecord[tag];
|
|
494
489
|
const instance = moduleMap.get(name);
|
|
495
490
|
try {
|
|
@@ -518,7 +513,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
518
513
|
};
|
|
519
514
|
}), tag);
|
|
520
515
|
instance.context = contextData;
|
|
521
|
-
ret.push(await
|
|
516
|
+
ret.push(await moduleMap.get(name)[method](...args));
|
|
522
517
|
} catch (e) {
|
|
523
518
|
const m = Context.metaRecord[tag];
|
|
524
519
|
m.handlers.forEach((handler) => handler.error?.(e));
|
|
@@ -531,7 +526,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
531
526
|
return Promise.all(data.map((item) => {
|
|
532
527
|
return new Promise(async (resolve) => {
|
|
533
528
|
const { tag } = item;
|
|
534
|
-
const [name] = tag.split("-");
|
|
529
|
+
const [name, method] = tag.split("-");
|
|
535
530
|
const { reflect, params, handlers } = Context.metaDataRecord[tag];
|
|
536
531
|
const instance = moduleMap.get(name);
|
|
537
532
|
try {
|
|
@@ -549,7 +544,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
549
544
|
};
|
|
550
545
|
}), tag);
|
|
551
546
|
instance.context = contextData;
|
|
552
|
-
resolve(await
|
|
547
|
+
resolve(await moduleMap.get(name)[method](...args));
|
|
553
548
|
} catch (e) {
|
|
554
549
|
handlers.forEach((handler) => handler.error?.(e));
|
|
555
550
|
resolve(await context.useFilter(e));
|
|
@@ -564,62 +559,72 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
564
559
|
res.status(err.status).json(err);
|
|
565
560
|
}
|
|
566
561
|
});
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
});
|
|
562
|
+
async function createRoute() {
|
|
563
|
+
for (const i of meta) {
|
|
564
|
+
const { method, route: route2, header, tag } = i.data;
|
|
565
|
+
const methodTag = `${tag}-${method}`;
|
|
566
|
+
contextMeta[methodTag] = i;
|
|
567
|
+
Context.metaRecord[methodTag] = i;
|
|
568
|
+
let { guards, reflect, interceptors, params, handlers, middlewares } = Context.metaDataRecord[methodTag] ? Context.metaDataRecord[methodTag] : Context.metaDataRecord[methodTag] = parseMeta(i);
|
|
569
|
+
guards = [
|
|
570
|
+
...globalGuards,
|
|
571
|
+
...guards
|
|
572
|
+
];
|
|
573
|
+
interceptors = [
|
|
574
|
+
...globalInterceptors,
|
|
575
|
+
...interceptors
|
|
576
|
+
];
|
|
577
|
+
if (route2) {
|
|
578
|
+
app[route2.type](route2.route, ...ServerContext.useMiddleware(middlewares), async (req, res) => {
|
|
579
|
+
const instance = moduleMap.get(tag);
|
|
580
|
+
const contextData = {
|
|
581
|
+
request: req,
|
|
582
|
+
meta: i,
|
|
583
|
+
response: res,
|
|
584
|
+
moduleMap
|
|
585
|
+
};
|
|
586
|
+
const context = new ServerContext(methodTag, contextData);
|
|
587
|
+
try {
|
|
588
|
+
for (const name in header)
|
|
589
|
+
res.set(name, header[name]);
|
|
590
|
+
await context.useGuard(guards);
|
|
591
|
+
await context.useInterceptor(interceptors);
|
|
592
|
+
const args = await context.usePipe(params.map(({ type, key, option, index }) => {
|
|
593
|
+
return {
|
|
594
|
+
arg: resolveDep(req[type], key),
|
|
595
|
+
option,
|
|
596
|
+
key,
|
|
597
|
+
type,
|
|
598
|
+
index,
|
|
599
|
+
reflect: reflect[index]
|
|
600
|
+
};
|
|
601
|
+
}), methodTag);
|
|
602
|
+
instance.context = contextData;
|
|
603
|
+
const ret = await context.usePost(await instance[method](...args));
|
|
604
|
+
if (isObject(ret))
|
|
605
|
+
res.json(ret);
|
|
606
|
+
else
|
|
607
|
+
res.send(String(ret));
|
|
608
|
+
} catch (e) {
|
|
609
|
+
handlers.forEach((handler) => handler.error?.(e));
|
|
610
|
+
const err = await context.useFilter(e);
|
|
611
|
+
res.status(err.status).json(err);
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
}
|
|
621
615
|
}
|
|
622
616
|
}
|
|
617
|
+
__name(createRoute, "createRoute");
|
|
618
|
+
createRoute();
|
|
619
|
+
if (dev) {
|
|
620
|
+
const rawMetaHmr = globalThis.__PHECDA_SERVER_META__;
|
|
621
|
+
globalThis.__PHECDA_SERVER_META__ = () => {
|
|
622
|
+
app.stack = app.stack.slice(0, 1);
|
|
623
|
+
Context.metaDataRecord = {};
|
|
624
|
+
createRoute();
|
|
625
|
+
rawMetaHmr?.();
|
|
626
|
+
};
|
|
627
|
+
}
|
|
623
628
|
}
|
|
624
629
|
__name(bindApp, "bindApp");
|
|
625
630
|
|
|
@@ -628,6 +633,7 @@ var import_reflect_metadata = require("reflect-metadata");
|
|
|
628
633
|
var import_fs = __toESM(require("fs"));
|
|
629
634
|
var import_node_events = __toESM(require("events"));
|
|
630
635
|
var import_phecda_core2 = require("phecda-core");
|
|
636
|
+
var import_debug = __toESM(require("debug"));
|
|
631
637
|
|
|
632
638
|
// src/meta.ts
|
|
633
639
|
var Meta = class {
|
|
@@ -643,11 +649,14 @@ var Meta = class {
|
|
|
643
649
|
__name(Meta, "Meta");
|
|
644
650
|
|
|
645
651
|
// src/core.ts
|
|
652
|
+
var debug = (0, import_debug.default)("phecda-server");
|
|
646
653
|
var emitter = new import_node_events.default();
|
|
647
|
-
|
|
648
|
-
async function Factory(Modules) {
|
|
654
|
+
async function Factory(Modules, opts = {}) {
|
|
649
655
|
const moduleMap = /* @__PURE__ */ new Map();
|
|
650
656
|
const meta = [];
|
|
657
|
+
const constructorMap = /* @__PURE__ */ new Map();
|
|
658
|
+
const moduleGraph = /* @__PURE__ */ new Map();
|
|
659
|
+
const { dev = process.env.NODE_ENV !== "production", file = "pmeta.js" } = opts;
|
|
651
660
|
(0, import_phecda_core2.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
|
|
652
661
|
const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
|
|
653
662
|
if (options?.once)
|
|
@@ -655,45 +664,104 @@ async function Factory(Modules) {
|
|
|
655
664
|
else
|
|
656
665
|
emitter.on(eventName, fn);
|
|
657
666
|
});
|
|
667
|
+
async function update(Module) {
|
|
668
|
+
const tag = Module.prototype?.__TAG__ || Module.name;
|
|
669
|
+
if (!moduleMap.has(tag))
|
|
670
|
+
return;
|
|
671
|
+
debug(`update module "${tag}"`);
|
|
672
|
+
const instance = moduleMap.get(tag);
|
|
673
|
+
if (instance?.[UNMOUNT_SYMBOL]) {
|
|
674
|
+
for (const cb of instance[UNMOUNT_SYMBOL])
|
|
675
|
+
await cb();
|
|
676
|
+
}
|
|
677
|
+
moduleMap.delete(tag);
|
|
678
|
+
constructorMap.delete(tag);
|
|
679
|
+
for (let i = meta.length - 1; i >= 0; i--) {
|
|
680
|
+
if (meta[i].data.tag === tag)
|
|
681
|
+
meta.splice(i, 1);
|
|
682
|
+
}
|
|
683
|
+
const { instance: newModule } = await buildNestModule(Module);
|
|
684
|
+
if (moduleGraph.has(tag)) {
|
|
685
|
+
[
|
|
686
|
+
...moduleGraph.get(tag)
|
|
687
|
+
].forEach((tag2) => {
|
|
688
|
+
const module2 = moduleMap.get(tag2);
|
|
689
|
+
for (const key in module2) {
|
|
690
|
+
if (module2[key] === instance)
|
|
691
|
+
module2[key] = newModule;
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
moduleMap.set(tag, newModule);
|
|
696
|
+
}
|
|
697
|
+
__name(update, "update");
|
|
698
|
+
async function buildNestModule(Module) {
|
|
699
|
+
const paramtypes = getParamtypes(Module);
|
|
700
|
+
let instance;
|
|
701
|
+
const tag = Module.prototype?.__TAG__ || Module.name;
|
|
702
|
+
if (moduleMap.has(tag)) {
|
|
703
|
+
instance = moduleMap.get(tag);
|
|
704
|
+
if (!instance)
|
|
705
|
+
throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`);
|
|
706
|
+
if (constructorMap.get(tag) !== Module)
|
|
707
|
+
warn(`Synonym module: Module taged "${tag}" has been loaded before, so phecda-server won't load Module "${Module.name}"`);
|
|
708
|
+
return {
|
|
709
|
+
instance,
|
|
710
|
+
tag
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
moduleMap.set(tag, void 0);
|
|
714
|
+
if (paramtypes) {
|
|
715
|
+
const paramtypesInstances = [];
|
|
716
|
+
for (const i in paramtypes) {
|
|
717
|
+
const { instance: sub, tag: subTag } = await buildNestModule(paramtypes[i]);
|
|
718
|
+
paramtypesInstances[i] = sub;
|
|
719
|
+
if (!moduleGraph.has(subTag))
|
|
720
|
+
moduleGraph.set(subTag, /* @__PURE__ */ new Set());
|
|
721
|
+
moduleGraph.get(subTag).add(tag);
|
|
722
|
+
}
|
|
723
|
+
instance = new Module(...paramtypesInstances);
|
|
724
|
+
} else {
|
|
725
|
+
instance = new Module();
|
|
726
|
+
}
|
|
727
|
+
meta.push(...getMetaFromInstance(instance, tag, Module.name));
|
|
728
|
+
await (0, import_phecda_core2.registerAsync)(instance);
|
|
729
|
+
moduleMap.set(tag, instance);
|
|
730
|
+
constructorMap.set(tag, Module);
|
|
731
|
+
return {
|
|
732
|
+
instance,
|
|
733
|
+
tag
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
__name(buildNestModule, "buildNestModule");
|
|
658
737
|
for (const Module of Modules)
|
|
659
|
-
await buildNestModule(Module
|
|
660
|
-
|
|
738
|
+
await buildNestModule(Module);
|
|
739
|
+
function writeMeta() {
|
|
740
|
+
debug("write metadata");
|
|
741
|
+
import_fs.default.promises.writeFile(file, JSON.stringify(meta.map((item) => item.data)));
|
|
742
|
+
}
|
|
743
|
+
__name(writeMeta, "writeMeta");
|
|
744
|
+
writeMeta();
|
|
745
|
+
if (dev) {
|
|
746
|
+
globalThis.__PHECDA_SERVER_HMR__ = async (file2) => {
|
|
747
|
+
debug(`reload file ${file2}`);
|
|
748
|
+
const module2 = await import(file2);
|
|
749
|
+
for (const i in module2) {
|
|
750
|
+
if ((0, import_phecda_core2.isPhecda)(module2[i]))
|
|
751
|
+
await update(module2[i]);
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
globalThis.__PHECDA_SERVER_META__ = writeMeta;
|
|
755
|
+
}
|
|
661
756
|
return {
|
|
662
757
|
moduleMap,
|
|
663
758
|
meta,
|
|
664
|
-
|
|
759
|
+
constructorMap,
|
|
760
|
+
update
|
|
665
761
|
};
|
|
666
762
|
}
|
|
667
763
|
__name(Factory, "Factory");
|
|
668
|
-
|
|
669
|
-
const paramtypes = getParamtypes(Module);
|
|
670
|
-
let instance;
|
|
671
|
-
const tag = Module.prototype?.__TAG__ || Module.name;
|
|
672
|
-
if (map.has(tag)) {
|
|
673
|
-
instance = map.get(tag);
|
|
674
|
-
if (!instance)
|
|
675
|
-
throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`);
|
|
676
|
-
if (constructorMap.get(tag) !== Module)
|
|
677
|
-
warn(`Synonym module: Module taged "${tag}" has been loaded before, so phecda-server won't load Module "${Module.name}"`);
|
|
678
|
-
return instance;
|
|
679
|
-
}
|
|
680
|
-
map.set(tag, void 0);
|
|
681
|
-
if (paramtypes) {
|
|
682
|
-
const paramtypesInstances = [];
|
|
683
|
-
for (const i in paramtypes)
|
|
684
|
-
paramtypesInstances[i] = await buildNestModule(paramtypes[i], map, meta);
|
|
685
|
-
instance = new Module(...paramtypesInstances);
|
|
686
|
-
} else {
|
|
687
|
-
instance = new Module();
|
|
688
|
-
}
|
|
689
|
-
meta.push(...getMetaFromInstance(instance, Module.name, tag));
|
|
690
|
-
await (0, import_phecda_core2.registerAsync)(instance);
|
|
691
|
-
map.set(tag, instance);
|
|
692
|
-
constructorMap.set(tag, Module);
|
|
693
|
-
return instance;
|
|
694
|
-
}
|
|
695
|
-
__name(buildNestModule, "buildNestModule");
|
|
696
|
-
function getMetaFromInstance(instance, name, tag) {
|
|
764
|
+
function getMetaFromInstance(instance, tag, name) {
|
|
697
765
|
const vars = (0, import_phecda_core2.getExposeKey)(instance).filter((item) => item !== "__CLASS");
|
|
698
766
|
const baseState = (0, import_phecda_core2.getState)(instance, "__CLASS") || {};
|
|
699
767
|
initState(baseState);
|
|
@@ -764,6 +832,13 @@ function initState(state) {
|
|
|
764
832
|
state.interceptors = [];
|
|
765
833
|
}
|
|
766
834
|
__name(initState, "initState");
|
|
835
|
+
var Dev = class {
|
|
836
|
+
[UNMOUNT_SYMBOL] = [];
|
|
837
|
+
onUnmount(cb) {
|
|
838
|
+
this[UNMOUNT_SYMBOL].push(cb);
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
__name(Dev, "Dev");
|
|
767
842
|
|
|
768
843
|
// src/decorators/index.ts
|
|
769
844
|
var import_phecda_core5 = require("phecda-core");
|
|
@@ -925,6 +1000,7 @@ __reExport(src_exports, require("phecda-core"), module.exports);
|
|
|
925
1000
|
Controller,
|
|
926
1001
|
Define,
|
|
927
1002
|
Delete,
|
|
1003
|
+
Dev,
|
|
928
1004
|
Factory,
|
|
929
1005
|
ForbiddenException,
|
|
930
1006
|
FrameworkException,
|
|
@@ -949,6 +1025,7 @@ __reExport(src_exports, require("phecda-core"), module.exports);
|
|
|
949
1025
|
ServerContext,
|
|
950
1026
|
ServiceUnavailableException,
|
|
951
1027
|
TimeoutException,
|
|
1028
|
+
UNMOUNT_SYMBOL,
|
|
952
1029
|
UnauthorizedException,
|
|
953
1030
|
UndefinedException,
|
|
954
1031
|
UnsupportedMediaTypeException,
|
|
@@ -957,10 +1034,8 @@ __reExport(src_exports, require("phecda-core"), module.exports);
|
|
|
957
1034
|
addInterceptor,
|
|
958
1035
|
addMiddleware,
|
|
959
1036
|
bindApp,
|
|
960
|
-
constructorMap,
|
|
961
1037
|
defaultPipe,
|
|
962
1038
|
emitter,
|
|
963
|
-
getInstance,
|
|
964
1039
|
isMerge,
|
|
965
1040
|
parseMeta,
|
|
966
1041
|
resolveDep,
|