@gravito/cosmos 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/core/src/Application.d.ts +185 -0
  3. package/dist/core/src/ConfigManager.d.ts +21 -0
  4. package/dist/core/src/Container.d.ts +38 -0
  5. package/dist/core/src/Event.d.ts +5 -0
  6. package/dist/core/src/EventManager.d.ts +123 -0
  7. package/dist/core/src/GlobalErrorHandlers.d.ts +31 -0
  8. package/dist/core/src/GravitoServer.d.ts +20 -0
  9. package/dist/core/src/HookManager.d.ts +70 -0
  10. package/dist/core/src/Listener.d.ts +4 -0
  11. package/dist/core/src/Logger.d.ts +20 -0
  12. package/dist/core/src/PlanetCore.d.ts +207 -0
  13. package/dist/core/src/Route.d.ts +25 -0
  14. package/dist/core/src/Router.d.ts +232 -0
  15. package/dist/core/src/ServiceProvider.d.ts +150 -0
  16. package/dist/core/src/adapters/PhotonAdapter.d.ts +142 -0
  17. package/dist/core/src/adapters/bun/BunContext.d.ts +36 -0
  18. package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +21 -0
  19. package/dist/core/src/adapters/bun/BunRequest.d.ts +27 -0
  20. package/dist/core/src/adapters/bun/RadixNode.d.ts +15 -0
  21. package/dist/core/src/adapters/bun/RadixRouter.d.ts +31 -0
  22. package/dist/core/src/adapters/bun/types.d.ts +20 -0
  23. package/dist/core/src/adapters/types.d.ts +186 -0
  24. package/dist/core/src/engine/AOTRouter.d.ts +117 -0
  25. package/dist/core/src/engine/FastContext.d.ts +34 -0
  26. package/dist/core/src/engine/Gravito.d.ts +191 -0
  27. package/dist/core/src/engine/MinimalContext.d.ts +36 -0
  28. package/dist/core/src/engine/analyzer.d.ts +21 -0
  29. package/dist/core/src/engine/index.d.ts +26 -0
  30. package/dist/core/src/engine/path.d.ts +26 -0
  31. package/dist/core/src/engine/pool.d.ts +83 -0
  32. package/dist/core/src/engine/types.d.ts +107 -0
  33. package/dist/core/src/exceptions/AuthenticationException.d.ts +4 -0
  34. package/dist/core/src/exceptions/AuthorizationException.d.ts +4 -0
  35. package/dist/core/src/exceptions/GravitoException.d.ts +15 -0
  36. package/dist/core/src/exceptions/HttpException.d.ts +5 -0
  37. package/dist/core/src/exceptions/ModelNotFoundException.d.ts +6 -0
  38. package/dist/core/src/exceptions/ValidationException.d.ts +14 -0
  39. package/dist/core/src/exceptions/index.d.ts +6 -0
  40. package/dist/core/src/helpers/Arr.d.ts +14 -0
  41. package/dist/core/src/helpers/Str.d.ts +18 -0
  42. package/dist/core/src/helpers/data.d.ts +5 -0
  43. package/dist/core/src/helpers/errors.d.ts +12 -0
  44. package/dist/core/src/helpers/response.d.ts +17 -0
  45. package/dist/core/src/helpers.d.ts +38 -0
  46. package/dist/core/src/http/CookieJar.d.ts +37 -0
  47. package/dist/core/src/http/middleware/BodySizeLimit.d.ts +6 -0
  48. package/dist/core/src/http/middleware/Cors.d.ts +12 -0
  49. package/dist/core/src/http/middleware/Csrf.d.ts +11 -0
  50. package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +11 -0
  51. package/dist/core/src/http/middleware/SecurityHeaders.d.ts +17 -0
  52. package/dist/core/src/http/middleware/ThrottleRequests.d.ts +12 -0
  53. package/dist/core/src/http/types.d.ts +312 -0
  54. package/dist/core/src/index.d.ts +60 -0
  55. package/dist/core/src/runtime.d.ts +63 -0
  56. package/dist/core/src/security/Encrypter.d.ts +24 -0
  57. package/dist/core/src/security/Hasher.d.ts +29 -0
  58. package/dist/core/src/testing/HttpTester.d.ts +38 -0
  59. package/dist/core/src/testing/TestResponse.d.ts +78 -0
  60. package/dist/core/src/testing/index.d.ts +2 -0
  61. package/dist/core/src/types/events.d.ts +94 -0
  62. package/dist/cosmos/src/I18nService.d.ts +144 -0
  63. package/dist/cosmos/src/index.d.ts +21 -0
  64. package/dist/cosmos/src/loader.d.ts +11 -0
  65. package/dist/index.js +1 -1
  66. package/dist/photon/src/index.d.ts +2 -0
  67. package/package.json +1 -1
  68. package/src/I18nService.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @gravito/cosmos
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @gravito/core@1.2.0
9
+
3
10
  ## 2.0.0
4
11
 
5
12
  ### Patch Changes
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @fileoverview Application - Enterprise Application Container
3
+ *
4
+ * A high-level application class that orchestrates the entire framework.
5
+ * Provides a centralized entry point for enterprise applications with
6
+ * auto-discovery of providers, config loading, and lifecycle management.
7
+ *
8
+ * @module @gravito/core
9
+ * @since 2.0.0
10
+ */
11
+ import { ConfigManager } from './ConfigManager';
12
+ import { Container } from './Container';
13
+ import type { EventManager } from './EventManager';
14
+ import type { Logger } from './Logger';
15
+ import { PlanetCore } from './PlanetCore';
16
+ import type { ServiceProvider } from './ServiceProvider';
17
+ export interface ApplicationConfig {
18
+ /**
19
+ * Base path of the application
20
+ */
21
+ basePath: string;
22
+ /**
23
+ * Path to the config directory (relative to basePath)
24
+ * @default 'config'
25
+ */
26
+ configPath?: string;
27
+ /**
28
+ * Path to the providers directory (relative to basePath)
29
+ * @default 'src/Providers'
30
+ */
31
+ providersPath?: string;
32
+ /**
33
+ * Environment (development, production, testing)
34
+ */
35
+ env?: 'development' | 'production' | 'testing';
36
+ /**
37
+ * Logger instance
38
+ */
39
+ logger?: Logger;
40
+ /**
41
+ * Initial configuration values
42
+ */
43
+ config?: Record<string, unknown>;
44
+ /**
45
+ * Service providers to register
46
+ */
47
+ providers?: ServiceProvider[];
48
+ /**
49
+ * Whether to auto-discover providers from providersPath
50
+ * @default true
51
+ */
52
+ autoDiscoverProviders?: boolean;
53
+ }
54
+ /**
55
+ * Application - Enterprise-grade application container.
56
+ *
57
+ * Provides a higher-level abstraction over PlanetCore for building
58
+ * enterprise applications with convention-over-configuration patterns.
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // Create application
63
+ * const app = new Application({
64
+ * basePath: import.meta.dir,
65
+ * env: process.env.NODE_ENV as 'development' | 'production',
66
+ * });
67
+ *
68
+ * // Boot the application
69
+ * await app.boot();
70
+ *
71
+ * // Access core
72
+ * export default app.core.liftoff();
73
+ * ```
74
+ */
75
+ export declare class Application {
76
+ /**
77
+ * The underlying PlanetCore instance.
78
+ */
79
+ readonly core: PlanetCore;
80
+ /**
81
+ * The IoC container.
82
+ */
83
+ readonly container: Container;
84
+ /**
85
+ * The configuration manager.
86
+ */
87
+ readonly config: ConfigManager;
88
+ /**
89
+ * The event manager.
90
+ */
91
+ readonly events: EventManager;
92
+ /**
93
+ * The logger instance.
94
+ */
95
+ readonly logger: Logger;
96
+ /**
97
+ * Application base path.
98
+ */
99
+ readonly basePath: string;
100
+ /**
101
+ * Environment mode.
102
+ */
103
+ readonly env: 'development' | 'production' | 'testing';
104
+ /**
105
+ * Configuration options.
106
+ */
107
+ private readonly options;
108
+ /**
109
+ * Whether the application has been booted.
110
+ */
111
+ private booted;
112
+ constructor(options: ApplicationConfig);
113
+ /**
114
+ * Boot the application.
115
+ *
116
+ * This will:
117
+ * 1. Load configuration files
118
+ * 2. Auto-discover providers (if enabled)
119
+ * 3. Register all providers
120
+ * 4. Bootstrap the core
121
+ *
122
+ * @returns Promise that resolves when boot is complete
123
+ */
124
+ boot(): Promise<this>;
125
+ /**
126
+ * Load configuration files from the config directory.
127
+ *
128
+ * @internal
129
+ */
130
+ private loadConfiguration;
131
+ /**
132
+ * Discover and register providers from the providers directory.
133
+ *
134
+ * @internal
135
+ */
136
+ private discoverProviders;
137
+ /**
138
+ * Get a service from the container.
139
+ *
140
+ * @param key - The service key
141
+ * @returns The resolved service
142
+ */
143
+ make<T>(key: string): T;
144
+ /**
145
+ * Check if a service is bound.
146
+ *
147
+ * @param key - The service key
148
+ * @returns True if bound
149
+ */
150
+ has(key: string): boolean;
151
+ /**
152
+ * Get a configuration value.
153
+ *
154
+ * @param key - The config key (supports dot notation)
155
+ * @param defaultValue - Default value if not found
156
+ * @returns The config value
157
+ */
158
+ getConfig<T>(key: string, defaultValue?: T): T;
159
+ /**
160
+ * Create application path helper.
161
+ *
162
+ * @param segments - Path segments relative to base path
163
+ * @returns Absolute path
164
+ */
165
+ path(...segments: string[]): string;
166
+ /**
167
+ * Get the config path.
168
+ *
169
+ * @param segments - Additional path segments
170
+ * @returns Absolute path to config directory
171
+ */
172
+ configPath(...segments: string[]): string;
173
+ /**
174
+ * Check if running in production.
175
+ */
176
+ isProduction(): boolean;
177
+ /**
178
+ * Check if running in development.
179
+ */
180
+ isDevelopment(): boolean;
181
+ /**
182
+ * Check if running in testing.
183
+ */
184
+ isTesting(): boolean;
185
+ }
@@ -0,0 +1,21 @@
1
+ export declare class ConfigManager {
2
+ private config;
3
+ constructor(initialConfig?: Record<string, unknown>);
4
+ /**
5
+ * Load all environment variables from the active runtime.
6
+ */
7
+ private loadEnv;
8
+ /**
9
+ * Get a configuration value (generic return type supported).
10
+ * Supports dot notation for deep access (e.g. 'app.name').
11
+ */
12
+ get<T = unknown>(key: string, defaultValue?: T): T;
13
+ /**
14
+ * Set a configuration value.
15
+ */
16
+ set(key: string, value: unknown): void;
17
+ /**
18
+ * Check whether a key exists.
19
+ */
20
+ has(key: string): boolean;
21
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Factory type for creating service instances
3
+ */
4
+ export type Factory<T> = (container: Container) => T;
5
+ export declare class Container {
6
+ private bindings;
7
+ private instances;
8
+ /**
9
+ * Bind a service to the container.
10
+ * New instance will be created on each resolution.
11
+ */
12
+ bind<T>(key: string, factory: Factory<T>): void;
13
+ /**
14
+ * Bind a shared service to the container (Singleton).
15
+ * Same instance will be returned on each resolution.
16
+ */
17
+ singleton<T>(key: string, factory: Factory<T>): void;
18
+ /**
19
+ * Register an existing instance as shared service.
20
+ */
21
+ instance<T>(key: string, instance: T): void;
22
+ /**
23
+ * Resolve a service from the container.
24
+ */
25
+ make<T>(key: string): T;
26
+ /**
27
+ * Check if a service is bound.
28
+ */
29
+ has(key: string): boolean;
30
+ /**
31
+ * Flush all instances and bindings.
32
+ */
33
+ flush(): void;
34
+ /**
35
+ * Forget a specific instance (but keep binding)
36
+ */
37
+ forget(key: string): void;
38
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Event base class and related types.
3
+ */
4
+ export type { Channel, ShouldBroadcast } from './types/events';
5
+ export { Event } from './types/events';
@@ -0,0 +1,123 @@
1
+ import { Event } from './Event';
2
+ import type { Listener } from './Listener';
3
+ import type { PlanetCore } from './PlanetCore';
4
+ /**
5
+ * Listener registration metadata.
6
+ */
7
+ interface ListenerRegistration<TEvent extends Event = Event> {
8
+ listener: Listener<TEvent> | (new () => Listener<TEvent>);
9
+ queue?: string;
10
+ connection?: string;
11
+ delay?: number;
12
+ }
13
+ /**
14
+ * Event manager.
15
+ *
16
+ * Provides type-safe event dispatching and listener registration.
17
+ * Supports both synchronous listeners and asynchronous (queued) listeners.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * class UserRegistered extends Event {
22
+ * constructor(public user: User) {
23
+ * super()
24
+ * }
25
+ * }
26
+ *
27
+ * class SendWelcomeEmail implements Listener<UserRegistered> {
28
+ * async handle(event: UserRegistered): Promise<void> {
29
+ * // send welcome email
30
+ * }
31
+ * }
32
+ *
33
+ * // Register listener
34
+ * core.events.listen(UserRegistered, SendWelcomeEmail)
35
+ *
36
+ * // Dispatch event
37
+ * await core.events.dispatch(new UserRegistered(user))
38
+ * ```
39
+ */
40
+ export declare class EventManager {
41
+ private core;
42
+ /**
43
+ * Listener registry.
44
+ * Key: event class or event name
45
+ * Value: listener registrations
46
+ */
47
+ private listeners;
48
+ /**
49
+ * Broadcast manager (optional, injected by `orbit-broadcasting`).
50
+ */
51
+ private broadcastManager;
52
+ /**
53
+ * Queue manager (optional, injected by `orbit-queue`).
54
+ */
55
+ private queueManager;
56
+ constructor(core: PlanetCore);
57
+ /**
58
+ * Register the broadcast manager (called by `orbit-broadcasting`).
59
+ */
60
+ setBroadcastManager(manager: EventManager['broadcastManager']): void;
61
+ /**
62
+ * Register the queue manager (called by `orbit-queue`).
63
+ */
64
+ setQueueManager(manager: EventManager['queueManager']): void;
65
+ /**
66
+ * Register an event listener.
67
+ *
68
+ * @param event - Event class or event name
69
+ * @param listener - Listener instance or listener class
70
+ * @param options - Optional queue options
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * // Synchronous listener
75
+ * core.events.listen(UserRegistered, SendWelcomeEmail)
76
+ *
77
+ * // Queued listener (async)
78
+ * core.events.listen(UserRegistered, SendWelcomeEmail, {
79
+ * queue: 'emails',
80
+ * delay: 60
81
+ * })
82
+ * ```
83
+ */
84
+ listen<TEvent extends Event>(event: string | (new (...args: unknown[]) => TEvent), listener: Listener<TEvent> | (new () => Listener<TEvent>), options?: {
85
+ queue?: string;
86
+ connection?: string;
87
+ delay?: number;
88
+ }): void;
89
+ /**
90
+ * Remove an event listener.
91
+ *
92
+ * @param event - Event class or event name
93
+ * @param listener - Listener to remove
94
+ */
95
+ unlisten<TEvent extends Event>(event: string | (new (...args: unknown[]) => TEvent), listener: Listener<TEvent> | (new () => Listener<TEvent>)): void;
96
+ /**
97
+ * Dispatch an event.
98
+ *
99
+ * Runs all registered listeners. If a listener implements `ShouldQueue` or
100
+ * has queue options, the listener will be pushed to the queue for async execution.
101
+ *
102
+ * @param event - Event instance
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * await core.events.dispatch(new UserRegistered(user))
107
+ * ```
108
+ */
109
+ dispatch<TEvent extends Event>(event: TEvent): Promise<void>;
110
+ /**
111
+ * Serialize an event (for queueing).
112
+ */
113
+ private serializeEvent;
114
+ /**
115
+ * Get all registered listeners.
116
+ */
117
+ getListeners(event?: string | (new () => Event)): ListenerRegistration[];
118
+ /**
119
+ * Clear all listeners.
120
+ */
121
+ clear(): void;
122
+ }
123
+ export {};
@@ -0,0 +1,31 @@
1
+ import type { Logger } from './Logger';
2
+ import type { PlanetCore } from './PlanetCore';
3
+ export type GlobalProcessErrorKind = 'unhandledRejection' | 'uncaughtException';
4
+ export type GlobalProcessErrorHandlerContext = {
5
+ core?: PlanetCore;
6
+ kind: GlobalProcessErrorKind;
7
+ error: unknown;
8
+ isProduction: boolean;
9
+ timestamp: number;
10
+ logLevel?: 'error' | 'warn' | 'info' | 'none';
11
+ logMessage?: string;
12
+ exit?: boolean;
13
+ exitCode?: number;
14
+ gracePeriodMs?: number;
15
+ };
16
+ export type GlobalErrorHandlersMode = 'log' | 'exit' | 'exitInProduction';
17
+ export type RegisterGlobalErrorHandlersOptions = {
18
+ core?: PlanetCore;
19
+ logger?: Logger;
20
+ mode?: GlobalErrorHandlersMode;
21
+ exitCode?: number;
22
+ gracePeriodMs?: number;
23
+ };
24
+ /**
25
+ * Register process-level error handlers (`unhandledRejection` / `uncaughtException`).
26
+ *
27
+ * - `mode: "log"`: only log/report
28
+ * - `mode: "exit"`: report then `process.exit(exitCode)`
29
+ * - `mode: "exitInProduction"`: exit only when `NODE_ENV=production` (default)
30
+ */
31
+ export declare function registerGlobalErrorHandlers(options?: RegisterGlobalErrorHandlersOptions): () => void;
@@ -0,0 +1,20 @@
1
+ import { type GravitoConfig, PlanetCore } from './PlanetCore';
2
+ export interface GravitoManifest {
3
+ name: string;
4
+ version?: string;
5
+ modules: string[];
6
+ config?: GravitoConfig;
7
+ }
8
+ export type ModuleResolver = () => Promise<any>;
9
+ /**
10
+ * Gravito 核心啟動引擎 (已解耦)
11
+ */
12
+ export declare class GravitoServer {
13
+ /**
14
+ * 一鍵建立並組裝伺服器
15
+ * @param manifest 站點描述清單
16
+ * @param resolvers 模組解析器字典
17
+ * @param baseOrbits 基礎軌道模組 (例如 OrbitMonolith)
18
+ */
19
+ static create(manifest: GravitoManifest, resolvers: Record<string, ModuleResolver>, baseOrbits?: any[]): Promise<PlanetCore>;
20
+ }
@@ -0,0 +1,70 @@
1
+ export type FilterCallback<T = unknown> = (value: T, ...args: unknown[]) => Promise<T> | T;
2
+ export type ActionCallback<TArgs = unknown> = (args: TArgs) => Promise<void> | void;
3
+ export declare class HookManager {
4
+ private filters;
5
+ private actions;
6
+ /**
7
+ * Register a filter hook.
8
+ *
9
+ * Filters are used to transform a value (input/output).
10
+ *
11
+ * @template T - The type of value being filtered.
12
+ * @param hook - The name of the hook.
13
+ * @param callback - The callback function to execute.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * core.hooks.addFilter('content', async (content: string) => {
18
+ * return content.toUpperCase();
19
+ * });
20
+ * ```
21
+ */
22
+ addFilter<T = unknown>(hook: string, callback: FilterCallback<T>): void;
23
+ /**
24
+ * Apply all registered filters sequentially.
25
+ *
26
+ * Each callback receives the previous callback's return value.
27
+ *
28
+ * @template T - The type of value being filtered.
29
+ * @param hook - The name of the hook.
30
+ * @param initialValue - The initial value to filter.
31
+ * @param args - Additional arguments to pass to the callbacks.
32
+ * @returns The final filtered value.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const content = await core.hooks.applyFilters('content', 'hello world');
37
+ * ```
38
+ */
39
+ applyFilters<T = unknown>(hook: string, initialValue: T, ...args: unknown[]): Promise<T>;
40
+ /**
41
+ * Register an action hook.
42
+ *
43
+ * Actions are for side effects (no return value).
44
+ *
45
+ * @template TArgs - The type of arguments passed to the action.
46
+ * @param hook - The name of the hook.
47
+ * @param callback - The callback function to execute.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * core.hooks.addAction('user_registered', async (user: User) => {
52
+ * await sendWelcomeEmail(user);
53
+ * });
54
+ * ```
55
+ */
56
+ addAction<TArgs = unknown>(hook: string, callback: ActionCallback<TArgs>): void;
57
+ /**
58
+ * Run all registered actions sequentially.
59
+ *
60
+ * @template TArgs - The type of arguments passed to the action.
61
+ * @param hook - The name of the hook.
62
+ * @param args - The arguments to pass to the callbacks.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * await core.hooks.doAction('user_registered', user);
67
+ * ```
68
+ */
69
+ doAction<TArgs = unknown>(hook: string, args: TArgs): Promise<void>;
70
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Listener-related types and interfaces.
3
+ */
4
+ export type { Listener, ShouldQueue } from './types/events';
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Standard logger interface.
3
+ *
4
+ * PSR-3 inspired API for easy swapping (e.g., Winston, Pino).
5
+ */
6
+ export interface Logger {
7
+ debug(message: string, ...args: unknown[]): void;
8
+ info(message: string, ...args: unknown[]): void;
9
+ warn(message: string, ...args: unknown[]): void;
10
+ error(message: string, ...args: unknown[]): void;
11
+ }
12
+ /**
13
+ * Default console logger implementation.
14
+ */
15
+ export declare class ConsoleLogger implements Logger {
16
+ debug(message: string, ...args: unknown[]): void;
17
+ info(message: string, ...args: unknown[]): void;
18
+ warn(message: string, ...args: unknown[]): void;
19
+ error(message: string, ...args: unknown[]): void;
20
+ }