@gravito/cosmos 3.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/.turbo/turbo-build.log +20 -0
- package/.turbo/turbo-test$colon$ci.log +35 -0
- package/.turbo/turbo-test$colon$coverage.log +35 -0
- package/.turbo/turbo-test.log +27 -0
- package/.turbo/turbo-typecheck.log +2 -0
- package/CHANGELOG.md +7 -0
- package/build.ts +35 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +274 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.js +139 -30
- package/package.json +18 -6
- package/src/I18nService.ts +94 -15
- package/src/index.ts +32 -6
- package/tests/manager.test.ts +0 -1
- package/tests/service.test.ts +5 -0
- package/dist/core/src/Application.d.ts +0 -185
- package/dist/core/src/ConfigManager.d.ts +0 -21
- package/dist/core/src/Container.d.ts +0 -38
- package/dist/core/src/Event.d.ts +0 -5
- package/dist/core/src/EventManager.d.ts +0 -123
- package/dist/core/src/GlobalErrorHandlers.d.ts +0 -31
- package/dist/core/src/GravitoServer.d.ts +0 -20
- package/dist/core/src/HookManager.d.ts +0 -70
- package/dist/core/src/Listener.d.ts +0 -4
- package/dist/core/src/Logger.d.ts +0 -20
- package/dist/core/src/PlanetCore.d.ts +0 -207
- package/dist/core/src/Route.d.ts +0 -25
- package/dist/core/src/Router.d.ts +0 -232
- package/dist/core/src/ServiceProvider.d.ts +0 -150
- package/dist/core/src/adapters/PhotonAdapter.d.ts +0 -142
- package/dist/core/src/adapters/bun/BunContext.d.ts +0 -36
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +0 -21
- package/dist/core/src/adapters/bun/BunRequest.d.ts +0 -27
- package/dist/core/src/adapters/bun/RadixNode.d.ts +0 -15
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +0 -31
- package/dist/core/src/adapters/bun/types.d.ts +0 -20
- package/dist/core/src/adapters/types.d.ts +0 -186
- package/dist/core/src/engine/AOTRouter.d.ts +0 -117
- package/dist/core/src/engine/FastContext.d.ts +0 -34
- package/dist/core/src/engine/Gravito.d.ts +0 -191
- package/dist/core/src/engine/MinimalContext.d.ts +0 -36
- package/dist/core/src/engine/analyzer.d.ts +0 -21
- package/dist/core/src/engine/index.d.ts +0 -26
- package/dist/core/src/engine/path.d.ts +0 -26
- package/dist/core/src/engine/pool.d.ts +0 -83
- package/dist/core/src/engine/types.d.ts +0 -107
- package/dist/core/src/exceptions/AuthenticationException.d.ts +0 -4
- package/dist/core/src/exceptions/AuthorizationException.d.ts +0 -4
- package/dist/core/src/exceptions/GravitoException.d.ts +0 -15
- package/dist/core/src/exceptions/HttpException.d.ts +0 -5
- package/dist/core/src/exceptions/ModelNotFoundException.d.ts +0 -6
- package/dist/core/src/exceptions/ValidationException.d.ts +0 -14
- package/dist/core/src/exceptions/index.d.ts +0 -6
- package/dist/core/src/helpers/Arr.d.ts +0 -14
- package/dist/core/src/helpers/Str.d.ts +0 -18
- package/dist/core/src/helpers/data.d.ts +0 -5
- package/dist/core/src/helpers/errors.d.ts +0 -12
- package/dist/core/src/helpers/response.d.ts +0 -17
- package/dist/core/src/helpers.d.ts +0 -38
- package/dist/core/src/http/CookieJar.d.ts +0 -37
- package/dist/core/src/http/middleware/BodySizeLimit.d.ts +0 -6
- package/dist/core/src/http/middleware/Cors.d.ts +0 -12
- package/dist/core/src/http/middleware/Csrf.d.ts +0 -11
- package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +0 -11
- package/dist/core/src/http/middleware/SecurityHeaders.d.ts +0 -17
- package/dist/core/src/http/middleware/ThrottleRequests.d.ts +0 -12
- package/dist/core/src/http/types.d.ts +0 -312
- package/dist/core/src/index.d.ts +0 -60
- package/dist/core/src/runtime.d.ts +0 -63
- package/dist/core/src/security/Encrypter.d.ts +0 -24
- package/dist/core/src/security/Hasher.d.ts +0 -29
- package/dist/core/src/testing/HttpTester.d.ts +0 -38
- package/dist/core/src/testing/TestResponse.d.ts +0 -78
- package/dist/core/src/testing/index.d.ts +0 -2
- package/dist/core/src/types/events.d.ts +0 -94
- package/dist/cosmos/src/I18nService.d.ts +0 -144
- package/dist/cosmos/src/index.d.ts +0 -21
- package/dist/cosmos/src/loader.d.ts +0 -11
- package/dist/photon/src/index.d.ts +0 -2
- package/dist/src/index.js +0 -173
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Photon Adapter Implementation
|
|
3
|
-
*
|
|
4
|
-
* This adapter wraps Photon to implement the Gravito HttpAdapter interface.
|
|
5
|
-
* It serves as the default adapter and reference implementation for others.
|
|
6
|
-
*
|
|
7
|
-
* @module @gravito/core/adapters/photon
|
|
8
|
-
* @since 2.0.0
|
|
9
|
-
*/
|
|
10
|
-
import type { Context, Handler, MiddlewareHandler, Photon } from '@gravito/photon';
|
|
11
|
-
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoRequest, GravitoVariables, HttpMethod, StatusCode } from '../http/types';
|
|
12
|
-
import type { AdapterConfig, HttpAdapter, RouteDefinition } from './types';
|
|
13
|
-
/**
|
|
14
|
-
* Wraps Photon's request object to implement GravitoRequest
|
|
15
|
-
*/
|
|
16
|
-
declare class PhotonRequestWrapper implements GravitoRequest {
|
|
17
|
-
readonly photonCtx: Context;
|
|
18
|
-
constructor(photonCtx: Context);
|
|
19
|
-
/**
|
|
20
|
-
* Create a proxied instance to delegate to Photon's request
|
|
21
|
-
*/
|
|
22
|
-
static create(photonCtx: Context): PhotonRequestWrapper;
|
|
23
|
-
get url(): string;
|
|
24
|
-
get method(): string;
|
|
25
|
-
get path(): string;
|
|
26
|
-
param(name: string): string | undefined;
|
|
27
|
-
params(): Record<string, string>;
|
|
28
|
-
query(name: string): string | undefined;
|
|
29
|
-
queries(): Record<string, string | string[]>;
|
|
30
|
-
header(name: string): string | undefined;
|
|
31
|
-
header(): Record<string, string>;
|
|
32
|
-
json<T = unknown>(): Promise<T>;
|
|
33
|
-
text(): Promise<string>;
|
|
34
|
-
formData(): Promise<FormData>;
|
|
35
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
36
|
-
parseBody<T = unknown>(): Promise<T>;
|
|
37
|
-
get raw(): Request;
|
|
38
|
-
valid<T = unknown>(target: string): T;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Wraps Photon's context to implement GravitoContext
|
|
42
|
-
*/
|
|
43
|
-
declare class PhotonContextWrapper<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
|
|
44
|
-
private photonCtx;
|
|
45
|
-
private _req;
|
|
46
|
-
constructor(photonCtx: Context);
|
|
47
|
-
/**
|
|
48
|
-
* Create a proxied instance to enable object destructuring of context variables
|
|
49
|
-
* This allows: async list({ userService }: Context)
|
|
50
|
-
*/
|
|
51
|
-
static create<V extends GravitoVariables = GravitoVariables>(photonCtx: Context): GravitoContext<V>;
|
|
52
|
-
get req(): GravitoRequest;
|
|
53
|
-
json<T>(data: T, status?: number): Response;
|
|
54
|
-
text(text: string, status?: number): Response;
|
|
55
|
-
html(html: string, status?: number): Response;
|
|
56
|
-
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
57
|
-
body(data: BodyInit | null, status?: number): Response;
|
|
58
|
-
stream(stream: ReadableStream, status?: number): Response;
|
|
59
|
-
notFound(message?: string): Response;
|
|
60
|
-
forbidden(message?: string): Response;
|
|
61
|
-
unauthorized(message?: string): Response;
|
|
62
|
-
badRequest(message?: string): Response;
|
|
63
|
-
header(name: string): string | undefined;
|
|
64
|
-
header(name: string, value: string, options?: {
|
|
65
|
-
append?: boolean;
|
|
66
|
-
}): void;
|
|
67
|
-
status(code: StatusCode): void;
|
|
68
|
-
get<K extends keyof V>(key: K): V[K];
|
|
69
|
-
set<K extends keyof V>(key: K, value: V[K]): void;
|
|
70
|
-
get executionCtx(): ExecutionContext | undefined;
|
|
71
|
-
get env(): Record<string, unknown> | undefined;
|
|
72
|
-
get native(): Context;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Convert a GravitoHandler to a Photon Handler
|
|
76
|
-
*/
|
|
77
|
-
declare function toPhotonHandler<V extends GravitoVariables>(handler: GravitoHandler<V>): Handler;
|
|
78
|
-
/**
|
|
79
|
-
* Convert a GravitoMiddleware to a Photon MiddlewareHandler
|
|
80
|
-
*/
|
|
81
|
-
declare function toPhotonMiddleware<V extends GravitoVariables>(middleware: GravitoMiddleware<V>): MiddlewareHandler;
|
|
82
|
-
/**
|
|
83
|
-
* Default HTTP adapter using the optimized Gravito Core Engine.
|
|
84
|
-
*
|
|
85
|
-
* This adapter provides a consistent interface that can be
|
|
86
|
-
* swapped out for other implementations without changing application code.
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```typescript
|
|
90
|
-
* import { GravitoAdapter } from '@gravito/core'
|
|
91
|
-
*
|
|
92
|
-
* const adapter = new GravitoAdapter()
|
|
93
|
-
*
|
|
94
|
-
* // Register routes
|
|
95
|
-
* adapter.route('get', '/hello', async (ctx) => {
|
|
96
|
-
* return ctx.json({ message: 'Hello, World!' })
|
|
97
|
-
* })
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
export declare class PhotonAdapter<V extends GravitoVariables = GravitoVariables> implements HttpAdapter<V> {
|
|
101
|
-
private config;
|
|
102
|
-
readonly name = "photon";
|
|
103
|
-
readonly version = "1.0.0";
|
|
104
|
-
private app;
|
|
105
|
-
constructor(config?: AdapterConfig, photonInstance?: unknown);
|
|
106
|
-
/**
|
|
107
|
-
* Get the underlying Photon app instance
|
|
108
|
-
*/
|
|
109
|
-
get native(): Photon;
|
|
110
|
-
/**
|
|
111
|
-
* Set the underlying Photon app instance
|
|
112
|
-
* Used by PlanetCore during initialization
|
|
113
|
-
*/
|
|
114
|
-
setNative(app: Photon): void;
|
|
115
|
-
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
|
|
116
|
-
routes(routes: RouteDefinition[]): void;
|
|
117
|
-
use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
118
|
-
useGlobal(...middleware: GravitoMiddleware<V>[]): void;
|
|
119
|
-
mount(path: string, subAdapter: HttpAdapter<V>): void;
|
|
120
|
-
onError(handler: GravitoErrorHandler<V>): void;
|
|
121
|
-
onNotFound(handler: GravitoNotFoundHandler<V>): void;
|
|
122
|
-
fetch: (request: Request, server?: unknown) => Response | Promise<Response>;
|
|
123
|
-
createContext(_request: Request): GravitoContext<V>;
|
|
124
|
-
init(): Promise<void>;
|
|
125
|
-
shutdown(): Promise<void>;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Create a new PhotonAdapter instance
|
|
129
|
-
*/
|
|
130
|
-
export declare function createPhotonAdapter<V extends GravitoVariables = GravitoVariables>(config?: AdapterConfig): PhotonAdapter<V>;
|
|
131
|
-
export { PhotonContextWrapper, PhotonRequestWrapper, toPhotonHandler, toPhotonMiddleware };
|
|
132
|
-
/**
|
|
133
|
-
* Rebranded alias for PhotonAdapter.
|
|
134
|
-
* @category Rebranding
|
|
135
|
-
*/
|
|
136
|
-
export declare const GravitoAdapter: typeof PhotonAdapter;
|
|
137
|
-
export type GravitoAdapter<V extends GravitoVariables = GravitoVariables> = PhotonAdapter<V>;
|
|
138
|
-
/**
|
|
139
|
-
* Rebranded alias for createPhotonAdapter.
|
|
140
|
-
* @category Rebranding
|
|
141
|
-
*/
|
|
142
|
-
export declare const createGravitoAdapter: typeof createPhotonAdapter;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { ContentfulStatusCode, GravitoContext, GravitoVariables, StatusCode } from '../../http/types';
|
|
2
|
-
import { BunRequest } from './BunRequest';
|
|
3
|
-
export declare class BunContext<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
|
|
4
|
-
readonly env: Record<string, unknown>;
|
|
5
|
-
readonly req: BunRequest;
|
|
6
|
-
private _variables;
|
|
7
|
-
private _status;
|
|
8
|
-
private _headers;
|
|
9
|
-
private _executionCtx?;
|
|
10
|
-
res: Response | undefined;
|
|
11
|
-
readonly native: unknown;
|
|
12
|
-
constructor(request: Request, env?: Record<string, unknown>, executionCtx?: ExecutionContext);
|
|
13
|
-
/**
|
|
14
|
-
* Create a proxied instance to enable object destructuring of context variables
|
|
15
|
-
* This allows: async list({ userService }: Context)
|
|
16
|
-
*/
|
|
17
|
-
static create<V extends GravitoVariables = GravitoVariables>(request: Request, env?: Record<string, unknown>, executionCtx?: ExecutionContext): GravitoContext<V>;
|
|
18
|
-
json<T>(data: T, status?: ContentfulStatusCode): Response;
|
|
19
|
-
text(text: string, status?: ContentfulStatusCode): Response;
|
|
20
|
-
html(html: string, status?: ContentfulStatusCode): Response;
|
|
21
|
-
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
22
|
-
body(data: BodyInit | null, status?: StatusCode): Response;
|
|
23
|
-
stream(stream: ReadableStream, status?: ContentfulStatusCode): Response;
|
|
24
|
-
notFound(message?: string): Response;
|
|
25
|
-
forbidden(message?: string): Response;
|
|
26
|
-
unauthorized(message?: string): Response;
|
|
27
|
-
badRequest(message?: string): Response;
|
|
28
|
-
header(name: string, value: string, options?: {
|
|
29
|
-
append?: boolean;
|
|
30
|
-
}): void;
|
|
31
|
-
header(name: string): string | undefined;
|
|
32
|
-
status(code: StatusCode): void;
|
|
33
|
-
get<K extends keyof V>(key: K): V[K];
|
|
34
|
-
set<K extends keyof V>(key: K, value: V[K]): void;
|
|
35
|
-
get executionCtx(): ExecutionContext | undefined;
|
|
36
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, HttpMethod } from '../../http/types';
|
|
2
|
-
import type { HttpAdapter, RouteDefinition } from '../types';
|
|
3
|
-
export declare class BunNativeAdapter implements HttpAdapter {
|
|
4
|
-
readonly name = "bun-native";
|
|
5
|
-
readonly version = "0.0.1";
|
|
6
|
-
get native(): unknown;
|
|
7
|
-
private router;
|
|
8
|
-
private middlewares;
|
|
9
|
-
private errorHandler;
|
|
10
|
-
private notFoundHandler;
|
|
11
|
-
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler | GravitoMiddleware)[]): void;
|
|
12
|
-
routes(routes: RouteDefinition[]): void;
|
|
13
|
-
use(path: string, ...middleware: GravitoMiddleware[]): void;
|
|
14
|
-
useGlobal(...middleware: GravitoMiddleware[]): void;
|
|
15
|
-
mount(path: string, subAdapter: HttpAdapter): void;
|
|
16
|
-
createContext(request: Request): GravitoContext;
|
|
17
|
-
onError(handler: GravitoErrorHandler): void;
|
|
18
|
-
onNotFound(handler: GravitoNotFoundHandler): void;
|
|
19
|
-
fetch(request: Request, _server?: unknown): Promise<Response>;
|
|
20
|
-
private executeChain;
|
|
21
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { GravitoRequest, ValidationTarget } from '../../http/types';
|
|
2
|
-
export declare class BunRequest implements GravitoRequest {
|
|
3
|
-
readonly raw: Request;
|
|
4
|
-
private _url;
|
|
5
|
-
private _params;
|
|
6
|
-
private _query;
|
|
7
|
-
private _validated;
|
|
8
|
-
constructor(raw: Request, params?: Record<string, string>);
|
|
9
|
-
setParams(params: Record<string, string>): void;
|
|
10
|
-
get url(): string;
|
|
11
|
-
get method(): string;
|
|
12
|
-
get path(): string;
|
|
13
|
-
param(name: string): string | undefined;
|
|
14
|
-
params(): Record<string, string>;
|
|
15
|
-
query(name: string): string | undefined;
|
|
16
|
-
queries(): Record<string, string | string[]>;
|
|
17
|
-
header(name: string): string | undefined;
|
|
18
|
-
header(): Record<string, string>;
|
|
19
|
-
json<T = unknown>(): Promise<T>;
|
|
20
|
-
text(): Promise<string>;
|
|
21
|
-
formData(): Promise<FormData>;
|
|
22
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
23
|
-
parseBody<T = unknown>(): Promise<T>;
|
|
24
|
-
setValidated(target: ValidationTarget, data: unknown): void;
|
|
25
|
-
valid<T = unknown>(target: ValidationTarget): T;
|
|
26
|
-
private parseQuery;
|
|
27
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { HttpMethod } from '../../http/types';
|
|
2
|
-
import { NodeType, type RouteHandler } from './types';
|
|
3
|
-
export declare class RadixNode {
|
|
4
|
-
segment: string;
|
|
5
|
-
type: NodeType;
|
|
6
|
-
children: Map<string, RadixNode>;
|
|
7
|
-
paramChild: RadixNode | null;
|
|
8
|
-
wildcardChild: RadixNode | null;
|
|
9
|
-
handlers: Map<HttpMethod, RouteHandler[]>;
|
|
10
|
-
paramName: string | null;
|
|
11
|
-
regex: RegExp | null;
|
|
12
|
-
constructor(segment?: string, type?: NodeType);
|
|
13
|
-
toJSON(): any;
|
|
14
|
-
static fromJSON(json: any): RadixNode;
|
|
15
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { HttpMethod } from '../../http/types';
|
|
2
|
-
import { type RouteHandler, type RouteMatch } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* High-performance Radix Tree Router for Bun
|
|
5
|
-
*/
|
|
6
|
-
export declare class RadixRouter {
|
|
7
|
-
private root;
|
|
8
|
-
private globalConstraints;
|
|
9
|
-
/**
|
|
10
|
-
* Add a generic parameter constraint
|
|
11
|
-
*/
|
|
12
|
-
where(param: string, regex: RegExp): void;
|
|
13
|
-
/**
|
|
14
|
-
* Register a route
|
|
15
|
-
*/
|
|
16
|
-
add(method: HttpMethod, path: string, handlers: RouteHandler[]): void;
|
|
17
|
-
/**
|
|
18
|
-
* Match a request
|
|
19
|
-
*/
|
|
20
|
-
match(method: string, path: string): RouteMatch | null;
|
|
21
|
-
private matchRecursive;
|
|
22
|
-
private splitPath;
|
|
23
|
-
/**
|
|
24
|
-
* Serialize the router to a JSON string
|
|
25
|
-
*/
|
|
26
|
-
serialize(): string;
|
|
27
|
-
/**
|
|
28
|
-
* Restore a router from a serialized JSON string
|
|
29
|
-
*/
|
|
30
|
-
static fromSerialized(json: string): RadixRouter;
|
|
31
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Route Handler type (simplified for internal use)
|
|
3
|
-
* In the full framework this will align with GravitoHandler
|
|
4
|
-
*/
|
|
5
|
-
export type RouteHandler = Function;
|
|
6
|
-
/**
|
|
7
|
-
* Route Match Result
|
|
8
|
-
*/
|
|
9
|
-
export interface RouteMatch {
|
|
10
|
-
handlers: RouteHandler[];
|
|
11
|
-
params: Record<string, string>;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Radix Node Type
|
|
15
|
-
*/
|
|
16
|
-
export declare enum NodeType {
|
|
17
|
-
STATIC = 0,
|
|
18
|
-
PARAM = 1,
|
|
19
|
-
WILDCARD = 2
|
|
20
|
-
}
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview HTTP Adapter Interface for Gravito Framework
|
|
3
|
-
*
|
|
4
|
-
* This module defines the contract that all HTTP adapters must implement.
|
|
5
|
-
* By programming to this interface, Gravito can swap out the underlying
|
|
6
|
-
* HTTP engine without changing application code.
|
|
7
|
-
*
|
|
8
|
-
* @module @gravito/core/adapters
|
|
9
|
-
* @since 2.0.0
|
|
10
|
-
*/
|
|
11
|
-
import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoVariables, HttpMethod } from '../http/types';
|
|
12
|
-
/**
|
|
13
|
-
* Configuration options for HTTP adapters
|
|
14
|
-
*/
|
|
15
|
-
export interface AdapterConfig {
|
|
16
|
-
/**
|
|
17
|
-
* Base path prefix for all routes
|
|
18
|
-
* @default ''
|
|
19
|
-
*/
|
|
20
|
-
basePath?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Whether to enable strict routing (trailing slashes matter)
|
|
23
|
-
* @default false
|
|
24
|
-
*/
|
|
25
|
-
strictRouting?: boolean;
|
|
26
|
-
/**
|
|
27
|
-
* Custom options passed to the underlying HTTP engine
|
|
28
|
-
*/
|
|
29
|
-
engineOptions?: Record<string, unknown>;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Route definition structure
|
|
33
|
-
*/
|
|
34
|
-
export interface RouteDefinition {
|
|
35
|
-
method: HttpMethod;
|
|
36
|
-
path: string;
|
|
37
|
-
handlers: (GravitoHandler | GravitoMiddleware)[];
|
|
38
|
-
name?: string;
|
|
39
|
-
middleware?: GravitoMiddleware[];
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* HttpAdapter - The core interface for HTTP engine abstraction
|
|
43
|
-
*
|
|
44
|
-
* Any HTTP engine (Photon, Express, Fastify, custom Bun implementation)
|
|
45
|
-
* must implement this interface to be usable with Gravito.
|
|
46
|
-
*
|
|
47
|
-
* @typeParam V - Context variables type
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* // Using the default Photon adapter
|
|
52
|
-
* import { PhotonAdapter } from '@gravito/core/adapters'
|
|
53
|
-
*
|
|
54
|
-
* const core = new PlanetCore({
|
|
55
|
-
* adapter: new PhotonAdapter()
|
|
56
|
-
* })
|
|
57
|
-
*
|
|
58
|
-
* // Using a custom adapter
|
|
59
|
-
* import { BunNativeAdapter } from '@gravito/adapter-bun'
|
|
60
|
-
*
|
|
61
|
-
* const core = new PlanetCore({
|
|
62
|
-
* adapter: new BunNativeAdapter()
|
|
63
|
-
* })
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export interface HttpAdapter<V extends GravitoVariables = GravitoVariables> {
|
|
67
|
-
/**
|
|
68
|
-
* Adapter name for identification
|
|
69
|
-
* @example 'photon', 'bun-native', 'express'
|
|
70
|
-
*/
|
|
71
|
-
readonly name: string;
|
|
72
|
-
/**
|
|
73
|
-
* Adapter version
|
|
74
|
-
*/
|
|
75
|
-
readonly version: string;
|
|
76
|
-
/**
|
|
77
|
-
* Access the underlying native HTTP engine instance.
|
|
78
|
-
*
|
|
79
|
-
* ⚠️ WARNING: Using this ties your code to a specific adapter.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```typescript
|
|
83
|
-
* // For Photon adapter
|
|
84
|
-
* const photonApp = adapter.native as Photon
|
|
85
|
-
*
|
|
86
|
-
* // For custom Bun adapter
|
|
87
|
-
* const bunApp = adapter.native as BunApp
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
readonly native: unknown;
|
|
91
|
-
/**
|
|
92
|
-
* Register a route with the adapter
|
|
93
|
-
*
|
|
94
|
-
* @param method - HTTP method
|
|
95
|
-
* @param path - Route path (may include parameters like ':id')
|
|
96
|
-
* @param handlers - One or more handlers for this route (handlers or middleware)
|
|
97
|
-
*/
|
|
98
|
-
route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
|
|
99
|
-
/**
|
|
100
|
-
* Register multiple routes at once
|
|
101
|
-
*
|
|
102
|
-
* @param routes - Array of route definitions
|
|
103
|
-
*/
|
|
104
|
-
routes(routes: RouteDefinition[]): void;
|
|
105
|
-
/**
|
|
106
|
-
* Register a middleware for a path
|
|
107
|
-
*
|
|
108
|
-
* @param path - Path pattern to match
|
|
109
|
-
* @param middleware - One or more middleware functions
|
|
110
|
-
*/
|
|
111
|
-
use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
|
|
112
|
-
/**
|
|
113
|
-
* Register a global middleware (applied to all routes)
|
|
114
|
-
*
|
|
115
|
-
* @param middleware - Middleware function
|
|
116
|
-
*/
|
|
117
|
-
useGlobal(...middleware: GravitoMiddleware<V>[]): void;
|
|
118
|
-
/**
|
|
119
|
-
* Mount a sub-adapter at a path
|
|
120
|
-
*
|
|
121
|
-
* @param path - Mount path
|
|
122
|
-
* @param subAdapter - The adapter to mount
|
|
123
|
-
*/
|
|
124
|
-
mount(path: string, subAdapter: HttpAdapter<V>): void;
|
|
125
|
-
/**
|
|
126
|
-
* Set the error handler
|
|
127
|
-
*
|
|
128
|
-
* @param handler - Error handler function
|
|
129
|
-
*/
|
|
130
|
-
onError(handler: GravitoErrorHandler<V>): void;
|
|
131
|
-
/**
|
|
132
|
-
* Set the not-found handler
|
|
133
|
-
*
|
|
134
|
-
* @param handler - Not-found handler function
|
|
135
|
-
*/
|
|
136
|
-
onNotFound(handler: GravitoNotFoundHandler<V>): void;
|
|
137
|
-
/**
|
|
138
|
-
* The main fetch handler for serving requests.
|
|
139
|
-
*
|
|
140
|
-
* This is compatible with `Bun.serve()`, Cloudflare Workers,
|
|
141
|
-
* and other fetch-based runtimes.
|
|
142
|
-
*
|
|
143
|
-
* @param request - Incoming HTTP request
|
|
144
|
-
* @param server - Optional server context (Bun.Server, etc.)
|
|
145
|
-
* @returns HTTP response
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* ```typescript
|
|
149
|
-
* // With Bun.serve
|
|
150
|
-
* Bun.serve({
|
|
151
|
-
* port: 3000,
|
|
152
|
-
* fetch: adapter.fetch
|
|
153
|
-
* })
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
fetch(request: Request, server?: unknown): Response | Promise<Response>;
|
|
157
|
-
/**
|
|
158
|
-
* Initialize the adapter
|
|
159
|
-
*
|
|
160
|
-
* Called during PlanetCore.boot()
|
|
161
|
-
*/
|
|
162
|
-
init?(): void | Promise<void>;
|
|
163
|
-
/**
|
|
164
|
-
* Cleanup resources
|
|
165
|
-
*
|
|
166
|
-
* Called during graceful shutdown
|
|
167
|
-
*/
|
|
168
|
-
shutdown?(): void | Promise<void>;
|
|
169
|
-
/**
|
|
170
|
-
* Create a GravitoContext from a raw request.
|
|
171
|
-
*
|
|
172
|
-
* This is used internally for testing and advanced scenarios.
|
|
173
|
-
*
|
|
174
|
-
* @param request - Raw HTTP request
|
|
175
|
-
* @returns Gravito context
|
|
176
|
-
*/
|
|
177
|
-
createContext(request: Request): GravitoContext<V>;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Factory function type for creating adapters
|
|
181
|
-
*/
|
|
182
|
-
export type AdapterFactory<V extends GravitoVariables = GravitoVariables> = (config?: AdapterConfig) => HttpAdapter<V>;
|
|
183
|
-
/**
|
|
184
|
-
* Check if a value is an HttpAdapter
|
|
185
|
-
*/
|
|
186
|
-
export declare function isHttpAdapter(value: unknown): value is HttpAdapter;
|
|
@@ -1,117 +0,0 @@
|
|
|
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 } from './types';
|
|
15
|
-
/**
|
|
16
|
-
* AOT Router - Optimized for Bun
|
|
17
|
-
*
|
|
18
|
-
* Performance characteristics:
|
|
19
|
-
* - Static routes: O(1) lookup via Map
|
|
20
|
-
* - Dynamic routes: O(log n) via Radix Tree
|
|
21
|
-
* - Middleware: O(m) where m = number of matching middleware
|
|
22
|
-
*/
|
|
23
|
-
export declare class AOTRouter {
|
|
24
|
-
private staticRoutes;
|
|
25
|
-
private dynamicRouter;
|
|
26
|
-
private globalMiddleware;
|
|
27
|
-
private pathMiddleware;
|
|
28
|
-
/**
|
|
29
|
-
* Register a route
|
|
30
|
-
*
|
|
31
|
-
* Automatically determines if route is static or dynamic.
|
|
32
|
-
* Static routes are stored in a Map for O(1) lookup.
|
|
33
|
-
* Dynamic routes use the Radix Tree.
|
|
34
|
-
*
|
|
35
|
-
* @param method - HTTP method
|
|
36
|
-
* @param path - Route path
|
|
37
|
-
* @param handler - Route handler
|
|
38
|
-
* @param middleware - Route-specific middleware
|
|
39
|
-
*/
|
|
40
|
-
add(method: HttpMethod, path: string, handler: Handler, middleware?: Middleware[]): void;
|
|
41
|
-
/**
|
|
42
|
-
* Add global middleware
|
|
43
|
-
*
|
|
44
|
-
* These run for every request, before route-specific middleware.
|
|
45
|
-
*
|
|
46
|
-
* @param middleware - Middleware functions
|
|
47
|
-
*/
|
|
48
|
-
use(...middleware: Middleware[]): void;
|
|
49
|
-
/**
|
|
50
|
-
* Add path-based middleware
|
|
51
|
-
*
|
|
52
|
-
* Supports wildcard patterns like '/api/*'
|
|
53
|
-
*
|
|
54
|
-
* @param pattern - Path pattern
|
|
55
|
-
* @param middleware - Middleware functions
|
|
56
|
-
*/
|
|
57
|
-
usePattern(pattern: string, ...middleware: Middleware[]): void;
|
|
58
|
-
/**
|
|
59
|
-
* Match a request to a route
|
|
60
|
-
*
|
|
61
|
-
* Returns the handler, params, and all applicable middleware.
|
|
62
|
-
*
|
|
63
|
-
* @param method - HTTP method
|
|
64
|
-
* @param path - Request path
|
|
65
|
-
* @returns Route match or null if not found
|
|
66
|
-
*/
|
|
67
|
-
match(method: string, path: string): RouteMatch;
|
|
68
|
-
/**
|
|
69
|
-
* Public wrapper for collectMiddleware (used by Gravito for optimization)
|
|
70
|
-
*/
|
|
71
|
-
collectMiddlewarePublic(path: string, routeMiddleware: Middleware[]): Middleware[];
|
|
72
|
-
/**
|
|
73
|
-
* Collect all applicable middleware for a path
|
|
74
|
-
*
|
|
75
|
-
* Order: global -> pattern-based -> route-specific
|
|
76
|
-
*
|
|
77
|
-
* @param path - Request path
|
|
78
|
-
* @param routeMiddleware - Route-specific middleware
|
|
79
|
-
* @returns Combined middleware array
|
|
80
|
-
*/
|
|
81
|
-
private collectMiddleware;
|
|
82
|
-
/**
|
|
83
|
-
* Check if a path is static (no parameters or wildcards)
|
|
84
|
-
*/
|
|
85
|
-
private isStaticPath;
|
|
86
|
-
/**
|
|
87
|
-
* Match a pattern against a path
|
|
88
|
-
*
|
|
89
|
-
* Supports:
|
|
90
|
-
* - Exact match: '/api/users'
|
|
91
|
-
* - Wildcard suffix: '/api/*'
|
|
92
|
-
*
|
|
93
|
-
* @param pattern - Pattern to match
|
|
94
|
-
* @param path - Path to test
|
|
95
|
-
* @returns True if pattern matches
|
|
96
|
-
*/
|
|
97
|
-
private matchPattern;
|
|
98
|
-
/**
|
|
99
|
-
* Find the original route key for a matched dynamic route
|
|
100
|
-
*
|
|
101
|
-
* This is needed to look up route-specific middleware.
|
|
102
|
-
* It's a bit of a hack, but avoids storing duplicate data.
|
|
103
|
-
*
|
|
104
|
-
* @param method - HTTP method
|
|
105
|
-
* @param path - Matched path
|
|
106
|
-
* @returns Route key or null
|
|
107
|
-
*/
|
|
108
|
-
private findDynamicRouteKey;
|
|
109
|
-
/**
|
|
110
|
-
* Get all registered routes (for debugging)
|
|
111
|
-
*/
|
|
112
|
-
getRoutes(): Array<{
|
|
113
|
-
method: string;
|
|
114
|
-
path: string;
|
|
115
|
-
type: 'static' | 'dynamic';
|
|
116
|
-
}>;
|
|
117
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
* FastContext - Pooled request context
|
|
12
|
-
*
|
|
13
|
-
* Designed for minimal memory allocation and maximum reuse.
|
|
14
|
-
* All response helpers create Response objects directly without intermediate wrappers.
|
|
15
|
-
*/
|
|
16
|
-
export declare class FastContext implements IFastContext {
|
|
17
|
-
private _req;
|
|
18
|
-
private _headers;
|
|
19
|
-
/**
|
|
20
|
-
* Reset context for pooling
|
|
21
|
-
*
|
|
22
|
-
* This is called when acquiring from the pool.
|
|
23
|
-
* Must clear all state from previous request.
|
|
24
|
-
*/
|
|
25
|
-
reset(request: Request, params?: Record<string, string>): this;
|
|
26
|
-
get req(): FastRequest;
|
|
27
|
-
json<T>(data: T, status?: number): Response;
|
|
28
|
-
text(text: string, status?: number): Response;
|
|
29
|
-
html(html: string, status?: number): Response;
|
|
30
|
-
redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
31
|
-
body(data: BodyInit | null, status?: number): Response;
|
|
32
|
-
header(name: string, value: string): void;
|
|
33
|
-
status(_code: number): void;
|
|
34
|
-
}
|