@gravito/ripple 3.0.1 → 4.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 (168) hide show
  1. package/README.md +432 -18
  2. package/README.zh-TW.md +104 -2
  3. package/dist/atlas/src/DB.d.ts +301 -0
  4. package/dist/atlas/src/OrbitAtlas.d.ts +9 -0
  5. package/dist/atlas/src/config/defineConfig.d.ts +14 -0
  6. package/dist/atlas/src/config/index.d.ts +7 -0
  7. package/dist/atlas/src/config/loadConfig.d.ts +48 -0
  8. package/dist/atlas/src/connection/Connection.d.ts +108 -0
  9. package/dist/atlas/src/connection/ConnectionManager.d.ts +111 -0
  10. package/dist/atlas/src/drivers/BunSQLDriver.d.ts +32 -0
  11. package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +118 -0
  12. package/dist/atlas/src/drivers/MongoDBDriver.d.ts +36 -0
  13. package/dist/atlas/src/drivers/MySQLDriver.d.ts +66 -0
  14. package/dist/atlas/src/drivers/PostgresDriver.d.ts +83 -0
  15. package/dist/atlas/src/drivers/RedisDriver.d.ts +43 -0
  16. package/dist/atlas/src/drivers/SQLiteDriver.d.ts +45 -0
  17. package/dist/atlas/src/drivers/types.d.ts +260 -0
  18. package/dist/atlas/src/errors/index.d.ts +45 -0
  19. package/dist/atlas/src/grammar/Grammar.d.ts +342 -0
  20. package/dist/atlas/src/grammar/MongoGrammar.d.ts +47 -0
  21. package/dist/atlas/src/grammar/MySQLGrammar.d.ts +54 -0
  22. package/dist/atlas/src/grammar/NullGrammar.d.ts +35 -0
  23. package/dist/atlas/src/grammar/PostgresGrammar.d.ts +62 -0
  24. package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +32 -0
  25. package/dist/atlas/src/index.d.ts +67 -0
  26. package/dist/atlas/src/migration/Migration.d.ts +64 -0
  27. package/dist/atlas/src/migration/MigrationRepository.d.ts +65 -0
  28. package/dist/atlas/src/migration/Migrator.d.ts +110 -0
  29. package/dist/atlas/src/migration/index.d.ts +6 -0
  30. package/dist/atlas/src/observability/AtlasMetrics.d.ts +11 -0
  31. package/dist/atlas/src/observability/AtlasObservability.d.ts +15 -0
  32. package/dist/atlas/src/observability/AtlasTracer.d.ts +12 -0
  33. package/dist/atlas/src/observability/index.d.ts +9 -0
  34. package/dist/atlas/src/orm/index.d.ts +5 -0
  35. package/dist/atlas/src/orm/model/DirtyTracker.d.ts +121 -0
  36. package/dist/atlas/src/orm/model/Model.d.ts +449 -0
  37. package/dist/atlas/src/orm/model/ModelRegistry.d.ts +20 -0
  38. package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +136 -0
  39. package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +36 -0
  40. package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +87 -0
  41. package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +117 -0
  42. package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +64 -0
  43. package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +15 -0
  44. package/dist/atlas/src/orm/model/concerns/index.d.ts +12 -0
  45. package/dist/atlas/src/orm/model/decorators.d.ts +109 -0
  46. package/dist/atlas/src/orm/model/errors.d.ts +52 -0
  47. package/dist/atlas/src/orm/model/index.d.ts +10 -0
  48. package/dist/atlas/src/orm/model/relationships.d.ts +207 -0
  49. package/dist/atlas/src/orm/model/types.d.ts +12 -0
  50. package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +123 -0
  51. package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +54 -0
  52. package/dist/atlas/src/orm/schema/index.d.ts +6 -0
  53. package/dist/atlas/src/orm/schema/types.d.ts +85 -0
  54. package/dist/atlas/src/query/Expression.d.ts +60 -0
  55. package/dist/atlas/src/query/NPlusOneDetector.d.ts +10 -0
  56. package/dist/atlas/src/query/QueryBuilder.d.ts +573 -0
  57. package/dist/atlas/src/query/clauses/GroupByClause.d.ts +51 -0
  58. package/dist/atlas/src/query/clauses/HavingClause.d.ts +70 -0
  59. package/dist/atlas/src/query/clauses/JoinClause.d.ts +87 -0
  60. package/dist/atlas/src/query/clauses/LimitClause.d.ts +82 -0
  61. package/dist/atlas/src/query/clauses/OrderByClause.d.ts +69 -0
  62. package/dist/atlas/src/query/clauses/SelectClause.d.ts +71 -0
  63. package/dist/atlas/src/query/clauses/WhereClause.d.ts +167 -0
  64. package/dist/atlas/src/query/clauses/index.d.ts +11 -0
  65. package/dist/atlas/src/schema/Blueprint.d.ts +276 -0
  66. package/dist/atlas/src/schema/ColumnDefinition.d.ts +154 -0
  67. package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +37 -0
  68. package/dist/atlas/src/schema/Schema.d.ts +131 -0
  69. package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +23 -0
  70. package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +26 -0
  71. package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +28 -0
  72. package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +97 -0
  73. package/dist/atlas/src/schema/grammars/index.d.ts +7 -0
  74. package/dist/atlas/src/schema/index.d.ts +8 -0
  75. package/dist/atlas/src/seed/Factory.d.ts +90 -0
  76. package/dist/atlas/src/seed/Seeder.d.ts +28 -0
  77. package/dist/atlas/src/seed/SeederRunner.d.ts +74 -0
  78. package/dist/atlas/src/seed/index.d.ts +6 -0
  79. package/dist/atlas/src/types/index.d.ts +1100 -0
  80. package/dist/atlas/src/utils/levenshtein.d.ts +9 -0
  81. package/dist/core/src/Application.d.ts +43 -17
  82. package/dist/core/src/CommandKernel.d.ts +33 -0
  83. package/dist/core/src/Container.d.ts +78 -14
  84. package/dist/core/src/HookManager.d.ts +422 -8
  85. package/dist/core/src/PlanetCore.d.ts +52 -7
  86. package/dist/core/src/Router.d.ts +41 -7
  87. package/dist/core/src/ServiceProvider.d.ts +14 -8
  88. package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +1 -0
  89. package/dist/core/src/adapters/PhotonAdapter.d.ts +1 -0
  90. package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +1 -0
  91. package/dist/core/src/adapters/types.d.ts +39 -0
  92. package/dist/core/src/engine/AOTRouter.d.ts +1 -11
  93. package/dist/core/src/engine/FastContext.d.ts +4 -2
  94. package/dist/core/src/engine/Gravito.d.ts +1 -1
  95. package/dist/core/src/engine/MinimalContext.d.ts +4 -2
  96. package/dist/core/src/engine/types.d.ts +6 -1
  97. package/dist/core/src/events/CircuitBreaker.d.ts +229 -0
  98. package/dist/core/src/events/DeadLetterQueue.d.ts +145 -0
  99. package/dist/core/src/events/EventBackend.d.ts +11 -0
  100. package/dist/core/src/events/EventOptions.d.ts +109 -0
  101. package/dist/core/src/events/EventPriorityQueue.d.ts +202 -0
  102. package/dist/core/src/events/IdempotencyCache.d.ts +60 -0
  103. package/dist/core/src/events/index.d.ts +14 -0
  104. package/dist/core/src/events/observability/EventMetrics.d.ts +132 -0
  105. package/dist/core/src/events/observability/EventTracer.d.ts +68 -0
  106. package/dist/core/src/events/observability/EventTracing.d.ts +161 -0
  107. package/dist/core/src/events/observability/OTelEventMetrics.d.ts +240 -0
  108. package/dist/core/src/events/observability/ObservableHookManager.d.ts +108 -0
  109. package/dist/core/src/events/observability/index.d.ts +20 -0
  110. package/dist/core/src/events/observability/metrics-types.d.ts +16 -0
  111. package/dist/core/src/events/types.d.ts +75 -0
  112. package/dist/core/src/exceptions/CircularDependencyException.d.ts +9 -0
  113. package/dist/core/src/exceptions/index.d.ts +1 -0
  114. package/dist/core/src/http/cookie.d.ts +29 -0
  115. package/dist/core/src/http/types.d.ts +21 -0
  116. package/dist/core/src/index.d.ts +13 -3
  117. package/dist/core/src/instrumentation/index.d.ts +35 -0
  118. package/dist/core/src/instrumentation/opentelemetry.d.ts +178 -0
  119. package/dist/core/src/instrumentation/types.d.ts +182 -0
  120. package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +316 -0
  121. package/dist/core/src/reliability/RetryPolicy.d.ts +217 -0
  122. package/dist/core/src/reliability/index.d.ts +6 -0
  123. package/dist/core/src/router/ControllerDispatcher.d.ts +12 -0
  124. package/dist/core/src/router/RequestValidator.d.ts +20 -0
  125. package/dist/index.js +6487 -9562
  126. package/dist/index.js.map +68 -62
  127. package/dist/photon/src/index.d.ts +69 -5
  128. package/dist/photon/src/middleware/binary.d.ts +12 -15
  129. package/dist/photon/src/middleware/htmx.d.ts +39 -0
  130. package/dist/photon/src/middleware/ratelimit.d.ts +157 -0
  131. package/dist/photon/src/openapi.d.ts +19 -0
  132. package/dist/proto/ripple.proto +120 -0
  133. package/dist/ripple/src/OrbitRipple.d.ts +34 -12
  134. package/dist/ripple/src/RippleServer.d.ts +76 -63
  135. package/dist/ripple/src/channels/ChannelManager.d.ts +132 -22
  136. package/dist/ripple/src/drivers/LocalDriver.d.ts +43 -11
  137. package/dist/ripple/src/drivers/NATSDriver.d.ts +87 -0
  138. package/dist/ripple/src/drivers/RedisDriver.d.ts +135 -28
  139. package/dist/ripple/src/drivers/index.d.ts +1 -0
  140. package/dist/ripple/src/engines/BunEngine.d.ts +98 -0
  141. package/dist/ripple/src/engines/IRippleEngine.d.ts +205 -0
  142. package/dist/ripple/src/engines/index.d.ts +11 -0
  143. package/dist/ripple/src/errors/RippleError.d.ts +48 -0
  144. package/dist/ripple/src/errors/index.d.ts +1 -0
  145. package/dist/ripple/src/events/BroadcastEvent.d.ts +78 -6
  146. package/dist/ripple/src/events/BroadcastManager.d.ts +100 -0
  147. package/dist/ripple/src/events/Broadcaster.d.ts +211 -14
  148. package/dist/ripple/src/events/index.d.ts +1 -0
  149. package/dist/ripple/src/health/HealthChecker.d.ts +93 -0
  150. package/dist/ripple/src/health/index.d.ts +1 -0
  151. package/dist/ripple/src/index.d.ts +42 -17
  152. package/dist/ripple/src/logging/Logger.d.ts +99 -0
  153. package/dist/ripple/src/logging/index.d.ts +1 -0
  154. package/dist/ripple/src/middleware/InterceptorManager.d.ts +21 -0
  155. package/dist/ripple/src/observability/RippleMetrics.d.ts +24 -0
  156. package/dist/ripple/src/reliability/AckManager.d.ts +48 -0
  157. package/dist/ripple/src/serializers/ISerializer.d.ts +39 -0
  158. package/dist/ripple/src/serializers/JsonSerializer.d.ts +19 -0
  159. package/dist/ripple/src/serializers/ProtobufSerializer.d.ts +38 -0
  160. package/dist/ripple/src/serializers/index.d.ts +3 -0
  161. package/dist/ripple/src/tracking/ConnectionTracker.d.ts +116 -0
  162. package/dist/ripple/src/tracking/SessionManager.d.ts +104 -0
  163. package/dist/ripple/src/tracking/index.d.ts +2 -0
  164. package/dist/ripple/src/types.d.ts +766 -28
  165. package/dist/ripple/src/utils/MessageSerializer.d.ts +54 -0
  166. package/dist/ripple/src/utils/TokenBucket.d.ts +25 -0
  167. package/dist/ripple/src/utils/index.d.ts +1 -0
  168. package/package.json +25 -7
@@ -1,20 +1,84 @@
1
1
  /**
2
- * @gravito/photon - High-performance web framework based on Hono.
2
+ * @gravito/photon - High-performance web engine for the Gravito Galaxy Architecture.
3
3
  *
4
- * Photon is the primary web engine for Gravito, providing a fast,
5
- * flexible, and standard-compliant API for building web applications.
6
- * It re-exports Hono while adding enterprise-grade middleware and utilities.
4
+ * Photon serves as the foundational HTTP layer for Gravito, providing an ultra-fast,
5
+ * type-safe routing system based on Hono. It is designed to be the "light" that
6
+ * connects Satellites (domain plugins) and Orbits (infrastructure) within the ecosystem.
7
+ *
8
+ * Key features:
9
+ * - Zero-overhead routing and middleware.
10
+ * - Full TypeScript inference for request parameters and body.
11
+ * - Built-in support for HTMX and binary (CBOR) protocols.
7
12
  *
8
13
  * @example
9
14
  * ```typescript
10
15
  * import { Photon } from '@gravito/photon'
16
+ *
11
17
  * const app = new Photon()
12
- * app.get('/', (c) => c.text('Hello!'))
18
+ *
19
+ * app.get('/welcome', (c) => c.text('Welcome to the Galaxy!'))
20
+ *
21
+ * export default app
13
22
  * ```
23
+ * @packageDocumentation
14
24
  */
15
25
  export * from 'hono';
26
+ /**
27
+ * The primary application class for Photon.
28
+ *
29
+ * An alias for `Hono`, providing the core routing and middleware capabilities.
30
+ * Use this to define your API structure and mount domain-specific Satellites.
31
+ *
32
+ * @remarks
33
+ * Photon extends Hono's capabilities with Gravito-specific optimizations.
34
+ * It serves as the entry point for defining routes, applying middleware,
35
+ * and handling the request-response lifecycle.
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const app = new Photon()
40
+ *
41
+ * // Basic routing
42
+ * app.get('/api/health', (c) => c.json({ status: 'ok' }))
43
+ *
44
+ * // Middleware integration
45
+ * app.use('/api/*', myMiddleware)
46
+ *
47
+ * // Mounting sub-routers
48
+ * app.route('/v1', v1Router)
49
+ * ```
50
+ * @public
51
+ */
16
52
  export { Hono as Photon } from 'hono';
17
53
  /**
18
54
  * Binary-related middleware for Photon.
55
+ *
56
+ * Provides utilities for handling binary data formats like CBOR,
57
+ * optimizing payload size and serialization speed for high-performance APIs.
58
+ *
59
+ * @public
19
60
  */
20
61
  export * from './middleware/binary';
62
+ /**
63
+ * HTMX-related middleware for Photon.
64
+ *
65
+ * Enhances Photon with first-class support for HTMX, including
66
+ * automatic request detection and simplified header access for hypermedia-driven UIs.
67
+ *
68
+ * @public
69
+ */
70
+ export * from './middleware/htmx';
71
+ /**
72
+ * Rate limiting middleware for Photon.
73
+ *
74
+ * Provides built-in rate limiting with token bucket and sliding window strategies.
75
+ * Supports both memory-based and custom storage backends.
76
+ *
77
+ * @public
78
+ */
79
+ export * from './middleware/ratelimit';
80
+ /**
81
+ * OpenAPI utilities
82
+ * @public
83
+ */
84
+ export * from './openapi';
@@ -1,10 +1,17 @@
1
1
  import type { MiddlewareHandler } from 'hono';
2
2
  /**
3
- * Binary Middleware for Photon
3
+ * Binary Middleware for Photon.
4
4
  *
5
5
  * Automatically detects 'Accept: application/cbor' and encodes
6
6
  * JSON responses using the CBOR binary format for high-performance communication.
7
7
  *
8
+ * @remarks
9
+ * This middleware is essential for high-frequency API calls where payload size
10
+ * and serialization speed are critical. It leverages the `cborg` library for
11
+ * efficient binary encoding.
12
+ *
13
+ * @returns A Hono middleware handler that intercepts JSON responses.
14
+ *
8
15
  * @example
9
16
  * ```typescript
10
17
  * import { Photon } from '@gravito/photon'
@@ -13,22 +20,12 @@ import type { MiddlewareHandler } from 'hono';
13
20
  * const app = new Photon()
14
21
  * app.use(binaryMiddleware())
15
22
  *
16
- * app.get('/api/data', (c) => c.json({ items: [...] }))
23
+ * app.get('/api/data', (c) => c.json({ items: [1, 2, 3] }))
17
24
  * ```
18
25
  *
19
26
  * @performance
20
- * - CBOR encoding is ~2-3x faster than JSON.stringify for large objects
21
- * - Binary format reduces payload size by 20-40% on average
22
- * - Recommended for high-frequency API calls with large datasets
23
- *
24
- * @client_usage
25
- * ```typescript
26
- * import { decode } from 'cborg'
27
- *
28
- * const res = await fetch('/api/data', {
29
- * headers: { Accept: 'application/cbor' }
30
- * })
31
- * const data = decode(new Uint8Array(await res.arrayBuffer()))
32
- * ```
27
+ * - CBOR encoding is ~2-3x faster than JSON.stringify for large objects.
28
+ * - Binary format reduces payload size by 20-40% on average.
29
+ * - Optimized to read body directly without clone(), saving ~30% overhead.
33
30
  */
34
31
  export declare const binaryMiddleware: () => MiddlewareHandler;
@@ -0,0 +1,39 @@
1
+ import type { MiddlewareHandler } from 'hono';
2
+ /**
3
+ * HTMX Middleware for Photon.
4
+ *
5
+ * Automatically detects HTMX requests and populates the context with
6
+ * HTMX-specific headers and state.
7
+ *
8
+ * @remarks
9
+ * This middleware enables hypermedia-driven UIs by providing first-class
10
+ * support for HTMX. It allows handlers to easily distinguish between
11
+ * full-page loads and partial updates.
12
+ *
13
+ * @returns A Hono middleware handler that populates HTMX context variables.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { Photon } from '@gravito/photon'
18
+ * import { htmxMiddleware } from '@gravito/photon/middleware/htmx'
19
+ *
20
+ * const app = new Photon()
21
+ * app.use(htmxMiddleware())
22
+ *
23
+ * app.get('/search', async (c) => {
24
+ * // Check if request is from HTMX
25
+ * if (c.get('htmx')) {
26
+ * return c.html('<div>Search results...</div>')
27
+ * }
28
+ *
29
+ * return c.html('<html>...</html>')
30
+ * })
31
+ * ```
32
+ *
33
+ * @context_variables
34
+ * - `htmx`: Boolean indicating if the request is an HTMX request.
35
+ * - `htmx.boosted`: Boolean indicating if the request was boosted.
36
+ * - `htmx.target`: The ID of the target element.
37
+ * - `htmx.trigger`: The ID of the trigger element.
38
+ */
39
+ export declare const htmxMiddleware: () => MiddlewareHandler;
@@ -0,0 +1,157 @@
1
+ /**
2
+ * @fileoverview Rate Limiting Middleware for Photon
3
+ *
4
+ * Provides token bucket and sliding window rate limiting strategies.
5
+ * Supports memory-based and Redis-based storage backends.
6
+ *
7
+ * @module @gravito/photon/middleware/ratelimit
8
+ * @since 1.0.0
9
+ */
10
+ import type { Context, MiddlewareHandler } from '@gravito/photon';
11
+ export interface RateLimitConfig {
12
+ /**
13
+ * Maximum number of requests allowed within the time window
14
+ * @default 100
15
+ */
16
+ maxRequests: number;
17
+ /**
18
+ * Time window in milliseconds
19
+ * @default 60000 (1 minute)
20
+ */
21
+ windowMs: number;
22
+ /**
23
+ * Strategy for rate limiting
24
+ * - 'token-bucket': Smooth rate limiting with token refill
25
+ * - 'sliding-window': Time-based window that slides continuously
26
+ * @default 'token-bucket'
27
+ */
28
+ strategy?: 'token-bucket' | 'sliding-window';
29
+ /**
30
+ * Custom key generator function
31
+ * @default (c) => c.req.header('x-forwarded-for') || c.req.header('x-real-ip') || 'unknown'
32
+ */
33
+ keyGenerator?: (c: Context) => string | Promise<string>;
34
+ /**
35
+ * Storage backend for rate limit state
36
+ * @default new MemoryStore()
37
+ */
38
+ store?: RateLimitStore;
39
+ /**
40
+ * Custom handler for rate-limited requests
41
+ * @default (c) => c.json({ error: 'Too Many Requests', retryAfter: <seconds> }, 429)
42
+ */
43
+ onRateLimitExceeded?: (c: Context, retryAfter: number) => Response | Promise<Response>;
44
+ /**
45
+ * Skip rate limiting for certain requests
46
+ * @default undefined
47
+ */
48
+ skip?: (c: Context) => boolean | Promise<boolean>;
49
+ /**
50
+ * Custom headers to include in responses
51
+ * @default true
52
+ */
53
+ standardHeaders?: boolean;
54
+ /**
55
+ * Include draft RateLimit headers (RateLimit-*)
56
+ * @default false
57
+ */
58
+ draftHeaders?: boolean;
59
+ }
60
+ export interface RateLimitStore {
61
+ /**
62
+ * Increment request count and return current state
63
+ */
64
+ increment(key: string): Promise<RateLimitState>;
65
+ /**
66
+ * Reset the rate limit for a key
67
+ */
68
+ reset(key: string): Promise<void>;
69
+ /**
70
+ * Get current state without incrementing
71
+ */
72
+ get(key: string): Promise<RateLimitState | null>;
73
+ }
74
+ export interface RateLimitState {
75
+ /**
76
+ * Number of requests made in current window
77
+ */
78
+ count: number;
79
+ /**
80
+ * Timestamp when the window expires (ms)
81
+ */
82
+ resetTime: number;
83
+ /**
84
+ * Remaining requests allowed
85
+ */
86
+ remaining: number;
87
+ }
88
+ export declare class MemoryStore implements RateLimitStore {
89
+ private config;
90
+ private store;
91
+ private cleanupInterval;
92
+ constructor(config: {
93
+ maxRequests: number;
94
+ windowMs: number;
95
+ }, cleanupIntervalMs?: number);
96
+ increment(key: string): Promise<RateLimitState>;
97
+ reset(key: string): Promise<void>;
98
+ get(key: string): Promise<RateLimitState | null>;
99
+ /**
100
+ * Cleanup resources
101
+ */
102
+ destroy(): void;
103
+ }
104
+ /**
105
+ * Create a rate limiting middleware
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * import { Photon } from '@gravito/photon'
110
+ * import { rateLimit } from '@gravito/photon/middleware'
111
+ *
112
+ * const app = new Photon()
113
+ *
114
+ * // Basic usage: 100 requests per minute
115
+ * app.use('*', rateLimit({ maxRequests: 100, windowMs: 60000 }))
116
+ *
117
+ * // Per-user rate limiting
118
+ * app.use('/api/*', rateLimit({
119
+ * maxRequests: 50,
120
+ * windowMs: 60000,
121
+ * keyGenerator: (c) => {
122
+ * const userId = c.get('userId')
123
+ * return userId || c.req.header('x-forwarded-for') || 'anonymous'
124
+ * }
125
+ * }))
126
+ * ```
127
+ */
128
+ export declare function rateLimit(config: RateLimitConfig): MiddlewareHandler;
129
+ /**
130
+ * Convenience factory: Create rate limiter with common presets
131
+ */
132
+ export declare const createRateLimiter: {
133
+ /**
134
+ * Strict: 10 requests per minute
135
+ */
136
+ strict: (overrides?: Partial<RateLimitConfig>) => MiddlewareHandler;
137
+ /**
138
+ * Moderate: 60 requests per minute
139
+ */
140
+ moderate: (overrides?: Partial<RateLimitConfig>) => MiddlewareHandler;
141
+ /**
142
+ * Lenient: 100 requests per minute
143
+ */
144
+ lenient: (overrides?: Partial<RateLimitConfig>) => MiddlewareHandler;
145
+ /**
146
+ * API: 1000 requests per hour
147
+ */
148
+ api: (overrides?: Partial<RateLimitConfig>) => MiddlewareHandler;
149
+ /**
150
+ * Auth: 5 attempts per 15 minutes (for login endpoints)
151
+ */
152
+ auth: (overrides?: Partial<RateLimitConfig>) => MiddlewareHandler;
153
+ };
154
+ /**
155
+ * Export convenience preset
156
+ */
157
+ export { createRateLimiter as rateLimiter };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @file packages/photon/src/openapi.ts
3
+ * @module @gravito/photon/openapi
4
+ * @description OpenAPI (Swagger) integration for Photon
5
+ */
6
+ import type { RouteConfig } from '@hono/zod-openapi';
7
+ import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
8
+ import { z } from 'zod';
9
+ /**
10
+ * Photon Open API Class
11
+ * Extends OpenAPIHono to provide a seamless OpenAPI integration.
12
+ */
13
+ export declare class PhotonOpenAPI extends OpenAPIHono {
14
+ /**
15
+ * Helper to create a fully typed route definition.
16
+ */
17
+ static route(config: RouteConfig): RouteConfig;
18
+ }
19
+ export { createRoute, z };
@@ -0,0 +1,120 @@
1
+ syntax = "proto3";
2
+
3
+ package ripple;
4
+
5
+ // -------------------------------------------------------------------
6
+ // Client -> Server Messages
7
+ // -------------------------------------------------------------------
8
+
9
+ message ClientMessage {
10
+ string req_id = 1; // Optional request ID for troubleshooting
11
+
12
+ oneof payload {
13
+ SubscribeRequest subscribe = 10;
14
+ UnsubscribeRequest unsubscribe = 11;
15
+ WhisperRequest whisper = 12;
16
+ PingRequest ping = 13;
17
+ BinaryRequest binary = 14;
18
+ AckRequest ack = 15;
19
+ }
20
+ }
21
+
22
+ message SubscribeRequest {
23
+ string channel = 1;
24
+ optional AuthData auth = 2;
25
+ }
26
+
27
+ message UnsubscribeRequest {
28
+ string channel = 1;
29
+ }
30
+
31
+ message WhisperRequest {
32
+ string channel = 1;
33
+ string event = 2;
34
+ // Payload is generic bytes, application needs to decode based on event
35
+ bytes data = 3;
36
+ }
37
+
38
+ message PingRequest {}
39
+
40
+ message BinaryRequest {
41
+ string channel = 1;
42
+ string event = 2;
43
+ bytes data = 3;
44
+ }
45
+
46
+ message AckRequest {
47
+ int32 seq = 1;
48
+ }
49
+
50
+ message AuthData {
51
+ string socket_id = 1;
52
+ string signature = 2;
53
+ }
54
+
55
+ // -------------------------------------------------------------------
56
+ // Server -> Client Messages
57
+ // -------------------------------------------------------------------
58
+
59
+ message ServerMessage {
60
+ oneof payload {
61
+ ConnectedResponse connected = 10;
62
+ SubscribedResponse subscribed = 11;
63
+ UnsubscribedResponse unsubscribed = 12;
64
+ ErrorResponse error = 13;
65
+ EventMessage event = 14;
66
+ PresenceMessage presence = 15;
67
+ PongResponse pong = 16;
68
+ BinaryMessage binary = 17;
69
+ AckReceivedResponse ack_received = 18;
70
+ ReconnectionTokenMessage reconnection_token = 19;
71
+ }
72
+ }
73
+
74
+ message ConnectedResponse {
75
+ string socket_id = 1;
76
+ }
77
+
78
+ message SubscribedResponse {
79
+ string channel = 1;
80
+ }
81
+
82
+ message UnsubscribedResponse {
83
+ string channel = 1;
84
+ }
85
+
86
+ message ErrorResponse {
87
+ string code = 1;
88
+ string message = 2;
89
+ optional string channel = 3;
90
+ }
91
+
92
+ message EventMessage {
93
+ string channel = 1;
94
+ string event = 2;
95
+ bytes data = 3; // JSON encoded string or raw bytes
96
+ optional int32 seq = 4;
97
+ optional bool need_ack = 5;
98
+ }
99
+
100
+ message PresenceMessage {
101
+ string channel = 1;
102
+ string event = 2; // "join", "leave", "members"
103
+ bytes data = 3; // JSON encoded user info
104
+ }
105
+
106
+ message PongResponse {}
107
+
108
+ message BinaryMessage {
109
+ string channel = 1;
110
+ string event = 2;
111
+ bytes data = 3;
112
+ }
113
+
114
+ message AckReceivedResponse {
115
+ int32 seq = 1;
116
+ }
117
+
118
+ message ReconnectionTokenMessage {
119
+ string token = 1;
120
+ }
@@ -1,42 +1,64 @@
1
1
  import type { GravitoOrbit, PlanetCore } from '@gravito/core';
2
+ import { BroadcastManager } from './events/BroadcastManager';
2
3
  import { RippleServer } from './RippleServer';
3
4
  import type { RippleConfig } from './types';
4
5
  /**
5
- * OrbitRipple provides native WebSocket support for Gravito.
6
- * it manages the RippleServer lifecycle, integrates with the core event system,
7
- * and provides a request-scoped server instance.
6
+ * OrbitRipple integrates the Ripple WebSocket module into the Gravito framework.
7
+ *
8
+ * It handles the registration of the RippleServer in the IoC container,
9
+ * adds middleware for context access, and manages the server lifecycle.
8
10
  *
9
11
  * @example
10
12
  * ```typescript
11
- * const ripple = new OrbitRipple({ path: '/realtime' });
12
- * core.addOrbit(ripple);
13
+ * import { PlanetCore } from '@gravito/core'
14
+ * import { OrbitRipple } from '@gravito/ripple'
15
+ *
16
+ * const core = new PlanetCore()
17
+ *
18
+ * // Install Ripple as an Orbit
19
+ * core.install(new OrbitRipple({
20
+ * path: '/ws',
21
+ * authorizer: async (channel, userId) => userId !== undefined
22
+ * }))
13
23
  * ```
14
- * @public
15
24
  */
16
25
  export declare class OrbitRipple implements GravitoOrbit {
17
26
  private server;
18
27
  private config;
19
28
  /**
20
- * Create a new OrbitRipple instance.
21
- * @param config - Configuration options for the WebSocket server.
29
+ * Create a new OrbitRipple.
30
+ *
31
+ * @param config - Ripple configuration options
22
32
  */
23
33
  constructor(config?: RippleConfig);
24
34
  /**
25
- * Install the module into PlanetCore
35
+ * Install the orbit into the Gravito core.
36
+ *
37
+ * - Registers 'ripple' (RippleServer) and 'broadcast' (BroadcastManager) in the container.
38
+ * - Adds middleware to inject 'ripple' and 'broadcast' into GravitoContext.
39
+ * - Initializes the server and registers shutdown hooks.
40
+ *
41
+ * @param core - The PlanetCore instance
26
42
  */
27
43
  install(core: PlanetCore): void;
28
44
  /**
29
- * Get the underlying RippleServer instance
45
+ * Get the underlying RippleServer instance.
46
+ *
47
+ * @returns The RippleServer instance
30
48
  */
31
49
  getServer(): RippleServer;
32
50
  /**
33
- * Get WebSocket handler for Bun.serve integration
51
+ * Get the WebSocket handler for Bun.serve.
52
+ *
53
+ * @returns The WebSocket handler object
34
54
  */
35
55
  getHandler(): import("./types").WebSocketHandlerConfig;
36
56
  }
37
57
  declare module '@gravito/core' {
38
58
  interface GravitoVariables {
39
- /** Ripple WebSocket server */
59
+ /** RippleServer instance available in context */
40
60
  ripple?: RippleServer;
61
+ /** BroadcastManager instance available in context */
62
+ broadcast?: BroadcastManager;
41
63
  }
42
64
  }