@gravito/echo 2.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/src/Application.d.ts +189 -0
- package/dist/core/src/ConfigManager.d.ts +26 -0
- package/dist/core/src/Container.d.ts +44 -0
- package/dist/core/src/ErrorHandler.d.ts +63 -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 +82 -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 +244 -0
- package/dist/core/src/Route.d.ts +36 -0
- package/dist/core/src/Router.d.ts +250 -0
- package/dist/core/src/ServiceProvider.d.ts +150 -0
- package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +26 -0
- package/dist/core/src/adapters/PhotonAdapter.d.ts +170 -0
- package/dist/core/src/adapters/bun/BunContext.d.ts +45 -0
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +30 -0
- package/dist/core/src/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/core/src/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +31 -0
- package/dist/core/src/adapters/bun/types.d.ts +20 -0
- package/dist/core/src/adapters/photon-types.d.ts +73 -0
- package/dist/core/src/adapters/types.d.ts +196 -0
- package/dist/core/src/engine/AOTRouter.d.ts +134 -0
- package/dist/core/src/engine/FastContext.d.ts +98 -0
- package/dist/core/src/engine/Gravito.d.ts +137 -0
- package/dist/core/src/engine/MinimalContext.d.ts +77 -0
- package/dist/core/src/engine/analyzer.d.ts +27 -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 +138 -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/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 +6 -0
- package/dist/core/src/helpers/Arr.d.ts +19 -0
- package/dist/core/src/helpers/Str.d.ts +23 -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/http/CookieJar.d.ts +51 -0
- package/dist/core/src/http/middleware/BodySizeLimit.d.ts +16 -0
- package/dist/core/src/http/middleware/Cors.d.ts +24 -0
- package/dist/core/src/http/middleware/Csrf.d.ts +23 -0
- package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +28 -0
- package/dist/core/src/http/middleware/SecurityHeaders.d.ts +29 -0
- package/dist/core/src/http/middleware/ThrottleRequests.d.ts +18 -0
- package/dist/core/src/http/types.d.ts +334 -0
- package/dist/core/src/index.d.ts +66 -0
- package/dist/core/src/runtime.d.ts +119 -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 +39 -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/types/events.d.ts +94 -0
- package/dist/echo/src/OrbitEcho.d.ts +60 -0
- package/dist/echo/src/index.d.ts +48 -0
- package/dist/echo/src/providers/GenericProvider.d.ts +34 -0
- package/dist/echo/src/providers/GitHubProvider.d.ts +26 -0
- package/dist/echo/src/providers/StripeProvider.d.ts +30 -0
- package/dist/echo/src/providers/index.d.ts +3 -0
- package/dist/echo/src/receive/SignatureValidator.d.ts +34 -0
- package/dist/echo/src/receive/WebhookReceiver.d.ts +67 -0
- package/dist/echo/src/receive/index.d.ts +2 -0
- package/dist/echo/src/send/WebhookDispatcher.d.ts +54 -0
- package/dist/echo/src/send/index.d.ts +1 -0
- package/dist/echo/src/types.d.ts +164 -0
- package/dist/index.js +6 -2
- package/dist/index.js.map +3 -3
- package/dist/photon/src/index.d.ts +20 -0
- package/dist/photon/src/middleware/binary.d.ts +34 -0
- package/package.json +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
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 middlewareCache;
|
|
38
|
+
private cacheMaxSize;
|
|
39
|
+
private version;
|
|
40
|
+
/**
|
|
41
|
+
* Register a route
|
|
42
|
+
*
|
|
43
|
+
* Automatically determines if route is static or dynamic.
|
|
44
|
+
* Static routes are stored in a Map for O(1) lookup.
|
|
45
|
+
* Dynamic routes use the Radix Tree.
|
|
46
|
+
*
|
|
47
|
+
* @param method - HTTP method
|
|
48
|
+
* @param path - Route path
|
|
49
|
+
* @param handler - Route handler
|
|
50
|
+
* @param middleware - Route-specific middleware
|
|
51
|
+
*/
|
|
52
|
+
add(method: HttpMethod, path: string, handler: Handler, middleware?: Middleware[]): void;
|
|
53
|
+
/**
|
|
54
|
+
* Mount another router at a prefix
|
|
55
|
+
*/
|
|
56
|
+
mount(prefix: string, other: AOTRouter): void;
|
|
57
|
+
/**
|
|
58
|
+
* Add global middleware
|
|
59
|
+
*
|
|
60
|
+
* These run for every request, before route-specific middleware.
|
|
61
|
+
*
|
|
62
|
+
* @param middleware - Middleware functions
|
|
63
|
+
*/
|
|
64
|
+
use(...middleware: Middleware[]): void;
|
|
65
|
+
/**
|
|
66
|
+
* Add path-based middleware
|
|
67
|
+
*
|
|
68
|
+
* Supports wildcard patterns like '/api/*'
|
|
69
|
+
*
|
|
70
|
+
* @param pattern - Path pattern
|
|
71
|
+
* @param middleware - Middleware functions
|
|
72
|
+
*/
|
|
73
|
+
usePattern(pattern: string, ...middleware: Middleware[]): void;
|
|
74
|
+
/**
|
|
75
|
+
* Match a request to a route
|
|
76
|
+
*
|
|
77
|
+
* Returns the handler, params, and all applicable middleware.
|
|
78
|
+
*
|
|
79
|
+
* @param method - HTTP method
|
|
80
|
+
* @param path - Request path
|
|
81
|
+
* @returns Route match or null if not found
|
|
82
|
+
*/
|
|
83
|
+
match(method: string, path: string): RouteMatch;
|
|
84
|
+
/**
|
|
85
|
+
* Public wrapper for collectMiddleware (used by Gravito for optimization)
|
|
86
|
+
*/
|
|
87
|
+
collectMiddlewarePublic(path: string, routeMiddleware: Middleware[]): Middleware[];
|
|
88
|
+
/**
|
|
89
|
+
* Collect all applicable middleware for a path
|
|
90
|
+
*
|
|
91
|
+
* Order: global -> pattern-based -> route-specific
|
|
92
|
+
*
|
|
93
|
+
* @param path - Request path
|
|
94
|
+
* @param routeMiddleware - Route-specific middleware
|
|
95
|
+
* @returns Combined middleware array
|
|
96
|
+
*/
|
|
97
|
+
private collectMiddleware;
|
|
98
|
+
/**
|
|
99
|
+
* Check if a path is static (no parameters or wildcards)
|
|
100
|
+
*/
|
|
101
|
+
private isStaticPath;
|
|
102
|
+
/**
|
|
103
|
+
* Match a pattern against a path
|
|
104
|
+
*
|
|
105
|
+
* Supports:
|
|
106
|
+
* - Exact match: '/api/users'
|
|
107
|
+
* - Wildcard suffix: '/api/*'
|
|
108
|
+
*
|
|
109
|
+
* @param pattern - Pattern to match
|
|
110
|
+
* @param path - Path to test
|
|
111
|
+
* @returns True if pattern matches
|
|
112
|
+
*/
|
|
113
|
+
private matchPattern;
|
|
114
|
+
/**
|
|
115
|
+
* Find the original route key for a matched dynamic route
|
|
116
|
+
*
|
|
117
|
+
* This is needed to look up route-specific middleware.
|
|
118
|
+
* It's a bit of a hack, but avoids storing duplicate data.
|
|
119
|
+
*
|
|
120
|
+
* @param method - HTTP method
|
|
121
|
+
* @param path - Matched path
|
|
122
|
+
* @returns Route key or null
|
|
123
|
+
*/
|
|
124
|
+
private findDynamicRouteKey;
|
|
125
|
+
/**
|
|
126
|
+
* Get all registered routes (for debugging)
|
|
127
|
+
*/
|
|
128
|
+
getRoutes(): Array<{
|
|
129
|
+
method: string;
|
|
130
|
+
path: string;
|
|
131
|
+
type: 'static' | 'dynamic';
|
|
132
|
+
}>;
|
|
133
|
+
}
|
|
134
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
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 type { FastRequest, FastContext as IFastContext } from './types';
|
|
10
|
+
/**
|
|
11
|
+
* Lazy-parsed request wrapper
|
|
12
|
+
*
|
|
13
|
+
* Delays parsing of query params, headers, and body until accessed.
|
|
14
|
+
* This is a key optimization for requests that don't need all data.
|
|
15
|
+
*/
|
|
16
|
+
declare class FastRequestImpl implements FastRequest {
|
|
17
|
+
private _request;
|
|
18
|
+
private _params;
|
|
19
|
+
private _path;
|
|
20
|
+
private _url;
|
|
21
|
+
private _query;
|
|
22
|
+
private _headers;
|
|
23
|
+
private _cachedJson;
|
|
24
|
+
private _jsonParsed;
|
|
25
|
+
private _ctx;
|
|
26
|
+
constructor(ctx: FastContext);
|
|
27
|
+
/**
|
|
28
|
+
* Initialize for new request
|
|
29
|
+
*/
|
|
30
|
+
init(request: Request, params?: Record<string, string>, path?: string): this;
|
|
31
|
+
/**
|
|
32
|
+
* Reset for pooling
|
|
33
|
+
*/
|
|
34
|
+
reset(): void;
|
|
35
|
+
private checkReleased;
|
|
36
|
+
get url(): string;
|
|
37
|
+
get method(): string;
|
|
38
|
+
get path(): string;
|
|
39
|
+
param(name: string): string | undefined;
|
|
40
|
+
params(): Record<string, string>;
|
|
41
|
+
private getUrl;
|
|
42
|
+
query(name: string): string | undefined;
|
|
43
|
+
queries(): Record<string, string | string[]>;
|
|
44
|
+
header(name: string): string | undefined;
|
|
45
|
+
headers(): Record<string, string>;
|
|
46
|
+
json<T = unknown>(): Promise<T>;
|
|
47
|
+
text(): Promise<string>;
|
|
48
|
+
formData(): Promise<FormData>;
|
|
49
|
+
get raw(): Request;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* FastContext - Pooled request context
|
|
53
|
+
*
|
|
54
|
+
* Designed for minimal memory allocation and maximum reuse.
|
|
55
|
+
* All response helpers create Response objects directly without intermediate wrappers.
|
|
56
|
+
*/
|
|
57
|
+
export declare class FastContext implements IFastContext {
|
|
58
|
+
readonly req: FastRequestImpl;
|
|
59
|
+
private _headers;
|
|
60
|
+
_isReleased: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Initialize context for a new request
|
|
63
|
+
*
|
|
64
|
+
* This is called when acquiring from the pool.
|
|
65
|
+
*/
|
|
66
|
+
init(request: Request, params?: Record<string, string>, path?: string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Reset context for pooling (Cleanup)
|
|
69
|
+
*
|
|
70
|
+
* This is called when releasing back to the pool.
|
|
71
|
+
* Implements "Deep-Reset Protocol" and "Release Guard".
|
|
72
|
+
*/
|
|
73
|
+
reset(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Check if context is released
|
|
76
|
+
*/
|
|
77
|
+
private checkReleased;
|
|
78
|
+
json<T>(data: T, status?: number): Response;
|
|
79
|
+
text(text: string, status?: number): Response;
|
|
80
|
+
html(html: string, status?: number): Response;
|
|
81
|
+
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
82
|
+
body(data: BodyInit | null, status?: number): Response;
|
|
83
|
+
stream(stream: ReadableStream, status?: number): Response;
|
|
84
|
+
notFound(message?: string): Response;
|
|
85
|
+
forbidden(message?: string): Response;
|
|
86
|
+
unauthorized(message?: string): Response;
|
|
87
|
+
badRequest(message?: string): Response;
|
|
88
|
+
forward(target: string, _options?: any): Promise<Response>;
|
|
89
|
+
header(name: string): string | undefined;
|
|
90
|
+
header(name: string, value: string): void;
|
|
91
|
+
status(_code: number): void;
|
|
92
|
+
private _store;
|
|
93
|
+
get<T>(key: string): T;
|
|
94
|
+
set(key: string, value: any): void;
|
|
95
|
+
route: (name: string, params?: any, query?: any) => string;
|
|
96
|
+
get native(): this;
|
|
97
|
+
}
|
|
98
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
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
|
+
private middlewareVersion;
|
|
29
|
+
/**
|
|
30
|
+
* Create a new Gravito instance
|
|
31
|
+
*
|
|
32
|
+
* @param options - Engine configuration options
|
|
33
|
+
*/
|
|
34
|
+
constructor(options?: EngineOptions);
|
|
35
|
+
/**
|
|
36
|
+
* Register a GET route
|
|
37
|
+
*
|
|
38
|
+
* @param path - Route path (e.g., '/users/:id')
|
|
39
|
+
* @param handlers - Handler and optional middleware
|
|
40
|
+
* @returns This instance for chaining
|
|
41
|
+
*/
|
|
42
|
+
get(path: string, ...handlers: Handler[]): this;
|
|
43
|
+
/**
|
|
44
|
+
* Register a POST route
|
|
45
|
+
*/
|
|
46
|
+
post(path: string, ...handlers: Handler[]): this;
|
|
47
|
+
/**
|
|
48
|
+
* Register a PUT route
|
|
49
|
+
*/
|
|
50
|
+
put(path: string, ...handlers: Handler[]): this;
|
|
51
|
+
/**
|
|
52
|
+
* Register a DELETE route
|
|
53
|
+
*/
|
|
54
|
+
delete(path: string, ...handlers: Handler[]): this;
|
|
55
|
+
/**
|
|
56
|
+
* Register a PDF route
|
|
57
|
+
*/
|
|
58
|
+
patch(path: string, ...handlers: Handler[]): this;
|
|
59
|
+
/**
|
|
60
|
+
* Register an OPTIONS route
|
|
61
|
+
*/
|
|
62
|
+
options(path: string, ...handlers: Handler[]): this;
|
|
63
|
+
/**
|
|
64
|
+
* Register a HEAD route
|
|
65
|
+
*/
|
|
66
|
+
head(path: string, ...handlers: Handler[]): this;
|
|
67
|
+
/**
|
|
68
|
+
* Register a route for all HTTP methods
|
|
69
|
+
*/
|
|
70
|
+
all(path: string, ...handlers: Handler[]): this;
|
|
71
|
+
/**
|
|
72
|
+
* Register global or path-based middleware
|
|
73
|
+
*/
|
|
74
|
+
use(path: string, ...middleware: Middleware[]): this;
|
|
75
|
+
use(...middleware: Middleware[]): this;
|
|
76
|
+
/**
|
|
77
|
+
* Mount a sub-application at a path prefix
|
|
78
|
+
*/
|
|
79
|
+
route(path: string, app: Gravito): this;
|
|
80
|
+
/**
|
|
81
|
+
* Set custom error handler
|
|
82
|
+
*/
|
|
83
|
+
onError(handler: ErrorHandler): this;
|
|
84
|
+
/**
|
|
85
|
+
* Set custom 404 handler
|
|
86
|
+
*/
|
|
87
|
+
notFound(handler: NotFoundHandler): this;
|
|
88
|
+
/**
|
|
89
|
+
* Predictive Route Warming (JIT Optimization)
|
|
90
|
+
*
|
|
91
|
+
* Simulates requests to specified routes to trigger JIT compilation (FTL)
|
|
92
|
+
* before real traffic arrives.
|
|
93
|
+
*
|
|
94
|
+
* @param paths List of paths to warm up (e.g. ['/api/users', '/health'])
|
|
95
|
+
*/
|
|
96
|
+
warmup(paths: string[]): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Handle an incoming request
|
|
99
|
+
*/
|
|
100
|
+
fetch: (request: Request) => Response | Promise<Response>;
|
|
101
|
+
/**
|
|
102
|
+
* Handle routes with middleware (async path)
|
|
103
|
+
*/
|
|
104
|
+
private handleWithMiddleware;
|
|
105
|
+
/**
|
|
106
|
+
* Handle dynamic routes (Radix Tree lookup)
|
|
107
|
+
*/
|
|
108
|
+
private handleDynamicRoute;
|
|
109
|
+
/**
|
|
110
|
+
* Sync error handler (for ultra-fast path)
|
|
111
|
+
*/
|
|
112
|
+
private handleErrorSync;
|
|
113
|
+
/**
|
|
114
|
+
* Sync 404 handler (for ultra-fast path)
|
|
115
|
+
*/
|
|
116
|
+
private handleNotFoundSync;
|
|
117
|
+
/**
|
|
118
|
+
* Collect middleware for a specific path
|
|
119
|
+
*/
|
|
120
|
+
private collectMiddlewareForPath;
|
|
121
|
+
/**
|
|
122
|
+
* Compile routes for optimization
|
|
123
|
+
*/
|
|
124
|
+
private compileRoutes;
|
|
125
|
+
/**
|
|
126
|
+
* Add a route to the router
|
|
127
|
+
*/
|
|
128
|
+
private addRoute;
|
|
129
|
+
/**
|
|
130
|
+
* Execute middleware chain followed by handler
|
|
131
|
+
*/
|
|
132
|
+
private executeMiddleware;
|
|
133
|
+
/**
|
|
134
|
+
* Handle errors (Async version for dynamic/middleware paths)
|
|
135
|
+
*/
|
|
136
|
+
private handleError;
|
|
137
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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 type { FastRequest, FastContext as IFastContext } from './types';
|
|
15
|
+
/**
|
|
16
|
+
* Minimal request wrapper
|
|
17
|
+
*/
|
|
18
|
+
declare class MinimalRequest implements FastRequest {
|
|
19
|
+
private readonly _request;
|
|
20
|
+
private readonly _params;
|
|
21
|
+
private readonly _path;
|
|
22
|
+
private _searchParams;
|
|
23
|
+
constructor(_request: Request, _params: Record<string, string>, _path: string);
|
|
24
|
+
get url(): string;
|
|
25
|
+
get method(): string;
|
|
26
|
+
get path(): string;
|
|
27
|
+
param(name: string): string | undefined;
|
|
28
|
+
params(): Record<string, string>;
|
|
29
|
+
/**
|
|
30
|
+
* Lazy-initialize searchParams, only parse once
|
|
31
|
+
*/
|
|
32
|
+
private getSearchParams;
|
|
33
|
+
query(name: string): string | undefined;
|
|
34
|
+
queries(): Record<string, string | string[]>;
|
|
35
|
+
header(name: string): string | undefined;
|
|
36
|
+
headers(): Record<string, string>;
|
|
37
|
+
json<T = unknown>(): Promise<T>;
|
|
38
|
+
text(): Promise<string>;
|
|
39
|
+
formData(): Promise<FormData>;
|
|
40
|
+
get raw(): Request;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* MinimalContext - Optimized for simple, fast responses
|
|
44
|
+
*
|
|
45
|
+
* Use when:
|
|
46
|
+
* - No middleware
|
|
47
|
+
* - Static routes
|
|
48
|
+
* - Simple JSON/text responses
|
|
49
|
+
* - No custom headers needed
|
|
50
|
+
*/
|
|
51
|
+
export declare class MinimalContext implements IFastContext {
|
|
52
|
+
readonly req: MinimalRequest;
|
|
53
|
+
private _resHeaders;
|
|
54
|
+
constructor(request: Request, params: Record<string, string>, path: string);
|
|
55
|
+
private getHeaders;
|
|
56
|
+
json<T>(data: T, status?: number): Response;
|
|
57
|
+
text(text: string, status?: number): Response;
|
|
58
|
+
html(html: string, status?: number): Response;
|
|
59
|
+
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
60
|
+
body(data: BodyInit | null, status?: number): Response;
|
|
61
|
+
header(name: string): string | undefined;
|
|
62
|
+
header(name: string, value: string): void;
|
|
63
|
+
status(_code: number): void;
|
|
64
|
+
stream(stream: ReadableStream, status?: number): Response;
|
|
65
|
+
notFound(message?: string): Response;
|
|
66
|
+
forbidden(message?: string): Response;
|
|
67
|
+
unauthorized(message?: string): Response;
|
|
68
|
+
badRequest(message?: string): Response;
|
|
69
|
+
forward(target: string, _options?: any): Promise<Response>;
|
|
70
|
+
get<T>(_key: string): T;
|
|
71
|
+
set(_key: string, _value: any): void;
|
|
72
|
+
route: (name: string, params?: any, query?: any) => string;
|
|
73
|
+
get native(): this;
|
|
74
|
+
init(_request: Request, _params?: Record<string, string>, _path?: string): this;
|
|
75
|
+
reset(): void;
|
|
76
|
+
}
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handler Static Analyzer (Elysia-inspired)
|
|
3
|
+
*
|
|
4
|
+
* Analyzes handler functions to determine what request properties
|
|
5
|
+
* they access, allowing for optimized code paths.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Represents the results of a static analysis performed on a handler function.
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
* @since 3.0.0
|
|
12
|
+
*/
|
|
13
|
+
export interface HandlerAnalysis {
|
|
14
|
+
usesHeaders: boolean;
|
|
15
|
+
usesQuery: boolean;
|
|
16
|
+
usesBody: boolean;
|
|
17
|
+
usesParams: boolean;
|
|
18
|
+
isAsync: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Analyze a handler function to detect what it accesses
|
|
22
|
+
*/
|
|
23
|
+
export declare function analyzeHandler(handler: Function): HandlerAnalysis;
|
|
24
|
+
/**
|
|
25
|
+
* Determine optimal context type based on analysis
|
|
26
|
+
*/
|
|
27
|
+
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
|
+
}
|