@gravito/ripple 3.1.0 → 4.0.1
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 +260 -19
- package/dist/atlas/src/DB.d.ts +348 -0
- package/dist/atlas/src/OrbitAtlas.d.ts +9 -0
- package/dist/atlas/src/config/defineConfig.d.ts +14 -0
- package/dist/atlas/src/config/index.d.ts +7 -0
- package/dist/atlas/src/config/loadConfig.d.ts +41 -0
- package/dist/atlas/src/connection/Connection.d.ts +112 -0
- package/dist/atlas/src/connection/ConnectionManager.d.ts +180 -0
- package/dist/atlas/src/connection/ReplicaConnectionPool.d.ts +54 -0
- package/dist/atlas/src/drivers/BunSQLDriver.d.ts +32 -0
- package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +118 -0
- package/dist/atlas/src/drivers/MongoDBDriver.d.ts +36 -0
- package/dist/atlas/src/drivers/MySQLDriver.d.ts +79 -0
- package/dist/atlas/src/drivers/PostgresDriver.d.ts +96 -0
- package/dist/atlas/src/drivers/RedisDriver.d.ts +43 -0
- package/dist/atlas/src/drivers/SQLiteDriver.d.ts +45 -0
- package/dist/atlas/src/drivers/types.d.ts +260 -0
- package/dist/atlas/src/errors/index.d.ts +45 -0
- package/dist/atlas/src/grammar/Grammar.d.ts +342 -0
- package/dist/atlas/src/grammar/MongoGrammar.d.ts +47 -0
- package/dist/atlas/src/grammar/MySQLGrammar.d.ts +54 -0
- package/dist/atlas/src/grammar/NullGrammar.d.ts +35 -0
- package/dist/atlas/src/grammar/PostgresGrammar.d.ts +62 -0
- package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +32 -0
- package/dist/atlas/src/index.d.ts +79 -0
- package/dist/atlas/src/migration/Migration.d.ts +64 -0
- package/dist/atlas/src/migration/MigrationRepository.d.ts +65 -0
- package/dist/atlas/src/migration/Migrator.d.ts +110 -0
- package/dist/atlas/src/migration/index.d.ts +6 -0
- package/dist/atlas/src/observability/AtlasMetrics.d.ts +33 -0
- package/dist/atlas/src/observability/AtlasObservability.d.ts +15 -0
- package/dist/atlas/src/observability/AtlasTracer.d.ts +12 -0
- package/dist/atlas/src/observability/index.d.ts +9 -0
- package/dist/atlas/src/orm/Repository.d.ts +247 -0
- package/dist/atlas/src/orm/index.d.ts +6 -0
- package/dist/atlas/src/orm/model/DirtyTracker.d.ts +121 -0
- package/dist/atlas/src/orm/model/Model.d.ts +458 -0
- package/dist/atlas/src/orm/model/ModelRegistry.d.ts +20 -0
- package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +150 -0
- package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +36 -0
- package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +92 -0
- package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +117 -0
- package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +64 -0
- package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +15 -0
- package/dist/atlas/src/orm/model/concerns/index.d.ts +12 -0
- package/dist/atlas/src/orm/model/decorators.d.ts +138 -0
- package/dist/atlas/src/orm/model/errors.d.ts +52 -0
- package/dist/atlas/src/orm/model/index.d.ts +10 -0
- package/dist/atlas/src/orm/model/relationships.d.ts +207 -0
- package/dist/atlas/src/orm/model/types.d.ts +12 -0
- package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +124 -0
- package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +54 -0
- package/dist/atlas/src/orm/schema/index.d.ts +6 -0
- package/dist/atlas/src/orm/schema/types.d.ts +85 -0
- package/dist/atlas/src/pool/AdaptivePoolManager.d.ts +98 -0
- package/dist/atlas/src/pool/PoolHealthChecker.d.ts +91 -0
- package/dist/atlas/src/pool/PoolStrategy.d.ts +129 -0
- package/dist/atlas/src/pool/PoolWarmer.d.ts +92 -0
- package/dist/atlas/src/query/Expression.d.ts +60 -0
- package/dist/atlas/src/query/NPlusOneDetector.d.ts +10 -0
- package/dist/atlas/src/query/QueryBuilder.d.ts +643 -0
- package/dist/atlas/src/query/RelationshipResolver.d.ts +23 -0
- package/dist/atlas/src/query/clauses/GroupByClause.d.ts +51 -0
- package/dist/atlas/src/query/clauses/HavingClause.d.ts +70 -0
- package/dist/atlas/src/query/clauses/JoinClause.d.ts +87 -0
- package/dist/atlas/src/query/clauses/LimitClause.d.ts +82 -0
- package/dist/atlas/src/query/clauses/OrderByClause.d.ts +69 -0
- package/dist/atlas/src/query/clauses/SelectClause.d.ts +71 -0
- package/dist/atlas/src/query/clauses/WhereClause.d.ts +167 -0
- package/dist/atlas/src/query/clauses/index.d.ts +11 -0
- package/dist/atlas/src/schema/Blueprint.d.ts +276 -0
- package/dist/atlas/src/schema/ColumnDefinition.d.ts +154 -0
- package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +37 -0
- package/dist/atlas/src/schema/MigrationGenerator.d.ts +45 -0
- package/dist/atlas/src/schema/Schema.d.ts +131 -0
- package/dist/atlas/src/schema/SchemaDiff.d.ts +73 -0
- package/dist/atlas/src/schema/TypeGenerator.d.ts +57 -0
- package/dist/atlas/src/schema/TypeWriter.d.ts +42 -0
- package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +23 -0
- package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +26 -0
- package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +28 -0
- package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +97 -0
- package/dist/atlas/src/schema/grammars/index.d.ts +7 -0
- package/dist/atlas/src/schema/index.d.ts +8 -0
- package/dist/atlas/src/seed/Factory.d.ts +90 -0
- package/dist/atlas/src/seed/Seeder.d.ts +28 -0
- package/dist/atlas/src/seed/SeederRunner.d.ts +74 -0
- package/dist/atlas/src/seed/index.d.ts +6 -0
- package/dist/atlas/src/sharding/ShardingManager.d.ts +59 -0
- package/dist/atlas/src/types/index.d.ts +1182 -0
- package/dist/atlas/src/utils/CursorEncoding.d.ts +63 -0
- package/dist/atlas/src/utils/levenshtein.d.ts +9 -0
- package/dist/core/src/CommandKernel.d.ts +33 -0
- package/dist/core/src/ConfigManager.d.ts +39 -0
- package/dist/core/src/Container/RequestScopeManager.d.ts +62 -0
- package/dist/core/src/Container/RequestScopeMetrics.d.ts +144 -0
- package/dist/core/src/Container.d.ts +86 -11
- package/dist/core/src/ErrorHandler.d.ts +3 -0
- package/dist/core/src/HookManager.d.ts +511 -4
- package/dist/core/src/PlanetCore.d.ts +89 -0
- package/dist/core/src/RequestContext.d.ts +97 -0
- package/dist/core/src/Router.d.ts +1 -5
- package/dist/core/src/ServiceProvider.d.ts +22 -0
- package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +1 -0
- package/dist/core/src/adapters/PhotonAdapter.d.ts +5 -0
- package/dist/core/src/adapters/bun/BunContext.d.ts +4 -0
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +1 -0
- package/dist/core/src/adapters/types.d.ts +27 -0
- package/dist/core/src/cli/queue-commands.d.ts +6 -0
- package/dist/core/src/engine/AOTRouter.d.ts +7 -12
- package/dist/core/src/engine/FastContext.d.ts +27 -2
- package/dist/core/src/engine/Gravito.d.ts +0 -1
- package/dist/core/src/engine/MinimalContext.d.ts +25 -2
- package/dist/core/src/engine/types.d.ts +9 -1
- package/dist/core/src/error-handling/RequestScopeErrorContext.d.ts +126 -0
- package/dist/core/src/events/BackpressureManager.d.ts +215 -0
- package/dist/core/src/events/CircuitBreaker.d.ts +229 -0
- package/dist/core/src/events/DeadLetterQueue.d.ts +219 -0
- package/dist/core/src/events/EventBackend.d.ts +12 -0
- package/dist/core/src/events/EventOptions.d.ts +204 -0
- package/dist/core/src/events/EventPriorityQueue.d.ts +301 -0
- package/dist/core/src/events/FlowControlStrategy.d.ts +109 -0
- package/dist/core/src/events/IdempotencyCache.d.ts +60 -0
- package/dist/core/src/events/MessageQueueBridge.d.ts +184 -0
- package/dist/core/src/events/PriorityEscalationManager.d.ts +82 -0
- package/dist/core/src/events/RetryScheduler.d.ts +104 -0
- package/dist/core/src/events/WorkerPool.d.ts +98 -0
- package/dist/core/src/events/WorkerPoolConfig.d.ts +153 -0
- package/dist/core/src/events/WorkerPoolMetrics.d.ts +65 -0
- package/dist/core/src/events/aggregation/AggregationWindow.d.ts +77 -0
- package/dist/core/src/events/aggregation/DeduplicationManager.d.ts +135 -0
- package/dist/core/src/events/aggregation/EventAggregationManager.d.ts +108 -0
- package/dist/core/src/events/aggregation/EventBatcher.d.ts +99 -0
- package/dist/core/src/events/aggregation/types.d.ts +117 -0
- package/dist/core/src/events/index.d.ts +25 -0
- package/dist/core/src/events/observability/EventMetrics.d.ts +132 -0
- package/dist/core/src/events/observability/EventTracer.d.ts +68 -0
- package/dist/core/src/events/observability/EventTracing.d.ts +161 -0
- package/dist/core/src/events/observability/OTelEventMetrics.d.ts +332 -0
- package/dist/core/src/events/observability/ObservableHookManager.d.ts +108 -0
- package/dist/core/src/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/core/src/events/observability/index.d.ts +24 -0
- package/dist/core/src/events/observability/metrics-types.d.ts +16 -0
- package/dist/core/src/events/types.d.ts +134 -0
- package/dist/core/src/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/core/src/exceptions/index.d.ts +1 -0
- package/dist/core/src/health/HealthProvider.d.ts +67 -0
- package/dist/core/src/http/types.d.ts +40 -0
- package/dist/core/src/index.d.ts +25 -4
- package/dist/core/src/instrumentation/index.d.ts +35 -0
- package/dist/core/src/instrumentation/opentelemetry.d.ts +178 -0
- package/dist/core/src/instrumentation/types.d.ts +182 -0
- package/dist/core/src/observability/Metrics.d.ts +244 -0
- package/dist/core/src/observability/QueueDashboard.d.ts +136 -0
- package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +350 -0
- package/dist/core/src/reliability/RetryPolicy.d.ts +217 -0
- package/dist/core/src/reliability/index.d.ts +6 -0
- package/dist/core/src/router/ControllerDispatcher.d.ts +12 -0
- package/dist/core/src/router/RequestValidator.d.ts +20 -0
- package/dist/index.js +6709 -9888
- package/dist/index.js.map +64 -62
- package/dist/photon/src/index.d.ts +19 -0
- package/dist/photon/src/middleware/ratelimit-redis.d.ts +50 -0
- package/dist/photon/src/middleware/ratelimit.d.ts +161 -0
- package/dist/photon/src/openapi.d.ts +19 -0
- package/dist/proto/ripple.proto +120 -0
- package/dist/ripple/src/RippleServer.d.ts +64 -445
- package/dist/ripple/src/channels/ChannelManager.d.ts +25 -10
- package/dist/ripple/src/drivers/NATSDriver.d.ts +87 -0
- package/dist/ripple/src/drivers/RedisDriver.d.ts +30 -1
- package/dist/ripple/src/drivers/index.d.ts +1 -0
- package/dist/ripple/src/engines/BunEngine.d.ts +98 -0
- package/dist/ripple/src/engines/IRippleEngine.d.ts +205 -0
- package/dist/ripple/src/engines/UWebSocketsEngine.d.ts +97 -0
- package/dist/ripple/src/engines/WsEngine.d.ts +69 -0
- package/dist/ripple/src/engines/index.d.ts +15 -0
- package/dist/ripple/src/index.d.ts +2 -0
- package/dist/ripple/src/middleware/InterceptorManager.d.ts +21 -0
- package/dist/ripple/src/observability/RippleMetrics.d.ts +24 -0
- package/dist/ripple/src/reliability/AckManager.d.ts +48 -0
- package/dist/ripple/src/serializers/ISerializer.d.ts +39 -0
- package/dist/ripple/src/serializers/JsonSerializer.d.ts +19 -0
- package/dist/ripple/src/serializers/ProtobufSerializer.d.ts +41 -0
- package/dist/ripple/src/serializers/index.d.ts +3 -0
- package/dist/ripple/src/tracking/SessionManager.d.ts +104 -0
- package/dist/ripple/src/tracking/index.d.ts +1 -0
- package/dist/ripple/src/types.d.ts +188 -12
- package/dist/ripple/src/utils/MessageSerializer.d.ts +33 -23
- package/dist/ripple/src/utils/TokenBucket.d.ts +25 -0
- package/package.json +25 -8
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview RequestContext - AsyncLocalStorage-based request context management
|
|
3
|
+
*
|
|
4
|
+
* Allows deep service layers to access request-scoped data (requestId, userId, etc.)
|
|
5
|
+
* without passing parameters through the call stack.
|
|
6
|
+
*
|
|
7
|
+
* @module @gravito/core/RequestContext
|
|
8
|
+
* @since 2.2.0
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* 請求上下文資料介面
|
|
12
|
+
* 包含請求相關的唯一識別碼和上下文資訊
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface RequestContextData {
|
|
16
|
+
/** 唯一的請求識別碼 */
|
|
17
|
+
requestId: string;
|
|
18
|
+
/** 使用者 ID(可選) */
|
|
19
|
+
userId?: string;
|
|
20
|
+
/** 租戶 ID(可選) */
|
|
21
|
+
tenantId?: string;
|
|
22
|
+
/** 追蹤 ID(可選) */
|
|
23
|
+
traceId?: string;
|
|
24
|
+
/** 自訂欄位 */
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* RequestContext 物件 - 管理請求上下文的 API
|
|
29
|
+
*
|
|
30
|
+
* 提供在非同步鏈中存取和設定請求上下文的方法
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export declare const RequestContext: {
|
|
34
|
+
/**
|
|
35
|
+
* 在給定的上下文中執行函式
|
|
36
|
+
*
|
|
37
|
+
* 確保函式及其所有非同步呼叫都在同一個上下文中執行
|
|
38
|
+
*
|
|
39
|
+
* @param data - 請求上下文資料
|
|
40
|
+
* @param fn - 要執行的函式(可以是非同步的或同步的)
|
|
41
|
+
* @returns 如果函式是非同步的,返回 Promise;如果是同步的,直接返回值
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* await RequestContext.run({ requestId: 'req-123' }, async () => {
|
|
46
|
+
* const userId = RequestContext.get()?.userId;
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* const result = RequestContext.run({ requestId: 'req-123' }, () => {
|
|
50
|
+
* return 'sync-result';
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
run<T>(data: RequestContextData, fn: () => T | Promise<T>): T | Promise<T>;
|
|
55
|
+
/**
|
|
56
|
+
* 獲取當前請求的上下文資料
|
|
57
|
+
*
|
|
58
|
+
* @returns 請求上下文資料,如果不在請求上下文中則返回 undefined
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const context = RequestContext.get();
|
|
63
|
+
* if (context) {
|
|
64
|
+
* console.log(context.requestId);
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
get(): RequestContextData | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* 獲取當前請求的上下文資料,如果不存在則拋出錯誤
|
|
71
|
+
*
|
|
72
|
+
* @returns 請求上下文資料
|
|
73
|
+
* @throws Error 如果不在請求上下文中
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const context = RequestContext.getOrThrow();
|
|
78
|
+
* console.log(context.requestId); // 保證不為 undefined
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
getOrThrow(): RequestContextData;
|
|
82
|
+
/**
|
|
83
|
+
* 在當前上下文中設定或修改值
|
|
84
|
+
*
|
|
85
|
+
* @param key - 要設定的欄位名稱
|
|
86
|
+
* @param value - 要設定的值
|
|
87
|
+
* @throws Error 如果不在請求上下文中
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* RequestContext.set('userId', 'user-456');
|
|
92
|
+
* const userId = RequestContext.get()?.userId; // 'user-456'
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
set(key: string, value: unknown): void;
|
|
96
|
+
};
|
|
97
|
+
export default RequestContext;
|
|
@@ -125,7 +125,7 @@ export declare class Router {
|
|
|
125
125
|
path: string;
|
|
126
126
|
domain?: string;
|
|
127
127
|
}>;
|
|
128
|
-
private
|
|
128
|
+
private dispatcher;
|
|
129
129
|
private namedRoutes;
|
|
130
130
|
private bindings;
|
|
131
131
|
/**
|
|
@@ -268,10 +268,6 @@ export declare class Router {
|
|
|
268
268
|
* Internal Request Registration
|
|
269
269
|
*/
|
|
270
270
|
req(method: HttpMethod, path: string, requestOrHandlerOrMiddleware: FormRequestClass | RouteHandler | GravitoMiddleware | GravitoMiddleware[], handler?: RouteHandler, options?: RouteOptions): Route;
|
|
271
|
-
/**
|
|
272
|
-
* Resolve Controller Instance and Method
|
|
273
|
-
*/
|
|
274
|
-
private resolveControllerHandler;
|
|
275
271
|
}
|
|
276
272
|
/**
|
|
277
273
|
* Standard RESTful resource action names.
|
|
@@ -74,6 +74,28 @@ export declare abstract class ServiceProvider {
|
|
|
74
74
|
* @param core - The PlanetCore application instance
|
|
75
75
|
*/
|
|
76
76
|
boot?(core: PlanetCore): void | Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Called when the application is ready to accept requests.
|
|
79
|
+
*
|
|
80
|
+
* This method is called after ALL providers have booted.
|
|
81
|
+
* Use this for final initialization before the server starts accepting traffic.
|
|
82
|
+
*
|
|
83
|
+
* @param core - The PlanetCore application instance
|
|
84
|
+
* @since 2.2.0
|
|
85
|
+
*/
|
|
86
|
+
onReady?(core: PlanetCore): void | Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Called when the application is shutting down.
|
|
89
|
+
*
|
|
90
|
+
* This method is called during graceful shutdown.
|
|
91
|
+
* Use this to clean up resources, close connections, etc.
|
|
92
|
+
*
|
|
93
|
+
* Providers are called in reverse order (LIFO).
|
|
94
|
+
*
|
|
95
|
+
* @param core - The PlanetCore application instance
|
|
96
|
+
* @since 2.2.0
|
|
97
|
+
*/
|
|
98
|
+
onShutdown?(core: PlanetCore): void | Promise<void>;
|
|
77
99
|
/**
|
|
78
100
|
* Set the core instance reference.
|
|
79
101
|
* Called internally by the application during registration.
|
|
@@ -23,4 +23,5 @@ export declare class GravitoEngineAdapter<V extends GravitoVariables = GravitoVa
|
|
|
23
23
|
fetch: (request: Request, _server?: unknown) => Response | Promise<Response>;
|
|
24
24
|
warmup(paths: string[]): Promise<void>;
|
|
25
25
|
createContext(_request: Request): GravitoContext<V>;
|
|
26
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
26
27
|
}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* @since 2.0.0
|
|
9
9
|
*/
|
|
10
10
|
import type { Context, Handler, MiddlewareHandler, Photon } from '@gravito/photon';
|
|
11
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
11
12
|
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoRequest, GravitoVariables, HttpMethod, ProxyOptions, StatusCode } from '../http/types';
|
|
12
13
|
import type { AdapterConfig, HttpAdapter, RouteDefinition } from './types';
|
|
13
14
|
/**
|
|
@@ -50,6 +51,7 @@ declare class PhotonRequestWrapper implements GravitoRequest {
|
|
|
50
51
|
declare class PhotonContextWrapper<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
|
|
51
52
|
private _req;
|
|
52
53
|
private photonCtx;
|
|
54
|
+
private _requestScope;
|
|
53
55
|
route: (name: string, params?: Record<string, any>, query?: Record<string, any>) => string;
|
|
54
56
|
/**
|
|
55
57
|
* Reset the wrapper for pooling
|
|
@@ -90,6 +92,8 @@ declare class PhotonContextWrapper<V extends GravitoVariables = GravitoVariables
|
|
|
90
92
|
get env(): Record<string, unknown> | undefined;
|
|
91
93
|
get native(): Context;
|
|
92
94
|
forward(target: string, options?: ProxyOptions): Promise<Response>;
|
|
95
|
+
requestScope(): RequestScopeManager;
|
|
96
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
93
97
|
}
|
|
94
98
|
/**
|
|
95
99
|
* Convert a GravitoHandler to a Photon Handler
|
|
@@ -136,6 +140,7 @@ export declare class PhotonAdapter<V extends GravitoVariables = GravitoVariables
|
|
|
136
140
|
routes(routes: RouteDefinition[]): void;
|
|
137
141
|
use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
138
142
|
useGlobal(...middleware: GravitoMiddleware<V>[]): void;
|
|
143
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
139
144
|
mount(path: string, subAdapter: HttpAdapter<V>): void;
|
|
140
145
|
onError(handler: GravitoErrorHandler<V>): void;
|
|
141
146
|
onNotFound(handler: GravitoNotFoundHandler<V>): void;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RequestScopeManager } from '../../Container/RequestScopeManager';
|
|
1
2
|
import type { ContentfulStatusCode, GravitoContext, GravitoVariables, ProxyOptions, StatusCode } from '../../http/types';
|
|
2
3
|
import { BunRequest } from './BunRequest';
|
|
3
4
|
/**
|
|
@@ -8,6 +9,7 @@ export declare class BunContext<V extends GravitoVariables = GravitoVariables> i
|
|
|
8
9
|
readonly env: Record<string, unknown>;
|
|
9
10
|
readonly req: BunRequest;
|
|
10
11
|
private _variables;
|
|
12
|
+
private _requestScope;
|
|
11
13
|
/**
|
|
12
14
|
* URL generator helper
|
|
13
15
|
*/
|
|
@@ -42,4 +44,6 @@ export declare class BunContext<V extends GravitoVariables = GravitoVariables> i
|
|
|
42
44
|
get<K extends keyof V>(key: K): V[K];
|
|
43
45
|
set<K extends keyof V>(key: K, value: V[K]): void;
|
|
44
46
|
get executionCtx(): ExecutionContext | undefined;
|
|
47
|
+
requestScope(): RequestScopeManager;
|
|
48
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
45
49
|
}
|
|
@@ -17,6 +17,7 @@ export declare class BunNativeAdapter implements HttpAdapter {
|
|
|
17
17
|
routes(routes: RouteDefinition[]): void;
|
|
18
18
|
use(path: string, ...middleware: GravitoMiddleware[]): void;
|
|
19
19
|
useGlobal(...middleware: GravitoMiddleware[]): void;
|
|
20
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware[]): void;
|
|
20
21
|
mount(path: string, subAdapter: HttpAdapter): void;
|
|
21
22
|
createContext(request: Request): GravitoContext;
|
|
22
23
|
onError(handler: GravitoErrorHandler): void;
|
|
@@ -115,6 +115,33 @@ export interface HttpAdapter<V extends GravitoVariables = GravitoVariables> {
|
|
|
115
115
|
* @param middleware - Middleware function
|
|
116
116
|
*/
|
|
117
117
|
useGlobal(...middleware: GravitoMiddleware<V>[]): void;
|
|
118
|
+
/**
|
|
119
|
+
* Register a scoped middleware for Orbit-level isolation
|
|
120
|
+
*
|
|
121
|
+
* Unlike regular `use()`, this method enforces stricter scoping rules:
|
|
122
|
+
* - REJECTS '*' wildcard paths (prevents global middleware in Orbits)
|
|
123
|
+
* - ENFORCES that all middleware paths must include the scope prefix
|
|
124
|
+
* - Throws error if attempting to register global middleware within an Orbit scope
|
|
125
|
+
*
|
|
126
|
+
* This is designed to prevent accidental middleware cross-contamination
|
|
127
|
+
* when multiple Orbits are mounted to a single PlanetCore instance.
|
|
128
|
+
*
|
|
129
|
+
* @param scope - The scope/path prefix (e.g., '/api', '/blog')
|
|
130
|
+
* @param path - Path pattern to match (cannot be '*')
|
|
131
|
+
* @param middleware - One or more middleware functions
|
|
132
|
+
* @throws {Error} If path is '*' when in Orbit scope
|
|
133
|
+
* @since 2.3.0
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* // Correct: Scoped to specific path
|
|
138
|
+
* adapter.useScoped('/api', '/users/*', authMiddleware)
|
|
139
|
+
*
|
|
140
|
+
* // Error: Cannot use wildcard in Orbit scope
|
|
141
|
+
* adapter.useScoped('/api', '*', loggerMiddleware)
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
118
145
|
/**
|
|
119
146
|
* Mount a sub-adapter at a path
|
|
120
147
|
*
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CommandKernel } from '../CommandKernel';
|
|
2
|
+
import type { QueueDashboard } from '../observability/QueueDashboard';
|
|
3
|
+
/**
|
|
4
|
+
* Register queue management commands with CommandKernel
|
|
5
|
+
*/
|
|
6
|
+
export declare function registerQueueCommands(kernel: CommandKernel, dashboard: QueueDashboard): void;
|
|
@@ -34,9 +34,15 @@ export declare class AOTRouter {
|
|
|
34
34
|
readonly globalMiddleware: Middleware[];
|
|
35
35
|
/** @internal */
|
|
36
36
|
readonly pathMiddleware: Map<string, Middleware[]>;
|
|
37
|
+
private dynamicRoutePatterns;
|
|
37
38
|
private middlewareCache;
|
|
38
39
|
private cacheMaxSize;
|
|
39
|
-
private
|
|
40
|
+
private _version;
|
|
41
|
+
/**
|
|
42
|
+
* Get the current version for cache invalidation
|
|
43
|
+
* Incremented whenever middleware or routes are modified
|
|
44
|
+
*/
|
|
45
|
+
get version(): number;
|
|
40
46
|
/**
|
|
41
47
|
* Register a route
|
|
42
48
|
*
|
|
@@ -111,17 +117,6 @@ export declare class AOTRouter {
|
|
|
111
117
|
* @returns True if pattern matches
|
|
112
118
|
*/
|
|
113
119
|
private matchPattern;
|
|
114
|
-
/**
|
|
115
|
-
* Find the original route key for a matched dynamic route
|
|
116
|
-
*
|
|
117
|
-
* This is needed to look up route-specific middleware.
|
|
118
|
-
* It's a bit of a hack, but avoids storing duplicate data.
|
|
119
|
-
*
|
|
120
|
-
* @param method - HTTP method
|
|
121
|
-
* @param path - Matched path
|
|
122
|
-
* @returns Route key or null
|
|
123
|
-
*/
|
|
124
|
-
private findDynamicRouteKey;
|
|
125
120
|
/**
|
|
126
121
|
* Get all registered routes (for debugging)
|
|
127
122
|
*/
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @module @gravito/core/engine
|
|
8
8
|
*/
|
|
9
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
9
10
|
import type { FastRequest, FastContext as IFastContext } from './types';
|
|
10
11
|
/**
|
|
11
12
|
* Lazy-parsed request wrapper
|
|
@@ -17,17 +18,23 @@ declare class FastRequestImpl implements FastRequest {
|
|
|
17
18
|
private _request;
|
|
18
19
|
private _params;
|
|
19
20
|
private _path;
|
|
21
|
+
private _routePattern?;
|
|
20
22
|
private _url;
|
|
21
23
|
private _query;
|
|
22
24
|
private _headers;
|
|
23
25
|
private _cachedJson;
|
|
24
26
|
private _jsonParsed;
|
|
27
|
+
private _cachedText;
|
|
28
|
+
private _textParsed;
|
|
29
|
+
private _cachedFormData;
|
|
30
|
+
private _formDataParsed;
|
|
31
|
+
private _cachedQueries;
|
|
25
32
|
private _ctx;
|
|
26
33
|
constructor(ctx: FastContext);
|
|
27
34
|
/**
|
|
28
35
|
* Initialize for new request
|
|
29
36
|
*/
|
|
30
|
-
init(request: Request, params?: Record<string, string>, path?: string): this;
|
|
37
|
+
init(request: Request, params?: Record<string, string>, path?: string, routePattern?: string): this;
|
|
31
38
|
/**
|
|
32
39
|
* Reset for pooling
|
|
33
40
|
*/
|
|
@@ -36,6 +43,7 @@ declare class FastRequestImpl implements FastRequest {
|
|
|
36
43
|
get url(): string;
|
|
37
44
|
get method(): string;
|
|
38
45
|
get path(): string;
|
|
46
|
+
get routePattern(): string | undefined;
|
|
39
47
|
param(name: string): string | undefined;
|
|
40
48
|
params(): Record<string, string>;
|
|
41
49
|
private getUrl;
|
|
@@ -58,12 +66,13 @@ export declare class FastContext implements IFastContext {
|
|
|
58
66
|
readonly req: FastRequestImpl;
|
|
59
67
|
private _headers;
|
|
60
68
|
_isReleased: boolean;
|
|
69
|
+
private _requestScope;
|
|
61
70
|
/**
|
|
62
71
|
* Initialize context for a new request
|
|
63
72
|
*
|
|
64
73
|
* This is called when acquiring from the pool.
|
|
65
74
|
*/
|
|
66
|
-
init(request: Request, params?: Record<string, string>, path?: string): this;
|
|
75
|
+
init(request: Request, params?: Record<string, string>, path?: string, routePattern?: string): this;
|
|
67
76
|
/**
|
|
68
77
|
* Reset context for pooling (Cleanup)
|
|
69
78
|
*
|
|
@@ -92,6 +101,22 @@ export declare class FastContext implements IFastContext {
|
|
|
92
101
|
private _store;
|
|
93
102
|
get<T>(key: string): T;
|
|
94
103
|
set(key: string, value: any): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get the request-scoped service manager for this request.
|
|
106
|
+
*
|
|
107
|
+
* @returns The RequestScopeManager for this request.
|
|
108
|
+
* @throws Error if called before init() or after reset().
|
|
109
|
+
*/
|
|
110
|
+
requestScope(): RequestScopeManager;
|
|
111
|
+
/**
|
|
112
|
+
* Resolve a request-scoped service (convenience method).
|
|
113
|
+
*
|
|
114
|
+
* @template T - The service type.
|
|
115
|
+
* @param key - The service key for caching.
|
|
116
|
+
* @param factory - Factory function to create the service.
|
|
117
|
+
* @returns The cached or newly created service instance.
|
|
118
|
+
*/
|
|
119
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
95
120
|
route: (name: string, params?: any, query?: any) => string;
|
|
96
121
|
get native(): this;
|
|
97
122
|
}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*
|
|
12
12
|
* @module @gravito/core/engine
|
|
13
13
|
*/
|
|
14
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
14
15
|
import type { FastRequest, FastContext as IFastContext } from './types';
|
|
15
16
|
/**
|
|
16
17
|
* Minimal request wrapper
|
|
@@ -19,11 +20,17 @@ declare class MinimalRequest implements FastRequest {
|
|
|
19
20
|
private readonly _request;
|
|
20
21
|
private readonly _params;
|
|
21
22
|
private readonly _path;
|
|
23
|
+
private readonly _routePattern?;
|
|
22
24
|
private _searchParams;
|
|
23
|
-
|
|
25
|
+
private _cachedQueries;
|
|
26
|
+
private _cachedJsonPromise;
|
|
27
|
+
private _cachedTextPromise;
|
|
28
|
+
private _cachedFormDataPromise;
|
|
29
|
+
constructor(_request: Request, _params: Record<string, string>, _path: string, _routePattern?: string | undefined);
|
|
24
30
|
get url(): string;
|
|
25
31
|
get method(): string;
|
|
26
32
|
get path(): string;
|
|
33
|
+
get routePattern(): string | undefined;
|
|
27
34
|
param(name: string): string | undefined;
|
|
28
35
|
params(): Record<string, string>;
|
|
29
36
|
/**
|
|
@@ -51,7 +58,8 @@ declare class MinimalRequest implements FastRequest {
|
|
|
51
58
|
export declare class MinimalContext implements IFastContext {
|
|
52
59
|
readonly req: MinimalRequest;
|
|
53
60
|
private _resHeaders;
|
|
54
|
-
|
|
61
|
+
private _requestScope;
|
|
62
|
+
constructor(request: Request, params: Record<string, string>, path: string, routePattern?: string);
|
|
55
63
|
private getHeaders;
|
|
56
64
|
json<T>(data: T, status?: number): Response;
|
|
57
65
|
text(text: string, status?: number): Response;
|
|
@@ -69,6 +77,21 @@ export declare class MinimalContext implements IFastContext {
|
|
|
69
77
|
forward(target: string, _options?: any): Promise<Response>;
|
|
70
78
|
get<T>(_key: string): T;
|
|
71
79
|
set(_key: string, _value: any): void;
|
|
80
|
+
/**
|
|
81
|
+
* Get the request-scoped service manager for this request.
|
|
82
|
+
*
|
|
83
|
+
* @returns The RequestScopeManager for this request.
|
|
84
|
+
*/
|
|
85
|
+
requestScope(): RequestScopeManager;
|
|
86
|
+
/**
|
|
87
|
+
* Resolve a request-scoped service (convenience method).
|
|
88
|
+
*
|
|
89
|
+
* @template T - The service type.
|
|
90
|
+
* @param key - The service key for caching.
|
|
91
|
+
* @param factory - Factory function to create the service.
|
|
92
|
+
* @returns The cached or newly created service instance.
|
|
93
|
+
*/
|
|
94
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
72
95
|
route: (name: string, params?: any, query?: any) => string;
|
|
73
96
|
get native(): this;
|
|
74
97
|
init(_request: Request, _params?: Record<string, string>, _path?: string): this;
|
|
@@ -32,11 +32,14 @@ export interface FastContext {
|
|
|
32
32
|
/** Context Variables */
|
|
33
33
|
get<T>(key: string): T;
|
|
34
34
|
set(key: string, value: any): void;
|
|
35
|
+
/** Request Scope Management */
|
|
36
|
+
requestScope(): any;
|
|
37
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
35
38
|
/** Lifecycle helpers */
|
|
36
39
|
route: (name: string, params?: any, query?: any) => string;
|
|
37
40
|
readonly native: any;
|
|
38
41
|
/** Internal initialization for pooling */
|
|
39
|
-
init(request: Request, params?: Record<string, string>, path?: string): this;
|
|
42
|
+
init(request: Request, params?: Record<string, string>, path?: string, routePattern?: string): this;
|
|
40
43
|
/** Internal cleanup for pooling */
|
|
41
44
|
reset(): void;
|
|
42
45
|
}
|
|
@@ -50,6 +53,11 @@ export interface FastRequest {
|
|
|
50
53
|
readonly method: string;
|
|
51
54
|
/** Path without query */
|
|
52
55
|
readonly path: string;
|
|
56
|
+
/**
|
|
57
|
+
* Route pattern (e.g., /users/:id) for metrics labeling
|
|
58
|
+
* Prevents high cardinality issues in monitoring systems
|
|
59
|
+
*/
|
|
60
|
+
readonly routePattern?: string;
|
|
53
61
|
/** Get route parameter */
|
|
54
62
|
param(name: string): string | undefined;
|
|
55
63
|
/** Get all route parameters */
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RequestScope-Aware Error Handling
|
|
3
|
+
*
|
|
4
|
+
* Integrates RequestScope lifecycle with error handling to provide:
|
|
5
|
+
* - Error context with request-scoped resources
|
|
6
|
+
* - Automatic cleanup of scoped services on error
|
|
7
|
+
* - Request tracing and diagnostics
|
|
8
|
+
* - Resource leak detection
|
|
9
|
+
*/
|
|
10
|
+
import type { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
11
|
+
import type { RequestScopeMetrics } from '../Container/RequestScopeMetrics';
|
|
12
|
+
import type { GravitoContext } from '../http/types';
|
|
13
|
+
/**
|
|
14
|
+
* Extended error context with RequestScope information
|
|
15
|
+
*
|
|
16
|
+
* Provides error handlers access to request-scoped resources
|
|
17
|
+
* for proper resource cleanup and error diagnostics.
|
|
18
|
+
*/
|
|
19
|
+
export interface RequestScopeErrorContext {
|
|
20
|
+
/**
|
|
21
|
+
* The original error that was thrown
|
|
22
|
+
*/
|
|
23
|
+
error: unknown;
|
|
24
|
+
/**
|
|
25
|
+
* HTTP context where error occurred
|
|
26
|
+
*/
|
|
27
|
+
context: GravitoContext;
|
|
28
|
+
/**
|
|
29
|
+
* RequestScope manager for this request
|
|
30
|
+
* Allows error handlers to access or clean up scoped resources
|
|
31
|
+
*/
|
|
32
|
+
scope?: RequestScopeManager;
|
|
33
|
+
/**
|
|
34
|
+
* Metrics about the request scope state
|
|
35
|
+
* Useful for diagnostics and understanding resource usage
|
|
36
|
+
*/
|
|
37
|
+
scopeMetrics?: RequestScopeMetrics;
|
|
38
|
+
/**
|
|
39
|
+
* Number of scoped services at time of error
|
|
40
|
+
* High numbers might indicate resource leaks
|
|
41
|
+
*/
|
|
42
|
+
scopeSize?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Request processing time in milliseconds
|
|
45
|
+
* Useful for timeout errors
|
|
46
|
+
*/
|
|
47
|
+
duration?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Additional diagnostic information
|
|
50
|
+
*/
|
|
51
|
+
diagnostics?: {
|
|
52
|
+
servicesCleanedUp?: string[];
|
|
53
|
+
cleanupErrors?: Array<{
|
|
54
|
+
service: string;
|
|
55
|
+
error: unknown;
|
|
56
|
+
}>;
|
|
57
|
+
peakMemoryMb?: number;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Error that occurred during RequestScope cleanup
|
|
62
|
+
*
|
|
63
|
+
* Wraps original error with cleanup context for proper error reporting
|
|
64
|
+
*/
|
|
65
|
+
export declare class RequestScopeCleanupError extends Error {
|
|
66
|
+
originalError: unknown;
|
|
67
|
+
cleanupErrors: Array<{
|
|
68
|
+
service: string;
|
|
69
|
+
error: unknown;
|
|
70
|
+
}>;
|
|
71
|
+
constructor(message: string, originalError: unknown, cleanupErrors: Array<{
|
|
72
|
+
service: string;
|
|
73
|
+
error: unknown;
|
|
74
|
+
}>);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Helper to extract RequestScope context from GravitoContext
|
|
78
|
+
*
|
|
79
|
+
* @param ctx - Gravito context
|
|
80
|
+
* @returns RequestScope error context with available information
|
|
81
|
+
*/
|
|
82
|
+
export declare function extractRequestScopeErrorContext(ctx: GravitoContext, error: unknown): RequestScopeErrorContext;
|
|
83
|
+
/**
|
|
84
|
+
* Cleanup scoped services safely during error handling
|
|
85
|
+
*
|
|
86
|
+
* Ensures all scoped services are cleaned up even if some fail,
|
|
87
|
+
* and collects cleanup errors for diagnostics.
|
|
88
|
+
*
|
|
89
|
+
* @param scope - RequestScope manager
|
|
90
|
+
* @returns Array of cleanup errors if any occurred
|
|
91
|
+
*/
|
|
92
|
+
export declare function cleanupRequestScopeOnError(scope?: RequestScopeManager): Promise<Array<{
|
|
93
|
+
service: string;
|
|
94
|
+
error: unknown;
|
|
95
|
+
}>>;
|
|
96
|
+
/**
|
|
97
|
+
* Safe error handler wrapper that manages RequestScope cleanup
|
|
98
|
+
*
|
|
99
|
+
* Ensures scoped services are properly cleaned up before returning error response.
|
|
100
|
+
* Use this to wrap error handlers to make them RequestScope-aware.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const errorHandler = withRequestScopeCleanup(async (ctx, error) => {
|
|
105
|
+
* // Handle error...
|
|
106
|
+
* return ctx.json({ error: error.message }, 500)
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* // In your app:
|
|
110
|
+
* try {
|
|
111
|
+
* // Handle request...
|
|
112
|
+
* } catch (error) {
|
|
113
|
+
* return errorHandler(ctx, error)
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function withRequestScopeCleanup<T extends (ctx: GravitoContext, error: unknown) => Promise<Response>>(handler: T): T;
|
|
118
|
+
/**
|
|
119
|
+
* Detect potential resource leaks in RequestScope
|
|
120
|
+
*
|
|
121
|
+
* Returns diagnostic information about suspicious resource usage patterns
|
|
122
|
+
*/
|
|
123
|
+
export declare function detectRequestScopeLeaks(context: RequestScopeErrorContext): {
|
|
124
|
+
potentialLeaks: boolean;
|
|
125
|
+
warnings: string[];
|
|
126
|
+
};
|