@gravito/radiance 1.0.3 → 1.0.5
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 +30 -7
- package/README.zh-TW.md +238 -3
- package/dist/core/src/Application.d.ts +256 -0
- package/dist/core/src/CommandKernel.d.ts +33 -0
- package/dist/core/src/ConfigManager.d.ts +65 -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 +153 -0
- package/dist/core/src/ErrorHandler.d.ts +66 -0
- package/dist/core/src/Event.d.ts +5 -0
- package/dist/core/src/EventManager.d.ts +123 -0
- package/dist/core/src/GlobalErrorHandlers.d.ts +47 -0
- package/dist/core/src/GravitoServer.d.ts +28 -0
- package/dist/core/src/HookManager.d.ts +435 -0
- package/dist/core/src/Listener.d.ts +4 -0
- package/dist/core/src/Logger.d.ts +20 -0
- package/dist/core/src/PlanetCore.d.ts +402 -0
- package/dist/core/src/RequestContext.d.ts +97 -0
- package/dist/core/src/Route.d.ts +36 -0
- package/dist/core/src/Router.d.ts +270 -0
- package/dist/core/src/ServiceProvider.d.ts +178 -0
- package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +27 -0
- package/dist/core/src/adapters/bun/BunContext.d.ts +54 -0
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +66 -0
- package/dist/core/src/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/core/src/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/core/src/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +32 -0
- package/dist/core/src/adapters/bun/index.d.ts +7 -0
- package/dist/core/src/adapters/bun/types.d.ts +20 -0
- package/dist/core/src/adapters/types.d.ts +235 -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 +139 -0
- package/dist/core/src/engine/FastContext.d.ts +141 -0
- package/dist/core/src/engine/Gravito.d.ts +131 -0
- package/dist/core/src/engine/MinimalContext.d.ts +102 -0
- package/dist/core/src/engine/analyzer.d.ts +113 -0
- package/dist/core/src/engine/constants.d.ts +23 -0
- package/dist/core/src/engine/index.d.ts +26 -0
- package/dist/core/src/engine/path.d.ts +26 -0
- package/dist/core/src/engine/pool.d.ts +83 -0
- package/dist/core/src/engine/types.d.ts +149 -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/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 +63 -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 +26 -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/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 +134 -0
- package/dist/core/src/exceptions/AuthenticationException.d.ts +8 -0
- package/dist/core/src/exceptions/AuthorizationException.d.ts +8 -0
- package/dist/core/src/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/core/src/exceptions/GravitoException.d.ts +23 -0
- package/dist/core/src/exceptions/HttpException.d.ts +9 -0
- package/dist/core/src/exceptions/ModelNotFoundException.d.ts +10 -0
- package/dist/core/src/exceptions/ValidationException.d.ts +22 -0
- package/dist/core/src/exceptions/index.d.ts +7 -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/Arr.d.ts +19 -0
- package/dist/core/src/helpers/Str.d.ts +38 -0
- package/dist/core/src/helpers/data.d.ts +25 -0
- package/dist/core/src/helpers/errors.d.ts +34 -0
- package/dist/core/src/helpers/response.d.ts +41 -0
- package/dist/core/src/helpers.d.ts +338 -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 +51 -0
- package/dist/core/src/http/cookie.d.ts +29 -0
- package/dist/core/src/http/types.d.ts +395 -0
- package/dist/core/src/index.d.ts +565 -0
- 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 +349 -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/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 +11 -0
- package/dist/core/src/security/Encrypter.d.ts +33 -0
- package/dist/core/src/security/Hasher.d.ts +29 -0
- package/dist/core/src/testing/HttpTester.d.ts +40 -0
- package/dist/core/src/testing/TestResponse.d.ts +78 -0
- package/dist/core/src/testing/index.d.ts +2 -0
- package/dist/core/src/transpiler-utils.d.ts +170 -0
- package/dist/core/src/types/events.d.ts +94 -0
- package/dist/index.js +1 -274
- package/dist/index.js.map +3 -10
- package/dist/radiance/src/BroadcastManager.d.ts +124 -0
- package/dist/radiance/src/OrbitRadiance.d.ts +98 -0
- package/dist/radiance/src/channels/Channel.d.ts +86 -0
- package/dist/radiance/src/drivers/AblyDriver.d.ts +73 -0
- package/dist/radiance/src/drivers/BroadcastDriver.d.ts +50 -0
- package/dist/radiance/src/drivers/PusherDriver.d.ts +95 -0
- package/dist/radiance/src/drivers/RedisDriver.d.ts +83 -0
- package/dist/radiance/src/drivers/WebSocketDriver.d.ts +89 -0
- package/dist/radiance/src/index.d.ts +39 -0
- package/package.json +10 -6
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HttpMethod } from '../../http/types';
|
|
2
|
+
import { NodeType, type RouteHandler } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Node in the Radix Router tree.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export declare class RadixNode {
|
|
8
|
+
segment: string;
|
|
9
|
+
type: NodeType;
|
|
10
|
+
children: Map<string, RadixNode>;
|
|
11
|
+
paramChild: RadixNode | null;
|
|
12
|
+
wildcardChild: RadixNode | null;
|
|
13
|
+
handlers: Map<HttpMethod, RouteHandler[]>;
|
|
14
|
+
paramName: string | null;
|
|
15
|
+
regex: RegExp | null;
|
|
16
|
+
constructor(segment?: string, type?: NodeType);
|
|
17
|
+
toJSON(): any;
|
|
18
|
+
static fromJSON(json: any): RadixNode;
|
|
19
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HttpMethod } from '../../http/types';
|
|
2
|
+
import { type RouteHandler, type RouteMatch } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* High-performance Radix Tree Router for Bun
|
|
5
|
+
*/
|
|
6
|
+
export declare class RadixRouter {
|
|
7
|
+
private root;
|
|
8
|
+
private globalConstraints;
|
|
9
|
+
private routeCache;
|
|
10
|
+
/**
|
|
11
|
+
* Add a generic parameter constraint
|
|
12
|
+
*/
|
|
13
|
+
where(param: string, regex: RegExp): void;
|
|
14
|
+
/**
|
|
15
|
+
* Register a route
|
|
16
|
+
*/
|
|
17
|
+
add(method: HttpMethod, path: string, handlers: RouteHandler[]): void;
|
|
18
|
+
/**
|
|
19
|
+
* Match a request
|
|
20
|
+
*/
|
|
21
|
+
match(method: string, path: string): RouteMatch | null;
|
|
22
|
+
private matchRecursive;
|
|
23
|
+
private splitPath;
|
|
24
|
+
/**
|
|
25
|
+
* Serialize the router to a JSON string
|
|
26
|
+
*/
|
|
27
|
+
serialize(): string;
|
|
28
|
+
/**
|
|
29
|
+
* Restore a router from a serialized JSON string
|
|
30
|
+
*/
|
|
31
|
+
static fromSerialized(json: string): RadixRouter;
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Handler type (simplified for internal use)
|
|
3
|
+
* In the full framework this will align with GravitoHandler
|
|
4
|
+
*/
|
|
5
|
+
export type RouteHandler = Function;
|
|
6
|
+
/**
|
|
7
|
+
* Route Match Result
|
|
8
|
+
*/
|
|
9
|
+
export interface RouteMatch {
|
|
10
|
+
handlers: RouteHandler[];
|
|
11
|
+
params: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Radix Node Type
|
|
15
|
+
*/
|
|
16
|
+
export declare enum NodeType {
|
|
17
|
+
STATIC = 0,
|
|
18
|
+
PARAM = 1,
|
|
19
|
+
WILDCARD = 2
|
|
20
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview HTTP Adapter Interface for Gravito Framework
|
|
3
|
+
*
|
|
4
|
+
* This module defines the contract that all HTTP adapters must implement.
|
|
5
|
+
* By programming to this interface, Gravito can swap out the underlying
|
|
6
|
+
* HTTP engine without changing application code.
|
|
7
|
+
*
|
|
8
|
+
* @module @gravito/core/adapters
|
|
9
|
+
* @since 2.0.0
|
|
10
|
+
*/
|
|
11
|
+
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoVariables, HttpMethod } from '../http/types';
|
|
12
|
+
/**
|
|
13
|
+
* Configuration options for HTTP adapters
|
|
14
|
+
*/
|
|
15
|
+
export interface AdapterConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Base path prefix for all routes
|
|
18
|
+
* @default ''
|
|
19
|
+
*/
|
|
20
|
+
basePath?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Whether to enable strict routing (trailing slashes matter)
|
|
23
|
+
* @default false
|
|
24
|
+
*/
|
|
25
|
+
strictRouting?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Custom options passed to the underlying HTTP engine
|
|
28
|
+
*/
|
|
29
|
+
engineOptions?: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Route definition structure
|
|
33
|
+
*/
|
|
34
|
+
export interface RouteDefinition {
|
|
35
|
+
method: HttpMethod;
|
|
36
|
+
path: string;
|
|
37
|
+
handlers: (GravitoHandler | GravitoMiddleware)[];
|
|
38
|
+
name?: string;
|
|
39
|
+
middleware?: GravitoMiddleware[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* HttpAdapter - The core interface for HTTP engine abstraction
|
|
43
|
+
*
|
|
44
|
+
* Any HTTP engine (Photon, Express, Fastify, custom Bun implementation)
|
|
45
|
+
* must implement this interface to be usable with Gravito.
|
|
46
|
+
*
|
|
47
|
+
* @typeParam V - Context variables type
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // Using the default Photon adapter
|
|
52
|
+
* import { PhotonAdapter } from '@gravito/core/adapters'
|
|
53
|
+
*
|
|
54
|
+
* const core = new PlanetCore({
|
|
55
|
+
* adapter: new PhotonAdapter()
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* // Using a custom adapter
|
|
59
|
+
* import { BunNativeAdapter } from '@gravito/adapter-bun'
|
|
60
|
+
*
|
|
61
|
+
* const core = new PlanetCore({
|
|
62
|
+
* adapter: new BunNativeAdapter()
|
|
63
|
+
* })
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export interface HttpAdapter<V extends GravitoVariables = GravitoVariables> {
|
|
67
|
+
/**
|
|
68
|
+
* Adapter name for identification
|
|
69
|
+
* @example 'photon', 'bun-native', 'express'
|
|
70
|
+
*/
|
|
71
|
+
readonly name: string;
|
|
72
|
+
/**
|
|
73
|
+
* Adapter version
|
|
74
|
+
*/
|
|
75
|
+
readonly version: string;
|
|
76
|
+
/**
|
|
77
|
+
* Access the underlying native HTTP engine instance.
|
|
78
|
+
*
|
|
79
|
+
* ⚠️ WARNING: Using this ties your code to a specific adapter.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // For Photon adapter
|
|
84
|
+
* const photonApp = adapter.native as Photon
|
|
85
|
+
*
|
|
86
|
+
* // For custom Bun adapter
|
|
87
|
+
* const bunApp = adapter.native as BunApp
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
readonly native: unknown;
|
|
91
|
+
/**
|
|
92
|
+
* Register a route with the adapter
|
|
93
|
+
*
|
|
94
|
+
* @param method - HTTP method
|
|
95
|
+
* @param path - Route path (may include parameters like ':id')
|
|
96
|
+
* @param handlers - One or more handlers for this route (handlers or middleware)
|
|
97
|
+
*/
|
|
98
|
+
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
|
|
99
|
+
/**
|
|
100
|
+
* Register multiple routes at once
|
|
101
|
+
*
|
|
102
|
+
* @param routes - Array of route definitions
|
|
103
|
+
*/
|
|
104
|
+
routes(routes: RouteDefinition[]): void;
|
|
105
|
+
/**
|
|
106
|
+
* Register a middleware for a path
|
|
107
|
+
*
|
|
108
|
+
* @param path - Path pattern to match
|
|
109
|
+
* @param middleware - One or more middleware functions
|
|
110
|
+
*/
|
|
111
|
+
use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
112
|
+
/**
|
|
113
|
+
* Register a global middleware (applied to all routes)
|
|
114
|
+
*
|
|
115
|
+
* @param middleware - Middleware function
|
|
116
|
+
*/
|
|
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;
|
|
145
|
+
/**
|
|
146
|
+
* Mount a sub-adapter at a path
|
|
147
|
+
*
|
|
148
|
+
* @param path - Mount path
|
|
149
|
+
* @param subAdapter - The adapter to mount
|
|
150
|
+
*/
|
|
151
|
+
mount(path: string, subAdapter: HttpAdapter<V>): void;
|
|
152
|
+
/**
|
|
153
|
+
* Set the error handler
|
|
154
|
+
*
|
|
155
|
+
* @param handler - Error handler function
|
|
156
|
+
*/
|
|
157
|
+
onError(handler: GravitoErrorHandler<V>): void;
|
|
158
|
+
/**
|
|
159
|
+
* Set the not-found handler
|
|
160
|
+
*
|
|
161
|
+
* @param handler - Not-found handler function
|
|
162
|
+
*/
|
|
163
|
+
onNotFound(handler: GravitoNotFoundHandler<V>): void;
|
|
164
|
+
/**
|
|
165
|
+
* The main fetch handler for serving requests.
|
|
166
|
+
*
|
|
167
|
+
* This is compatible with `Bun.serve()`, Cloudflare Workers,
|
|
168
|
+
* and other fetch-based runtimes.
|
|
169
|
+
*
|
|
170
|
+
* @param request - Incoming HTTP request
|
|
171
|
+
* @param server - Optional server context (Bun.Server, etc.)
|
|
172
|
+
* @returns HTTP response
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* // With Bun.serve
|
|
177
|
+
* Bun.serve({
|
|
178
|
+
* port: 3000,
|
|
179
|
+
* fetch: adapter.fetch
|
|
180
|
+
* })
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
fetch(request: Request, server?: unknown): Response | Promise<Response>;
|
|
184
|
+
/**
|
|
185
|
+
* Predictive Route Warming (JIT Optimization)
|
|
186
|
+
*
|
|
187
|
+
* Simulates requests to specified routes to trigger JIT compilation (FTL)
|
|
188
|
+
* before real traffic arrives.
|
|
189
|
+
*
|
|
190
|
+
* @param paths List of paths to warm up (e.g. ['/api/users', '/health'])
|
|
191
|
+
* @since 2.1.0
|
|
192
|
+
*/
|
|
193
|
+
warmup?(paths: string[]): Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* WebSocket Handler for Bun.serve
|
|
196
|
+
*
|
|
197
|
+
* @since 2.2.0
|
|
198
|
+
*/
|
|
199
|
+
websocket?: {
|
|
200
|
+
open?(ws: unknown): void | Promise<void>;
|
|
201
|
+
message?(ws: unknown, message: string | Buffer | Uint8Array): void | Promise<void>;
|
|
202
|
+
close?(ws: unknown, code: number, message: string): void | Promise<void>;
|
|
203
|
+
drain?(ws: unknown): void | Promise<void>;
|
|
204
|
+
[key: string]: unknown;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Initialize the adapter
|
|
208
|
+
*
|
|
209
|
+
* Called during PlanetCore.boot()
|
|
210
|
+
*/
|
|
211
|
+
init?(): void | Promise<void>;
|
|
212
|
+
/**
|
|
213
|
+
* Cleanup resources
|
|
214
|
+
*
|
|
215
|
+
* Called during graceful shutdown
|
|
216
|
+
*/
|
|
217
|
+
shutdown?(): void | Promise<void>;
|
|
218
|
+
/**
|
|
219
|
+
* Create a GravitoContext from a raw request.
|
|
220
|
+
*
|
|
221
|
+
* This is used internally for testing and advanced scenarios.
|
|
222
|
+
*
|
|
223
|
+
* @param request - Raw HTTP request
|
|
224
|
+
* @returns Gravito context
|
|
225
|
+
*/
|
|
226
|
+
createContext(request: Request): GravitoContext<V>;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Factory function type for creating adapters
|
|
230
|
+
*/
|
|
231
|
+
export type AdapterFactory<V extends GravitoVariables = GravitoVariables> = (config?: AdapterConfig) => HttpAdapter<V>;
|
|
232
|
+
/**
|
|
233
|
+
* Check if a value is an HttpAdapter
|
|
234
|
+
*/
|
|
235
|
+
export declare function isHttpAdapter(value: unknown): value is HttpAdapter;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BinaryUtils - 統一的二進制轉換工具類
|
|
3
|
+
*
|
|
4
|
+
* 支援 Bun 原生 API 優化,並提供 Node.js 回退路徑。
|
|
5
|
+
* 設計為 isomorphic,可在 Bun 和 Node.js 環境中運行。
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* BinaryUtils - 提供統一的二進制轉換、編碼、壓縮工具
|
|
10
|
+
* 所有方法均為靜態方法,可直接呼叫
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare class BinaryUtils {
|
|
14
|
+
/**
|
|
15
|
+
* 將各種資料類型轉換為 Uint8Array
|
|
16
|
+
* @param data - 輸入資料(Blob、Buffer、string、ArrayBuffer 或 Uint8Array)
|
|
17
|
+
* @returns Uint8Array
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
static toUint8Array(data: Blob | Buffer | string | ArrayBuffer | Uint8Array): Promise<Uint8Array>;
|
|
21
|
+
/**
|
|
22
|
+
* 將各種資料類型轉換為 ArrayBuffer
|
|
23
|
+
* @param data - 輸入資料
|
|
24
|
+
* @returns ArrayBuffer
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
static toArrayBuffer(data: Blob | Buffer | string | Uint8Array | ArrayBuffer): Promise<ArrayBuffer>;
|
|
28
|
+
/**
|
|
29
|
+
* 將 Uint8Array 編碼為 Base64 字串
|
|
30
|
+
* 在 Bun 環境中使用原生 toBase64(),否則使用 Buffer
|
|
31
|
+
* @param data - 輸入的 Uint8Array
|
|
32
|
+
* @returns Base64 字串
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
static toBase64(data: Uint8Array): string;
|
|
36
|
+
/**
|
|
37
|
+
* 將 Base64 字串解碼為 Uint8Array
|
|
38
|
+
* @param base64 - Base64 字串
|
|
39
|
+
* @returns Uint8Array
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
static fromBase64(base64: string): Uint8Array;
|
|
43
|
+
/**
|
|
44
|
+
* 將 Uint8Array 編碼為 Base64URL 字串(URL 安全,無填充)
|
|
45
|
+
* @param data - 輸入的 Uint8Array
|
|
46
|
+
* @returns Base64URL 字串
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
static toBase64Url(data: Uint8Array): string;
|
|
50
|
+
/**
|
|
51
|
+
* 將 Base64URL 字串解碼為 Uint8Array
|
|
52
|
+
* @param base64url - Base64URL 字串
|
|
53
|
+
* @returns Uint8Array
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
static fromBase64Url(base64url: string): Uint8Array;
|
|
57
|
+
/**
|
|
58
|
+
* 將 Uint8Array 編碼為十六進制字串
|
|
59
|
+
* 在 Bun 環境中使用原生 toHex(),否則使用 Buffer
|
|
60
|
+
* @param data - 輸入的 Uint8Array
|
|
61
|
+
* @returns 十六進制字串(小寫)
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
static toHex(data: Uint8Array): string;
|
|
65
|
+
/**
|
|
66
|
+
* 將十六進制字串解碼為 Uint8Array
|
|
67
|
+
* @param hex - 十六進制字串
|
|
68
|
+
* @returns Uint8Array
|
|
69
|
+
* @throws {TypeError} 如果輸入不是有效的十六進制字串
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
static fromHex(hex: string): Uint8Array;
|
|
73
|
+
/**
|
|
74
|
+
* 使用 gzip 壓縮資料
|
|
75
|
+
* 在 Bun 環境中使用原生 Bun.gzipSync(),在 Node.js 中使用 zlib
|
|
76
|
+
* @param data - 輸入資料
|
|
77
|
+
* @param level - 壓縮等級(0-9),預設為 6
|
|
78
|
+
* @returns 壓縮後的 Uint8Array
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
81
|
+
static gzip(data: Uint8Array, level?: number): Promise<Uint8Array>;
|
|
82
|
+
/**
|
|
83
|
+
* 解壓縮 gzip 資料
|
|
84
|
+
* 在 Bun 環境中使用原生 Bun.gunzipSync(),在 Node.js 中使用 zlib
|
|
85
|
+
* @param data - gzip 壓縮的 Uint8Array
|
|
86
|
+
* @returns 解壓縮後的 Uint8Array
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
static gunzip(data: Uint8Array): Promise<Uint8Array>;
|
|
90
|
+
/**
|
|
91
|
+
* 將 ReadableStream<Uint8Array> 讀取為 Uint8Array
|
|
92
|
+
* 在 Bun 環境中使用原生 Bun.readableStreamToBytes(),否則手動讀取 chunks
|
|
93
|
+
* @param stream - 輸入的 ReadableStream
|
|
94
|
+
* @returns Uint8Array
|
|
95
|
+
* @public
|
|
96
|
+
*/
|
|
97
|
+
static readableStreamToBytes(stream: ReadableStream<Uint8Array>): Promise<Uint8Array>;
|
|
98
|
+
/**
|
|
99
|
+
* 將 ReadableStream<Uint8Array> 讀取為 ArrayBuffer
|
|
100
|
+
* @param stream - 輸入的 ReadableStream
|
|
101
|
+
* @returns ArrayBuffer
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
static readableStreamToArrayBuffer(stream: ReadableStream<Uint8Array>): Promise<ArrayBuffer>;
|
|
105
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AOT (Ahead-of-Time) Router
|
|
3
|
+
*
|
|
4
|
+
* Hybrid routing strategy:
|
|
5
|
+
* - Static routes: O(1) Map lookup
|
|
6
|
+
* - Dynamic routes: Optimized Radix Tree
|
|
7
|
+
*
|
|
8
|
+
* The key optimization is separating static from dynamic routes at registration time,
|
|
9
|
+
* not at match time. This eliminates unnecessary tree traversal for static paths.
|
|
10
|
+
*
|
|
11
|
+
* @module @gravito/core/engine
|
|
12
|
+
*/
|
|
13
|
+
import type { HttpMethod } from '../http/types';
|
|
14
|
+
import type { Handler, Middleware, RouteMatch, RouteMetadata } from './types';
|
|
15
|
+
/**
|
|
16
|
+
* Route definition for re-playing routes (mounting)
|
|
17
|
+
*/
|
|
18
|
+
interface RouteDefinition {
|
|
19
|
+
method: HttpMethod;
|
|
20
|
+
path: string;
|
|
21
|
+
handler: Handler;
|
|
22
|
+
middleware: Middleware[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* AOT Router - Optimized for Bun
|
|
26
|
+
*/
|
|
27
|
+
export declare class AOTRouter {
|
|
28
|
+
/** @internal */
|
|
29
|
+
readonly staticRoutes: Map<string, RouteMetadata>;
|
|
30
|
+
private dynamicRouter;
|
|
31
|
+
/** @internal */
|
|
32
|
+
readonly routeDefinitions: RouteDefinition[];
|
|
33
|
+
/** @internal */
|
|
34
|
+
readonly globalMiddleware: Middleware[];
|
|
35
|
+
/** @internal */
|
|
36
|
+
readonly pathMiddleware: Map<string, Middleware[]>;
|
|
37
|
+
private dynamicRoutePatterns;
|
|
38
|
+
private middlewareCache;
|
|
39
|
+
private cacheMaxSize;
|
|
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;
|
|
46
|
+
/**
|
|
47
|
+
* Register a route
|
|
48
|
+
*
|
|
49
|
+
* Automatically determines if route is static or dynamic.
|
|
50
|
+
* Static routes are stored in a Map for O(1) lookup.
|
|
51
|
+
* Dynamic routes use the Radix Tree.
|
|
52
|
+
*
|
|
53
|
+
* @param method - HTTP method
|
|
54
|
+
* @param path - Route path
|
|
55
|
+
* @param handler - Route handler
|
|
56
|
+
* @param middleware - Route-specific middleware
|
|
57
|
+
*/
|
|
58
|
+
add(method: HttpMethod, path: string, handler: Handler, middleware?: Middleware[]): void;
|
|
59
|
+
/**
|
|
60
|
+
* Mount another router at a prefix
|
|
61
|
+
*/
|
|
62
|
+
mount(prefix: string, other: AOTRouter): void;
|
|
63
|
+
/**
|
|
64
|
+
* Add global middleware
|
|
65
|
+
*
|
|
66
|
+
* These run for every request, before route-specific middleware.
|
|
67
|
+
*
|
|
68
|
+
* @param middleware - Middleware functions
|
|
69
|
+
*/
|
|
70
|
+
use(...middleware: Middleware[]): void;
|
|
71
|
+
/**
|
|
72
|
+
* Add path-based middleware
|
|
73
|
+
*
|
|
74
|
+
* Supports wildcard patterns like '/api/*'
|
|
75
|
+
*
|
|
76
|
+
* @param pattern - Path pattern
|
|
77
|
+
* @param middleware - Middleware functions
|
|
78
|
+
*/
|
|
79
|
+
usePattern(pattern: string, ...middleware: Middleware[]): void;
|
|
80
|
+
/**
|
|
81
|
+
* Match a request to a route
|
|
82
|
+
*
|
|
83
|
+
* Returns the handler, params, and all applicable middleware.
|
|
84
|
+
*
|
|
85
|
+
* @param method - HTTP method
|
|
86
|
+
* @param path - Request path
|
|
87
|
+
* @returns Route match or null if not found
|
|
88
|
+
*/
|
|
89
|
+
match(method: string, path: string): RouteMatch;
|
|
90
|
+
/**
|
|
91
|
+
* Public wrapper for collectMiddleware (used by Gravito for optimization)
|
|
92
|
+
*/
|
|
93
|
+
collectMiddlewarePublic(path: string, routeMiddleware: Middleware[]): Middleware[];
|
|
94
|
+
/**
|
|
95
|
+
* Collect all applicable middleware for a path
|
|
96
|
+
*
|
|
97
|
+
* Order: global -> pattern-based -> route-specific
|
|
98
|
+
*
|
|
99
|
+
* @param path - Request path
|
|
100
|
+
* @param routeMiddleware - Route-specific middleware
|
|
101
|
+
* @returns Combined middleware array
|
|
102
|
+
*/
|
|
103
|
+
private collectMiddleware;
|
|
104
|
+
/**
|
|
105
|
+
* Get all static routes optimized for Bun's native router.
|
|
106
|
+
*
|
|
107
|
+
* Unlike basic offloading, this version supports routes with middleware
|
|
108
|
+
* by pre-compiling the middleware chain into a single native handler.
|
|
109
|
+
*
|
|
110
|
+
* @param onMatch - Factory to wrap handler and middleware into a Bun-compatible function
|
|
111
|
+
* @returns Record of path -> Handler (Bun-compatible)
|
|
112
|
+
*/
|
|
113
|
+
getNativeRoutes(onMatch: (handler: Handler, middleware: Middleware[], path: string) => (req: Request) => Response | Promise<Response>): Record<string, any>;
|
|
114
|
+
/**
|
|
115
|
+
* Check if a path is static (no parameters or wildcards)
|
|
116
|
+
*/
|
|
117
|
+
private isStaticPath;
|
|
118
|
+
/**
|
|
119
|
+
* Match a pattern against a path
|
|
120
|
+
*
|
|
121
|
+
* Supports:
|
|
122
|
+
* - Exact match: '/api/users'
|
|
123
|
+
* - Wildcard suffix: '/api/*'
|
|
124
|
+
*
|
|
125
|
+
* @param pattern - Pattern to match
|
|
126
|
+
* @param path - Path to test
|
|
127
|
+
* @returns True if pattern matches
|
|
128
|
+
*/
|
|
129
|
+
private matchPattern;
|
|
130
|
+
/**
|
|
131
|
+
* Get all registered routes (for debugging)
|
|
132
|
+
*/
|
|
133
|
+
getRoutes(): Array<{
|
|
134
|
+
method: string;
|
|
135
|
+
path: string;
|
|
136
|
+
type: 'static' | 'dynamic';
|
|
137
|
+
}>;
|
|
138
|
+
}
|
|
139
|
+
export {};
|