@getvision/server 0.2.0-b49d8db-develop → 0.2.1-d0f3a53-develop
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/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/service.ts +54 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @getvision/server
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- b49d8db: Added per-endpoint rate limiting with hono-rate-limiter: configure ratelimit in EndpointConfig with requests, window, and optional store for distributed caching. Includes docs and example.
|
|
8
|
+
|
|
3
9
|
## 0.1.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/package.json
CHANGED
package/src/service.ts
CHANGED
|
@@ -32,6 +32,44 @@ function getClientKey(c: Context, method: string, path: string): string {
|
|
|
32
32
|
return `${ip || ua}:${method}:${path}`
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Create a minimal Hono-like Context for event handlers
|
|
37
|
+
* so service-level middleware can populate c.set(...)
|
|
38
|
+
*/
|
|
39
|
+
function createEventContext<E extends Env = any, I extends Input = {}>(): Context<E, any, I> {
|
|
40
|
+
const store: Record<string, any> = {}
|
|
41
|
+
const fake: Partial<Context<E, any, I>> = {
|
|
42
|
+
get: (key: string) => store[key],
|
|
43
|
+
set: (key: string, value: any) => { store[key] = value },
|
|
44
|
+
req: {
|
|
45
|
+
header: () => undefined,
|
|
46
|
+
param: () => ({}),
|
|
47
|
+
query: () => ({}),
|
|
48
|
+
json: async () => ({}),
|
|
49
|
+
raw: {} as any,
|
|
50
|
+
} as any,
|
|
51
|
+
}
|
|
52
|
+
return fake as Context<E, any, I>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Run Hono middleware chain on a context
|
|
57
|
+
*/
|
|
58
|
+
async function runMiddlewareChain<E extends Env, I extends Input>(
|
|
59
|
+
middlewares: MiddlewareHandler<E, string, any, any>[],
|
|
60
|
+
c: Context<E, any, I>
|
|
61
|
+
): Promise<void> {
|
|
62
|
+
let index = -1
|
|
63
|
+
const dispatch = async (i: number): Promise<void> => {
|
|
64
|
+
if (i <= index) throw new Error('next() called multiple times')
|
|
65
|
+
index = i
|
|
66
|
+
const mw = middlewares[i]
|
|
67
|
+
if (!mw) return
|
|
68
|
+
await mw(c, () => dispatch(i + 1))
|
|
69
|
+
}
|
|
70
|
+
await dispatch(0)
|
|
71
|
+
}
|
|
72
|
+
|
|
35
73
|
/**
|
|
36
74
|
* Event schema map - accumulates event types as they're registered
|
|
37
75
|
*/
|
|
@@ -202,7 +240,7 @@ export class ServiceBuilder<
|
|
|
202
240
|
description?: string
|
|
203
241
|
icon?: string
|
|
204
242
|
tags?: string[]
|
|
205
|
-
handler: (event: T) => Promise<void>
|
|
243
|
+
handler: (event: T, c: Context<E, any, I>) => Promise<void>
|
|
206
244
|
}
|
|
207
245
|
): ServiceBuilder<TEvents & { [key in K]: T }, E, I> {
|
|
208
246
|
const { schema, handler, description, icon, tags } = config
|
|
@@ -210,16 +248,27 @@ export class ServiceBuilder<
|
|
|
210
248
|
// Store schema for type inference
|
|
211
249
|
this.eventSchemas[eventName] = schema
|
|
212
250
|
|
|
213
|
-
//
|
|
251
|
+
// Wrap handler to provide Hono-like context with middleware support
|
|
252
|
+
const wrappedHandler = async (data: T) => {
|
|
253
|
+
const ctx = createEventContext<E, I>()
|
|
254
|
+
// Run service-level middleware to populate ctx (e.g., c.set('db', db))
|
|
255
|
+
if (this.globalMiddleware.length > 0) {
|
|
256
|
+
await runMiddlewareChain(this.globalMiddleware, ctx)
|
|
257
|
+
}
|
|
258
|
+
// Call original handler with event data and context
|
|
259
|
+
await handler(data, ctx)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Register wrapped handler in event registry (for metadata/stats)
|
|
214
263
|
eventRegistry.registerEvent(
|
|
215
264
|
eventName,
|
|
216
265
|
schema,
|
|
217
|
-
|
|
266
|
+
wrappedHandler,
|
|
218
267
|
{ description, icon, tags }
|
|
219
268
|
)
|
|
220
269
|
|
|
221
|
-
// Register handler in event bus
|
|
222
|
-
this.eventBus.registerHandler(eventName,
|
|
270
|
+
// Register wrapped handler in event bus
|
|
271
|
+
this.eventBus.registerHandler(eventName, wrappedHandler)
|
|
223
272
|
|
|
224
273
|
// Store for later reference
|
|
225
274
|
this.eventHandlers.set(eventName, config)
|