silgi 0.51.2 → 0.51.3
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/dist/adapters/_fetch-adapter.mjs +3 -28
- package/dist/core/handler.d.mts +2 -0
- package/dist/core/handler.mjs +10 -2
- package/dist/core/serve.d.mts +2 -0
- package/dist/core/serve.mjs +3 -1
- package/dist/core/url.mjs +7 -1
- package/dist/silgi.d.mts +2 -1
- package/dist/silgi.mjs +5 -2
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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
|
|
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);
|
package/dist/core/handler.d.mts
CHANGED
|
@@ -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 };
|
package/dist/core/handler.mjs
CHANGED
|
@@ -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
|
-
|
|
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);
|
package/dist/core/serve.d.mts
CHANGED
|
@@ -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 */
|
package/dist/core/serve.mjs
CHANGED
|
@@ -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
|
|
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 };
|
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
|
-
/**
|
|
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
|
|
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 ===
|
|
133
|
+
if (new URL(request.url).pathname === wsPath) {
|
|
131
134
|
if (!wsHooks) {
|
|
132
135
|
wsInitPromise ??= initWsHooks();
|
|
133
136
|
await wsInitPromise;
|