silgi 0.51.2 → 0.51.4

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.
@@ -1,4 +1,3 @@
1
- import { analyticsTraceMap } from "../core/trace-map.mjs";
2
1
  import { createFetchHandler, wrapHandler } from "../core/handler.mjs";
3
2
  //#region src/adapters/_fetch-adapter.ts
4
3
  /**
@@ -10,29 +9,13 @@ import { createFetchHandler, wrapHandler } from "../core/handler.mjs";
10
9
  * This module eliminates the duplication. Each adapter file becomes a thin
11
10
  * wrapper that extracts the framework-specific Request and calls this factory.
12
11
  */
13
- /** Strip prefix from request URL and create a rewritten Request. */
14
- function rewriteRequest(request, prefix) {
15
- const url = new URL(request.url);
16
- let pathname = url.pathname;
17
- if (pathname.startsWith(prefix)) {
18
- pathname = pathname.slice(prefix.length);
19
- if (!pathname.startsWith("/")) pathname = "/" + pathname;
20
- }
21
- return new Request(new URL(pathname + url.search, url.origin), request);
22
- }
23
12
  /**
24
13
  * Create a fetch-passthrough adapter that strips a prefix and delegates to handler.
25
14
  * Used by adapters that receive a standard Request (Next.js, Astro, Remix).
26
15
  */
27
16
  function createFetchAdapter(router, options, defaultPrefix) {
28
17
  const prefix = options.prefix ?? defaultPrefix;
29
- const inner = createFetchHandler(router, options.context ?? (() => ({})));
30
- return wrapHandler((request) => {
31
- const rewritten = rewriteRequest(request, prefix);
32
- const trace = analyticsTraceMap.get(request);
33
- if (trace) analyticsTraceMap.set(rewritten, trace);
34
- return inner(rewritten);
35
- }, router, options, prefix);
18
+ return wrapHandler(createFetchHandler(router, options.context ?? (() => ({})), void 0, prefix), router, options, prefix);
36
19
  }
37
20
  /**
38
21
  * Create a fetch-passthrough adapter for frameworks that pass an event object
@@ -42,19 +25,11 @@ function createFetchAdapter(router, options, defaultPrefix) {
42
25
  function createEventFetchAdapter(router, options, defaultPrefix, extractRequest) {
43
26
  const prefix = options.prefix ?? defaultPrefix;
44
27
  const requestEventMap = /* @__PURE__ */ new WeakMap();
45
- const inner = createFetchHandler(router, (_req) => {
28
+ const handler = wrapHandler(createFetchHandler(router, (_req) => {
46
29
  const eventRef = requestEventMap.get(_req);
47
30
  if (options.context && eventRef) return options.context(eventRef);
48
31
  return {};
49
- });
50
- const handler = wrapHandler((request) => {
51
- const rewritten = rewriteRequest(request, prefix);
52
- const eventRef = requestEventMap.get(request);
53
- if (eventRef) requestEventMap.set(rewritten, eventRef);
54
- const trace = analyticsTraceMap.get(request);
55
- if (trace) analyticsTraceMap.set(rewritten, trace);
56
- return inner(rewritten);
57
- }, router, options, prefix);
32
+ }, void 0, prefix), router, options, prefix);
58
33
  return (event) => {
59
34
  const request = extractRequest(event);
60
35
  requestEventMap.set(request, event);
@@ -7,6 +7,8 @@ type FetchHandler = (request: Request) => Response | Promise<Response>;
7
7
  interface WrapHandlerOptions {
8
8
  analytics?: boolean | AnalyticsOptions;
9
9
  scalar?: boolean | ScalarOptions;
10
+ /** URL path prefix for the handler (e.g. "/api"). Requests not matching this prefix return 404. */
11
+ basePath?: string;
10
12
  }
11
13
  //#endregion
12
14
  export { FetchHandler, WrapHandlerOptions };
@@ -86,7 +86,7 @@ function wrapHandler(handler, router, options, prefix) {
86
86
  if (options.analytics) {
87
87
  const { wrapWithAnalytics } = await import("../plugins/analytics.mjs");
88
88
  const analyticsOpts = typeof options.analytics === "object" ? options.analytics : {};
89
- h = wrapWithAnalytics(h, analyticsOpts);
89
+ h = wrapWithAnalytics(h, router, analyticsOpts);
90
90
  }
91
91
  wrapped = h;
92
92
  }
@@ -96,12 +96,13 @@ function wrapHandler(handler, router, options, prefix) {
96
96
  return initPromise.then(() => wrapped(request));
97
97
  };
98
98
  }
99
- function createFetchHandler(routerDef, contextFactory, hooks) {
99
+ function createFetchHandler(routerDef, contextFactory, hooks, prefix) {
100
100
  let compiledRouter = routerCache.get(routerDef);
101
101
  if (!compiledRouter) {
102
102
  compiledRouter = compileRouter(routerDef);
103
103
  routerCache.set(routerDef, compiledRouter);
104
104
  }
105
+ const prefixLen = prefix ? prefix.length : 0;
105
106
  const jsonHeaders = { "content-type": "application/json" };
106
107
  const notFoundBody = JSON.stringify({
107
108
  code: "NOT_FOUND",
@@ -117,7 +118,14 @@ function createFetchHandler(routerDef, contextFactory, hooks) {
117
118
  }
118
119
  return async function handleRequest(request) {
119
120
  const url = request.url;
120
- const fullPath = parseUrlPath(url);
121
+ let fullPath = parseUrlPath(url);
122
+ if (prefix) {
123
+ if (!fullPath.startsWith(prefix)) return new Response(notFoundBody, {
124
+ status: 404,
125
+ headers: jsonHeaders
126
+ });
127
+ fullPath = fullPath.slice(prefixLen) || "/";
128
+ }
121
129
  const pathname = fullPath.length > 1 ? fullPath.slice(1) : "";
122
130
  const qMark = url.indexOf("?", url.indexOf("/", url.indexOf("//") + 2));
123
131
  const match = compiledRouter(request.method, fullPath);
@@ -20,6 +20,8 @@ interface SilgiServer {
20
20
  close(forceCloseConnections?: boolean): Promise<void>;
21
21
  }
22
22
  interface ServeOptions {
23
+ /** URL path prefix (e.g. "/api"). Only requests matching this prefix are handled; others return 404. */
24
+ basePath?: string;
23
25
  port?: number;
24
26
  hostname?: string;
25
27
  /** Enable Scalar API Reference UI at /api/reference and /api/openapi.json */
@@ -1,3 +1,4 @@
1
+ import { normalizePrefix } from "./url.mjs";
1
2
  import { createFetchHandler, wrapHandler } from "./handler.mjs";
2
3
  import { _createWSHooks } from "../ws.mjs";
3
4
  import { serve } from "srvx";
@@ -16,7 +17,8 @@ function routerHasSubscription(def) {
16
17
  async function createServeHandler(routerDef, contextFactory, hooks, options) {
17
18
  const port = options?.port ?? 3e3;
18
19
  const hostname = options?.hostname ?? "127.0.0.1";
19
- const httpHandler = wrapHandler(createFetchHandler(routerDef, contextFactory, hooks), routerDef, options);
20
+ const prefix = options?.basePath ? normalizePrefix(options.basePath) : void 0;
21
+ const httpHandler = wrapHandler(createFetchHandler(routerDef, contextFactory, hooks, prefix), routerDef, options, prefix);
20
22
  const shutdownOpt = options?.gracefulShutdown ?? true;
21
23
  let gracefulShutdown;
22
24
  if (typeof shutdownOpt === "object") gracefulShutdown = {
package/dist/core/url.mjs CHANGED
@@ -24,5 +24,11 @@ function parseUrlPathname(url) {
24
24
  const fullPath = parseUrlPath(url);
25
25
  return fullPath.length > 1 ? fullPath.slice(1) : "";
26
26
  }
27
+ /** Normalize a basePath: ensure leading slash, strip trailing slash. */
28
+ function normalizePrefix(basePath) {
29
+ let p = basePath.startsWith("/") ? basePath : "/" + basePath;
30
+ if (p.endsWith("/")) p = p.slice(0, -1);
31
+ return p;
32
+ }
27
33
  //#endregion
28
- export { parseUrlPath, parseUrlPathname };
34
+ export { normalizePrefix, parseUrlPath, parseUrlPathname };
@@ -151,7 +151,7 @@ function tracing(config) {
151
151
  try {
152
152
  const request = ctx.request;
153
153
  if (!request) return;
154
- const silgiCtx = request.__silgiCtx;
154
+ const silgiCtx = request.__silgiCtx ?? getCtx();
155
155
  if (!silgiCtx) return;
156
156
  const reqTrace = silgiCtx.__analyticsTrace;
157
157
  if (!reqTrace) return;
@@ -16,6 +16,11 @@ declare class AnalyticsCollector {
16
16
  /** Cost tracker */
17
17
  costTracker: CostTracker;
18
18
  constructor(options?: AnalyticsOptions);
19
+ /** Set procedure schemas extracted from router definition. */
20
+ setProcedureSchemas(schemas: Map<string, {
21
+ input?: Record<string, unknown>;
22
+ output?: Record<string, unknown>;
23
+ }>): void;
19
24
  /** Check if a path is server-side ignored (from config). */
20
25
  isIgnored(pathname: string): boolean;
21
26
  /** Check if a path is hidden in the dashboard (from runtime API). */
@@ -38,6 +38,8 @@ var AnalyticsCollector = class {
38
38
  #ignorePaths;
39
39
  /** Client-side hide — from dashboard, filters display only */
40
40
  #hiddenPaths = /* @__PURE__ */ new Set();
41
+ /** Procedure input/output JSON schemas (set once from router) */
42
+ #procedureSchemas = null;
41
43
  /** SSE hub for real-time streaming */
42
44
  sseHub;
43
45
  /** Multi-tier time-series aggregation */
@@ -71,6 +73,10 @@ var AnalyticsCollector = class {
71
73
  for (const p of paths) this.#hiddenPaths.add(p);
72
74
  });
73
75
  }
76
+ /** Set procedure schemas extracted from router definition. */
77
+ setProcedureSchemas(schemas) {
78
+ this.#procedureSchemas = schemas;
79
+ }
74
80
  /** Check if a path is server-side ignored (from config). */
75
81
  isIgnored(pathname) {
76
82
  if (this.#ignorePaths.size === 0) return false;
@@ -222,7 +228,7 @@ var AnalyticsCollector = class {
222
228
  let totalLatencyCount = 0;
223
229
  for (const [path, entry] of this.#procedures) {
224
230
  const avg = entry.latencies.avg();
225
- procedures[path] = {
231
+ const snapshot = {
226
232
  count: entry.count,
227
233
  errors: entry.errors,
228
234
  errorRate: entry.count > 0 ? round(entry.errors / entry.count * 100) : 0,
@@ -235,6 +241,10 @@ var AnalyticsCollector = class {
235
241
  lastError: entry.lastError,
236
242
  lastErrorTime: entry.lastErrorTime || null
237
243
  };
244
+ const schemaInfo = this.#procedureSchemas?.get(path);
245
+ if (schemaInfo?.input) snapshot.inputSchema = schemaInfo.input;
246
+ if (schemaInfo?.output) snapshot.outputSchema = schemaInfo.output;
247
+ procedures[path] = snapshot;
238
248
  totalLatencySum += avg * entry.latencies.count;
239
249
  totalLatencyCount += entry.latencies.count;
240
250
  }
@@ -119,6 +119,8 @@ interface ProcedureSnapshot {
119
119
  };
120
120
  lastError: string | null;
121
121
  lastErrorTime: number | null;
122
+ inputSchema?: Record<string, unknown>;
123
+ outputSchema?: Record<string, unknown>;
122
124
  }
123
125
  interface TaskSnapshot {
124
126
  totalRuns: number;
@@ -1,3 +1,4 @@
1
+ import { RouterDef } from "../types.mjs";
1
2
  import { AnalyticsOptions, AnalyticsSnapshot, ErrorEntry, ProcedureCall, ProcedureSnapshot, RequestEntry, SpanKind, TaskExecution, TaskSnapshot, TraceSpan } from "./analytics/types.mjs";
2
3
  import { AnalyticsCollector } from "./analytics/collector.mjs";
3
4
  import { RequestTrace, trace } from "./analytics/trace.mjs";
@@ -9,10 +10,14 @@ import { analyticsTraceMap } from "../core/trace-map.mjs";
9
10
  import { FetchHandler } from "../core/handler.mjs";
10
11
 
11
12
  //#region src/plugins/analytics.d.ts
13
+ interface ProcedureSchemaInfo {
14
+ input?: Record<string, unknown>;
15
+ output?: Record<string, unknown>;
16
+ }
12
17
  /**
13
18
  * Wrap a fetch handler with analytics collection.
14
19
  * Intercepts analytics dashboard routes and instruments every request.
15
20
  */
16
- declare function wrapWithAnalytics(handler: FetchHandler, options?: AnalyticsOptions): FetchHandler;
21
+ declare function wrapWithAnalytics(handler: FetchHandler, router: RouterDef | undefined, options?: AnalyticsOptions): FetchHandler;
17
22
  //#endregion
18
- export { AnalyticsCollector, type AnalyticsOptions, type AnalyticsSnapshot, type ErrorEntry, type ProcedureCall, type ProcedureSnapshot, RequestAccumulator, type RequestEntry, RequestTrace, type SpanKind, type TaskExecution, type TaskSnapshot, type TraceSpan, analyticsAuthResponse, analyticsHTML, analyticsTraceMap, checkAnalyticsAuth, errorToMarkdown, requestToMarkdown, sanitizeHeaders, serveAnalyticsRoute, trace, wrapWithAnalytics };
23
+ export { AnalyticsCollector, type AnalyticsOptions, type AnalyticsSnapshot, type ErrorEntry, type ProcedureCall, ProcedureSchemaInfo, type ProcedureSnapshot, RequestAccumulator, type RequestEntry, RequestTrace, type SpanKind, type TaskExecution, type TaskSnapshot, type TraceSpan, analyticsAuthResponse, analyticsHTML, analyticsTraceMap, checkAnalyticsAuth, errorToMarkdown, requestToMarkdown, sanitizeHeaders, serveAnalyticsRoute, trace, wrapWithAnalytics };
@@ -2,6 +2,7 @@ import { ValidationError } from "../core/schema.mjs";
2
2
  import { SilgiError, toSilgiError } from "../core/error.mjs";
3
3
  import { analyticsTraceMap } from "../core/trace-map.mjs";
4
4
  import { parseUrlPathname } from "../core/url.mjs";
5
+ import { ZodSchemaConverter } from "../integrations/zod/converter.mjs";
5
6
  import { generateRequestId } from "./analytics/request-id.mjs";
6
7
  import { isTrackedRequestPath, normalizeAnalyticsPath, round, sanitizeHeaders } from "./analytics/utils.mjs";
7
8
  import { RequestAccumulator } from "./analytics/accumulator.mjs";
@@ -80,12 +81,48 @@ function extractResponseError(output, status, fallback) {
80
81
  message: `Request failed with status ${status}`
81
82
  };
82
83
  }
84
+ function isProcedureDef(value) {
85
+ return typeof value === "object" && value !== null && "type" in value && "resolve" in value && typeof value.resolve === "function";
86
+ }
87
+ const _zodConverter = new ZodSchemaConverter();
88
+ function schemaToJson(schema, strategy) {
89
+ if (!schema) return void 0;
90
+ const std = schema["~standard"];
91
+ if (std?.jsonSchema?.input) try {
92
+ const result = std.jsonSchema.input({ target: "draft-2020-12" });
93
+ if (result && typeof result === "object") {
94
+ const { $schema: _, ...rest } = result;
95
+ return rest;
96
+ }
97
+ } catch {}
98
+ if (_zodConverter.condition(schema)) try {
99
+ const [, json] = _zodConverter.convert(schema, { strategy });
100
+ return json;
101
+ } catch {}
102
+ }
103
+ function extractProcedureSchemas(router) {
104
+ const schemas = /* @__PURE__ */ new Map();
105
+ function walk(node, path) {
106
+ if (isProcedureDef(node)) {
107
+ const info = {};
108
+ if (node.input) info.input = schemaToJson(node.input, "input");
109
+ if (node.output) info.output = schemaToJson(node.output, "output");
110
+ if (info.input || info.output) schemas.set(path.join("/"), info);
111
+ return;
112
+ }
113
+ if (typeof node === "object" && node !== null) for (const [key, child] of Object.entries(node)) walk(child, [...path, key]);
114
+ }
115
+ walk(router, []);
116
+ return schemas;
117
+ }
83
118
  /**
84
119
  * Wrap a fetch handler with analytics collection.
85
120
  * Intercepts analytics dashboard routes and instruments every request.
86
121
  */
87
- function wrapWithAnalytics(handler, options = {}) {
122
+ function wrapWithAnalytics(handler, router, options = {}) {
88
123
  const collector = new AnalyticsCollector(options);
124
+ const procedureSchemas = router ? extractProcedureSchemas(router) : void 0;
125
+ if (procedureSchemas) collector.setProcedureSchemas(procedureSchemas);
89
126
  const dashboardHtml = analyticsHTML();
90
127
  const auth = options.auth;
91
128
  import("../core/task.mjs").then(({ setTaskAnalytics }) => {
package/dist/scalar.mjs CHANGED
@@ -1,5 +1,12 @@
1
+ import { ZodSchemaConverter } from "./integrations/zod/converter.mjs";
1
2
  //#region src/scalar.ts
2
3
  /**
4
+ * Scalar API Reference — v2 OpenAPI integration.
5
+ *
6
+ * Generates OpenAPI 3.1.0 spec from v2 RouterDef and serves
7
+ * Scalar UI at /api/reference + spec at /api/openapi.json.
8
+ */
9
+ /**
3
10
  * Generate OpenAPI 3.1.0 document from a v2 RouterDef.
4
11
  */
5
12
  /**
@@ -67,7 +74,7 @@ function generateOpenAPI(router, options = {}, basePath = "") {
67
74
  if (route?.security === false) operation.security = [];
68
75
  else if (route?.security) operation.security = route.security.map((s) => ({ [s]: [] }));
69
76
  else if (options.security) operation.security = [{ auth: [] }];
70
- const inputSchema = proc.input ? schemaToJsonSchema(proc.input) : null;
77
+ const inputSchema = proc.input ? schemaToJsonSchema(proc.input, "input") : null;
71
78
  const successStatus = route?.successStatus ?? 200;
72
79
  const successDesc = route?.successDescription ?? "Successful response";
73
80
  const guards = (proc.use ?? []).filter((m) => m.kind === "guard" && m.errors);
@@ -100,7 +107,7 @@ function generateOpenAPI(router, options = {}, basePath = "") {
100
107
  };
101
108
  if (params.length > 0) op.parameters = params;
102
109
  if (proc.type === "subscription") {
103
- const outputSchema = proc.output ? schemaToJsonSchema(proc.output) : { type: "string" };
110
+ const outputSchema = proc.output ? schemaToJsonSchema(proc.output, "output") : { type: "string" };
104
111
  op.responses[String(successStatus)] = {
105
112
  description: "SSE event stream",
106
113
  content: { "text/event-stream": { schema: {
@@ -110,7 +117,7 @@ function generateOpenAPI(router, options = {}, basePath = "") {
110
117
  };
111
118
  } else if (proc.output) op.responses[String(successStatus)] = {
112
119
  description: successDesc,
113
- content: { "application/json": { schema: schemaToJsonSchema(proc.output) } }
120
+ content: { "application/json": { schema: schemaToJsonSchema(proc.output, "output") } }
114
121
  };
115
122
  else op.responses[String(successStatus)] = { description: successDesc };
116
123
  if (proc.input) op.responses["400"] = {
@@ -263,20 +270,25 @@ function collectProcedures(node, path, cb) {
263
270
  if (typeof node === "object" && node !== null) for (const [key, child] of Object.entries(node)) collectProcedures(child, [...path, key], cb);
264
271
  }
265
272
  /**
266
- * Convert a Standard Schema to JSON Schema via `~standard.jsonSchema.input()`.
273
+ * Convert a Standard Schema to JSON Schema.
267
274
  *
268
- * Works with Zod v4, Valibot, ArkType — any validator implementing Standard Schema v1.
275
+ * Fast path: `~standard.jsonSchema.input()` (StandardJSONSchemaV1 implementors).
276
+ * Fallback: vendor-specific converters (Zod v4 via ZodSchemaConverter).
269
277
  */
270
- function schemaToJsonSchema(schema) {
278
+ const _zodConverter = new ZodSchemaConverter();
279
+ function schemaToJsonSchema(schema, strategy = "input") {
271
280
  const std = schema["~standard"];
272
- if (!std?.jsonSchema?.input) return {};
273
- try {
281
+ if (std?.jsonSchema?.input) try {
274
282
  const result = std.jsonSchema.input({ target: "draft-2020-12" });
275
283
  if (result && typeof result === "object") {
276
284
  const { $schema: _, ...rest } = result;
277
285
  return rest;
278
286
  }
279
287
  } catch {}
288
+ if (_zodConverter.condition(schema)) try {
289
+ const [, jsonSchema] = _zodConverter.convert(schema, { strategy });
290
+ return jsonSchema;
291
+ } catch {}
280
292
  return {};
281
293
  }
282
294
  function objectSchemaToParams(schema) {
package/dist/silgi.d.mts CHANGED
@@ -95,7 +95,8 @@ interface SilgiInstance<TBaseCtx extends Record<string, unknown>> {
95
95
  router: <T extends RouterDef>(def: T) => T;
96
96
  /** Create a Fetch API handler: (Request) => Response */
97
97
  handler: (router: RouterDef, options?: {
98
- /** Enable Scalar API Reference UI at /api/reference and /api/openapi.json */scalar?: boolean | ScalarOptions; /** Enable analytics dashboard at /api/analytics */
98
+ /** URL path prefix (e.g. "/api"). Only requests matching this prefix are handled; others return 404. */basePath?: string; /** Enable Scalar API Reference UI at /api/reference and /api/openapi.json */
99
+ scalar?: boolean | ScalarOptions; /** Enable analytics dashboard at /api/analytics */
99
100
  analytics?: boolean | AnalyticsOptions;
100
101
  }) => (request: Request) => Response | Promise<Response>;
101
102
  /** Create a direct caller — call procedures without HTTP. For testing and server-side usage. */
package/dist/silgi.mjs CHANGED
@@ -3,6 +3,7 @@ import { createProcedureBuilder } from "./builder.mjs";
3
3
  import { assignPaths, routerCache } from "./core/router-utils.mjs";
4
4
  import { compileRouter } from "./compile.mjs";
5
5
  import { createCaller } from "./caller.mjs";
6
+ import { normalizePrefix } from "./core/url.mjs";
6
7
  import { createFetchHandler, wrapHandler } from "./core/handler.mjs";
7
8
  import { createHooks } from "hookable";
8
9
  //#region src/silgi.ts
@@ -113,7 +114,8 @@ function silgi(config) {
113
114
  return createCaller(routerDef, contextFactory, options);
114
115
  },
115
116
  handler: (routerDef, options) => {
116
- const fetchHandler = wrapHandler(createFetchHandler(routerDef, contextFactory, hooks), routerDef, options);
117
+ const prefix = options?.basePath ? normalizePrefix(options.basePath) : void 0;
118
+ const fetchHandler = wrapHandler(createFetchHandler(routerDef, contextFactory, hooks, prefix), routerDef, options, prefix);
117
119
  if (!(function checkWs(def) {
118
120
  if (!def || typeof def !== "object") return false;
119
121
  if (def.type === "subscription") return true;
@@ -126,8 +128,9 @@ function silgi(config) {
126
128
  const { _createWSHooks } = await import("./ws.mjs");
127
129
  wsHooks = _createWSHooks(routerDef);
128
130
  }
131
+ const wsPath = prefix ? `${prefix}/_ws` : "/_ws";
129
132
  return async (request) => {
130
- if (new URL(request.url).pathname === "/_ws") {
133
+ if (new URL(request.url).pathname === wsPath) {
131
134
  if (!wsHooks) {
132
135
  wsInitPromise ??= initWsHooks();
133
136
  await wsInitPromise;