@dangao/bun-server 2.0.8 → 2.2.0
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/README.md +4 -0
- package/dist/controller/controller.d.ts.map +1 -1
- package/dist/core/application.d.ts +6 -1
- package/dist/core/application.d.ts.map +1 -1
- package/dist/core/server.d.ts +5 -0
- package/dist/core/server.d.ts.map +1 -1
- package/dist/database/database-context.d.ts +25 -0
- package/dist/database/database-context.d.ts.map +1 -0
- package/dist/database/database-extension.d.ts +8 -9
- package/dist/database/database-extension.d.ts.map +1 -1
- package/dist/database/database-module.d.ts +7 -1
- package/dist/database/database-module.d.ts.map +1 -1
- package/dist/database/db-proxy.d.ts +12 -0
- package/dist/database/db-proxy.d.ts.map +1 -0
- package/dist/database/index.d.ts +6 -1
- package/dist/database/index.d.ts.map +1 -1
- package/dist/database/orm/transaction-interceptor.d.ts +0 -16
- package/dist/database/orm/transaction-interceptor.d.ts.map +1 -1
- package/dist/database/orm/transaction-manager.d.ts +10 -61
- package/dist/database/orm/transaction-manager.d.ts.map +1 -1
- package/dist/database/service.d.ts.map +1 -1
- package/dist/database/sql-manager.d.ts +14 -0
- package/dist/database/sql-manager.d.ts.map +1 -0
- package/dist/database/sqlite-adapter.d.ts +32 -0
- package/dist/database/sqlite-adapter.d.ts.map +1 -0
- package/dist/database/strategy-decorator.d.ts +8 -0
- package/dist/database/strategy-decorator.d.ts.map +1 -0
- package/dist/database/types.d.ts +122 -1
- package/dist/database/types.d.ts.map +1 -1
- package/dist/di/container.d.ts +16 -0
- package/dist/di/container.d.ts.map +1 -1
- package/dist/di/lifecycle.d.ts +48 -0
- package/dist/di/lifecycle.d.ts.map +1 -1
- package/dist/di/module-registry.d.ts +10 -6
- package/dist/di/module-registry.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3267 -2620
- package/dist/microservice/service-registry/service-registry-module.d.ts +16 -0
- package/dist/microservice/service-registry/service-registry-module.d.ts.map +1 -1
- package/dist/router/index.d.ts +1 -0
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/registry.d.ts +1 -1
- package/dist/router/registry.d.ts.map +1 -1
- package/dist/router/route.d.ts +2 -1
- package/dist/router/route.d.ts.map +1 -1
- package/dist/router/router.d.ts +1 -1
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/timeout-decorator.d.ts +6 -0
- package/dist/router/timeout-decorator.d.ts.map +1 -0
- package/docs/database.md +48 -15
- package/docs/idle-timeout.md +42 -0
- package/docs/lifecycle.md +80 -4
- package/docs/microservice-nacos.md +1 -0
- package/docs/microservice-service-registry.md +7 -0
- package/docs/zh/database.md +48 -15
- package/docs/zh/idle-timeout.md +41 -0
- package/docs/zh/lifecycle.md +49 -5
- package/docs/zh/microservice-nacos.md +1 -0
- package/docs/zh/microservice-service-registry.md +6 -0
- package/package.json +1 -1
- package/src/controller/controller.ts +11 -1
- package/src/core/application.ts +98 -26
- package/src/core/server.ts +10 -0
- package/src/database/database-context.ts +43 -0
- package/src/database/database-extension.ts +12 -45
- package/src/database/database-module.ts +254 -11
- package/src/database/db-proxy.ts +75 -0
- package/src/database/index.ts +29 -0
- package/src/database/orm/transaction-interceptor.ts +12 -149
- package/src/database/orm/transaction-manager.ts +143 -210
- package/src/database/service.ts +28 -2
- package/src/database/sql-manager.ts +62 -0
- package/src/database/sqlite-adapter.ts +121 -0
- package/src/database/strategy-decorator.ts +42 -0
- package/src/database/types.ts +133 -1
- package/src/di/container.ts +55 -1
- package/src/di/lifecycle.ts +114 -0
- package/src/di/module-registry.ts +78 -14
- package/src/index.ts +31 -1
- package/src/microservice/service-registry/service-registry-module.ts +25 -1
- package/src/router/index.ts +1 -0
- package/src/router/registry.ts +10 -1
- package/src/router/route.ts +31 -3
- package/src/router/router.ts +10 -1
- package/src/router/timeout-decorator.ts +35 -0
- package/tests/core/application.test.ts +10 -0
- package/tests/database/database-module.test.ts +91 -430
- package/tests/database/db-proxy.test.ts +93 -0
- package/tests/database/sql-manager.test.ts +43 -0
- package/tests/database/sqlite-adapter.test.ts +45 -0
- package/tests/database/strategy-decorator.test.ts +29 -0
- package/tests/database/transaction.test.ts +84 -222
- package/tests/di/lifecycle.test.ts +139 -1
- package/tests/di/scoped-lifecycle.test.ts +61 -0
- package/tests/microservice/service-registry.test.ts +15 -0
- package/tests/router/timeout-decorator.test.ts +48 -0
package/src/router/route.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { Constructor } from '../core/types';
|
|
|
3
3
|
import type { HttpMethod, RouteHandler, RouteMatch } from './types';
|
|
4
4
|
import { MiddlewarePipeline } from '../middleware/pipeline';
|
|
5
5
|
import type { Middleware } from '../middleware';
|
|
6
|
+
import { HttpException } from '../error';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 路由类
|
|
@@ -47,6 +48,7 @@ export class Route {
|
|
|
47
48
|
private readonly middlewarePipeline: MiddlewarePipeline | null;
|
|
48
49
|
private readonly staticKey?: string;
|
|
49
50
|
public readonly isStatic: boolean;
|
|
51
|
+
private readonly timeout?: number;
|
|
50
52
|
|
|
51
53
|
public constructor(
|
|
52
54
|
method: HttpMethod,
|
|
@@ -55,12 +57,14 @@ export class Route {
|
|
|
55
57
|
middlewares: Middleware[] = [],
|
|
56
58
|
controllerClass?: Constructor<unknown>,
|
|
57
59
|
methodName?: string,
|
|
60
|
+
timeout?: number,
|
|
58
61
|
) {
|
|
59
62
|
this.method = method;
|
|
60
63
|
this.path = path;
|
|
61
64
|
this.handler = handler;
|
|
62
65
|
this.controllerClass = controllerClass;
|
|
63
66
|
this.methodName = methodName;
|
|
67
|
+
this.timeout = timeout;
|
|
64
68
|
|
|
65
69
|
this.isStatic = !path.includes(':') && !path.includes('*');
|
|
66
70
|
if (this.isStatic) {
|
|
@@ -124,11 +128,35 @@ export class Route {
|
|
|
124
128
|
* @returns 响应对象
|
|
125
129
|
*/
|
|
126
130
|
public async execute(context: Context): Promise<Response> {
|
|
127
|
-
|
|
128
|
-
|
|
131
|
+
const executeHandler = async (): Promise<Response> => {
|
|
132
|
+
if (!this.middlewarePipeline || !this.middlewarePipeline.hasMiddlewares()) {
|
|
133
|
+
return await this.handler(context);
|
|
134
|
+
}
|
|
135
|
+
return await this.middlewarePipeline.run(
|
|
136
|
+
context,
|
|
137
|
+
async () => await this.handler(context),
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
if (!this.timeout || this.timeout <= 0) {
|
|
142
|
+
return await executeHandler();
|
|
129
143
|
}
|
|
130
144
|
|
|
131
|
-
|
|
145
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
146
|
+
const timeoutPromise = new Promise<Response>((_, reject) => {
|
|
147
|
+
timer = setTimeout(
|
|
148
|
+
() => reject(new HttpException(408, 'Request Timeout')),
|
|
149
|
+
this.timeout,
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
return await Promise.race([executeHandler(), timeoutPromise]);
|
|
155
|
+
} finally {
|
|
156
|
+
if (timer) {
|
|
157
|
+
clearTimeout(timer);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
132
160
|
}
|
|
133
161
|
|
|
134
162
|
/**
|
package/src/router/router.ts
CHANGED
|
@@ -49,10 +49,19 @@ export class Router {
|
|
|
49
49
|
middlewares: Middleware[] = [],
|
|
50
50
|
controllerClass?: Constructor<unknown>,
|
|
51
51
|
methodName?: string,
|
|
52
|
+
timeout?: number,
|
|
52
53
|
): void {
|
|
53
54
|
// 规范化路径
|
|
54
55
|
const normalizedPath = this.normalizePath(path);
|
|
55
|
-
const route = new Route(
|
|
56
|
+
const route = new Route(
|
|
57
|
+
method,
|
|
58
|
+
normalizedPath,
|
|
59
|
+
handler,
|
|
60
|
+
middlewares,
|
|
61
|
+
controllerClass,
|
|
62
|
+
methodName,
|
|
63
|
+
timeout,
|
|
64
|
+
);
|
|
56
65
|
this.routes.push(route);
|
|
57
66
|
const staticKey = route.getStaticKey();
|
|
58
67
|
if (staticKey) {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
|
|
3
|
+
import type { Constructor } from '../core/types';
|
|
4
|
+
|
|
5
|
+
export const IDLE_TIMEOUT_KEY = Symbol('@dangao/bun-server:route:idle-timeout');
|
|
6
|
+
|
|
7
|
+
export function IdleTimeout(ms: number): MethodDecorator & ClassDecorator {
|
|
8
|
+
return (target: object, propertyKey?: string | symbol) => {
|
|
9
|
+
if (propertyKey !== undefined) {
|
|
10
|
+
Reflect.defineMetadata(IDLE_TIMEOUT_KEY, ms, target, propertyKey);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
Reflect.defineMetadata(IDLE_TIMEOUT_KEY, ms, target);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getIdleTimeout(
|
|
18
|
+
controllerClass: Constructor<unknown>,
|
|
19
|
+
methodName: string,
|
|
20
|
+
): number | undefined {
|
|
21
|
+
const methodTimeout = Reflect.getMetadata(
|
|
22
|
+
IDLE_TIMEOUT_KEY,
|
|
23
|
+
controllerClass.prototype,
|
|
24
|
+
methodName,
|
|
25
|
+
) as number | undefined;
|
|
26
|
+
if (typeof methodTimeout === 'number') {
|
|
27
|
+
return methodTimeout;
|
|
28
|
+
}
|
|
29
|
+
const classTimeout = Reflect.getMetadata(
|
|
30
|
+
IDLE_TIMEOUT_KEY,
|
|
31
|
+
controllerClass,
|
|
32
|
+
) as number | undefined;
|
|
33
|
+
return typeof classTimeout === 'number' ? classTimeout : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
@@ -87,5 +87,15 @@ describe('Application', () => {
|
|
|
87
87
|
await app2.stop();
|
|
88
88
|
}
|
|
89
89
|
});
|
|
90
|
+
|
|
91
|
+
test('should pass idleTimeout to BunServer options', async () => {
|
|
92
|
+
const port = getTestPort();
|
|
93
|
+
app = new Application({ port, idleTimeout: 8 });
|
|
94
|
+
await app.listen();
|
|
95
|
+
|
|
96
|
+
const server = app.getServer() as any;
|
|
97
|
+
expect(server).toBeDefined();
|
|
98
|
+
expect(server.options.idleTimeout).toBe(8);
|
|
99
|
+
});
|
|
90
100
|
});
|
|
91
101
|
|