-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add aop runtime/dynamic inject runtime for standalone (#149)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [x] `npm test` passes - [x] tests and/or benchmarks are included - [ ] documentation is changed or added - [x] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL -->
- Loading branch information
Showing
20 changed files
with
429 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { LifecycleHook } from '@eggjs/tegg-lifecycle'; | ||
import { | ||
EggPrototypeFactory, | ||
LoadUnit, | ||
LoadUnitLifecycleContext, | ||
EggPrototypeCreatorFactory, | ||
} from '@eggjs/tegg-metadata'; | ||
import { EggProtoImplClass } from '@eggjs/tegg'; | ||
import { EggObjectFactory } from '@eggjs/tegg-dynamic-inject-runtime'; | ||
|
||
const INNER_CLASS_LIST = [ | ||
EggObjectFactory, | ||
]; | ||
|
||
export class LoadUnitInnerClassHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> { | ||
async postCreate(_: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void> { | ||
if (loadUnit.type === 'StandaloneLoadUnitType') { | ||
for (const clazz of INNER_CLASS_LIST) { | ||
const protos = await EggPrototypeCreatorFactory.createProto(clazz as EggProtoImplClass, loadUnit); | ||
for (const proto of protos) { | ||
EggPrototypeFactory.instance.registerPrototype(proto, loadUnit); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
standalone/standalone/test/fixtures/aop-module/Hello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { | ||
ContextProto, | ||
Inject, | ||
SingletonProto, | ||
} from '@eggjs/tegg'; | ||
import { | ||
Advice, | ||
AdviceContext, | ||
Crosscut, | ||
IAdvice, | ||
Pointcut, | ||
PointcutType, | ||
} from '@eggjs/tegg/aop'; | ||
import assert from 'assert'; | ||
|
||
export interface CallTraceMsg { | ||
className: string; | ||
methodName: string; | ||
id: number; | ||
name: string; | ||
result?: string; | ||
adviceParams?: any; | ||
} | ||
|
||
@SingletonProto() | ||
export class CallTrace { | ||
msgs: Array<CallTraceMsg> = []; | ||
|
||
addMsg(msg: CallTraceMsg) { | ||
this.msgs.push(msg); | ||
} | ||
} | ||
|
||
export const pointcutAdviceParams = { | ||
point: Math.random() | ||
.toString(), | ||
cut: Math.random() | ||
.toString(), | ||
}; | ||
|
||
@Advice() | ||
export class PointcutAdvice implements IAdvice<Hello> { | ||
@Inject() | ||
callTrace: CallTrace; | ||
|
||
async beforeCall(ctx: AdviceContext<Hello>): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: PointcutAdvice.name, | ||
methodName: 'beforeCall', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async afterReturn(ctx: AdviceContext<Hello>, result: any): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: PointcutAdvice.name, | ||
methodName: 'afterReturn', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
result, | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async afterThrow(ctx: AdviceContext<Hello, any>, error: Error): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: PointcutAdvice.name, | ||
methodName: 'afterThrow', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
result: error.message, | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async afterFinally(ctx: AdviceContext<Hello>): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: PointcutAdvice.name, | ||
methodName: 'afterFinally', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async around(ctx: AdviceContext<Hello>, next: () => Promise<any>): Promise<any> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); | ||
ctx.args[0] = `withPointAroundParam(${ctx.args[0]})`; | ||
const result = await next(); | ||
return `withPointAroundResult(${result}${JSON.stringify(pointcutAdviceParams)})`; | ||
} | ||
} | ||
|
||
@ContextProto() | ||
export class Hello { | ||
id = 233; | ||
|
||
@Pointcut(PointcutAdvice, { adviceParams: pointcutAdviceParams }) | ||
async hello(name: string) { | ||
return `hello ${name}`; | ||
} | ||
|
||
@Pointcut(PointcutAdvice, { adviceParams: pointcutAdviceParams }) | ||
async helloWithException(name: string) { | ||
throw new Error(`ops, exception for ${name}`); | ||
} | ||
|
||
} | ||
|
||
export const crosscutAdviceParams = { | ||
cross: Math.random() | ||
.toString(), | ||
cut: Math.random() | ||
.toString(), | ||
}; | ||
|
||
@Crosscut({ | ||
type: PointcutType.CLASS, | ||
clazz: Hello, | ||
methodName: 'hello', | ||
}, { adviceParams: crosscutAdviceParams }) | ||
@Advice() | ||
export class CrosscutAdvice implements IAdvice<Hello, String> { | ||
@Inject() | ||
callTrace: CallTrace; | ||
|
||
async beforeCall(ctx: AdviceContext<Hello, {}>): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: CrosscutAdvice.name, | ||
methodName: 'beforeCall', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async afterReturn(ctx: AdviceContext<Hello>, result: any): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: CrosscutAdvice.name, | ||
methodName: 'afterReturn', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
result, | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async afterFinally(ctx: AdviceContext<Hello>): Promise<void> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); | ||
this.callTrace.addMsg({ | ||
className: CrosscutAdvice.name, | ||
methodName: 'afterFinally', | ||
id: ctx.that.id, | ||
name: ctx.args[0], | ||
adviceParams: ctx.adviceParams, | ||
}); | ||
} | ||
|
||
async around(ctx: AdviceContext<Hello>, next: () => Promise<any>): Promise<any> { | ||
assert.ok(ctx.adviceParams); | ||
assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); | ||
ctx.args[0] = `withCrosscutAroundParam(${ctx.args[0]})`; | ||
const result = await next(); | ||
return `withCrossAroundResult(${result}${JSON.stringify(ctx.adviceParams)})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ContextProto, Inject } from '@eggjs/tegg'; | ||
import { Runner, MainRunner } from '@eggjs/tegg/standalone'; | ||
import { Hello } from './Hello'; | ||
|
||
|
||
@Runner() | ||
@ContextProto() | ||
export class Foo implements MainRunner<string> { | ||
@Inject() | ||
hello: Hello; | ||
|
||
async main(): Promise<string> { | ||
return await this.hello.hello('aop'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "aop-module", | ||
"eggModule": { | ||
"name": "aopModule" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
standalone/standalone/test/fixtures/dynamic-inject-module/AbstractContextHello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export abstract class AbstractContextHello { | ||
abstract hello(): string; | ||
} |
3 changes: 3 additions & 0 deletions
3
standalone/standalone/test/fixtures/dynamic-inject-module/AbstractSingletonHello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export abstract class AbstractSingletonHello { | ||
abstract hello(): string; | ||
} |
9 changes: 9 additions & 0 deletions
9
standalone/standalone/test/fixtures/dynamic-inject-module/FooType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum ContextHelloType { | ||
FOO = 'FOO', | ||
BAR = 'BAR', | ||
} | ||
|
||
export enum SingletonHelloType { | ||
FOO = 'FOO', | ||
BAR = 'BAR', | ||
} |
21 changes: 21 additions & 0 deletions
21
standalone/standalone/test/fixtures/dynamic-inject-module/HelloService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ContextProto, Inject, EggObjectFactory } from '@eggjs/tegg'; | ||
import { ContextHelloType, SingletonHelloType } from './FooType'; | ||
import { AbstractContextHello } from './AbstractContextHello'; | ||
import { AbstractSingletonHello } from './AbstractSingletonHello'; | ||
|
||
@ContextProto() | ||
export class HelloService { | ||
@Inject() | ||
private readonly eggObjectFactory: EggObjectFactory; | ||
|
||
async hello(): Promise<string[]> { | ||
const helloImpls = await Promise.all([ | ||
this.eggObjectFactory.getEggObject(AbstractContextHello, ContextHelloType.FOO), | ||
this.eggObjectFactory.getEggObject(AbstractContextHello, ContextHelloType.BAR), | ||
this.eggObjectFactory.getEggObject(AbstractSingletonHello, SingletonHelloType.FOO), | ||
this.eggObjectFactory.getEggObject(AbstractSingletonHello, SingletonHelloType.BAR), | ||
]); | ||
const msgs = helloImpls.map(helloImpl => helloImpl.hello()); | ||
return msgs; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
standalone/standalone/test/fixtures/dynamic-inject-module/decorator/ContextHello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ContextHelloType } from '../FooType'; | ||
import { ImplDecorator, QualifierImplDecoratorUtil } from '@eggjs/tegg'; | ||
import { AbstractContextHello } from '../AbstractContextHello'; | ||
|
||
export const CONTEXT_HELLO_ATTRIBUTE = 'CONTEXT_HELLO_ATTRIBUTE'; | ||
|
||
export const ContextHello: ImplDecorator<AbstractContextHello, typeof ContextHelloType> = | ||
QualifierImplDecoratorUtil.generatorDecorator(AbstractContextHello, CONTEXT_HELLO_ATTRIBUTE); | ||
|
8 changes: 8 additions & 0 deletions
8
standalone/standalone/test/fixtures/dynamic-inject-module/decorator/SingletonHello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SingletonHelloType } from '../FooType'; | ||
import { ImplDecorator, QualifierImplDecoratorUtil } from '@eggjs/tegg'; | ||
import { AbstractSingletonHello } from '../AbstractSingletonHello'; | ||
|
||
export const SINGLETON_HELLO_ATTRIBUTE = 'SINGLETON_HELLO_ATTRIBUTE'; | ||
|
||
export const SingletonHello: ImplDecorator<AbstractSingletonHello, typeof SingletonHelloType> = | ||
QualifierImplDecoratorUtil.generatorDecorator(AbstractSingletonHello, SINGLETON_HELLO_ATTRIBUTE); |
Oops, something went wrong.