@eggjs/tegg-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.
- package/dist/AppLoadUnit-BOWdS4O9.d.ts +23 -0
- package/dist/AppLoadUnit-uyHgElRL.js +78 -0
- package/dist/AppLoadUnitInstance-4Z_vSQV3.js +69 -0
- package/dist/AppLoadUnitInstance-DQ-DxlmR.d.ts +24 -0
- package/dist/CompatibleUtil-CNhydb0E.js +96 -0
- package/dist/ConfigSourceLoadUnitHook-BV-R31vA.js +22 -0
- package/dist/EggAppLoader-DFYg65ry.js +106 -0
- package/dist/EggCompatibleObject-XfIZyirS.js +37 -0
- package/dist/EggCompatibleObject-lzUCfsIY.d.ts +21 -0
- package/dist/EggCompatibleProtoImpl-CPxLvSu4.js +50 -0
- package/dist/EggCompatibleProtoImpl-DpacWKFB.d.ts +25 -0
- package/dist/EggContextCompatibleHook-eFfN2xm6.js +31 -0
- package/dist/EggContextHandler-D8gfTbZs.js +29 -0
- package/dist/EggContextHandler-vGi67He5.d.ts +13 -0
- package/dist/EggContextImpl-DkCLPTzT.js +18 -0
- package/dist/EggModuleLoader-CCfQMmp5.js +50 -0
- package/dist/EggQualifierProtoHook-D5jNm-1y.js +48 -0
- package/dist/ModuleConfigLoader-CvZLf99E.js +84 -0
- package/dist/ModuleHandler-Dyx7JGj-.d.ts +17 -0
- package/dist/ModuleHandler-fKcUqWca.js +46 -0
- package/dist/Utils-11aRpnkd.js +33 -0
- package/dist/app/extend/application.d.ts +3 -56
- package/dist/app/extend/application.js +8 -70
- package/dist/app/extend/application.unittest.d.ts +1 -25
- package/dist/app/extend/application.unittest.js +2 -41
- package/dist/app/extend/context.d.ts +1 -23
- package/dist/app/extend/context.js +3 -28
- package/dist/app/middleware/tegg_ctx_lifecycle_middleware.d.ts +1 -1
- package/dist/app/middleware/tegg_ctx_lifecycle_middleware.js +2 -1
- package/dist/app.d.ts +4 -1
- package/dist/app.js +18 -10
- package/dist/application-Ccljinuy.d.ts +57 -0
- package/dist/application-DFI1junS.js +70 -0
- package/dist/application.unittest-Cmia71Fx.js +43 -0
- package/dist/application.unittest-CpoCu6-E.d.ts +26 -0
- package/dist/context-BWYE1dif.js +30 -0
- package/dist/context-BoIt2G5h.d.ts +24 -0
- package/dist/ctx_lifecycle_middleware-CIeSrbVY.js +27 -0
- package/dist/ctx_lifecycle_middleware-DV7uN5PG.d.ts +6 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +13 -3
- package/dist/lib/AppLoadUnit.d.ts +1 -22
- package/dist/lib/AppLoadUnit.js +1 -76
- package/dist/lib/AppLoadUnitInstance.d.ts +1 -23
- package/dist/lib/AppLoadUnitInstance.js +1 -67
- package/dist/lib/CompatibleUtil.js +1 -94
- package/dist/lib/ConfigSourceLoadUnitHook.js +1 -20
- package/dist/lib/EggAppLoader.js +3 -104
- package/dist/lib/EggCompatibleObject.d.ts +2 -20
- package/dist/lib/EggCompatibleObject.js +2 -35
- package/dist/lib/EggCompatibleProtoImpl.d.ts +1 -24
- package/dist/lib/EggCompatibleProtoImpl.js +1 -48
- package/dist/lib/EggContextCompatibleHook.d.ts +1 -1
- package/dist/lib/EggContextCompatibleHook.js +7 -30
- package/dist/lib/EggContextHandler.d.ts +1 -12
- package/dist/lib/EggContextHandler.js +1 -27
- package/dist/lib/EggContextImpl.js +1 -16
- package/dist/lib/EggModuleLoader.js +4 -48
- package/dist/lib/EggQualifierProtoHook.js +4 -46
- package/dist/lib/ModuleConfigLoader.js +2 -82
- package/dist/lib/ModuleHandler.d.ts +1 -16
- package/dist/lib/ModuleHandler.js +6 -44
- package/dist/lib/Utils.js +1 -31
- package/dist/lib/ctx_lifecycle_middleware.d.ts +1 -5
- package/dist/lib/ctx_lifecycle_middleware.js +2 -25
- package/dist/lib/run_in_background.js +5 -50
- package/dist/run_in_background-CEUPBmDB.js +51 -0
- package/dist/types-BHg5p4Xk.js +3 -0
- package/dist/types-CuoKQV4X.d.ts +7 -0
- package/dist/types.d.ts +6 -7
- package/dist/types.js +13 -4
- package/package.json +11 -11
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { EggLoadUnitTypeLike, EggPrototype, LoadUnit, LoadUnitLifecycleContext, Loader } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { Id, QualifierInfo } from "@eggjs/tegg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/AppLoadUnit.d.ts
|
|
5
|
+
declare class AppLoadUnit implements LoadUnit {
|
|
6
|
+
private readonly loader;
|
|
7
|
+
id: Id;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly type: EggLoadUnitTypeLike;
|
|
10
|
+
readonly unitPath: string;
|
|
11
|
+
private protoMap;
|
|
12
|
+
constructor(name: string, unitPath: string, loader: Loader);
|
|
13
|
+
init(): Promise<void>;
|
|
14
|
+
containPrototype(proto: EggPrototype): boolean;
|
|
15
|
+
getEggPrototype(name: string, qualifiers: QualifierInfo[]): EggPrototype[];
|
|
16
|
+
registerEggPrototype(proto: EggPrototype): void;
|
|
17
|
+
deletePrototype(proto: EggPrototype): void;
|
|
18
|
+
destroy(): Promise<void>;
|
|
19
|
+
iterateEggPrototype(): IterableIterator<EggPrototype>;
|
|
20
|
+
static createModule(ctx: LoadUnitLifecycleContext): AppLoadUnit;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { AppLoadUnit };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
import { EggLoadUnitType, EggPrototypeCreatorFactory, EggPrototypeFactory, LoadUnitFactory } from "@eggjs/tegg-metadata";
|
|
3
|
+
import { IdenticalUtil, InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
|
|
4
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/AppLoadUnit.ts
|
|
7
|
+
const debug = debuglog("tegg/plugin/tegg/lib/AppLoadUnit");
|
|
8
|
+
var AppLoadUnit = class AppLoadUnit {
|
|
9
|
+
loader;
|
|
10
|
+
id;
|
|
11
|
+
name;
|
|
12
|
+
type = EggLoadUnitType.APP;
|
|
13
|
+
unitPath;
|
|
14
|
+
protoMap = /* @__PURE__ */ new Map();
|
|
15
|
+
constructor(name, unitPath, loader) {
|
|
16
|
+
this.id = IdenticalUtil.createLoadUnitId(name);
|
|
17
|
+
this.name = name;
|
|
18
|
+
this.unitPath = unitPath;
|
|
19
|
+
this.loader = loader;
|
|
20
|
+
}
|
|
21
|
+
async init() {
|
|
22
|
+
const clazzList = await this.loader.load();
|
|
23
|
+
if (debug.enabled) debug("init, get clazzList:%j, from unitPath:%o:%o:%o", clazzList.map((t) => t.name), this.type, this.name, this.unitPath);
|
|
24
|
+
for (const clazz of clazzList) {
|
|
25
|
+
const initTypeQualifierAttributeValue = await PrototypeUtil.getInitType(clazz, {
|
|
26
|
+
unitPath: this.unitPath,
|
|
27
|
+
moduleName: this.name
|
|
28
|
+
});
|
|
29
|
+
[{
|
|
30
|
+
attribute: InitTypeQualifierAttribute,
|
|
31
|
+
value: initTypeQualifierAttributeValue
|
|
32
|
+
}, {
|
|
33
|
+
attribute: LoadUnitNameQualifierAttribute,
|
|
34
|
+
value: this.name
|
|
35
|
+
}].forEach((qualifier) => {
|
|
36
|
+
QualifierUtil.addProtoQualifier(clazz, qualifier.attribute, qualifier.value);
|
|
37
|
+
});
|
|
38
|
+
const protos = await EggPrototypeCreatorFactory.createProto(clazz, this);
|
|
39
|
+
for (const proto of protos) EggPrototypeFactory.instance.registerPrototype(proto, this);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
containPrototype(proto) {
|
|
43
|
+
return !!this.protoMap.get(proto.name)?.find((t) => t === proto);
|
|
44
|
+
}
|
|
45
|
+
getEggPrototype(name, qualifiers) {
|
|
46
|
+
return this.protoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
|
|
47
|
+
}
|
|
48
|
+
registerEggPrototype(proto) {
|
|
49
|
+
MapUtil.getOrStore(this.protoMap, proto.name, []).push(proto);
|
|
50
|
+
}
|
|
51
|
+
deletePrototype(proto) {
|
|
52
|
+
const protos = this.protoMap.get(proto.name);
|
|
53
|
+
if (protos) {
|
|
54
|
+
const index = protos.indexOf(proto);
|
|
55
|
+
if (index !== -1) protos.splice(index, 1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async destroy() {
|
|
59
|
+
for (const namedProtos of this.protoMap.values()) {
|
|
60
|
+
const protos = namedProtos.slice();
|
|
61
|
+
for (const proto of protos) EggPrototypeFactory.instance.deletePrototype(proto, this);
|
|
62
|
+
}
|
|
63
|
+
this.protoMap.clear();
|
|
64
|
+
}
|
|
65
|
+
iterateEggPrototype() {
|
|
66
|
+
return Array.from(this.protoMap.values()).reduce((p, c) => {
|
|
67
|
+
p = p.concat(c);
|
|
68
|
+
return p;
|
|
69
|
+
}, []).values();
|
|
70
|
+
}
|
|
71
|
+
static createModule(ctx) {
|
|
72
|
+
return new AppLoadUnit("tegg-app", ctx.unitPath, ctx.loader);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
LoadUnitFactory.registerLoadUnitCreator(EggLoadUnitType.APP, AppLoadUnit.createModule);
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
export { AppLoadUnit };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { EggLoadUnitType } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { IdenticalUtil, ObjectInitType } from "@eggjs/tegg";
|
|
3
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
4
|
+
import { EggObjectFactory, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil } from "@eggjs/tegg-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/AppLoadUnitInstance.ts
|
|
7
|
+
var AppLoadUnitInstance = class AppLoadUnitInstance {
|
|
8
|
+
loadUnit;
|
|
9
|
+
id;
|
|
10
|
+
name;
|
|
11
|
+
protoToCreateMap = /* @__PURE__ */ new Map();
|
|
12
|
+
eggObjectMap = /* @__PURE__ */ new Map();
|
|
13
|
+
eggObjectPromiseMap = /* @__PURE__ */ new Map();
|
|
14
|
+
constructor(loadUnit) {
|
|
15
|
+
this.loadUnit = loadUnit;
|
|
16
|
+
this.name = loadUnit.name;
|
|
17
|
+
const iterator = this.loadUnit.iterateEggPrototype();
|
|
18
|
+
for (const proto of iterator) if (proto.initType === ObjectInitType.SINGLETON) this.protoToCreateMap.set(proto.name, proto);
|
|
19
|
+
this.id = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
20
|
+
}
|
|
21
|
+
iterateProtoToCreate() {
|
|
22
|
+
return this.protoToCreateMap.entries();
|
|
23
|
+
}
|
|
24
|
+
addProtoToCreate(name, proto) {
|
|
25
|
+
this.protoToCreateMap.set(name, proto);
|
|
26
|
+
}
|
|
27
|
+
deleteProtoToCreate(name) {
|
|
28
|
+
this.protoToCreateMap.delete(name);
|
|
29
|
+
}
|
|
30
|
+
async init(ctx) {
|
|
31
|
+
await LoadUnitInstanceLifecycleUtil.objectPreCreate(ctx, this);
|
|
32
|
+
for (const [name, proto] of this.protoToCreateMap) await this.getOrCreateEggObject(name, proto);
|
|
33
|
+
}
|
|
34
|
+
async destroy() {
|
|
35
|
+
const objs = [];
|
|
36
|
+
for (const protoObjMap of this.eggObjectMap.values()) for (const protoObj of protoObjMap.values()) objs.push(protoObj);
|
|
37
|
+
this.eggObjectMap.clear();
|
|
38
|
+
await Promise.all(objs.map(async (obj) => {
|
|
39
|
+
await EggObjectFactory.destroyObject(obj);
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
async getOrCreateEggObject(name, proto) {
|
|
43
|
+
if (!this.loadUnit.containPrototype(proto)) throw new Error("load unit not contain proto");
|
|
44
|
+
const protoObjMap = MapUtil.getOrStore(this.eggObjectMap, proto.id, /* @__PURE__ */ new Map());
|
|
45
|
+
if (!protoObjMap.has(name)) {
|
|
46
|
+
const protoObjPromiseMap = MapUtil.getOrStore(this.eggObjectPromiseMap, proto.id, /* @__PURE__ */ new Map());
|
|
47
|
+
if (!protoObjPromiseMap.has(name)) {
|
|
48
|
+
const objPromise = EggObjectFactory.createObject(name, proto);
|
|
49
|
+
protoObjPromiseMap.set(name, objPromise);
|
|
50
|
+
const obj = await objPromise;
|
|
51
|
+
protoObjPromiseMap.delete(name);
|
|
52
|
+
protoObjMap.set(name, obj);
|
|
53
|
+
} else await protoObjPromiseMap.get(name);
|
|
54
|
+
}
|
|
55
|
+
return protoObjMap.get(name);
|
|
56
|
+
}
|
|
57
|
+
getEggObject(name, proto) {
|
|
58
|
+
const protoObjMap = this.eggObjectMap.get(proto.id);
|
|
59
|
+
if (!protoObjMap || !protoObjMap.has(name)) throw new Error(`EggObject ${String(proto.name)} not found`);
|
|
60
|
+
return protoObjMap.get(name);
|
|
61
|
+
}
|
|
62
|
+
static createModuleLoadUnitInstance(ctx) {
|
|
63
|
+
return new AppLoadUnitInstance(ctx.loadUnit);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
LoadUnitInstanceFactory.registerLoadUnitInstanceClass(EggLoadUnitType.APP, AppLoadUnitInstance.createModuleLoadUnitInstance);
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { AppLoadUnitInstance };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { EggPrototype, LoadUnit } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { EggObjectName, EggPrototypeName } from "@eggjs/tegg";
|
|
3
|
+
import { EggObject, LoadUnitInstance, LoadUnitInstanceLifecycleContext } from "@eggjs/tegg-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/AppLoadUnitInstance.d.ts
|
|
6
|
+
declare class AppLoadUnitInstance implements LoadUnitInstance {
|
|
7
|
+
readonly loadUnit: LoadUnit;
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
private protoToCreateMap;
|
|
11
|
+
private eggObjectMap;
|
|
12
|
+
private eggObjectPromiseMap;
|
|
13
|
+
constructor(loadUnit: LoadUnit);
|
|
14
|
+
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
15
|
+
addProtoToCreate(name: string, proto: EggPrototype): void;
|
|
16
|
+
deleteProtoToCreate(name: string): void;
|
|
17
|
+
init(ctx: LoadUnitInstanceLifecycleContext): Promise<void>;
|
|
18
|
+
destroy(): Promise<void>;
|
|
19
|
+
getOrCreateEggObject(name: EggPrototypeName, proto: EggPrototype): Promise<EggObject>;
|
|
20
|
+
getEggObject(name: EggPrototypeName, proto: EggPrototype): EggObject;
|
|
21
|
+
static createModuleLoadUnitInstance(ctx: LoadUnitInstanceLifecycleContext): LoadUnitInstance;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { AppLoadUnitInstance };
|
|
@@ -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,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,106 @@
|
|
|
1
|
+
import { COMPATIBLE_PROTO_IMPLE_TYPE } from "./EggCompatibleProtoImpl-CPxLvSu4.js";
|
|
2
|
+
import { ModuleConfigLoader } from "./ModuleConfigLoader-CvZLf99E.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,37 @@
|
|
|
1
|
+
import { EggCompatibleProtoImpl } from "./EggCompatibleProtoImpl-CPxLvSu4.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,21 @@
|
|
|
1
|
+
import { EggCompatibleProtoImpl } from "./EggCompatibleProtoImpl-DpacWKFB.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,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,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,31 @@
|
|
|
1
|
+
import "@eggjs/tegg-metadata";
|
|
2
|
+
import { BackgroundTaskHelper, ObjectInitType, PrototypeUtil } from "@eggjs/tegg";
|
|
3
|
+
import { EggContainerFactory } from "@eggjs/tegg-runtime";
|
|
4
|
+
import { ROOT_PROTO } from "@eggjs/egg-module-common";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/EggContextCompatibleHook.ts
|
|
7
|
+
var EggContextCompatibleHook = class {
|
|
8
|
+
moduleHandler;
|
|
9
|
+
requestProtoList = [];
|
|
10
|
+
constructor(moduleHandler) {
|
|
11
|
+
this.moduleHandler = moduleHandler;
|
|
12
|
+
for (const loadUnitInstance of this.moduleHandler.loadUnitInstances) {
|
|
13
|
+
const iterator = loadUnitInstance.loadUnit.iterateEggPrototype();
|
|
14
|
+
for (const proto of iterator) if (proto.initType === ObjectInitType.CONTEXT) this.requestProtoList.push(proto);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async preCreate(_, ctx) {
|
|
18
|
+
if (!ctx.get(ROOT_PROTO)) for (const proto of this.requestProtoList) ctx.addProtoToCreate(proto.name, proto);
|
|
19
|
+
else {
|
|
20
|
+
const protoObj = PrototypeUtil.getClazzProto(BackgroundTaskHelper);
|
|
21
|
+
await EggContainerFactory.getOrCreateEggObject(protoObj);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async postCreate(_, ctx) {
|
|
25
|
+
const rootProto = ctx.get(ROOT_PROTO);
|
|
26
|
+
if (rootProto) await EggContainerFactory.getOrCreateEggObject(rootProto);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { EggContextCompatibleHook };
|
|
@@ -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,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,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 };
|