-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Tunnel route helper + Dynamic tunnel route generator for TanStack Start React #20264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ef7b5e9
1691509
316be89
e6b6290
08b4154
3991017
98202a9
320a55f
73fbbeb
5a2c6e9
ee3da8d
1d827f5
7c9124c
4390705
e68a210
c993616
52c33cb
d77dec4
29a5d89
c356b37
9e04b81
da03651
00c9a99
af60d11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| declare const __APP_DSN__: string; | ||
| declare const __APP_TUNNEL__: string; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import * as Sentry from '@sentry/tanstackstart-react'; | ||
| import { createFileRoute } from '@tanstack/react-router'; | ||
|
|
||
| const USE_TUNNEL_ROUTE = process.env.E2E_TEST_USE_TUNNEL_ROUTE === '1'; | ||
|
|
||
| const DEFAULT_DSN = 'https://public@dsn.ingest.sentry.io/1337'; | ||
| const TUNNEL_DSN = 'http://public@localhost:3031/1337'; | ||
|
|
||
| export const Route = createFileRoute('/monitor')({ | ||
| server: Sentry.createSentryTunnelRoute({ | ||
| allowedDsns: [USE_TUNNEL_ROUTE ? TUNNEL_DSN : DEFAULT_DSN], | ||
| }), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError } from '@sentry-internal/test-utils'; | ||
|
|
||
| const useTunnelRoute = process.env.E2E_TEST_USE_TUNNEL_ROUTE === '1'; | ||
|
|
||
| test.skip(!useTunnelRoute, 'Tunnel assertions only run in the tunnel variant'); | ||
|
|
||
| test('Sends client-side errors through the monitor tunnel route', async ({ page }) => { | ||
| const errorEventPromise = waitForError('tanstackstart-react', errorEvent => { | ||
| return errorEvent?.exception?.values?.[0]?.value === 'Sentry Client Test Error'; | ||
| }); | ||
|
|
||
| await page.goto('/'); | ||
|
|
||
| await expect(page.locator('button').filter({ hasText: 'Break the client' })).toBeVisible(); | ||
|
|
||
| const monitorResponsePromise = page.waitForResponse(response => { | ||
| return response.url().endsWith('/monitor') && response.request().method() === 'POST'; | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tunnel test may capture unrelated Sentry requestLow Severity The Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit 9e04b81. Configure here. |
||
|
|
||
| await page.locator('button').filter({ hasText: 'Break the client' }).click(); | ||
|
|
||
| const monitorResponse = await monitorResponsePromise; | ||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(monitorResponse.status()).toBe(200); | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Sentry Client Test Error'); | ||
| expect(errorEvent.transaction).toBe('/'); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { handleTunnelRequest } from '@sentry/core'; | ||
|
|
||
| export interface CreateSentryTunnelRouteOptions { | ||
| allowedDsns: string[]; | ||
| } | ||
|
|
||
| type SentryTunnelRouteHandlerContext = { | ||
| request: Request; | ||
| }; | ||
|
|
||
| type SentryTunnelRoute = { | ||
| handlers: { | ||
| POST: (context: SentryTunnelRouteHandlerContext) => Promise<Response>; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a TanStack Start server route configuration for tunneling Sentry envelopes. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { createFileRoute } from '@tanstack/react-router'; | ||
| * import * as Sentry from '@sentry/tanstackstart-react'; | ||
| * | ||
| * export const Route = createFileRoute('/monitoring')({ | ||
| * server: Sentry.createSentryTunnelRoute({ | ||
| * allowedDsns: ['https://public@o0.ingest.sentry.io/0'], | ||
| * }), | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export function createSentryTunnelRoute(options: CreateSentryTunnelRouteOptions): SentryTunnelRoute { | ||
| return { | ||
| handlers: { | ||
| POST: async ({ request }) => { | ||
| return handleTunnelRequest({ | ||
| request, | ||
| allowedDsns: options.allowedDsns, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const handleTunnelRequestSpy = vi.fn(); | ||
|
|
||
| vi.mock('@sentry/core', async importOriginal => { | ||
| const original = await importOriginal(); | ||
| return { | ||
| ...original, | ||
| handleTunnelRequest: (...args: unknown[]) => handleTunnelRequestSpy(...args), | ||
| }; | ||
| }); | ||
|
|
||
| const { createSentryTunnelRoute } = await import('../../src/server/tunnelRoute'); | ||
|
|
||
| describe('createSentryTunnelRoute', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('returns a server route config with only a POST handler', () => { | ||
| const route = createSentryTunnelRoute({ | ||
| allowedDsns: ['https://public@o0.ingest.sentry.io/0'], | ||
| }); | ||
|
|
||
| expect(Object.keys(route.handlers)).toEqual(['POST']); | ||
| expect(route.handlers.POST).toBeTypeOf('function'); | ||
| }); | ||
|
|
||
| it('forwards the request and allowed DSNs to handleTunnelRequest', async () => { | ||
| const request = new Request('http://localhost:3000/monitoring', { method: 'POST', body: 'envelope' }); | ||
| const allowedDsns = ['https://public@o0.ingest.sentry.io/0']; | ||
| const response = new Response('ok', { status: 200 }); | ||
|
|
||
| handleTunnelRequestSpy.mockResolvedValueOnce(response); | ||
|
|
||
| const route = createSentryTunnelRoute({ allowedDsns }); | ||
| const result = await route.handlers.POST({ request }); | ||
|
|
||
| expect(handleTunnelRequestSpy).toHaveBeenCalledTimes(1); | ||
| const [options] = handleTunnelRequestSpy.mock.calls[0]!; | ||
| expect(options).toEqual({ | ||
| request, | ||
| allowedDsns, | ||
| }); | ||
| expect(options.allowedDsns).toBe(allowedDsns); | ||
| expect(result).toBe(response); | ||
| }); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||


Uh oh!
There was an error while loading. Please reload this page.