@eggjs/aop-decorator 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/index.d.ts CHANGED
@@ -1,13 +1,98 @@
1
- import { Advice } from "./decorator/Advice.js";
2
- import { Crosscut } from "./decorator/Crosscut.js";
3
- import { Pointcut } from "./decorator/Pointcut.js";
4
- import { Aspect, AspectBuilder } from "./model/Aspect.js";
5
- import { ClassPointInfo, CustomPointInfo, NamePointInfo } from "./model/PointcutInfo.js";
6
- import { AdviceInfoUtil, IS_ADVICE } from "./util/AdviceInfoUtil.js";
7
- import { AspectInfoUtil } from "./util/AspectInfoUtil.js";
8
- import { CrosscutInfoUtil } from "./util/CrosscutInfoUtil.js";
9
- import { PointcutAdviceInfoUtil } from "./util/PointcutAdviceInfoUtil.js";
10
- import { CrosscutAdviceFactory } from "./CrosscutAdviceFactory.js";
11
- import { AspectMetaBuilder } from "./AspectMetaBuilder.js";
1
+ import { AdviceInfo, AspectAdvice, CrosscutInfo, CrosscutOptions, CrosscutParam, CustomPointcutCallback, EggProtoImplClass, IAdvice, PointcutInfo, PointcutOptions, PointcutType, PrototypeParams } from "@eggjs/tegg-types";
12
2
  export * from "@eggjs/tegg-types/aop";
3
+
4
+ //#region src/decorator/Advice.d.ts
5
+ declare function Advice(param?: PrototypeParams): (constructor: EggProtoImplClass<IAdvice>) => void;
6
+ //#endregion
7
+ //#region src/decorator/Crosscut.d.ts
8
+ declare function Crosscut(param: CrosscutParam, options?: CrosscutOptions): (constructor: EggProtoImplClass<IAdvice>) => void;
9
+ //#endregion
10
+ //#region src/decorator/Pointcut.d.ts
11
+ declare function Pointcut<T extends object, K = any>(adviceClazz: EggProtoImplClass<IAdvice<T, K>>, options?: PointcutOptions<K>): (target: any, propertyKey: PropertyKey) => void;
12
+ //#endregion
13
+ //#region src/model/Aspect.d.ts
14
+ declare class Aspect {
15
+ readonly clazz: EggProtoImplClass;
16
+ readonly method: PropertyKey;
17
+ readonly adviceList: readonly AspectAdvice[];
18
+ constructor(clazz: EggProtoImplClass, method: PropertyKey, adviceList: readonly AspectAdvice[]);
19
+ }
20
+ declare class AspectBuilder {
21
+ readonly clazz: EggProtoImplClass;
22
+ readonly method: PropertyKey;
23
+ private readonly adviceList;
24
+ constructor(clazz: EggProtoImplClass, method: PropertyKey);
25
+ addAdvice(adviceInfo: AdviceInfo): void;
26
+ build(): Aspect;
27
+ private adviceName;
28
+ }
29
+ //#endregion
30
+ //#region src/model/PointcutInfo.d.ts
31
+ declare class ClassPointInfo implements PointcutInfo {
32
+ readonly type = PointcutType.CLASS;
33
+ readonly clazz: EggProtoImplClass;
34
+ readonly method: PropertyKey;
35
+ constructor(clazz: EggProtoImplClass, method: PropertyKey);
36
+ match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
37
+ }
38
+ declare class NamePointInfo implements PointcutInfo {
39
+ readonly type = PointcutType.NAME;
40
+ readonly className: RegExp;
41
+ readonly methodName: RegExp;
42
+ constructor(className: RegExp, methodName: RegExp);
43
+ match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
44
+ }
45
+ declare class CustomPointInfo implements PointcutInfo {
46
+ readonly type = PointcutType.CUSTOM;
47
+ readonly cb: CustomPointcutCallback;
48
+ constructor(cb: CustomPointcutCallback);
49
+ match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
50
+ }
51
+ //#endregion
52
+ //#region src/util/AdviceInfoUtil.d.ts
53
+ declare const IS_ADVICE: unique symbol;
54
+ declare class AdviceInfoUtil {
55
+ static setIsAdvice(isAdvice: boolean, clazz: EggProtoImplClass<IAdvice>): void;
56
+ static isAdvice(clazz: EggProtoImplClass<IAdvice>): boolean;
57
+ }
58
+ //#endregion
59
+ //#region src/util/AspectInfoUtil.d.ts
60
+ declare class AspectInfoUtil {
61
+ static setAspectList(aspectList: Array<Aspect>, clazz: EggProtoImplClass<IAdvice>): void;
62
+ static getAspectList(clazz: EggProtoImplClass<IAdvice>): Array<Aspect>;
63
+ }
64
+ //#endregion
65
+ //#region src/util/CrosscutInfoUtil.d.ts
66
+ declare class CrosscutInfoUtil {
67
+ static setIsCrosscutAdvice(isCrosscutAdvice: boolean, clazz: EggProtoImplClass<IAdvice>): void;
68
+ static isCrosscutAdvice(clazz: EggProtoImplClass<IAdvice>): boolean;
69
+ static addCrosscutInfo(crosscutInfo: CrosscutInfo, clazz: EggProtoImplClass<IAdvice>): void;
70
+ static getCrosscutInfoList(clazz: EggProtoImplClass<IAdvice>): Array<CrosscutInfo>;
71
+ }
72
+ //#endregion
73
+ //#region src/util/PointcutAdviceInfoUtil.d.ts
74
+ declare class PointcutAdviceInfoUtil {
75
+ static addPointcutAdviceInfo(adviceInfo: AdviceInfo, clazz: EggProtoImplClass, method: PropertyKey): void;
76
+ static getPointcutAdviceInfoList(clazz: EggProtoImplClass, method: PropertyKey): Array<AdviceInfo>;
77
+ }
78
+ //#endregion
79
+ //#region src/CrosscutAdviceFactory.d.ts
80
+ declare class CrosscutAdviceFactory {
81
+ private readonly crosscutAdviceClazzList;
82
+ registerCrossAdviceClazz(clazz: EggProtoImplClass<IAdvice>): void;
83
+ getAdvice(clazz: EggProtoImplClass, method: PropertyKey): Array<AdviceInfo>;
84
+ }
85
+ //#endregion
86
+ //#region src/AspectMetaBuilder.d.ts
87
+ declare class AspectMetaBuilder {
88
+ private readonly clazz;
89
+ private readonly crosscutAdviceFactory;
90
+ constructor(clazz: EggProtoImplClass, options: {
91
+ crosscutAdviceFactory: CrosscutAdviceFactory;
92
+ });
93
+ build(): Array<Aspect>;
94
+ static getAllMethods(clazz: EggProtoImplClass): PropertyKey[];
95
+ private doBuildMethodAspect;
96
+ }
97
+ //#endregion
13
98
  export { Advice, AdviceInfoUtil, Aspect, AspectBuilder, AspectInfoUtil, AspectMetaBuilder, ClassPointInfo, Crosscut, CrosscutAdviceFactory, CrosscutInfoUtil, CustomPointInfo, IS_ADVICE, NamePointInfo, Pointcut, PointcutAdviceInfoUtil };
package/dist/index.js CHANGED
@@ -1,18 +1,266 @@
1
- import { AdviceInfoUtil, IS_ADVICE } from "./util/AdviceInfoUtil.js";
2
- import { Aspect, AspectBuilder } from "./model/Aspect.js";
3
- import { ClassPointInfo, CustomPointInfo, NamePointInfo } from "./model/PointcutInfo.js";
4
- import "./model/index.js";
5
- import { AspectInfoUtil } from "./util/AspectInfoUtil.js";
6
- import { CrosscutInfoUtil } from "./util/CrosscutInfoUtil.js";
7
- import { PointcutAdviceInfoUtil } from "./util/PointcutAdviceInfoUtil.js";
8
- import "./util/index.js";
9
- import { Advice } from "./decorator/Advice.js";
10
- import { Crosscut } from "./decorator/Crosscut.js";
11
- import { Pointcut } from "./decorator/Pointcut.js";
12
- import "./decorator/index.js";
13
- import { CrosscutAdviceFactory } from "./CrosscutAdviceFactory.js";
14
- import { AspectMetaBuilder } from "./AspectMetaBuilder.js";
1
+ import { MetadataUtil, Prototype, PrototypeUtil } from "@eggjs/core-decorator";
2
+ import { StackUtil } from "@eggjs/tegg-common-util";
3
+ import { ASPECT_LIST, AccessLevel, CROSSCUT_INFO_LIST, IS_CROSSCUT_ADVICE, ObjectInitType, POINTCUT_ADVICE_INFO_LIAR, PointcutType } from "@eggjs/tegg-types";
4
+ import assert from "node:assert";
15
5
 
16
6
  export * from "@eggjs/tegg-types/aop"
17
7
 
8
+ //#region src/util/AdviceInfoUtil.ts
9
+ const IS_ADVICE = Symbol.for("EggPrototype#isAdvice");
10
+ var AdviceInfoUtil = class {
11
+ static setIsAdvice(isAdvice, clazz) {
12
+ MetadataUtil.defineMetaData(IS_ADVICE, isAdvice, clazz);
13
+ }
14
+ static isAdvice(clazz) {
15
+ return !!MetadataUtil.getMetaData(IS_ADVICE, clazz);
16
+ }
17
+ };
18
+
19
+ //#endregion
20
+ //#region src/model/Aspect.ts
21
+ var Aspect = class {
22
+ clazz;
23
+ method;
24
+ adviceList;
25
+ constructor(clazz, method, adviceList) {
26
+ this.clazz = clazz;
27
+ this.method = method;
28
+ this.adviceList = adviceList;
29
+ }
30
+ };
31
+ var AspectBuilder = class {
32
+ clazz;
33
+ method;
34
+ adviceList;
35
+ constructor(clazz, method) {
36
+ this.clazz = clazz;
37
+ this.method = method;
38
+ this.adviceList = [];
39
+ }
40
+ addAdvice(adviceInfo) {
41
+ this.adviceList.push(adviceInfo);
42
+ }
43
+ build() {
44
+ this.adviceList.sort((a, b) => a.order - b.order);
45
+ const aspectAdviceList = this.adviceList.map((t, i) => {
46
+ return {
47
+ clazz: t.clazz,
48
+ name: this.adviceName(t.clazz, i),
49
+ adviceParams: t.adviceParams
50
+ };
51
+ });
52
+ return new Aspect(this.clazz, this.method, aspectAdviceList);
53
+ }
54
+ adviceName(advice, index) {
55
+ return `${this.clazz.name}#${String(this.method)}#${advice.name}#${index}`;
56
+ }
57
+ };
58
+
59
+ //#endregion
60
+ //#region src/model/PointcutInfo.ts
61
+ var ClassPointInfo = class {
62
+ type = PointcutType.CLASS;
63
+ clazz;
64
+ method;
65
+ constructor(clazz, method) {
66
+ this.clazz = clazz;
67
+ this.method = method;
68
+ }
69
+ match(clazz, method) {
70
+ return (this.clazz === clazz || clazz.prototype instanceof this.clazz) && this.method === method;
71
+ }
72
+ };
73
+ var NamePointInfo = class {
74
+ type = PointcutType.NAME;
75
+ className;
76
+ methodName;
77
+ constructor(className, methodName) {
78
+ this.className = className;
79
+ this.methodName = methodName;
80
+ }
81
+ match(clazz, method) {
82
+ return this.className.test(clazz.name) && this.methodName.test(String(method));
83
+ }
84
+ };
85
+ var CustomPointInfo = class {
86
+ type = PointcutType.CUSTOM;
87
+ cb;
88
+ constructor(cb) {
89
+ this.cb = cb;
90
+ }
91
+ match(clazz, method) {
92
+ return this.cb(clazz, method);
93
+ }
94
+ };
95
+
96
+ //#endregion
97
+ //#region src/util/AspectInfoUtil.ts
98
+ var AspectInfoUtil = class {
99
+ static setAspectList(aspectList, clazz) {
100
+ MetadataUtil.defineMetaData(ASPECT_LIST, aspectList, clazz);
101
+ }
102
+ static getAspectList(clazz) {
103
+ return MetadataUtil.getMetaData(ASPECT_LIST, clazz) || [];
104
+ }
105
+ };
106
+
107
+ //#endregion
108
+ //#region src/util/CrosscutInfoUtil.ts
109
+ var CrosscutInfoUtil = class {
110
+ static setIsCrosscutAdvice(isCrosscutAdvice, clazz) {
111
+ MetadataUtil.defineMetaData(IS_CROSSCUT_ADVICE, isCrosscutAdvice, clazz);
112
+ }
113
+ static isCrosscutAdvice(clazz) {
114
+ return !!MetadataUtil.getMetaData(IS_CROSSCUT_ADVICE, clazz);
115
+ }
116
+ static addCrosscutInfo(crosscutInfo, clazz) {
117
+ MetadataUtil.initOwnArrayMetaData(CROSSCUT_INFO_LIST, clazz, []).push(crosscutInfo);
118
+ }
119
+ static getCrosscutInfoList(clazz) {
120
+ return MetadataUtil.getArrayMetaData(CROSSCUT_INFO_LIST, clazz) || [];
121
+ }
122
+ };
123
+
124
+ //#endregion
125
+ //#region src/util/PointcutAdviceInfoUtil.ts
126
+ var PointcutAdviceInfoUtil = class {
127
+ static addPointcutAdviceInfo(adviceInfo, clazz, method) {
128
+ MetadataUtil.initOwnArrayMetaData(POINTCUT_ADVICE_INFO_LIAR, clazz, []).unshift({
129
+ method,
130
+ adviceInfo
131
+ });
132
+ }
133
+ static getPointcutAdviceInfoList(clazz, method) {
134
+ return (MetadataUtil.getMetaData(POINTCUT_ADVICE_INFO_LIAR, clazz) || []).filter((t) => t.method === method).map((t) => t.adviceInfo);
135
+ }
136
+ };
137
+
138
+ //#endregion
139
+ //#region src/decorator/Advice.ts
140
+ const defaultAdviceParam = {
141
+ accessLevel: AccessLevel.PUBLIC,
142
+ initType: ObjectInitType.SINGLETON
143
+ };
144
+ function Advice(param) {
145
+ return function(constructor) {
146
+ AdviceInfoUtil.setIsAdvice(true, constructor);
147
+ Prototype({
148
+ ...defaultAdviceParam,
149
+ ...param
150
+ })(constructor);
151
+ PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5));
152
+ };
153
+ }
154
+
155
+ //#endregion
156
+ //#region src/decorator/Crosscut.ts
157
+ const defaultCrossOptions = { order: 100 };
158
+ function Crosscut(param, options) {
159
+ return function(constructor) {
160
+ let crosscutInfo;
161
+ if (param.type === PointcutType.CLASS) crosscutInfo = {
162
+ pointcutInfo: new ClassPointInfo(param.clazz, param.methodName),
163
+ adviceInfo: {
164
+ clazz: constructor,
165
+ order: options?.order ?? defaultCrossOptions.order,
166
+ adviceParams: options?.adviceParams
167
+ }
168
+ };
169
+ else if (param.type === PointcutType.NAME) crosscutInfo = {
170
+ pointcutInfo: new NamePointInfo(param.className, param.methodName),
171
+ adviceInfo: {
172
+ clazz: constructor,
173
+ order: options?.order ?? defaultCrossOptions.order,
174
+ adviceParams: options?.adviceParams
175
+ }
176
+ };
177
+ else crosscutInfo = {
178
+ pointcutInfo: new CustomPointInfo(param.callback),
179
+ adviceInfo: {
180
+ clazz: constructor,
181
+ order: options?.order ?? defaultCrossOptions.order,
182
+ adviceParams: options?.adviceParams
183
+ }
184
+ };
185
+ CrosscutInfoUtil.setIsCrosscutAdvice(true, constructor);
186
+ CrosscutInfoUtil.addCrosscutInfo(crosscutInfo, constructor);
187
+ };
188
+ }
189
+
190
+ //#endregion
191
+ //#region src/decorator/Pointcut.ts
192
+ const defaultPointcutOptions = { order: 1e3 };
193
+ function Pointcut(adviceClazz, options) {
194
+ return function(target, propertyKey) {
195
+ assert(AdviceInfoUtil.isAdvice(adviceClazz), `class ${adviceClazz} has no @Advice decorator`);
196
+ const targetClazz = target.constructor;
197
+ const methodName = propertyKey;
198
+ PointcutAdviceInfoUtil.addPointcutAdviceInfo({
199
+ clazz: adviceClazz,
200
+ order: options?.order ?? defaultPointcutOptions.order,
201
+ adviceParams: options?.adviceParams
202
+ }, targetClazz, methodName);
203
+ };
204
+ }
205
+
206
+ //#endregion
207
+ //#region src/CrosscutAdviceFactory.ts
208
+ var CrosscutAdviceFactory = class {
209
+ crosscutAdviceClazzList = [];
210
+ registerCrossAdviceClazz(clazz) {
211
+ assert(CrosscutInfoUtil.isCrosscutAdvice(clazz), `clazz ${clazz.name} is not crosscut advice`);
212
+ this.crosscutAdviceClazzList.push(clazz);
213
+ }
214
+ getAdvice(clazz, method) {
215
+ const result = [];
216
+ for (const crosscutAdviceClazz of this.crosscutAdviceClazzList) {
217
+ const crosscutInfoList = CrosscutInfoUtil.getCrosscutInfoList(crosscutAdviceClazz);
218
+ for (const crosscutInfo of crosscutInfoList) if (crosscutInfo.pointcutInfo.match(clazz, method)) result.push(crosscutInfo.adviceInfo);
219
+ }
220
+ return result;
221
+ }
222
+ };
223
+
224
+ //#endregion
225
+ //#region src/AspectMetaBuilder.ts
226
+ var AspectMetaBuilder = class AspectMetaBuilder {
227
+ clazz;
228
+ crosscutAdviceFactory;
229
+ constructor(clazz, options) {
230
+ this.clazz = clazz;
231
+ this.crosscutAdviceFactory = options.crosscutAdviceFactory;
232
+ }
233
+ build() {
234
+ const aspectList = [];
235
+ const methods = AspectMetaBuilder.getAllMethods(this.clazz);
236
+ for (const method of methods) {
237
+ const aspect = this.doBuildMethodAspect(method);
238
+ if (aspect) aspectList.push(aspect);
239
+ }
240
+ return aspectList;
241
+ }
242
+ static getAllMethods(clazz) {
243
+ const methodSet = /* @__PURE__ */ new Set();
244
+ function getMethods(obj) {
245
+ if (obj) {
246
+ const propDescs = Object.getOwnPropertyDescriptors(obj);
247
+ for (const [name, desc] of Object.entries(propDescs)) if (desc.value instanceof Function) methodSet.add(name);
248
+ getMethods(Object.getPrototypeOf(obj));
249
+ }
250
+ }
251
+ getMethods(clazz.prototype);
252
+ return Array.from(methodSet);
253
+ }
254
+ doBuildMethodAspect(method) {
255
+ const crosscutAdviceList = this.crosscutAdviceFactory.getAdvice(this.clazz, method);
256
+ const pointcutAdviceList = PointcutAdviceInfoUtil.getPointcutAdviceInfoList(this.clazz, method);
257
+ if (!crosscutAdviceList.length && !pointcutAdviceList.length) return;
258
+ const aspectBuilder = new AspectBuilder(this.clazz, method);
259
+ for (const advice of crosscutAdviceList) aspectBuilder.addAdvice(advice);
260
+ for (const advice of pointcutAdviceList) aspectBuilder.addAdvice(advice);
261
+ return aspectBuilder.build();
262
+ }
263
+ };
264
+
265
+ //#endregion
18
266
  export { Advice, AdviceInfoUtil, Aspect, AspectBuilder, AspectInfoUtil, AspectMetaBuilder, ClassPointInfo, Crosscut, CrosscutAdviceFactory, CrosscutInfoUtil, CustomPointInfo, IS_ADVICE, NamePointInfo, Pointcut, PointcutAdviceInfoUtil };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/aop-decorator",
3
- "version": "4.0.0-beta.7",
3
+ "version": "4.0.0-beta.8",
4
4
  "description": "tegg aop decorator",
5
5
  "keywords": [
6
6
  "tegg",
@@ -16,9 +16,9 @@
16
16
  "directory": "core/aop-decorator"
17
17
  },
18
18
  "dependencies": {
19
- "@eggjs/core-decorator": "4.0.0-beta.7",
20
- "@eggjs/tegg-common-util": "4.0.0-beta.7",
21
- "@eggjs/tegg-types": "4.0.0-beta.7"
19
+ "@eggjs/core-decorator": "4.0.0-beta.8",
20
+ "@eggjs/tegg-common-util": "4.0.0-beta.8",
21
+ "@eggjs/tegg-types": "4.0.0-beta.8"
22
22
  },
23
23
  "publishConfig": {
24
24
  "access": "public"
@@ -1,17 +0,0 @@
1
- import { Aspect } from "./model/Aspect.js";
2
- import { CrosscutAdviceFactory } from "./CrosscutAdviceFactory.js";
3
- import { EggProtoImplClass } from "@eggjs/tegg-types";
4
-
5
- //#region src/AspectMetaBuilder.d.ts
6
- declare class AspectMetaBuilder {
7
- private readonly clazz;
8
- private readonly crosscutAdviceFactory;
9
- constructor(clazz: EggProtoImplClass, options: {
10
- crosscutAdviceFactory: CrosscutAdviceFactory;
11
- });
12
- build(): Array<Aspect>;
13
- static getAllMethods(clazz: EggProtoImplClass): PropertyKey[];
14
- private doBuildMethodAspect;
15
- }
16
- //#endregion
17
- export { AspectMetaBuilder };
@@ -1,48 +0,0 @@
1
- import { AspectBuilder } from "./model/Aspect.js";
2
- import "./model/index.js";
3
- import { PointcutAdviceInfoUtil } from "./util/PointcutAdviceInfoUtil.js";
4
- import "./util/index.js";
5
- import "./CrosscutAdviceFactory.js";
6
-
7
- //#region src/AspectMetaBuilder.ts
8
- var AspectMetaBuilder = class AspectMetaBuilder {
9
- clazz;
10
- crosscutAdviceFactory;
11
- constructor(clazz, options) {
12
- this.clazz = clazz;
13
- this.crosscutAdviceFactory = options.crosscutAdviceFactory;
14
- }
15
- build() {
16
- const aspectList = [];
17
- const methods = AspectMetaBuilder.getAllMethods(this.clazz);
18
- for (const method of methods) {
19
- const aspect = this.doBuildMethodAspect(method);
20
- if (aspect) aspectList.push(aspect);
21
- }
22
- return aspectList;
23
- }
24
- static getAllMethods(clazz) {
25
- const methodSet = /* @__PURE__ */ new Set();
26
- function getMethods(obj) {
27
- if (obj) {
28
- const propDescs = Object.getOwnPropertyDescriptors(obj);
29
- for (const [name, desc] of Object.entries(propDescs)) if (desc.value instanceof Function) methodSet.add(name);
30
- getMethods(Object.getPrototypeOf(obj));
31
- }
32
- }
33
- getMethods(clazz.prototype);
34
- return Array.from(methodSet);
35
- }
36
- doBuildMethodAspect(method) {
37
- const crosscutAdviceList = this.crosscutAdviceFactory.getAdvice(this.clazz, method);
38
- const pointcutAdviceList = PointcutAdviceInfoUtil.getPointcutAdviceInfoList(this.clazz, method);
39
- if (!crosscutAdviceList.length && !pointcutAdviceList.length) return;
40
- const aspectBuilder = new AspectBuilder(this.clazz, method);
41
- for (const advice of crosscutAdviceList) aspectBuilder.addAdvice(advice);
42
- for (const advice of pointcutAdviceList) aspectBuilder.addAdvice(advice);
43
- return aspectBuilder.build();
44
- }
45
- };
46
-
47
- //#endregion
48
- export { AspectMetaBuilder };
@@ -1,10 +0,0 @@
1
- import { AdviceInfo, EggProtoImplClass, IAdvice } from "@eggjs/tegg-types";
2
-
3
- //#region src/CrosscutAdviceFactory.d.ts
4
- declare class CrosscutAdviceFactory {
5
- private readonly crosscutAdviceClazzList;
6
- registerCrossAdviceClazz(clazz: EggProtoImplClass<IAdvice>): void;
7
- getAdvice(clazz: EggProtoImplClass, method: PropertyKey): Array<AdviceInfo>;
8
- }
9
- //#endregion
10
- export { CrosscutAdviceFactory };
@@ -1,23 +0,0 @@
1
- import { CrosscutInfoUtil } from "./util/CrosscutInfoUtil.js";
2
- import "./util/index.js";
3
- import assert from "node:assert";
4
-
5
- //#region src/CrosscutAdviceFactory.ts
6
- var CrosscutAdviceFactory = class {
7
- crosscutAdviceClazzList = [];
8
- registerCrossAdviceClazz(clazz) {
9
- assert(CrosscutInfoUtil.isCrosscutAdvice(clazz), `clazz ${clazz.name} is not crosscut advice`);
10
- this.crosscutAdviceClazzList.push(clazz);
11
- }
12
- getAdvice(clazz, method) {
13
- const result = [];
14
- for (const crosscutAdviceClazz of this.crosscutAdviceClazzList) {
15
- const crosscutInfoList = CrosscutInfoUtil.getCrosscutInfoList(crosscutAdviceClazz);
16
- for (const crosscutInfo of crosscutInfoList) if (crosscutInfo.pointcutInfo.match(clazz, method)) result.push(crosscutInfo.adviceInfo);
17
- }
18
- return result;
19
- }
20
- };
21
-
22
- //#endregion
23
- export { CrosscutAdviceFactory };
@@ -1,6 +0,0 @@
1
- import { EggProtoImplClass, IAdvice, PrototypeParams } from "@eggjs/tegg-types";
2
-
3
- //#region src/decorator/Advice.d.ts
4
- declare function Advice(param?: PrototypeParams): (constructor: EggProtoImplClass<IAdvice>) => void;
5
- //#endregion
6
- export { Advice };
@@ -1,24 +0,0 @@
1
- import { AdviceInfoUtil } from "../util/AdviceInfoUtil.js";
2
- import "../util/index.js";
3
- import { Prototype, PrototypeUtil } from "@eggjs/core-decorator";
4
- import { StackUtil } from "@eggjs/tegg-common-util";
5
- import { AccessLevel, ObjectInitType } from "@eggjs/tegg-types";
6
-
7
- //#region src/decorator/Advice.ts
8
- const defaultAdviceParam = {
9
- accessLevel: AccessLevel.PUBLIC,
10
- initType: ObjectInitType.SINGLETON
11
- };
12
- function Advice(param) {
13
- return function(constructor) {
14
- AdviceInfoUtil.setIsAdvice(true, constructor);
15
- Prototype({
16
- ...defaultAdviceParam,
17
- ...param
18
- })(constructor);
19
- PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5));
20
- };
21
- }
22
-
23
- //#endregion
24
- export { Advice };
@@ -1,6 +0,0 @@
1
- import { CrosscutOptions, CrosscutParam, EggProtoImplClass, IAdvice } from "@eggjs/tegg-types";
2
-
3
- //#region src/decorator/Crosscut.d.ts
4
- declare function Crosscut(param: CrosscutParam, options?: CrosscutOptions): (constructor: EggProtoImplClass<IAdvice>) => void;
5
- //#endregion
6
- export { Crosscut };
@@ -1,42 +0,0 @@
1
- import { ClassPointInfo, CustomPointInfo, NamePointInfo } from "../model/PointcutInfo.js";
2
- import "../model/index.js";
3
- import { CrosscutInfoUtil } from "../util/CrosscutInfoUtil.js";
4
- import "../util/index.js";
5
- import { PointcutType } from "@eggjs/tegg-types";
6
-
7
- //#region src/decorator/Crosscut.ts
8
- const defaultCrossOptions = { order: 100 };
9
- function Crosscut(param, options) {
10
- return function(constructor) {
11
- let crosscutInfo;
12
- if (param.type === PointcutType.CLASS) crosscutInfo = {
13
- pointcutInfo: new ClassPointInfo(param.clazz, param.methodName),
14
- adviceInfo: {
15
- clazz: constructor,
16
- order: options?.order ?? defaultCrossOptions.order,
17
- adviceParams: options?.adviceParams
18
- }
19
- };
20
- else if (param.type === PointcutType.NAME) crosscutInfo = {
21
- pointcutInfo: new NamePointInfo(param.className, param.methodName),
22
- adviceInfo: {
23
- clazz: constructor,
24
- order: options?.order ?? defaultCrossOptions.order,
25
- adviceParams: options?.adviceParams
26
- }
27
- };
28
- else crosscutInfo = {
29
- pointcutInfo: new CustomPointInfo(param.callback),
30
- adviceInfo: {
31
- clazz: constructor,
32
- order: options?.order ?? defaultCrossOptions.order,
33
- adviceParams: options?.adviceParams
34
- }
35
- };
36
- CrosscutInfoUtil.setIsCrosscutAdvice(true, constructor);
37
- CrosscutInfoUtil.addCrosscutInfo(crosscutInfo, constructor);
38
- };
39
- }
40
-
41
- //#endregion
42
- export { Crosscut };
@@ -1,6 +0,0 @@
1
- import { EggProtoImplClass, IAdvice, PointcutOptions } from "@eggjs/tegg-types";
2
-
3
- //#region src/decorator/Pointcut.d.ts
4
- declare function Pointcut<T extends object, K = any>(adviceClazz: EggProtoImplClass<IAdvice<T, K>>, options?: PointcutOptions<K>): (target: any, propertyKey: PropertyKey) => void;
5
- //#endregion
6
- export { Pointcut };
@@ -1,22 +0,0 @@
1
- import { AdviceInfoUtil } from "../util/AdviceInfoUtil.js";
2
- import { PointcutAdviceInfoUtil } from "../util/PointcutAdviceInfoUtil.js";
3
- import "../util/index.js";
4
- import assert from "node:assert";
5
-
6
- //#region src/decorator/Pointcut.ts
7
- const defaultPointcutOptions = { order: 1e3 };
8
- function Pointcut(adviceClazz, options) {
9
- return function(target, propertyKey) {
10
- assert(AdviceInfoUtil.isAdvice(adviceClazz), `class ${adviceClazz} has no @Advice decorator`);
11
- const targetClazz = target.constructor;
12
- const methodName = propertyKey;
13
- PointcutAdviceInfoUtil.addPointcutAdviceInfo({
14
- clazz: adviceClazz,
15
- order: options?.order ?? defaultPointcutOptions.order,
16
- adviceParams: options?.adviceParams
17
- }, targetClazz, methodName);
18
- };
19
- }
20
-
21
- //#endregion
22
- export { Pointcut };
@@ -1,5 +0,0 @@
1
- import { Advice } from "./Advice.js";
2
- import { Crosscut } from "./Crosscut.js";
3
- import { Pointcut } from "./Pointcut.js";
4
-
5
- export { };
@@ -1,20 +0,0 @@
1
- import { AdviceInfo, AspectAdvice, EggProtoImplClass } from "@eggjs/tegg-types";
2
-
3
- //#region src/model/Aspect.d.ts
4
- declare class Aspect {
5
- readonly clazz: EggProtoImplClass;
6
- readonly method: PropertyKey;
7
- readonly adviceList: readonly AspectAdvice[];
8
- constructor(clazz: EggProtoImplClass, method: PropertyKey, adviceList: readonly AspectAdvice[]);
9
- }
10
- declare class AspectBuilder {
11
- readonly clazz: EggProtoImplClass;
12
- readonly method: PropertyKey;
13
- private readonly adviceList;
14
- constructor(clazz: EggProtoImplClass, method: PropertyKey);
15
- addAdvice(adviceInfo: AdviceInfo): void;
16
- build(): Aspect;
17
- private adviceName;
18
- }
19
- //#endregion
20
- export { Aspect, AspectBuilder };
@@ -1,41 +0,0 @@
1
- //#region src/model/Aspect.ts
2
- var Aspect = class {
3
- clazz;
4
- method;
5
- adviceList;
6
- constructor(clazz, method, adviceList) {
7
- this.clazz = clazz;
8
- this.method = method;
9
- this.adviceList = adviceList;
10
- }
11
- };
12
- var AspectBuilder = class {
13
- clazz;
14
- method;
15
- adviceList;
16
- constructor(clazz, method) {
17
- this.clazz = clazz;
18
- this.method = method;
19
- this.adviceList = [];
20
- }
21
- addAdvice(adviceInfo) {
22
- this.adviceList.push(adviceInfo);
23
- }
24
- build() {
25
- this.adviceList.sort((a, b) => a.order - b.order);
26
- const aspectAdviceList = this.adviceList.map((t, i) => {
27
- return {
28
- clazz: t.clazz,
29
- name: this.adviceName(t.clazz, i),
30
- adviceParams: t.adviceParams
31
- };
32
- });
33
- return new Aspect(this.clazz, this.method, aspectAdviceList);
34
- }
35
- adviceName(advice, index) {
36
- return `${this.clazz.name}#${String(this.method)}#${advice.name}#${index}`;
37
- }
38
- };
39
-
40
- //#endregion
41
- export { Aspect, AspectBuilder };
@@ -1,25 +0,0 @@
1
- import { CustomPointcutCallback, EggProtoImplClass, PointcutInfo, PointcutType } from "@eggjs/tegg-types";
2
-
3
- //#region src/model/PointcutInfo.d.ts
4
- declare class ClassPointInfo implements PointcutInfo {
5
- readonly type = PointcutType.CLASS;
6
- readonly clazz: EggProtoImplClass;
7
- readonly method: PropertyKey;
8
- constructor(clazz: EggProtoImplClass, method: PropertyKey);
9
- match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
10
- }
11
- declare class NamePointInfo implements PointcutInfo {
12
- readonly type = PointcutType.NAME;
13
- readonly className: RegExp;
14
- readonly methodName: RegExp;
15
- constructor(className: RegExp, methodName: RegExp);
16
- match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
17
- }
18
- declare class CustomPointInfo implements PointcutInfo {
19
- readonly type = PointcutType.CUSTOM;
20
- readonly cb: CustomPointcutCallback;
21
- constructor(cb: CustomPointcutCallback);
22
- match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
23
- }
24
- //#endregion
25
- export { ClassPointInfo, CustomPointInfo, NamePointInfo };
@@ -1,40 +0,0 @@
1
- import { PointcutType } from "@eggjs/tegg-types";
2
-
3
- //#region src/model/PointcutInfo.ts
4
- var ClassPointInfo = class {
5
- type = PointcutType.CLASS;
6
- clazz;
7
- method;
8
- constructor(clazz, method) {
9
- this.clazz = clazz;
10
- this.method = method;
11
- }
12
- match(clazz, method) {
13
- return (this.clazz === clazz || clazz.prototype instanceof this.clazz) && this.method === method;
14
- }
15
- };
16
- var NamePointInfo = class {
17
- type = PointcutType.NAME;
18
- className;
19
- methodName;
20
- constructor(className, methodName) {
21
- this.className = className;
22
- this.methodName = methodName;
23
- }
24
- match(clazz, method) {
25
- return this.className.test(clazz.name) && this.methodName.test(String(method));
26
- }
27
- };
28
- var CustomPointInfo = class {
29
- type = PointcutType.CUSTOM;
30
- cb;
31
- constructor(cb) {
32
- this.cb = cb;
33
- }
34
- match(clazz, method) {
35
- return this.cb(clazz, method);
36
- }
37
- };
38
-
39
- //#endregion
40
- export { ClassPointInfo, CustomPointInfo, NamePointInfo };
@@ -1,4 +0,0 @@
1
- import { Aspect, AspectBuilder } from "./Aspect.js";
2
- import { ClassPointInfo, CustomPointInfo, NamePointInfo } from "./PointcutInfo.js";
3
-
4
- export { };
@@ -1,10 +0,0 @@
1
- import { EggProtoImplClass, IAdvice } from "@eggjs/tegg-types";
2
-
3
- //#region src/util/AdviceInfoUtil.d.ts
4
- declare const IS_ADVICE: unique symbol;
5
- declare class AdviceInfoUtil {
6
- static setIsAdvice(isAdvice: boolean, clazz: EggProtoImplClass<IAdvice>): void;
7
- static isAdvice(clazz: EggProtoImplClass<IAdvice>): boolean;
8
- }
9
- //#endregion
10
- export { AdviceInfoUtil, IS_ADVICE };
@@ -1,15 +0,0 @@
1
- import { MetadataUtil } from "@eggjs/core-decorator";
2
-
3
- //#region src/util/AdviceInfoUtil.ts
4
- const IS_ADVICE = Symbol.for("EggPrototype#isAdvice");
5
- var AdviceInfoUtil = class {
6
- static setIsAdvice(isAdvice, clazz) {
7
- MetadataUtil.defineMetaData(IS_ADVICE, isAdvice, clazz);
8
- }
9
- static isAdvice(clazz) {
10
- return !!MetadataUtil.getMetaData(IS_ADVICE, clazz);
11
- }
12
- };
13
-
14
- //#endregion
15
- export { AdviceInfoUtil, IS_ADVICE };
@@ -1,10 +0,0 @@
1
- import { Aspect } from "../model/Aspect.js";
2
- import { EggProtoImplClass, IAdvice } from "@eggjs/tegg-types";
3
-
4
- //#region src/util/AspectInfoUtil.d.ts
5
- declare class AspectInfoUtil {
6
- static setAspectList(aspectList: Array<Aspect>, clazz: EggProtoImplClass<IAdvice>): void;
7
- static getAspectList(clazz: EggProtoImplClass<IAdvice>): Array<Aspect>;
8
- }
9
- //#endregion
10
- export { AspectInfoUtil };
@@ -1,16 +0,0 @@
1
- import "../model/index.js";
2
- import { MetadataUtil } from "@eggjs/core-decorator";
3
- import { ASPECT_LIST } from "@eggjs/tegg-types";
4
-
5
- //#region src/util/AspectInfoUtil.ts
6
- var AspectInfoUtil = class {
7
- static setAspectList(aspectList, clazz) {
8
- MetadataUtil.defineMetaData(ASPECT_LIST, aspectList, clazz);
9
- }
10
- static getAspectList(clazz) {
11
- return MetadataUtil.getMetaData(ASPECT_LIST, clazz) || [];
12
- }
13
- };
14
-
15
- //#endregion
16
- export { AspectInfoUtil };
@@ -1,11 +0,0 @@
1
- import { CrosscutInfo, EggProtoImplClass, IAdvice } from "@eggjs/tegg-types";
2
-
3
- //#region src/util/CrosscutInfoUtil.d.ts
4
- declare class CrosscutInfoUtil {
5
- static setIsCrosscutAdvice(isCrosscutAdvice: boolean, clazz: EggProtoImplClass<IAdvice>): void;
6
- static isCrosscutAdvice(clazz: EggProtoImplClass<IAdvice>): boolean;
7
- static addCrosscutInfo(crosscutInfo: CrosscutInfo, clazz: EggProtoImplClass<IAdvice>): void;
8
- static getCrosscutInfoList(clazz: EggProtoImplClass<IAdvice>): Array<CrosscutInfo>;
9
- }
10
- //#endregion
11
- export { CrosscutInfoUtil };
@@ -1,21 +0,0 @@
1
- import { MetadataUtil } from "@eggjs/core-decorator";
2
- import { CROSSCUT_INFO_LIST, IS_CROSSCUT_ADVICE } from "@eggjs/tegg-types";
3
-
4
- //#region src/util/CrosscutInfoUtil.ts
5
- var CrosscutInfoUtil = class {
6
- static setIsCrosscutAdvice(isCrosscutAdvice, clazz) {
7
- MetadataUtil.defineMetaData(IS_CROSSCUT_ADVICE, isCrosscutAdvice, clazz);
8
- }
9
- static isCrosscutAdvice(clazz) {
10
- return !!MetadataUtil.getMetaData(IS_CROSSCUT_ADVICE, clazz);
11
- }
12
- static addCrosscutInfo(crosscutInfo, clazz) {
13
- MetadataUtil.initOwnArrayMetaData(CROSSCUT_INFO_LIST, clazz, []).push(crosscutInfo);
14
- }
15
- static getCrosscutInfoList(clazz) {
16
- return MetadataUtil.getArrayMetaData(CROSSCUT_INFO_LIST, clazz) || [];
17
- }
18
- };
19
-
20
- //#endregion
21
- export { CrosscutInfoUtil };
@@ -1,9 +0,0 @@
1
- import { AdviceInfo, EggProtoImplClass } from "@eggjs/tegg-types";
2
-
3
- //#region src/util/PointcutAdviceInfoUtil.d.ts
4
- declare class PointcutAdviceInfoUtil {
5
- static addPointcutAdviceInfo(adviceInfo: AdviceInfo, clazz: EggProtoImplClass, method: PropertyKey): void;
6
- static getPointcutAdviceInfoList(clazz: EggProtoImplClass, method: PropertyKey): Array<AdviceInfo>;
7
- }
8
- //#endregion
9
- export { PointcutAdviceInfoUtil };
@@ -1,18 +0,0 @@
1
- import { MetadataUtil } from "@eggjs/core-decorator";
2
- import { POINTCUT_ADVICE_INFO_LIAR } from "@eggjs/tegg-types";
3
-
4
- //#region src/util/PointcutAdviceInfoUtil.ts
5
- var PointcutAdviceInfoUtil = class {
6
- static addPointcutAdviceInfo(adviceInfo, clazz, method) {
7
- MetadataUtil.initOwnArrayMetaData(POINTCUT_ADVICE_INFO_LIAR, clazz, []).unshift({
8
- method,
9
- adviceInfo
10
- });
11
- }
12
- static getPointcutAdviceInfoList(clazz, method) {
13
- return (MetadataUtil.getMetaData(POINTCUT_ADVICE_INFO_LIAR, clazz) || []).filter((t) => t.method === method).map((t) => t.adviceInfo);
14
- }
15
- };
16
-
17
- //#endregion
18
- export { PointcutAdviceInfoUtil };
@@ -1,6 +0,0 @@
1
- import { AdviceInfoUtil, IS_ADVICE } from "./AdviceInfoUtil.js";
2
- import { AspectInfoUtil } from "./AspectInfoUtil.js";
3
- import { CrosscutInfoUtil } from "./CrosscutInfoUtil.js";
4
- import { PointcutAdviceInfoUtil } from "./PointcutAdviceInfoUtil.js";
5
-
6
- export { };