@tanstack/start-server-core 1.120.3 → 1.120.4-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) 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 +235 -30
  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 +20 -14
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs/index.d.cts +7 -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 +154 -0
  19. package/dist/cjs/server-functions-handler.cjs.map +1 -0
  20. package/dist/cjs/server-functions-handler.d.cts +4 -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/undici.cjs +14 -0
  25. package/dist/cjs/undici.cjs.map +1 -0
  26. package/dist/cjs/undici.d.cts +43 -0
  27. package/dist/esm/createRequestHandler.js +1 -3
  28. package/dist/esm/createRequestHandler.js.map +1 -1
  29. package/dist/esm/createStartHandler.d.ts +8 -6
  30. package/dist/esm/createStartHandler.js +215 -32
  31. package/dist/esm/createStartHandler.js.map +1 -1
  32. package/dist/esm/h3.d.ts +11 -7
  33. package/dist/esm/h3.js +26 -63
  34. package/dist/esm/h3.js.map +1 -1
  35. package/dist/esm/handlerCallback.d.ts +3 -4
  36. package/dist/esm/handlerCallback.js.map +1 -1
  37. package/dist/esm/index.d.ts +7 -1
  38. package/dist/esm/index.js +17 -5
  39. package/dist/esm/index.js.map +1 -1
  40. package/dist/esm/router-manifest.d.ts +17 -0
  41. package/dist/esm/router-manifest.js +44 -0
  42. package/dist/esm/router-manifest.js.map +1 -0
  43. package/dist/esm/server-functions-handler.d.ts +4 -0
  44. package/dist/esm/server-functions-handler.js +154 -0
  45. package/dist/esm/server-functions-handler.js.map +1 -0
  46. package/dist/esm/serverRoute.d.ts +115 -0
  47. package/dist/esm/serverRoute.js +100 -0
  48. package/dist/esm/serverRoute.js.map +1 -0
  49. package/dist/esm/undici.d.ts +43 -0
  50. package/dist/esm/undici.js +14 -0
  51. package/dist/esm/undici.js.map +1 -0
  52. package/package.json +9 -6
  53. package/src/createRequestHandler.ts +1 -3
  54. package/src/createStartHandler.ts +325 -48
  55. package/src/h3.ts +71 -78
  56. package/src/handlerCallback.ts +5 -12
  57. package/src/index.tsx +13 -1
  58. package/src/router-manifest.ts +79 -0
  59. package/src/server-functions-handler.ts +273 -0
  60. package/src/serverRoute.ts +661 -0
  61. package/src/tanstack-start.d.ts +5 -0
  62. package/src/undici.ts +60 -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 {};
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const undici = require("undici");
4
+ const __setGlobalOrigin = undici.setGlobalOrigin;
5
+ function __getAbsoluteUrl(req, options = { trustProxy: false }) {
6
+ const headers = req.headers;
7
+ const host = options.trustProxy ? headers.get("x-forwarded-host") || headers.get("host") : headers.get("host");
8
+ const protocol = options.trustProxy ? headers.get("x-forwarded-proto") || "http" : "http";
9
+ if (!host) throw new Error("Cannot determine host from request headers");
10
+ return `${protocol}://${host}`;
11
+ }
12
+ exports.__getAbsoluteUrl = __getAbsoluteUrl;
13
+ exports.__setGlobalOrigin = __setGlobalOrigin;
14
+ //# sourceMappingURL=undici.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"undici.cjs","sources":["../../src/undici.ts"],"sourcesContent":["import { setGlobalOrigin } from 'undici'\n\n/**\n * DO NOT USE THIS FUNCTION. THIS FUNCTION IS FOR INTERNAL USE ONLY.\n *\n * @internal\n */\nexport const __setGlobalOrigin = setGlobalOrigin\n\n/**\n * DO NOT USE THIS FUNCTION. THIS FUNCTION IS FOR INTERNAL USE ONLY.\n *\n * Constructs an absolute URL from the given request object and options.\n *\n * @internal\n * @param req - The `Request` object containing the headers to extract the host and protocol.\n * @param options - Configuration options for determining the trust level of proxy headers.\n * @param options.trustProxy - If `true`, the function will trust the `x-forwarded-host` and `x-forwarded-proto` headers\n * to determine the host and protocol. Defaults to `false`.\n * @returns The absolute URL constructed from the request headers as a string.\n * @throws Will throw an error if the `host` cannot be determined from the request headers.\n *\n * @example\n * ```ts\n * // Example usage:\n * const req = new Request('http://example.com', {\n * headers: {\n * 'host': 'example.com',\n * 'x-forwarded-host': 'proxy.example.com',\n * 'x-forwarded-proto': 'https',\n * },\n * });\n *\n * // Without trusting proxy headers\n * const url1 = getAbsoluteUrl(req);\n * console.log(url1); // Output: \"http://example.com\"\n *\n * // With trusting proxy headers\n * const url2 = getAbsoluteUrl(req, { trustProxy: true });\n * console.log(url2); // Output: \"https://proxy.example.com\"\n * ```\n */\nexport function __getAbsoluteUrl(\n req: Request,\n options: { trustProxy: boolean } = { trustProxy: false },\n): string {\n const headers = req.headers\n\n const host = options.trustProxy\n ? headers.get('x-forwarded-host') || headers.get('host')\n : headers.get('host')\n\n const protocol = options.trustProxy\n ? headers.get('x-forwarded-proto') || 'http'\n : 'http'\n\n if (!host) throw new Error('Cannot determine host from request headers')\n\n return `${protocol}://${host}`\n}\n"],"names":["setGlobalOrigin"],"mappings":";;;AAOO,MAAM,oBAAoBA,OAAAA;AAmC1B,SAAS,iBACd,KACA,UAAmC,EAAE,YAAY,SACzC;AACR,QAAM,UAAU,IAAI;AAEpB,QAAM,OAAO,QAAQ,aACjB,QAAQ,IAAI,kBAAkB,KAAK,QAAQ,IAAI,MAAM,IACrD,QAAQ,IAAI,MAAM;AAEtB,QAAM,WAAW,QAAQ,aACrB,QAAQ,IAAI,mBAAmB,KAAK,SACpC;AAEJ,MAAI,CAAC,KAAY,OAAA,IAAI,MAAM,4CAA4C;AAEhE,SAAA,GAAG,QAAQ,MAAM,IAAI;AAC9B;;;"}
@@ -0,0 +1,43 @@
1
+ import { setGlobalOrigin } from 'undici';
2
+ /**
3
+ * DO NOT USE THIS FUNCTION. THIS FUNCTION IS FOR INTERNAL USE ONLY.
4
+ *
5
+ * @internal
6
+ */
7
+ export declare const __setGlobalOrigin: typeof setGlobalOrigin;
8
+ /**
9
+ * DO NOT USE THIS FUNCTION. THIS FUNCTION IS FOR INTERNAL USE ONLY.
10
+ *
11
+ * Constructs an absolute URL from the given request object and options.
12
+ *
13
+ * @internal
14
+ * @param req - The `Request` object containing the headers to extract the host and protocol.
15
+ * @param options - Configuration options for determining the trust level of proxy headers.
16
+ * @param options.trustProxy - If `true`, the function will trust the `x-forwarded-host` and `x-forwarded-proto` headers
17
+ * to determine the host and protocol. Defaults to `false`.
18
+ * @returns The absolute URL constructed from the request headers as a string.
19
+ * @throws Will throw an error if the `host` cannot be determined from the request headers.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * // Example usage:
24
+ * const req = new Request('http://example.com', {
25
+ * headers: {
26
+ * 'host': 'example.com',
27
+ * 'x-forwarded-host': 'proxy.example.com',
28
+ * 'x-forwarded-proto': 'https',
29
+ * },
30
+ * });
31
+ *
32
+ * // Without trusting proxy headers
33
+ * const url1 = getAbsoluteUrl(req);
34
+ * console.log(url1); // Output: "http://example.com"
35
+ *
36
+ * // With trusting proxy headers
37
+ * const url2 = getAbsoluteUrl(req, { trustProxy: true });
38
+ * console.log(url2); // Output: "https://proxy.example.com"
39
+ * ```
40
+ */
41
+ export declare function __getAbsoluteUrl(req: Request, options?: {
42
+ trustProxy: boolean;
43
+ }): string;
@@ -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>;
@@ -1,56 +1,239 @@
1
1
  import { createMemoryHistory } from "@tanstack/history";
2
- import { mergeHeaders } from "@tanstack/start-client-core";
3
- import { eventHandler, toWebRequest, getResponseHeaders } from "h3";
2
+ import { mergeHeaders, json, flattenMiddlewares } from "@tanstack/start-client-core";
3
+ import { joinPaths, trimPath, isRedirect, tsrRedirectHeaderKey, processRouteTree, getMatchedRoutes, rootRouteId } from "@tanstack/router-core";
4
+ import { getResponseHeaders, requestHandler } from "./h3.js";
4
5
  import { attachRouterServerSsrUtils, dehydrateRouter } from "./ssr-server.js";
6
+ import { getStartManifest } from "./router-manifest.js";
7
+ import { handleServerAction } from "./server-functions-handler.js";
8
+ function getStartResponseHeaders(opts) {
9
+ let headers = mergeHeaders(
10
+ getResponseHeaders(),
11
+ {
12
+ "Content-Type": "text/html; charset=UTF-8"
13
+ },
14
+ ...opts.router.state.matches.map((match) => {
15
+ return match.headers;
16
+ })
17
+ );
18
+ const { redirect } = opts.router.state;
19
+ if (redirect) {
20
+ headers = mergeHeaders(headers, redirect.headers);
21
+ }
22
+ return headers;
23
+ }
5
24
  function createStartHandler({
6
- createRouter,
7
- getRouterManifest
25
+ createRouter
8
26
  }) {
9
27
  return (cb) => {
10
- return eventHandler(async (event) => {
11
- const request = toWebRequest(event);
28
+ return requestHandler(async ({ request }) => {
12
29
  const url = new URL(request.url);
13
30
  const href = url.href.replace(url.origin, "");
14
31
  const history = createMemoryHistory({
15
32
  initialEntries: [href]
16
33
  });
17
34
  const router = createRouter();
18
- attachRouterServerSsrUtils(router, await (getRouterManifest == null ? void 0 : getRouterManifest()));
35
+ attachRouterServerSsrUtils(router, getStartManifest());
19
36
  router.update({
20
37
  history
21
38
  });
22
- await router.load();
23
- dehydrateRouter(router);
24
- const responseHeaders = getStartResponseHeaders({ event, router });
25
- const response = await cb({
26
- request,
27
- router,
28
- responseHeaders
39
+ const response = await (async () => {
40
+ try {
41
+ if (!process.env.TSS_SERVER_FN_BASE) {
42
+ throw new Error(
43
+ "tanstack/start-server-core: TSS_SERVER_FN_BASE must be defined in your environment for createStartHandler()"
44
+ );
45
+ }
46
+ const serverFnBase = joinPaths([
47
+ "/",
48
+ trimPath(process.env.TSS_SERVER_FN_BASE),
49
+ "/"
50
+ ]);
51
+ if (href.startsWith(serverFnBase)) {
52
+ return await handleServerAction({ request });
53
+ }
54
+ const serverRouteTreeModule = await (async () => {
55
+ try {
56
+ return await import("tanstack:server-routes");
57
+ } catch (e) {
58
+ console.log(e);
59
+ return void 0;
60
+ }
61
+ })();
62
+ if (serverRouteTreeModule) {
63
+ const [_matchedRoutes, response3] = await handleServerRoutes({
64
+ routeTree: serverRouteTreeModule.routeTree,
65
+ request
66
+ });
67
+ if (response3) return response3;
68
+ }
69
+ const requestAcceptHeader = request.headers.get("Accept") || "*/*";
70
+ const splitRequestAcceptHeader = requestAcceptHeader.split(",");
71
+ const supportedMimeTypes = ["*/*", "text/html"];
72
+ const isRouterAcceptSupported = supportedMimeTypes.some(
73
+ (mimeType) => splitRequestAcceptHeader.some(
74
+ (acceptedMimeType) => acceptedMimeType.trim().startsWith(mimeType)
75
+ )
76
+ );
77
+ if (!isRouterAcceptSupported) {
78
+ return json(
79
+ {
80
+ error: "Only HTML requests are supported here"
81
+ },
82
+ {
83
+ status: 500
84
+ }
85
+ );
86
+ }
87
+ await router.load();
88
+ if (router.state.redirect) return router.state.redirect;
89
+ dehydrateRouter(router);
90
+ const responseHeaders = getStartResponseHeaders({ router });
91
+ const response2 = await cb({
92
+ request,
93
+ router,
94
+ responseHeaders
95
+ });
96
+ return response2;
97
+ } catch (err) {
98
+ if (err instanceof Response) {
99
+ return err;
100
+ }
101
+ throw err;
102
+ }
103
+ })();
104
+ if (isRedirect(response)) {
105
+ if (response.options.to && typeof response.options.to === "string" && !response.options.to.startsWith("/")) {
106
+ throw new Error(
107
+ `Server side redirects must use absolute paths via the 'href' or 'to' options. Received: ${JSON.stringify(response.options)}`
108
+ );
109
+ }
110
+ if (["params", "search", "hash"].some(
111
+ (d) => typeof response.options[d] === "function"
112
+ )) {
113
+ throw new Error(
114
+ `Server side redirects must use static search, params, and hash values and do not support functional values. Received functional values for: ${Object.keys(
115
+ response.options
116
+ ).filter((d) => typeof response.options[d] === "function").map((d) => `"${d}"`).join(", ")}`
117
+ );
118
+ }
119
+ const redirect = router.resolveRedirect(response);
120
+ if (request.headers.get("x-tsr-redirect") === "manual") {
121
+ return json(
122
+ {
123
+ ...response.options,
124
+ isSerializedRedirect: true
125
+ },
126
+ {
127
+ headers: redirect.headers
128
+ }
129
+ );
130
+ }
131
+ return redirect;
132
+ }
133
+ const body = response.status === 204 ? null : await response.clone().blob();
134
+ const headers = new Headers(response.headers);
135
+ headers.append("Access-Control-Expose-Headers", tsrRedirectHeaderKey);
136
+ return new Response(body, {
137
+ status: response.status,
138
+ statusText: response.statusText,
139
+ headers
29
140
  });
30
- return response;
31
141
  });
32
142
  };
33
143
  }
34
- function getStartResponseHeaders(opts) {
35
- let headers = mergeHeaders(
36
- getResponseHeaders(opts.event),
37
- opts.event.___ssrRpcResponseHeaders,
38
- {
39
- "Content-Type": "text/html; charset=UTF-8"
40
- },
41
- ...opts.router.state.matches.map((match) => {
42
- return match.headers;
43
- })
44
- );
45
- const { redirect } = opts.router.state;
46
- if (redirect) {
47
- headers = mergeHeaders(headers, redirect.headers, {
48
- Location: redirect.href
144
+ async function handleServerRoutes({
145
+ routeTree,
146
+ request
147
+ }) {
148
+ const { flatRoutes, routesById, routesByPath } = processRouteTree({
149
+ routeTree,
150
+ initRoute: (route, i) => {
151
+ route.init({
152
+ originalIndex: i
153
+ });
154
+ }
155
+ });
156
+ const url = new URL(request.url);
157
+ const pathname = url.pathname;
158
+ const history = createMemoryHistory({
159
+ initialEntries: [pathname]
160
+ });
161
+ const { matchedRoutes, foundRoute, routeParams } = getMatchedRoutes({
162
+ pathname: history.location.pathname,
163
+ basepath: "/",
164
+ caseSensitive: true,
165
+ routesByPath,
166
+ routesById,
167
+ flatRoutes
168
+ });
169
+ let response;
170
+ if (foundRoute && foundRoute.id !== rootRouteId) {
171
+ const method = Object.keys(foundRoute.options.methods).find(
172
+ (method2) => method2.toLowerCase() === request.method.toLowerCase()
173
+ );
174
+ if (method) {
175
+ const handler = foundRoute.options.methods[method];
176
+ if (handler) {
177
+ const middlewares = flattenMiddlewares(
178
+ matchedRoutes.flatMap((r) => r.options.middleware).filter(Boolean)
179
+ ).map((d) => d.options.server);
180
+ middlewares.push(handlerToMiddleware(handler));
181
+ const ctx = await executeMiddleware(middlewares, {
182
+ request,
183
+ context: {},
184
+ params: routeParams,
185
+ pathname: history.location.pathname
186
+ });
187
+ response = ctx.response;
188
+ }
189
+ }
190
+ }
191
+ return [matchedRoutes, response];
192
+ }
193
+ function handlerToMiddleware(handler) {
194
+ return async ({ next, ...rest }) => ({
195
+ response: await handler(rest)
196
+ });
197
+ }
198
+ function executeMiddleware(middlewares, ctx) {
199
+ let index = -1;
200
+ const next = async (ctx2) => {
201
+ index++;
202
+ const middleware = middlewares[index];
203
+ if (!middleware) return ctx2;
204
+ const result = await middleware({
205
+ ...ctx2,
206
+ // Allow the middleware to call the next middleware in the chain
207
+ next: async (nextCtx) => {
208
+ const nextResult = await next({ ...ctx2, ...nextCtx });
209
+ return Object.assign(ctx2, handleCtxResult(nextResult));
210
+ }
211
+ // Allow the middleware result to extend the return context
212
+ }).catch((err) => {
213
+ if (isSpecialResponse(err)) {
214
+ return {
215
+ response: err
216
+ };
217
+ }
218
+ throw err;
49
219
  });
220
+ return Object.assign(ctx2, handleCtxResult(result));
221
+ };
222
+ return handleCtxResult(next(ctx));
223
+ }
224
+ function handleCtxResult(result) {
225
+ if (isSpecialResponse(result)) {
226
+ return {
227
+ response: result
228
+ };
50
229
  }
51
- return headers;
230
+ return result;
231
+ }
232
+ function isSpecialResponse(err) {
233
+ return err instanceof Response || isRedirect(err);
52
234
  }
53
235
  export {
54
- createStartHandler
236
+ createStartHandler,
237
+ getStartResponseHeaders
55
238
  };
56
239
  //# sourceMappingURL=createStartHandler.js.map