forked from eggjs/tegg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
165 lines (148 loc) · 6.92 KB
/
app.ts
File metadata and controls
165 lines (148 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Application } from 'egg';
import { CONTROLLER_LOAD_UNIT, ControllerLoadUnit } from './lib/ControllerLoadUnit';
import { AppLoadUnitControllerHook } from './lib/AppLoadUnitControllerHook';
import { LoadUnitLifecycleContext } from '@eggjs/tegg-metadata';
import { ControllerMetaBuilderFactory, ControllerType } from '@eggjs/tegg';
import { HTTPControllerRegister } from './lib/impl/http/HTTPControllerRegister';
import { ControllerRegisterFactory } from './lib/ControllerRegisterFactory';
import { ControllerLoadUnitHandler } from './lib/ControllerLoadUnitHandler';
import { LoadUnitInstanceLifecycleContext, ModuleLoadUnitInstance } from '@eggjs/tegg-runtime';
import { ControllerMetadataManager } from './lib/ControllerMetadataManager';
import { EggControllerPrototypeHook } from './lib/EggControllerPrototypeHook';
import { RootProtoManager } from './lib/RootProtoManager';
import { EggControllerLoader } from './lib/EggControllerLoader';
import { MCPControllerRegister } from './lib/impl/mcp/MCPControllerRegister';
import assert from 'node:assert';
// Load Controller process
// 1. await add load unit is ready, controller may depend other load unit
// 2. load ${app_base_dir}app/controller file
// 3. ControllerRegister register controller implement
const majorVersion = parseInt(process.versions.node.split('.')[0], 10);
export default class ControllerAppBootHook {
private readonly app: Application;
private readonly loadUnitHook: AppLoadUnitControllerHook;
private readonly controllerRegisterFactory: ControllerRegisterFactory;
private controllerLoadUnitHandler: ControllerLoadUnitHandler;
private readonly controllerPrototypeHook: EggControllerPrototypeHook;
constructor(app: Application) {
this.app = app;
this.controllerRegisterFactory = new ControllerRegisterFactory(this.app);
this.app.rootProtoManager = new RootProtoManager();
this.app.controllerRegisterFactory = this.controllerRegisterFactory;
this.app.controllerMetaBuilderFactory = ControllerMetaBuilderFactory;
this.loadUnitHook = new AppLoadUnitControllerHook(this.controllerRegisterFactory, this.app.rootProtoManager);
this.controllerPrototypeHook = new EggControllerPrototypeHook();
}
configWillLoad() {
this.app.loadUnitLifecycleUtil.registerLifecycle(this.loadUnitHook);
this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.controllerPrototypeHook);
this.app.loaderFactory.registerLoader(CONTROLLER_LOAD_UNIT, unitPath => {
return new EggControllerLoader(unitPath);
});
this.controllerRegisterFactory.registerControllerRegister(ControllerType.HTTP, HTTPControllerRegister.create);
this.app.loadUnitFactory.registerLoadUnitCreator(
CONTROLLER_LOAD_UNIT,
(ctx: LoadUnitLifecycleContext): ControllerLoadUnit => {
return new ControllerLoadUnit(
`tegg-app-controller:${ctx.unitPath}`,
ctx.unitPath,
ctx.loader,
this.app.eggPrototypeFactory,
this.app.eggPrototypeCreatorFactory,
);
});
this.app.loadUnitInstanceFactory.registerLoadUnitInstanceClass(
CONTROLLER_LOAD_UNIT,
(ctx: LoadUnitInstanceLifecycleContext): ModuleLoadUnitInstance => {
return new ModuleLoadUnitInstance(ctx.loadUnit);
},
);
if (this.app.config.security?.csrf !== void 0) {
assert(typeof this.app.config.security.csrf === 'boolean' || typeof this.app.config.security.csrf === 'object', 'csrf must be boolean or object');
if (typeof this.app.config.security.csrf === 'boolean') {
this.app.config.security.csrf = {
enable: this.app.config.security.csrf,
};
}
}
// init http root proto middleware
this.prepareMiddleware(this.app.config.coreMiddleware);
if (this.mcpEnable()) {
this.controllerRegisterFactory.registerControllerRegister(ControllerType.MCP, MCPControllerRegister.create);
// Don't let the mcp's body be consumed
this.app.config.coreMiddleware.unshift('mcpBodyMiddleware');
if (this.app.config.security.csrf.ignore) {
if (Array.isArray(this.app.config.security.csrf.ignore)) {
this.app.config.security.csrf.ignore = [
/^\/mcp\//,
this.app.config.mcp.sseInitPath,
this.app.config.mcp.sseMessagePath,
this.app.config.mcp.streamPath,
this.app.config.mcp.statelessStreamPath,
...(Array.isArray(this.app.config.security.csrf.ignore)
? this.app.config.security.csrf.ignore
: [ this.app.config.security.csrf.ignore ]),
];
}
} else {
this.app.config.security.csrf.ignore = [
/^\/mcp\//,
this.app.config.mcp.sseInitPath,
this.app.config.mcp.sseMessagePath,
this.app.config.mcp.streamPath,
this.app.config.mcp.statelessStreamPath,
];
}
if (this.app.config.mcp.multipleServer) {
for (const name of Object.keys(this.app.config.mcp.multipleServer)) {
[ 'sseInitPath', 'sseMessagePath', 'streamPath', 'statelessStreamPath' ].forEach(key => {
if (this.app.config.mcp.multipleServer[name][key]) this.app.config.security.csrf.ignore.push(this.app.config.mcp.multipleServer[name][key]);
});
}
}
}
}
prepareMiddleware(middlewareNames: string[]) {
if (!middlewareNames.includes('teggCtxLifecycleMiddleware')) {
middlewareNames.unshift('teggCtxLifecycleMiddleware');
}
const index = middlewareNames.indexOf('teggCtxLifecycleMiddleware');
middlewareNames.splice(index, 0, 'teggRootProto');
return middlewareNames;
}
async didLoad() {
await this.app.moduleHandler.ready();
this.controllerLoadUnitHandler = new ControllerLoadUnitHandler(this.app);
await this.controllerLoadUnitHandler.ready();
// The real register HTTP controller/method.
// HTTP method should sort by priority
// The HTTPControllerRegister will collect all the methods
// and register methods after collect is done.
HTTPControllerRegister.instance?.doRegister(this.app.rootProtoManager);
this.app.config.mcp.hooks = MCPControllerRegister.hooks;
}
async willReady() {
if (this.mcpEnable()) {
await MCPControllerRegister.connectStatelessStreamTransport();
const names = MCPControllerRegister.instance?.mcpConfig.getMultipleServerNames();
if (names && names.length > 0) {
for (const name of names) {
await MCPControllerRegister.connectStatelessStreamTransport(name);
}
}
}
}
mcpEnable() {
return majorVersion >= 18 && !!this.app.plugins.mcpProxy?.enable;
}
async beforeClose() {
if (this.controllerLoadUnitHandler) {
await this.controllerLoadUnitHandler.destroy();
}
this.app.loadUnitLifecycleUtil.deleteLifecycle(this.loadUnitHook);
this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.controllerPrototypeHook);
ControllerMetadataManager.instance.clear();
HTTPControllerRegister.clean();
MCPControllerRegister.clean();
}
}