@gravito/radiance 1.0.4 → 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/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 -294
- 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 +4 -2
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview FastContext - Pooled Request Context
|
|
3
|
+
*
|
|
4
|
+
* Minimal, high-performance context implementation designed for object pooling.
|
|
5
|
+
* Lazy parsing strategy: only parse what's actually accessed.
|
|
6
|
+
*
|
|
7
|
+
* @module @gravito/core/engine
|
|
8
|
+
*/
|
|
9
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
10
|
+
import type { FastRequest, FastContext as IFastContext } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* Lazy-parsed request wrapper
|
|
13
|
+
*
|
|
14
|
+
* Delays parsing of query params, headers, and body until accessed.
|
|
15
|
+
* This is a key optimization for requests that don't need all data.
|
|
16
|
+
*/
|
|
17
|
+
declare class FastRequestImpl implements FastRequest {
|
|
18
|
+
private _request;
|
|
19
|
+
private _params;
|
|
20
|
+
private _path;
|
|
21
|
+
private _routePattern?;
|
|
22
|
+
private _url;
|
|
23
|
+
private _query;
|
|
24
|
+
private _headers;
|
|
25
|
+
private _cachedJson;
|
|
26
|
+
private _jsonParsed;
|
|
27
|
+
private _cachedText;
|
|
28
|
+
private _textParsed;
|
|
29
|
+
private _cachedFormData;
|
|
30
|
+
private _formDataParsed;
|
|
31
|
+
private _cachedQueries;
|
|
32
|
+
private _cachedCookies;
|
|
33
|
+
private _ctx;
|
|
34
|
+
constructor(ctx: FastContext);
|
|
35
|
+
/**
|
|
36
|
+
* Initialize for new request
|
|
37
|
+
*/
|
|
38
|
+
init(request: Request, params?: Record<string, string>, path?: string, routePattern?: string): this;
|
|
39
|
+
/**
|
|
40
|
+
* Reset for pooling
|
|
41
|
+
*/
|
|
42
|
+
reset(): void;
|
|
43
|
+
private checkReleased;
|
|
44
|
+
get url(): string;
|
|
45
|
+
get method(): string;
|
|
46
|
+
get path(): string;
|
|
47
|
+
get routePattern(): string | undefined;
|
|
48
|
+
param(name: string): string | undefined;
|
|
49
|
+
params(): Record<string, string>;
|
|
50
|
+
private getUrl;
|
|
51
|
+
query(name: string): string | undefined;
|
|
52
|
+
queries(): Record<string, string | string[]>;
|
|
53
|
+
header(name: string): string | undefined;
|
|
54
|
+
headers(): Record<string, string>;
|
|
55
|
+
get cookies(): Record<string, string>;
|
|
56
|
+
json<T = unknown>(): Promise<T>;
|
|
57
|
+
text(): Promise<string>;
|
|
58
|
+
formData(): Promise<FormData>;
|
|
59
|
+
get raw(): Request;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* FastContext - Pooled request context
|
|
63
|
+
*
|
|
64
|
+
* Designed for minimal memory allocation and maximum reuse.
|
|
65
|
+
* All response helpers create Response objects directly without intermediate wrappers.
|
|
66
|
+
*/
|
|
67
|
+
export declare class FastContext implements IFastContext {
|
|
68
|
+
readonly req: FastRequestImpl;
|
|
69
|
+
private _headers;
|
|
70
|
+
_isReleased: boolean;
|
|
71
|
+
private _requestScope;
|
|
72
|
+
/**
|
|
73
|
+
* Initialize context for a new request
|
|
74
|
+
*
|
|
75
|
+
* This is called when acquiring from the pool.
|
|
76
|
+
*/
|
|
77
|
+
init(request: Request, params?: Record<string, string>, path?: string, routePattern?: string): this;
|
|
78
|
+
/**
|
|
79
|
+
* Reset context for pooling (Cleanup)
|
|
80
|
+
*
|
|
81
|
+
* This is called when releasing back to the pool.
|
|
82
|
+
* Implements "Deep-Reset Protocol" and "Release Guard".
|
|
83
|
+
*/
|
|
84
|
+
reset(): void;
|
|
85
|
+
/**
|
|
86
|
+
* Check if context is released
|
|
87
|
+
*/
|
|
88
|
+
private checkReleased;
|
|
89
|
+
json<T>(data: T, status?: number): Response;
|
|
90
|
+
text(text: string, status?: number): Response;
|
|
91
|
+
html(html: string, status?: number): Response;
|
|
92
|
+
/**
|
|
93
|
+
* Escape HTML using Bun's SIMD-accelerated native implementation
|
|
94
|
+
*/
|
|
95
|
+
escape(html: string): string;
|
|
96
|
+
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
97
|
+
body(data: BodyInit | null, status?: number): Response;
|
|
98
|
+
/**
|
|
99
|
+
* Send high-performance binary response (e.g. CBOR, Protobuf)
|
|
100
|
+
* Utilizing Bun's native ArrayBufferSink for zero-allocation construction.
|
|
101
|
+
*/
|
|
102
|
+
binary(data: Uint8Array | ArrayBuffer, status?: number): Response;
|
|
103
|
+
stream(stream: any, status?: number): Response;
|
|
104
|
+
notFound(message?: string): Response;
|
|
105
|
+
forbidden(message?: string): Response;
|
|
106
|
+
unauthorized(message?: string): Response;
|
|
107
|
+
badRequest(message?: string): Response;
|
|
108
|
+
forward(target: string, _options?: any): Promise<Response>;
|
|
109
|
+
header(name: string): string | undefined;
|
|
110
|
+
header(name: string, value: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* Status code setter (no-op)
|
|
113
|
+
*
|
|
114
|
+
* Note: Since all response helpers accept a `status` parameter,
|
|
115
|
+
* this method is not actively used. Status should be set directly
|
|
116
|
+
* in the response helper call (e.g., `ctx.json({}, 201)`).
|
|
117
|
+
*/
|
|
118
|
+
status(_code: number): void;
|
|
119
|
+
private _store;
|
|
120
|
+
get<T>(key: string): T;
|
|
121
|
+
set(key: string, value: any): void;
|
|
122
|
+
/**
|
|
123
|
+
* Get the request-scoped service manager for this request.
|
|
124
|
+
*
|
|
125
|
+
* @returns The RequestScopeManager for this request.
|
|
126
|
+
* @throws Error if called before init() or after reset().
|
|
127
|
+
*/
|
|
128
|
+
requestScope(): RequestScopeManager;
|
|
129
|
+
/**
|
|
130
|
+
* Resolve a request-scoped service (convenience method).
|
|
131
|
+
*
|
|
132
|
+
* @template T - The service type.
|
|
133
|
+
* @param key - The service key for caching.
|
|
134
|
+
* @param factory - Factory function to create the service.
|
|
135
|
+
* @returns The cached or newly created service instance.
|
|
136
|
+
*/
|
|
137
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
138
|
+
route: (name: string, params?: any, query?: any) => string;
|
|
139
|
+
get native(): this;
|
|
140
|
+
}
|
|
141
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Gravito - High-Performance Web Engine for Bun
|
|
3
|
+
*
|
|
4
|
+
* The standalone engine optimized exclusively for Bun runtime.
|
|
5
|
+
* 99% API-compatible with Hono, but faster through Bun-specific optimizations.
|
|
6
|
+
*
|
|
7
|
+
* Key optimizations:
|
|
8
|
+
* 1. Object pooling for zero-allocation request handling
|
|
9
|
+
* 2. AOT router with O(1) static route lookup
|
|
10
|
+
* 3. Lazy parsing - only parse what's accessed
|
|
11
|
+
* 4. Direct Bun.serve integration without wrapper layers
|
|
12
|
+
*
|
|
13
|
+
* @module @gravito/core/engine
|
|
14
|
+
*/
|
|
15
|
+
import type { EngineOptions, ErrorHandler, Handler, Middleware, NotFoundHandler, RouteMetadata } from './types';
|
|
16
|
+
/**
|
|
17
|
+
* Gravito - The High-Performance Web Engine
|
|
18
|
+
*/
|
|
19
|
+
export declare class Gravito {
|
|
20
|
+
private router;
|
|
21
|
+
private contextPool;
|
|
22
|
+
private errorHandler?;
|
|
23
|
+
private notFoundHandler?;
|
|
24
|
+
/** @internal */
|
|
25
|
+
staticRoutes: Map<string, RouteMetadata>;
|
|
26
|
+
private isPureStaticApp;
|
|
27
|
+
private compiledDynamicRoutes;
|
|
28
|
+
/**
|
|
29
|
+
* Create a new Gravito instance
|
|
30
|
+
*
|
|
31
|
+
* @param options - Engine configuration options
|
|
32
|
+
*/
|
|
33
|
+
constructor(options?: EngineOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Register a GET route
|
|
36
|
+
*/
|
|
37
|
+
get(path: string, ...handlers: Handler[]): this;
|
|
38
|
+
/**
|
|
39
|
+
* Register a POST route
|
|
40
|
+
*/
|
|
41
|
+
post(path: string, ...handlers: Handler[]): this;
|
|
42
|
+
/**
|
|
43
|
+
* Register a PUT route
|
|
44
|
+
*/
|
|
45
|
+
put(path: string, ...handlers: Handler[]): this;
|
|
46
|
+
/**
|
|
47
|
+
* Register a DELETE route
|
|
48
|
+
*/
|
|
49
|
+
delete(path: string, ...handlers: Handler[]): this;
|
|
50
|
+
/**
|
|
51
|
+
* Register a PATCH route
|
|
52
|
+
*/
|
|
53
|
+
patch(path: string, ...handlers: Handler[]): this;
|
|
54
|
+
/**
|
|
55
|
+
* Register an OPTIONS route
|
|
56
|
+
*/
|
|
57
|
+
options(path: string, ...handlers: Handler[]): this;
|
|
58
|
+
/**
|
|
59
|
+
* Register a HEAD route
|
|
60
|
+
*/
|
|
61
|
+
head(path: string, ...handlers: Handler[]): this;
|
|
62
|
+
/**
|
|
63
|
+
* Register a route for all HTTP methods
|
|
64
|
+
*/
|
|
65
|
+
all(path: string, ...handlers: Handler[]): this;
|
|
66
|
+
/**
|
|
67
|
+
* Register global or path-based middleware
|
|
68
|
+
*/
|
|
69
|
+
use(path: string, ...middleware: Middleware[]): this;
|
|
70
|
+
use(...middleware: Middleware[]): this;
|
|
71
|
+
/**
|
|
72
|
+
* Mount a sub-application at a path prefix
|
|
73
|
+
*/
|
|
74
|
+
route(path: string, app: Gravito): this;
|
|
75
|
+
/**
|
|
76
|
+
* Set custom error handler
|
|
77
|
+
*/
|
|
78
|
+
onError(handler: ErrorHandler): this;
|
|
79
|
+
/**
|
|
80
|
+
* Set custom 404 handler
|
|
81
|
+
*/
|
|
82
|
+
notFound(handler: NotFoundHandler): this;
|
|
83
|
+
/**
|
|
84
|
+
* Predictive Route Warming (JIT Optimization)
|
|
85
|
+
*
|
|
86
|
+
* Simulates requests to specified routes to trigger JIT compilation (FTL)
|
|
87
|
+
* before real traffic arrives.
|
|
88
|
+
*
|
|
89
|
+
* @param paths List of paths to warm up (e.g. ['/api/users', '/health'])
|
|
90
|
+
*/
|
|
91
|
+
warmup(paths: string[]): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Generate Native Bun.serve Configuration
|
|
94
|
+
*
|
|
95
|
+
* Offloads static routes to Bun's SIMD-accelerated native router.
|
|
96
|
+
* Supports pre-compiled middleware chains for zero runtime lookup.
|
|
97
|
+
*/
|
|
98
|
+
serveConfig(baseConfig?: Record<string, unknown>): Record<string, unknown>;
|
|
99
|
+
/**
|
|
100
|
+
* Optimize TLS Configuration for Bun 1.39+
|
|
101
|
+
*/
|
|
102
|
+
private optimizeTLS;
|
|
103
|
+
/**
|
|
104
|
+
* Handle an incoming request
|
|
105
|
+
*/
|
|
106
|
+
fetch: (request: Request) => Promise<Response>;
|
|
107
|
+
/**
|
|
108
|
+
* Handle dynamic routes with Radix Tree
|
|
109
|
+
*/
|
|
110
|
+
private handleDynamicRoute;
|
|
111
|
+
/**
|
|
112
|
+
* Sync error handler (for ultra-fast path)
|
|
113
|
+
*/
|
|
114
|
+
private handleErrorSync;
|
|
115
|
+
/**
|
|
116
|
+
* Sync 404 handler (for ultra-fast path)
|
|
117
|
+
*/
|
|
118
|
+
private handleNotFoundSync;
|
|
119
|
+
/**
|
|
120
|
+
* Collect middleware for a specific path
|
|
121
|
+
*/
|
|
122
|
+
private collectMiddlewareForPath;
|
|
123
|
+
/**
|
|
124
|
+
* Compile routes for optimization
|
|
125
|
+
*/
|
|
126
|
+
private compileRoutes;
|
|
127
|
+
/**
|
|
128
|
+
* Add a route to the router
|
|
129
|
+
*/
|
|
130
|
+
private addRoute;
|
|
131
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview MinimalContext - Ultra-lightweight Request Context
|
|
3
|
+
*
|
|
4
|
+
* Designed for zero-middleware static routes where pool overhead
|
|
5
|
+
* exceeds the cost of creating a new object.
|
|
6
|
+
*
|
|
7
|
+
* Key difference from FastContext:
|
|
8
|
+
* - No object pooling (direct instantiation is faster for simple cases)
|
|
9
|
+
* - No Headers object reuse (creates inline)
|
|
10
|
+
* - Minimal memory footprint
|
|
11
|
+
*
|
|
12
|
+
* @module @gravito/core/engine
|
|
13
|
+
*/
|
|
14
|
+
import { RequestScopeManager } from '../Container/RequestScopeManager';
|
|
15
|
+
import type { FastRequest, FastContext as IFastContext } from './types';
|
|
16
|
+
/**
|
|
17
|
+
* Minimal request wrapper
|
|
18
|
+
*/
|
|
19
|
+
declare class MinimalRequest implements FastRequest {
|
|
20
|
+
private readonly _request;
|
|
21
|
+
private readonly _params;
|
|
22
|
+
private readonly _path;
|
|
23
|
+
private readonly _routePattern?;
|
|
24
|
+
private _searchParams;
|
|
25
|
+
private _cachedQueries;
|
|
26
|
+
private _cachedJsonPromise;
|
|
27
|
+
private _cachedTextPromise;
|
|
28
|
+
private _cachedFormDataPromise;
|
|
29
|
+
constructor(_request: Request, _params: Record<string, string>, _path: string, _routePattern?: string | undefined);
|
|
30
|
+
get url(): string;
|
|
31
|
+
get method(): string;
|
|
32
|
+
get path(): string;
|
|
33
|
+
get routePattern(): string | undefined;
|
|
34
|
+
param(name: string): string | undefined;
|
|
35
|
+
params(): Record<string, string>;
|
|
36
|
+
/**
|
|
37
|
+
* Lazy-initialize searchParams, only parse once
|
|
38
|
+
*/
|
|
39
|
+
private getSearchParams;
|
|
40
|
+
query(name: string): string | undefined;
|
|
41
|
+
queries(): Record<string, string | string[]>;
|
|
42
|
+
header(name: string): string | undefined;
|
|
43
|
+
headers(): Record<string, string>;
|
|
44
|
+
json<T = unknown>(): Promise<T>;
|
|
45
|
+
text(): Promise<string>;
|
|
46
|
+
formData(): Promise<FormData>;
|
|
47
|
+
get cookies(): Record<string, string>;
|
|
48
|
+
get raw(): Request;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* MinimalContext - Optimized for simple, fast responses
|
|
52
|
+
*
|
|
53
|
+
* Use when:
|
|
54
|
+
* - No middleware
|
|
55
|
+
* - Static routes
|
|
56
|
+
* - Simple JSON/text responses
|
|
57
|
+
* - No custom headers needed
|
|
58
|
+
*/
|
|
59
|
+
export declare class MinimalContext implements IFastContext {
|
|
60
|
+
readonly req: MinimalRequest;
|
|
61
|
+
private _resHeaders;
|
|
62
|
+
private _requestScope;
|
|
63
|
+
constructor(request: Request, params: Record<string, string>, path: string, routePattern?: string);
|
|
64
|
+
private getHeaders;
|
|
65
|
+
json<T>(data: T, status?: number): Response;
|
|
66
|
+
text(text: string, status?: number): Response;
|
|
67
|
+
html(html: string, status?: number): Response;
|
|
68
|
+
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
69
|
+
body(data: BodyInit | null, status?: number): Response;
|
|
70
|
+
header(name: string): string | undefined;
|
|
71
|
+
header(name: string, value: string): void;
|
|
72
|
+
status(_code: number): void;
|
|
73
|
+
stream(stream: ReadableStream, status?: number): Response;
|
|
74
|
+
notFound(message?: string): Response;
|
|
75
|
+
forbidden(message?: string): Response;
|
|
76
|
+
unauthorized(message?: string): Response;
|
|
77
|
+
badRequest(message?: string): Response;
|
|
78
|
+
forward(target: string, _options?: any): Promise<Response>;
|
|
79
|
+
escape(html: string): string;
|
|
80
|
+
get<T>(_key: string): T;
|
|
81
|
+
set(_key: string, _value: any): void;
|
|
82
|
+
/**
|
|
83
|
+
* Get the request-scoped service manager for this request.
|
|
84
|
+
*
|
|
85
|
+
* @returns The RequestScopeManager for this request.
|
|
86
|
+
*/
|
|
87
|
+
requestScope(): RequestScopeManager;
|
|
88
|
+
/**
|
|
89
|
+
* Resolve a request-scoped service (convenience method).
|
|
90
|
+
*
|
|
91
|
+
* @template T - The service type.
|
|
92
|
+
* @param key - The service key for caching.
|
|
93
|
+
* @param factory - Factory function to create the service.
|
|
94
|
+
* @returns The cached or newly created service instance.
|
|
95
|
+
*/
|
|
96
|
+
scoped<T>(key: string | symbol, factory: () => T): T;
|
|
97
|
+
route: (name: string, params?: any, query?: any) => string;
|
|
98
|
+
get native(): this;
|
|
99
|
+
init(_request: Request, _params?: Record<string, string>, _path?: string): this;
|
|
100
|
+
reset(): void;
|
|
101
|
+
}
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handler 靜態分析器(Elysia-inspired,升級版)
|
|
3
|
+
*
|
|
4
|
+
* 分析 handler 函式,偵測其存取了哪些請求屬性,
|
|
5
|
+
* 以選擇最優化的 Context 類型(minimal/fast/full)。
|
|
6
|
+
*
|
|
7
|
+
* ## 版本歷史
|
|
8
|
+
*
|
|
9
|
+
* ### v2(目前版本)- 使用 Bun.Transpiler
|
|
10
|
+
* - 精確度:~99%(AST 層級分析)
|
|
11
|
+
* - 正確處理假陽性:`const header = '...'` 不再誤判
|
|
12
|
+
* - 支援解構賦值:`const { header } = ctx.req`
|
|
13
|
+
* - 支援 Minified 代碼(transformSync 先標準化)
|
|
14
|
+
* - Fallback:若 Transpiler 失敗,退回字串匹配
|
|
15
|
+
*
|
|
16
|
+
* ### v1(原版本)- 字串匹配
|
|
17
|
+
* - 精確度:~85%
|
|
18
|
+
* - 假陽性:變數名稱包含目標字串會誤判
|
|
19
|
+
* - 假陰性:解構賦值無法偵測
|
|
20
|
+
* - Minified 代碼可能失效
|
|
21
|
+
*
|
|
22
|
+
* @module @gravito/core/engine/analyzer
|
|
23
|
+
* @since 3.0.0
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Handler 靜態分析結果
|
|
27
|
+
*
|
|
28
|
+
* 記錄 handler 函式使用了哪些請求屬性,
|
|
29
|
+
* 用於選擇最優化的 Context 類型。
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
* @since 3.0.0
|
|
33
|
+
*/
|
|
34
|
+
export interface HandlerAnalysis {
|
|
35
|
+
/** 是否存取了 request headers */
|
|
36
|
+
usesHeaders: boolean;
|
|
37
|
+
/** 是否存取了 query string 參數 */
|
|
38
|
+
usesQuery: boolean;
|
|
39
|
+
/** 是否存取了 request body */
|
|
40
|
+
usesBody: boolean;
|
|
41
|
+
/** 是否存取了 route 路徑參數 */
|
|
42
|
+
usesParams: boolean;
|
|
43
|
+
/** 是否為非同步函式(含 async/await) */
|
|
44
|
+
isAsync: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 分析 handler 函式,偵測其使用了哪些請求屬性
|
|
48
|
+
*
|
|
49
|
+
* 使用 Bun.Transpiler 進行 AST 層級的精確分析(精確度 ~99%)。
|
|
50
|
+
* 若 Transpiler 不可用,自動 fallback 到字串匹配(精確度 ~85%)。
|
|
51
|
+
*
|
|
52
|
+
* ## 精確度提升說明
|
|
53
|
+
*
|
|
54
|
+
* **假陽性修復**(原本誤判,現在正確):
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // 這個 handler 原本會誤判 usesHeaders = true
|
|
57
|
+
* // 因為字串 'header' 出現在變數名稱中
|
|
58
|
+
* function handler(ctx) {
|
|
59
|
+
* const header = 'Content-Type' // ← 變數名稱,不是 API 呼叫
|
|
60
|
+
* return ctx.json({ header })
|
|
61
|
+
* }
|
|
62
|
+
* // 現在:usesHeaders = false ✅
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* **假陰性修復**(原本漏偵測,現在正確):
|
|
66
|
+
* ```typescript
|
|
67
|
+
* // 這個 handler 原本會漏偵測解構賦值
|
|
68
|
+
* function handler(ctx) {
|
|
69
|
+
* const { header, query } = ctx.req // ← 解構賦值
|
|
70
|
+
* return ctx.json({ header })
|
|
71
|
+
* }
|
|
72
|
+
* // 現在:usesHeaders = true, usesQuery = true ✅
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param handler - 要分析的 handler 函式
|
|
76
|
+
* @returns HandlerAnalysis 分析結果
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const handler = async (ctx) => {
|
|
81
|
+
* const name = ctx.req.query('name')
|
|
82
|
+
* return ctx.json({ name })
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* const analysis = analyzeHandler(handler)
|
|
86
|
+
* // analysis.usesQuery === true
|
|
87
|
+
* // analysis.usesHeaders === false
|
|
88
|
+
* // analysis.isAsync === true
|
|
89
|
+
*
|
|
90
|
+
* const type = getOptimalContextType(analysis)
|
|
91
|
+
* // type === 'fast'
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare function analyzeHandler(handler: Function): HandlerAnalysis;
|
|
95
|
+
/**
|
|
96
|
+
* 根據分析結果決定最優化的 Context 類型
|
|
97
|
+
*
|
|
98
|
+
* Context 類型由輕到重:
|
|
99
|
+
* - `minimal`:僅支援路徑參數與靜態回應,零 overhead
|
|
100
|
+
* - `fast`:支援 headers 與 query,使用物件池
|
|
101
|
+
* - `full`:支援完整功能含 body 解析(async)
|
|
102
|
+
*
|
|
103
|
+
* 選擇邏輯(優先順序):
|
|
104
|
+
* 1. 若存取 headers → `fast`(header 設置需要完整支援)
|
|
105
|
+
* 2. 若不存取任何屬性 → `minimal`
|
|
106
|
+
* 3. 若僅存取 params → `minimal`(params 在 minimal 中也可用)
|
|
107
|
+
* 4. 若存取 body → `full`(async body 解析需要完整 context)
|
|
108
|
+
* 5. 其他 → `fast`
|
|
109
|
+
*
|
|
110
|
+
* @param analysis - HandlerAnalysis 分析結果
|
|
111
|
+
* @returns 最優化的 context 類型
|
|
112
|
+
*/
|
|
113
|
+
export declare function getOptimalContextType(analysis: HandlerAnalysis): 'minimal' | 'fast' | 'full';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Engine Constants & Cached Buffers
|
|
3
|
+
*
|
|
4
|
+
* Pre-allocated resources to minimize runtime allocation overhead.
|
|
5
|
+
* Specifically targets Bun's zero-copy capabilities.
|
|
6
|
+
*/
|
|
7
|
+
export declare const CACHED_RESPONSES: {
|
|
8
|
+
readonly NOT_FOUND: Uint8Array<ArrayBuffer>;
|
|
9
|
+
readonly INTERNAL_ERROR: Uint8Array<ArrayBuffer>;
|
|
10
|
+
readonly OK: Uint8Array<ArrayBuffer>;
|
|
11
|
+
readonly EMPTY: Uint8Array<ArrayBuffer>;
|
|
12
|
+
};
|
|
13
|
+
export declare const HEADERS: {
|
|
14
|
+
readonly JSON: {
|
|
15
|
+
readonly 'Content-Type': "application/json; charset=utf-8";
|
|
16
|
+
};
|
|
17
|
+
readonly TEXT: {
|
|
18
|
+
readonly 'Content-Type': "text/plain; charset=utf-8";
|
|
19
|
+
};
|
|
20
|
+
readonly HTML: {
|
|
21
|
+
readonly 'Content-Type': "text/html; charset=utf-8";
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Gravito Core Engine - Public API
|
|
3
|
+
*
|
|
4
|
+
* The High-Performance Web Engine for Bun
|
|
5
|
+
*
|
|
6
|
+
* @module @gravito/core/engine
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Gravito } from '@gravito/core/engine'
|
|
12
|
+
*
|
|
13
|
+
* const app = new Gravito()
|
|
14
|
+
*
|
|
15
|
+
* app.get('/', (c) => c.json({ message: 'Hello, World!' }))
|
|
16
|
+
*
|
|
17
|
+
* export default app
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export { Gravito } from './Gravito';
|
|
21
|
+
export type { EngineOptions, ErrorHandler, FastContext, FastRequest, Handler, Middleware, NotFoundHandler, RouteMatch, } from './types';
|
|
22
|
+
export { AOTRouter } from './AOTRouter';
|
|
23
|
+
export { FastContext as FastContextImpl } from './FastContext';
|
|
24
|
+
export { MinimalContext } from './MinimalContext';
|
|
25
|
+
export { extractPath } from './path';
|
|
26
|
+
export { ObjectPool } from './pool';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Lightweight Path Utilities
|
|
3
|
+
*
|
|
4
|
+
* High-performance path extraction without creating URL objects.
|
|
5
|
+
* Performance critical - every optimization matters.
|
|
6
|
+
*
|
|
7
|
+
* @module @gravito/core/engine
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Extract pathname from URL string without creating URL object
|
|
11
|
+
*
|
|
12
|
+
* @param url - Full URL string (e.g., "http://localhost:3000/api/users?id=1")
|
|
13
|
+
* @returns pathname (e.g., "/api/users")
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* extractPath("http://localhost:3000/api/users?id=1") // "/api/users"
|
|
18
|
+
* extractPath("https://example.com/") // "/"
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function extractPath(url: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Extract pathname using simpler logic (alternative implementation)
|
|
24
|
+
* Use this if the above doesn't cover edge cases
|
|
25
|
+
*/
|
|
26
|
+
export declare function extractPathSimple(url: string): string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Generic Object Pool Implementation
|
|
3
|
+
*
|
|
4
|
+
* High-performance object pooling to reduce GC pressure.
|
|
5
|
+
* Implements "fixed pool + overflow fallback" strategy.
|
|
6
|
+
*
|
|
7
|
+
* @module @gravito/core/engine
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Generic object pool with fixed size and overflow handling
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - Type of objects to pool
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const pool = new ObjectPool(
|
|
17
|
+
* () => new MyObject(),
|
|
18
|
+
* (obj) => obj.reset(),
|
|
19
|
+
* 256
|
|
20
|
+
* )
|
|
21
|
+
*
|
|
22
|
+
* const obj = pool.acquire()
|
|
23
|
+
* try {
|
|
24
|
+
* // Use object
|
|
25
|
+
* } finally {
|
|
26
|
+
* pool.release(obj)
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare class ObjectPool<T> {
|
|
31
|
+
private pool;
|
|
32
|
+
private readonly factory;
|
|
33
|
+
private readonly reset;
|
|
34
|
+
private readonly maxSize;
|
|
35
|
+
/**
|
|
36
|
+
* Create a new object pool
|
|
37
|
+
*
|
|
38
|
+
* @param factory - Function to create new objects
|
|
39
|
+
* @param reset - Function to reset objects before reuse
|
|
40
|
+
* @param maxSize - Maximum pool size (default: 256)
|
|
41
|
+
*/
|
|
42
|
+
constructor(factory: () => T, reset: (obj: T) => void, maxSize?: number);
|
|
43
|
+
/**
|
|
44
|
+
* Acquire an object from the pool
|
|
45
|
+
*
|
|
46
|
+
* If the pool is empty, creates a new object (overflow strategy).
|
|
47
|
+
* This ensures the pool never blocks under high load.
|
|
48
|
+
*
|
|
49
|
+
* @returns Object from pool or newly created
|
|
50
|
+
*/
|
|
51
|
+
acquire(): T;
|
|
52
|
+
/**
|
|
53
|
+
* Release an object back to the pool
|
|
54
|
+
*
|
|
55
|
+
* If the pool is full, the object is discarded (will be GC'd).
|
|
56
|
+
* This prevents unbounded memory growth.
|
|
57
|
+
*
|
|
58
|
+
* @param obj - Object to release
|
|
59
|
+
*/
|
|
60
|
+
release(obj: T): void;
|
|
61
|
+
/**
|
|
62
|
+
* Clear all objects from the pool
|
|
63
|
+
*
|
|
64
|
+
* Useful for testing or when you need to force a clean slate.
|
|
65
|
+
*/
|
|
66
|
+
clear(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Get current pool size
|
|
69
|
+
*/
|
|
70
|
+
get size(): number;
|
|
71
|
+
/**
|
|
72
|
+
* Get maximum pool size
|
|
73
|
+
*/
|
|
74
|
+
get capacity(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Pre-warm the pool by creating objects in advance
|
|
77
|
+
*
|
|
78
|
+
* This can reduce latency for the first N requests.
|
|
79
|
+
*
|
|
80
|
+
* @param count - Number of objects to pre-create
|
|
81
|
+
*/
|
|
82
|
+
prewarm(count: number): void;
|
|
83
|
+
}
|