@tanstack/start-server-core 1.120.7 → 1.121.0-alpha.11

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 (60) hide show
  1. package/README.md +6 -27
  2. package/dist/cjs/createRequestHandler.cjs +1 -3
  3. package/dist/cjs/createRequestHandler.cjs.map +1 -1
  4. package/dist/cjs/createStartHandler.cjs +268 -31
  5. package/dist/cjs/createStartHandler.cjs.map +1 -1
  6. package/dist/cjs/createStartHandler.d.cts +8 -6
  7. package/dist/cjs/h3.cjs +30 -73
  8. package/dist/cjs/h3.cjs.map +1 -1
  9. package/dist/cjs/h3.d.cts +11 -7
  10. package/dist/cjs/handlerCallback.cjs.map +1 -1
  11. package/dist/cjs/handlerCallback.d.cts +3 -4
  12. package/dist/cjs/index.cjs +17 -14
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs/index.d.cts +6 -1
  15. package/dist/cjs/router-manifest.cjs +44 -0
  16. package/dist/cjs/router-manifest.cjs.map +1 -0
  17. package/dist/cjs/router-manifest.d.cts +17 -0
  18. package/dist/cjs/server-functions-handler.cjs +145 -0
  19. package/dist/cjs/server-functions-handler.cjs.map +1 -0
  20. package/dist/cjs/server-functions-handler.d.cts +3 -0
  21. package/dist/cjs/serverRoute.cjs +100 -0
  22. package/dist/cjs/serverRoute.cjs.map +1 -0
  23. package/dist/cjs/serverRoute.d.cts +115 -0
  24. package/dist/cjs/ssr-server.cjs +3 -2
  25. package/dist/cjs/ssr-server.cjs.map +1 -1
  26. package/dist/esm/createRequestHandler.js +1 -3
  27. package/dist/esm/createRequestHandler.js.map +1 -1
  28. package/dist/esm/createStartHandler.d.ts +8 -6
  29. package/dist/esm/createStartHandler.js +248 -33
  30. package/dist/esm/createStartHandler.js.map +1 -1
  31. package/dist/esm/h3.d.ts +11 -7
  32. package/dist/esm/h3.js +26 -63
  33. package/dist/esm/h3.js.map +1 -1
  34. package/dist/esm/handlerCallback.d.ts +3 -4
  35. package/dist/esm/handlerCallback.js.map +1 -1
  36. package/dist/esm/index.d.ts +6 -1
  37. package/dist/esm/index.js +14 -5
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/router-manifest.d.ts +17 -0
  40. package/dist/esm/router-manifest.js +44 -0
  41. package/dist/esm/router-manifest.js.map +1 -0
  42. package/dist/esm/server-functions-handler.d.ts +3 -0
  43. package/dist/esm/server-functions-handler.js +145 -0
  44. package/dist/esm/server-functions-handler.js.map +1 -0
  45. package/dist/esm/serverRoute.d.ts +115 -0
  46. package/dist/esm/serverRoute.js +100 -0
  47. package/dist/esm/serverRoute.js.map +1 -0
  48. package/dist/esm/ssr-server.js +3 -2
  49. package/dist/esm/ssr-server.js.map +1 -1
  50. package/package.json +8 -6
  51. package/src/createRequestHandler.ts +1 -3
  52. package/src/createStartHandler.ts +373 -47
  53. package/src/h3.ts +71 -78
  54. package/src/handlerCallback.ts +5 -12
  55. package/src/index.tsx +11 -1
  56. package/src/router-manifest.ts +79 -0
  57. package/src/server-functions-handler.ts +260 -0
  58. package/src/serverRoute.ts +661 -0
  59. package/src/ssr-server.ts +2 -0
  60. package/src/tanstack-start.d.ts +5 -0
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const routerCore = require("@tanstack/router-core");
4
+ function createServerFileRoute(__) {
5
+ return createServerRoute();
6
+ }
7
+ function createServerRoute(__, __opts) {
8
+ const options = __opts || {};
9
+ const route = {
10
+ path: "",
11
+ id: "",
12
+ fullPath: "",
13
+ to: "",
14
+ options,
15
+ parentRoute: void 0,
16
+ _types: {},
17
+ // children: undefined as TChildren,
18
+ middleware: (middlewares) => createServerRoute(void 0, {
19
+ ...options,
20
+ middleware: middlewares
21
+ }),
22
+ methods: (methodsOrGetMethods) => {
23
+ const methods = (() => {
24
+ if (typeof methodsOrGetMethods === "function") {
25
+ return methodsOrGetMethods(createMethodBuilder());
26
+ }
27
+ return methodsOrGetMethods;
28
+ })();
29
+ return createServerRoute(void 0, {
30
+ ...__opts,
31
+ methods
32
+ });
33
+ },
34
+ update: (opts) => createServerRoute(void 0, {
35
+ ...options,
36
+ ...opts
37
+ }),
38
+ init: (opts) => {
39
+ var _a;
40
+ options.originalIndex = opts.originalIndex;
41
+ const isRoot = !options.path && !options.id;
42
+ route.parentRoute = (_a = options.getParentRoute) == null ? void 0 : _a.call(options);
43
+ if (isRoot) {
44
+ route.path = routerCore.rootRouteId;
45
+ } else if (!route.parentRoute) {
46
+ throw new Error(
47
+ `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a ServerRoute instance.`
48
+ );
49
+ }
50
+ let path = isRoot ? routerCore.rootRouteId : options.path;
51
+ if (path && path !== "/") {
52
+ path = routerCore.trimPathLeft(path);
53
+ }
54
+ const customId = options.id || path;
55
+ let id = isRoot ? routerCore.rootRouteId : routerCore.joinPaths([
56
+ route.parentRoute.id === routerCore.rootRouteId ? "" : route.parentRoute.id,
57
+ customId
58
+ ]);
59
+ if (path === routerCore.rootRouteId) {
60
+ path = "/";
61
+ }
62
+ if (id !== routerCore.rootRouteId) {
63
+ id = routerCore.joinPaths(["/", id]);
64
+ }
65
+ const fullPath = id === routerCore.rootRouteId ? "/" : routerCore.joinPaths([route.parentRoute.fullPath, path]);
66
+ route.path = path;
67
+ route.id = id;
68
+ route.fullPath = fullPath;
69
+ route.to = fullPath;
70
+ },
71
+ _addFileChildren: (children) => {
72
+ if (Array.isArray(children)) {
73
+ route.children = children;
74
+ }
75
+ if (typeof children === "object" && children !== null) {
76
+ route.children = Object.values(children);
77
+ }
78
+ return route;
79
+ },
80
+ _addFileTypes: () => route
81
+ };
82
+ return route;
83
+ }
84
+ const createMethodBuilder = (__opts) => {
85
+ return {
86
+ _options: __opts || {},
87
+ _types: {},
88
+ middleware: (middlewares) => createMethodBuilder({
89
+ ...__opts,
90
+ middlewares
91
+ }),
92
+ handler: (handler) => createMethodBuilder({
93
+ ...__opts,
94
+ handler
95
+ })
96
+ };
97
+ };
98
+ exports.createServerFileRoute = createServerFileRoute;
99
+ exports.createServerRoute = createServerRoute;
100
+ //# sourceMappingURL=serverRoute.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serverRoute.cjs","sources":["../../src/serverRoute.ts"],"sourcesContent":["import { joinPaths, rootRouteId, trimPathLeft } from '@tanstack/router-core'\nimport type {\n Assign,\n Constrain,\n Expand,\n ResolveParams,\n RouteConstraints,\n TrimPathRight,\n} from '@tanstack/router-core'\nimport type {\n AnyRequestMiddleware,\n AssignAllServerContext,\n} from '@tanstack/start-client-core'\n\ntype TODO = any\n\nexport function createServerFileRoute<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TChildren,\n>(__?: never): ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren> {\n return createServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>(\n undefined,\n )\n}\n\nexport interface ServerRouteOptions<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TMiddlewares,\n> {\n id: TId\n path: TPath\n pathname: TFullPath\n originalIndex: number\n getParentRoute?: () => TParentRoute\n middleware: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>\n methods: Record<\n string,\n ServerRouteMethodHandlerFn<TParentRoute, TFullPath, TMiddlewares, any, any>\n >\n caseSensitive?: boolean\n}\n\nexport type ServerRouteManifest = {\n middleware: boolean\n methods: Record<string, { middleware: boolean }>\n}\n\nexport function createServerRoute<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TChildren,\n>(\n __?: never,\n __opts?: Partial<\n ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, undefined>\n >,\n): ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren> {\n const options = __opts || {}\n\n const route: ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren> = {\n path: '' as TPath,\n id: '' as TId,\n fullPath: '' as TFullPath,\n to: '' as TrimPathRight<TFullPath>,\n options: options as TODO,\n parentRoute: undefined as unknown as TParentRoute,\n _types: {} as TODO,\n // children: undefined as TChildren,\n middleware: (middlewares: TODO) =>\n createServerRoute(undefined, {\n ...options,\n middleware: middlewares,\n }) as TODO,\n methods: (methodsOrGetMethods: TODO) => {\n const methods = (() => {\n if (typeof methodsOrGetMethods === 'function') {\n return methodsOrGetMethods(createMethodBuilder())\n }\n\n return methodsOrGetMethods\n })()\n\n return createServerRoute(undefined, {\n ...__opts,\n methods,\n }) as TODO\n },\n update: (opts) =>\n createServerRoute(undefined, {\n ...options,\n ...opts,\n }) as TODO,\n init: (opts: { originalIndex: number }): void => {\n options.originalIndex = opts.originalIndex\n\n const isRoot = !options.path && !options.id\n\n route.parentRoute = options.getParentRoute?.() as TParentRoute\n\n if (isRoot) {\n route.path = rootRouteId as TPath\n } else if (!(route.parentRoute as any)) {\n throw new Error(\n `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a ServerRoute instance.`,\n )\n }\n\n let path: undefined | string = isRoot ? rootRouteId : options.path\n\n // If the path is anything other than an index path, trim it up\n if (path && path !== '/') {\n path = trimPathLeft(path)\n }\n\n const customId = options.id || path\n\n // Strip the parentId prefix from the first level of children\n let id = isRoot\n ? rootRouteId\n : joinPaths([\n route.parentRoute.id === rootRouteId ? '' : route.parentRoute.id,\n customId,\n ])\n\n if (path === rootRouteId) {\n path = '/'\n }\n\n if (id !== rootRouteId) {\n id = joinPaths(['/', id])\n }\n\n const fullPath =\n id === rootRouteId ? '/' : joinPaths([route.parentRoute.fullPath, path])\n\n route.path = path as TPath\n route.id = id as TId\n route.fullPath = fullPath as TFullPath\n route.to = fullPath as TrimPathRight<TFullPath>\n },\n\n _addFileChildren: (children) => {\n if (Array.isArray(children)) {\n route.children = children as TChildren as TODO\n }\n\n if (typeof children === 'object' && children !== null) {\n route.children = Object.values(children) as TChildren as TODO\n }\n\n return route as any\n },\n\n _addFileTypes: <TFileTypes>() => route,\n }\n\n return route\n}\n\nexport type ServerRouteAddFileChildrenFn<\n in out TParentRoute extends AnyServerRouteWithTypes,\n in out TId extends RouteConstraints['TId'],\n in out TPath extends RouteConstraints['TPath'],\n in out TFullPath extends RouteConstraints['TFullPath'],\n in out TMiddlewares,\n in out TMethods,\n in out TChildren,\n> = (\n children: TChildren,\n) => ServerRouteWithTypes<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TMethods,\n TChildren\n>\n\nconst createMethodBuilder = <\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n>(\n __opts?: TODO,\n): ServerRouteMethodBuilder<TParentRoute, TFullPath, TMiddlewares> => {\n return {\n _options: __opts || {},\n _types: {} as TODO,\n middleware: (middlewares) =>\n createMethodBuilder({\n ...__opts,\n middlewares,\n }) as TODO,\n handler: (handler) =>\n createMethodBuilder({\n ...__opts,\n handler,\n }) as TODO,\n }\n}\n\nexport type CreateServerFileRoute<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TChildren,\n> = (\n options?: undefined,\n) => ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>\n\nexport type AnyServerRouteWithTypes = ServerRouteWithTypes<\n any,\n any,\n any,\n any,\n any,\n any,\n any\n>\n\nexport interface ServerRouteWithTypes<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TMiddlewares,\n TMethods,\n TChildren,\n> {\n _types: ServerRouteTypes<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TMethods\n >\n path: TPath\n id: TId\n fullPath: TFullPath\n to: TrimPathRight<TFullPath>\n parentRoute: TParentRoute\n children?: TChildren\n options: ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, TMiddlewares>\n update: (\n opts: ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, undefined>,\n ) => ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>\n init: (opts: { originalIndex: number }) => void\n _addFileChildren: ServerRouteAddFileChildrenFn<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TMethods,\n TChildren\n >\n _addFileTypes: () => ServerRouteWithTypes<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TMethods,\n TChildren\n >\n}\n\nexport interface ServerRouteTypes<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TMiddlewares,\n TMethods,\n> {\n id: TId\n path: TPath\n fullPath: TFullPath\n middlewares: TMiddlewares\n methods: TMethods\n parentRoute: TParentRoute\n allContext: ResolveAllServerContext<TParentRoute, TMiddlewares>\n}\n\nexport type ResolveAllServerContext<\n TParentRoute extends AnyServerRouteWithTypes,\n TMiddlewares,\n> = unknown extends TParentRoute\n ? AssignAllServerContext<TMiddlewares>\n : Assign<\n TParentRoute['_types']['allContext'],\n AssignAllServerContext<TMiddlewares>\n >\n\nexport type AnyServerRoute = AnyServerRouteWithTypes\n\nexport interface ServerRoute<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TChildren,\n> extends ServerRouteWithTypes<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n undefined,\n undefined,\n TChildren\n >,\n ServerRouteMiddleware<TParentRoute, TId, TPath, TFullPath, TChildren>,\n ServerRouteMethods<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n undefined,\n TChildren\n > {}\n\nexport interface ServerRouteMiddleware<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TChildren,\n> {\n middleware: <const TNewMiddleware>(\n middleware: Constrain<TNewMiddleware, ReadonlyArray<AnyRequestMiddleware>>,\n ) => ServerRouteAfterMiddleware<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TNewMiddleware,\n TChildren\n >\n}\n\nexport interface ServerRouteAfterMiddleware<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TMiddlewares,\n TChildren,\n> extends ServerRouteWithTypes<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n undefined,\n TChildren\n >,\n ServerRouteMethods<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TChildren\n > {}\n\nexport interface ServerRouteMethods<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TMiddlewares,\n TChildren,\n> {\n methods: <const TMethods>(\n methodsOrGetMethods: Constrain<\n TMethods,\n ServerRouteMethodsOptions<TParentRoute, TFullPath, TMiddlewares>\n >,\n ) => ServerRouteAfterMethods<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TMethods,\n TChildren\n >\n}\n\nexport type ServerRouteMethodsOptions<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n> =\n | ServerRouteMethodsRecord<TParentRoute, TFullPath, TMiddlewares>\n | ((\n api: ServerRouteMethodBuilder<TParentRoute, TFullPath, TMiddlewares>,\n ) => ServerRouteMethodsRecord<TParentRoute, TFullPath, TMiddlewares>)\n\nexport interface ServerRouteMethodsRecord<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n> {\n GET?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n POST?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n PUT?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n PATCH?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n DELETE?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n OPTIONS?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n HEAD?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>\n}\n\nexport type ServerRouteMethodRecordValue<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n> =\n | ServerRouteMethodHandlerFn<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n undefined,\n any\n >\n | AnyRouteMethodsBuilder\n\nexport type ServerRouteVerb = (typeof ServerRouteVerbs)[number]\n\nexport const ServerRouteVerbs = [\n 'GET',\n 'POST',\n 'PUT',\n 'PATCH',\n 'DELETE',\n 'OPTIONS',\n 'HEAD',\n] as const\n\nexport type ServerRouteMethodHandlerFn<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse,\n> = (\n ctx: ServerRouteMethodHandlerCtx<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares\n >,\n) => TResponse | Promise<TResponse>\n\nexport interface ServerRouteMethodHandlerCtx<\n in out TParentRoute extends AnyServerRouteWithTypes,\n in out TFullPath extends string,\n in out TMiddlewares,\n in out TMethodMiddlewares,\n> {\n context: Expand<\n AssignAllMethodContext<TParentRoute, TMiddlewares, TMethodMiddlewares>\n >\n request: Request\n params: Expand<ResolveParams<TFullPath>>\n pathname: TFullPath\n}\n\nexport type MergeMethodMiddlewares<TMiddlewares, TMethodMiddlewares> =\n TMiddlewares extends ReadonlyArray<any>\n ? TMethodMiddlewares extends ReadonlyArray<any>\n ? readonly [...TMiddlewares, ...TMethodMiddlewares]\n : TMiddlewares\n : TMethodMiddlewares\n\nexport type AssignAllMethodContext<\n TParentRoute extends AnyServerRouteWithTypes,\n TMiddlewares,\n TMethodMiddlewares,\n> = ResolveAllServerContext<\n TParentRoute,\n MergeMethodMiddlewares<TMiddlewares, TMethodMiddlewares>\n>\n\nexport type AnyRouteMethodsBuilder = ServerRouteMethodBuilderWithTypes<\n any,\n any,\n any,\n any\n>\n\nexport interface ServerRouteMethodBuilder<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n> extends ServerRouteMethodBuilderWithTypes<\n TFullPath,\n TMiddlewares,\n undefined,\n undefined\n >,\n ServerRouteMethodBuilderMiddleware<TParentRoute, TFullPath, TMiddlewares>,\n ServerRouteMethodBuilderHandler<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n undefined\n > {}\n\nexport interface ServerRouteMethodBuilderWithTypes<\n TFullPath extends string,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse,\n> {\n _options: TODO\n _types: ServerRouteMethodBuilderTypes<\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse\n >\n}\n\nexport interface ServerRouteMethodBuilderTypes<\n in out TFullPath extends string,\n in out TMiddlewares,\n in out TMethodMiddlewares,\n in out TResponse,\n> {\n middlewares: TMiddlewares\n methodMiddleware: TMethodMiddlewares\n fullPath: TFullPath\n response: TResponse\n}\n\nexport interface ServerRouteMethodBuilderMiddleware<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n> {\n middleware: <const TNewMethodMiddlewares>(\n middleware: Constrain<\n TNewMethodMiddlewares,\n ReadonlyArray<AnyRequestMiddleware>\n >,\n ) => ServerRouteMethodBuilderAfterMiddleware<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TNewMethodMiddlewares\n >\n}\n\nexport interface ServerRouteMethodBuilderAfterMiddleware<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n TMethodMiddlewares,\n> extends ServerRouteMethodBuilderWithTypes<\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares,\n undefined\n >,\n ServerRouteMethodBuilderHandler<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares\n > {}\n\nexport interface ServerRouteMethodBuilderHandler<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n TMethodMiddlewares,\n> {\n handler: <TResponse>(\n handler: ServerRouteMethodHandlerFn<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse\n >,\n ) => ServerRouteMethodBuilderAfterHandler<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse\n >\n}\n\nexport interface ServerRouteMethodBuilderAfterHandler<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse,\n> extends ServerRouteMethodBuilderWithTypes<\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares,\n TResponse\n > {\n opts: ServerRouteMethod<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares\n >\n}\n\nexport interface ServerRouteMethod<\n TParentRoute extends AnyServerRouteWithTypes,\n TFullPath extends string,\n TMiddlewares,\n TMethodMiddlewares,\n> {\n middleware?: Constrain<TMiddlewares, Array<AnyRequestMiddleware>>\n handler?: ServerRouteMethodHandlerFn<\n TParentRoute,\n TFullPath,\n TMiddlewares,\n TMethodMiddlewares,\n undefined\n >\n}\n\nexport interface ServerRouteAfterMethods<\n TParentRoute extends AnyServerRouteWithTypes,\n TId extends RouteConstraints['TId'],\n TPath extends RouteConstraints['TPath'],\n TFullPath extends RouteConstraints['TFullPath'],\n TMiddlewares,\n TMethods,\n TChildren,\n> extends ServerRouteWithTypes<\n TParentRoute,\n TId,\n TPath,\n TFullPath,\n TMiddlewares,\n TMethods,\n TChildren\n > {\n options: ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, TMiddlewares>\n}\n"],"names":["rootRouteId","trimPathLeft","joinPaths"],"mappings":";;;AAgBO,SAAS,sBAMd,IAAyE;AAClE,SAAA,kBAEP;AACF;AA2BgB,SAAA,kBAOd,IACA,QAG6D;AACvD,QAAA,UAAU,UAAU,CAAC;AAE3B,QAAM,QAAqE;AAAA,IACzE,MAAM;AAAA,IACN,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,IAAI;AAAA,IACJ;AAAA,IACA,aAAa;AAAA,IACb,QAAQ,CAAC;AAAA;AAAA,IAET,YAAY,CAAC,gBACX,kBAAkB,QAAW;AAAA,MAC3B,GAAG;AAAA,MACH,YAAY;AAAA,IAAA,CACb;AAAA,IACH,SAAS,CAAC,wBAA8B;AACtC,YAAM,WAAW,MAAM;AACjB,YAAA,OAAO,wBAAwB,YAAY;AACtC,iBAAA,oBAAoB,qBAAqB;AAAA,QAAA;AAG3C,eAAA;AAAA,MAAA,GACN;AAEH,aAAO,kBAAkB,QAAW;AAAA,QAClC,GAAG;AAAA,QACH;AAAA,MAAA,CACD;AAAA,IACH;AAAA,IACA,QAAQ,CAAC,SACP,kBAAkB,QAAW;AAAA,MAC3B,GAAG;AAAA,MACH,GAAG;AAAA,IAAA,CACJ;AAAA,IACH,MAAM,CAAC,SAA0C;;AAC/C,cAAQ,gBAAgB,KAAK;AAE7B,YAAM,SAAS,CAAC,QAAQ,QAAQ,CAAC,QAAQ;AAEnC,YAAA,eAAc,aAAQ,mBAAR;AAEpB,UAAI,QAAQ;AACV,cAAM,OAAOA,WAAA;AAAA,MAAA,WACJ,CAAE,MAAM,aAAqB;AACtC,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MAAA;AAGE,UAAA,OAA2B,SAASA,WAAA,cAAc,QAAQ;AAG1D,UAAA,QAAQ,SAAS,KAAK;AACxB,eAAOC,wBAAa,IAAI;AAAA,MAAA;AAGpB,YAAA,WAAW,QAAQ,MAAM;AAG3B,UAAA,KAAK,SACLD,WAAA,cACAE,qBAAU;AAAA,QACR,MAAM,YAAY,OAAOF,WAAAA,cAAc,KAAK,MAAM,YAAY;AAAA,QAC9D;AAAA,MAAA,CACD;AAEL,UAAI,SAASA,WAAAA,aAAa;AACjB,eAAA;AAAA,MAAA;AAGT,UAAI,OAAOA,WAAAA,aAAa;AACtB,aAAKE,WAAU,UAAA,CAAC,KAAK,EAAE,CAAC;AAAA,MAAA;AAGpB,YAAA,WACJ,OAAOF,WAAA,cAAc,MAAME,WAAAA,UAAU,CAAC,MAAM,YAAY,UAAU,IAAI,CAAC;AAEzE,YAAM,OAAO;AACb,YAAM,KAAK;AACX,YAAM,WAAW;AACjB,YAAM,KAAK;AAAA,IACb;AAAA,IAEA,kBAAkB,CAAC,aAAa;AAC1B,UAAA,MAAM,QAAQ,QAAQ,GAAG;AAC3B,cAAM,WAAW;AAAA,MAAA;AAGnB,UAAI,OAAO,aAAa,YAAY,aAAa,MAAM;AAC/C,cAAA,WAAW,OAAO,OAAO,QAAQ;AAAA,MAAA;AAGlC,aAAA;AAAA,IACT;AAAA,IAEA,eAAe,MAAkB;AAAA,EACnC;AAEO,SAAA;AACT;AAsBA,MAAM,sBAAsB,CAK1B,WACoE;AAC7D,SAAA;AAAA,IACL,UAAU,UAAU,CAAC;AAAA,IACrB,QAAQ,CAAC;AAAA,IACT,YAAY,CAAC,gBACX,oBAAoB;AAAA,MAClB,GAAG;AAAA,MACH;AAAA,IAAA,CACD;AAAA,IACH,SAAS,CAAC,YACR,oBAAoB;AAAA,MAClB,GAAG;AAAA,MACH;AAAA,IACD,CAAA;AAAA,EACL;AACF;;;"}
@@ -0,0 +1,115 @@
1
+ import { Assign, Constrain, Expand, ResolveParams, RouteConstraints, TrimPathRight } from '@tanstack/router-core';
2
+ import { AnyRequestMiddleware, AssignAllServerContext } from '@tanstack/start-client-core';
3
+ type TODO = any;
4
+ export declare function createServerFileRoute<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TChildren>(__?: never): ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>;
5
+ export interface ServerRouteOptions<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TMiddlewares> {
6
+ id: TId;
7
+ path: TPath;
8
+ pathname: TFullPath;
9
+ originalIndex: number;
10
+ getParentRoute?: () => TParentRoute;
11
+ middleware: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>;
12
+ methods: Record<string, ServerRouteMethodHandlerFn<TParentRoute, TFullPath, TMiddlewares, any, any>>;
13
+ caseSensitive?: boolean;
14
+ }
15
+ export type ServerRouteManifest = {
16
+ middleware: boolean;
17
+ methods: Record<string, {
18
+ middleware: boolean;
19
+ }>;
20
+ };
21
+ export declare function createServerRoute<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TChildren>(__?: never, __opts?: Partial<ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, undefined>>): ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>;
22
+ export type ServerRouteAddFileChildrenFn<in out TParentRoute extends AnyServerRouteWithTypes, in out TId extends RouteConstraints['TId'], in out TPath extends RouteConstraints['TPath'], in out TFullPath extends RouteConstraints['TFullPath'], in out TMiddlewares, in out TMethods, in out TChildren> = (children: TChildren) => ServerRouteWithTypes<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TMethods, TChildren>;
23
+ export type CreateServerFileRoute<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TChildren> = (options?: undefined) => ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>;
24
+ export type AnyServerRouteWithTypes = ServerRouteWithTypes<any, any, any, any, any, any, any>;
25
+ export interface ServerRouteWithTypes<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TMiddlewares, TMethods, TChildren> {
26
+ _types: ServerRouteTypes<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TMethods>;
27
+ path: TPath;
28
+ id: TId;
29
+ fullPath: TFullPath;
30
+ to: TrimPathRight<TFullPath>;
31
+ parentRoute: TParentRoute;
32
+ children?: TChildren;
33
+ options: ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, TMiddlewares>;
34
+ update: (opts: ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, undefined>) => ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>;
35
+ init: (opts: {
36
+ originalIndex: number;
37
+ }) => void;
38
+ _addFileChildren: ServerRouteAddFileChildrenFn<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TMethods, TChildren>;
39
+ _addFileTypes: () => ServerRouteWithTypes<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TMethods, TChildren>;
40
+ }
41
+ export interface ServerRouteTypes<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TMiddlewares, TMethods> {
42
+ id: TId;
43
+ path: TPath;
44
+ fullPath: TFullPath;
45
+ middlewares: TMiddlewares;
46
+ methods: TMethods;
47
+ parentRoute: TParentRoute;
48
+ allContext: ResolveAllServerContext<TParentRoute, TMiddlewares>;
49
+ }
50
+ export type ResolveAllServerContext<TParentRoute extends AnyServerRouteWithTypes, TMiddlewares> = unknown extends TParentRoute ? AssignAllServerContext<TMiddlewares> : Assign<TParentRoute['_types']['allContext'], AssignAllServerContext<TMiddlewares>>;
51
+ export type AnyServerRoute = AnyServerRouteWithTypes;
52
+ export interface ServerRoute<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TChildren> extends ServerRouteWithTypes<TParentRoute, TId, TPath, TFullPath, undefined, undefined, TChildren>, ServerRouteMiddleware<TParentRoute, TId, TPath, TFullPath, TChildren>, ServerRouteMethods<TParentRoute, TId, TPath, TFullPath, undefined, TChildren> {
53
+ }
54
+ export interface ServerRouteMiddleware<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TChildren> {
55
+ middleware: <const TNewMiddleware>(middleware: Constrain<TNewMiddleware, ReadonlyArray<AnyRequestMiddleware>>) => ServerRouteAfterMiddleware<TParentRoute, TId, TPath, TFullPath, TNewMiddleware, TChildren>;
56
+ }
57
+ export interface ServerRouteAfterMiddleware<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TMiddlewares, TChildren> extends ServerRouteWithTypes<TParentRoute, TId, TPath, TFullPath, TMiddlewares, undefined, TChildren>, ServerRouteMethods<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TChildren> {
58
+ }
59
+ export interface ServerRouteMethods<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TMiddlewares, TChildren> {
60
+ methods: <const TMethods>(methodsOrGetMethods: Constrain<TMethods, ServerRouteMethodsOptions<TParentRoute, TFullPath, TMiddlewares>>) => ServerRouteAfterMethods<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TMethods, TChildren>;
61
+ }
62
+ export type ServerRouteMethodsOptions<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares> = ServerRouteMethodsRecord<TParentRoute, TFullPath, TMiddlewares> | ((api: ServerRouteMethodBuilder<TParentRoute, TFullPath, TMiddlewares>) => ServerRouteMethodsRecord<TParentRoute, TFullPath, TMiddlewares>);
63
+ export interface ServerRouteMethodsRecord<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares> {
64
+ GET?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
65
+ POST?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
66
+ PUT?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
67
+ PATCH?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
68
+ DELETE?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
69
+ OPTIONS?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
70
+ HEAD?: ServerRouteMethodRecordValue<TParentRoute, TFullPath, TMiddlewares>;
71
+ }
72
+ export type ServerRouteMethodRecordValue<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares> = ServerRouteMethodHandlerFn<TParentRoute, TFullPath, TMiddlewares, undefined, any> | AnyRouteMethodsBuilder;
73
+ export type ServerRouteVerb = (typeof ServerRouteVerbs)[number];
74
+ export declare const ServerRouteVerbs: readonly ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"];
75
+ export type ServerRouteMethodHandlerFn<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares, TMethodMiddlewares, TResponse> = (ctx: ServerRouteMethodHandlerCtx<TParentRoute, TFullPath, TMiddlewares, TMethodMiddlewares>) => TResponse | Promise<TResponse>;
76
+ export interface ServerRouteMethodHandlerCtx<in out TParentRoute extends AnyServerRouteWithTypes, in out TFullPath extends string, in out TMiddlewares, in out TMethodMiddlewares> {
77
+ context: Expand<AssignAllMethodContext<TParentRoute, TMiddlewares, TMethodMiddlewares>>;
78
+ request: Request;
79
+ params: Expand<ResolveParams<TFullPath>>;
80
+ pathname: TFullPath;
81
+ }
82
+ export type MergeMethodMiddlewares<TMiddlewares, TMethodMiddlewares> = TMiddlewares extends ReadonlyArray<any> ? TMethodMiddlewares extends ReadonlyArray<any> ? readonly [...TMiddlewares, ...TMethodMiddlewares] : TMiddlewares : TMethodMiddlewares;
83
+ export type AssignAllMethodContext<TParentRoute extends AnyServerRouteWithTypes, TMiddlewares, TMethodMiddlewares> = ResolveAllServerContext<TParentRoute, MergeMethodMiddlewares<TMiddlewares, TMethodMiddlewares>>;
84
+ export type AnyRouteMethodsBuilder = ServerRouteMethodBuilderWithTypes<any, any, any, any>;
85
+ export interface ServerRouteMethodBuilder<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares> extends ServerRouteMethodBuilderWithTypes<TFullPath, TMiddlewares, undefined, undefined>, ServerRouteMethodBuilderMiddleware<TParentRoute, TFullPath, TMiddlewares>, ServerRouteMethodBuilderHandler<TParentRoute, TFullPath, TMiddlewares, undefined> {
86
+ }
87
+ export interface ServerRouteMethodBuilderWithTypes<TFullPath extends string, TMiddlewares, TMethodMiddlewares, TResponse> {
88
+ _options: TODO;
89
+ _types: ServerRouteMethodBuilderTypes<TFullPath, TMiddlewares, TMethodMiddlewares, TResponse>;
90
+ }
91
+ export interface ServerRouteMethodBuilderTypes<in out TFullPath extends string, in out TMiddlewares, in out TMethodMiddlewares, in out TResponse> {
92
+ middlewares: TMiddlewares;
93
+ methodMiddleware: TMethodMiddlewares;
94
+ fullPath: TFullPath;
95
+ response: TResponse;
96
+ }
97
+ export interface ServerRouteMethodBuilderMiddleware<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares> {
98
+ middleware: <const TNewMethodMiddlewares>(middleware: Constrain<TNewMethodMiddlewares, ReadonlyArray<AnyRequestMiddleware>>) => ServerRouteMethodBuilderAfterMiddleware<TParentRoute, TFullPath, TMiddlewares, TNewMethodMiddlewares>;
99
+ }
100
+ export interface ServerRouteMethodBuilderAfterMiddleware<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares, TMethodMiddlewares> extends ServerRouteMethodBuilderWithTypes<TFullPath, TMiddlewares, TMethodMiddlewares, undefined>, ServerRouteMethodBuilderHandler<TParentRoute, TFullPath, TMiddlewares, TMethodMiddlewares> {
101
+ }
102
+ export interface ServerRouteMethodBuilderHandler<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares, TMethodMiddlewares> {
103
+ handler: <TResponse>(handler: ServerRouteMethodHandlerFn<TParentRoute, TFullPath, TMiddlewares, TMethodMiddlewares, TResponse>) => ServerRouteMethodBuilderAfterHandler<TParentRoute, TFullPath, TMiddlewares, TMethodMiddlewares, TResponse>;
104
+ }
105
+ export interface ServerRouteMethodBuilderAfterHandler<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares, TMethodMiddlewares, TResponse> extends ServerRouteMethodBuilderWithTypes<TFullPath, TMiddlewares, TMethodMiddlewares, TResponse> {
106
+ opts: ServerRouteMethod<TParentRoute, TFullPath, TMiddlewares, TMethodMiddlewares>;
107
+ }
108
+ export interface ServerRouteMethod<TParentRoute extends AnyServerRouteWithTypes, TFullPath extends string, TMiddlewares, TMethodMiddlewares> {
109
+ middleware?: Constrain<TMiddlewares, Array<AnyRequestMiddleware>>;
110
+ handler?: ServerRouteMethodHandlerFn<TParentRoute, TFullPath, TMiddlewares, TMethodMiddlewares, undefined>;
111
+ }
112
+ export interface ServerRouteAfterMethods<TParentRoute extends AnyServerRouteWithTypes, TId extends RouteConstraints['TId'], TPath extends RouteConstraints['TPath'], TFullPath extends RouteConstraints['TFullPath'], TMiddlewares, TMethods, TChildren> extends ServerRouteWithTypes<TParentRoute, TId, TPath, TFullPath, TMiddlewares, TMethods, TChildren> {
113
+ options: ServerRouteOptions<TParentRoute, TId, TPath, TFullPath, TMiddlewares>;
114
+ }
115
+ export {};
@@ -54,10 +54,11 @@ ${jsesc(script, { quotes: "backtick" })}\`)` : ""}; if (typeof __TSR_SSR__ !== '
54
54
  });
55
55
  }
56
56
  function dehydrateRouter(router) {
57
- var _a, _b;
57
+ var _a, _b, _c;
58
58
  const dehydratedRouter = {
59
59
  manifest: router.ssr.manifest,
60
- dehydratedData: (_b = (_a = router.options).dehydrate) == null ? void 0 : _b.call(_a)
60
+ dehydratedData: (_b = (_a = router.options).dehydrate) == null ? void 0 : _b.call(_a),
61
+ lastMatchId: ((_c = router.state.matches[router.state.matches.length - 1]) == null ? void 0 : _c.id) || ""
61
62
  };
62
63
  router.serverSsr.injectScript(
63
64
  () => `__TSR_SSR__.dehydrated = ${jsesc(
@@ -1 +1 @@
1
- {"version":3,"file":"ssr-server.cjs","sources":["../../src/ssr-server.ts"],"sourcesContent":["import { default as warning } from 'tiny-warning'\nimport {\n TSR_DEFERRED_PROMISE,\n defer,\n isPlainArray,\n isPlainObject,\n pick,\n} from '@tanstack/router-core'\nimport jsesc from 'jsesc'\nimport { startSerializer } from '@tanstack/start-client-core'\nimport minifiedTsrBootStrapScript from './tsrScript?script-string'\nimport type {\n ClientExtractedBaseEntry,\n DehydratedRouter,\n ResolvePromiseState,\n SsrMatch,\n} from '@tanstack/start-client-core'\nimport type {\n AnyRouteMatch,\n AnyRouter,\n DeferredPromise,\n Manifest,\n} from '@tanstack/router-core'\n\nexport type ServerExtractedEntry =\n | ServerExtractedStream\n | ServerExtractedPromise\n\nexport interface ServerExtractedBaseEntry extends ClientExtractedBaseEntry {\n id: number\n matchIndex: number\n}\n\nexport interface ServerExtractedStream extends ServerExtractedBaseEntry {\n type: 'stream'\n stream: ReadableStream\n}\n\nexport interface ServerExtractedPromise extends ServerExtractedBaseEntry {\n type: 'promise'\n promise: DeferredPromise<any>\n}\n\nexport function attachRouterServerSsrUtils(\n router: AnyRouter,\n manifest: Manifest | undefined,\n) {\n router.ssr = {\n manifest,\n serializer: startSerializer,\n }\n\n router.serverSsr = {\n injectedHtml: [],\n streamedKeys: new Set(),\n injectHtml: (getHtml) => {\n const promise = Promise.resolve().then(getHtml)\n router.serverSsr!.injectedHtml.push(promise)\n router.emit({\n type: 'onInjectedHtml',\n promise,\n })\n\n return promise.then(() => {})\n },\n injectScript: (getScript, opts) => {\n return router.serverSsr!.injectHtml(async () => {\n const script = await getScript()\n return `<script class='tsr-once'>${script}${\n process.env.NODE_ENV === 'development' && (opts?.logScript ?? true)\n ? `; console.info(\\`Injected From Server:\n${jsesc(script, { quotes: 'backtick' })}\\`)`\n : ''\n }; if (typeof __TSR_SSR__ !== 'undefined') __TSR_SSR__.cleanScripts()</script>`\n })\n },\n streamValue: (key, value) => {\n warning(\n !router.serverSsr!.streamedKeys.has(key),\n 'Key has already been streamed: ' + key,\n )\n\n router.serverSsr!.streamedKeys.add(key)\n router.serverSsr!.injectScript(\n () =>\n `__TSR_SSR__.streamedValues['${key}'] = { value: ${jsesc(\n router.ssr!.serializer.stringify(value),\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )}}`,\n )\n },\n onMatchSettled,\n }\n\n router.serverSsr.injectScript(() => minifiedTsrBootStrapScript, {\n logScript: false,\n })\n}\n\nexport function dehydrateRouter(router: AnyRouter) {\n const dehydratedRouter: DehydratedRouter = {\n manifest: router.ssr!.manifest,\n dehydratedData: router.options.dehydrate?.(),\n }\n\n router.serverSsr!.injectScript(\n () =>\n `__TSR_SSR__.dehydrated = ${jsesc(\n router.ssr!.serializer.stringify(dehydratedRouter),\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )}`,\n )\n}\n\nexport function extractAsyncLoaderData(\n loaderData: any,\n ctx: {\n match: AnyRouteMatch\n router: AnyRouter\n },\n) {\n const extracted: Array<ServerExtractedEntry> = []\n\n const replaced = replaceBy(loaderData, (value, path) => {\n // If it's a stream, we need to tee it so we can read it multiple times\n if (value instanceof ReadableStream) {\n const [copy1, copy2] = value.tee()\n const entry: ServerExtractedStream = {\n type: 'stream',\n path,\n id: extracted.length,\n matchIndex: ctx.match.index,\n stream: copy2,\n }\n\n extracted.push(entry)\n return copy1\n } else if (value instanceof Promise) {\n const deferredPromise = defer(value)\n const entry: ServerExtractedPromise = {\n type: 'promise',\n path,\n id: extracted.length,\n matchIndex: ctx.match.index,\n promise: deferredPromise,\n }\n extracted.push(entry)\n }\n\n return value\n })\n\n return { replaced, extracted }\n}\n\nexport function onMatchSettled(opts: {\n router: AnyRouter\n match: AnyRouteMatch\n}) {\n const { router, match } = opts\n\n let extracted: Array<ServerExtractedEntry> | undefined = undefined\n let serializedLoaderData: any = undefined\n if (match.loaderData !== undefined) {\n const result = extractAsyncLoaderData(match.loaderData, {\n router,\n match,\n })\n match.loaderData = result.replaced\n extracted = result.extracted\n serializedLoaderData = extracted.reduce(\n (acc: any, entry: ServerExtractedEntry) => {\n return deepImmutableSetByPath(acc, ['temp', ...entry.path], undefined)\n },\n { temp: result.replaced },\n ).temp\n }\n\n const initCode = `__TSR_SSR__.initMatch(${jsesc(\n {\n id: match.id,\n __beforeLoadContext: router.ssr!.serializer.stringify(\n match.__beforeLoadContext,\n ),\n loaderData: router.ssr!.serializer.stringify(serializedLoaderData),\n error: router.ssr!.serializer.stringify(match.error),\n extracted: extracted?.map((entry) => pick(entry, ['type', 'path'])),\n updatedAt: match.updatedAt,\n status: match.status,\n } satisfies SsrMatch,\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`\n\n router.serverSsr!.injectScript(() => initCode)\n\n if (extracted) {\n extracted.forEach((entry) => {\n if (entry.type === 'promise') return injectPromise(entry)\n return injectStream(entry)\n })\n }\n\n function injectPromise(entry: ServerExtractedPromise) {\n router.serverSsr!.injectScript(async () => {\n await entry.promise\n\n return `__TSR_SSR__.resolvePromise(${jsesc(\n {\n matchId: match.id,\n id: entry.id,\n promiseState: entry.promise[TSR_DEFERRED_PROMISE],\n } satisfies ResolvePromiseState,\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`\n })\n }\n\n function injectStream(entry: ServerExtractedStream) {\n // Inject a promise that resolves when the stream is done\n // We do this to keep the stream open until we're done\n router.serverSsr!.injectHtml(async () => {\n //\n try {\n const reader = entry.stream.getReader()\n let chunk: ReadableStreamReadResult<any> | null = null\n while (!(chunk = await reader.read()).done) {\n if (chunk.value) {\n const code = `__TSR_SSR__.injectChunk(${jsesc(\n {\n matchId: match.id,\n id: entry.id,\n chunk: chunk.value,\n },\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`\n\n router.serverSsr!.injectScript(() => code)\n }\n }\n\n router.serverSsr!.injectScript(\n () =>\n `__TSR_SSR__.closeStream(${jsesc(\n {\n matchId: match.id,\n id: entry.id,\n },\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`,\n )\n } catch (err) {\n console.error('stream read error', err)\n }\n\n return ''\n })\n }\n}\n\nfunction deepImmutableSetByPath<T>(obj: T, path: Array<string>, value: any): T {\n // immutable set by path retaining array and object references\n if (path.length === 0) {\n return value\n }\n\n const [key, ...rest] = path\n\n if (Array.isArray(obj)) {\n return obj.map((item, i) => {\n if (i === Number(key)) {\n return deepImmutableSetByPath(item, rest, value)\n }\n return item\n }) as T\n }\n\n if (isPlainObject(obj)) {\n return {\n ...obj,\n [key!]: deepImmutableSetByPath((obj as any)[key!], rest, value),\n }\n }\n\n return obj\n}\n\nexport function replaceBy<T>(\n obj: T,\n cb: (value: any, path: Array<string>) => any,\n path: Array<string> = [],\n): T {\n if (isPlainArray(obj)) {\n return obj.map((value, i) => replaceBy(value, cb, [...path, `${i}`])) as any\n }\n\n if (isPlainObject(obj)) {\n // Do not allow objects with illegal\n const newObj: any = {}\n\n for (const key in obj) {\n newObj[key] = replaceBy(obj[key], cb, [...path, key])\n }\n\n return newObj\n }\n\n // // Detect classes, functions, and other non-serializable objects\n // // and return undefined. Exclude some known types that are serializable\n // if (\n // typeof obj === 'function' ||\n // (typeof obj === 'object' &&\n // ![Object, Promise, ReadableStream].includes((obj as any)?.constructor))\n // ) {\n // console.info(obj)\n // warning(false, `Non-serializable value ☝️ found at ${path.join('.')}`)\n // return undefined as any\n // }\n\n const newObj = cb(obj, path)\n\n if (newObj !== obj) {\n return newObj\n }\n\n return obj\n}\n"],"names":["startSerializer","minifiedTs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I,GAAG,MAAM,KAAK;AAAA,IAChE;AAAA,EAAA;AAGK,SAAA;AACT;AAEO,SAAS,UACd,KACA,IACA,OAAsB,CAAA,GACnB;AACC,MAAAC,WAAAA,aAAa,GAAG,GAAG;AACrB,WAAO,IAAI,IAAI,CAAC,OAAO,MAAM,UAAU,OAAO,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,CAAC;AAAA,EAAA;AAGlE,MAAAD,WAAAA,cAAc,GAAG,GAAG;AAEtB,UAAME,UAAc,CAAC;AAErB,eAAW,OAAO,KAAK;AACrBA,cAAO,GAAG,IAAI,UAAU,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC;AAAA,IAAA;AAG/CA,WAAAA;AAAAA,EAAA;AAeH,QAAA,SAAS,GAAG,KAAK,IAAI;AAE3B,MAAI,WAAW,KAAK;AACX,WAAA;AAAA,EAAA;AAGF,SAAA;AACT;;;;;;"}
1
+ {"version":3,"file":"ssr-server.cjs","sources":["../../src/ssr-server.ts"],"sourcesContent":["import { default as warning } from 'tiny-warning'\nimport {\n TSR_DEFERRED_PROMISE,\n defer,\n isPlainArray,\n isPlainObject,\n pick,\n} from '@tanstack/router-core'\nimport jsesc from 'jsesc'\nimport { startSerializer } from '@tanstack/start-client-core'\nimport minifiedTsrBootStrapScript from './tsrScript?script-string'\nimport type {\n ClientExtractedBaseEntry,\n DehydratedRouter,\n ResolvePromiseState,\n SsrMatch,\n} from '@tanstack/start-client-core'\nimport type {\n AnyRouteMatch,\n AnyRouter,\n DeferredPromise,\n Manifest,\n} from '@tanstack/router-core'\n\nexport type ServerExtractedEntry =\n | ServerExtractedStream\n | ServerExtractedPromise\n\nexport interface ServerExtractedBaseEntry extends ClientExtractedBaseEntry {\n id: number\n matchIndex: number\n}\n\nexport interface ServerExtractedStream extends ServerExtractedBaseEntry {\n type: 'stream'\n stream: ReadableStream\n}\n\nexport interface ServerExtractedPromise extends ServerExtractedBaseEntry {\n type: 'promise'\n promise: DeferredPromise<any>\n}\n\nexport function attachRouterServerSsrUtils(\n router: AnyRouter,\n manifest: Manifest | undefined,\n) {\n router.ssr = {\n manifest,\n serializer: startSerializer,\n }\n\n router.serverSsr = {\n injectedHtml: [],\n streamedKeys: new Set(),\n injectHtml: (getHtml) => {\n const promise = Promise.resolve().then(getHtml)\n router.serverSsr!.injectedHtml.push(promise)\n router.emit({\n type: 'onInjectedHtml',\n promise,\n })\n\n return promise.then(() => {})\n },\n injectScript: (getScript, opts) => {\n return router.serverSsr!.injectHtml(async () => {\n const script = await getScript()\n return `<script class='tsr-once'>${script}${\n process.env.NODE_ENV === 'development' && (opts?.logScript ?? true)\n ? `; console.info(\\`Injected From Server:\n${jsesc(script, { quotes: 'backtick' })}\\`)`\n : ''\n }; if (typeof __TSR_SSR__ !== 'undefined') __TSR_SSR__.cleanScripts()</script>`\n })\n },\n streamValue: (key, value) => {\n warning(\n !router.serverSsr!.streamedKeys.has(key),\n 'Key has already been streamed: ' + key,\n )\n\n router.serverSsr!.streamedKeys.add(key)\n router.serverSsr!.injectScript(\n () =>\n `__TSR_SSR__.streamedValues['${key}'] = { value: ${jsesc(\n router.ssr!.serializer.stringify(value),\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )}}`,\n )\n },\n onMatchSettled,\n }\n\n router.serverSsr.injectScript(() => minifiedTsrBootStrapScript, {\n logScript: false,\n })\n}\n\nexport function dehydrateRouter(router: AnyRouter) {\n const dehydratedRouter: DehydratedRouter = {\n manifest: router.ssr!.manifest,\n dehydratedData: router.options.dehydrate?.(),\n lastMatchId:\n router.state.matches[router.state.matches.length - 1]?.id || '',\n }\n\n router.serverSsr!.injectScript(\n () =>\n `__TSR_SSR__.dehydrated = ${jsesc(\n router.ssr!.serializer.stringify(dehydratedRouter),\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )}`,\n )\n}\n\nexport function extractAsyncLoaderData(\n loaderData: any,\n ctx: {\n match: AnyRouteMatch\n router: AnyRouter\n },\n) {\n const extracted: Array<ServerExtractedEntry> = []\n\n const replaced = replaceBy(loaderData, (value, path) => {\n // If it's a stream, we need to tee it so we can read it multiple times\n if (value instanceof ReadableStream) {\n const [copy1, copy2] = value.tee()\n const entry: ServerExtractedStream = {\n type: 'stream',\n path,\n id: extracted.length,\n matchIndex: ctx.match.index,\n stream: copy2,\n }\n\n extracted.push(entry)\n return copy1\n } else if (value instanceof Promise) {\n const deferredPromise = defer(value)\n const entry: ServerExtractedPromise = {\n type: 'promise',\n path,\n id: extracted.length,\n matchIndex: ctx.match.index,\n promise: deferredPromise,\n }\n extracted.push(entry)\n }\n\n return value\n })\n\n return { replaced, extracted }\n}\n\nexport function onMatchSettled(opts: {\n router: AnyRouter\n match: AnyRouteMatch\n}) {\n const { router, match } = opts\n\n let extracted: Array<ServerExtractedEntry> | undefined = undefined\n let serializedLoaderData: any = undefined\n if (match.loaderData !== undefined) {\n const result = extractAsyncLoaderData(match.loaderData, {\n router,\n match,\n })\n match.loaderData = result.replaced\n extracted = result.extracted\n serializedLoaderData = extracted.reduce(\n (acc: any, entry: ServerExtractedEntry) => {\n return deepImmutableSetByPath(acc, ['temp', ...entry.path], undefined)\n },\n { temp: result.replaced },\n ).temp\n }\n\n const initCode = `__TSR_SSR__.initMatch(${jsesc(\n {\n id: match.id,\n __beforeLoadContext: router.ssr!.serializer.stringify(\n match.__beforeLoadContext,\n ),\n loaderData: router.ssr!.serializer.stringify(serializedLoaderData),\n error: router.ssr!.serializer.stringify(match.error),\n extracted: extracted?.map((entry) => pick(entry, ['type', 'path'])),\n updatedAt: match.updatedAt,\n status: match.status,\n } satisfies SsrMatch,\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`\n\n router.serverSsr!.injectScript(() => initCode)\n\n if (extracted) {\n extracted.forEach((entry) => {\n if (entry.type === 'promise') return injectPromise(entry)\n return injectStream(entry)\n })\n }\n\n function injectPromise(entry: ServerExtractedPromise) {\n router.serverSsr!.injectScript(async () => {\n await entry.promise\n\n return `__TSR_SSR__.resolvePromise(${jsesc(\n {\n matchId: match.id,\n id: entry.id,\n promiseState: entry.promise[TSR_DEFERRED_PROMISE],\n } satisfies ResolvePromiseState,\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`\n })\n }\n\n function injectStream(entry: ServerExtractedStream) {\n // Inject a promise that resolves when the stream is done\n // We do this to keep the stream open until we're done\n router.serverSsr!.injectHtml(async () => {\n //\n try {\n const reader = entry.stream.getReader()\n let chunk: ReadableStreamReadResult<any> | null = null\n while (!(chunk = await reader.read()).done) {\n if (chunk.value) {\n const code = `__TSR_SSR__.injectChunk(${jsesc(\n {\n matchId: match.id,\n id: entry.id,\n chunk: chunk.value,\n },\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`\n\n router.serverSsr!.injectScript(() => code)\n }\n }\n\n router.serverSsr!.injectScript(\n () =>\n `__TSR_SSR__.closeStream(${jsesc(\n {\n matchId: match.id,\n id: entry.id,\n },\n {\n isScriptContext: true,\n wrap: true,\n json: true,\n },\n )})`,\n )\n } catch (err) {\n console.error('stream read error', err)\n }\n\n return ''\n })\n }\n}\n\nfunction deepImmutableSetByPath<T>(obj: T, path: Array<string>, value: any): T {\n // immutable set by path retaining array and object references\n if (path.length === 0) {\n return value\n }\n\n const [key, ...rest] = path\n\n if (Array.isArray(obj)) {\n return obj.map((item, i) => {\n if (i === Number(key)) {\n return deepImmutableSetByPath(item, rest, value)\n }\n return item\n }) as T\n }\n\n if (isPlainObject(obj)) {\n return {\n ...obj,\n [key!]: deepImmutableSetByPath((obj as any)[key!], rest, value),\n }\n }\n\n return obj\n}\n\nexport function replaceBy<T>(\n obj: T,\n cb: (value: any, path: Array<string>) => any,\n path: Array<string> = [],\n): T {\n if (isPlainArray(obj)) {\n return obj.map((value, i) => replaceBy(value, cb, [...path, `${i}`])) as any\n }\n\n if (isPlainObject(obj)) {\n // Do not allow objects with illegal\n const newObj: any = {}\n\n for (const key in obj) {\n newObj[key] = replaceBy(obj[key], cb, [...path, key])\n }\n\n return newObj\n }\n\n // // Detect classes, functions, and other non-serializable objects\n // // and return undefined. Exclude some known types that are serializable\n // if (\n // typeof obj === 'function' ||\n // (typeof obj === 'object' &&\n // ![Object, Promise, ReadableStream].includes((obj as any)?.constructor))\n // ) {\n // console.info(obj)\n // warning(false, `Non-serializable value ☝️ found at ${path.join('.')}`)\n // return undefined as any\n // }\n\n const newObj = cb(obj, path)\n\n if (newObj !== obj) {\n return newObj\n }\n\n return obj\n}\n"],"names":["startSerializer","minifiedTsrBootStrapScript","defer","pick","TSR_DEFERRED_PROMISE","isPlainObject","isPlainArray","newObj"],"mappings":";;;;;;;AA2CgB,SAAA,2BACd,QACA,UACA;AACA,SAAO,MAAM;AAAA,IACX;AAAA,IACA,YAAYA,gBAAAA;AAAAA,EACd;AAEA,SAAO,YAAY;AAAA,IACjB,cAAc,CAAC;AAAA,IACf,kCAAkB,IAAI;AAAA,IACtB,YAAY,CAAC,YAAY;AACvB,YAAM,UAAU,QAAQ,QAAQ,EAAE,KAAK,OAAO;AACvC,aAAA,UAAW,aAAa,KAAK,OAAO;AAC3C,aAAO,KAAK;AAAA,QACV,MAAM;AAAA,QACN;AAAA,MAAA,CACD;AAEM,aAAA,QAAQ,KAAK,MAAM;AAAA,MAAA,CAAE;AAAA,IAC9B;AAAA,IACA,cAAc,CAAC,WAAW,SAAS;AAC1B,aAAA,OAAO,UAAW,WAAW,YAAY;AACxC,cAAA,SAAS,MAAM,UAAU;AACxB,eAAA,4BAA4B,MAAM,GACvC,QAAQ,IAAI,aAAa,mBAAkB,6BAAM,cAAa,QAC1D;AAAA,EACZ,MAAM,QAAQ,EAAE,QAAQ,WAAY,CAAA,CAAC,QACzB,EACN;AAAA,MAAA,CACD;AAAA,IACH;AAAA,IACA,aAAa,CAAC,KAAK,UAAU;AAC3B;AAAA,QACE,CAAC,OAAO,UAAW,aAAa,IAAI,GAAG;AAAA,QACvC,oCAAoC;AAAA,MACtC;AAEO,aAAA,UAAW,aAAa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EzE,MAAA,KAAK,WAAW,GAAG;AACd,WAAA;AAAA,EAAA;AAGT,QAAM,CAAC,KAAK,GAAG,IAAI,IAAI;AAEnB,MAAA,MAAM,QAAQ,GAAG,GAAG;AACtB,WAAO,IAAI,IAAI,CAAC,MAAM,MAAM;AACtB,UAAA,MAAM,OAAO,GAAG,GAAG;AACd,eAAA,uBAAuB,MAAM,MAAM,KAAK;AAAA,MAAA;AAE1C,aAAA;AAAA,IAAA,CACR;AAAA,EAAA;AAGC,MAAAC,WAAAA,cAAc,GAAG,GAAG;AACf,WAAA;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAI,GAAG,uBAAwB,IAAY,GAAI,GAAG,MAAM,KAAK;AAAA,IAChE;AAAA,EAAA;AAGK,SAAA;AACT;AAEO,SAAS,UACd,KACA,IACA,OAAsB,CAAA,GACnB;AACC,MAAAC,WAAAA,aAAa,GAAG,GAAG;AACrB,WAAO,IAAI,IAAI,CAAC,OAAO,MAAM,UAAU,OAAO,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,CAAC;AAAA,EAAA;AAGlE,MAAAD,WAAAA,cAAc,GAAG,GAAG;AAEtB,UAAME,UAAc,CAAC;AAErB,eAAW,OAAO,KAAK;AACrBA,cAAO,GAAG,IAAI,UAAU,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC;AAAA,IAAA;AAG/CA,WAAAA;AAAAA,EAAA;AAeH,QAAA,SAAS,GAAG,KAAK,IAAI;AAE3B,MAAI,WAAW,KAAK;AACX,WAAA;AAAA,EAAA;AAGF,SAAA;AACT;;;;;;"}
@@ -40,9 +40,7 @@ function getRequestHeaders(opts) {
40
40
  );
41
41
  const { redirect } = opts.router.state;
42
42
  if (redirect) {
43
- headers = mergeHeaders(headers, redirect.headers, {
44
- Location: redirect.href
45
- });
43
+ headers = mergeHeaders(headers, redirect.headers);
46
44
  }
47
45
  return headers;
48
46
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createRequestHandler.js","sources":["../../src/createRequestHandler.ts"],"sourcesContent":["import { createMemoryHistory } from '@tanstack/history'\nimport { mergeHeaders } from '@tanstack/start-client-core'\nimport { attachRouterServerSsrUtils, dehydrateRouter } from './ssr-server'\nimport type { HandlerCallback } from './handlerCallback'\nimport type { AnyRouter, Manifest } from '@tanstack/router-core'\n\nexport type RequestHandler<TRouter extends AnyRouter> = (\n cb: HandlerCallback<TRouter>,\n) => Promise<Response>\n\nexport function createRequestHandler<TRouter extends AnyRouter>({\n createRouter,\n request,\n getRouterManifest,\n}: {\n createRouter: () => TRouter\n request: Request\n getRouterManifest?: () => Manifest | Promise<Manifest>\n}): RequestHandler<TRouter> {\n return async (cb) => {\n const router = createRouter()\n\n attachRouterServerSsrUtils(router, await getRouterManifest?.())\n\n const url = new URL(request.url, 'http://localhost')\n\n const href = url.href.replace(url.origin, '')\n\n // Create a history for the router\n const history = createMemoryHistory({\n initialEntries: [href],\n })\n\n // Update the router with the history and context\n router.update({\n history,\n })\n\n await router.load()\n\n dehydrateRouter(router)\n\n const responseHeaders = getRequestHeaders({\n router,\n })\n\n return cb({\n request,\n router,\n responseHeaders,\n } as any)\n }\n}\n\nfunction getRequestHeaders(opts: { router: AnyRouter }): Headers {\n let headers = mergeHeaders(\n {\n 'Content-Type': 'text/html; charset=UTF-8',\n },\n ...opts.router.state.matches.map((match) => {\n return match.headers\n }),\n )\n\n // Handle Redirects\n const { redirect } = opts.router.state\n\n if (redirect) {\n headers = mergeHeaders(headers, redirect.headers, {\n Location: redirect.href,\n })\n }\n\n return headers\n}\n"],"names":[],"mappings":";;;AAUO,SAAS,qBAAgD;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AACF,GAI4B;AAC1B,SAAO,OAAO,OAAO;AACnB,UAAM,SAAS,aAAa;AAED,+BAAA,QAAQ,OAAM,yDAAqB;AAE9D,UAAM,MAAM,IAAI,IAAI,QAAQ,KAAK,kBAAkB;AAEnD,UAAM,OAAO,IAAI,KAAK,QAAQ,IAAI,QAAQ,EAAE;AAG5C,UAAM,UAAU,oBAAoB;AAAA,MAClC,gBAAgB,CAAC,IAAI;AAAA,IAAA,CACtB;AAGD,WAAO,OAAO;AAAA,MACZ;AAAA,IAAA,CACD;AAED,UAAM,OAAO,KAAK;AAElB,oBAAgB,MAAM;AAEtB,UAAM,kBAAkB,kBAAkB;AAAA,MACxC;AAAA,IAAA,CACD;AAED,WAAO,GAAG;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACM;AAAA,EACV;AACF;AAEA,SAAS,kBAAkB,MAAsC;AAC/D,MAAI,UAAU;AAAA,IACZ;AAAA,MACE,gBAAgB;AAAA,IAClB;AAAA,IACA,GAAG,KAAK,OAAO,MAAM,QAAQ,IAAI,CAAC,UAAU;AAC1C,aAAO,MAAM;AAAA,IACd,CAAA;AAAA,EACH;AAGA,QAAM,EAAE,SAAA,IAAa,KAAK,OAAO;AAEjC,MAAI,UAAU;AACF,cAAA,aAAa,SAAS,SAAS,SAAS;AAAA,MAChD,UAAU,SAAS;AAAA,IAAA,CACpB;AAAA,EAAA;AAGI,SAAA;AACT;"}
1
+ {"version":3,"file":"createRequestHandler.js","sources":["../../src/createRequestHandler.ts"],"sourcesContent":["import { createMemoryHistory } from '@tanstack/history'\nimport { mergeHeaders } from '@tanstack/start-client-core'\nimport { attachRouterServerSsrUtils, dehydrateRouter } from './ssr-server'\nimport type { HandlerCallback } from './handlerCallback'\nimport type { AnyRouter, Manifest } from '@tanstack/router-core'\n\nexport type RequestHandler<TRouter extends AnyRouter> = (\n cb: HandlerCallback<TRouter>,\n) => Promise<Response>\n\nexport function createRequestHandler<TRouter extends AnyRouter>({\n createRouter,\n request,\n getRouterManifest,\n}: {\n createRouter: () => TRouter\n request: Request\n getRouterManifest?: () => Manifest | Promise<Manifest>\n}): RequestHandler<TRouter> {\n return async (cb) => {\n const router = createRouter()\n\n attachRouterServerSsrUtils(router, await getRouterManifest?.())\n\n const url = new URL(request.url, 'http://localhost')\n\n const href = url.href.replace(url.origin, '')\n\n // Create a history for the router\n const history = createMemoryHistory({\n initialEntries: [href],\n })\n\n // Update the router with the history and context\n router.update({\n history,\n })\n\n await router.load()\n\n dehydrateRouter(router)\n\n const responseHeaders = getRequestHeaders({\n router,\n })\n\n return cb({\n request,\n router,\n responseHeaders,\n } as any)\n }\n}\n\nfunction getRequestHeaders(opts: { router: AnyRouter }): Headers {\n let headers = mergeHeaders(\n {\n 'Content-Type': 'text/html; charset=UTF-8',\n },\n ...opts.router.state.matches.map((match) => {\n return match.headers\n }),\n )\n\n // Handle Redirects\n const { redirect } = opts.router.state\n\n if (redirect) {\n headers = mergeHeaders(headers, redirect.headers)\n }\n\n return headers\n}\n"],"names":[],"mappings":";;;AAUO,SAAS,qBAAgD;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AACF,GAI4B;AAC1B,SAAO,OAAO,OAAO;AACnB,UAAM,SAAS,aAAa;AAED,+BAAA,QAAQ,OAAM,yDAAqB;AAE9D,UAAM,MAAM,IAAI,IAAI,QAAQ,KAAK,kBAAkB;AAEnD,UAAM,OAAO,IAAI,KAAK,QAAQ,IAAI,QAAQ,EAAE;AAG5C,UAAM,UAAU,oBAAoB;AAAA,MAClC,gBAAgB,CAAC,IAAI;AAAA,IAAA,CACtB;AAGD,WAAO,OAAO;AAAA,MACZ;AAAA,IAAA,CACD;AAED,UAAM,OAAO,KAAK;AAElB,oBAAgB,MAAM;AAEtB,UAAM,kBAAkB,kBAAkB;AAAA,MACxC;AAAA,IAAA,CACD;AAED,WAAO,GAAG;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACM;AAAA,EACV;AACF;AAEA,SAAS,kBAAkB,MAAsC;AAC/D,MAAI,UAAU;AAAA,IACZ;AAAA,MACE,gBAAgB;AAAA,IAClB;AAAA,IACA,GAAG,KAAK,OAAO,MAAM,QAAQ,IAAI,CAAC,UAAU;AAC1C,aAAO,MAAM;AAAA,IACd,CAAA;AAAA,EACH;AAGA,QAAM,EAAE,SAAA,IAAa,KAAK,OAAO;AAEjC,MAAI,UAAU;AACF,cAAA,aAAa,SAAS,SAAS,OAAO;AAAA,EAAA;AAG3C,SAAA;AACT;"}
@@ -1,8 +1,10 @@
1
- import { eventHandler, EventHandlerResponse } from 'h3';
1
+ import { RequestHandler } from './h3.js';
2
+ import { AnyRouter } from '@tanstack/router-core';
2
3
  import { HandlerCallback } from './handlerCallback.js';
3
- import { AnyRouter, Manifest } from '@tanstack/router-core';
4
- export type CustomizeStartHandler<TRouter extends AnyRouter, TResponse extends EventHandlerResponse = EventHandlerResponse> = (cb: HandlerCallback<TRouter, TResponse>) => ReturnType<typeof eventHandler>;
5
- export declare function createStartHandler<TRouter extends AnyRouter, TResponse extends EventHandlerResponse = EventHandlerResponse>({ createRouter, getRouterManifest, }: {
4
+ export type CustomizeStartHandler<TRouter extends AnyRouter> = (cb: HandlerCallback<TRouter>) => RequestHandler;
5
+ export declare function getStartResponseHeaders(opts: {
6
+ router: AnyRouter;
7
+ }): Headers;
8
+ export declare function createStartHandler<TRouter extends AnyRouter>({ createRouter, }: {
6
9
  createRouter: () => TRouter;
7
- getRouterManifest?: () => Manifest | Promise<Manifest>;
8
- }): CustomizeStartHandler<TRouter, TResponse>;
10
+ }): CustomizeStartHandler<TRouter>;