@gravito/ripple 4.0.0 → 4.0.3
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 +26 -10
- package/dist/{ripple/src/OrbitRipple.d.ts → OrbitRipple.d.ts} +1 -0
- package/dist/{ripple/src/RippleServer.d.ts → RippleServer.d.ts} +32 -2
- package/dist/core/src/Application.d.ts +41 -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 +45 -0
- package/dist/core/src/ErrorHandler.d.ts +3 -0
- package/dist/core/src/HookManager.d.ts +104 -165
- package/dist/core/src/PlanetCore.d.ts +114 -1
- package/dist/core/src/RequestContext.d.ts +97 -0
- package/dist/core/src/Router.d.ts +32 -46
- package/dist/core/src/ServiceProvider.d.ts +22 -0
- package/dist/core/src/adapters/bun/BunContext.d.ts +10 -1
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +35 -0
- package/dist/core/src/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +1 -0
- package/dist/core/src/adapters/bun/index.d.ts +7 -0
- package/dist/core/src/binary/BinaryUtils.d.ts +105 -0
- package/dist/core/src/binary/index.d.ts +5 -0
- package/dist/core/src/cli/queue-commands.d.ts +6 -0
- package/dist/core/src/compat/async-local-storage.d.ts +7 -0
- package/dist/core/src/compat/crypto.d.ts +6 -0
- package/dist/core/src/engine/AOTRouter.d.ts +16 -1
- package/dist/core/src/engine/FastContext.d.ts +42 -1
- package/dist/core/src/engine/Gravito.d.ts +13 -19
- package/dist/core/src/engine/MinimalContext.d.ts +23 -0
- package/dist/core/src/engine/analyzer.d.ts +92 -6
- package/dist/core/src/engine/types.d.ts +6 -0
- 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/DeadLetterQueue.d.ts +75 -1
- package/dist/core/src/events/EventBackend.d.ts +2 -1
- package/dist/core/src/events/EventOptions.d.ts +99 -4
- package/dist/core/src/events/EventPriorityQueue.d.ts +36 -175
- package/dist/core/src/events/FlowControlStrategy.d.ts +109 -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 +18 -6
- package/dist/core/src/events/observability/OTelEventMetrics.d.ts +92 -0
- package/dist/core/src/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/core/src/events/observability/index.d.ts +4 -0
- package/dist/core/src/events/queue-core.d.ts +77 -0
- package/dist/core/src/events/task-executor.d.ts +51 -0
- package/dist/core/src/events/types.d.ts +59 -0
- package/dist/core/src/ffi/NativeAccelerator.d.ts +62 -0
- package/dist/core/src/ffi/NativeHasher.d.ts +139 -0
- package/dist/core/src/ffi/cbor-fallback.d.ts +96 -0
- package/dist/core/src/ffi/hash-fallback.d.ts +33 -0
- package/dist/core/src/ffi/index.d.ts +10 -0
- package/dist/core/src/ffi/types.d.ts +135 -0
- package/dist/core/src/health/HealthProvider.d.ts +67 -0
- package/dist/core/src/helpers/Str.d.ts +15 -0
- package/dist/core/src/hooks/ActionManager.d.ts +132 -0
- package/dist/core/src/hooks/AsyncDetector.d.ts +84 -0
- package/dist/core/src/hooks/FilterManager.d.ts +71 -0
- package/dist/core/src/hooks/MigrationWarner.d.ts +24 -0
- package/dist/core/src/hooks/dlq-operations.d.ts +60 -0
- package/dist/core/src/hooks/types.d.ts +107 -0
- package/dist/core/src/http/CookieJar.d.ts +2 -2
- package/dist/core/src/http/types.d.ts +43 -3
- package/dist/core/src/index.d.ts +504 -15
- package/dist/core/src/observability/QueueDashboard.d.ts +136 -0
- package/dist/core/src/observability/contracts.d.ts +137 -0
- package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +36 -3
- package/dist/core/src/runtime/adapter-bun.d.ts +12 -0
- package/dist/core/src/runtime/adapter-deno.d.ts +12 -0
- package/dist/core/src/runtime/adapter-node.d.ts +12 -0
- package/dist/core/src/runtime/adapter-unknown.d.ts +13 -0
- package/dist/core/src/runtime/archive.d.ts +17 -0
- package/dist/core/src/runtime/compression.d.ts +21 -0
- package/dist/core/src/runtime/deep-equals.d.ts +56 -0
- package/dist/core/src/runtime/detection.d.ts +22 -0
- package/dist/core/src/runtime/escape.d.ts +34 -0
- package/dist/core/src/runtime/index.d.ts +44 -0
- package/dist/core/src/runtime/markdown.d.ts +44 -0
- package/dist/core/src/runtime/types.d.ts +436 -0
- package/dist/core/src/runtime-helpers.d.ts +67 -0
- package/dist/core/src/runtime.d.ts +10 -118
- package/dist/core/src/testing/HttpTester.d.ts +1 -0
- package/dist/core/src/transpiler-utils.d.ts +170 -0
- package/dist/{ripple/src/engines → engines}/BunEngine.d.ts +11 -0
- package/dist/engines/UWebSocketsEngine.d.ts +97 -0
- package/dist/engines/WsEngine.d.ts +69 -0
- package/dist/{ripple/src/engines → engines}/index.d.ts +4 -0
- package/dist/index.js +1 -7139
- package/dist/index.js.map +3 -69
- package/dist/{ripple/src/serializers → serializers}/ISerializer.d.ts +1 -1
- package/dist/{ripple/src/serializers → serializers}/JsonSerializer.d.ts +1 -1
- package/dist/{ripple/src/serializers → serializers}/ProtobufSerializer.d.ts +6 -3
- package/dist/{ripple/src/types.d.ts → types.d.ts} +11 -0
- package/package.json +7 -2
- package/dist/atlas/src/DB.d.ts +0 -301
- package/dist/atlas/src/OrbitAtlas.d.ts +0 -9
- package/dist/atlas/src/config/defineConfig.d.ts +0 -14
- package/dist/atlas/src/config/index.d.ts +0 -7
- package/dist/atlas/src/config/loadConfig.d.ts +0 -48
- package/dist/atlas/src/connection/Connection.d.ts +0 -108
- package/dist/atlas/src/connection/ConnectionManager.d.ts +0 -111
- package/dist/atlas/src/drivers/BunSQLDriver.d.ts +0 -32
- package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +0 -118
- package/dist/atlas/src/drivers/MongoDBDriver.d.ts +0 -36
- package/dist/atlas/src/drivers/MySQLDriver.d.ts +0 -66
- package/dist/atlas/src/drivers/PostgresDriver.d.ts +0 -83
- package/dist/atlas/src/drivers/RedisDriver.d.ts +0 -43
- package/dist/atlas/src/drivers/SQLiteDriver.d.ts +0 -45
- package/dist/atlas/src/drivers/types.d.ts +0 -260
- package/dist/atlas/src/errors/index.d.ts +0 -45
- package/dist/atlas/src/grammar/Grammar.d.ts +0 -342
- package/dist/atlas/src/grammar/MongoGrammar.d.ts +0 -47
- package/dist/atlas/src/grammar/MySQLGrammar.d.ts +0 -54
- package/dist/atlas/src/grammar/NullGrammar.d.ts +0 -35
- package/dist/atlas/src/grammar/PostgresGrammar.d.ts +0 -62
- package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +0 -32
- package/dist/atlas/src/index.d.ts +0 -67
- package/dist/atlas/src/migration/Migration.d.ts +0 -64
- package/dist/atlas/src/migration/MigrationRepository.d.ts +0 -65
- package/dist/atlas/src/migration/Migrator.d.ts +0 -110
- package/dist/atlas/src/migration/index.d.ts +0 -6
- package/dist/atlas/src/observability/AtlasMetrics.d.ts +0 -11
- package/dist/atlas/src/observability/AtlasObservability.d.ts +0 -15
- package/dist/atlas/src/observability/AtlasTracer.d.ts +0 -12
- package/dist/atlas/src/observability/index.d.ts +0 -9
- package/dist/atlas/src/orm/index.d.ts +0 -5
- package/dist/atlas/src/orm/model/DirtyTracker.d.ts +0 -121
- package/dist/atlas/src/orm/model/Model.d.ts +0 -449
- package/dist/atlas/src/orm/model/ModelRegistry.d.ts +0 -20
- package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +0 -136
- package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +0 -36
- package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +0 -87
- package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +0 -117
- package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +0 -64
- package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +0 -15
- package/dist/atlas/src/orm/model/concerns/index.d.ts +0 -12
- package/dist/atlas/src/orm/model/decorators.d.ts +0 -109
- package/dist/atlas/src/orm/model/errors.d.ts +0 -52
- package/dist/atlas/src/orm/model/index.d.ts +0 -10
- package/dist/atlas/src/orm/model/relationships.d.ts +0 -207
- package/dist/atlas/src/orm/model/types.d.ts +0 -12
- package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +0 -123
- package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +0 -54
- package/dist/atlas/src/orm/schema/index.d.ts +0 -6
- package/dist/atlas/src/orm/schema/types.d.ts +0 -85
- package/dist/atlas/src/query/Expression.d.ts +0 -60
- package/dist/atlas/src/query/NPlusOneDetector.d.ts +0 -10
- package/dist/atlas/src/query/QueryBuilder.d.ts +0 -573
- package/dist/atlas/src/query/clauses/GroupByClause.d.ts +0 -51
- package/dist/atlas/src/query/clauses/HavingClause.d.ts +0 -70
- package/dist/atlas/src/query/clauses/JoinClause.d.ts +0 -87
- package/dist/atlas/src/query/clauses/LimitClause.d.ts +0 -82
- package/dist/atlas/src/query/clauses/OrderByClause.d.ts +0 -69
- package/dist/atlas/src/query/clauses/SelectClause.d.ts +0 -71
- package/dist/atlas/src/query/clauses/WhereClause.d.ts +0 -167
- package/dist/atlas/src/query/clauses/index.d.ts +0 -11
- package/dist/atlas/src/schema/Blueprint.d.ts +0 -276
- package/dist/atlas/src/schema/ColumnDefinition.d.ts +0 -154
- package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +0 -37
- package/dist/atlas/src/schema/Schema.d.ts +0 -131
- package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +0 -23
- package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +0 -26
- package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +0 -28
- package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +0 -97
- package/dist/atlas/src/schema/grammars/index.d.ts +0 -7
- package/dist/atlas/src/schema/index.d.ts +0 -8
- package/dist/atlas/src/seed/Factory.d.ts +0 -90
- package/dist/atlas/src/seed/Seeder.d.ts +0 -28
- package/dist/atlas/src/seed/SeederRunner.d.ts +0 -74
- package/dist/atlas/src/seed/index.d.ts +0 -6
- package/dist/atlas/src/types/index.d.ts +0 -1100
- package/dist/atlas/src/utils/levenshtein.d.ts +0 -9
- package/dist/core/src/adapters/PhotonAdapter.d.ts +0 -171
- package/dist/core/src/adapters/photon-types.d.ts +0 -73
- package/dist/core/src/http/middleware/BodySizeLimit.d.ts +0 -16
- package/dist/core/src/http/middleware/Cors.d.ts +0 -24
- package/dist/core/src/http/middleware/Csrf.d.ts +0 -23
- package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +0 -28
- package/dist/core/src/http/middleware/SecurityHeaders.d.ts +0 -29
- package/dist/core/src/http/middleware/ThrottleRequests.d.ts +0 -18
- package/dist/core/src/instrumentation/index.d.ts +0 -35
- package/dist/core/src/instrumentation/opentelemetry.d.ts +0 -178
- package/dist/core/src/instrumentation/types.d.ts +0 -182
- package/dist/photon/src/index.d.ts +0 -84
- package/dist/photon/src/middleware/binary.d.ts +0 -31
- package/dist/photon/src/middleware/htmx.d.ts +0 -39
- package/dist/photon/src/middleware/ratelimit.d.ts +0 -157
- package/dist/photon/src/openapi.d.ts +0 -19
- /package/dist/{ripple/src/channels → channels}/Channel.d.ts +0 -0
- /package/dist/{ripple/src/channels → channels}/ChannelManager.d.ts +0 -0
- /package/dist/{ripple/src/channels → channels}/index.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/LocalDriver.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/NATSDriver.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/RedisDriver.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/index.d.ts +0 -0
- /package/dist/{ripple/src/engines → engines}/IRippleEngine.d.ts +0 -0
- /package/dist/{ripple/src/errors → errors}/RippleError.d.ts +0 -0
- /package/dist/{ripple/src/errors → errors}/index.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/BroadcastEvent.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/BroadcastManager.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/Broadcaster.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/index.d.ts +0 -0
- /package/dist/{ripple/src/health → health}/HealthChecker.d.ts +0 -0
- /package/dist/{ripple/src/health → health}/index.d.ts +0 -0
- /package/dist/{ripple/src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{ripple/src/logging → logging}/Logger.d.ts +0 -0
- /package/dist/{ripple/src/logging → logging}/index.d.ts +0 -0
- /package/dist/{ripple/src/middleware → middleware}/InterceptorManager.d.ts +0 -0
- /package/dist/{ripple/src/observability → observability}/RippleMetrics.d.ts +0 -0
- /package/dist/{ripple/src/reliability → reliability}/AckManager.d.ts +0 -0
- /package/dist/{ripple/src/serializers → serializers}/index.d.ts +0 -0
- /package/dist/{ripple/src/tracking → tracking}/ConnectionTracker.d.ts +0 -0
- /package/dist/{ripple/src/tracking → tracking}/SessionManager.d.ts +0 -0
- /package/dist/{ripple/src/tracking → tracking}/index.d.ts +0 -0
- /package/dist/{ripple/src/utils → utils}/MessageSerializer.d.ts +0 -0
- /package/dist/{ripple/src/utils → utils}/TokenBucket.d.ts +0 -0
- /package/dist/{ripple/src/utils → utils}/index.d.ts +0 -0
package/README.md
CHANGED
|
@@ -11,16 +11,32 @@
|
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
- ⚡ **Multi-Runtime Support** - Run on Bun (native WebSocket), Node.js (uWebSockets.js or ws)
|
|
15
|
-
- 🚀 **Simplified API** - Single `start()` method to initialize and run your server
|
|
16
|
-
- 📡 **
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
- 🌐 **Horizontal Scaling** - Redis or NATS driver for multi-server deployments
|
|
20
|
-
- 🔍 **Full Observability** - Built-in logging, health checks, metrics, and connection tracking
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
- ⚡ **Multi-Runtime Support** - Run on Bun (native WebSocket), Node.js (uWebSockets.js or ws).
|
|
15
|
+
- 🚀 **Simplified API** - Single `start()` method to initialize and run your server.
|
|
16
|
+
- 📡 **Real-time Pulse Sync** - Channel-based broadcasting for Public, Private, and Presence channels.
|
|
17
|
+
- 🪐 **Galaxy-Ready Core** - Native integration with PlanetCore for universal WebSocket communication.
|
|
18
|
+
- 🔒 **Secure Authorization** - Flexible callback-based authorization system.
|
|
19
|
+
- 🌐 **Horizontal Scaling** - Redis or NATS driver for multi-server Galaxy deployments.
|
|
20
|
+
- 🔍 **Full Observability** - Built-in logging, health checks, metrics, and connection tracking.
|
|
21
|
+
|
|
22
|
+
## 🌌 Role in Galaxy Architecture
|
|
23
|
+
|
|
24
|
+
In the **Gravito Galaxy Architecture**, Ripple acts as the **Gravitational Waves (Real-time Pulse)**.
|
|
25
|
+
|
|
26
|
+
- **Instant Propagation**: Propagates state changes from the Galaxy's core to external observers (Users) with sub-millisecond latency.
|
|
27
|
+
- **Satellite Interactivity**: Provides the foundation for real-time collaboration features within and between Satellites.
|
|
28
|
+
- **Bi-directional Channel**: Unlike the one-way `Radiance` broadcast, Ripple provides a bi-directional "Pulse" that allows Clients to interact with the Sensing Layer over a persistent connection.
|
|
29
|
+
|
|
30
|
+
```mermaid
|
|
31
|
+
graph LR
|
|
32
|
+
Client([Client]) <--> Ripple{Ripple Pulse}
|
|
33
|
+
Ripple <--> Photon[Photon Engine]
|
|
34
|
+
subgraph Galaxy
|
|
35
|
+
Photon <--> S1[Satellite: Chat]
|
|
36
|
+
Photon <--> S2[Satellite: Dashboard]
|
|
37
|
+
end
|
|
38
|
+
Ripple -.-> Plasma[(Plasma: Redis Sync)]
|
|
39
|
+
```
|
|
24
40
|
|
|
25
41
|
## Why Ripple?
|
|
26
42
|
|
|
@@ -36,6 +36,7 @@ export declare class OrbitRipple implements GravitoOrbit {
|
|
|
36
36
|
*
|
|
37
37
|
* - Registers 'ripple' (RippleServer) and 'broadcast' (BroadcastManager) in the container.
|
|
38
38
|
* - Adds middleware to inject 'ripple' and 'broadcast' into GravitoContext.
|
|
39
|
+
* - Integrates with BunNativeAdapter for same-port WebSocket support.
|
|
39
40
|
* - Initializes the server and registers shutdown hooks.
|
|
40
41
|
*
|
|
41
42
|
* @param core - The PlanetCore instance
|
|
@@ -80,7 +80,6 @@ export declare class RippleServer {
|
|
|
80
80
|
private broadcastBinaryToChannel;
|
|
81
81
|
broadcastBinary(channel: string, event: string, data: ArrayBuffer): void;
|
|
82
82
|
private handleClose;
|
|
83
|
-
private handleDrain;
|
|
84
83
|
private handleSubscribe;
|
|
85
84
|
private handleUnsubscribe;
|
|
86
85
|
private handleWhisper;
|
|
@@ -115,6 +114,36 @@ export declare class RippleServer {
|
|
|
115
114
|
* Get server health status.
|
|
116
115
|
*/
|
|
117
116
|
getHealth(): Promise<import(".").HealthCheckResult>;
|
|
117
|
+
/**
|
|
118
|
+
* Initialize driver and serializer without starting the engine.
|
|
119
|
+
*
|
|
120
|
+
* This method:
|
|
121
|
+
* 1. Initializes the driver (Redis/NATS/Local)
|
|
122
|
+
* 2. Initializes the serializer (JSON/Protobuf)
|
|
123
|
+
* 3. Starts the ping interval
|
|
124
|
+
*
|
|
125
|
+
* Used by OrbitRipple for same-port integration with BunNativeAdapter.
|
|
126
|
+
*
|
|
127
|
+
* @since 5.0.0
|
|
128
|
+
*/
|
|
129
|
+
initDriver(): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Start ping interval (internal helper)
|
|
132
|
+
*/
|
|
133
|
+
private startPingInterval;
|
|
134
|
+
/**
|
|
135
|
+
* Get WebSocket handler configuration (Bun only).
|
|
136
|
+
*
|
|
137
|
+
* Returns handler object for use with BunNativeAdapter.registerWebSocketRoute().
|
|
138
|
+
*
|
|
139
|
+
* @throws Error if engine is not BunEngine
|
|
140
|
+
*/
|
|
141
|
+
getWebSocketConfig(): {
|
|
142
|
+
open: (ws: any) => void;
|
|
143
|
+
message: (ws: any, data: any) => void;
|
|
144
|
+
close: (ws: any, code: number, reason: string) => void;
|
|
145
|
+
drain: (_ws: any) => void;
|
|
146
|
+
};
|
|
118
147
|
/**
|
|
119
148
|
* Initialize and start the RippleServer.
|
|
120
149
|
*
|
|
@@ -124,9 +153,10 @@ export declare class RippleServer {
|
|
|
124
153
|
* 3. Starts the WebSocket engine (Bun/uWS/ws)
|
|
125
154
|
* 4. Starts the ping interval
|
|
126
155
|
*
|
|
156
|
+
* @param port - Optional port override (defaults to config.port or 3000)
|
|
127
157
|
* @since 5.0.0
|
|
128
158
|
*/
|
|
129
|
-
start(): Promise<void>;
|
|
159
|
+
start(port?: number): Promise<void>;
|
|
130
160
|
/**
|
|
131
161
|
* Initialize the RippleServer without starting the engine.
|
|
132
162
|
*
|
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
* Provides a centralized entry point for enterprise applications with
|
|
6
6
|
* auto-discovery of providers, config loading, and lifecycle management.
|
|
7
7
|
*
|
|
8
|
+
* Phase 4 優化:Provider 預掃描 + 平行載入
|
|
9
|
+
* - Phase 1:預掃描所有 Provider 文件(語法驗證)
|
|
10
|
+
* - Phase 2:篩選有效 Provider(跳過無效的)
|
|
11
|
+
* - Phase 3:平行 import 所有有效 Provider
|
|
12
|
+
* - Phase 4:註冊到容器
|
|
13
|
+
*
|
|
8
14
|
* @module @gravito/core
|
|
9
15
|
* @since 2.0.0
|
|
10
16
|
*/
|
|
@@ -141,9 +147,44 @@ export declare class Application {
|
|
|
141
147
|
/**
|
|
142
148
|
* Discover and register providers from the providers directory.
|
|
143
149
|
*
|
|
150
|
+
* Phase 4 優化版本:採用 4-Phase 載入策略
|
|
151
|
+
* - Phase 1:預掃描所有候選 Provider 檔案(語法驗證)
|
|
152
|
+
* - Phase 2:篩選有效 Provider 檔案(跳過語法錯誤的)
|
|
153
|
+
* - Phase 3:平行 import 所有有效 Provider(從循序 → 並行)
|
|
154
|
+
* - Phase 4:逐一註冊到容器
|
|
155
|
+
*
|
|
156
|
+
* 效能改進:N 個 Provider 從 O(N * importTime) 降至 O(importTime + overhead)
|
|
157
|
+
*
|
|
144
158
|
* @internal
|
|
145
159
|
*/
|
|
146
160
|
private discoverProviders;
|
|
161
|
+
/**
|
|
162
|
+
* Phase 1+2:預掃描 Provider 目錄中的所有候選檔案。
|
|
163
|
+
*
|
|
164
|
+
* 使用輕量語法驗證(嘗試讀取 + 基本結構檢查)篩選有效的 Provider 檔案,
|
|
165
|
+
* 讓語法錯誤在 import 之前被發現,提供更清晰的錯誤訊息。
|
|
166
|
+
*
|
|
167
|
+
* 策略:
|
|
168
|
+
* 1. 使用 Bun.Transpiler 進行 import 掃描(在 Bun 環境下)
|
|
169
|
+
* 2. Fallback 至基本檔案讀取驗證(在 Node/Deno 環境下)
|
|
170
|
+
*
|
|
171
|
+
* @param providersPath - Provider 目錄絕對路徑
|
|
172
|
+
* @returns 掃描結果陣列(含有效/無效標記)
|
|
173
|
+
* @internal
|
|
174
|
+
*/
|
|
175
|
+
private prescribeProviders;
|
|
176
|
+
/**
|
|
177
|
+
* Phase 3:平行 import 所有有效的 Provider 檔案。
|
|
178
|
+
*
|
|
179
|
+
* 使用 Promise.all() 同時 import 所有通過預掃描的 Provider 檔案,
|
|
180
|
+
* 從循序載入(O(N))改善為平行載入(O(1) 理論上),
|
|
181
|
+
* 實際效能受 I/O、CPU 和 V8 模組解析限制。
|
|
182
|
+
*
|
|
183
|
+
* @param validProviders - 通過預掃描的 Provider 掃描結果
|
|
184
|
+
* @returns 模組載入結果陣列
|
|
185
|
+
* @internal
|
|
186
|
+
*/
|
|
187
|
+
private loadProvidersInParallel;
|
|
147
188
|
/**
|
|
148
189
|
* Resolve a service instance from the IoC container.
|
|
149
190
|
*
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration manager (ConfigManager)
|
|
3
|
+
*
|
|
4
|
+
* Unifies environment variables and application configuration access.
|
|
5
|
+
*/
|
|
6
|
+
import type { ZodSchema } from 'zod';
|
|
1
7
|
/**
|
|
2
8
|
* ConfigManager - Central configuration store.
|
|
3
9
|
* Supports loading from environment variables and initial objects.
|
|
@@ -5,6 +11,7 @@
|
|
|
5
11
|
*/
|
|
6
12
|
export declare class ConfigManager {
|
|
7
13
|
private config;
|
|
14
|
+
private schema;
|
|
8
15
|
constructor(initialConfig?: Record<string, unknown>);
|
|
9
16
|
/**
|
|
10
17
|
* Load all environment variables from the active runtime.
|
|
@@ -23,4 +30,36 @@ export declare class ConfigManager {
|
|
|
23
30
|
* Check whether a key exists.
|
|
24
31
|
*/
|
|
25
32
|
has(key: string): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Define a Zod schema for configuration validation.
|
|
35
|
+
*
|
|
36
|
+
* @param schema - Zod schema for validation
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* config.defineSchema(z.object({
|
|
41
|
+
* DATABASE_URL: z.string().url(),
|
|
42
|
+
* PORT: z.number().default(3000),
|
|
43
|
+
* }))
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
defineSchema(schema: ZodSchema): void;
|
|
47
|
+
/**
|
|
48
|
+
* Validate configuration against the defined schema.
|
|
49
|
+
*
|
|
50
|
+
* Should be called during bootstrap to catch configuration errors early.
|
|
51
|
+
*
|
|
52
|
+
* @throws Error if validation fails with details about missing/invalid fields
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* try {
|
|
57
|
+
* config.validate()
|
|
58
|
+
* } catch (error) {
|
|
59
|
+
* console.error('Config validation failed:', error.message)
|
|
60
|
+
* process.exit(1)
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
validate(): void;
|
|
26
65
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ServiceKey } from '../Container';
|
|
2
|
+
import { RequestScopeMetrics, type RequestScopeObserver } from './RequestScopeMetrics';
|
|
3
|
+
/**
|
|
4
|
+
* Manages request-scoped service instances within a single HTTP request.
|
|
5
|
+
*
|
|
6
|
+
* Each request gets its own RequestScopeManager instance with isolated state.
|
|
7
|
+
* Services are cached within the request and automatically cleaned up when
|
|
8
|
+
* the request ends.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const scope = new RequestScopeManager()
|
|
13
|
+
* const cache = scope.resolve('productCache', () => new ProductCache())
|
|
14
|
+
* // ... use cache ...
|
|
15
|
+
* await scope.cleanup() // Called automatically by Gravito engine
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class RequestScopeManager {
|
|
19
|
+
private scoped;
|
|
20
|
+
private metadata;
|
|
21
|
+
private metrics;
|
|
22
|
+
private observer;
|
|
23
|
+
constructor(observer?: RequestScopeObserver);
|
|
24
|
+
/**
|
|
25
|
+
* Set observer for monitoring scope lifecycle
|
|
26
|
+
*/
|
|
27
|
+
setObserver(observer: RequestScopeObserver): void;
|
|
28
|
+
/**
|
|
29
|
+
* Get metrics for this scope
|
|
30
|
+
*/
|
|
31
|
+
getMetrics(): RequestScopeMetrics;
|
|
32
|
+
/**
|
|
33
|
+
* Resolve or retrieve a request-scoped service instance.
|
|
34
|
+
*
|
|
35
|
+
* If the service already exists in this scope, returns the cached instance.
|
|
36
|
+
* Otherwise, calls the factory function to create a new instance and caches it.
|
|
37
|
+
*
|
|
38
|
+
* Automatically detects and records services with cleanup methods.
|
|
39
|
+
*
|
|
40
|
+
* @template T - The type of the service.
|
|
41
|
+
* @param key - The service key (for caching).
|
|
42
|
+
* @param factory - Factory function to create the instance if not cached.
|
|
43
|
+
* @returns The cached or newly created instance.
|
|
44
|
+
*/
|
|
45
|
+
resolve<T>(key: ServiceKey, factory: () => T): T;
|
|
46
|
+
/**
|
|
47
|
+
* Clean up all request-scoped instances.
|
|
48
|
+
*
|
|
49
|
+
* Calls the cleanup() method on each service that has one.
|
|
50
|
+
* Silently ignores cleanup errors to prevent cascading failures.
|
|
51
|
+
* Called automatically by the Gravito engine in the request finally block.
|
|
52
|
+
*
|
|
53
|
+
* @returns Promise that resolves when all cleanup is complete.
|
|
54
|
+
*/
|
|
55
|
+
cleanup(): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Get the number of services in this scope (for monitoring).
|
|
58
|
+
*
|
|
59
|
+
* @returns The count of cached services.
|
|
60
|
+
*/
|
|
61
|
+
size(): number;
|
|
62
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RequestScopeMetrics - Observability for RequestScope lifecycle
|
|
3
|
+
*
|
|
4
|
+
* Tracks cleanup execution time, scope size, and service counts
|
|
5
|
+
* for performance monitoring and diagnostics.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const metrics = new RequestScopeMetrics()
|
|
10
|
+
* metrics.recordCleanupStart()
|
|
11
|
+
* await scope.cleanup()
|
|
12
|
+
* metrics.recordCleanupEnd()
|
|
13
|
+
*
|
|
14
|
+
* console.log(metrics.toJSON())
|
|
15
|
+
* // { cleanupDuration: 2.5, scopeSize: 3, servicesCleaned: 3 }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class RequestScopeMetrics {
|
|
19
|
+
private cleanupStartTime;
|
|
20
|
+
private cleanupDuration;
|
|
21
|
+
private scopeSize;
|
|
22
|
+
private servicesCleaned;
|
|
23
|
+
private errorsOccurred;
|
|
24
|
+
/**
|
|
25
|
+
* Record start of cleanup operation
|
|
26
|
+
*/
|
|
27
|
+
recordCleanupStart(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Record end of cleanup operation
|
|
30
|
+
*
|
|
31
|
+
* @param scopeSize - Number of services in the scope
|
|
32
|
+
* @param servicesCleaned - Number of services that had cleanup called
|
|
33
|
+
* @param errorsOccurred - Number of cleanup errors
|
|
34
|
+
*/
|
|
35
|
+
recordCleanupEnd(scopeSize: number, servicesCleaned: number, errorsOccurred?: number): void;
|
|
36
|
+
/**
|
|
37
|
+
* Get cleanup duration in milliseconds
|
|
38
|
+
*
|
|
39
|
+
* @returns Duration in ms, or null if cleanup not completed
|
|
40
|
+
*/
|
|
41
|
+
getCleanupDuration(): number | null;
|
|
42
|
+
/**
|
|
43
|
+
* Check if cleanup took longer than threshold (default 2ms)
|
|
44
|
+
* Useful for detecting slow cleanups
|
|
45
|
+
*
|
|
46
|
+
* @param thresholdMs - Threshold in milliseconds
|
|
47
|
+
* @returns True if cleanup exceeded threshold
|
|
48
|
+
*/
|
|
49
|
+
isSlowCleanup(thresholdMs?: number): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Export metrics as JSON for logging/monitoring
|
|
52
|
+
*/
|
|
53
|
+
toJSON(): {
|
|
54
|
+
cleanupDuration: number | null;
|
|
55
|
+
scopeSize: number;
|
|
56
|
+
servicesCleaned: number;
|
|
57
|
+
errorsOccurred: number;
|
|
58
|
+
hasErrors: boolean;
|
|
59
|
+
isSlowCleanup: boolean;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Export metrics as compact string for logging
|
|
63
|
+
*/
|
|
64
|
+
toString(): string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* RequestScopeObserver - Hook for monitoring RequestScope lifecycle
|
|
68
|
+
*
|
|
69
|
+
* Implement this interface to receive callbacks during scope operations
|
|
70
|
+
*/
|
|
71
|
+
export interface RequestScopeObserver {
|
|
72
|
+
/**
|
|
73
|
+
* Called when a service is resolved in the scope
|
|
74
|
+
*/
|
|
75
|
+
onServiceResolved?(key: string | symbol, isFromCache: boolean): void;
|
|
76
|
+
/**
|
|
77
|
+
* Called when cleanup starts
|
|
78
|
+
*/
|
|
79
|
+
onCleanupStart?(): void;
|
|
80
|
+
/**
|
|
81
|
+
* Called when cleanup completes
|
|
82
|
+
*/
|
|
83
|
+
onCleanupEnd?(metrics: RequestScopeMetrics): void;
|
|
84
|
+
/**
|
|
85
|
+
* Called when cleanup encounters an error
|
|
86
|
+
*/
|
|
87
|
+
onCleanupError?(error: Error): void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* RequestScopeMetricsCollector - Aggregates metrics across multiple scopes
|
|
91
|
+
*
|
|
92
|
+
* Used for application-level monitoring and performance tracking
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const collector = new RequestScopeMetricsCollector()
|
|
97
|
+
*
|
|
98
|
+
* // Record metrics from multiple requests
|
|
99
|
+
* collector.record(metrics1)
|
|
100
|
+
* collector.record(metrics2)
|
|
101
|
+
* collector.record(metrics3)
|
|
102
|
+
*
|
|
103
|
+
* // Get aggregated stats
|
|
104
|
+
* const stats = collector.getStats()
|
|
105
|
+
* console.log(stats.averageCleanupTime) // 3.5ms
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare class RequestScopeMetricsCollector {
|
|
109
|
+
private metrics;
|
|
110
|
+
/**
|
|
111
|
+
* Record metrics from a request scope
|
|
112
|
+
*/
|
|
113
|
+
record(metrics: RequestScopeMetrics): void;
|
|
114
|
+
/**
|
|
115
|
+
* Get aggregated statistics
|
|
116
|
+
*/
|
|
117
|
+
getStats(): {
|
|
118
|
+
count: number;
|
|
119
|
+
averageCleanupTime: number | null;
|
|
120
|
+
maxCleanupTime: number | null;
|
|
121
|
+
minCleanupTime: number | null;
|
|
122
|
+
totalErrorCount: number;
|
|
123
|
+
errorRate: number;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Clear collected metrics
|
|
127
|
+
*/
|
|
128
|
+
clear(): void;
|
|
129
|
+
/**
|
|
130
|
+
* Get number of recorded metrics
|
|
131
|
+
*/
|
|
132
|
+
size(): number;
|
|
133
|
+
/**
|
|
134
|
+
* Export metrics as JSON array
|
|
135
|
+
*/
|
|
136
|
+
toJSON(): {
|
|
137
|
+
cleanupDuration: number | null;
|
|
138
|
+
scopeSize: number;
|
|
139
|
+
servicesCleaned: number;
|
|
140
|
+
errorsOccurred: number;
|
|
141
|
+
hasErrors: boolean;
|
|
142
|
+
isSlowCleanup: boolean;
|
|
143
|
+
}[];
|
|
144
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RequestScopeManager } from './Container/RequestScopeManager';
|
|
1
2
|
/**
|
|
2
3
|
* Factory type for creating service instances
|
|
3
4
|
*/
|
|
@@ -31,6 +32,27 @@ export declare class Container {
|
|
|
31
32
|
private bindings;
|
|
32
33
|
private instances;
|
|
33
34
|
private resolutionStack;
|
|
35
|
+
/**
|
|
36
|
+
* Run a function within a request scope context.
|
|
37
|
+
*
|
|
38
|
+
* All service resolutions within the function will use the provided scope,
|
|
39
|
+
* enabling request-scoped service instances to be properly isolated.
|
|
40
|
+
*
|
|
41
|
+
* @template T - The return type of the function.
|
|
42
|
+
* @param scope - The RequestScopeManager for this request.
|
|
43
|
+
* @param fn - The function to execute within the scope.
|
|
44
|
+
* @returns The result of the function.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const scope = new RequestScopeManager()
|
|
49
|
+
* const result = await Container.runWithScope(scope, async () => {
|
|
50
|
+
* const service = container.make('requestScoped')
|
|
51
|
+
* return service.doSomething()
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
static runWithScope<T>(scope: RequestScopeManager, fn: () => T | Promise<T>): T | Promise<T>;
|
|
34
56
|
/**
|
|
35
57
|
* Bind a service to the container.
|
|
36
58
|
*
|
|
@@ -63,6 +85,22 @@ export declare class Container {
|
|
|
63
85
|
* ```
|
|
64
86
|
*/
|
|
65
87
|
singleton<T>(key: ServiceKey, factory: Factory<T>): void;
|
|
88
|
+
/**
|
|
89
|
+
* Bind a request-scoped service to the container.
|
|
90
|
+
*
|
|
91
|
+
* A new instance will be created for each request and cached within that request.
|
|
92
|
+
* The service is automatically cleaned up when the request ends.
|
|
93
|
+
*
|
|
94
|
+
* @template T - The type of the service being bound.
|
|
95
|
+
* @param key - The unique identifier for the service.
|
|
96
|
+
* @param factory - The factory function that creates the service instance.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* container.scoped('requestCache', (c) => new RequestProductCache());
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
scoped<T>(key: ServiceKey, factory: Factory<T>): void;
|
|
66
104
|
/**
|
|
67
105
|
* Register an existing instance as a shared service.
|
|
68
106
|
*
|
|
@@ -70,6 +108,13 @@ export declare class Container {
|
|
|
70
108
|
* @param instance - The instance to register.
|
|
71
109
|
*/
|
|
72
110
|
instance<T>(key: ServiceKey, instance: T): void;
|
|
111
|
+
/**
|
|
112
|
+
* Check if a service is request-scoped.
|
|
113
|
+
*
|
|
114
|
+
* @param key - The service key to check.
|
|
115
|
+
* @returns True if the service is request-scoped.
|
|
116
|
+
*/
|
|
117
|
+
isRequestScoped(key: ServiceKey): boolean;
|
|
73
118
|
/**
|
|
74
119
|
* Resolve a service instance from the container.
|
|
75
120
|
*
|
|
@@ -42,6 +42,9 @@ export declare class ErrorHandler {
|
|
|
42
42
|
constructor(deps: ErrorHandlerDeps);
|
|
43
43
|
/**
|
|
44
44
|
* Handle application errors
|
|
45
|
+
*
|
|
46
|
+
* Integrates RequestScope cleanup to ensure proper resource management
|
|
47
|
+
* even when errors occur during request processing.
|
|
45
48
|
*/
|
|
46
49
|
handleError(err: unknown, c: GravitoContext): Promise<Response>;
|
|
47
50
|
/**
|