@syncular/server-hono 0.0.1-60

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 (76) hide show
  1. package/dist/api-key-auth.d.ts +49 -0
  2. package/dist/api-key-auth.d.ts.map +1 -0
  3. package/dist/api-key-auth.js +110 -0
  4. package/dist/api-key-auth.js.map +1 -0
  5. package/dist/blobs.d.ts +69 -0
  6. package/dist/blobs.d.ts.map +1 -0
  7. package/dist/blobs.js +383 -0
  8. package/dist/blobs.js.map +1 -0
  9. package/dist/console/index.d.ts +8 -0
  10. package/dist/console/index.d.ts.map +1 -0
  11. package/dist/console/index.js +7 -0
  12. package/dist/console/index.js.map +1 -0
  13. package/dist/console/routes.d.ts +106 -0
  14. package/dist/console/routes.d.ts.map +1 -0
  15. package/dist/console/routes.js +1612 -0
  16. package/dist/console/routes.js.map +1 -0
  17. package/dist/console/schemas.d.ts +308 -0
  18. package/dist/console/schemas.d.ts.map +1 -0
  19. package/dist/console/schemas.js +201 -0
  20. package/dist/console/schemas.js.map +1 -0
  21. package/dist/create-server.d.ts +78 -0
  22. package/dist/create-server.d.ts.map +1 -0
  23. package/dist/create-server.js +99 -0
  24. package/dist/create-server.js.map +1 -0
  25. package/dist/index.d.ts +16 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +25 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/openapi.d.ts +45 -0
  30. package/dist/openapi.d.ts.map +1 -0
  31. package/dist/openapi.js +59 -0
  32. package/dist/openapi.js.map +1 -0
  33. package/dist/proxy/connection-manager.d.ts +78 -0
  34. package/dist/proxy/connection-manager.d.ts.map +1 -0
  35. package/dist/proxy/connection-manager.js +251 -0
  36. package/dist/proxy/connection-manager.js.map +1 -0
  37. package/dist/proxy/index.d.ts +8 -0
  38. package/dist/proxy/index.d.ts.map +1 -0
  39. package/dist/proxy/index.js +8 -0
  40. package/dist/proxy/index.js.map +1 -0
  41. package/dist/proxy/routes.d.ts +74 -0
  42. package/dist/proxy/routes.d.ts.map +1 -0
  43. package/dist/proxy/routes.js +147 -0
  44. package/dist/proxy/routes.js.map +1 -0
  45. package/dist/rate-limit.d.ts +101 -0
  46. package/dist/rate-limit.d.ts.map +1 -0
  47. package/dist/rate-limit.js +186 -0
  48. package/dist/rate-limit.js.map +1 -0
  49. package/dist/routes.d.ts +126 -0
  50. package/dist/routes.d.ts.map +1 -0
  51. package/dist/routes.js +788 -0
  52. package/dist/routes.js.map +1 -0
  53. package/dist/ws.d.ts +230 -0
  54. package/dist/ws.d.ts.map +1 -0
  55. package/dist/ws.js +601 -0
  56. package/dist/ws.js.map +1 -0
  57. package/package.json +73 -0
  58. package/src/__tests__/create-server.test.ts +187 -0
  59. package/src/__tests__/pull-chunk-storage.test.ts +189 -0
  60. package/src/__tests__/rate-limit.test.ts +78 -0
  61. package/src/__tests__/realtime-bridge.test.ts +131 -0
  62. package/src/__tests__/ws-connection-manager.test.ts +176 -0
  63. package/src/api-key-auth.ts +179 -0
  64. package/src/blobs.ts +534 -0
  65. package/src/console/index.ts +17 -0
  66. package/src/console/routes.ts +2155 -0
  67. package/src/console/schemas.ts +299 -0
  68. package/src/create-server.ts +180 -0
  69. package/src/index.ts +42 -0
  70. package/src/openapi.ts +74 -0
  71. package/src/proxy/connection-manager.ts +340 -0
  72. package/src/proxy/index.ts +8 -0
  73. package/src/proxy/routes.ts +223 -0
  74. package/src/rate-limit.ts +321 -0
  75. package/src/routes.ts +1186 -0
  76. package/src/ws.ts +789 -0
@@ -0,0 +1,321 @@
1
+ /**
2
+ * @syncular/server-hono - Rate limiting middleware for sync endpoints
3
+ *
4
+ * Provides per-user rate limiting to prevent DoS attacks and excessive
5
+ * server load from misbehaving clients.
6
+ */
7
+
8
+ import { logSyncEvent } from '@syncular/core';
9
+ import type { Context, MiddlewareHandler } from 'hono';
10
+
11
+ /**
12
+ * Rate limit configuration
13
+ */
14
+ export interface RateLimitConfig {
15
+ /**
16
+ * Maximum requests per window (default: 60)
17
+ */
18
+ maxRequests: number;
19
+
20
+ /**
21
+ * Time window in milliseconds (default: 60000 = 1 minute)
22
+ */
23
+ windowMs: number;
24
+
25
+ /**
26
+ * Function to extract the rate limit key from a request.
27
+ * Typically returns userId, deviceId, or IP address.
28
+ * Return null to skip rate limiting for this request.
29
+ */
30
+ keyGenerator: (c: Context) => string | null | Promise<string | null>;
31
+
32
+ /**
33
+ * Whether to include rate limit headers in responses (default: true)
34
+ */
35
+ includeHeaders?: boolean;
36
+
37
+ /**
38
+ * Custom handler for rate-limited requests (optional)
39
+ * If not provided, returns a 429 JSON response
40
+ */
41
+ onRateLimited?: (
42
+ c: Context,
43
+ retryAfterMs: number
44
+ ) => Response | Promise<Response>;
45
+
46
+ /**
47
+ * Whether to skip rate limiting in test environments (default: false)
48
+ */
49
+ skipInTest?: boolean;
50
+ }
51
+
52
+ /**
53
+ * Default rate limit configuration
54
+ */
55
+ const DEFAULT_RATE_LIMIT_CONFIG: Omit<RateLimitConfig, 'keyGenerator'> = {
56
+ maxRequests: 60,
57
+ windowMs: 60_000,
58
+ includeHeaders: true,
59
+ skipInTest: false,
60
+ };
61
+
62
+ /**
63
+ * Rate limit entry for tracking request counts
64
+ */
65
+ interface RateLimitEntry {
66
+ /** Request count in current window */
67
+ count: number;
68
+ /** Window start timestamp */
69
+ windowStart: number;
70
+ }
71
+
72
+ /**
73
+ * In-memory rate limiter store
74
+ *
75
+ * Note: This is suitable for single-instance deployments.
76
+ * For distributed deployments, use Redis or similar.
77
+ */
78
+ class RateLimitStore {
79
+ private entries = new Map<string, RateLimitEntry>();
80
+ private cleanupInterval: ReturnType<typeof setInterval> | null = null;
81
+
82
+ constructor(private windowMs: number) {
83
+ // Clean up expired entries periodically
84
+ this.cleanupInterval = setInterval(
85
+ () => this.cleanup(),
86
+ Math.max(windowMs, 60_000)
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Check and increment the rate limit counter for a key.
92
+ *
93
+ * @param key - Rate limit key (e.g., userId)
94
+ * @param maxRequests - Maximum requests allowed
95
+ * @returns Rate limit check result
96
+ */
97
+ check(
98
+ key: string,
99
+ maxRequests: number
100
+ ): {
101
+ allowed: boolean;
102
+ current: number;
103
+ remaining: number;
104
+ resetAt: number;
105
+ } {
106
+ const now = Date.now();
107
+ let entry = this.entries.get(key);
108
+
109
+ // Check if window has expired
110
+ if (!entry || now - entry.windowStart >= this.windowMs) {
111
+ entry = { count: 0, windowStart: now };
112
+ this.entries.set(key, entry);
113
+ }
114
+
115
+ const resetAt = entry.windowStart + this.windowMs;
116
+ const allowed = entry.count < maxRequests;
117
+
118
+ if (allowed) {
119
+ entry.count++;
120
+ }
121
+
122
+ return {
123
+ allowed,
124
+ current: entry.count,
125
+ remaining: Math.max(0, maxRequests - entry.count),
126
+ resetAt,
127
+ };
128
+ }
129
+
130
+ /**
131
+ * Clean up expired entries
132
+ */
133
+ cleanup(): void {
134
+ const now = Date.now();
135
+ for (const [key, entry] of this.entries) {
136
+ if (now - entry.windowStart >= this.windowMs) {
137
+ this.entries.delete(key);
138
+ }
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Stop the cleanup interval
144
+ */
145
+ stop(): void {
146
+ if (this.cleanupInterval) {
147
+ clearInterval(this.cleanupInterval);
148
+ this.cleanupInterval = null;
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Clear all entries (for testing)
154
+ */
155
+ clear(): void {
156
+ this.entries.clear();
157
+ }
158
+
159
+ /**
160
+ * Get current entry count (for monitoring)
161
+ */
162
+ get size(): number {
163
+ return this.entries.size;
164
+ }
165
+ }
166
+
167
+ // Track created stores so tests can reset state deterministically.
168
+ const activeStores = new Set<RateLimitStore>();
169
+
170
+ /**
171
+ * Reset the global store (for testing)
172
+ */
173
+ export function resetRateLimitStore(): void {
174
+ for (const store of activeStores) {
175
+ store.stop();
176
+ }
177
+ activeStores.clear();
178
+ }
179
+
180
+ /**
181
+ * Create a rate limiting middleware for Hono.
182
+ *
183
+ * @example
184
+ * ```typescript
185
+ * import { createRateLimiter } from '@syncular/server-hono';
186
+ *
187
+ * const rateLimiter = createRateLimiter({
188
+ * maxRequests: 60,
189
+ * windowMs: 60_000,
190
+ * keyGenerator: async (c) => {
191
+ * const auth = await authenticate(c);
192
+ * return auth?.userId ?? null;
193
+ * },
194
+ * });
195
+ *
196
+ * app.use('/sync/*', rateLimiter);
197
+ * ```
198
+ */
199
+ export function createRateLimiter(
200
+ config: Partial<RateLimitConfig> & Pick<RateLimitConfig, 'keyGenerator'>
201
+ ): MiddlewareHandler {
202
+ const {
203
+ maxRequests = DEFAULT_RATE_LIMIT_CONFIG.maxRequests,
204
+ windowMs = DEFAULT_RATE_LIMIT_CONFIG.windowMs,
205
+ keyGenerator,
206
+ includeHeaders = DEFAULT_RATE_LIMIT_CONFIG.includeHeaders,
207
+ onRateLimited,
208
+ skipInTest = DEFAULT_RATE_LIMIT_CONFIG.skipInTest,
209
+ } = config;
210
+
211
+ const store = new RateLimitStore(windowMs);
212
+ activeStores.add(store);
213
+
214
+ return async (c, next) => {
215
+ // Skip in test environment if configured
216
+ if (skipInTest && process.env.NODE_ENV === 'test') {
217
+ return next();
218
+ }
219
+
220
+ // Get the rate limit key
221
+ const key = await keyGenerator(c);
222
+ if (key === null) {
223
+ // Skip rate limiting for this request
224
+ return next();
225
+ }
226
+
227
+ const result = store.check(key, maxRequests);
228
+
229
+ // Add rate limit headers if configured
230
+ if (includeHeaders) {
231
+ c.header('X-RateLimit-Limit', String(maxRequests));
232
+ c.header('X-RateLimit-Remaining', String(result.remaining));
233
+ c.header('X-RateLimit-Reset', String(Math.ceil(result.resetAt / 1000)));
234
+ }
235
+
236
+ if (!result.allowed) {
237
+ const retryAfterMs = result.resetAt - Date.now();
238
+ const retryAfterSec = Math.ceil(retryAfterMs / 1000);
239
+
240
+ // Log rate limit event
241
+ logSyncEvent({
242
+ event: 'sync.rate_limit',
243
+ key,
244
+ current: result.current,
245
+ maxRequests,
246
+ retryAfterMs,
247
+ });
248
+
249
+ // Add Retry-After header
250
+ c.header('Retry-After', String(retryAfterSec));
251
+
252
+ // Use custom handler or default response
253
+ if (onRateLimited) {
254
+ return onRateLimited(c, retryAfterMs);
255
+ }
256
+
257
+ return c.json(
258
+ {
259
+ error: 'RATE_LIMITED',
260
+ message: 'Too many requests. Please try again later.',
261
+ retryAfterMs,
262
+ retryAfterSec,
263
+ },
264
+ 429
265
+ );
266
+ }
267
+
268
+ return next();
269
+ };
270
+ }
271
+
272
+ /**
273
+ * Create a rate limiter that uses userId from auth context.
274
+ *
275
+ * This is a convenience function for the common case of rate limiting
276
+ * by authenticated user.
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * const syncRoutes = createSyncRoutes({
281
+ * db,
282
+ * handlers: [tasksHandler],
283
+ * authenticate,
284
+ * sync: {
285
+ * rateLimit: {
286
+ * pull: { maxRequests: 120, windowMs: 60_000 },
287
+ * push: { maxRequests: 60, windowMs: 60_000 },
288
+ * },
289
+ * },
290
+ * });
291
+ * ```
292
+ */
293
+ export interface SyncRateLimitConfig {
294
+ /**
295
+ * Rate limit config for pull requests.
296
+ * Set to false to disable rate limiting for pulls.
297
+ */
298
+ pull?: Omit<RateLimitConfig, 'keyGenerator'> | false;
299
+
300
+ /**
301
+ * Rate limit config for push requests.
302
+ * Set to false to disable rate limiting for pushes.
303
+ */
304
+ push?: Omit<RateLimitConfig, 'keyGenerator'> | false;
305
+ }
306
+
307
+ /**
308
+ * Default sync rate limit configuration
309
+ */
310
+ export const DEFAULT_SYNC_RATE_LIMITS: SyncRateLimitConfig = {
311
+ pull: {
312
+ maxRequests: 120, // 2 requests per second average
313
+ windowMs: 60_000,
314
+ includeHeaders: true,
315
+ },
316
+ push: {
317
+ maxRequests: 60, // 1 request per second average
318
+ windowMs: 60_000,
319
+ includeHeaders: true,
320
+ },
321
+ };