Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 6x 6x 4776x 4776x 4776x 4776x 4776x 4773x 6x 4776x 4776x 4776x 4776x 6x 4773x 3x | import type { AnyRoute } from "@tanstack/react-router";
import { createFileRoute } from "@tanstack/react-router";
import type { Register, RouteMethodHandlerFn } from "@tanstack/react-start";
import { proxyRequest } from "@tanstack/react-start/server";
import { TRPCError } from "@trpc/server";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { DEFAULT_TRPC_ENDPOINT } from "~app/contexts/links-context";
import { apiCookieNames } from "~utils/mocks";
import { transformer } from "~utils/transformer";
import { router } from "~web/handlers";
import { getCookie } from "~web/utils/cookies";
import { env } from "~web/utils/env";
import { createServerContext } from "~web/utils/server/trpc";
type Callback = RouteMethodHandlerFn<
Register,
AnyRoute,
"/api/trpc/$",
unknown,
unknown,
unknown,
unknown
>;
const getTestRequestHandlerProps = ({
// @ts-expect-error This is a hack for tests
router: overrideRouter,
}: Omit<Parameters<Callback>[0], "request">): object => {
/* c8 ignore start */
if (!import.meta.env.VITEST) {
return {};
}
/* c8 ignore stop */
return { endpoint: "", router: overrideRouter as unknown };
};
const redirectTestHandler = async (
request: Request,
cookieHeader: string,
proxyPort: string,
) => {
if (import.meta.env.MODE !== "test") {
// oxlint-disable-next-line no-console
console.warn(
"You are trying to use proxying without activating --mode=test",
);
return new Response("Proxying is only allowed in test mode", {
status: 403,
});
}
const proxyUrl = new URL(request.url);
proxyUrl.port = proxyPort;
request.headers.set(
"cookie",
cookieHeader
.replace(
new RegExp(
String.raw`(?:^|;\s*)(${apiCookieNames.proxyPort}=[^;]+)(?=;|$)`,
),
"",
)
.replace(/^;/, "") || "",
);
const { body, status, statusText, headers } = await proxyRequest(
proxyUrl.toString(),
{ headers: request.headers, fetchOptions: { signal: request.signal } },
);
return new Response(body, { status, statusText, headers });
};
const callback = async (
request: Request,
rest: Omit<Parameters<Callback>[0], "request">,
) => {
const cookieHeader = request.headers.get("cookie") ?? "";
const proxyPort = getCookie(cookieHeader, apiCookieNames.proxyPort);
Eif (proxyPort) {
return redirectTestHandler(request, cookieHeader, proxyPort);
}
if (import.meta.env.MODE === "test" && env.PLAYWRIGHT) {
return Response.json({
error: transformer.serialize({
code: 400,
message: [
"Unexpected test mode tRPC fetch for url",
decodeURIComponent(request.url),
].join("\n"),
}),
});
}
return fetchRequestHandler({
endpoint: DEFAULT_TRPC_ENDPOINT,
req: request,
router,
createContext: (opts) => createServerContext(opts.req),
onError: ({ error, type, path, ctx }) => {
/* c8 ignore start */
if (!ctx) {
return;
}
/* c8 ignore stop */
if (error instanceof TRPCError && !error.message) {
const errors =
error.cause instanceof AggregateError ? error.cause.errors : [error];
const internalConnectionError = errors.find(
(
subError: Record<string, unknown>,
): subError is {
syscall: "connect";
code: "ECONNREFUSED";
address: string;
port: number;
} =>
subError.syscall === "connect" && subError.code === "ECONNREFUSED",
);
if (internalConnectionError) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Can't connect to the ${internalConnectionError.address}:${internalConnectionError.port}`,
});
}
}
if (error.code === "UNAUTHORIZED" && path === "user.get") {
// Do not log an attempt to fetch the user without a cookie
return;
}
ctx.logger.error(
`[${error.code}] [${
ctx.reqHeaders.get("user-agent") ?? "no-user-agent"
}] ${type} "${path}": ${error.message}`,
);
},
responseMeta: ({ ctx }) => ({
status: 200,
headers: ctx ? ctx.resHeaders : {},
}),
...getTestRequestHandlerProps(rest),
});
};
export const Route = createFileRoute("/api/trpc/$")({
server: {
handlers: {
GET: ({ request, ...rest }) => callback(request, rest),
POST: ({ request, ...rest }) => callback(request, rest),
},
},
});
|