@eggjs/tegg-aop-runtime 4.0.0-beta.6 → 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/index.d.ts CHANGED
@@ -1,7 +1,56 @@
1
- import { AspectExecutor } from "./AspectExecutor.js";
2
- import { crossCutGraphHook } from "./CrossCutGraphHook.js";
3
- import { EggObjectAopHook } from "./EggObjectAopHook.js";
4
- import { EggPrototypeCrossCutHook } from "./EggPrototypeCrossCutHook.js";
5
- import { LoadUnitAopHook } from "./LoadUnitAopHook.js";
6
- import { pointCutGraphHook } from "./PointCutGraphHook.js";
1
+ import { CrosscutAdviceFactory } from "@eggjs/aop-decorator";
2
+ import { GlobalGraph } from "@eggjs/tegg-metadata";
3
+ import { AdviceContext, AspectAdvice, EggObject, EggObjectLifeCycleContext, EggPrototype, EggPrototypeLifecycleContext, IAdvice, LifecycleHook, LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-types";
4
+
5
+ //#region src/AspectExecutor.d.ts
6
+ declare class InternalAdviceContext<T = Record<string, IAdvice>> {
7
+ private readonly state;
8
+ that: T;
9
+ method: PropertyKey;
10
+ args: any[];
11
+ constructor(that: T, method: PropertyKey, args: any[]);
12
+ get(key: PropertyKey): any;
13
+ set(key: PropertyKey, value: any): this;
14
+ createCallContext(adviceParams?: any): AdviceContext<T>;
15
+ }
16
+ declare class AspectExecutor {
17
+ obj: object;
18
+ method: PropertyKey;
19
+ aspectAdviceList: readonly AspectAdvice[];
20
+ constructor(obj: object, method: PropertyKey, aspectAdviceList: readonly AspectAdvice[]);
21
+ execute(...args: any[]): Promise<void>;
22
+ beforeCall(ctx: InternalAdviceContext): Promise<void>;
23
+ afterReturn(ctx: InternalAdviceContext, result: any): Promise<void>;
24
+ afterThrow(ctx: InternalAdviceContext, error: Error): Promise<void>;
25
+ afterFinally(ctx: InternalAdviceContext): Promise<void>;
26
+ doExecute(ctx: InternalAdviceContext): Promise<void>;
27
+ }
28
+ //#endregion
29
+ //#region src/CrossCutGraphHook.d.ts
30
+ declare function crossCutGraphHook(globalGraph: GlobalGraph): void;
31
+ //#endregion
32
+ //#region src/EggObjectAopHook.d.ts
33
+ declare class EggObjectAopHook implements LifecycleHook<EggObjectLifeCycleContext, EggObject> {
34
+ private hijackMethods;
35
+ private injectAdvice;
36
+ postCreate(_: EggObjectLifeCycleContext, eggObject: EggObject): Promise<void>;
37
+ }
38
+ //#endregion
39
+ //#region src/EggPrototypeCrossCutHook.d.ts
40
+ declare class EggPrototypeCrossCutHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
41
+ private readonly crosscutAdviceFactory;
42
+ constructor(crosscutAdviceFactory: CrosscutAdviceFactory);
43
+ preCreate(ctx: EggPrototypeLifecycleContext): Promise<void>;
44
+ }
45
+ //#endregion
46
+ //#region src/LoadUnitAopHook.d.ts
47
+ declare class LoadUnitAopHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
48
+ private readonly crosscutAdviceFactory;
49
+ constructor(crosscutAdviceFactory: CrosscutAdviceFactory);
50
+ postCreate(_: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void>;
51
+ }
52
+ //#endregion
53
+ //#region src/PointCutGraphHook.d.ts
54
+ declare function pointCutGraphHook(globalGraph: GlobalGraph): void;
55
+ //#endregion
7
56
  export { AspectExecutor, EggObjectAopHook, EggPrototypeCrossCutHook, LoadUnitAopHook, crossCutGraphHook, pointCutGraphHook };
package/dist/index.js CHANGED
@@ -1,8 +1,268 @@
1
- import { AspectExecutor } from "./AspectExecutor.js";
2
- import { crossCutGraphHook } from "./CrossCutGraphHook.js";
3
- import { EggObjectAopHook } from "./EggObjectAopHook.js";
4
- import { EggPrototypeCrossCutHook } from "./EggPrototypeCrossCutHook.js";
5
- import { LoadUnitAopHook } from "./LoadUnitAopHook.js";
6
- import { pointCutGraphHook } from "./PointCutGraphHook.js";
1
+ import compose from "koa-compose";
2
+ import { AspectInfoUtil, AspectMetaBuilder, CrosscutAdviceFactory, CrosscutInfoUtil, PointcutAdviceInfoUtil } from "@eggjs/aop-decorator";
3
+ import "@eggjs/tegg-common-util";
4
+ import { ClassProtoDescriptor, GlobalGraph, TeggError } from "@eggjs/tegg-metadata";
5
+ import assert from "node:assert";
6
+ import { ASPECT_LIST, InjectType } from "@eggjs/tegg-types";
7
+ import { PrototypeUtil, QualifierUtil } from "@eggjs/core-decorator";
8
+ import { EggContainerFactory } from "@eggjs/tegg-runtime";
7
9
 
10
+ //#region src/AspectExecutor.ts
11
+ var InternalAdviceContext = class {
12
+ state;
13
+ that;
14
+ method;
15
+ args;
16
+ constructor(that, method, args) {
17
+ this.state = /* @__PURE__ */ new Map();
18
+ this.that = that;
19
+ this.method = method;
20
+ this.args = args;
21
+ }
22
+ get(key) {
23
+ return this.state.get(key);
24
+ }
25
+ set(key, value) {
26
+ this.state.set(key, value);
27
+ return this;
28
+ }
29
+ createCallContext(adviceParams) {
30
+ return Object.create(this, { adviceParams: { value: adviceParams } });
31
+ }
32
+ };
33
+ var AspectExecutor = class {
34
+ obj;
35
+ method;
36
+ aspectAdviceList;
37
+ constructor(obj, method, aspectAdviceList) {
38
+ this.obj = obj;
39
+ this.method = method;
40
+ this.aspectAdviceList = aspectAdviceList;
41
+ }
42
+ async execute(...args) {
43
+ const ctx = new InternalAdviceContext(this.obj, this.method, args);
44
+ await this.beforeCall(ctx);
45
+ try {
46
+ const result = await this.doExecute(ctx);
47
+ await this.afterReturn(ctx, result);
48
+ return result;
49
+ } catch (e) {
50
+ await this.afterThrow(ctx, e);
51
+ throw e;
52
+ } finally {
53
+ await this.afterFinally(ctx);
54
+ }
55
+ }
56
+ async beforeCall(ctx) {
57
+ for (const aspectAdvice of this.aspectAdviceList) {
58
+ const advice = ctx.that[aspectAdvice.name];
59
+ if (advice.beforeCall) {
60
+ /**
61
+ * 这里...写法使传入的参数变成了一个新的对象
62
+ * 因此beforeCall里面如果修改了ctx.args
63
+ * 最新的args是不会在方法里生效的
64
+ * 先保证args可以生效
65
+ * 不改动其余地方
66
+ */
67
+ const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
68
+ await advice.beforeCall(fnCtx);
69
+ ctx.args = fnCtx.args;
70
+ }
71
+ }
72
+ }
73
+ async afterReturn(ctx, result) {
74
+ for (const aspectAdvice of this.aspectAdviceList) {
75
+ const advice = ctx.that[aspectAdvice.name];
76
+ if (advice.afterReturn) {
77
+ const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
78
+ await advice.afterReturn(fnCtx, result);
79
+ }
80
+ }
81
+ }
82
+ async afterThrow(ctx, error) {
83
+ for (const aspectAdvice of this.aspectAdviceList) {
84
+ const advice = ctx.that[aspectAdvice.name];
85
+ if (advice.afterThrow) {
86
+ const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
87
+ await advice.afterThrow(fnCtx, error);
88
+ }
89
+ }
90
+ }
91
+ async afterFinally(ctx) {
92
+ for (const aspectAdvice of this.aspectAdviceList) {
93
+ const advice = ctx.that[aspectAdvice.name];
94
+ if (advice.afterFinally) {
95
+ const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
96
+ await advice.afterFinally(fnCtx);
97
+ }
98
+ }
99
+ }
100
+ async doExecute(ctx) {
101
+ const lastCall = () => {
102
+ const originMethod = Object.getPrototypeOf(this.obj)[this.method];
103
+ return Reflect.apply(originMethod, ctx.that, ctx.args);
104
+ };
105
+ const functions = [];
106
+ for (const aspectAdvice of this.aspectAdviceList) {
107
+ const advice = ctx.that[aspectAdvice.name];
108
+ const fn = advice.around;
109
+ if (fn) functions.push(async (ctx$1, next) => {
110
+ const fnCtx = ctx$1.createCallContext(aspectAdvice.adviceParams);
111
+ return await fn.call(advice, fnCtx, next);
112
+ });
113
+ }
114
+ functions.push(lastCall);
115
+ return compose(functions)(ctx);
116
+ }
117
+ };
118
+
119
+ //#endregion
120
+ //#region src/CrossCutGraphHook.ts
121
+ function crossCutGraphHook(globalGraph) {
122
+ for (const moduleNode of globalGraph.moduleGraph.nodes.values()) for (const crossCutProtoNode of moduleNode.val.protos) {
123
+ const protoNodes = findCrossCuttedClazz(globalGraph, crossCutProtoNode);
124
+ if (!protoNodes) continue;
125
+ for (const crossCuttedProtoNode of protoNodes) {
126
+ const crossCuttedModuleNode = globalGraph.findModuleNode(crossCuttedProtoNode.val.proto.instanceModuleName);
127
+ if (!crossCuttedModuleNode) continue;
128
+ globalGraph.addInject(crossCuttedModuleNode, crossCuttedProtoNode, crossCutProtoNode, crossCutProtoNode.val.proto.name);
129
+ }
130
+ }
131
+ }
132
+ function findCrossCuttedClazz(globalGraph, protoNode) {
133
+ const proto = protoNode.val.proto;
134
+ if (!ClassProtoDescriptor.isClassProtoDescriptor(proto)) return;
135
+ if (!CrosscutInfoUtil.isCrosscutAdvice(proto.clazz)) return;
136
+ const crosscutInfoList = CrosscutInfoUtil.getCrosscutInfoList(proto.clazz);
137
+ const result = [];
138
+ crosscut: for (const crosscutInfo of crosscutInfoList) for (const protoNode$1 of globalGraph.protoGraph.nodes.values()) if (checkClazzMatchCrossCut(protoNode$1, crosscutInfo)) {
139
+ result.push(protoNode$1);
140
+ break crosscut;
141
+ }
142
+ return result;
143
+ }
144
+ function checkClazzMatchCrossCut(protoNode, crosscutInfo) {
145
+ const proto = protoNode.val.proto;
146
+ if (!ClassProtoDescriptor.isClassProtoDescriptor(proto)) return;
147
+ const allMethods = AspectMetaBuilder.getAllMethods(proto.clazz);
148
+ for (const method of allMethods) if (crosscutInfo.pointcutInfo.match(proto.clazz, method)) return true;
149
+ return false;
150
+ }
151
+
152
+ //#endregion
153
+ //#region src/EggObjectAopHook.ts
154
+ var EggObjectAopHook = class {
155
+ hijackMethods(obj, aspectList) {
156
+ for (const aspect of aspectList) {
157
+ const newExecutor = new AspectExecutor(obj, aspect.method, aspect.adviceList);
158
+ obj[aspect.method] = newExecutor.execute.bind(newExecutor);
159
+ }
160
+ }
161
+ injectAdvice(eggObject, obj, aspectList) {
162
+ if (eggObject.proto.getMetaData(PrototypeUtil.INJECT_TYPE) !== InjectType.CONSTRUCTOR) return;
163
+ for (const aspect of aspectList) for (const advice of aspect.adviceList) {
164
+ const injectObject = eggObject.proto.injectObjects.find((t) => t.objName === advice.name);
165
+ assert(injectObject, `not found inject advice ${advice.name}`);
166
+ const adviceObj = EggContainerFactory.getEggObject(injectObject.proto, advice.name);
167
+ Object.defineProperty(obj, advice.name, {
168
+ value: adviceObj.obj,
169
+ enumerable: false
170
+ });
171
+ }
172
+ }
173
+ async postCreate(_, eggObject) {
174
+ const aspectList = eggObject.proto.getMetaData(ASPECT_LIST);
175
+ if (!aspectList || !aspectList.length) return;
176
+ const propertyDesc = eggObject.constructor && Reflect.getOwnPropertyDescriptor(eggObject.constructor.prototype, "obj");
177
+ if (propertyDesc?.get) {
178
+ let obj;
179
+ const self = this;
180
+ Object.defineProperty(eggObject, "obj", {
181
+ ...propertyDesc,
182
+ get() {
183
+ if (!obj) {
184
+ obj = Reflect.apply(propertyDesc.get, eggObject, []);
185
+ self.hijackMethods(obj, aspectList);
186
+ self.injectAdvice(eggObject, obj, aspectList);
187
+ }
188
+ return obj;
189
+ }
190
+ });
191
+ } else {
192
+ this.hijackMethods(eggObject.obj, aspectList);
193
+ this.injectAdvice(eggObject, eggObject.obj, aspectList);
194
+ }
195
+ }
196
+ };
197
+
198
+ //#endregion
199
+ //#region src/EggPrototypeCrossCutHook.ts
200
+ var EggPrototypeCrossCutHook = class {
201
+ crosscutAdviceFactory;
202
+ constructor(crosscutAdviceFactory) {
203
+ this.crosscutAdviceFactory = crosscutAdviceFactory;
204
+ }
205
+ async preCreate(ctx) {
206
+ if (CrosscutInfoUtil.isCrosscutAdvice(ctx.clazz)) this.crosscutAdviceFactory.registerCrossAdviceClazz(ctx.clazz);
207
+ }
208
+ };
209
+
210
+ //#endregion
211
+ //#region src/LoadUnitAopHook.ts
212
+ var LoadUnitAopHook = class {
213
+ crosscutAdviceFactory;
214
+ constructor(crosscutAdviceFactory) {
215
+ this.crosscutAdviceFactory = crosscutAdviceFactory;
216
+ }
217
+ async postCreate(_, loadUnit) {
218
+ for (const proto of loadUnit.iterateEggPrototype()) {
219
+ const clazz = proto.clazz;
220
+ if (!clazz) continue;
221
+ const aspectList = new AspectMetaBuilder(clazz, { crosscutAdviceFactory: this.crosscutAdviceFactory }).build();
222
+ AspectInfoUtil.setAspectList(aspectList, clazz);
223
+ for (const aspect of aspectList) for (const advice of aspect.adviceList) {
224
+ const adviceProto = PrototypeUtil.getClazzProto(advice.clazz);
225
+ if (!adviceProto) throw TeggError.create(`Aop Advice(${advice.clazz.name}) not found in loadUnits`, "advice_not_found");
226
+ proto.injectObjects.push({
227
+ refName: advice.name,
228
+ objName: advice.name,
229
+ qualifiers: [],
230
+ proto: adviceProto
231
+ });
232
+ }
233
+ }
234
+ }
235
+ };
236
+
237
+ //#endregion
238
+ //#region src/PointCutGraphHook.ts
239
+ function pointCutGraphHook(globalGraph) {
240
+ for (const moduleNode of globalGraph.moduleGraph.nodes.values()) for (const pointCuttedProtoNode of moduleNode.val.protos) {
241
+ const pointCutAdviceProtoList = findPointCutAdvice(globalGraph, pointCuttedProtoNode);
242
+ if (!pointCutAdviceProtoList) continue;
243
+ for (const pointCutAdviceProto of pointCutAdviceProtoList) globalGraph.addInject(moduleNode, pointCuttedProtoNode, pointCutAdviceProto, pointCutAdviceProto.val.proto.name);
244
+ }
245
+ }
246
+ function findPointCutAdvice(globalGraph, protoNode) {
247
+ const proto = protoNode.val.proto;
248
+ if (!ClassProtoDescriptor.isClassProtoDescriptor(proto)) return;
249
+ const result = /* @__PURE__ */ new Set();
250
+ const allMethods = AspectMetaBuilder.getAllMethods(proto.clazz);
251
+ for (const method of allMethods) {
252
+ const adviceInfoList = PointcutAdviceInfoUtil.getPointcutAdviceInfoList(proto.clazz, method);
253
+ for (const { clazz } of adviceInfoList) {
254
+ const property = PrototypeUtil.getProperty(clazz);
255
+ assert(property, "not found property");
256
+ const injectProto = globalGraph.findDependencyProtoNode(protoNode.val.proto, {
257
+ objName: property.name,
258
+ refName: property.name,
259
+ qualifiers: QualifierUtil.mergeQualifiers(property?.qualifiers ?? [], QualifierUtil.getProtoQualifiers(clazz))
260
+ });
261
+ if (injectProto) result.add(injectProto);
262
+ }
263
+ }
264
+ return Array.from(result);
265
+ }
266
+
267
+ //#endregion
8
268
  export { AspectExecutor, EggObjectAopHook, EggPrototypeCrossCutHook, LoadUnitAopHook, crossCutGraphHook, pointCutGraphHook };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/tegg-aop-runtime",
3
- "version": "4.0.0-beta.6",
3
+ "version": "4.0.0-beta.8",
4
4
  "description": "tegg aop",
5
5
  "eggModule": {
6
6
  "name": "teggAopRuntime"
@@ -39,12 +39,12 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "koa-compose": "^4.1.0",
42
- "@eggjs/aop-decorator": "4.0.0-beta.6",
43
- "@eggjs/core-decorator": "4.0.0-beta.6",
44
- "@eggjs/tegg-common-util": "4.0.0-beta.6",
45
- "@eggjs/tegg-metadata": "4.0.0-beta.6",
46
- "@eggjs/tegg-runtime": "4.0.0-beta.6",
47
- "@eggjs/tegg-types": "4.0.0-beta.6"
42
+ "@eggjs/aop-decorator": "4.0.0-beta.8",
43
+ "@eggjs/tegg-common-util": "4.0.0-beta.8",
44
+ "@eggjs/core-decorator": "4.0.0-beta.8",
45
+ "@eggjs/tegg-metadata": "4.0.0-beta.8",
46
+ "@eggjs/tegg-types": "4.0.0-beta.8",
47
+ "@eggjs/tegg-runtime": "4.0.0-beta.8"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@types/koa-compose": "^3.2.5",
@@ -53,7 +53,7 @@
53
53
  "tsdown": "^0.15.6",
54
54
  "unplugin-unused": "^0.5.3",
55
55
  "@eggjs/module-test-util": "4.0.0-beta.4",
56
- "@eggjs/tegg-loader": "4.0.0-beta.6"
56
+ "@eggjs/tegg-loader": "4.0.0-beta.8"
57
57
  },
58
58
  "main": "./dist/index.js",
59
59
  "module": "./dist/index.js",
@@ -1,27 +0,0 @@
1
- import { AdviceContext, AspectAdvice, IAdvice } from "@eggjs/tegg-types";
2
-
3
- //#region src/AspectExecutor.d.ts
4
- declare class InternalAdviceContext<T = Record<string, IAdvice>> {
5
- private readonly state;
6
- that: T;
7
- method: PropertyKey;
8
- args: any[];
9
- constructor(that: T, method: PropertyKey, args: any[]);
10
- get(key: PropertyKey): any;
11
- set(key: PropertyKey, value: any): this;
12
- createCallContext(adviceParams?: any): AdviceContext<T>;
13
- }
14
- declare class AspectExecutor {
15
- obj: object;
16
- method: PropertyKey;
17
- aspectAdviceList: readonly AspectAdvice[];
18
- constructor(obj: object, method: PropertyKey, aspectAdviceList: readonly AspectAdvice[]);
19
- execute(...args: any[]): Promise<void>;
20
- beforeCall(ctx: InternalAdviceContext): Promise<void>;
21
- afterReturn(ctx: InternalAdviceContext, result: any): Promise<void>;
22
- afterThrow(ctx: InternalAdviceContext, error: Error): Promise<void>;
23
- afterFinally(ctx: InternalAdviceContext): Promise<void>;
24
- doExecute(ctx: InternalAdviceContext): Promise<void>;
25
- }
26
- //#endregion
27
- export { AspectExecutor };
@@ -1,113 +0,0 @@
1
- import compose from "koa-compose";
2
-
3
- //#region src/AspectExecutor.ts
4
- var InternalAdviceContext = class {
5
- state;
6
- that;
7
- method;
8
- args;
9
- constructor(that, method, args) {
10
- this.state = /* @__PURE__ */ new Map();
11
- this.that = that;
12
- this.method = method;
13
- this.args = args;
14
- }
15
- get(key) {
16
- return this.state.get(key);
17
- }
18
- set(key, value) {
19
- this.state.set(key, value);
20
- return this;
21
- }
22
- createCallContext(adviceParams) {
23
- return Object.create(this, { adviceParams: { value: adviceParams } });
24
- }
25
- };
26
- var AspectExecutor = class {
27
- obj;
28
- method;
29
- aspectAdviceList;
30
- constructor(obj, method, aspectAdviceList) {
31
- this.obj = obj;
32
- this.method = method;
33
- this.aspectAdviceList = aspectAdviceList;
34
- }
35
- async execute(...args) {
36
- const ctx = new InternalAdviceContext(this.obj, this.method, args);
37
- await this.beforeCall(ctx);
38
- try {
39
- const result = await this.doExecute(ctx);
40
- await this.afterReturn(ctx, result);
41
- return result;
42
- } catch (e) {
43
- await this.afterThrow(ctx, e);
44
- throw e;
45
- } finally {
46
- await this.afterFinally(ctx);
47
- }
48
- }
49
- async beforeCall(ctx) {
50
- for (const aspectAdvice of this.aspectAdviceList) {
51
- const advice = ctx.that[aspectAdvice.name];
52
- if (advice.beforeCall) {
53
- /**
54
- * 这里...写法使传入的参数变成了一个新的对象
55
- * 因此beforeCall里面如果修改了ctx.args
56
- * 最新的args是不会在方法里生效的
57
- * 先保证args可以生效
58
- * 不改动其余地方
59
- */
60
- const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
61
- await advice.beforeCall(fnCtx);
62
- ctx.args = fnCtx.args;
63
- }
64
- }
65
- }
66
- async afterReturn(ctx, result) {
67
- for (const aspectAdvice of this.aspectAdviceList) {
68
- const advice = ctx.that[aspectAdvice.name];
69
- if (advice.afterReturn) {
70
- const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
71
- await advice.afterReturn(fnCtx, result);
72
- }
73
- }
74
- }
75
- async afterThrow(ctx, error) {
76
- for (const aspectAdvice of this.aspectAdviceList) {
77
- const advice = ctx.that[aspectAdvice.name];
78
- if (advice.afterThrow) {
79
- const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
80
- await advice.afterThrow(fnCtx, error);
81
- }
82
- }
83
- }
84
- async afterFinally(ctx) {
85
- for (const aspectAdvice of this.aspectAdviceList) {
86
- const advice = ctx.that[aspectAdvice.name];
87
- if (advice.afterFinally) {
88
- const fnCtx = ctx.createCallContext(aspectAdvice.adviceParams);
89
- await advice.afterFinally(fnCtx);
90
- }
91
- }
92
- }
93
- async doExecute(ctx) {
94
- const lastCall = () => {
95
- const originMethod = Object.getPrototypeOf(this.obj)[this.method];
96
- return Reflect.apply(originMethod, ctx.that, ctx.args);
97
- };
98
- const functions = [];
99
- for (const aspectAdvice of this.aspectAdviceList) {
100
- const advice = ctx.that[aspectAdvice.name];
101
- const fn = advice.around;
102
- if (fn) functions.push(async (ctx$1, next) => {
103
- const fnCtx = ctx$1.createCallContext(aspectAdvice.adviceParams);
104
- return await fn.call(advice, fnCtx, next);
105
- });
106
- }
107
- functions.push(lastCall);
108
- return compose(functions)(ctx);
109
- }
110
- };
111
-
112
- //#endregion
113
- export { AspectExecutor };
@@ -1,6 +0,0 @@
1
- import { GlobalGraph } from "@eggjs/tegg-metadata";
2
-
3
- //#region src/CrossCutGraphHook.d.ts
4
- declare function crossCutGraphHook(globalGraph: GlobalGraph): void;
5
- //#endregion
6
- export { crossCutGraphHook };
@@ -1,38 +0,0 @@
1
- import { AspectMetaBuilder, CrosscutInfoUtil } from "@eggjs/aop-decorator";
2
- import "@eggjs/tegg-common-util";
3
- import { ClassProtoDescriptor, GlobalGraph } from "@eggjs/tegg-metadata";
4
-
5
- //#region src/CrossCutGraphHook.ts
6
- function crossCutGraphHook(globalGraph) {
7
- for (const moduleNode of globalGraph.moduleGraph.nodes.values()) for (const crossCutProtoNode of moduleNode.val.protos) {
8
- const protoNodes = findCrossCuttedClazz(globalGraph, crossCutProtoNode);
9
- if (!protoNodes) continue;
10
- for (const crossCuttedProtoNode of protoNodes) {
11
- const crossCuttedModuleNode = globalGraph.findModuleNode(crossCuttedProtoNode.val.proto.instanceModuleName);
12
- if (!crossCuttedModuleNode) continue;
13
- globalGraph.addInject(crossCuttedModuleNode, crossCuttedProtoNode, crossCutProtoNode, crossCutProtoNode.val.proto.name);
14
- }
15
- }
16
- }
17
- function findCrossCuttedClazz(globalGraph, protoNode) {
18
- const proto = protoNode.val.proto;
19
- if (!ClassProtoDescriptor.isClassProtoDescriptor(proto)) return;
20
- if (!CrosscutInfoUtil.isCrosscutAdvice(proto.clazz)) return;
21
- const crosscutInfoList = CrosscutInfoUtil.getCrosscutInfoList(proto.clazz);
22
- const result = [];
23
- crosscut: for (const crosscutInfo of crosscutInfoList) for (const protoNode$1 of globalGraph.protoGraph.nodes.values()) if (checkClazzMatchCrossCut(protoNode$1, crosscutInfo)) {
24
- result.push(protoNode$1);
25
- break crosscut;
26
- }
27
- return result;
28
- }
29
- function checkClazzMatchCrossCut(protoNode, crosscutInfo) {
30
- const proto = protoNode.val.proto;
31
- if (!ClassProtoDescriptor.isClassProtoDescriptor(proto)) return;
32
- const allMethods = AspectMetaBuilder.getAllMethods(proto.clazz);
33
- for (const method of allMethods) if (crosscutInfo.pointcutInfo.match(proto.clazz, method)) return true;
34
- return false;
35
- }
36
-
37
- //#endregion
38
- export { crossCutGraphHook };
@@ -1,10 +0,0 @@
1
- import { EggObject, EggObjectLifeCycleContext, LifecycleHook } from "@eggjs/tegg-types";
2
-
3
- //#region src/EggObjectAopHook.d.ts
4
- declare class EggObjectAopHook implements LifecycleHook<EggObjectLifeCycleContext, EggObject> {
5
- private hijackMethods;
6
- private injectAdvice;
7
- postCreate(_: EggObjectLifeCycleContext, eggObject: EggObject): Promise<void>;
8
- }
9
- //#endregion
10
- export { EggObjectAopHook };
@@ -1,54 +0,0 @@
1
- import { AspectExecutor } from "./AspectExecutor.js";
2
- import "@eggjs/aop-decorator";
3
- import assert from "node:assert";
4
- import { ASPECT_LIST, InjectType } from "@eggjs/tegg-types";
5
- import { PrototypeUtil } from "@eggjs/core-decorator";
6
- import { EggContainerFactory } from "@eggjs/tegg-runtime";
7
-
8
- //#region src/EggObjectAopHook.ts
9
- var EggObjectAopHook = class {
10
- hijackMethods(obj, aspectList) {
11
- for (const aspect of aspectList) {
12
- const newExecutor = new AspectExecutor(obj, aspect.method, aspect.adviceList);
13
- obj[aspect.method] = newExecutor.execute.bind(newExecutor);
14
- }
15
- }
16
- injectAdvice(eggObject, obj, aspectList) {
17
- if (eggObject.proto.getMetaData(PrototypeUtil.INJECT_TYPE) !== InjectType.CONSTRUCTOR) return;
18
- for (const aspect of aspectList) for (const advice of aspect.adviceList) {
19
- const injectObject = eggObject.proto.injectObjects.find((t) => t.objName === advice.name);
20
- assert(injectObject, `not found inject advice ${advice.name}`);
21
- const adviceObj = EggContainerFactory.getEggObject(injectObject.proto, advice.name);
22
- Object.defineProperty(obj, advice.name, {
23
- value: adviceObj.obj,
24
- enumerable: false
25
- });
26
- }
27
- }
28
- async postCreate(_, eggObject) {
29
- const aspectList = eggObject.proto.getMetaData(ASPECT_LIST);
30
- if (!aspectList || !aspectList.length) return;
31
- const propertyDesc = eggObject.constructor && Reflect.getOwnPropertyDescriptor(eggObject.constructor.prototype, "obj");
32
- if (propertyDesc?.get) {
33
- let obj;
34
- const self = this;
35
- Object.defineProperty(eggObject, "obj", {
36
- ...propertyDesc,
37
- get() {
38
- if (!obj) {
39
- obj = Reflect.apply(propertyDesc.get, eggObject, []);
40
- self.hijackMethods(obj, aspectList);
41
- self.injectAdvice(eggObject, obj, aspectList);
42
- }
43
- return obj;
44
- }
45
- });
46
- } else {
47
- this.hijackMethods(eggObject.obj, aspectList);
48
- this.injectAdvice(eggObject, eggObject.obj, aspectList);
49
- }
50
- }
51
- };
52
-
53
- //#endregion
54
- export { EggObjectAopHook };
@@ -1,11 +0,0 @@
1
- import { CrosscutAdviceFactory } from "@eggjs/aop-decorator";
2
- import { EggPrototype, EggPrototypeLifecycleContext, LifecycleHook } from "@eggjs/tegg-types";
3
-
4
- //#region src/EggPrototypeCrossCutHook.d.ts
5
- declare class EggPrototypeCrossCutHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
6
- private readonly crosscutAdviceFactory;
7
- constructor(crosscutAdviceFactory: CrosscutAdviceFactory);
8
- preCreate(ctx: EggPrototypeLifecycleContext): Promise<void>;
9
- }
10
- //#endregion
11
- export { EggPrototypeCrossCutHook };
@@ -1,15 +0,0 @@
1
- import { CrosscutAdviceFactory, CrosscutInfoUtil } from "@eggjs/aop-decorator";
2
-
3
- //#region src/EggPrototypeCrossCutHook.ts
4
- var EggPrototypeCrossCutHook = class {
5
- crosscutAdviceFactory;
6
- constructor(crosscutAdviceFactory) {
7
- this.crosscutAdviceFactory = crosscutAdviceFactory;
8
- }
9
- async preCreate(ctx) {
10
- if (CrosscutInfoUtil.isCrosscutAdvice(ctx.clazz)) this.crosscutAdviceFactory.registerCrossAdviceClazz(ctx.clazz);
11
- }
12
- };
13
-
14
- //#endregion
15
- export { EggPrototypeCrossCutHook };
@@ -1,11 +0,0 @@
1
- import { CrosscutAdviceFactory } from "@eggjs/aop-decorator";
2
- import { LifecycleHook, LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-types";
3
-
4
- //#region src/LoadUnitAopHook.d.ts
5
- declare class LoadUnitAopHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
6
- private readonly crosscutAdviceFactory;
7
- constructor(crosscutAdviceFactory: CrosscutAdviceFactory);
8
- postCreate(_: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void>;
9
- }
10
- //#endregion
11
- export { LoadUnitAopHook };
@@ -1,32 +0,0 @@
1
- import { AspectInfoUtil, AspectMetaBuilder, CrosscutAdviceFactory } from "@eggjs/aop-decorator";
2
- import { TeggError } from "@eggjs/tegg-metadata";
3
- import { PrototypeUtil } from "@eggjs/core-decorator";
4
-
5
- //#region src/LoadUnitAopHook.ts
6
- var LoadUnitAopHook = class {
7
- crosscutAdviceFactory;
8
- constructor(crosscutAdviceFactory) {
9
- this.crosscutAdviceFactory = crosscutAdviceFactory;
10
- }
11
- async postCreate(_, loadUnit) {
12
- for (const proto of loadUnit.iterateEggPrototype()) {
13
- const clazz = proto.clazz;
14
- if (!clazz) continue;
15
- const aspectList = new AspectMetaBuilder(clazz, { crosscutAdviceFactory: this.crosscutAdviceFactory }).build();
16
- AspectInfoUtil.setAspectList(aspectList, clazz);
17
- for (const aspect of aspectList) for (const advice of aspect.adviceList) {
18
- const adviceProto = PrototypeUtil.getClazzProto(advice.clazz);
19
- if (!adviceProto) throw TeggError.create(`Aop Advice(${advice.clazz.name}) not found in loadUnits`, "advice_not_found");
20
- proto.injectObjects.push({
21
- refName: advice.name,
22
- objName: advice.name,
23
- qualifiers: [],
24
- proto: adviceProto
25
- });
26
- }
27
- }
28
- }
29
- };
30
-
31
- //#endregion
32
- export { LoadUnitAopHook };
@@ -1,6 +0,0 @@
1
- import { GlobalGraph } from "@eggjs/tegg-metadata";
2
-
3
- //#region src/PointCutGraphHook.d.ts
4
- declare function pointCutGraphHook(globalGraph: GlobalGraph): void;
5
- //#endregion
6
- export { pointCutGraphHook };
@@ -1,37 +0,0 @@
1
- import { AspectMetaBuilder, PointcutAdviceInfoUtil } from "@eggjs/aop-decorator";
2
- import "@eggjs/tegg-common-util";
3
- import { ClassProtoDescriptor, GlobalGraph } from "@eggjs/tegg-metadata";
4
- import assert from "node:assert";
5
- import { PrototypeUtil, QualifierUtil } from "@eggjs/core-decorator";
6
-
7
- //#region src/PointCutGraphHook.ts
8
- function pointCutGraphHook(globalGraph) {
9
- for (const moduleNode of globalGraph.moduleGraph.nodes.values()) for (const pointCuttedProtoNode of moduleNode.val.protos) {
10
- const pointCutAdviceProtoList = findPointCutAdvice(globalGraph, pointCuttedProtoNode);
11
- if (!pointCutAdviceProtoList) continue;
12
- for (const pointCutAdviceProto of pointCutAdviceProtoList) globalGraph.addInject(moduleNode, pointCuttedProtoNode, pointCutAdviceProto, pointCutAdviceProto.val.proto.name);
13
- }
14
- }
15
- function findPointCutAdvice(globalGraph, protoNode) {
16
- const proto = protoNode.val.proto;
17
- if (!ClassProtoDescriptor.isClassProtoDescriptor(proto)) return;
18
- const result = /* @__PURE__ */ new Set();
19
- const allMethods = AspectMetaBuilder.getAllMethods(proto.clazz);
20
- for (const method of allMethods) {
21
- const adviceInfoList = PointcutAdviceInfoUtil.getPointcutAdviceInfoList(proto.clazz, method);
22
- for (const { clazz } of adviceInfoList) {
23
- const property = PrototypeUtil.getProperty(clazz);
24
- assert(property, "not found property");
25
- const injectProto = globalGraph.findDependencyProtoNode(protoNode.val.proto, {
26
- objName: property.name,
27
- refName: property.name,
28
- qualifiers: QualifierUtil.mergeQualifiers(property?.qualifiers ?? [], QualifierUtil.getProtoQualifiers(clazz))
29
- });
30
- if (injectProto) result.add(injectProto);
31
- }
32
- }
33
- return Array.from(result);
34
- }
35
-
36
- //#endregion
37
- export { pointCutGraphHook };