@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.
Files changed (97) hide show
  1. package/README.md +4 -0
  2. package/dist/controller/controller.d.ts.map +1 -1
  3. package/dist/core/application.d.ts +6 -1
  4. package/dist/core/application.d.ts.map +1 -1
  5. package/dist/core/server.d.ts +5 -0
  6. package/dist/core/server.d.ts.map +1 -1
  7. package/dist/database/database-context.d.ts +25 -0
  8. package/dist/database/database-context.d.ts.map +1 -0
  9. package/dist/database/database-extension.d.ts +8 -9
  10. package/dist/database/database-extension.d.ts.map +1 -1
  11. package/dist/database/database-module.d.ts +7 -1
  12. package/dist/database/database-module.d.ts.map +1 -1
  13. package/dist/database/db-proxy.d.ts +12 -0
  14. package/dist/database/db-proxy.d.ts.map +1 -0
  15. package/dist/database/index.d.ts +6 -1
  16. package/dist/database/index.d.ts.map +1 -1
  17. package/dist/database/orm/transaction-interceptor.d.ts +0 -16
  18. package/dist/database/orm/transaction-interceptor.d.ts.map +1 -1
  19. package/dist/database/orm/transaction-manager.d.ts +10 -61
  20. package/dist/database/orm/transaction-manager.d.ts.map +1 -1
  21. package/dist/database/service.d.ts.map +1 -1
  22. package/dist/database/sql-manager.d.ts +14 -0
  23. package/dist/database/sql-manager.d.ts.map +1 -0
  24. package/dist/database/sqlite-adapter.d.ts +32 -0
  25. package/dist/database/sqlite-adapter.d.ts.map +1 -0
  26. package/dist/database/strategy-decorator.d.ts +8 -0
  27. package/dist/database/strategy-decorator.d.ts.map +1 -0
  28. package/dist/database/types.d.ts +122 -1
  29. package/dist/database/types.d.ts.map +1 -1
  30. package/dist/di/container.d.ts +16 -0
  31. package/dist/di/container.d.ts.map +1 -1
  32. package/dist/di/lifecycle.d.ts +48 -0
  33. package/dist/di/lifecycle.d.ts.map +1 -1
  34. package/dist/di/module-registry.d.ts +10 -6
  35. package/dist/di/module-registry.d.ts.map +1 -1
  36. package/dist/index.d.ts +4 -4
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +3267 -2620
  39. package/dist/microservice/service-registry/service-registry-module.d.ts +16 -0
  40. package/dist/microservice/service-registry/service-registry-module.d.ts.map +1 -1
  41. package/dist/router/index.d.ts +1 -0
  42. package/dist/router/index.d.ts.map +1 -1
  43. package/dist/router/registry.d.ts +1 -1
  44. package/dist/router/registry.d.ts.map +1 -1
  45. package/dist/router/route.d.ts +2 -1
  46. package/dist/router/route.d.ts.map +1 -1
  47. package/dist/router/router.d.ts +1 -1
  48. package/dist/router/router.d.ts.map +1 -1
  49. package/dist/router/timeout-decorator.d.ts +6 -0
  50. package/dist/router/timeout-decorator.d.ts.map +1 -0
  51. package/docs/database.md +48 -15
  52. package/docs/idle-timeout.md +42 -0
  53. package/docs/lifecycle.md +80 -4
  54. package/docs/microservice-nacos.md +1 -0
  55. package/docs/microservice-service-registry.md +7 -0
  56. package/docs/zh/database.md +48 -15
  57. package/docs/zh/idle-timeout.md +41 -0
  58. package/docs/zh/lifecycle.md +49 -5
  59. package/docs/zh/microservice-nacos.md +1 -0
  60. package/docs/zh/microservice-service-registry.md +6 -0
  61. package/package.json +1 -1
  62. package/src/controller/controller.ts +11 -1
  63. package/src/core/application.ts +98 -26
  64. package/src/core/server.ts +10 -0
  65. package/src/database/database-context.ts +43 -0
  66. package/src/database/database-extension.ts +12 -45
  67. package/src/database/database-module.ts +254 -11
  68. package/src/database/db-proxy.ts +75 -0
  69. package/src/database/index.ts +29 -0
  70. package/src/database/orm/transaction-interceptor.ts +12 -149
  71. package/src/database/orm/transaction-manager.ts +143 -210
  72. package/src/database/service.ts +28 -2
  73. package/src/database/sql-manager.ts +62 -0
  74. package/src/database/sqlite-adapter.ts +121 -0
  75. package/src/database/strategy-decorator.ts +42 -0
  76. package/src/database/types.ts +133 -1
  77. package/src/di/container.ts +55 -1
  78. package/src/di/lifecycle.ts +114 -0
  79. package/src/di/module-registry.ts +78 -14
  80. package/src/index.ts +31 -1
  81. package/src/microservice/service-registry/service-registry-module.ts +25 -1
  82. package/src/router/index.ts +1 -0
  83. package/src/router/registry.ts +10 -1
  84. package/src/router/route.ts +31 -3
  85. package/src/router/router.ts +10 -1
  86. package/src/router/timeout-decorator.ts +35 -0
  87. package/tests/core/application.test.ts +10 -0
  88. package/tests/database/database-module.test.ts +91 -430
  89. package/tests/database/db-proxy.test.ts +93 -0
  90. package/tests/database/sql-manager.test.ts +43 -0
  91. package/tests/database/sqlite-adapter.test.ts +45 -0
  92. package/tests/database/strategy-decorator.test.ts +29 -0
  93. package/tests/database/transaction.test.ts +84 -222
  94. package/tests/di/lifecycle.test.ts +139 -1
  95. package/tests/di/scoped-lifecycle.test.ts +61 -0
  96. package/tests/microservice/service-registry.test.ts +15 -0
  97. package/tests/router/timeout-decorator.test.ts +48 -0
@@ -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
- if (!this.middlewarePipeline || !this.middlewarePipeline.hasMiddlewares()) {
128
- return await this.handler(context);
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
- return await this.middlewarePipeline.run(context, async () => this.handler(context));
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
  /**
@@ -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(method, normalizedPath, handler, middlewares, controllerClass, methodName);
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