-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcreateTunnelHandler.ts
More file actions
38 lines (34 loc) · 1.29 KB
/
createTunnelHandler.ts
File metadata and controls
38 lines (34 loc) · 1.29 KB
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
import { type DsnComponents, handleTunnelRequest, makeDsn } from '@sentry/core';
/**
* Creates a Sentry tunnel handler for TanStack Start.
*
* @param allowedDsns - Array of DSN strings that this tunnel will accept.
* @returns TanStack Start compatible request handler
*
* @example
* const handler = createSentryTunnelHandler([process.env.SENTRY_DSN])
* export const Route = createFileRoute('/tunnel')({
* server: { handlers: { POST: handler } }
* })
*/
export function createTunnelHandler(
allowedDsns: Array<string>,
): (args: { request: Request }) => Promise<Response> {
const allowedDsnComponents = allowedDsns.map(makeDsn).filter((c): c is DsnComponents => c !== undefined);
if (allowedDsnComponents.length === 0) {
// eslint-disable-next-line no-console
console.warn('Sentry tunnel: No valid DSNs provided. All requests will be rejected.');
}
return async ({ request }: { request: Request }): Promise<Response> => {
try {
const body = await request.text();
const result = await handleTunnelRequest(body, allowedDsnComponents);
return new Response(result.body, {
status: result.status,
headers: { 'Content-Type': result.contentType },
});
} catch (error) {
return new Response('Internal server error', { status: 500 });
}
};
}