silgi 0.0.14 → 0.1.0-beta.10

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 (210) hide show
  1. package/README.md +102 -1
  2. package/dist/_virtual/_rolldown/runtime.mjs +5 -0
  3. package/dist/adapters/_fetch-adapter.d.mts +18 -0
  4. package/dist/adapters/_fetch-adapter.mjs +53 -0
  5. package/dist/adapters/astro.d.mts +15 -0
  6. package/dist/adapters/astro.mjs +31 -0
  7. package/dist/adapters/aws-lambda.d.mts +42 -0
  8. package/dist/adapters/aws-lambda.mjs +92 -0
  9. package/dist/adapters/express.d.mts +16 -0
  10. package/dist/adapters/express.mjs +120 -0
  11. package/dist/adapters/message-port.d.mts +42 -0
  12. package/dist/adapters/message-port.mjs +132 -0
  13. package/dist/adapters/nestjs.d.mts +25 -0
  14. package/dist/adapters/nestjs.mjs +75 -0
  15. package/dist/adapters/nextjs.d.mts +14 -0
  16. package/dist/adapters/nextjs.mjs +29 -0
  17. package/dist/adapters/peer.d.mts +27 -0
  18. package/dist/adapters/peer.mjs +36 -0
  19. package/dist/adapters/remix.d.mts +15 -0
  20. package/dist/adapters/remix.mjs +30 -0
  21. package/dist/adapters/solidstart.d.mts +12 -0
  22. package/dist/adapters/solidstart.mjs +29 -0
  23. package/dist/adapters/sveltekit.d.mts +14 -0
  24. package/dist/adapters/sveltekit.mjs +30 -0
  25. package/dist/broker/index.d.mts +62 -0
  26. package/dist/broker/index.mjs +153 -0
  27. package/dist/broker/nats.d.mts +33 -0
  28. package/dist/broker/nats.mjs +31 -0
  29. package/dist/broker/redis.d.mts +51 -0
  30. package/dist/broker/redis.mjs +92 -0
  31. package/dist/builder.d.mts +55 -0
  32. package/dist/builder.mjs +70 -0
  33. package/dist/callable.d.mts +19 -0
  34. package/dist/callable.mjs +42 -0
  35. package/dist/caller.mjs +90 -0
  36. package/dist/client/adapters/fetch/index.d.mts +19 -0
  37. package/dist/client/adapters/fetch/index.mjs +82 -0
  38. package/dist/client/adapters/ofetch/index.d.mts +57 -0
  39. package/dist/client/adapters/ofetch/index.mjs +100 -0
  40. package/dist/client/adapters/websocket/index.d.mts +20 -0
  41. package/dist/client/adapters/websocket/index.mjs +101 -0
  42. package/dist/client/client.d.mts +37 -0
  43. package/dist/client/client.mjs +80 -0
  44. package/dist/client/consume.d.mts +50 -0
  45. package/dist/client/consume.mjs +66 -0
  46. package/dist/client/dynamic-link.d.mts +16 -0
  47. package/dist/client/dynamic-link.mjs +19 -0
  48. package/dist/client/index.d.mts +6 -0
  49. package/dist/client/index.mjs +5 -0
  50. package/dist/client/interceptor.d.mts +31 -0
  51. package/dist/client/interceptor.mjs +34 -0
  52. package/dist/client/openapi.d.mts +29 -0
  53. package/dist/client/openapi.mjs +89 -0
  54. package/dist/client/plugins/batch.d.mts +26 -0
  55. package/dist/client/plugins/batch.mjs +64 -0
  56. package/dist/client/plugins/circuit-breaker.d.mts +24 -0
  57. package/dist/client/plugins/circuit-breaker.mjs +60 -0
  58. package/dist/client/plugins/csrf.d.mts +13 -0
  59. package/dist/client/plugins/csrf.mjs +20 -0
  60. package/dist/client/plugins/dedupe.d.mts +10 -0
  61. package/dist/client/plugins/dedupe.mjs +28 -0
  62. package/dist/client/plugins/index.d.mts +8 -0
  63. package/dist/client/plugins/index.mjs +8 -0
  64. package/dist/client/plugins/otel.d.mts +12 -0
  65. package/dist/client/plugins/otel.mjs +27 -0
  66. package/dist/client/plugins/retry.d.mts +34 -0
  67. package/dist/client/plugins/retry.mjs +79 -0
  68. package/dist/client/plugins/timeout.d.mts +10 -0
  69. package/dist/client/plugins/timeout.mjs +14 -0
  70. package/dist/client/server.d.mts +16 -0
  71. package/dist/client/server.mjs +62 -0
  72. package/dist/client/types.d.mts +29 -0
  73. package/dist/codec/devalue.d.mts +21 -0
  74. package/dist/codec/devalue.mjs +33 -0
  75. package/dist/codec/msgpack.d.mts +18 -0
  76. package/dist/codec/msgpack.mjs +45 -0
  77. package/dist/codec/sanitize.mjs +38 -0
  78. package/dist/compile.d.mts +46 -0
  79. package/dist/compile.mjs +332 -0
  80. package/dist/contract.d.mts +36 -0
  81. package/dist/contract.mjs +40 -0
  82. package/dist/core/codec.mjs +67 -0
  83. package/dist/core/dispatch.mjs +62 -0
  84. package/dist/core/error.d.mts +104 -0
  85. package/dist/core/error.mjs +139 -0
  86. package/dist/core/handler.d.mts +6 -0
  87. package/dist/core/handler.mjs +148 -0
  88. package/dist/core/input.mjs +49 -0
  89. package/dist/core/iterator.d.mts +17 -0
  90. package/dist/core/iterator.mjs +79 -0
  91. package/dist/core/router-utils.d.mts +25 -0
  92. package/dist/core/router-utils.mjs +98 -0
  93. package/dist/core/schema.d.mts +20 -0
  94. package/dist/core/schema.mjs +33 -0
  95. package/dist/core/serve.d.mts +51 -0
  96. package/dist/core/serve.mjs +76 -0
  97. package/dist/core/sse.d.mts +18 -0
  98. package/dist/core/sse.mjs +98 -0
  99. package/dist/core/storage.d.mts +20 -0
  100. package/dist/core/storage.mjs +61 -0
  101. package/dist/core/task.d.mts +62 -0
  102. package/dist/core/task.mjs +165 -0
  103. package/dist/core/utils.mjs +24 -0
  104. package/dist/index.d.mts +18 -37
  105. package/dist/index.mjs +15 -7
  106. package/dist/integrations/ai/index.d.mts +25 -0
  107. package/dist/integrations/ai/index.mjs +117 -0
  108. package/dist/integrations/better-auth/index.d.mts +61 -0
  109. package/dist/integrations/better-auth/index.mjs +332 -0
  110. package/dist/integrations/drizzle/index.d.mts +27 -0
  111. package/dist/integrations/drizzle/index.mjs +286 -0
  112. package/dist/integrations/hey-api/index.d.mts +2 -0
  113. package/dist/integrations/hey-api/index.mjs +2 -0
  114. package/dist/integrations/hey-api/to-client.d.mts +20 -0
  115. package/dist/integrations/hey-api/to-client.mjs +39 -0
  116. package/dist/integrations/pinia-colada/general-utils.d.mts +13 -0
  117. package/dist/integrations/pinia-colada/general-utils.mjs +9 -0
  118. package/dist/integrations/pinia-colada/index.d.mts +6 -0
  119. package/dist/integrations/pinia-colada/index.mjs +5 -0
  120. package/dist/integrations/pinia-colada/key.d.mts +11 -0
  121. package/dist/integrations/pinia-colada/key.mjs +11 -0
  122. package/dist/integrations/pinia-colada/procedure-utils.d.mts +25 -0
  123. package/dist/integrations/pinia-colada/procedure-utils.mjs +33 -0
  124. package/dist/integrations/pinia-colada/router-utils.d.mts +17 -0
  125. package/dist/integrations/pinia-colada/router-utils.mjs +30 -0
  126. package/dist/integrations/pinia-colada/types.d.mts +25 -0
  127. package/dist/integrations/react/index.d.mts +83 -0
  128. package/dist/integrations/react/index.mjs +196 -0
  129. package/dist/integrations/tanstack-query/index.d.mts +120 -0
  130. package/dist/integrations/tanstack-query/index.mjs +100 -0
  131. package/dist/integrations/tanstack-query/ssr.d.mts +60 -0
  132. package/dist/integrations/tanstack-query/ssr.mjs +102 -0
  133. package/dist/integrations/zod/converter.d.mts +75 -0
  134. package/dist/integrations/zod/converter.mjs +345 -0
  135. package/dist/integrations/zod/index.d.mts +2 -0
  136. package/dist/integrations/zod/index.mjs +2 -0
  137. package/dist/lazy.d.mts +22 -0
  138. package/dist/lazy.mjs +34 -0
  139. package/dist/lifecycle.d.mts +36 -0
  140. package/dist/lifecycle.mjs +46 -0
  141. package/dist/map-input.d.mts +17 -0
  142. package/dist/map-input.mjs +47 -0
  143. package/dist/plugins/analytics.d.mts +236 -0
  144. package/dist/plugins/analytics.mjs +816 -0
  145. package/dist/plugins/batch-server.d.mts +20 -0
  146. package/dist/plugins/batch-server.mjs +91 -0
  147. package/dist/plugins/body-limit.d.mts +19 -0
  148. package/dist/plugins/body-limit.mjs +49 -0
  149. package/dist/plugins/cache.d.mts +170 -0
  150. package/dist/plugins/cache.mjs +212 -0
  151. package/dist/plugins/coerce.d.mts +24 -0
  152. package/dist/plugins/coerce.mjs +70 -0
  153. package/dist/plugins/cookies.d.mts +14 -0
  154. package/dist/plugins/cookies.mjs +48 -0
  155. package/dist/plugins/cors.d.mts +43 -0
  156. package/dist/plugins/cors.mjs +62 -0
  157. package/dist/plugins/file-upload.d.mts +38 -0
  158. package/dist/plugins/file-upload.mjs +102 -0
  159. package/dist/plugins/index.d.mts +14 -0
  160. package/dist/plugins/index.mjs +14 -0
  161. package/dist/plugins/otel.d.mts +35 -0
  162. package/dist/plugins/otel.mjs +40 -0
  163. package/dist/plugins/pino.d.mts +60 -0
  164. package/dist/plugins/pino.mjs +42 -0
  165. package/dist/plugins/pubsub.d.mts +50 -0
  166. package/dist/plugins/pubsub.mjs +53 -0
  167. package/dist/plugins/ratelimit.d.mts +53 -0
  168. package/dist/plugins/ratelimit.mjs +92 -0
  169. package/dist/plugins/signing.d.mts +41 -0
  170. package/dist/plugins/signing.mjs +118 -0
  171. package/dist/plugins/strict-get.d.mts +10 -0
  172. package/dist/plugins/strict-get.mjs +33 -0
  173. package/dist/scalar.d.mts +49 -0
  174. package/dist/scalar.mjs +311 -0
  175. package/dist/silgi.d.mts +144 -0
  176. package/dist/silgi.mjs +156 -0
  177. package/dist/trpc-interop.d.mts +22 -0
  178. package/dist/trpc-interop.mjs +68 -0
  179. package/dist/types.d.mts +108 -0
  180. package/dist/ws.d.mts +88 -0
  181. package/dist/ws.mjs +205 -0
  182. package/lib/dashboard/index.html +123 -0
  183. package/lib/ocache.d.mts +1 -0
  184. package/lib/ocache.mjs +1 -0
  185. package/lib/ofetch.d.mts +1 -0
  186. package/lib/ofetch.mjs +1 -0
  187. package/lib/srvx.d.mts +1 -0
  188. package/lib/srvx.mjs +1 -0
  189. package/lib/unstorage.d.mts +1 -0
  190. package/lib/unstorage.mjs +1 -0
  191. package/package.json +324 -65
  192. package/bin/silgi.mjs +0 -3
  193. package/dist/chunks/generate.mjs +0 -933
  194. package/dist/chunks/init.mjs +0 -21
  195. package/dist/cli/config.d.mts +0 -19
  196. package/dist/cli/config.d.ts +0 -19
  197. package/dist/cli/config.mjs +0 -5
  198. package/dist/cli/index.d.mts +0 -2
  199. package/dist/cli/index.d.ts +0 -2
  200. package/dist/cli/index.mjs +0 -119
  201. package/dist/index.d.ts +0 -37
  202. package/dist/plugins/openapi.d.mts +0 -138
  203. package/dist/plugins/openapi.d.ts +0 -138
  204. package/dist/plugins/openapi.mjs +0 -204
  205. package/dist/plugins/scalar.d.mts +0 -14
  206. package/dist/plugins/scalar.d.ts +0 -14
  207. package/dist/plugins/scalar.mjs +0 -66
  208. package/dist/shared/silgi.BMCYk2cR.mjs +0 -841
  209. package/dist/shared/silgi.D5qK9QOm.d.mts +0 -301
  210. package/dist/shared/silgi.D5qK9QOm.d.ts +0 -301
@@ -0,0 +1,236 @@
1
+ import { FetchHandler } from "../core/handler.mjs";
2
+
3
+ //#region src/plugins/analytics.d.ts
4
+ /**
5
+ * Built-in analytics plugin — zero-dependency monitoring with deep error tracing.
6
+ *
7
+ * - Per-procedure metrics (count, errors, latency percentiles) via ring buffers
8
+ * - Full error log with input, headers, stack trace, custom spans
9
+ * - `trace()` helper for measuring DB queries, API calls, etc.
10
+ * - "Copy for AI" — one-click markdown export of any error
11
+ * - HTTP-level request tracking with procedure grouping (batch support)
12
+ * - Unique request IDs via `x-request-id` response header
13
+ *
14
+ * Dashboard at /analytics, JSON API at /analytics/api, errors at /analytics/errors.
15
+ */
16
+ interface TimeWindow {
17
+ time: number;
18
+ count: number;
19
+ errors: number;
20
+ }
21
+ type SpanKind = 'db' | 'http' | 'cache' | 'queue' | 'email' | 'ai' | 'custom';
22
+ interface TraceSpan {
23
+ name: string;
24
+ kind: SpanKind;
25
+ durationMs: number;
26
+ startOffsetMs?: number;
27
+ detail?: string;
28
+ input?: unknown;
29
+ output?: unknown;
30
+ error?: string;
31
+ /** Structured key-value attributes (db.name, auth.operation, user.id, etc.) */
32
+ attributes?: Record<string, string | number | boolean>;
33
+ }
34
+ interface ErrorEntry {
35
+ id: number;
36
+ /** Links back to the RequestEntry that produced this error. */
37
+ requestId: string;
38
+ timestamp: number;
39
+ procedure: string;
40
+ error: string;
41
+ code: string;
42
+ status: number;
43
+ stack: string;
44
+ input: unknown;
45
+ headers: Record<string, string>;
46
+ durationMs: number;
47
+ spans: TraceSpan[];
48
+ }
49
+ /** A single procedure call within an HTTP request. */
50
+ interface ProcedureCall {
51
+ procedure: string;
52
+ durationMs: number;
53
+ status: number;
54
+ input: unknown;
55
+ output: unknown;
56
+ spans: TraceSpan[];
57
+ error?: string;
58
+ }
59
+ /** An HTTP request containing one or more procedure calls. */
60
+ interface RequestEntry {
61
+ id: number;
62
+ /** Unique request ID (returned in x-request-id response header). */
63
+ requestId: string;
64
+ /** Persistent session ID (from cookie — survives browser restart). */
65
+ sessionId: string;
66
+ timestamp: number;
67
+ durationMs: number;
68
+ method: string;
69
+ path: string;
70
+ ip: string;
71
+ headers: Record<string, string>;
72
+ responseHeaders: Record<string, string>;
73
+ userAgent: string;
74
+ status: number;
75
+ procedures: ProcedureCall[];
76
+ isBatch: boolean;
77
+ }
78
+ /** A background task execution record. */
79
+ interface TaskExecution {
80
+ id: number;
81
+ taskName: string;
82
+ trigger: 'dispatch' | 'cron' | 'http';
83
+ timestamp: number;
84
+ durationMs: number;
85
+ status: 'success' | 'error';
86
+ error?: string;
87
+ input?: unknown;
88
+ output?: unknown;
89
+ spans: TraceSpan[];
90
+ }
91
+ interface AnalyticsOptions {
92
+ /** Latency samples to keep per procedure (default: 1024) */
93
+ bufferSize?: number;
94
+ /** Time-series history in seconds (default: 120) */
95
+ historySeconds?: number;
96
+ /** Max error entries to keep (default: 100) */
97
+ maxErrors?: number;
98
+ /** Max recent request entries to keep (default: 200) */
99
+ maxRequests?: number;
100
+ /** Max task execution entries to keep (default: 200) */
101
+ maxTasks?: number;
102
+ /**
103
+ * Protect dashboard access.
104
+ * - `string` — secret token checked against `Authorization: Bearer <token>` header or `?token=` query param
105
+ * - `(req: Request) => boolean | Promise<boolean>` — custom auth function
106
+ * - `undefined` — no auth (open access, NOT recommended in production)
107
+ */
108
+ auth?: string | ((req: Request) => boolean | Promise<boolean>);
109
+ /** Interval in ms between storage flushes (default: 5000) */
110
+ flushInterval?: number;
111
+ }
112
+ interface ProcedureSnapshot {
113
+ count: number;
114
+ errors: number;
115
+ errorRate: number;
116
+ latency: {
117
+ avg: number;
118
+ p50: number;
119
+ p95: number;
120
+ p99: number;
121
+ };
122
+ lastError: string | null;
123
+ lastErrorTime: number | null;
124
+ }
125
+ interface TaskSnapshot {
126
+ totalRuns: number;
127
+ totalErrors: number;
128
+ tasks: Record<string, {
129
+ runs: number;
130
+ errors: number;
131
+ avgDurationMs: number;
132
+ lastRun: number | null;
133
+ }>;
134
+ }
135
+ interface AnalyticsSnapshot {
136
+ uptime: number;
137
+ totalRequests: number;
138
+ totalErrors: number;
139
+ errorRate: number;
140
+ requestsPerSecond: number;
141
+ avgLatency: number;
142
+ procedures: Record<string, ProcedureSnapshot>;
143
+ timeSeries: TimeWindow[];
144
+ tasks: TaskSnapshot;
145
+ }
146
+ declare class RequestTrace {
147
+ spans: TraceSpan[];
148
+ /** Procedure-level input — set via `setProcedureInput()` or `trace(..., { procedure: { input } })` */
149
+ procedureInput: unknown;
150
+ /** Procedure-level output — set via `setProcedureOutput()` or `trace(..., { procedure: { output } })` */
151
+ procedureOutput: unknown;
152
+ /** @internal Start time — used by integrations (drizzle etc.) for span offset calculation */
153
+ readonly t0: number;
154
+ trace<T>(name: string, fn: () => T | Promise<T>, opts?: {
155
+ kind?: SpanKind;
156
+ detail?: string;
157
+ input?: unknown;
158
+ output?: unknown | ((result: T) => unknown);
159
+ procedure?: {
160
+ input?: unknown;
161
+ output?: unknown | ((result: T) => unknown);
162
+ };
163
+ }): Promise<T>;
164
+ totalByKind(kind: SpanKind): number;
165
+ }
166
+ /**
167
+ * Standalone trace function — works with or without analytics.
168
+ *
169
+ * ```ts
170
+ * import { trace } from 'silgi/analytics'
171
+ *
172
+ * const listUsers = s.$resolve(async ({ ctx }) => {
173
+ * return await trace(ctx, 'db.users.findMany', () => db.users.findMany())
174
+ * // or with explicit kind:
175
+ * return await trace(ctx, 'findUsers', () => db.users.findMany(), { kind: 'db', detail: 'SELECT * FROM users' })
176
+ * })
177
+ * ```
178
+ */
179
+ declare function trace<T>(ctx: Record<string, unknown>, name: string, fn: () => T | Promise<T>, opts?: {
180
+ kind?: SpanKind;
181
+ detail?: string;
182
+ input?: unknown;
183
+ output?: unknown | ((result: T) => unknown);
184
+ procedure?: {
185
+ input?: unknown;
186
+ output?: unknown | ((result: T) => unknown);
187
+ };
188
+ }): Promise<T>;
189
+ declare class AnalyticsCollector {
190
+ #private;
191
+ constructor(options?: AnalyticsOptions);
192
+ record(path: string, durationMs: number): void;
193
+ recordError(path: string, durationMs: number, errorMsg: string): void;
194
+ recordDetailedError(entry: Omit<ErrorEntry, 'id'>): void;
195
+ recordDetailedRequest(entry: Omit<RequestEntry, 'id'>): void;
196
+ recordTask(entry: Omit<TaskExecution, 'id'>): void;
197
+ getTaskExecutions(): Promise<TaskExecution[]>;
198
+ getErrors(): Promise<ErrorEntry[]>;
199
+ getRequests(): Promise<RequestEntry[]>;
200
+ dispose(): Promise<void>;
201
+ toJSON(): AnalyticsSnapshot;
202
+ }
203
+ declare class RequestAccumulator {
204
+ #private;
205
+ readonly requestId: string;
206
+ readonly sessionId: string;
207
+ /** True if a new session cookie needs to be set. */
208
+ readonly isNewSession: boolean;
209
+ t0: number;
210
+ constructor(request: Request, collector: AnalyticsCollector);
211
+ addProcedure(call: ProcedureCall): void;
212
+ /** Get Set-Cookie header value (only if new session). */
213
+ getSessionCookie(): string | null;
214
+ /** Flush with response headers extracted from the actual Response object. */
215
+ flushWithResponse(res: Response): void;
216
+ /** Whether any procedures have been recorded. */
217
+ get hasProcedures(): boolean;
218
+ }
219
+ declare function errorToMarkdown(e: ErrorEntry): string;
220
+ declare function requestToMarkdown(r: RequestEntry): string;
221
+ declare function analyticsHTML(): string;
222
+ declare function checkAnalyticsAuth(request: Request, auth: string | ((req: Request) => boolean | Promise<boolean>)): boolean | Promise<boolean>;
223
+ declare function sanitizeHeaders(headers: Headers): Record<string, string>;
224
+ /** Return auth-failure response for analytics routes. */
225
+ declare function analyticsAuthResponse(pathname: string): Response;
226
+ /** Serve analytics dashboard and API routes. */
227
+ declare function serveAnalyticsRoute(pathname: string, collector: AnalyticsCollector, dashboardHtml: string | undefined): Promise<Response>;
228
+ /**
229
+ * Wrap a fetch handler with analytics collection.
230
+ * Intercepts analytics dashboard routes and instruments every request.
231
+ */
232
+ declare function wrapWithAnalytics(handler: FetchHandler, options?: AnalyticsOptions): FetchHandler;
233
+ /** WeakMap to pass analytics trace to context without coupling handler to analytics. */
234
+ declare const analyticsTraceMap: WeakMap<Request, RequestTrace>;
235
+ //#endregion
236
+ export { AnalyticsCollector, AnalyticsOptions, AnalyticsSnapshot, ErrorEntry, ProcedureCall, ProcedureSnapshot, RequestAccumulator, RequestEntry, RequestTrace, SpanKind, TaskExecution, TaskSnapshot, TraceSpan, analyticsAuthResponse, analyticsHTML, analyticsTraceMap, checkAnalyticsAuth, errorToMarkdown, requestToMarkdown, sanitizeHeaders, serveAnalyticsRoute, trace, wrapWithAnalytics };