freshyo/apps/web-ui/.output/server/_ssr/ssr.mjs
2026-05-10 16:45:39 +05:30

1367 lines
48 KiB
JavaScript

import "../_runtime.mjs";
import { h as require_react, m as require_jsx_runtime } from "../_libs/react+tanstack__react-query.mjs";
import { S as isNotFound, _ as executeRewriteInput, g as resolveManifestAssetLink, i as RouterProvider, m as getStylesheetHref, t as renderRouterToStream, v as isRedirect, w as invariant, x as rootRouteId, y as isResolvedRedirect } from "../_libs/@tanstack/react-router+[...].mjs";
import { n as createMemoryHistory } from "../_libs/tanstack__history.mjs";
import { a as getOrigin, c as createSerializationAdapter, d as iu, f as su, i as getNormalizedURL, l as makeSerovalPlugin, n as mergeHeaders, o as defaultSerovalPlugins, r as attachRouterServerSsrUtils, s as createRawStreamRPCPlugin, t as defineHandlerCallback, u as Pu } from "../_libs/@tanstack/router-core+[...].mjs";
import { n as toResponse, t as H3Event } from "../_libs/h3-v2.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
require_react();
var import_jsx_runtime = require_jsx_runtime();
function StartServer(props) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RouterProvider, { router: props.router });
}
var defaultStreamHandler = defineHandlerCallback(({ request, router, responseHeaders }) => renderRouterToStream({
request,
router,
responseHeaders,
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(StartServer, { router })
}));
var GLOBAL_EVENT_STORAGE_KEY = Symbol.for("tanstack-start:event-storage");
var globalObj$1 = globalThis;
if (!globalObj$1[GLOBAL_EVENT_STORAGE_KEY]) globalObj$1[GLOBAL_EVENT_STORAGE_KEY] = new AsyncLocalStorage();
var eventStorage = globalObj$1[GLOBAL_EVENT_STORAGE_KEY];
function isPromiseLike(value) {
return typeof value.then === "function";
}
function getSetCookieValues(headers) {
const headersWithSetCookie = headers;
if (typeof headersWithSetCookie.getSetCookie === "function") return headersWithSetCookie.getSetCookie();
const value = headers.get("set-cookie");
return value ? [value] : [];
}
function mergeEventResponseHeaders(response, event) {
if (response.ok) return;
const eventSetCookies = getSetCookieValues(event.res.headers);
if (eventSetCookies.length === 0) return;
const responseSetCookies = getSetCookieValues(response.headers);
response.headers.delete("set-cookie");
for (const cookie of responseSetCookies) response.headers.append("set-cookie", cookie);
for (const cookie of eventSetCookies) response.headers.append("set-cookie", cookie);
}
function attachResponseHeaders(value, event) {
if (isPromiseLike(value)) return value.then((resolved) => {
if (resolved instanceof Response) mergeEventResponseHeaders(resolved, event);
return resolved;
});
if (value instanceof Response) mergeEventResponseHeaders(value, event);
return value;
}
function requestHandler(handler) {
return (request, requestOpts) => {
let h3Event;
try {
h3Event = new H3Event(request);
} catch (error) {
if (error instanceof URIError) return new Response(null, {
status: 400,
statusText: "Bad Request"
});
throw error;
}
return toResponse(attachResponseHeaders(eventStorage.run({ h3Event }, () => handler(request, requestOpts)), h3Event), h3Event);
};
}
function getH3Event() {
const event = eventStorage.getStore();
if (!event) throw new Error(`No StartEvent found in AsyncLocalStorage. Make sure you are using the function within the server runtime.`);
return event.h3Event;
}
function getResponse() {
return getH3Event().res;
}
var HEADERS = { TSS_SHELL: "X-TSS_SHELL" };
/**
* @description Returns the router manifest data that should be sent to the client.
* This includes only the assets and preloads for the current route and any
* special assets that are needed for the client. It does not include relationships
* between routes or any other data that is not needed for the client.
*
* The client entry URL is returned separately so that it can be transformed
* (e.g. for CDN rewriting) before being embedded into the `<script>` tag.
*
* @param matchedRoutes - In dev mode, the matched routes are used to build
* the dev styles URL for route-scoped CSS collection.
*/
async function getStartManifest(matchedRoutes) {
const { tsrStartManifest } = await import("../_tanstack-start-manifest_v-B3vqSR1M.mjs");
const startManifest = tsrStartManifest();
const rootRoute = startManifest.routes[rootRouteId] = startManifest.routes["__root__"] || {};
rootRoute.assets = rootRoute.assets || [];
let injectedHeadScripts;
return {
manifest: {
inlineCss: startManifest.inlineCss,
routes: Object.fromEntries(Object.entries(startManifest.routes).flatMap(([k, v]) => {
const result = {};
let hasData = false;
if (v.preloads && v.preloads.length > 0) {
result["preloads"] = v.preloads;
hasData = true;
}
if (v.assets && v.assets.length > 0) {
result["assets"] = v.assets;
hasData = true;
}
if (!hasData) return [];
return [[k, result]];
}))
},
clientEntry: startManifest.clientEntry,
injectedHeadScripts
};
}
var manifest = {};
async function getServerFnById(id, access) {
const serverFnInfo = manifest[id];
if (!serverFnInfo) throw new Error("Server function info not found for " + id);
const fnModule = serverFnInfo.module ?? await serverFnInfo.importer();
if (!fnModule) throw new Error("Server function module not resolved for " + id);
const action = fnModule[serverFnInfo.functionName];
if (!action) throw new Error("Server function module export not resolved for serverFn ID: " + id);
return action;
}
var TSS_FORMDATA_CONTEXT = "__TSS_CONTEXT";
var TSS_SERVER_FUNCTION = Symbol.for("TSS_SERVER_FUNCTION");
var X_TSS_SERIALIZED = "x-tss-serialized";
var X_TSS_RAW_RESPONSE = "x-tss-raw";
/** Content-Type for multiplexed framed responses (RawStream support) */
var TSS_CONTENT_TYPE_FRAMED = "application/x-tss-framed";
/**
* Frame types for binary multiplexing protocol.
*/
var FrameType = {
JSON: 0,
CHUNK: 1,
END: 2,
ERROR: 3
};
/** Full Content-Type header value with version parameter */
var TSS_CONTENT_TYPE_FRAMED_VERSIONED = `${TSS_CONTENT_TYPE_FRAMED}; v=1`;
function isSafeKey(key) {
return key !== "__proto__" && key !== "constructor" && key !== "prototype";
}
/**
* Merge target and source into a new null-proto object, filtering dangerous keys.
*/
function safeObjectMerge(target, source) {
const result = Object.create(null);
if (target) {
for (const key of Object.keys(target)) if (isSafeKey(key)) result[key] = target[key];
}
if (source && typeof source === "object") {
for (const key of Object.keys(source)) if (isSafeKey(key)) result[key] = source[key];
}
return result;
}
/**
* Create a null-prototype object, optionally copying from source.
*/
function createNullProtoObject(source) {
if (!source) return Object.create(null);
const obj = Object.create(null);
for (const key of Object.keys(source)) if (isSafeKey(key)) obj[key] = source[key];
return obj;
}
var GLOBAL_STORAGE_KEY = Symbol.for("tanstack-start:start-storage-context");
var globalObj = globalThis;
if (!globalObj[GLOBAL_STORAGE_KEY]) globalObj[GLOBAL_STORAGE_KEY] = new AsyncLocalStorage();
var startStorage = globalObj[GLOBAL_STORAGE_KEY];
async function runWithStartContext(context, fn) {
return startStorage.run(context, fn);
}
function getStartContext(opts) {
const context = startStorage.getStore();
if (!context && opts?.throwIfNotFound !== false) throw new Error(`No Start context found in AsyncLocalStorage. Make sure you are using the function within the server runtime.`);
return context;
}
var getStartOptions = () => getStartContext().startOptions;
function flattenMiddlewares(middlewares, maxDepth = 100) {
const seen = /* @__PURE__ */ new Set();
const flattened = [];
const recurse = (middleware, depth) => {
if (depth > maxDepth) throw new Error(`Middleware nesting depth exceeded maximum of ${maxDepth}. Check for circular references.`);
middleware.forEach((m) => {
if (m.options.middleware) recurse(m.options.middleware, depth + 1);
if (!seen.has(m)) {
seen.add(m);
flattened.push(m);
}
});
};
recurse(middlewares, 0);
return flattened;
}
function getDefaultSerovalPlugins() {
return [...(getStartOptions()?.serializationAdapters)?.map(makeSerovalPlugin) ?? [], ...defaultSerovalPlugins];
}
/**
* Binary frame protocol for multiplexing JSON and raw streams over HTTP.
*
* Frame format: [type:1][streamId:4][length:4][payload:length]
* - type: 1 byte - frame type (JSON, CHUNK, END, ERROR)
* - streamId: 4 bytes big-endian uint32 - stream identifier
* - length: 4 bytes big-endian uint32 - payload length
* - payload: variable length bytes
*/
/** Cached TextEncoder for frame encoding */
var textEncoder = new TextEncoder();
/** Shared empty payload for END frames - avoids allocation per call */
var EMPTY_PAYLOAD = new Uint8Array(0);
/**
* Encodes a single frame with header and payload.
*/
function encodeFrame(type, streamId, payload) {
const frame = new Uint8Array(9 + payload.length);
frame[0] = type;
frame[1] = streamId >>> 24 & 255;
frame[2] = streamId >>> 16 & 255;
frame[3] = streamId >>> 8 & 255;
frame[4] = streamId & 255;
frame[5] = payload.length >>> 24 & 255;
frame[6] = payload.length >>> 16 & 255;
frame[7] = payload.length >>> 8 & 255;
frame[8] = payload.length & 255;
frame.set(payload, 9);
return frame;
}
/**
* Encodes a JSON frame (type 0, streamId 0).
*/
function encodeJSONFrame(json) {
return encodeFrame(FrameType.JSON, 0, textEncoder.encode(json));
}
/**
* Encodes a raw stream chunk frame.
*/
function encodeChunkFrame(streamId, chunk) {
return encodeFrame(FrameType.CHUNK, streamId, chunk);
}
/**
* Encodes a raw stream end frame.
*/
function encodeEndFrame(streamId) {
return encodeFrame(FrameType.END, streamId, EMPTY_PAYLOAD);
}
/**
* Encodes a raw stream error frame.
*/
function encodeErrorFrame(streamId, error) {
const message = error instanceof Error ? error.message : String(error ?? "Unknown error");
return encodeFrame(FrameType.ERROR, streamId, textEncoder.encode(message));
}
/**
* Creates a multiplexed ReadableStream from JSON stream and raw streams.
*
* The JSON stream emits NDJSON lines (from seroval's toCrossJSONStream).
* Raw streams are pumped concurrently, interleaved with JSON frames.
*
* Supports late stream registration for RawStreams discovered after initial
* serialization (e.g., from resolved Promises).
*
* @param jsonStream Stream of JSON strings (each string is one NDJSON line)
* @param rawStreams Map of stream IDs to raw binary streams (known at start)
* @param lateStreamSource Optional stream of late registrations for streams discovered later
*/
function createMultiplexedStream(jsonStream, rawStreams, lateStreamSource) {
let controller;
let cancelled = false;
const readers = [];
const enqueue = (frame) => {
if (cancelled) return false;
try {
controller.enqueue(frame);
return true;
} catch {
return false;
}
};
const errorOutput = (error) => {
if (cancelled) return;
cancelled = true;
try {
controller.error(error);
} catch {}
for (const reader of readers) reader.cancel().catch(() => {});
};
async function pumpRawStream(streamId, stream) {
const reader = stream.getReader();
readers.push(reader);
try {
while (!cancelled) {
const { done, value } = await reader.read();
if (done) {
enqueue(encodeEndFrame(streamId));
return;
}
if (!enqueue(encodeChunkFrame(streamId, value))) return;
}
} catch (error) {
enqueue(encodeErrorFrame(streamId, error));
} finally {
reader.releaseLock();
}
}
async function pumpJSON() {
const reader = jsonStream.getReader();
readers.push(reader);
try {
while (!cancelled) {
const { done, value } = await reader.read();
if (done) return;
if (!enqueue(encodeJSONFrame(value))) return;
}
} catch (error) {
errorOutput(error);
throw error;
} finally {
reader.releaseLock();
}
}
async function pumpLateStreams() {
if (!lateStreamSource) return [];
const lateStreamPumps = [];
const reader = lateStreamSource.getReader();
readers.push(reader);
try {
while (!cancelled) {
const { done, value } = await reader.read();
if (done) break;
lateStreamPumps.push(pumpRawStream(value.id, value.stream));
}
} finally {
reader.releaseLock();
}
return lateStreamPumps;
}
return new ReadableStream({
async start(ctrl) {
controller = ctrl;
const pumps = [pumpJSON()];
for (const [streamId, stream] of rawStreams) pumps.push(pumpRawStream(streamId, stream));
if (lateStreamSource) pumps.push(pumpLateStreams());
try {
const latePumps = (await Promise.all(pumps)).find(Array.isArray);
if (latePumps && latePumps.length > 0) await Promise.all(latePumps);
if (!cancelled) try {
controller.close();
} catch {}
} catch {}
},
cancel() {
cancelled = true;
for (const reader of readers) reader.cancel().catch(() => {});
readers.length = 0;
}
});
}
var serovalPlugins = void 0;
var FORM_DATA_CONTENT_TYPES = ["multipart/form-data", "application/x-www-form-urlencoded"];
var MAX_PAYLOAD_SIZE = 1e6;
var handleServerAction = async ({ request, context, serverFnId }) => {
const methodUpper = request.method.toUpperCase();
const url = new URL(request.url);
const action = await getServerFnById(serverFnId, { origin: "client" });
if (action.method && methodUpper !== action.method) return new Response(`expected ${action.method} method. Got ${methodUpper}`, {
status: 405,
headers: { Allow: action.method }
});
const isServerFn = request.headers.get("x-tsr-serverFn") === "true";
if (!serovalPlugins) serovalPlugins = getDefaultSerovalPlugins();
const contentType = request.headers.get("Content-Type");
function parsePayload(payload) {
return Pu(payload, { plugins: serovalPlugins });
}
return await (async () => {
try {
let res = await (async () => {
if (FORM_DATA_CONTENT_TYPES.some((type) => contentType && contentType.includes(type))) {
if (methodUpper === "GET") invariant();
const formData = await request.formData();
const serializedContext = formData.get(TSS_FORMDATA_CONTEXT);
formData.delete(TSS_FORMDATA_CONTEXT);
const params = {
context,
data: formData,
method: methodUpper
};
if (typeof serializedContext === "string") try {
const deserializedContext = Pu(JSON.parse(serializedContext), { plugins: serovalPlugins });
if (typeof deserializedContext === "object" && deserializedContext) params.context = safeObjectMerge(deserializedContext, context);
} catch (e) {}
return await action(params);
}
if (methodUpper === "GET") {
const payloadParam = url.searchParams.get("payload");
if (payloadParam && payloadParam.length > MAX_PAYLOAD_SIZE) throw new Error("Payload too large");
const payload = payloadParam ? parsePayload(JSON.parse(payloadParam)) : {};
payload.context = safeObjectMerge(payload.context, context);
payload.method = methodUpper;
return await action(payload);
}
let jsonPayload;
if (contentType?.includes("application/json")) jsonPayload = await request.json();
const payload = jsonPayload ? parsePayload(jsonPayload) : {};
payload.context = safeObjectMerge(payload.context, context);
payload.method = methodUpper;
return await action(payload);
})();
const unwrapped = res.result || res.error;
if (isNotFound(res)) res = isNotFoundResponse(res);
if (!isServerFn) return unwrapped;
if (unwrapped instanceof Response) {
if (isRedirect(unwrapped)) return unwrapped;
unwrapped.headers.set(X_TSS_RAW_RESPONSE, "true");
return unwrapped;
}
return serializeResult(res);
function serializeResult(res) {
let nonStreamingBody = void 0;
const alsResponse = getResponse();
if (res !== void 0) {
const rawStreams = /* @__PURE__ */ new Map();
let initialPhase = true;
let lateStreamWriter;
let lateStreamReadable = void 0;
const pendingLateStreams = [];
const plugins = [createRawStreamRPCPlugin((id, stream) => {
if (initialPhase) {
rawStreams.set(id, stream);
return;
}
if (lateStreamWriter) {
lateStreamWriter.write({
id,
stream
}).catch(() => {});
return;
}
pendingLateStreams.push({
id,
stream
});
}), ...serovalPlugins || []];
let done = false;
const callbacks = {
onParse: (value) => {
nonStreamingBody = value;
},
onDone: () => {
done = true;
},
onError: (error) => {
throw error;
}
};
iu(res, {
refs: /* @__PURE__ */ new Map(),
plugins,
onParse(value) {
callbacks.onParse(value);
},
onDone() {
callbacks.onDone();
},
onError: (error) => {
callbacks.onError(error);
}
});
initialPhase = false;
if (done && rawStreams.size === 0) return new Response(nonStreamingBody ? JSON.stringify(nonStreamingBody) : void 0, {
status: alsResponse.status,
statusText: alsResponse.statusText,
headers: {
"Content-Type": "application/json",
[X_TSS_SERIALIZED]: "true"
}
});
const { readable, writable } = new TransformStream();
lateStreamReadable = readable;
lateStreamWriter = writable.getWriter();
for (const registration of pendingLateStreams) lateStreamWriter.write(registration).catch(() => {});
pendingLateStreams.length = 0;
const multiplexedStream = createMultiplexedStream(new ReadableStream({
start(controller) {
callbacks.onParse = (value) => {
controller.enqueue(JSON.stringify(value) + "\n");
};
callbacks.onDone = () => {
try {
controller.close();
} catch {}
lateStreamWriter?.close().catch(() => {}).finally(() => {
lateStreamWriter = void 0;
});
};
callbacks.onError = (error) => {
controller.error(error);
lateStreamWriter?.abort(error).catch(() => {}).finally(() => {
lateStreamWriter = void 0;
});
};
if (nonStreamingBody !== void 0) callbacks.onParse(nonStreamingBody);
if (done) callbacks.onDone();
},
cancel() {
lateStreamWriter?.abort().catch(() => {});
lateStreamWriter = void 0;
}
}), rawStreams, lateStreamReadable);
return new Response(multiplexedStream, {
status: alsResponse.status,
statusText: alsResponse.statusText,
headers: {
"Content-Type": TSS_CONTENT_TYPE_FRAMED_VERSIONED,
[X_TSS_SERIALIZED]: "true"
}
});
}
return new Response(void 0, {
status: alsResponse.status,
statusText: alsResponse.statusText
});
}
} catch (error) {
if (error instanceof Response) return error;
if (isNotFound(error)) return isNotFoundResponse(error);
console.info();
console.info("Server Fn Error!");
console.info();
console.error(error);
console.info();
const serializedError = JSON.stringify(await Promise.resolve(su(error, {
refs: /* @__PURE__ */ new Map(),
plugins: serovalPlugins
})));
const response = getResponse();
return new Response(serializedError, {
status: response.status ?? 500,
statusText: response.statusText,
headers: {
"Content-Type": "application/json",
[X_TSS_SERIALIZED]: "true"
}
});
}
})();
};
function isNotFoundResponse(error) {
const { headers, ...rest } = error;
return new Response(JSON.stringify(rest), {
status: 404,
headers: {
"Content-Type": "application/json",
...headers || {}
}
});
}
function normalizeTransformAssetResult(result) {
if (typeof result === "string") return { href: result };
return result;
}
function resolveTransformAssetsCrossOrigin(config, kind) {
if (!config) return void 0;
if (typeof config === "string") return config;
return config[kind];
}
function isObjectShorthand(transform) {
return "prefix" in transform;
}
function resolveTransformAssetsConfig(transform) {
if (typeof transform === "string") {
const prefix = transform;
return {
type: "transform",
transformFn: ({ url }) => ({ href: `${prefix}${url}` }),
cache: true
};
}
if (typeof transform === "function") return {
type: "transform",
transformFn: transform,
cache: true
};
if (isObjectShorthand(transform)) {
const { prefix, crossOrigin } = transform;
return {
type: "transform",
transformFn: ({ url, kind }) => {
const href = `${prefix}${url}`;
if (kind === "clientEntry") return { href };
const co = resolveTransformAssetsCrossOrigin(crossOrigin, kind);
return co ? {
href,
crossOrigin: co
} : { href };
},
cache: true
};
}
if ("createTransform" in transform && transform.createTransform) return {
type: "createTransform",
createTransform: transform.createTransform,
cache: transform.cache !== false
};
return {
type: "transform",
transformFn: typeof transform.transform === "string" ? (({ url }) => ({ href: `${transform.transform}${url}` })) : transform.transform,
cache: transform.cache !== false
};
}
function adaptTransformAssetUrlsToTransformAssets(transformFn) {
return async ({ url, kind }) => ({ href: await transformFn({
url,
type: kind
}) });
}
function adaptTransformAssetUrlsConfigToTransformAssets(transform) {
if (typeof transform === "string") return transform;
if (typeof transform === "function") return adaptTransformAssetUrlsToTransformAssets(transform);
if ("createTransform" in transform && transform.createTransform) return {
createTransform: async (ctx) => adaptTransformAssetUrlsToTransformAssets(await transform.createTransform(ctx)),
cache: transform.cache,
warmup: transform.warmup
};
return {
transform: typeof transform.transform === "string" ? transform.transform : adaptTransformAssetUrlsToTransformAssets(transform.transform),
cache: transform.cache,
warmup: transform.warmup
};
}
/**
* Builds the client entry `<script>` tag from a (possibly transformed) client
* entry URL and optional injected head scripts.
*/
function buildClientEntryScriptTag(clientEntry, injectedHeadScripts) {
let script = `import(${JSON.stringify(clientEntry)})`;
if (injectedHeadScripts) script = `${injectedHeadScripts};${script}`;
return {
tag: "script",
attrs: {
type: "module",
async: true
},
children: script
};
}
function assignManifestAssetLink(link, next) {
if (typeof link === "string") return next.crossOrigin ? next : next.href;
return next.crossOrigin ? next : { href: next.href };
}
async function transformManifestAssets(source, transformFn, _opts) {
const manifest = structuredClone(source.manifest);
for (const route of Object.values(manifest.routes)) {
if (route.preloads) route.preloads = await Promise.all(route.preloads.map(async (link) => {
const result = normalizeTransformAssetResult(await transformFn({
url: resolveManifestAssetLink(link).href,
kind: "modulepreload"
}));
return assignManifestAssetLink(link, {
href: result.href,
crossOrigin: result.crossOrigin
});
}));
if (route.assets && !source.manifest.inlineCss) {
for (const asset of route.assets) if (asset.tag === "link" && asset.attrs?.href) {
const rel = asset.attrs.rel;
if (!(typeof rel === "string" ? rel.split(/\s+/) : []).includes("stylesheet")) continue;
const result = normalizeTransformAssetResult(await transformFn({
url: asset.attrs.href,
kind: "stylesheet"
}));
asset.attrs.href = result.href;
if (result.crossOrigin) asset.attrs.crossOrigin = result.crossOrigin;
else delete asset.attrs.crossOrigin;
}
}
}
const transformedClientEntry = normalizeTransformAssetResult(await transformFn({
url: source.clientEntry,
kind: "clientEntry"
}));
const rootRoute = manifest.routes[rootRouteId] = manifest.routes["__root__"] || {};
rootRoute.assets = rootRoute.assets || [];
rootRoute.assets.push(buildClientEntryScriptTag(transformedClientEntry.href, source.injectedHeadScripts));
return manifest;
}
/**
* Builds a final Manifest from a StartManifestWithClientEntry without any
* URL transforms. Used when no transformAssetUrls option is provided.
*
* Returns a new manifest object so the cached base manifest is never mutated.
*/
function buildManifestWithClientEntry(source) {
const scriptTag = buildClientEntryScriptTag(source.clientEntry, source.injectedHeadScripts);
const baseRootRoute = source.manifest.routes[rootRouteId];
const routes = {
...source.manifest.routes,
[rootRouteId]: {
...baseRootRoute,
assets: [...baseRootRoute?.assets || [], scriptTag]
}
};
return {
inlineCss: source.manifest.inlineCss,
routes
};
}
var LINK_PARAM_TOKEN_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
var PRELOAD_AS_VALUES = new Set([
"fetch",
"font",
"image",
"script",
"style",
"track"
]);
function buildLinkParam(name, value) {
if (value === void 0) return name;
if (LINK_PARAM_TOKEN_RE.test(value)) return `${name}=${value}`;
return `${name}=${JSON.stringify(value)}`;
}
function serializeEarlyHint(hint) {
const parts = [`<${hint.href}>`, buildLinkParam("rel", hint.rel)];
if (hint.as) parts.push(buildLinkParam("as", hint.as));
if (hint.crossOrigin !== void 0) parts.push(buildLinkParam("crossorigin", hint.crossOrigin || void 0));
if (hint.type) parts.push(buildLinkParam("type", hint.type));
if (hint.integrity) parts.push(buildLinkParam("integrity", hint.integrity));
if (hint.referrerPolicy) parts.push(buildLinkParam("referrerpolicy", hint.referrerPolicy));
if (hint.fetchPriority) parts.push(buildLinkParam("fetchpriority", hint.fetchPriority));
return parts.join("; ");
}
function getStringAttr(attrs, name, fallbackName) {
const value = attrs?.[name] ?? (fallbackName ? attrs?.[fallbackName] : void 0);
return typeof value === "string" ? value : void 0;
}
function getPreloadAs(attrs) {
const as = getStringAttr(attrs, "as");
return as && PRELOAD_AS_VALUES.has(as) ? as : void 0;
}
function addEarlyHintFetchAttrs(hint, attrs) {
const crossOrigin = getStringAttr(attrs, "crossOrigin", "crossorigin");
const type = getStringAttr(attrs, "type");
const integrity = getStringAttr(attrs, "integrity");
const referrerPolicy = getStringAttr(attrs, "referrerPolicy", "referrerpolicy");
const fetchPriority = getStringAttr(attrs, "fetchPriority", "fetchpriority");
if (crossOrigin !== void 0) hint.crossOrigin = crossOrigin;
if (type) hint.type = type;
if (integrity) hint.integrity = integrity;
if (referrerPolicy) hint.referrerPolicy = referrerPolicy;
if (fetchPriority) hint.fetchPriority = fetchPriority;
}
function linkAttrsToEarlyHint(attrs) {
const href = getStringAttr(attrs, "href");
const rel = getStringAttr(attrs, "rel");
if (!href || !rel) return void 0;
const relTokens = rel.split(/\s+/);
let hintRel;
let hintAs;
if (relTokens.includes("modulepreload")) {
hintRel = "modulepreload";
hintAs = "script";
} else if (relTokens.includes("stylesheet")) {
hintRel = "preload";
hintAs = "style";
} else if (relTokens.includes("preload")) {
hintAs = getPreloadAs(attrs);
if (!hintAs) return void 0;
hintRel = "preload";
} else if (relTokens.includes("preconnect")) {
hintRel = "preconnect";
hintAs = void 0;
} else if (relTokens.includes("dns-prefetch")) {
hintRel = "dns-prefetch";
hintAs = void 0;
}
if (!hintRel) return void 0;
const hint = {
href,
rel: hintRel
};
if (hintAs) hint.as = hintAs;
addEarlyHintFetchAttrs(hint, attrs);
return hint;
}
function collectStaticHintsFromManifest(manifest, matchedRoutes) {
const hints = [];
for (const route of matchedRoutes) {
const routeManifest = manifest.routes[route.id];
if (!routeManifest) continue;
for (const link of routeManifest.preloads ?? []) {
const { href, crossOrigin } = resolveManifestAssetLink(link);
const hint = {
href,
rel: "modulepreload",
as: "script"
};
if (crossOrigin !== void 0) hint.crossOrigin = crossOrigin;
hints.push(hint);
}
for (const asset of routeManifest.assets ?? []) {
if (asset.tag !== "link") continue;
const stylesheetHref = getStylesheetHref(asset);
if (stylesheetHref) {
if (manifest.inlineCss?.styles[stylesheetHref] !== void 0) continue;
const hint = {
href: stylesheetHref,
rel: "preload",
as: "style"
};
addEarlyHintFetchAttrs(hint, asset.attrs);
hints.push(hint);
continue;
}
const hint = linkAttrsToEarlyHint(asset.attrs);
if (hint) hints.push(hint);
}
}
return hints;
}
function collectDynamicHintsFromMatches(matches) {
const hints = [];
for (const match of matches) {
const links = match.links;
if (!Array.isArray(links)) continue;
for (const link of links) {
const hint = linkAttrsToEarlyHint(link);
if (hint) hints.push(hint);
}
}
return hints;
}
function createEarlyHintsEvent(opts) {
const nextHints = [];
const nextLinks = [];
for (const hint of opts.hints) {
const link = serializeEarlyHint(hint);
if (opts.sentLinks.has(link)) continue;
opts.sentLinks.add(link);
opts.sentHints.push(hint);
nextHints.push(hint);
nextLinks.push(link);
}
if (!nextHints.length && opts.phase !== "dynamic") return void 0;
return {
phase: opts.phase,
hints: nextHints,
links: nextLinks,
allHints: opts.sentHints.slice(),
allLinks: Array.from(opts.sentLinks)
};
}
function createResponseLinkHeaderEntries(opts) {
for (const hint of opts.hints) {
const link = serializeEarlyHint(hint);
if (opts.sentLinks.has(link)) continue;
opts.sentLinks.add(link);
opts.entries.push({
phase: opts.phase,
hint,
link
});
}
}
function getResponseLinkHeaderEntries(opts) {
if (!opts.filter) return opts.entries.map((entry) => entry.link);
try {
const links = [];
for (const entry of opts.entries) if (opts.filter(entry)) links.push(entry.link);
return links;
} catch (err) {
console.error("Error filtering response Link headers:", err);
return [];
}
}
var ServerFunctionSerializationAdapter = createSerializationAdapter({
key: "$TSS/serverfn",
test: (v) => {
if (typeof v !== "function") return false;
if (!(TSS_SERVER_FUNCTION in v)) return false;
return !!v[TSS_SERVER_FUNCTION];
},
toSerializable: ({ serverFnMeta }) => ({ functionId: serverFnMeta.id }),
fromSerializable: ({ functionId }) => {
const fn = async (opts, signal) => {
return (await (await getServerFnById(functionId, { origin: "client" }))(opts ?? {}, signal)).result;
};
return fn;
}
});
function getStartResponseHeaders(opts) {
return mergeHeaders({ "Content-Type": "text/html; charset=utf-8" }, ...opts.router.stores.matches.get().map((match) => {
return match.headers;
}));
}
function notifyEarlyHints(phase, event, onEarlyHints) {
try {
const result = onEarlyHints(event);
if (result) Promise.resolve(result).catch((err) => {
console.error(`Error sending ${phase} early hints:`, err);
});
} catch (err) {
console.error(`Error sending ${phase} early hints:`, err);
}
}
function getResponseLinkHeaderFilter(responseLinkHeader) {
if (typeof responseLinkHeader !== "object") return;
return responseLinkHeader.filter;
}
function appendResponseLinkHeaders(opts) {
if (!opts.filter) {
for (const entry of opts.entries) opts.responseHeaders.append("Link", entry.link);
return;
}
const links = getResponseLinkHeaderEntries(opts);
for (const link of links) opts.responseHeaders.append("Link", link);
}
function collectResponseLinkHeaderEntries(opts) {
for (let index = 0; index < opts.event.hints.length; index++) opts.entries.push({
phase: opts.phase,
hint: opts.event.hints[index],
link: opts.event.links[index]
});
}
function handleCollectedEarlyHints(opts) {
const event = opts.onEarlyHints ? createEarlyHintsEvent({
phase: opts.phase,
hints: opts.hints,
sentLinks: opts.sentLinks,
sentHints: opts.sentHints
}) : void 0;
if (event) notifyEarlyHints(opts.phase, event, opts.onEarlyHints);
if (!opts.responseLinkHeaderEntries) return;
if (event) {
collectResponseLinkHeaderEntries({
phase: opts.phase,
event,
entries: opts.responseLinkHeaderEntries
});
return;
}
createResponseLinkHeaderEntries({
phase: opts.phase,
hints: opts.hints,
sentLinks: opts.sentLinks,
entries: opts.responseLinkHeaderEntries
});
}
var entriesPromise;
var baseManifestPromise;
/**
* Cached final manifest (with client entry script tag). In production,
* this is computed once and reused for every request when caching is enabled.
*/
var cachedFinalManifestPromise;
async function loadEntries() {
const [routerEntry, startEntry, pluginAdapters] = await Promise.all([
import("./router-DCfyUfNl.mjs"),
import("./start-DkBWIoco.mjs"),
import("../__23tanstack-start-plugin-adapters-y_fshQDY.mjs")
]);
return {
routerEntry,
startEntry,
pluginAdapters
};
}
function getEntries() {
if (!entriesPromise) entriesPromise = loadEntries();
return entriesPromise;
}
/**
* Returns the raw manifest data (without client entry script tag baked in).
* In dev mode, always returns fresh data. In prod, cached.
*/
function getBaseManifest(matchedRoutes) {
if (!baseManifestPromise) baseManifestPromise = getStartManifest();
return baseManifestPromise;
}
/**
* Resolves a final Manifest for a given request.
*
* - No transform: builds client entry script tag and returns (cached in prod).
* - Cached transform: transforms all URLs + builds script tag, caches result.
* - Per-request transform: deep-clones base manifest, transforms per-request.
*/
async function resolveManifest(matchedRoutes, transformFn, cache) {
const base = await getBaseManifest(matchedRoutes);
const computeFinalManifest = async () => {
return transformFn ? await transformManifestAssets(base, transformFn, { clone: !cache }) : buildManifestWithClientEntry(base);
};
if (!transformFn || cache) {
if (!cachedFinalManifestPromise) cachedFinalManifestPromise = computeFinalManifest();
return cachedFinalManifestPromise;
}
return computeFinalManifest();
}
var ROUTER_BASEPATH = "/";
var SERVER_FN_BASE = "/_serverFn/";
var IS_PRERENDERING = process.env.TSS_PRERENDERING === "true";
var IS_SHELL_ENV = process.env.TSS_SHELL === "true";
var IS_DEV = false;
var ERR_NO_RESPONSE = IS_DEV ? `It looks like you forgot to return a response from your server route handler. If you want to defer to the app router, make sure to have a component set in this route.` : "Internal Server Error";
var ERR_NO_DEFER = IS_DEV ? `You cannot defer to the app router if there is no component defined on this route.` : "Internal Server Error";
function throwRouteHandlerError() {
throw new Error(ERR_NO_RESPONSE);
}
function throwIfMayNotDefer() {
throw new Error(ERR_NO_DEFER);
}
/**
* Check if a value is a special response (Response or Redirect)
*/
function isSpecialResponse(value) {
return value instanceof Response || isRedirect(value);
}
/**
* Normalize middleware result to context shape
*/
function handleCtxResult(result) {
if (isSpecialResponse(result)) return { response: result };
return result;
}
/**
* Execute a middleware chain
*/
function executeMiddleware(middlewares, ctx) {
let index = -1;
const next = async (nextCtx) => {
if (nextCtx) {
if (nextCtx.context) ctx.context = safeObjectMerge(ctx.context, nextCtx.context);
for (const key of Object.keys(nextCtx)) if (key !== "context") ctx[key] = nextCtx[key];
}
index++;
const middleware = middlewares[index];
if (!middleware) return ctx;
let result;
try {
result = await middleware({
...ctx,
next
});
} catch (err) {
if (isSpecialResponse(err)) {
ctx.response = err;
return ctx;
}
throw err;
}
const normalized = handleCtxResult(result);
if (normalized) {
if (normalized.response !== void 0) ctx.response = normalized.response;
if (normalized.context) ctx.context = safeObjectMerge(ctx.context, normalized.context);
}
return ctx;
};
return next();
}
/**
* Wrap a route handler as middleware
*/
function handlerToMiddleware(handler, mayDefer = false) {
if (mayDefer) return handler;
return async (ctx) => {
const response = await handler({
...ctx,
next: throwIfMayNotDefer
});
if (!response) throwRouteHandlerError();
return response;
};
}
/**
* Creates the TanStack Start request handler.
*
* @example Backwards-compatible usage (handler callback only):
* ```ts
* export default createStartHandler(defaultStreamHandler)
* ```
*
* @example With CDN URL rewriting:
* ```ts
* export default createStartHandler({
* handler: defaultStreamHandler,
* transformAssets: 'https://cdn.example.com',
* })
* ```
*
* @example With per-request URL rewriting:
* ```ts
* export default createStartHandler({
* handler: defaultStreamHandler,
* transformAssets: {
* transform: ({ url }) => {
* const cdnBase = getRequest().headers.get('x-cdn-base') || ''
* return { href: `${cdnBase}${url}` }
* },
* cache: false,
* },
* })
* ```
*/
function createStartHandler(cbOrOptions) {
const cb = typeof cbOrOptions === "function" ? cbOrOptions : cbOrOptions.handler;
const transformAssetsOption = typeof cbOrOptions === "function" ? void 0 : cbOrOptions.transformAssets;
const transformAssetUrlsOption = typeof cbOrOptions === "function" ? void 0 : cbOrOptions.transformAssetUrls;
const transformOption = transformAssetsOption !== void 0 ? resolveTransformAssetsConfig(transformAssetsOption) : transformAssetUrlsOption !== void 0 ? resolveTransformAssetsConfig(adaptTransformAssetUrlsConfigToTransformAssets(transformAssetUrlsOption)) : void 0;
const warmupTransformManifest = !!transformAssetsOption && typeof transformAssetsOption === "object" && "warmup" in transformAssetsOption && transformAssetsOption.warmup === true || !!transformAssetUrlsOption && typeof transformAssetUrlsOption === "object" && transformAssetUrlsOption.warmup === true;
const resolvedTransformConfig = transformOption;
const cache = resolvedTransformConfig ? resolvedTransformConfig.cache : true;
const shouldCacheCreateTransform = cache && true;
let cachedCreateTransformPromise;
const getTransformFn = async (opts) => {
if (!resolvedTransformConfig) return void 0;
if (resolvedTransformConfig.type === "createTransform") {
if (shouldCacheCreateTransform) {
if (!cachedCreateTransformPromise) cachedCreateTransformPromise = Promise.resolve(resolvedTransformConfig.createTransform(opts)).catch((error) => {
cachedCreateTransformPromise = void 0;
throw error;
});
return cachedCreateTransformPromise;
}
return resolvedTransformConfig.createTransform(opts);
}
return resolvedTransformConfig.transformFn;
};
if (warmupTransformManifest && cache && !cachedFinalManifestPromise) {
const warmupPromise = (async () => {
const base = await getBaseManifest(void 0);
const transformFn = await getTransformFn({ warmup: true });
return transformFn ? await transformManifestAssets(base, transformFn, { clone: false }) : buildManifestWithClientEntry(base);
})();
cachedFinalManifestPromise = warmupPromise;
warmupPromise.catch(() => {
if (cachedFinalManifestPromise === warmupPromise) cachedFinalManifestPromise = void 0;
cachedCreateTransformPromise = void 0;
});
}
const startRequestResolver = async (request, requestOpts) => {
let router = null;
let cbWillCleanup = false;
try {
const { url, handledProtocolRelativeURL } = getNormalizedURL(request.url);
const href = url.pathname + url.search + url.hash;
const origin = getOrigin(request);
if (handledProtocolRelativeURL) return Response.redirect(url, 308);
const entries = await getEntries();
const startOptions = await entries.startEntry.startInstance?.getOptions() || {};
const { hasPluginAdapters, pluginSerializationAdapters } = entries.pluginAdapters;
const serializationAdapters = [
...startOptions.serializationAdapters || [],
...hasPluginAdapters ? pluginSerializationAdapters : [],
ServerFunctionSerializationAdapter
];
const requestStartOptions = {
...startOptions,
serializationAdapters
};
const flattenedRequestMiddlewares = startOptions.requestMiddleware ? flattenMiddlewares(startOptions.requestMiddleware) : [];
const executedRequestMiddlewares = new Set(flattenedRequestMiddlewares);
const getRouter = async () => {
if (router) return router;
router = await entries.routerEntry.getRouter();
let isShell = IS_SHELL_ENV;
if (IS_PRERENDERING && !isShell) isShell = request.headers.get(HEADERS.TSS_SHELL) === "true";
const history = createMemoryHistory({ initialEntries: [href] });
router.update({
history,
isShell,
isPrerendering: IS_PRERENDERING,
origin: router.options.origin ?? origin,
defaultSsr: requestStartOptions.defaultSsr,
serializationAdapters: [...requestStartOptions.serializationAdapters, ...router.options.serializationAdapters || []],
basepath: ROUTER_BASEPATH
});
return router;
};
if (SERVER_FN_BASE && url.pathname.startsWith(SERVER_FN_BASE)) {
const serverFnId = url.pathname.slice(SERVER_FN_BASE.length).split("/")[0];
if (!serverFnId) throw new Error("Invalid server action param for serverFnId");
const serverFnHandler = async ({ context }) => {
return runWithStartContext({
getRouter,
startOptions: requestStartOptions,
contextAfterGlobalMiddlewares: context,
request,
executedRequestMiddlewares,
handlerType: "serverFn"
}, () => handleServerAction({
request,
context: requestOpts?.context,
serverFnId
}));
};
return handleRedirectResponse((await executeMiddleware([...flattenedRequestMiddlewares.map((d) => d.options.server), serverFnHandler], {
request,
pathname: url.pathname,
context: createNullProtoObject(requestOpts?.context)
})).response, request, getRouter);
}
const executeRouter = async (serverContext, matchedRoutes) => {
const acceptParts = (request.headers.get("Accept") || "*/*").split(",");
if (!["*/*", "text/html"].some((mimeType) => acceptParts.some((part) => part.trim().startsWith(mimeType)))) return Response.json({ error: "Only HTML requests are supported here" }, { status: 500 });
const manifest = await resolveManifest(matchedRoutes, await getTransformFn({
warmup: false,
request
}), cache);
const onEarlyHints = requestOpts?.onEarlyHints;
const responseLinkHeader = requestOpts?.responseLinkHeader;
const shouldCollectEarlyHints = !!onEarlyHints || !!responseLinkHeader;
const sentEarlyHintLinks = shouldCollectEarlyHints ? /* @__PURE__ */ new Set() : void 0;
const sentEarlyHints = onEarlyHints ? new Array() : void 0;
const responseLinkHeaderEntries = shouldCollectEarlyHints && responseLinkHeader ? new Array() : void 0;
const responseLinkHeaderFilter = shouldCollectEarlyHints ? getResponseLinkHeaderFilter(responseLinkHeader) : void 0;
if (shouldCollectEarlyHints && sentEarlyHintLinks && matchedRoutes?.length) handleCollectedEarlyHints({
phase: "static",
hints: collectStaticHintsFromManifest(manifest, matchedRoutes),
sentLinks: sentEarlyHintLinks,
sentHints: sentEarlyHints,
onEarlyHints,
responseLinkHeaderEntries
});
const routerInstance = await getRouter();
attachRouterServerSsrUtils({
router: routerInstance,
manifest,
getRequestAssets: () => getStartContext({ throwIfNotFound: false })?.requestAssets,
includeUnmatchedRouteAssets: false
});
routerInstance.update({ additionalContext: { serverContext } });
await routerInstance.load();
if (routerInstance.state.redirect) return routerInstance.state.redirect;
if (shouldCollectEarlyHints && sentEarlyHintLinks) handleCollectedEarlyHints({
phase: "dynamic",
hints: collectDynamicHintsFromMatches(routerInstance.stores.matches.get()),
sentLinks: sentEarlyHintLinks,
sentHints: sentEarlyHints,
onEarlyHints,
responseLinkHeaderEntries
});
const ctx = getStartContext({ throwIfNotFound: false });
await routerInstance.serverSsr.dehydrate({ requestAssets: ctx?.requestAssets });
const responseHeaders = getStartResponseHeaders({ router: routerInstance });
if (responseLinkHeaderEntries?.length) appendResponseLinkHeaders({
responseHeaders,
entries: responseLinkHeaderEntries,
filter: responseLinkHeaderFilter
});
cbWillCleanup = true;
return cb({
request,
router: routerInstance,
responseHeaders
});
};
const requestHandlerMiddleware = async ({ context }) => {
return runWithStartContext({
getRouter,
startOptions: requestStartOptions,
contextAfterGlobalMiddlewares: context,
request,
executedRequestMiddlewares,
handlerType: "router"
}, async () => {
try {
return await handleServerRoutes({
getRouter,
request,
url,
executeRouter,
context,
executedRequestMiddlewares
});
} catch (err) {
if (err instanceof Response) return err;
throw err;
}
});
};
return handleRedirectResponse((await executeMiddleware([...flattenedRequestMiddlewares.map((d) => d.options.server), requestHandlerMiddleware], {
request,
pathname: url.pathname,
context: createNullProtoObject(requestOpts?.context)
})).response, request, getRouter);
} finally {
if (router && !cbWillCleanup) router.serverSsr?.cleanup();
router = null;
}
};
return requestHandler(startRequestResolver);
}
async function handleRedirectResponse(response, request, getRouter) {
if (!isRedirect(response)) return response;
if (isResolvedRedirect(response)) {
if (request.headers.get("x-tsr-serverFn") === "true") return Response.json({
...response.options,
isSerializedRedirect: true
}, { headers: response.headers });
return response;
}
const opts = response.options;
if (opts.to && typeof opts.to === "string" && !opts.to.startsWith("/")) throw new Error(`Server side redirects must use absolute paths via the 'href' or 'to' options. The redirect() method's "to" property accepts an internal path only. Use the "href" property to provide an external URL. Received: ${JSON.stringify(opts)}`);
if ([
"params",
"search",
"hash"
].some((d) => typeof opts[d] === "function")) throw new Error(`Server side redirects must use static search, params, and hash values and do not support functional values. Received functional values for: ${Object.keys(opts).filter((d) => typeof opts[d] === "function").map((d) => `"${d}"`).join(", ")}`);
const redirect = (await getRouter()).resolveRedirect(response);
if (request.headers.get("x-tsr-serverFn") === "true") return Response.json({
...response.options,
isSerializedRedirect: true
}, { headers: response.headers });
return redirect;
}
async function handleServerRoutes({ getRouter, request, url, executeRouter, context, executedRequestMiddlewares }) {
const router = await getRouter();
const pathname = executeRewriteInput(router.rewrite, url).pathname;
const { matchedRoutes, foundRoute, routeParams } = router.getMatchedRoutes(pathname);
const isExactMatch = foundRoute && routeParams["**"] === void 0;
const routeMiddlewares = [];
for (const route of matchedRoutes) {
const serverMiddleware = route.options.server?.middleware;
if (serverMiddleware) {
const flattened = flattenMiddlewares(serverMiddleware);
for (const m of flattened) if (!executedRequestMiddlewares.has(m)) routeMiddlewares.push(m.options.server);
}
}
const server = foundRoute?.options.server;
let isHeadFallback = false;
if (server?.handlers && isExactMatch) {
const handlers = typeof server.handlers === "function" ? server.handlers({ createHandlers: (d) => d }) : server.handlers;
const requestMethod = request.method.toUpperCase();
const handler = requestMethod === "HEAD" ? handlers["HEAD"] ?? handlers["GET"] ?? handlers["ANY"] : handlers[requestMethod] ?? handlers["ANY"];
isHeadFallback = requestMethod === "HEAD" && handler !== void 0 && !handlers["HEAD"];
if (handler) {
const mayDefer = !!foundRoute.options.component;
if (typeof handler === "function") routeMiddlewares.push(handlerToMiddleware(handler, mayDefer));
else {
if (handler.middleware?.length) {
const handlerMiddlewares = flattenMiddlewares(handler.middleware);
for (const m of handlerMiddlewares) routeMiddlewares.push(m.options.server);
}
if (handler.handler) routeMiddlewares.push(handlerToMiddleware(handler.handler, mayDefer));
}
}
}
routeMiddlewares.push((ctx) => executeRouter(ctx.context, matchedRoutes));
const ctx = await executeMiddleware(routeMiddlewares, {
request,
context,
params: routeParams,
pathname
});
if (isHeadFallback) {
if (!ctx.response) throwRouteHandlerError();
const resolved = await handleRedirectResponse(ctx.response, request, getRouter);
return new Response(null, resolved);
}
return ctx.response;
}
var fetch = createStartHandler(defaultStreamHandler);
function createServerEntry(entry) {
return { async fetch(...args) {
return await entry.fetch(...args);
} };
}
var server_default = createServerEntry({ fetch });
//#endregion
export { createServerEntry, server_default as default };