@eggjs/tegg-plugin 4.0.0-beta.3 → 4.0.0-beta.5

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 (99) hide show
  1. package/README.md +13 -1
  2. package/dist/app/extend/application.d.ts +57 -0
  3. package/dist/app/extend/application.js +72 -0
  4. package/dist/app/extend/application.unittest.d.ts +26 -0
  5. package/dist/app/extend/application.unittest.js +43 -0
  6. package/dist/app/extend/context.d.ts +24 -0
  7. package/dist/app/extend/context.js +30 -0
  8. package/dist/app/middleware/tegg_ctx_lifecycle_middleware.d.ts +6 -0
  9. package/dist/app/middleware/tegg_ctx_lifecycle_middleware.js +9 -0
  10. package/dist/app.d.ts +19 -0
  11. package/dist/app.js +57 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/index.js +5 -0
  14. package/dist/lib/AppLoadUnit.d.ts +23 -0
  15. package/dist/lib/AppLoadUnit.js +78 -0
  16. package/dist/lib/AppLoadUnitInstance.d.ts +24 -0
  17. package/dist/lib/AppLoadUnitInstance.js +69 -0
  18. package/dist/lib/CompatibleUtil.d.ts +18 -0
  19. package/dist/lib/CompatibleUtil.js +96 -0
  20. package/dist/lib/ConfigSourceLoadUnitHook.d.ts +15 -0
  21. package/dist/lib/ConfigSourceLoadUnitHook.js +22 -0
  22. package/dist/lib/EggAppLoader.d.ts +20 -0
  23. package/dist/lib/EggAppLoader.js +106 -0
  24. package/dist/lib/EggCompatibleObject.d.ts +21 -0
  25. package/dist/lib/EggCompatibleObject.js +37 -0
  26. package/dist/lib/EggCompatibleProtoImpl.d.ts +25 -0
  27. package/dist/lib/EggCompatibleProtoImpl.js +50 -0
  28. package/dist/lib/EggContextCompatibleHook.d.ts +14 -0
  29. package/dist/lib/EggContextCompatibleHook.js +32 -0
  30. package/dist/lib/EggContextHandler.d.ts +13 -0
  31. package/dist/lib/EggContextHandler.js +29 -0
  32. package/dist/lib/EggContextImpl.d.ts +10 -0
  33. package/dist/lib/EggContextImpl.js +18 -0
  34. package/dist/lib/EggModuleLoader.d.ts +15 -0
  35. package/dist/lib/EggModuleLoader.js +50 -0
  36. package/dist/lib/EggQualifierProtoHook.d.ts +14 -0
  37. package/dist/lib/EggQualifierProtoHook.js +48 -0
  38. package/dist/lib/ModuleConfigLoader.d.ts +12 -0
  39. package/dist/lib/ModuleConfigLoader.js +84 -0
  40. package/dist/lib/ModuleHandler.d.ts +17 -0
  41. package/dist/lib/ModuleHandler.js +46 -0
  42. package/dist/lib/Utils.d.ts +4 -0
  43. package/dist/lib/Utils.js +33 -0
  44. package/dist/lib/ctx_lifecycle_middleware.d.ts +6 -0
  45. package/dist/lib/ctx_lifecycle_middleware.js +27 -0
  46. package/dist/lib/run_in_background.d.ts +7 -0
  47. package/dist/lib/run_in_background.js +52 -0
  48. package/dist/types.d.ts +7 -0
  49. package/dist/types.js +6 -0
  50. package/package.json +63 -45
  51. package/app/extend/application.d.ts +0 -24
  52. package/app/extend/application.js +0 -76
  53. package/app/extend/application.unittest.d.ts +0 -8
  54. package/app/extend/application.unittest.js +0 -51
  55. package/app/extend/context.d.ts +0 -15
  56. package/app/extend/context.js +0 -32
  57. package/app/middleware/tegg_ctx_lifecycle_middleware.d.ts +0 -3
  58. package/app/middleware/tegg_ctx_lifecycle_middleware.js +0 -5
  59. package/app.d.ts +0 -17
  60. package/app.js +0 -63
  61. package/index.d.ts +0 -3
  62. package/index.js +0 -4
  63. package/lib/AppLoadUnit.d.ts +0 -19
  64. package/lib/AppLoadUnit.js +0 -85
  65. package/lib/AppLoadUnitInstance.d.ts +0 -20
  66. package/lib/AppLoadUnitInstance.js +0 -82
  67. package/lib/CompatibleUtil.d.ts +0 -14
  68. package/lib/CompatibleUtil.js +0 -94
  69. package/lib/ConfigSourceLoadUnitHook.d.ts +0 -10
  70. package/lib/ConfigSourceLoadUnitHook.js +0 -20
  71. package/lib/EggAppLoader.d.ts +0 -16
  72. package/lib/EggAppLoader.js +0 -127
  73. package/lib/EggCompatibleObject.d.ts +0 -18
  74. package/lib/EggCompatibleObject.js +0 -41
  75. package/lib/EggCompatibleProtoImpl.d.ts +0 -21
  76. package/lib/EggCompatibleProtoImpl.js +0 -51
  77. package/lib/EggContextCompatibleHook.d.ts +0 -10
  78. package/lib/EggContextCompatibleHook.js +0 -42
  79. package/lib/EggContextHandler.d.ts +0 -9
  80. package/lib/EggContextHandler.js +0 -25
  81. package/lib/EggContextImpl.d.ts +0 -6
  82. package/lib/EggContextImpl.js +0 -15
  83. package/lib/EggModuleLoader.d.ts +0 -11
  84. package/lib/EggModuleLoader.js +0 -53
  85. package/lib/EggQualifierProtoHook.d.ts +0 -10
  86. package/lib/EggQualifierProtoHook.js +0 -50
  87. package/lib/ModuleConfigLoader.d.ts +0 -8
  88. package/lib/ModuleConfigLoader.js +0 -80
  89. package/lib/ModuleHandler.d.ts +0 -13
  90. package/lib/ModuleHandler.js +0 -53
  91. package/lib/Utils.d.ts +0 -1
  92. package/lib/Utils.js +0 -36
  93. package/lib/ctx_lifecycle_middleware.d.ts +0 -2
  94. package/lib/ctx_lifecycle_middleware.js +0 -32
  95. package/lib/run_in_background.d.ts +0 -3
  96. package/lib/run_in_background.js +0 -55
  97. package/lib/types.d.ts +0 -43
  98. package/lib/types.js +0 -2
  99. package/typings/index.d.ts +0 -1
@@ -0,0 +1,96 @@
1
+ import { EggPrototypeFactory } from "@eggjs/tegg-metadata";
2
+ import { InitTypeQualifierAttribute, ObjectInitType } from "@eggjs/tegg";
3
+ import { ProxyUtil } from "@eggjs/tegg-common-util";
4
+ import { EggContainerFactory } from "@eggjs/tegg-runtime";
5
+ import { Application, Context } from "egg";
6
+
7
+ //#region src/lib/CompatibleUtil.ts
8
+ var CompatibleUtil = class CompatibleUtil {
9
+ static singletonProtoCache = /* @__PURE__ */ new Map();
10
+ static requestProtoCache = /* @__PURE__ */ new Map();
11
+ static getSingletonProto(name) {
12
+ if (!this.singletonProtoCache.has(name)) {
13
+ const proto = EggPrototypeFactory.instance.getPrototype(name, void 0, [{
14
+ attribute: InitTypeQualifierAttribute,
15
+ value: ObjectInitType.SINGLETON
16
+ }]);
17
+ this.singletonProtoCache.set(name, proto);
18
+ }
19
+ return this.singletonProtoCache.get(name);
20
+ }
21
+ static getRequestProto(name) {
22
+ if (!this.requestProtoCache.has(name)) {
23
+ const proto = EggPrototypeFactory.instance.getPrototype(name, void 0, [{
24
+ attribute: InitTypeQualifierAttribute,
25
+ value: ObjectInitType.CONTEXT
26
+ }]);
27
+ this.requestProtoCache.set(name, proto);
28
+ }
29
+ return this.requestProtoCache.get(name);
30
+ }
31
+ static singletonModuleProxyFactory(app, loadUnitInstance) {
32
+ let deprecated = false;
33
+ return function(_, p) {
34
+ const proto = CompatibleUtil.getSingletonProto(p);
35
+ const eggObj = EggContainerFactory.getEggObject(proto);
36
+ if (!deprecated) {
37
+ deprecated = true;
38
+ app.deprecate(`[egg/module] Please use await app.getEggObject(clazzName) instead of app.${loadUnitInstance.name}.${String(p)}`);
39
+ }
40
+ return eggObj.obj;
41
+ };
42
+ }
43
+ static appCompatible(app, loadUnitInstance) {
44
+ const moduleLoadUnitProxy = ProxyUtil.safeProxy(loadUnitInstance, CompatibleUtil.singletonModuleProxyFactory(app, loadUnitInstance));
45
+ Reflect.defineProperty(app.module, loadUnitInstance.name, {
46
+ configurable: true,
47
+ value: moduleLoadUnitProxy
48
+ });
49
+ }
50
+ static contextModuleProxyFactory(holder, ctx, loadUnitInstance) {
51
+ const cacheKey = `_${loadUnitInstance.name}Proxy`;
52
+ if (!holder[cacheKey]) {
53
+ let deprecated = false;
54
+ const getter = function(_, p) {
55
+ const proto = CompatibleUtil.getRequestProto(p);
56
+ const eggObj = EggContainerFactory.getEggObject(proto, p);
57
+ if (!deprecated) {
58
+ deprecated = true;
59
+ ctx.app.deprecate(`[egg/module] Please use await ctx.getEggObject(clazzName) instead of ctx.${loadUnitInstance.name}.${String(p)}`);
60
+ }
61
+ return eggObj.obj;
62
+ };
63
+ holder[cacheKey] = ProxyUtil.safeProxy(loadUnitInstance, getter);
64
+ }
65
+ return holder[cacheKey];
66
+ }
67
+ static contextModuleCompatible(ctx, loadUnitInstances) {
68
+ const loadUnitInstanceMap = loadUnitInstances.reduce((p, c) => {
69
+ p[c.name] = c;
70
+ return p;
71
+ }, {});
72
+ Reflect.defineProperty(ctx, "module", {
73
+ configurable: true,
74
+ enumerable: true,
75
+ get() {
76
+ const ctx$1 = this;
77
+ if (!this._moduleProxy) {
78
+ const ctxModule = Object.create(loadUnitInstanceMap);
79
+ this._moduleProxy = ProxyUtil.safeProxy(ctxModule, (_, p) => {
80
+ const loadUnitInstance = Reflect.get(ctxModule, p);
81
+ if (!loadUnitInstance) return;
82
+ return CompatibleUtil.contextModuleProxyFactory(ctxModule, ctx$1, loadUnitInstance);
83
+ });
84
+ }
85
+ return this._moduleProxy;
86
+ }
87
+ });
88
+ }
89
+ static clean() {
90
+ this.singletonProtoCache.clear();
91
+ this.requestProtoCache.clear();
92
+ }
93
+ };
94
+
95
+ //#endregion
96
+ export { CompatibleUtil };
@@ -0,0 +1,15 @@
1
+ import { LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-metadata";
2
+ import { LifecycleHook } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/ConfigSourceLoadUnitHook.d.ts
5
+
6
+ /**
7
+ * Copy from standalone/src/ConfigSourceLoadUnitHook
8
+ * Hook for inject moduleConfig.
9
+ * Add default qualifier value is current module name.
10
+ */
11
+ declare class ConfigSourceLoadUnitHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
12
+ preCreate(ctx: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void>;
13
+ }
14
+ //#endregion
15
+ export { ConfigSourceLoadUnitHook };
@@ -0,0 +1,22 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import { ConfigSourceQualifier, ConfigSourceQualifierAttribute, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/ConfigSourceLoadUnitHook.ts
5
+ /**
6
+ * Copy from standalone/src/ConfigSourceLoadUnitHook
7
+ * Hook for inject moduleConfig.
8
+ * Add default qualifier value is current module name.
9
+ */
10
+ var ConfigSourceLoadUnitHook = class {
11
+ async preCreate(ctx, loadUnit) {
12
+ const classList = await ctx.loader.load();
13
+ for (const clazz of classList) {
14
+ const moduleConfigObject = PrototypeUtil.getInjectObjects(clazz).find((t) => t.objName === "moduleConfig");
15
+ const configSourceQualifier = QualifierUtil.getProperQualifier(clazz, "moduleConfig", ConfigSourceQualifierAttribute);
16
+ if (moduleConfigObject && !configSourceQualifier) ConfigSourceQualifier(loadUnit.name)(clazz.prototype, moduleConfigObject.refName);
17
+ }
18
+ }
19
+ };
20
+
21
+ //#endregion
22
+ export { ConfigSourceLoadUnitHook };
@@ -0,0 +1,20 @@
1
+ import { Loader } from "@eggjs/tegg-metadata";
2
+ import { EggProtoImplClass } from "@eggjs/tegg";
3
+ import { Application } from "egg";
4
+
5
+ //#region src/lib/EggAppLoader.d.ts
6
+ declare const APP_CLAZZ_BLACK_LIST: string[];
7
+ declare const CONTEXT_CLAZZ_BLACK_LIST: string[];
8
+ declare const DEFAULT_APP_CLAZZ: string[];
9
+ declare const DEFAULT_CONTEXT_CLAZZ: string[];
10
+ declare class EggAppLoader implements Loader {
11
+ private readonly app;
12
+ private readonly moduleConfigLoader;
13
+ constructor(app: Application);
14
+ private buildClazz;
15
+ private buildAppLoggerClazz;
16
+ private getLoggerNames;
17
+ load(): Promise<EggProtoImplClass[]>;
18
+ }
19
+ //#endregion
20
+ export { APP_CLAZZ_BLACK_LIST, CONTEXT_CLAZZ_BLACK_LIST, DEFAULT_APP_CLAZZ, DEFAULT_CONTEXT_CLAZZ, EggAppLoader };
@@ -0,0 +1,106 @@
1
+ import { COMPATIBLE_PROTO_IMPLE_TYPE } from "./EggCompatibleProtoImpl.js";
2
+ import { ModuleConfigLoader } from "./ModuleConfigLoader.js";
3
+ import { TeggError } from "@eggjs/tegg-metadata";
4
+ import { AccessLevel, EggQualifierAttribute, EggType, InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute, ObjectInitType, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
5
+ import { ObjectUtils } from "@eggjs/tegg-common-util";
6
+ import { Application } from "egg";
7
+ import { BackgroundTaskHelper as BackgroundTaskHelper$1 } from "@eggjs/tegg-background-task";
8
+ import { EggObjectFactory } from "@eggjs/tegg-dynamic-inject-runtime";
9
+
10
+ //#region src/lib/EggAppLoader.ts
11
+ const APP_CLAZZ_BLACK_LIST = ["eggObjectFactory", "moduleConfigs"];
12
+ const CONTEXT_CLAZZ_BLACK_LIST = ["logger"];
13
+ const DEFAULT_APP_CLAZZ = [];
14
+ const DEFAULT_CONTEXT_CLAZZ = ["user"];
15
+ var EggAppLoader = class {
16
+ app;
17
+ moduleConfigLoader;
18
+ constructor(app) {
19
+ this.app = app;
20
+ this.moduleConfigLoader = new ModuleConfigLoader(this.app);
21
+ }
22
+ buildClazz(name, eggType) {
23
+ const app = this.app;
24
+ let func;
25
+ if (eggType === EggType.APP) func = function() {
26
+ return app[name];
27
+ };
28
+ else func = function() {
29
+ const ctx = app.currentContext;
30
+ if (!ctx) throw TeggError.create(`Can not read property \`${name}\` because egg ctx has been destroyed`, "read_after_ctx_destroyed");
31
+ return ctx[name];
32
+ };
33
+ Object.defineProperty(func, "name", {
34
+ value: name,
35
+ writable: false,
36
+ enumerable: false,
37
+ configurable: true
38
+ });
39
+ PrototypeUtil.setIsEggPrototype(func);
40
+ PrototypeUtil.setFilePath(func, "mock_file_path");
41
+ PrototypeUtil.setProperty(func, {
42
+ name,
43
+ initType: ObjectInitType.SINGLETON,
44
+ accessLevel: AccessLevel.PUBLIC,
45
+ protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE
46
+ });
47
+ QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, "app");
48
+ QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON);
49
+ QualifierUtil.addProtoQualifier(func, EggQualifierAttribute, eggType);
50
+ return func;
51
+ }
52
+ buildAppLoggerClazz(name) {
53
+ const app = this.app;
54
+ const func = function() {
55
+ return app.getLogger(name);
56
+ };
57
+ Object.defineProperty(func, "name", {
58
+ value: name,
59
+ writable: false,
60
+ enumerable: false,
61
+ configurable: true
62
+ });
63
+ PrototypeUtil.setIsEggPrototype(func);
64
+ PrototypeUtil.setFilePath(func, "mock_file_path");
65
+ PrototypeUtil.setProperty(func, {
66
+ name,
67
+ initType: ObjectInitType.SINGLETON,
68
+ accessLevel: AccessLevel.PUBLIC,
69
+ protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE
70
+ });
71
+ QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, "app");
72
+ QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON);
73
+ QualifierUtil.addProtoQualifier(func, EggQualifierAttribute, EggType.APP);
74
+ return func;
75
+ }
76
+ getLoggerNames(ctxClazzNames, singletonClazzNames) {
77
+ return Array.from(this.app.loggers.keys()).filter((t) => !ctxClazzNames.includes(t) && !singletonClazzNames.includes(t));
78
+ }
79
+ async load() {
80
+ const app = this.app;
81
+ const appProperties = ObjectUtils.getProperties(app);
82
+ const contextProperties = ObjectUtils.getProperties(app.context);
83
+ const allSingletonClazzNameSet = new Set([...appProperties, ...DEFAULT_APP_CLAZZ]);
84
+ APP_CLAZZ_BLACK_LIST.forEach((t) => allSingletonClazzNameSet.delete(t));
85
+ const allSingletonClazzNames = Array.from(allSingletonClazzNameSet);
86
+ const allContextClazzNamesSet = new Set([...contextProperties, ...DEFAULT_CONTEXT_CLAZZ]);
87
+ CONTEXT_CLAZZ_BLACK_LIST.forEach((t) => allContextClazzNamesSet.delete(t));
88
+ const allContextClazzNames = Array.from(allContextClazzNamesSet);
89
+ const loggerNames = this.getLoggerNames(allContextClazzNames, allSingletonClazzNames);
90
+ const allSingletonClazzs = allSingletonClazzNames.map((name) => this.buildClazz(name, EggType.APP));
91
+ const allContextClazzs = allContextClazzNames.map((name) => this.buildClazz(name, EggType.CONTEXT));
92
+ const appLoggerClazzs = loggerNames.map((name) => this.buildAppLoggerClazz(name));
93
+ const moduleConfigList = this.moduleConfigLoader.loadModuleConfigList();
94
+ return [
95
+ ...allSingletonClazzs,
96
+ ...allContextClazzs,
97
+ ...appLoggerClazzs,
98
+ ...moduleConfigList,
99
+ BackgroundTaskHelper$1,
100
+ EggObjectFactory
101
+ ];
102
+ }
103
+ };
104
+
105
+ //#endregion
106
+ export { APP_CLAZZ_BLACK_LIST, CONTEXT_CLAZZ_BLACK_LIST, DEFAULT_APP_CLAZZ, DEFAULT_CONTEXT_CLAZZ, EggAppLoader };
@@ -0,0 +1,21 @@
1
+ import { EggCompatibleProtoImpl } from "./EggCompatibleProtoImpl.js";
2
+ import { EggPrototype } from "@eggjs/tegg-metadata";
3
+ import { EggObjectName } from "@eggjs/tegg";
4
+ import { EggObject } from "@eggjs/tegg-runtime";
5
+
6
+ //#region src/lib/EggCompatibleObject.d.ts
7
+ declare const OBJ: unique symbol;
8
+ declare class EggCompatibleObject implements EggObject {
9
+ readonly isReady: boolean;
10
+ private [OBJ];
11
+ readonly proto: EggCompatibleProtoImpl;
12
+ readonly name: EggObjectName;
13
+ readonly id: string;
14
+ readonly isContext: boolean;
15
+ constructor(name: EggObjectName, proto: EggCompatibleProtoImpl);
16
+ get obj(): object;
17
+ injectProperty(): void;
18
+ static createObject(name: EggObjectName, proto: EggPrototype): Promise<EggCompatibleObject>;
19
+ }
20
+ //#endregion
21
+ export { EggCompatibleObject };
@@ -0,0 +1,37 @@
1
+ import { EggCompatibleProtoImpl } from "./EggCompatibleProtoImpl.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import { EggQualifierAttribute, EggType, IdenticalUtil } from "@eggjs/tegg";
4
+ import { EggObjectFactory } from "@eggjs/tegg-runtime";
5
+
6
+ //#region src/lib/EggCompatibleObject.ts
7
+ const OBJ = Symbol("EggCompatibleObject#obj");
8
+ var EggCompatibleObject = class EggCompatibleObject {
9
+ isReady = true;
10
+ [OBJ];
11
+ proto;
12
+ name;
13
+ id;
14
+ isContext;
15
+ constructor(name, proto) {
16
+ this.proto = proto;
17
+ this.name = name;
18
+ this.id = IdenticalUtil.createObjectId(this.proto.id);
19
+ this.isContext = this.proto.verifyQualifier({
20
+ value: EggType.CONTEXT,
21
+ attribute: EggQualifierAttribute
22
+ });
23
+ }
24
+ get obj() {
25
+ if (this.isContext) return this.proto.constructEggObject();
26
+ if (!this[OBJ]) this[OBJ] = this.proto.constructEggObject();
27
+ return this[OBJ];
28
+ }
29
+ injectProperty() {}
30
+ static async createObject(name, proto) {
31
+ return new EggCompatibleObject(name, proto);
32
+ }
33
+ };
34
+ EggObjectFactory.registerEggObjectCreateMethod(EggCompatibleProtoImpl, EggCompatibleObject.createObject);
35
+
36
+ //#endregion
37
+ export { EggCompatibleObject };
@@ -0,0 +1,25 @@
1
+ import { EggPrototype, EggPrototypeLifecycleContext, InjectObjectProto } from "@eggjs/tegg-metadata";
2
+ import { AccessLevel, EggProtoImplClass, EggPrototypeName, Id, MetaDataKey, ObjectInitTypeLike, QualifierInfo, QualifierValue } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/EggCompatibleProtoImpl.d.ts
5
+ declare const COMPATIBLE_PROTO_IMPLE_TYPE = "EGG_COMPATIBLE";
6
+ declare class EggCompatibleProtoImpl implements EggPrototype {
7
+ [key: symbol]: PropertyDescriptor;
8
+ private readonly clazz;
9
+ private readonly qualifiers;
10
+ readonly id: string;
11
+ readonly name: EggPrototypeName;
12
+ readonly initType: ObjectInitTypeLike;
13
+ readonly accessLevel: AccessLevel;
14
+ readonly injectObjects: InjectObjectProto[];
15
+ readonly loadUnitId: Id;
16
+ constructor(id: string, name: EggPrototypeName, clazz: EggProtoImplClass, initType: ObjectInitTypeLike, loadUnitId: Id, qualifiers: QualifierInfo[]);
17
+ verifyQualifiers(qualifiers: QualifierInfo[]): boolean;
18
+ verifyQualifier(qualifier: QualifierInfo): boolean;
19
+ getQualifier(attribute: string): QualifierValue | undefined;
20
+ constructEggObject(): object;
21
+ getMetaData<T>(metadataKey: MetaDataKey): T | undefined;
22
+ static create(ctx: EggPrototypeLifecycleContext): EggPrototype;
23
+ }
24
+ //#endregion
25
+ export { COMPATIBLE_PROTO_IMPLE_TYPE, EggCompatibleProtoImpl };
@@ -0,0 +1,50 @@
1
+ import "@eggjs/tegg-metadata";
2
+ import { AccessLevel, IdenticalUtil, MetadataUtil, QualifierUtil } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/EggCompatibleProtoImpl.ts
5
+ const COMPATIBLE_PROTO_IMPLE_TYPE = "EGG_COMPATIBLE";
6
+ var EggCompatibleProtoImpl = class EggCompatibleProtoImpl {
7
+ clazz;
8
+ qualifiers;
9
+ id;
10
+ name;
11
+ initType;
12
+ accessLevel;
13
+ injectObjects;
14
+ loadUnitId;
15
+ constructor(id, name, clazz, initType, loadUnitId, qualifiers) {
16
+ this.id = id;
17
+ this.clazz = clazz;
18
+ this.name = name;
19
+ this.initType = initType;
20
+ this.accessLevel = AccessLevel.PUBLIC;
21
+ this.injectObjects = [];
22
+ this.loadUnitId = loadUnitId;
23
+ this.qualifiers = qualifiers;
24
+ }
25
+ verifyQualifiers(qualifiers) {
26
+ for (const qualifier of qualifiers) if (!this.verifyQualifier(qualifier)) return false;
27
+ return true;
28
+ }
29
+ verifyQualifier(qualifier) {
30
+ return this.qualifiers.find((t) => t.attribute === qualifier.attribute)?.value === qualifier.value;
31
+ }
32
+ getQualifier(attribute) {
33
+ return this.qualifiers.find((t) => t.attribute === attribute)?.value;
34
+ }
35
+ constructEggObject() {
36
+ return Reflect.apply(this.clazz, null, []);
37
+ }
38
+ getMetaData(metadataKey) {
39
+ return MetadataUtil.getMetaData(metadataKey, this.clazz);
40
+ }
41
+ static create(ctx) {
42
+ const { clazz, loadUnit } = ctx;
43
+ const name = ctx.prototypeInfo.name;
44
+ const id = IdenticalUtil.createProtoId(loadUnit.id, name);
45
+ return new EggCompatibleProtoImpl(id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.mergeQualifiers(QualifierUtil.getProtoQualifiers(clazz), ctx.prototypeInfo.qualifiers ?? []));
46
+ }
47
+ };
48
+
49
+ //#endregion
50
+ export { COMPATIBLE_PROTO_IMPLE_TYPE, EggCompatibleProtoImpl };
@@ -0,0 +1,14 @@
1
+ import { ModuleHandler } from "./ModuleHandler.js";
2
+ import { LifecycleHook } from "@eggjs/tegg";
3
+ import { EggContext, EggContextLifecycleContext } from "@eggjs/tegg-runtime";
4
+
5
+ //#region src/lib/EggContextCompatibleHook.d.ts
6
+ declare class EggContextCompatibleHook implements LifecycleHook<EggContextLifecycleContext, EggContext> {
7
+ private readonly moduleHandler;
8
+ private requestProtoList;
9
+ constructor(moduleHandler: ModuleHandler);
10
+ preCreate(_: unknown, ctx: EggContext): Promise<void>;
11
+ postCreate(_: unknown, ctx: EggContext): Promise<void>;
12
+ }
13
+ //#endregion
14
+ export { EggContextCompatibleHook };
@@ -0,0 +1,32 @@
1
+ import "./ModuleHandler.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import { BackgroundTaskHelper, ObjectInitType, PrototypeUtil } from "@eggjs/tegg";
4
+ import { EggContainerFactory } from "@eggjs/tegg-runtime";
5
+ import { ROOT_PROTO } from "@eggjs/egg-module-common";
6
+
7
+ //#region src/lib/EggContextCompatibleHook.ts
8
+ var EggContextCompatibleHook = class {
9
+ moduleHandler;
10
+ requestProtoList = [];
11
+ constructor(moduleHandler) {
12
+ this.moduleHandler = moduleHandler;
13
+ for (const loadUnitInstance of this.moduleHandler.loadUnitInstances) {
14
+ const iterator = loadUnitInstance.loadUnit.iterateEggPrototype();
15
+ for (const proto of iterator) if (proto.initType === ObjectInitType.CONTEXT) this.requestProtoList.push(proto);
16
+ }
17
+ }
18
+ async preCreate(_, ctx) {
19
+ if (!ctx.get(ROOT_PROTO)) for (const proto of this.requestProtoList) ctx.addProtoToCreate(proto.name, proto);
20
+ else {
21
+ const protoObj = PrototypeUtil.getClazzProto(BackgroundTaskHelper);
22
+ await EggContainerFactory.getOrCreateEggObject(protoObj);
23
+ }
24
+ }
25
+ async postCreate(_, ctx) {
26
+ const rootProto = ctx.get(ROOT_PROTO);
27
+ if (rootProto) await EggContainerFactory.getOrCreateEggObject(rootProto);
28
+ }
29
+ };
30
+
31
+ //#endregion
32
+ export { EggContextCompatibleHook };
@@ -0,0 +1,13 @@
1
+ import { EggContext } from "@eggjs/tegg-runtime";
2
+ import { Application } from "egg";
3
+
4
+ //#region src/lib/EggContextHandler.d.ts
5
+ declare class EggContextHandler {
6
+ private readonly app;
7
+ constructor(app: Application);
8
+ getContextCallback(): EggContext | undefined;
9
+ run<R>(eggContext: EggContext, fn: () => Promise<R>): Promise<R>;
10
+ register(): void;
11
+ }
12
+ //#endregion
13
+ export { EggContextHandler };
@@ -0,0 +1,29 @@
1
+ import { ContextHandler } from "@eggjs/tegg-runtime";
2
+ import { EGG_CONTEXT } from "@eggjs/egg-module-common";
3
+ import "egg";
4
+
5
+ //#region src/lib/EggContextHandler.ts
6
+ var EggContextHandler = class {
7
+ app;
8
+ constructor(app) {
9
+ this.app = app;
10
+ }
11
+ getContextCallback() {
12
+ return this.app.currentContext?.teggContext;
13
+ }
14
+ async run(eggContext, fn) {
15
+ const ctx = eggContext.get(EGG_CONTEXT);
16
+ return await this.app.ctxStorage.run(ctx, fn);
17
+ }
18
+ register() {
19
+ ContextHandler.getContextCallback = () => {
20
+ return this.getContextCallback();
21
+ };
22
+ ContextHandler.runInContextCallback = async (context, fn) => {
23
+ return await this.run(context, fn);
24
+ };
25
+ }
26
+ };
27
+
28
+ //#endregion
29
+ export { EggContextHandler };
@@ -0,0 +1,10 @@
1
+ import { AbstractEggContext } from "@eggjs/tegg-runtime";
2
+ import { Context } from "egg";
3
+
4
+ //#region src/lib/EggContextImpl.d.ts
5
+ declare class EggContextImpl extends AbstractEggContext {
6
+ readonly id: string;
7
+ constructor(ctx: Context);
8
+ }
9
+ //#endregion
10
+ export { EggContextImpl };
@@ -0,0 +1,18 @@
1
+ import { IdenticalUtil } from "@eggjs/tegg";
2
+ import { AbstractEggContext } from "@eggjs/tegg-runtime";
3
+ import { EGG_CONTEXT, TEGG_CONTEXT } from "@eggjs/egg-module-common";
4
+
5
+ //#region src/lib/EggContextImpl.ts
6
+ var EggContextImpl = class extends AbstractEggContext {
7
+ id;
8
+ constructor(ctx) {
9
+ super();
10
+ this.set(EGG_CONTEXT, ctx);
11
+ ctx[TEGG_CONTEXT] = this;
12
+ const tracer = ctx.tracer;
13
+ this.id = IdenticalUtil.createContextId(tracer?.traceId);
14
+ }
15
+ };
16
+
17
+ //#endregion
18
+ export { EggContextImpl };
@@ -0,0 +1,15 @@
1
+ import { GlobalGraph } from "@eggjs/tegg-metadata";
2
+ import { Application } from "egg";
3
+
4
+ //#region src/lib/EggModuleLoader.d.ts
5
+ declare class EggModuleLoader {
6
+ app: Application;
7
+ globalGraph: GlobalGraph;
8
+ constructor(app: Application);
9
+ private loadApp;
10
+ private buildAppGraph;
11
+ private loadModule;
12
+ load(): Promise<void>;
13
+ }
14
+ //#endregion
15
+ export { EggModuleLoader };
@@ -0,0 +1,50 @@
1
+ import { EggAppLoader } from "./EggAppLoader.js";
2
+ import { EggLoadUnitType, GlobalGraph, LoadUnitFactory, ModuleDescriptorDumper } from "@eggjs/tegg-metadata";
3
+ import { Application } from "egg";
4
+ import { LoaderFactory } from "@eggjs/tegg-loader";
5
+
6
+ //#region src/lib/EggModuleLoader.ts
7
+ var EggModuleLoader = class {
8
+ app;
9
+ globalGraph;
10
+ constructor(app) {
11
+ this.app = app;
12
+ }
13
+ async loadApp() {
14
+ const loader = new EggAppLoader(this.app);
15
+ const loadUnit = await LoadUnitFactory.createLoadUnit(this.app.baseDir, EggLoadUnitType.APP, loader);
16
+ this.app.moduleHandler.loadUnits.push(loadUnit);
17
+ }
18
+ async buildAppGraph() {
19
+ for (const plugin of Object.values(this.app.plugins)) {
20
+ if (!plugin.enable) continue;
21
+ const modulePlugin = this.app.moduleReferences.find((t) => t.path === plugin.path);
22
+ if (modulePlugin) modulePlugin.optional = false;
23
+ }
24
+ const moduleDescriptors = await LoaderFactory.loadApp(this.app.moduleReferences);
25
+ for (const moduleDescriptor of moduleDescriptors) ModuleDescriptorDumper.dump(moduleDescriptor, { dumpDir: this.app.baseDir }).catch((e) => {
26
+ e.message = "dump module descriptor failed: " + e.message;
27
+ this.app.logger.warn(e);
28
+ });
29
+ return await GlobalGraph.create(moduleDescriptors);
30
+ }
31
+ async loadModule() {
32
+ this.globalGraph.build();
33
+ this.globalGraph.sort();
34
+ const moduleConfigList = this.globalGraph.moduleConfigList;
35
+ for (const moduleConfig of moduleConfigList) {
36
+ const modulePath = moduleConfig.path;
37
+ const loader = LoaderFactory.createLoader(modulePath, EggLoadUnitType.MODULE);
38
+ const loadUnit = await LoadUnitFactory.createLoadUnit(modulePath, EggLoadUnitType.MODULE, loader);
39
+ this.app.moduleHandler.loadUnits.push(loadUnit);
40
+ }
41
+ }
42
+ async load() {
43
+ GlobalGraph.instance = this.globalGraph = await this.buildAppGraph();
44
+ await this.loadApp();
45
+ await this.loadModule();
46
+ }
47
+ };
48
+
49
+ //#endregion
50
+ export { EggModuleLoader };
@@ -0,0 +1,14 @@
1
+ import { LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-metadata";
2
+ import { LifecycleHook } from "@eggjs/tegg";
3
+ import { Application } from "egg";
4
+
5
+ //#region src/lib/EggQualifierProtoHook.d.ts
6
+ declare class EggQualifierProtoHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
7
+ private readonly app;
8
+ constructor(app: Application);
9
+ preCreate(ctx: LoadUnitLifecycleContext): Promise<void>;
10
+ private isAppObject;
11
+ private isCtxObject;
12
+ }
13
+ //#endregion
14
+ export { EggQualifierProtoHook };
@@ -0,0 +1,48 @@
1
+ import { APP_CLAZZ_BLACK_LIST, CONTEXT_CLAZZ_BLACK_LIST, DEFAULT_APP_CLAZZ, DEFAULT_CONTEXT_CLAZZ } from "./EggAppLoader.js";
2
+ import { debuglog } from "node:util";
3
+ import "@eggjs/tegg-metadata";
4
+ import { EggQualifierAttribute, EggType, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
5
+ import { ObjectUtils } from "@eggjs/tegg-common-util";
6
+ import { Application } from "egg";
7
+
8
+ //#region src/lib/EggQualifierProtoHook.ts
9
+ const debug = debuglog("tegg/plugin/tegg/lib/EggQualifierProtoHook");
10
+ var EggQualifierProtoHook = class {
11
+ app;
12
+ constructor(app) {
13
+ this.app = app;
14
+ }
15
+ async preCreate(ctx) {
16
+ const clazzList = await ctx.loader.load();
17
+ const appProperties = ObjectUtils.getProperties(this.app);
18
+ const ctxProperties = ObjectUtils.getProperties(this.app.context);
19
+ if (debug.enabled) debug("preCreate, get clazzList:%o, appProperties:%o, ctxProperties:%o, from unitPath:%o", clazzList.map((t) => t.name), appProperties.length, ctxProperties.length, ctx.unitPath);
20
+ for (const clazz of clazzList) {
21
+ const inbjectObjects = PrototypeUtil.getInjectObjects(clazz) || [];
22
+ if (debug.enabled && inbjectObjects.length > 0) debug("preCreate, get injectObjects:%o, from clazz:%o, from unitPath:%o", inbjectObjects.map((t) => t.refName), clazz.name, ctx.unitPath);
23
+ for (const injectObject of inbjectObjects) {
24
+ if (QualifierUtil.getProperQualifiers(clazz, injectObject.refName).find((t) => t.attribute === EggQualifierAttribute)) continue;
25
+ if (this.isCtxObject(injectObject.objName, ctxProperties)) QualifierUtil.addProperQualifier(clazz, injectObject.refName, EggQualifierAttribute, EggType.CONTEXT);
26
+ else if (this.isAppObject(injectObject.objName, appProperties)) {
27
+ QualifierUtil.addProperQualifier(clazz, injectObject.refName, EggQualifierAttribute, EggType.APP);
28
+ debug("preCreate, add proper qualifier:%o to clazz:%o, from unitPath:%o", injectObject.refName, clazz.name, ctx.unitPath);
29
+ }
30
+ }
31
+ }
32
+ }
33
+ isAppObject(name, appProperties) {
34
+ name = String(name);
35
+ if (APP_CLAZZ_BLACK_LIST.includes(name)) return false;
36
+ if (DEFAULT_APP_CLAZZ.includes(name)) return true;
37
+ return appProperties.includes(name);
38
+ }
39
+ isCtxObject(name, ctxProperties) {
40
+ name = String(name);
41
+ if (CONTEXT_CLAZZ_BLACK_LIST.includes(name)) return false;
42
+ if (DEFAULT_CONTEXT_CLAZZ.includes(name)) return true;
43
+ return ctxProperties.includes(name);
44
+ }
45
+ };
46
+
47
+ //#endregion
48
+ export { EggQualifierProtoHook };
@@ -0,0 +1,12 @@
1
+ import { EggProtoImplClass } from "@eggjs/tegg";
2
+ import { Application } from "egg";
3
+
4
+ //#region src/lib/ModuleConfigLoader.d.ts
5
+ declare class ModuleConfigLoader {
6
+ readonly app: Application;
7
+ constructor(app: Application);
8
+ private loadModuleConfigs;
9
+ loadModuleConfigList(): EggProtoImplClass[];
10
+ }
11
+ //#endregion
12
+ export { ModuleConfigLoader };