@gravito/core 1.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -6
- package/README.zh-TW.md +101 -6
- package/dist/Application.d.ts +256 -0
- package/dist/CommandKernel.d.ts +33 -0
- package/dist/ConfigManager.d.ts +65 -0
- package/dist/Container/RequestScopeManager.d.ts +62 -0
- package/dist/Container/RequestScopeMetrics.d.ts +144 -0
- package/dist/Container.d.ts +153 -0
- package/dist/ErrorHandler.d.ts +66 -0
- package/dist/Event.d.ts +5 -0
- package/dist/EventManager.d.ts +123 -0
- package/dist/GlobalErrorHandlers.d.ts +47 -0
- package/dist/GravitoServer.d.ts +28 -0
- package/dist/HookManager.d.ts +435 -0
- package/dist/Listener.d.ts +4 -0
- package/dist/Logger.d.ts +20 -0
- package/dist/PlanetCore.d.ts +402 -0
- package/dist/RequestContext.d.ts +97 -0
- package/dist/Route.d.ts +36 -0
- package/dist/Router.d.ts +270 -0
- package/dist/ServiceProvider.d.ts +178 -0
- package/dist/adapters/GravitoEngineAdapter.d.ts +27 -0
- package/dist/adapters/bun/AdaptiveAdapter.d.ts +99 -0
- package/dist/adapters/bun/BunContext.d.ts +54 -0
- package/dist/adapters/bun/BunNativeAdapter.d.ts +66 -0
- package/dist/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/adapters/bun/RadixRouter.d.ts +32 -0
- package/dist/adapters/bun/index.d.ts +7 -0
- package/dist/adapters/bun/types.d.ts +20 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/types.d.ts +235 -0
- package/dist/binary/BinaryUtils.d.ts +105 -0
- package/dist/binary/index.d.ts +5 -0
- package/dist/cli/queue-commands.d.ts +6 -0
- package/dist/compat/async-local-storage.browser.d.ts +9 -0
- package/dist/compat/async-local-storage.d.ts +7 -0
- package/dist/compat/crypto.browser.d.ts +5 -0
- package/dist/compat/crypto.d.ts +6 -0
- package/dist/compat.cjs +42 -11
- package/dist/compat.cjs.map +9 -0
- package/dist/compat.d.ts +23 -1
- package/dist/compat.js +3 -0
- package/dist/compat.js.map +9 -0
- package/dist/engine/AOTRouter.d.ts +139 -0
- package/dist/engine/FastContext.d.ts +141 -0
- package/dist/engine/Gravito.d.ts +131 -0
- package/dist/engine/MinimalContext.d.ts +102 -0
- package/dist/engine/analyzer.d.ts +113 -0
- package/dist/engine/constants.d.ts +23 -0
- package/dist/engine/index.cjs +758 -532
- package/dist/engine/index.cjs.map +22 -0
- package/dist/engine/index.d.ts +14 -690
- package/dist/engine/index.js +758 -508
- package/dist/engine/index.js.map +22 -0
- package/dist/engine/path.d.ts +26 -0
- package/dist/engine/pool.d.ts +83 -0
- package/dist/engine/types.d.ts +149 -0
- package/dist/error-handling/RequestScopeErrorContext.d.ts +126 -0
- package/dist/events/BackpressureManager.d.ts +215 -0
- package/dist/events/CircuitBreaker.d.ts +229 -0
- package/dist/events/DeadLetterQueue.d.ts +219 -0
- package/dist/events/EventBackend.d.ts +12 -0
- package/dist/events/EventOptions.d.ts +204 -0
- package/dist/events/EventPriorityQueue.d.ts +63 -0
- package/dist/events/FlowControlStrategy.d.ts +109 -0
- package/dist/events/IdempotencyCache.d.ts +60 -0
- package/dist/events/MessageQueueBridge.d.ts +184 -0
- package/dist/events/PriorityEscalationManager.d.ts +82 -0
- package/dist/events/RetryScheduler.d.ts +104 -0
- package/dist/events/WorkerPool.d.ts +98 -0
- package/dist/events/WorkerPoolConfig.d.ts +153 -0
- package/dist/events/WorkerPoolMetrics.d.ts +65 -0
- package/dist/events/aggregation/AggregationWindow.d.ts +77 -0
- package/dist/events/aggregation/DeduplicationManager.d.ts +135 -0
- package/dist/events/aggregation/EventAggregationManager.d.ts +108 -0
- package/dist/events/aggregation/EventBatcher.d.ts +99 -0
- package/dist/events/aggregation/index.d.ts +10 -0
- package/dist/events/aggregation/types.d.ts +117 -0
- package/dist/events/index.d.ts +26 -0
- package/dist/events/observability/EventMetrics.d.ts +132 -0
- package/dist/events/observability/EventTracer.d.ts +68 -0
- package/dist/events/observability/EventTracing.d.ts +161 -0
- package/dist/events/observability/OTelEventMetrics.d.ts +332 -0
- package/dist/events/observability/ObservableHookManager.d.ts +108 -0
- package/dist/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/events/observability/index.d.ts +24 -0
- package/dist/events/observability/metrics-types.d.ts +16 -0
- package/dist/events/queue-core.d.ts +77 -0
- package/dist/events/task-executor.d.ts +51 -0
- package/dist/events/types.d.ts +134 -0
- package/dist/exceptions/AuthenticationException.d.ts +8 -0
- package/dist/exceptions/AuthorizationException.d.ts +8 -0
- package/dist/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/exceptions/GravitoException.d.ts +23 -0
- package/dist/exceptions/HttpException.d.ts +9 -0
- package/dist/exceptions/ModelNotFoundException.d.ts +10 -0
- package/dist/exceptions/ValidationException.d.ts +22 -0
- package/dist/exceptions/index.d.ts +7 -0
- package/dist/ffi/NativeAccelerator.d.ts +69 -0
- package/dist/ffi/NativeHasher.d.ts +139 -0
- package/dist/ffi/cbor-fallback.d.ts +96 -0
- package/dist/ffi/hash-fallback.d.ts +33 -0
- package/dist/ffi/index.cjs +621 -0
- package/dist/ffi/index.cjs.map +14 -0
- package/dist/ffi/index.d.ts +10 -0
- package/dist/ffi/index.js +602 -0
- package/dist/ffi/index.js.map +14 -0
- package/dist/ffi/types.d.ts +135 -0
- package/dist/health/HealthProvider.d.ts +67 -0
- package/dist/helpers/Arr.d.ts +19 -0
- package/dist/helpers/Str.d.ts +38 -0
- package/dist/helpers/data.d.ts +25 -0
- package/dist/helpers/errors.d.ts +34 -0
- package/dist/helpers/response.d.ts +41 -0
- package/dist/helpers.d.ts +338 -0
- package/dist/hooks/ActionManager.d.ts +132 -0
- package/dist/hooks/AsyncDetector.d.ts +84 -0
- package/dist/hooks/FilterManager.d.ts +71 -0
- package/dist/hooks/MigrationWarner.d.ts +24 -0
- package/dist/hooks/dlq-operations.d.ts +60 -0
- package/dist/hooks/index.d.ts +11 -0
- package/dist/hooks/types.d.ts +107 -0
- package/dist/http/CookieJar.d.ts +51 -0
- package/dist/http/cookie.d.ts +29 -0
- package/dist/http/index.d.ts +12 -0
- package/dist/{compat-C4Src6NN.d.cts → http/types.d.ts} +48 -16
- package/dist/index.browser.d.ts +34 -0
- package/dist/index.cjs +12909 -9492
- package/dist/index.cjs.map +168 -0
- package/dist/index.d.ts +58 -8707
- package/dist/index.js +12906 -9381
- package/dist/index.js.map +168 -0
- package/dist/observability/QueueDashboard.d.ts +136 -0
- package/dist/observability/contracts.d.ts +137 -0
- package/dist/observability/index.d.ts +13 -0
- package/dist/reliability/DeadLetterQueueManager.d.ts +349 -0
- package/dist/reliability/RetryPolicy.d.ts +217 -0
- package/dist/reliability/index.d.ts +6 -0
- package/dist/router/ControllerDispatcher.d.ts +12 -0
- package/dist/router/RequestValidator.d.ts +20 -0
- package/dist/runtime/adapter-bun.d.ts +12 -0
- package/dist/runtime/adapter-deno.d.ts +12 -0
- package/dist/runtime/adapter-node.d.ts +12 -0
- package/dist/runtime/adapter-unknown.d.ts +13 -0
- package/dist/runtime/archive.d.ts +17 -0
- package/dist/runtime/compression.d.ts +21 -0
- package/dist/runtime/deep-equals.d.ts +56 -0
- package/dist/runtime/detection.d.ts +22 -0
- package/dist/runtime/escape.d.ts +34 -0
- package/dist/runtime/index.browser.d.ts +20 -0
- package/dist/runtime/index.d.ts +44 -0
- package/dist/runtime/markdown.d.ts +44 -0
- package/dist/runtime/types.d.ts +436 -0
- package/dist/runtime-helpers.d.ts +67 -0
- package/dist/runtime.d.ts +11 -0
- package/dist/security/Encrypter.d.ts +33 -0
- package/dist/security/Hasher.d.ts +29 -0
- package/dist/testing/HttpTester.d.ts +39 -0
- package/dist/testing/TestResponse.d.ts +78 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/transpiler-utils.d.ts +170 -0
- package/dist/types/events.d.ts +94 -0
- package/dist/types.d.ts +13 -0
- package/package.json +23 -53
- package/src/ffi/native/cbor.c +1148 -0
- package/dist/compat-C4Src6NN.d.ts +0 -357
- package/dist/compat.d.cts +0 -1
- package/dist/engine/index.d.cts +0 -702
- package/dist/index.d.cts +0 -8734
package/dist/Router.d.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import type { GravitoHandler, GravitoMiddleware, HttpMethod, ProxyOptions } from './http/types';
|
|
2
|
+
import type { PlanetCore } from './PlanetCore';
|
|
3
|
+
import { Route } from './Route';
|
|
4
|
+
/**
|
|
5
|
+
* Type for Controller Class Constructor
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export type ControllerClass = new (core: PlanetCore) => any;
|
|
9
|
+
/**
|
|
10
|
+
* Handler can be a function or [Class, 'methodName']
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export type RouteHandler = GravitoHandler | [ControllerClass, string];
|
|
14
|
+
/**
|
|
15
|
+
* Interface for FormRequest classes (from @gravito/impulse).
|
|
16
|
+
* Used for duck-typing detection without hard dependency.
|
|
17
|
+
*/
|
|
18
|
+
export interface FormRequestLike {
|
|
19
|
+
schema: unknown;
|
|
20
|
+
source?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Validate the request context.
|
|
23
|
+
* @param ctx - The request context
|
|
24
|
+
*/
|
|
25
|
+
validate?(ctx: unknown): Promise<{
|
|
26
|
+
success: boolean;
|
|
27
|
+
data?: unknown;
|
|
28
|
+
error?: unknown;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Type for FormRequest class constructor
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export type FormRequestClass = new () => FormRequestLike;
|
|
36
|
+
/**
|
|
37
|
+
* Symbol to mark FormRequest classes for fast identification.
|
|
38
|
+
* FormRequest classes from @gravito/impulse should set this symbol.
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* Symbol to mark FormRequest classes for fast identification.
|
|
42
|
+
* FormRequest classes from @gravito/impulse should set this symbol.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare const FORM_REQUEST_SYMBOL: unique symbol;
|
|
46
|
+
/**
|
|
47
|
+
* Options for route definitions
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export interface RouteOptions {
|
|
51
|
+
/** Route prefix path */
|
|
52
|
+
prefix?: string;
|
|
53
|
+
/** Domain/Hostname constraint */
|
|
54
|
+
domain?: string;
|
|
55
|
+
/** Middleware stack for the route */
|
|
56
|
+
middleware?: GravitoMiddleware[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Common routing handler argument definition
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export type RouteDefinitionArg = FormRequestClass | RouteHandler | GravitoMiddleware | GravitoMiddleware[];
|
|
63
|
+
/**
|
|
64
|
+
* Interface merging for HTTP routing methods to establish overloads
|
|
65
|
+
* without duplicate bodies.
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export interface RoutingMethods {
|
|
69
|
+
get(path: string, handler: RouteHandler): Route;
|
|
70
|
+
get(path: string, request: FormRequestClass, handler: RouteHandler): Route;
|
|
71
|
+
get(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
|
|
72
|
+
post(path: string, handler: RouteHandler): Route;
|
|
73
|
+
post(path: string, request: FormRequestClass, handler: RouteHandler): Route;
|
|
74
|
+
post(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
|
|
75
|
+
put(path: string, handler: RouteHandler): Route;
|
|
76
|
+
put(path: string, request: FormRequestClass, handler: RouteHandler): Route;
|
|
77
|
+
put(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
|
|
78
|
+
delete(path: string, handler: RouteHandler): Route;
|
|
79
|
+
delete(path: string, request: FormRequestClass, handler: RouteHandler): Route;
|
|
80
|
+
delete(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
|
|
81
|
+
patch(path: string, handler: RouteHandler): Route;
|
|
82
|
+
patch(path: string, request: FormRequestClass, handler: RouteHandler): Route;
|
|
83
|
+
patch(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* RouteGroup
|
|
87
|
+
* Helper class for chained route configuration (prefix, domain, etc.)
|
|
88
|
+
*/
|
|
89
|
+
/**
|
|
90
|
+
* RouteGroup
|
|
91
|
+
* Helper class for chained route configuration (prefix, domain, etc.)
|
|
92
|
+
* @public
|
|
93
|
+
*/
|
|
94
|
+
export interface RouteGroup extends RoutingMethods {
|
|
95
|
+
}
|
|
96
|
+
export declare class RouteGroup {
|
|
97
|
+
private router;
|
|
98
|
+
private options;
|
|
99
|
+
constructor(router: Router, options: RouteOptions);
|
|
100
|
+
/**
|
|
101
|
+
* Add a prefix to the current group
|
|
102
|
+
*/
|
|
103
|
+
prefix(path: string): RouteGroup;
|
|
104
|
+
/**
|
|
105
|
+
* Add middleware to the current group.
|
|
106
|
+
* Accepts individual handlers or arrays of handlers.
|
|
107
|
+
*/
|
|
108
|
+
middleware(...handlers: (GravitoMiddleware | GravitoMiddleware[])[]): RouteGroup;
|
|
109
|
+
/**
|
|
110
|
+
* Define routes within this group
|
|
111
|
+
*/
|
|
112
|
+
group(callback: (router: Router | RouteGroup) => void): void;
|
|
113
|
+
resource(name: string, controller: ControllerClass, options?: ResourceOptions): void;
|
|
114
|
+
/**
|
|
115
|
+
* Register a route that forwards requests to another URL (Gateway Proxy).
|
|
116
|
+
* @param method - HTTP method or 'all'
|
|
117
|
+
* @param path - Local route path
|
|
118
|
+
* @param target - Remote URL or base URL to forward to
|
|
119
|
+
* @param options - Optional proxy options
|
|
120
|
+
*/
|
|
121
|
+
forward(method: HttpMethod | HttpMethod[] | 'all', path: string, target: string, options?: ProxyOptions): void;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Gravito Router
|
|
125
|
+
*
|
|
126
|
+
* Provides a Laravel-like fluent API for defining routes.
|
|
127
|
+
* Supports:
|
|
128
|
+
* - Controller-based routing: router.get('/', [HomeController, 'index'])
|
|
129
|
+
* - Route groups with prefixes: router.prefix('/api').group(...)
|
|
130
|
+
* - Domain-based routing: router.domain('api.app').group(...)
|
|
131
|
+
* - Middleware chaining: router.middleware(auth).group(...)
|
|
132
|
+
* - FormRequest validation: router.post('/users', StoreUserRequest, [UserController, 'store'])
|
|
133
|
+
* - Inline Middleware: router.get('/users', authMiddleware, [UserController, 'index'])
|
|
134
|
+
*/
|
|
135
|
+
export interface Router extends RoutingMethods {
|
|
136
|
+
}
|
|
137
|
+
export declare class Router {
|
|
138
|
+
private core;
|
|
139
|
+
routes: Array<{
|
|
140
|
+
method: string;
|
|
141
|
+
path: string;
|
|
142
|
+
domain?: string;
|
|
143
|
+
}>;
|
|
144
|
+
private dispatcher;
|
|
145
|
+
private namedRoutes;
|
|
146
|
+
private bindings;
|
|
147
|
+
/**
|
|
148
|
+
* Compile all registered routes into a flat array for caching or manifest generation.
|
|
149
|
+
* Optimized: O(n) complexity using Set for lookups instead of O(n²) with Array.some()
|
|
150
|
+
*/
|
|
151
|
+
compile(): {
|
|
152
|
+
method: string;
|
|
153
|
+
path: string;
|
|
154
|
+
name?: string;
|
|
155
|
+
domain?: string | undefined;
|
|
156
|
+
}[];
|
|
157
|
+
/**
|
|
158
|
+
* Register a named route
|
|
159
|
+
*/
|
|
160
|
+
registerName(name: string, method: string, path: string, options?: RouteOptions): void;
|
|
161
|
+
/**
|
|
162
|
+
* Generate a URL from a named route.
|
|
163
|
+
*
|
|
164
|
+
* Replaces route parameters (e.g., `:id`) with provided values and appends
|
|
165
|
+
* query parameters to the URL.
|
|
166
|
+
*
|
|
167
|
+
* @param name - The name of the route.
|
|
168
|
+
* @param params - Key-value pairs for route parameters.
|
|
169
|
+
* @param query - Key-value pairs for query string parameters.
|
|
170
|
+
* @returns The generated URL string.
|
|
171
|
+
* @throws Error if the named route is not found or if a required parameter is missing.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const url = router.url('users.show', { id: 1 }, { tab: 'profile' });
|
|
176
|
+
* // Result: "/users/1?tab=profile"
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
url(name: string, params?: Record<string, string | number>, query?: Record<string, string | number | boolean | null | undefined>): string;
|
|
180
|
+
/**
|
|
181
|
+
* Export named routes as a serializable manifest (for caching).
|
|
182
|
+
*/
|
|
183
|
+
exportNamedRoutes(): Record<string, {
|
|
184
|
+
method: string;
|
|
185
|
+
path: string;
|
|
186
|
+
domain?: string;
|
|
187
|
+
}>;
|
|
188
|
+
/**
|
|
189
|
+
* Load named routes from a manifest (for caching).
|
|
190
|
+
*/
|
|
191
|
+
loadNamedRoutes(manifest: Record<string, {
|
|
192
|
+
method: string;
|
|
193
|
+
path: string;
|
|
194
|
+
domain?: string;
|
|
195
|
+
}>): void;
|
|
196
|
+
/**
|
|
197
|
+
* Register a route model binding.
|
|
198
|
+
*
|
|
199
|
+
* Automatically resolves a route parameter to an object using the provided
|
|
200
|
+
* resolver function. The resolved object is then available in the request context.
|
|
201
|
+
*
|
|
202
|
+
* @param param - The name of the route parameter to bind.
|
|
203
|
+
* @param resolver - An async function that resolves the parameter value to an object.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* router.bind('user', async (id) => await User.find(id));
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
bind(param: string, resolver: (id: string) => Promise<unknown>): void;
|
|
211
|
+
/**
|
|
212
|
+
* Register a route model binding for a Model class.
|
|
213
|
+
*/
|
|
214
|
+
model(param: string, modelClass: unknown): void;
|
|
215
|
+
constructor(core: PlanetCore);
|
|
216
|
+
/**
|
|
217
|
+
* Start a route group with a prefix
|
|
218
|
+
*/
|
|
219
|
+
prefix(path: string): RouteGroup;
|
|
220
|
+
/**
|
|
221
|
+
* Start a route group with a domain constraint
|
|
222
|
+
*/
|
|
223
|
+
domain(host: string): RouteGroup;
|
|
224
|
+
/**
|
|
225
|
+
* Start a route group with middleware.
|
|
226
|
+
* Accepts individual handlers or arrays of handlers.
|
|
227
|
+
*/
|
|
228
|
+
middleware(...handlers: (GravitoMiddleware | GravitoMiddleware[])[]): RouteGroup;
|
|
229
|
+
/**
|
|
230
|
+
* Register a route that forwards requests to another URL (Gateway Proxy).
|
|
231
|
+
* @param method - HTTP method or 'all'
|
|
232
|
+
* @param path - Local route path
|
|
233
|
+
* @param target - Remote URL or base URL to forward to
|
|
234
|
+
* @param options - Optional proxy options
|
|
235
|
+
*/
|
|
236
|
+
forward(method: HttpMethod | HttpMethod[] | 'all', path: string, target: string, options?: ProxyOptions): void;
|
|
237
|
+
/**
|
|
238
|
+
* Register a resource route (RESTful).
|
|
239
|
+
*
|
|
240
|
+
* Automatically creates multiple routes for a resource (index, create, store,
|
|
241
|
+
* show, edit, update, destroy) mapping to specific controller methods.
|
|
242
|
+
*
|
|
243
|
+
* @param name - The resource name (e.g., 'users').
|
|
244
|
+
* @param controller - The controller class handling the resource.
|
|
245
|
+
* @param options - Optional constraints (only/except) for resource actions.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* router.resource('photos', PhotoController);
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
resource(name: string, controller: ControllerClass, options?: ResourceOptions): void;
|
|
253
|
+
/**
|
|
254
|
+
* Internal Request Registration
|
|
255
|
+
*/
|
|
256
|
+
req(method: HttpMethod, path: string, requestOrHandlerOrMiddleware: RouteDefinitionArg, handler?: RouteHandler, options?: RouteOptions): Route;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Standard RESTful resource action names.
|
|
260
|
+
* @public
|
|
261
|
+
*/
|
|
262
|
+
export type ResourceAction = 'index' | 'create' | 'store' | 'show' | 'edit' | 'update' | 'destroy';
|
|
263
|
+
/**
|
|
264
|
+
* Options for resource route registration.
|
|
265
|
+
* @public
|
|
266
|
+
*/
|
|
267
|
+
export interface ResourceOptions {
|
|
268
|
+
only?: ResourceAction[];
|
|
269
|
+
except?: ResourceAction[];
|
|
270
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { ConfigManager } from './ConfigManager';
|
|
2
|
+
import type { Container } from './Container';
|
|
3
|
+
import type { PlanetCore } from './PlanetCore';
|
|
4
|
+
/**
|
|
5
|
+
* ServiceProvider - The foundation for modular service registration.
|
|
6
|
+
*
|
|
7
|
+
* Service providers are the central place to configure your application.
|
|
8
|
+
* They bind services to the container and bootstrap application features.
|
|
9
|
+
*
|
|
10
|
+
* Lifecycle:
|
|
11
|
+
* 1. register() - Called during registration phase (sync or async)
|
|
12
|
+
* 2. boot() - Called after ALL providers have registered
|
|
13
|
+
*
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* class DatabaseServiceProvider extends ServiceProvider {
|
|
18
|
+
* register(container: Container) {
|
|
19
|
+
* container.singleton('db', () => new DatabaseManager());
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* boot(core: PlanetCore) {
|
|
23
|
+
* const db = core.container.make<DatabaseManager>('db');
|
|
24
|
+
* db.setDefaultConnection(core.config.get('database.default'));
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare abstract class ServiceProvider {
|
|
30
|
+
/**
|
|
31
|
+
* Reference to the application core instance.
|
|
32
|
+
* Set during provider registration.
|
|
33
|
+
*/
|
|
34
|
+
protected core?: PlanetCore;
|
|
35
|
+
/**
|
|
36
|
+
* Whether this provider should be deferred.
|
|
37
|
+
* Deferred providers are only registered when one of their
|
|
38
|
+
* provided services is actually requested from the container.
|
|
39
|
+
*/
|
|
40
|
+
deferred: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Get the services provided by this provider.
|
|
43
|
+
* Used for deferred loading - provider is only loaded when
|
|
44
|
+
* one of these services is requested.
|
|
45
|
+
*
|
|
46
|
+
* @returns Array of service keys this provider offers
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* provides(): string[] {
|
|
51
|
+
* return ['db', 'db.connection'];
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
provides(): string[];
|
|
56
|
+
/**
|
|
57
|
+
* Register bindings in the container.
|
|
58
|
+
*
|
|
59
|
+
* This method is called during the registration phase.
|
|
60
|
+
* **Warning**: Do not resolve services from other providers here,
|
|
61
|
+
* as they may not be registered yet.
|
|
62
|
+
*
|
|
63
|
+
* Supports both synchronous and asynchronous registration.
|
|
64
|
+
*
|
|
65
|
+
* @param container - The IoC container instance
|
|
66
|
+
*/
|
|
67
|
+
abstract register(container: Container): void | Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Bootstrap any application services.
|
|
70
|
+
*
|
|
71
|
+
* This method is called after ALL providers have registered.
|
|
72
|
+
* You can safely resolve services from the container here.
|
|
73
|
+
*
|
|
74
|
+
* @param core - The PlanetCore application instance
|
|
75
|
+
*/
|
|
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>;
|
|
99
|
+
/**
|
|
100
|
+
* Set the core instance reference.
|
|
101
|
+
* Called internally by the application during registration.
|
|
102
|
+
*
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
setCore(core: PlanetCore): void;
|
|
106
|
+
/**
|
|
107
|
+
* Merge configuration from a value into the application config.
|
|
108
|
+
*
|
|
109
|
+
* If the configuration key already exists and both the existing value and
|
|
110
|
+
* the new value are objects, they will be shallow-merged. Otherwise, the
|
|
111
|
+
* new value will overwrite the existing one.
|
|
112
|
+
*
|
|
113
|
+
* @param config - The ConfigManager instance.
|
|
114
|
+
* @param key - The configuration key to set (supports dot notation).
|
|
115
|
+
* @param value - The configuration value or object to merge.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* this.mergeConfig(config, 'database', {
|
|
120
|
+
* default: 'mysql',
|
|
121
|
+
* connections: { ... }
|
|
122
|
+
* });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
protected mergeConfig(config: ConfigManager, key: string, value: unknown): void;
|
|
126
|
+
/**
|
|
127
|
+
* Merge configuration from an async loader.
|
|
128
|
+
* Useful for loading config from .ts files dynamically.
|
|
129
|
+
*
|
|
130
|
+
* @param config - The ConfigManager instance
|
|
131
|
+
* @param key - The configuration key
|
|
132
|
+
* @param loader - Async function that returns config value
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* await this.mergeConfigFrom(config, 'database', async () => {
|
|
137
|
+
* return (await import('./config/database')).default;
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
protected mergeConfigFrom(config: ConfigManager, key: string, loader: () => Promise<unknown>): Promise<void>;
|
|
142
|
+
/**
|
|
143
|
+
* Paths that should be published by the CLI.
|
|
144
|
+
* Maps source paths to destination paths.
|
|
145
|
+
*/
|
|
146
|
+
private static publishables;
|
|
147
|
+
/**
|
|
148
|
+
* Register paths to be published by the CLI.
|
|
149
|
+
*
|
|
150
|
+
* Used by CLI commands like `gravito vendor:publish` to copy configuration,
|
|
151
|
+
* views, or assets from the package to the application directory.
|
|
152
|
+
*
|
|
153
|
+
* @param paths - A record mapping source paths to destination paths.
|
|
154
|
+
* @param group - Optional group name for selective publishing (e.g., 'config', 'views').
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* this.publishes({
|
|
159
|
+
* './config/cache.ts': 'config/cache.ts',
|
|
160
|
+
* './views/errors': 'resources/views/errors'
|
|
161
|
+
* }, 'config');
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
protected publishes(paths: Record<string, string>, group?: string): void;
|
|
165
|
+
/**
|
|
166
|
+
* Get all publishable paths for a group.
|
|
167
|
+
*
|
|
168
|
+
* @param group - The group name (defaults to provider class name)
|
|
169
|
+
* @returns Map of source to destination paths
|
|
170
|
+
*/
|
|
171
|
+
static getPublishables(group?: string): Map<string, string>;
|
|
172
|
+
/**
|
|
173
|
+
* Get all publish groups.
|
|
174
|
+
*
|
|
175
|
+
* @returns Array of group names
|
|
176
|
+
*/
|
|
177
|
+
static getPublishGroups(): string[];
|
|
178
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Gravito } from '../engine/Gravito';
|
|
2
|
+
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoVariables, HttpMethod } from '../http/types';
|
|
3
|
+
import type { AdapterConfig, HttpAdapter, RouteDefinition } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* GravitoEngineAdapter - Optimized adapter using the Standalone Gravito Engine
|
|
6
|
+
*
|
|
7
|
+
* This adapter is exclusively for Bun and provides the best performance
|
|
8
|
+
* by using the specialized Gravito engine with object pooling and AOT routing.
|
|
9
|
+
*/
|
|
10
|
+
export declare class GravitoEngineAdapter<V extends GravitoVariables = GravitoVariables> implements HttpAdapter<V> {
|
|
11
|
+
readonly name = "gravito-engine";
|
|
12
|
+
readonly version = "1.0.0";
|
|
13
|
+
private engine;
|
|
14
|
+
constructor(config?: AdapterConfig);
|
|
15
|
+
get native(): Gravito;
|
|
16
|
+
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
|
|
17
|
+
routes(routes: RouteDefinition[]): void;
|
|
18
|
+
use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
19
|
+
useGlobal(...middleware: GravitoMiddleware<V>[]): void;
|
|
20
|
+
mount(path: string, subAdapter: HttpAdapter<V>): void;
|
|
21
|
+
onError(handler: GravitoErrorHandler<V>): void;
|
|
22
|
+
onNotFound(handler: GravitoNotFoundHandler<V>): void;
|
|
23
|
+
fetch: (request: Request, _server?: unknown) => Response | Promise<Response>;
|
|
24
|
+
warmup(paths: string[]): Promise<void>;
|
|
25
|
+
createContext(_request: Request): GravitoContext<V>;
|
|
26
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adaptive HTTP Adapter Selector
|
|
3
|
+
*
|
|
4
|
+
* Automatically chooses between BunNativeAdapter (high-performance)
|
|
5
|
+
* and PhotonAdapter (feature-rich) based on real-time metrics.
|
|
6
|
+
*
|
|
7
|
+
* @experimental
|
|
8
|
+
* @since 2.1.0
|
|
9
|
+
*/
|
|
10
|
+
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, HttpMethod } from '../../http/types';
|
|
11
|
+
import type { HttpAdapter, RouteDefinition } from '../types';
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for adaptive adapter
|
|
14
|
+
*/
|
|
15
|
+
export interface AdaptiveAdapterConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Enable adaptive selection
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Measurement window size (default: 1000 requests)
|
|
23
|
+
* @default 1000
|
|
24
|
+
*/
|
|
25
|
+
windowSize?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Switch threshold (multiple of slower adapter)
|
|
28
|
+
* @default 1.5 (switch if 50% faster)
|
|
29
|
+
*/
|
|
30
|
+
switchThreshold?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Minimum requests before switching
|
|
33
|
+
* @default 100
|
|
34
|
+
*/
|
|
35
|
+
minRequestsBeforeSwitch?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Enable verbose logging
|
|
38
|
+
* @default false
|
|
39
|
+
*/
|
|
40
|
+
verbose?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Adaptive HTTP Adapter
|
|
44
|
+
*
|
|
45
|
+
* Monitors performance and automatically switches between adapters
|
|
46
|
+
* to optimize for the current workload.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const adapter = new AdaptiveAdapter({
|
|
51
|
+
* enabled: true,
|
|
52
|
+
* switchThreshold: 1.5,
|
|
53
|
+
* minRequestsBeforeSwitch: 100
|
|
54
|
+
* })
|
|
55
|
+
*
|
|
56
|
+
* adapter.route('GET', '/api/users', handler)
|
|
57
|
+
*
|
|
58
|
+
* // Automatically switches to BunNativeAdapter after 100 requests
|
|
59
|
+
* // if it's 50% faster than PhotonAdapter
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare class AdaptiveAdapter implements HttpAdapter {
|
|
63
|
+
readonly name = "adaptive";
|
|
64
|
+
readonly version = "0.0.1";
|
|
65
|
+
private primary;
|
|
66
|
+
private metrics;
|
|
67
|
+
private requestCount;
|
|
68
|
+
private config;
|
|
69
|
+
get native(): unknown;
|
|
70
|
+
constructor(config?: AdaptiveAdapterConfig);
|
|
71
|
+
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler | GravitoMiddleware)[]): void;
|
|
72
|
+
routes(routes: RouteDefinition[]): void;
|
|
73
|
+
use(path: string, ...middleware: GravitoMiddleware[]): void;
|
|
74
|
+
useGlobal(...middleware: GravitoMiddleware[]): void;
|
|
75
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware[]): void;
|
|
76
|
+
mount(path: string, subAdapter: HttpAdapter): void;
|
|
77
|
+
createContext(request: Request): GravitoContext;
|
|
78
|
+
onError(handler: GravitoErrorHandler): void;
|
|
79
|
+
onNotFound(handler: GravitoNotFoundHandler): void;
|
|
80
|
+
fetch(request: Request, _server?: unknown): Promise<Response>;
|
|
81
|
+
/**
|
|
82
|
+
* Get current performance metrics
|
|
83
|
+
*/
|
|
84
|
+
getMetrics(): {
|
|
85
|
+
adapter: string;
|
|
86
|
+
requestCount: number;
|
|
87
|
+
averageTime: number;
|
|
88
|
+
p50: number;
|
|
89
|
+
p99: number;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Force adapter switching (for testing/debugging)
|
|
93
|
+
*/
|
|
94
|
+
switchAdapter(adapter: HttpAdapter): void;
|
|
95
|
+
/**
|
|
96
|
+
* Reset metrics
|
|
97
|
+
*/
|
|
98
|
+
resetMetrics(): void;
|
|
99
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { RequestScopeManager } from '../../Container/RequestScopeManager';
|
|
2
|
+
import type { ContentfulStatusCode, GravitoContext, GravitoVariables, ProxyOptions, StatusCode } from '../../http/types';
|
|
3
|
+
import { BunRequest } from './BunRequest';
|
|
4
|
+
/**
|
|
5
|
+
* Bun-optimized implementation of GravitoContext.
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare class BunContext<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
|
|
9
|
+
readonly env: Record<string, unknown>;
|
|
10
|
+
req: BunRequest;
|
|
11
|
+
private _variables;
|
|
12
|
+
private _requestScope;
|
|
13
|
+
/**
|
|
14
|
+
* URL generator helper
|
|
15
|
+
*/
|
|
16
|
+
route: (name: string, params?: Record<string, any>, query?: Record<string, any>) => string;
|
|
17
|
+
private _status;
|
|
18
|
+
private _headers;
|
|
19
|
+
private _executionCtx?;
|
|
20
|
+
res: Response | undefined;
|
|
21
|
+
readonly native: unknown;
|
|
22
|
+
constructor(request: Request, env?: Record<string, unknown>, executionCtx?: ExecutionContext);
|
|
23
|
+
/**
|
|
24
|
+
* Create a proxied instance to enable object destructuring of context variables
|
|
25
|
+
* This allows: async list({ userService }: Context)
|
|
26
|
+
*/
|
|
27
|
+
static create<V extends GravitoVariables = GravitoVariables>(request: Request, env?: Record<string, unknown>, executionCtx?: ExecutionContext): GravitoContext<V>;
|
|
28
|
+
json<T>(data: T, status?: ContentfulStatusCode): Response;
|
|
29
|
+
text(text: string, status?: ContentfulStatusCode): Response;
|
|
30
|
+
html(html: string, status?: ContentfulStatusCode): Response;
|
|
31
|
+
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
32
|
+
body(data: BodyInit | null, status?: StatusCode): Response;
|
|
33
|
+
stream(stream: ReadableStream, status?: ContentfulStatusCode): Response;
|
|
34
|
+
notFound(message?: string): Response;
|
|
35
|
+
forbidden(message?: string): Response;
|
|
36
|
+
unauthorized(message?: string): Response;
|
|
37
|
+
badRequest(message?: string): Response;
|
|
38
|
+
forward(target: string, options?: ProxyOptions): Promise<Response>;
|
|
39
|
+
header(name: string, value: string, options?: {
|
|
40
|
+
append?: boolean;
|
|
41
|
+
}): void;
|
|
42
|
+
header(name: string): string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Reset context state for reuse in pooling scenarios
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
reset(request: Request): void;
|
|
48
|
+
status(code: StatusCode): void;
|
|
49
|
+
get<K extends keyof V>(key: K): V[K];
|
|
50
|
+
set<K extends keyof V>(key: K, value: V[K]): void;
|
|
51
|
+
get executionCtx(): ExecutionContext | undefined;
|
|
52
|
+
requestScope(): RequestScopeManager;
|
|
53
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
54
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, HttpMethod } from '../../http/types';
|
|
2
|
+
import type { HttpAdapter, RouteDefinition } from '../types';
|
|
3
|
+
import type { WebSocketRouteHandlers } from './BunWebSocketHandler';
|
|
4
|
+
/**
|
|
5
|
+
* Native Bun-optimized HTTP Adapter for Gravito.
|
|
6
|
+
* Uses Bun's standard Request/Response classes and efficient router.
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare class BunNativeAdapter implements HttpAdapter {
|
|
10
|
+
readonly name = "bun-native";
|
|
11
|
+
readonly version = "0.0.1";
|
|
12
|
+
get native(): unknown;
|
|
13
|
+
private router;
|
|
14
|
+
private middlewares;
|
|
15
|
+
private errorHandler;
|
|
16
|
+
private notFoundHandler;
|
|
17
|
+
private contextPool;
|
|
18
|
+
private readonly maxPoolSize;
|
|
19
|
+
private middlewareChainCache;
|
|
20
|
+
private wsHandler;
|
|
21
|
+
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler | GravitoMiddleware)[]): void;
|
|
22
|
+
routes(routes: RouteDefinition[]): void;
|
|
23
|
+
use(path: string, ...middleware: GravitoMiddleware[]): void;
|
|
24
|
+
useGlobal(...middleware: GravitoMiddleware[]): void;
|
|
25
|
+
useScoped(scope: string, path: string, ...middleware: GravitoMiddleware[]): void;
|
|
26
|
+
/**
|
|
27
|
+
* P1 Fix: Accurate path pattern matching for middleware
|
|
28
|
+
* Handles wildcards: *, /api/*, /api
|
|
29
|
+
*/
|
|
30
|
+
private matchesPath;
|
|
31
|
+
/**
|
|
32
|
+
* P2 optimization: Pre-compile middleware chain for a path
|
|
33
|
+
*/
|
|
34
|
+
private getCompiledMiddlewareChain;
|
|
35
|
+
/**
|
|
36
|
+
* P0 Fix: Context object pooling to prevent state pollution
|
|
37
|
+
*/
|
|
38
|
+
private acquireContext;
|
|
39
|
+
/**
|
|
40
|
+
* P0 Fix: Release context back to pool
|
|
41
|
+
*/
|
|
42
|
+
private releaseContext;
|
|
43
|
+
mount(path: string, subAdapter: HttpAdapter): void;
|
|
44
|
+
createContext(request: Request): GravitoContext;
|
|
45
|
+
onError(handler: GravitoErrorHandler): void;
|
|
46
|
+
onNotFound(handler: GravitoNotFoundHandler): void;
|
|
47
|
+
/**
|
|
48
|
+
* 註冊 WebSocket 路由
|
|
49
|
+
*/
|
|
50
|
+
registerWebSocketRoute(path: string, handlers: WebSocketRouteHandlers): void;
|
|
51
|
+
/**
|
|
52
|
+
* 取得 WebSocket handler(供 Bun.serve 使用)
|
|
53
|
+
*/
|
|
54
|
+
get websocket(): {
|
|
55
|
+
open?: (ws: unknown) => void | Promise<void>;
|
|
56
|
+
message?: (ws: unknown, data: string | Buffer | Uint8Array) => void | Promise<void>;
|
|
57
|
+
close?: (ws: unknown, code: number, reason: string) => void | Promise<void>;
|
|
58
|
+
drain?: (ws: unknown) => void | Promise<void>;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Predictive Route Warming (JIT Optimization)
|
|
62
|
+
*/
|
|
63
|
+
warmup(paths: string[]): Promise<void>;
|
|
64
|
+
fetch(request: Request, _server?: unknown): Promise<Response>;
|
|
65
|
+
private executeChain;
|
|
66
|
+
}
|