@eggjs/tegg-controller-plugin 4.0.0-beta.7 → 4.0.0-beta.8

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.
Files changed (48) hide show
  1. package/dist/Acl-BF8dxD8f.js +29 -0
  2. package/dist/AppLoadUnitControllerHook-C3F2NtiE.js +27 -0
  3. package/dist/ControllerLoadUnit-DWgcQuC0.js +63 -0
  4. package/dist/ControllerLoadUnitHandler-BiPlOVSN.js +29 -0
  5. package/dist/ControllerMetadataManager-CC5nmTlv.js +23 -0
  6. package/dist/ControllerRegister-DDwsWz5F.d.ts +9 -0
  7. package/dist/ControllerRegister-DPqM08MF.js +3 -0
  8. package/dist/ControllerRegisterFactory-Bz7bulBg.js +23 -0
  9. package/dist/ControllerRegisterFactory-DCPVkM4E.d.ts +16 -0
  10. package/dist/EggControllerLoader-iohfFKpj.js +29 -0
  11. package/dist/EggControllerPrototypeHook-Djf4Mwvw.js +13 -0
  12. package/dist/HTTPControllerRegister-CZQJslUT.js +58 -0
  13. package/dist/HTTPMethodRegister-CHHFV-eT.js +138 -0
  14. package/dist/Req-D3i-7606.js +16 -0
  15. package/dist/RootProtoManager-CbLm-PD1.js +28 -0
  16. package/dist/RootProtoManager-DkqD47oc.d.ts +12 -0
  17. package/dist/app-CZQLJgoQ.d.ts +27 -0
  18. package/dist/app-Dq89t2rF.js +67 -0
  19. package/dist/app.d.ts +4 -26
  20. package/dist/app.js +15 -65
  21. package/dist/errors-CnXI09lf.js +16 -0
  22. package/dist/index.d.ts +5 -1
  23. package/dist/index.js +16 -1
  24. package/dist/lib/AppLoadUnitControllerHook.d.ts +3 -2
  25. package/dist/lib/AppLoadUnitControllerHook.js +5 -27
  26. package/dist/lib/ControllerLoadUnit.js +1 -61
  27. package/dist/lib/ControllerLoadUnitHandler.js +2 -27
  28. package/dist/lib/ControllerMetadataManager.js +1 -21
  29. package/dist/lib/ControllerRegister.d.ts +2 -8
  30. package/dist/lib/ControllerRegister.js +2 -2
  31. package/dist/lib/ControllerRegisterFactory.d.ts +3 -15
  32. package/dist/lib/ControllerRegisterFactory.js +3 -22
  33. package/dist/lib/EggControllerLoader.js +1 -27
  34. package/dist/lib/EggControllerPrototypeHook.js +1 -11
  35. package/dist/lib/RootProtoManager.d.ts +1 -11
  36. package/dist/lib/RootProtoManager.js +1 -26
  37. package/dist/lib/errors.js +1 -14
  38. package/dist/lib/impl/http/Acl.js +1 -27
  39. package/dist/lib/impl/http/HTTPControllerRegister.d.ts +2 -2
  40. package/dist/lib/impl/http/HTTPControllerRegister.js +7 -58
  41. package/dist/lib/impl/http/HTTPMethodRegister.d.ts +1 -1
  42. package/dist/lib/impl/http/HTTPMethodRegister.js +5 -137
  43. package/dist/lib/impl/http/Req.js +1 -14
  44. package/dist/types-D9vieyYE.d.ts +1 -0
  45. package/dist/types-DkvgFpN4.js +3 -0
  46. package/dist/types.d.ts +5 -1
  47. package/dist/types.js +16 -2
  48. package/package.json +11 -11
@@ -0,0 +1,29 @@
1
+ import "@eggjs/tegg";
2
+
3
+ //#region src/lib/impl/http/Acl.ts
4
+ function aclMiddlewareFactory(controllerMeta, methodMeta) {
5
+ if (!controllerMeta.hasMethodAcl(methodMeta)) return;
6
+ const code = controllerMeta.getMethodAcl(methodMeta);
7
+ return async function aclMiddleware(ctx, next) {
8
+ try {
9
+ await ctx.acl(code);
10
+ } catch (e) {
11
+ const { redirectUrl, status } = e.data || {};
12
+ if (!redirectUrl) throw e;
13
+ if (ctx.acceptJSON) {
14
+ ctx.body = {
15
+ target: redirectUrl,
16
+ stat: "deny"
17
+ };
18
+ return;
19
+ }
20
+ if (status) ctx.realStatus = status;
21
+ ctx.redirect(redirectUrl);
22
+ return;
23
+ }
24
+ return next();
25
+ };
26
+ }
27
+
28
+ //#endregion
29
+ export { aclMiddlewareFactory };
@@ -0,0 +1,27 @@
1
+ import { ControllerMetadataManager } from "./ControllerMetadataManager-CC5nmTlv.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import { CONTROLLER_META_DATA } from "@eggjs/tegg";
4
+
5
+ //#region src/lib/AppLoadUnitControllerHook.ts
6
+ var AppLoadUnitControllerHook = class {
7
+ controllerRegisterFactory;
8
+ rootProtoManager;
9
+ constructor(controllerRegisterFactory, rootProtoManager) {
10
+ this.controllerRegisterFactory = controllerRegisterFactory;
11
+ this.rootProtoManager = rootProtoManager;
12
+ }
13
+ async postCreate(_, obj) {
14
+ const iterator = obj.iterateEggPrototype();
15
+ for (const proto of iterator) {
16
+ const metadata = proto.getMetaData(CONTROLLER_META_DATA);
17
+ if (!metadata) continue;
18
+ const register = this.controllerRegisterFactory.getControllerRegister(proto, metadata);
19
+ if (!register) throw new Error(`not find controller implement for ${String(proto.name)} which type is ${metadata.type}`);
20
+ ControllerMetadataManager.instance.addController(metadata);
21
+ await register.register(this.rootProtoManager, obj);
22
+ }
23
+ }
24
+ };
25
+
26
+ //#endregion
27
+ export { AppLoadUnitControllerHook };
@@ -0,0 +1,63 @@
1
+ import { EggPrototypeFactory } from "@eggjs/tegg-metadata";
2
+ import { IdenticalUtil } from "@eggjs/tegg";
3
+ import { MapUtil } from "@eggjs/tegg-common-util";
4
+
5
+ //#region src/lib/ControllerLoadUnit.ts
6
+ const CONTROLLER_LOAD_UNIT = "app#controller";
7
+ var ControllerLoadUnit = class {
8
+ loader;
9
+ id;
10
+ name;
11
+ type = CONTROLLER_LOAD_UNIT;
12
+ unitPath;
13
+ eggPrototypeFactory;
14
+ eggPrototypeCreatorFactory;
15
+ protoMap = /* @__PURE__ */ new Map();
16
+ constructor(name, unitPath, loader, eggPrototypeFactory, eggPrototypeCreatorFactory) {
17
+ this.id = IdenticalUtil.createLoadUnitId(name);
18
+ this.name = name;
19
+ this.unitPath = unitPath;
20
+ this.loader = loader;
21
+ this.eggPrototypeFactory = eggPrototypeFactory;
22
+ this.eggPrototypeCreatorFactory = eggPrototypeCreatorFactory;
23
+ }
24
+ async init() {
25
+ const clazzList = await this.loader.load();
26
+ for (const clazz of clazzList) {
27
+ const protos = await this.eggPrototypeCreatorFactory.createProto(clazz, this);
28
+ for (const proto of protos) this.eggPrototypeFactory.registerPrototype(proto, this);
29
+ }
30
+ }
31
+ containPrototype(proto) {
32
+ return !!this.protoMap.get(proto.name)?.find((t) => t === proto);
33
+ }
34
+ getEggPrototype(name, qualifiers) {
35
+ return this.protoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
36
+ }
37
+ registerEggPrototype(proto) {
38
+ MapUtil.getOrStore(this.protoMap, proto.name, []).push(proto);
39
+ }
40
+ deletePrototype(proto) {
41
+ const protos = this.protoMap.get(proto.name);
42
+ if (protos) {
43
+ const index = protos.indexOf(proto);
44
+ if (index !== -1) protos.splice(index, 1);
45
+ }
46
+ }
47
+ async destroy() {
48
+ for (const namedProtos of this.protoMap.values()) {
49
+ const protos = namedProtos.slice();
50
+ for (const proto of protos) EggPrototypeFactory.instance.deletePrototype(proto, this);
51
+ }
52
+ this.protoMap.clear();
53
+ }
54
+ iterateEggPrototype() {
55
+ return Array.from(this.protoMap.values()).reduce((p, c) => {
56
+ p = p.concat(c);
57
+ return p;
58
+ }, []).values();
59
+ }
60
+ };
61
+
62
+ //#endregion
63
+ export { CONTROLLER_LOAD_UNIT, ControllerLoadUnit };
@@ -0,0 +1,29 @@
1
+ import { CONTROLLER_LOAD_UNIT } from "./ControllerLoadUnit-DWgcQuC0.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import "@eggjs/tegg-runtime";
4
+ import path from "node:path";
5
+ import { Base } from "sdk-base";
6
+
7
+ //#region src/lib/ControllerLoadUnitHandler.ts
8
+ var ControllerLoadUnitHandler = class extends Base {
9
+ app;
10
+ controllerLoadUnit;
11
+ controllerLoadUnitInstance;
12
+ constructor(app) {
13
+ super({ initMethod: "_init" });
14
+ this.app = app;
15
+ }
16
+ async _init() {
17
+ const controllerDir = path.join(this.app.config.baseDir, "app/controller");
18
+ const loader = this.app.loaderFactory.createLoader(controllerDir, CONTROLLER_LOAD_UNIT);
19
+ this.controllerLoadUnit = await this.app.loadUnitFactory.createLoadUnit(controllerDir, CONTROLLER_LOAD_UNIT, loader);
20
+ this.controllerLoadUnitInstance = await this.app.loadUnitInstanceFactory.createLoadUnitInstance(this.controllerLoadUnit);
21
+ }
22
+ async destroy() {
23
+ if (this.controllerLoadUnit) await this.app.loadUnitFactory.destroyLoadUnit(this.controllerLoadUnit);
24
+ if (this.controllerLoadUnitInstance) await this.app.loadUnitInstanceFactory.destroyLoadUnitInstance(this.controllerLoadUnitInstance);
25
+ }
26
+ };
27
+
28
+ //#endregion
29
+ export { ControllerLoadUnitHandler };
@@ -0,0 +1,23 @@
1
+ import "@eggjs/tegg";
2
+ import { MapUtil } from "@eggjs/tegg-common-util";
3
+
4
+ //#region src/lib/ControllerMetadataManager.ts
5
+ var ControllerMetadataManager = class ControllerMetadataManager {
6
+ controllers = /* @__PURE__ */ new Map();
7
+ static instance = new ControllerMetadataManager();
8
+ constructor() {
9
+ this.controllers = /* @__PURE__ */ new Map();
10
+ }
11
+ addController(metadata) {
12
+ const typeControllers = MapUtil.getOrStore(this.controllers, metadata.type, []);
13
+ if (typeControllers.filter((c) => c.controllerName === metadata.controllerName).length) throw new Error(`duplicate controller name ${metadata.controllerName}`);
14
+ if (typeControllers.filter((c) => c.protoName === metadata.protoName).length) throw new Error(`duplicate proto name ${String(metadata.protoName)}`);
15
+ typeControllers.push(metadata);
16
+ }
17
+ clear() {
18
+ this.controllers.clear();
19
+ }
20
+ };
21
+
22
+ //#endregion
23
+ export { ControllerMetadataManager };
@@ -0,0 +1,9 @@
1
+ import { RootProtoManager } from "./RootProtoManager-DkqD47oc.js";
2
+ import { LoadUnit } from "@eggjs/tegg-metadata";
3
+
4
+ //#region src/lib/ControllerRegister.d.ts
5
+ interface ControllerRegister {
6
+ register(rootProtoManager: RootProtoManager, loadUnit?: LoadUnit): Promise<void>;
7
+ }
8
+ //#endregion
9
+ export { ControllerRegister };
@@ -0,0 +1,3 @@
1
+ import "@eggjs/tegg-metadata";
2
+
3
+ export { };
@@ -0,0 +1,23 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import "@eggjs/tegg";
3
+
4
+ //#region src/lib/ControllerRegisterFactory.ts
5
+ var ControllerRegisterFactory = class {
6
+ app;
7
+ registerCreatorMap;
8
+ constructor(app) {
9
+ this.app = app;
10
+ this.registerCreatorMap = /* @__PURE__ */ new Map();
11
+ }
12
+ registerControllerRegister(type, creator) {
13
+ this.registerCreatorMap.set(type, creator);
14
+ }
15
+ getControllerRegister(proto, metadata) {
16
+ const creator = this.registerCreatorMap.get(metadata.type);
17
+ if (!creator) return;
18
+ return creator(proto, metadata, this.app);
19
+ }
20
+ };
21
+
22
+ //#endregion
23
+ export { ControllerRegisterFactory };
@@ -0,0 +1,16 @@
1
+ import { ControllerRegister } from "./ControllerRegister-DDwsWz5F.js";
2
+ import { EggPrototype } from "@eggjs/tegg-metadata";
3
+ import { ControllerMetadata, ControllerTypeLike } from "@eggjs/tegg";
4
+ import { Application } from "egg";
5
+
6
+ //#region src/lib/ControllerRegisterFactory.d.ts
7
+ type RegisterCreator = (proto: EggPrototype, controllerMeta: ControllerMetadata, app: Application) => ControllerRegister;
8
+ declare class ControllerRegisterFactory {
9
+ private readonly app;
10
+ private registerCreatorMap;
11
+ constructor(app: Application);
12
+ registerControllerRegister(type: ControllerTypeLike, creator: RegisterCreator): void;
13
+ getControllerRegister(proto: EggPrototype, metadata: ControllerMetadata): ControllerRegister | undefined;
14
+ }
15
+ //#endregion
16
+ export { ControllerRegisterFactory, RegisterCreator };
@@ -0,0 +1,29 @@
1
+ import path from "node:path";
2
+ import { globby } from "globby";
3
+ import { LoaderUtil } from "@eggjs/tegg-loader";
4
+
5
+ //#region src/lib/EggControllerLoader.ts
6
+ var EggControllerLoader = class {
7
+ controllerDir;
8
+ constructor(controllerDir) {
9
+ this.controllerDir = controllerDir;
10
+ }
11
+ async load() {
12
+ const filePattern = LoaderUtil.filePattern();
13
+ let files;
14
+ try {
15
+ files = (await globby(filePattern, { cwd: this.controllerDir })).map((file) => path.join(this.controllerDir, file));
16
+ } catch {
17
+ files = [];
18
+ }
19
+ const protoClassList = [];
20
+ for (const file of files) {
21
+ const fileClazzList = await LoaderUtil.loadFile(file);
22
+ for (const clazz of fileClazzList) protoClassList.push(clazz);
23
+ }
24
+ return protoClassList;
25
+ }
26
+ };
27
+
28
+ //#endregion
29
+ export { EggControllerLoader };
@@ -0,0 +1,13 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import { ControllerMetaBuilderFactory, ControllerMetadataUtil } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/EggControllerPrototypeHook.ts
5
+ var EggControllerPrototypeHook = class {
6
+ async postCreate(ctx) {
7
+ const metadata = ControllerMetaBuilderFactory.build(ctx.clazz);
8
+ if (metadata) ControllerMetadataUtil.setControllerMetadata(ctx.clazz, metadata);
9
+ }
10
+ };
11
+
12
+ //#endregion
13
+ export { EggControllerPrototypeHook };
@@ -0,0 +1,58 @@
1
+ import { HTTPMethodRegister } from "./HTTPMethodRegister-CHHFV-eT.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import { CONTROLLER_META_DATA, ControllerType, HTTPControllerMeta, HTTPMethodMeta } from "@eggjs/tegg";
4
+ import { EggContainerFactory } from "@eggjs/tegg-runtime";
5
+ import assert from "node:assert/strict";
6
+ import { Application, Router } from "egg";
7
+
8
+ //#region src/lib/impl/http/HTTPControllerRegister.ts
9
+ var HTTPControllerRegister = class HTTPControllerRegister {
10
+ static instance;
11
+ router;
12
+ checkRouters;
13
+ eggContainerFactory;
14
+ controllerProtos = [];
15
+ static create(proto, controllerMeta, app) {
16
+ assert(controllerMeta.type === ControllerType.HTTP, "controller meta type is not HTTP");
17
+ if (!HTTPControllerRegister.instance) HTTPControllerRegister.instance = new HTTPControllerRegister(app.router, app.eggContainerFactory);
18
+ HTTPControllerRegister.instance.controllerProtos.push(proto);
19
+ return HTTPControllerRegister.instance;
20
+ }
21
+ constructor(router, eggContainerFactory) {
22
+ this.router = router;
23
+ this.checkRouters = /* @__PURE__ */ new Map();
24
+ this.checkRouters.set("default", router);
25
+ this.eggContainerFactory = eggContainerFactory;
26
+ }
27
+ register() {
28
+ return Promise.resolve();
29
+ }
30
+ static clean() {
31
+ if (this.instance) {
32
+ this.instance.controllerProtos = [];
33
+ this.instance.checkRouters.clear();
34
+ }
35
+ this.instance = void 0;
36
+ }
37
+ doRegister(rootProtoManager) {
38
+ const methodMap = /* @__PURE__ */ new Map();
39
+ for (const proto of this.controllerProtos) {
40
+ const metadata = proto.getMetaData(CONTROLLER_META_DATA);
41
+ for (const method of metadata.methods) methodMap.set(method, proto);
42
+ }
43
+ const allMethods = Array.from(methodMap.keys()).sort((a, b) => b.priority - a.priority);
44
+ for (const method of allMethods) {
45
+ const controllerProto = methodMap.get(method);
46
+ const controllerMeta = controllerProto.getMetaData(CONTROLLER_META_DATA);
47
+ new HTTPMethodRegister(controllerProto, controllerMeta, method, this.router, this.checkRouters, this.eggContainerFactory).checkDuplicate();
48
+ }
49
+ for (const method of allMethods) {
50
+ const controllerProto = methodMap.get(method);
51
+ const controllerMeta = controllerProto.getMetaData(CONTROLLER_META_DATA);
52
+ new HTTPMethodRegister(controllerProto, controllerMeta, method, this.router, this.checkRouters, this.eggContainerFactory).register(rootProtoManager);
53
+ }
54
+ }
55
+ };
56
+
57
+ //#endregion
58
+ export { HTTPControllerRegister };
@@ -0,0 +1,138 @@
1
+ import { aclMiddlewareFactory } from "./Acl-BF8dxD8f.js";
2
+ import { HTTPRequest as HTTPRequest$1 } from "./Req-D3i-7606.js";
3
+ import { RouterConflictError } from "./errors-CnXI09lf.js";
4
+ import "@eggjs/tegg-metadata";
5
+ import { HTTPControllerMeta, HTTPCookies, HTTPMethodMeta, HTTPParamType } from "@eggjs/tegg";
6
+ import { EggContainerFactory } from "@eggjs/tegg-runtime";
7
+ import { Context, Router } from "egg";
8
+ import assert from "node:assert";
9
+ import pathToRegexp from "path-to-regexp";
10
+ import { EggRouter } from "@eggjs/router";
11
+ import { FrameworkErrorFormater } from "egg-errors";
12
+
13
+ //#region src/lib/impl/http/HTTPMethodRegister.ts
14
+ const noop = () => {};
15
+ var HTTPMethodRegister = class {
16
+ router;
17
+ checkRouters;
18
+ controllerMeta;
19
+ methodMeta;
20
+ proto;
21
+ eggContainerFactory;
22
+ constructor(proto, controllerMeta, methodMeta, router, checkRouters, eggContainerFactory) {
23
+ this.proto = proto;
24
+ this.controllerMeta = controllerMeta;
25
+ this.router = router;
26
+ this.methodMeta = methodMeta;
27
+ this.checkRouters = checkRouters;
28
+ this.eggContainerFactory = eggContainerFactory;
29
+ }
30
+ createHandler(methodMeta, host) {
31
+ const argsLength = methodMeta.paramMap.size;
32
+ const hasContext = methodMeta.contextParamIndex !== void 0;
33
+ const contextIndex = methodMeta.contextParamIndex;
34
+ const methodArgsLength = argsLength + (hasContext ? 1 : 0);
35
+ const self = this;
36
+ return async function(ctx, next) {
37
+ if (host && host !== ctx.host) return await next();
38
+ const realObj = (await self.eggContainerFactory.getOrCreateEggObject(self.proto, self.proto.name)).obj;
39
+ const realMethod = realObj[methodMeta.name];
40
+ const args = Array.from({ length: methodArgsLength });
41
+ if (hasContext) args[contextIndex] = ctx;
42
+ for (const [index, param] of methodMeta.paramMap) switch (param.type) {
43
+ case HTTPParamType.BODY:
44
+ args[index] = ctx.request.body;
45
+ break;
46
+ case HTTPParamType.PARAM: {
47
+ const pathParam = param;
48
+ args[index] = ctx.params[pathParam.name];
49
+ break;
50
+ }
51
+ case HTTPParamType.QUERY: {
52
+ const queryParam = param;
53
+ args[index] = ctx.query[queryParam.name];
54
+ break;
55
+ }
56
+ case HTTPParamType.QUERIES: {
57
+ const queryParam = param;
58
+ args[index] = ctx.queries[queryParam.name];
59
+ break;
60
+ }
61
+ case HTTPParamType.HEADERS:
62
+ args[index] = ctx.request.headers;
63
+ break;
64
+ case HTTPParamType.REQUEST:
65
+ args[index] = new HTTPRequest$1(ctx);
66
+ break;
67
+ case HTTPParamType.COOKIES:
68
+ args[index] = new HTTPCookies(ctx, []);
69
+ break;
70
+ default: assert.fail("never arrive");
71
+ }
72
+ const body = await Reflect.apply(realMethod, realObj, args);
73
+ const explicitStatus = ctx.response._explicitStatus;
74
+ if (body !== null && body !== void 0 || !explicitStatus) ctx.body = body;
75
+ };
76
+ }
77
+ checkDuplicate() {
78
+ this.checkDuplicateInRouter(this.router);
79
+ let hostRouter;
80
+ (this.controllerMeta.getMethodHosts(this.methodMeta) || []).forEach((h) => {
81
+ if (h) {
82
+ hostRouter = this.checkRouters.get(h);
83
+ if (!hostRouter) {
84
+ hostRouter = new EggRouter({ sensitive: true }, this.router.app);
85
+ this.checkRouters.set(h, hostRouter);
86
+ }
87
+ }
88
+ if (hostRouter) {
89
+ this.checkDuplicateInRouter(hostRouter);
90
+ this.registerToRouter(hostRouter);
91
+ }
92
+ });
93
+ }
94
+ registerToRouter(router) {
95
+ const routerFunc = router[this.methodMeta.method.toLowerCase()];
96
+ const methodRealPath = this.controllerMeta.getMethodRealPath(this.methodMeta);
97
+ const methodName = this.controllerMeta.getMethodName(this.methodMeta);
98
+ Reflect.apply(routerFunc, router, [
99
+ methodName,
100
+ methodRealPath,
101
+ noop
102
+ ]);
103
+ }
104
+ checkDuplicateInRouter(router) {
105
+ const methodRealPath = this.controllerMeta.getMethodRealPath(this.methodMeta);
106
+ const matched = router.match(methodRealPath, this.methodMeta.method);
107
+ const methodName = this.controllerMeta.getMethodName(this.methodMeta);
108
+ if (matched.route) {
109
+ const [layer] = matched.path;
110
+ const err = new RouterConflictError(`register http controller ${methodName} failed, ${this.methodMeta.method} ${methodRealPath} is conflict with exists rule ${layer.path}`);
111
+ throw FrameworkErrorFormater.format(err);
112
+ }
113
+ }
114
+ register(rootProtoManager) {
115
+ const methodRealPath = this.controllerMeta.getMethodRealPath(this.methodMeta);
116
+ const methodName = this.controllerMeta.getMethodName(this.methodMeta);
117
+ const routerFunc = this.router[this.methodMeta.method.toLowerCase()];
118
+ const methodMiddlewares = this.controllerMeta.getMethodMiddlewares(this.methodMeta);
119
+ const aclMiddleware = aclMiddlewareFactory(this.controllerMeta, this.methodMeta);
120
+ if (aclMiddleware) methodMiddlewares.push(aclMiddleware);
121
+ (this.controllerMeta.getMethodHosts(this.methodMeta) || [void 0]).forEach((h) => {
122
+ const handler = this.createHandler(this.methodMeta, h);
123
+ Reflect.apply(routerFunc, this.router, [
124
+ methodName,
125
+ methodRealPath,
126
+ ...methodMiddlewares,
127
+ handler
128
+ ]);
129
+ const regExp = pathToRegexp(methodRealPath, { sensitive: true });
130
+ rootProtoManager.registerRootProto(this.methodMeta.method, (ctx) => {
131
+ if (regExp.test(ctx.path)) return this.proto;
132
+ }, h || "");
133
+ });
134
+ }
135
+ };
136
+
137
+ //#endregion
138
+ export { HTTPMethodRegister };
@@ -0,0 +1,16 @@
1
+ import { HTTPRequest } from "@eggjs/tegg";
2
+
3
+ //#region src/lib/impl/http/Req.ts
4
+ var HTTPRequest$1 = class extends HTTPRequest {
5
+ constructor(ctx) {
6
+ const request = ctx.request;
7
+ super(request.href, {
8
+ method: request.method,
9
+ headers: request.headers,
10
+ body: ctx.request.rawBody
11
+ });
12
+ }
13
+ };
14
+
15
+ //#endregion
16
+ export { HTTPRequest$1 as HTTPRequest };
@@ -0,0 +1,28 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import "@eggjs/tegg";
3
+ import { MapUtil } from "@eggjs/tegg-common-util";
4
+
5
+ //#region src/lib/RootProtoManager.ts
6
+ var RootProtoManager = class {
7
+ protoMap = /* @__PURE__ */ new Map();
8
+ registerRootProto(method, cb, host) {
9
+ host = host || "";
10
+ MapUtil.getOrStore(this.protoMap, method + host, []).push(cb);
11
+ }
12
+ getRootProto(ctx) {
13
+ const hostCbList = this.protoMap.get(ctx.method + ctx.host);
14
+ if (hostCbList) for (const cb of hostCbList) {
15
+ const proto = cb(ctx);
16
+ if (proto) return proto;
17
+ }
18
+ const cbList = this.protoMap.get(ctx.method);
19
+ if (!cbList) return;
20
+ for (const cb of cbList) {
21
+ const proto = cb(ctx);
22
+ if (proto) return proto;
23
+ }
24
+ }
25
+ };
26
+
27
+ //#endregion
28
+ export { RootProtoManager };
@@ -0,0 +1,12 @@
1
+ import { EggPrototype } from "@eggjs/tegg-metadata";
2
+ import { EggContext } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/RootProtoManager.d.ts
5
+ type GetRootProtoCallback = (ctx: EggContext) => EggPrototype | undefined;
6
+ declare class RootProtoManager {
7
+ protoMap: Map<string, GetRootProtoCallback[]>;
8
+ registerRootProto(method: string, cb: GetRootProtoCallback, host: string): void;
9
+ getRootProto(ctx: EggContext): EggPrototype | undefined;
10
+ }
11
+ //#endregion
12
+ export { GetRootProtoCallback, RootProtoManager };
@@ -0,0 +1,27 @@
1
+ import { RootProtoManager } from "./RootProtoManager-DkqD47oc.js";
2
+ import { ControllerRegisterFactory } from "./ControllerRegisterFactory-DCPVkM4E.js";
3
+ import { ControllerMetaBuilderFactory } from "@eggjs/tegg";
4
+ import { Application, ILifecycleBoot } from "egg";
5
+
6
+ //#region src/app.d.ts
7
+ declare class ControllerAppBootHook implements ILifecycleBoot {
8
+ private readonly app;
9
+ private readonly loadUnitHook;
10
+ private readonly controllerRegisterFactory;
11
+ private controllerLoadUnitHandler;
12
+ private readonly controllerPrototypeHook;
13
+ constructor(app: Application);
14
+ configWillLoad(): void;
15
+ prepareMiddleware(middlewareNames: string[]): string[];
16
+ didLoad(): Promise<void>;
17
+ beforeClose(): Promise<void>;
18
+ }
19
+ declare module 'egg' {
20
+ interface Application {
21
+ rootProtoManager: RootProtoManager;
22
+ controllerRegisterFactory: ControllerRegisterFactory;
23
+ controllerMetaBuilderFactory: typeof ControllerMetaBuilderFactory;
24
+ }
25
+ }
26
+ //#endregion
27
+ export { ControllerAppBootHook };
@@ -0,0 +1,67 @@
1
+ import { CONTROLLER_LOAD_UNIT, ControllerLoadUnit } from "./ControllerLoadUnit-DWgcQuC0.js";
2
+ import { RootProtoManager } from "./RootProtoManager-CbLm-PD1.js";
3
+ import { ControllerRegisterFactory } from "./ControllerRegisterFactory-Bz7bulBg.js";
4
+ import { ControllerMetadataManager } from "./ControllerMetadataManager-CC5nmTlv.js";
5
+ import { AppLoadUnitControllerHook } from "./AppLoadUnitControllerHook-C3F2NtiE.js";
6
+ import { HTTPControllerRegister } from "./HTTPControllerRegister-CZQJslUT.js";
7
+ import { ControllerLoadUnitHandler } from "./ControllerLoadUnitHandler-BiPlOVSN.js";
8
+ import { EggControllerPrototypeHook } from "./EggControllerPrototypeHook-Djf4Mwvw.js";
9
+ import { EggControllerLoader } from "./EggControllerLoader-iohfFKpj.js";
10
+ import "@eggjs/tegg-metadata";
11
+ import { ControllerMetaBuilderFactory, ControllerType } from "@eggjs/tegg";
12
+ import { ModuleLoadUnitInstance } from "@eggjs/tegg-runtime";
13
+
14
+ //#region src/app.ts
15
+ var ControllerAppBootHook = class {
16
+ app;
17
+ loadUnitHook;
18
+ controllerRegisterFactory;
19
+ controllerLoadUnitHandler;
20
+ controllerPrototypeHook;
21
+ constructor(app) {
22
+ this.app = app;
23
+ this.controllerRegisterFactory = new ControllerRegisterFactory(this.app);
24
+ this.app.rootProtoManager = new RootProtoManager();
25
+ this.app.controllerRegisterFactory = this.controllerRegisterFactory;
26
+ this.app.controllerMetaBuilderFactory = ControllerMetaBuilderFactory;
27
+ this.loadUnitHook = new AppLoadUnitControllerHook(this.controllerRegisterFactory, this.app.rootProtoManager);
28
+ this.controllerPrototypeHook = new EggControllerPrototypeHook();
29
+ }
30
+ configWillLoad() {
31
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.loadUnitHook);
32
+ this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.controllerPrototypeHook);
33
+ this.app.loaderFactory.registerLoader(CONTROLLER_LOAD_UNIT, (unitPath) => {
34
+ return new EggControllerLoader(unitPath);
35
+ });
36
+ this.controllerRegisterFactory.registerControllerRegister(ControllerType.HTTP, HTTPControllerRegister.create);
37
+ this.app.loadUnitFactory.registerLoadUnitCreator(CONTROLLER_LOAD_UNIT, (ctx) => {
38
+ return new ControllerLoadUnit(`tegg-app-controller:${ctx.unitPath}`, ctx.unitPath, ctx.loader, this.app.eggPrototypeFactory, this.app.eggPrototypeCreatorFactory);
39
+ });
40
+ this.app.loadUnitInstanceFactory.registerLoadUnitInstanceClass(CONTROLLER_LOAD_UNIT, (ctx) => {
41
+ return new ModuleLoadUnitInstance(ctx.loadUnit);
42
+ });
43
+ this.prepareMiddleware(this.app.config.coreMiddleware);
44
+ }
45
+ prepareMiddleware(middlewareNames) {
46
+ if (!middlewareNames.includes("teggCtxLifecycleMiddleware")) middlewareNames.unshift("teggCtxLifecycleMiddleware");
47
+ const index = middlewareNames.indexOf("teggCtxLifecycleMiddleware");
48
+ middlewareNames.splice(index, 0, "teggRootProto");
49
+ return middlewareNames;
50
+ }
51
+ async didLoad() {
52
+ await this.app.moduleHandler.ready();
53
+ this.controllerLoadUnitHandler = new ControllerLoadUnitHandler(this.app);
54
+ await this.controllerLoadUnitHandler.ready();
55
+ HTTPControllerRegister.instance?.doRegister(this.app.rootProtoManager);
56
+ }
57
+ async beforeClose() {
58
+ if (this.controllerLoadUnitHandler) await this.controllerLoadUnitHandler.destroy();
59
+ this.app.loadUnitLifecycleUtil.deleteLifecycle(this.loadUnitHook);
60
+ this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.controllerPrototypeHook);
61
+ ControllerMetadataManager.instance.clear();
62
+ HTTPControllerRegister.clean();
63
+ }
64
+ };
65
+
66
+ //#endregion
67
+ export { ControllerAppBootHook };
package/dist/app.d.ts CHANGED
@@ -1,27 +1,5 @@
1
- import { RootProtoManager } from "./lib/RootProtoManager.js";
2
- import { ControllerRegisterFactory } from "./lib/ControllerRegisterFactory.js";
3
- import { ControllerMetaBuilderFactory } from "@eggjs/tegg";
4
- import { Application, ILifecycleBoot } from "egg";
5
-
6
- //#region src/app.d.ts
7
- declare class ControllerAppBootHook implements ILifecycleBoot {
8
- private readonly app;
9
- private readonly loadUnitHook;
10
- private readonly controllerRegisterFactory;
11
- private controllerLoadUnitHandler;
12
- private readonly controllerPrototypeHook;
13
- constructor(app: Application);
14
- configWillLoad(): void;
15
- prepareMiddleware(middlewareNames: string[]): string[];
16
- didLoad(): Promise<void>;
17
- beforeClose(): Promise<void>;
18
- }
19
- declare module 'egg' {
20
- interface Application {
21
- rootProtoManager: RootProtoManager;
22
- controllerRegisterFactory: ControllerRegisterFactory;
23
- controllerMetaBuilderFactory: typeof ControllerMetaBuilderFactory;
24
- }
25
- }
26
- //#endregion
1
+ import "./RootProtoManager-DkqD47oc.js";
2
+ import "./ControllerRegister-DDwsWz5F.js";
3
+ import "./ControllerRegisterFactory-DCPVkM4E.js";
4
+ import { ControllerAppBootHook } from "./app-CZQLJgoQ.js";
27
5
  export { ControllerAppBootHook as default };