|
1 | | -import { flushIfServerless } from '@sentry/core'; |
2 | | -import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/node'; |
| 1 | +import { flushIfServerless, getTraceMetaTags } from '@sentry/core'; |
| 2 | +import { |
| 3 | + captureException, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 5 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 6 | + startSpan, |
| 7 | +} from '@sentry/node'; |
3 | 8 | import { extractServerFunctionSha256 } from './utils'; |
4 | 9 |
|
5 | 10 | export type ServerEntry = { |
6 | 11 | fetch: (request: Request, opts?: unknown) => Promise<Response> | Response; |
7 | 12 | }; |
8 | 13 |
|
| 14 | +/** |
| 15 | + * This function optimistically assumes that the HTML coming in chunks will not be split |
| 16 | + * within the <head> tag. If this still happens, we simply won't replace anything. |
| 17 | + */ |
| 18 | +function addMetaTagToHead(htmlChunk: string, metaTagsStr: string): string { |
| 19 | + if (typeof htmlChunk !== 'string' || !metaTagsStr) { |
| 20 | + return htmlChunk; |
| 21 | + } |
| 22 | + |
| 23 | + if (htmlChunk.includes('"sentry-trace"')) { |
| 24 | + return htmlChunk; |
| 25 | + } |
| 26 | + |
| 27 | + // Skip quoted attribute values so we don't match <head> inside e.g. data-code="...<head>..." |
| 28 | + let replaced = false; |
| 29 | + return htmlChunk.replace(/"[^"]*"|'[^']*'|(<head>)/g, (match, headTag) => { |
| 30 | + if (headTag && !replaced) { |
| 31 | + replaced = true; |
| 32 | + return `<head>${metaTagsStr}`; |
| 33 | + } |
| 34 | + return match; |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function injectMetaTagsInResponse(originalResponse: Response): Response { |
| 39 | + try { |
| 40 | + const contentType = originalResponse.headers.get('content-type'); |
| 41 | + |
| 42 | + const isPageloadRequest = contentType?.startsWith('text/html'); |
| 43 | + if (!isPageloadRequest) { |
| 44 | + return originalResponse; |
| 45 | + } |
| 46 | + |
| 47 | + // Type case necessary b/c the body's ReadableStream type doesn't include |
| 48 | + // the async iterator that is actually available in Node |
| 49 | + // We later on use the async iterator to read the body chunks |
| 50 | + // see https://github.com/microsoft/TypeScript/issues/39051 |
| 51 | + const originalBody = originalResponse.body as NodeJS.ReadableStream | null; |
| 52 | + if (!originalBody) { |
| 53 | + return originalResponse; |
| 54 | + } |
| 55 | + |
| 56 | + const metaTagsStr = getTraceMetaTags(); |
| 57 | + const decoder = new TextDecoder(); |
| 58 | + |
| 59 | + const newResponseStream = new ReadableStream({ |
| 60 | + start: async controller => { |
| 61 | + // Assign to a new variable to avoid TS losing the narrower type checked above. |
| 62 | + const body = originalBody; |
| 63 | + |
| 64 | + async function* bodyReporter(): AsyncGenerator<string | Buffer> { |
| 65 | + try { |
| 66 | + for await (const chunk of body) { |
| 67 | + yield chunk; |
| 68 | + } |
| 69 | + } catch (e) { |
| 70 | + captureException(e, { |
| 71 | + mechanism: { type: 'auto.http.tanstackstart', handled: false }, |
| 72 | + }); |
| 73 | + throw e; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + let errored = false; |
| 78 | + try { |
| 79 | + for await (const chunk of bodyReporter()) { |
| 80 | + const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true }); |
| 81 | + const modifiedHtml = addMetaTagToHead(html, metaTagsStr); |
| 82 | + controller.enqueue(new TextEncoder().encode(modifiedHtml)); |
| 83 | + } |
| 84 | + } catch (e) { |
| 85 | + errored = true; |
| 86 | + controller.error(e); |
| 87 | + } finally { |
| 88 | + if (!errored) { |
| 89 | + controller.close(); |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + return new Response(newResponseStream, { |
| 96 | + status: originalResponse.status, |
| 97 | + statusText: originalResponse.statusText, |
| 98 | + headers: new Headers(originalResponse.headers), |
| 99 | + }); |
| 100 | + } catch (e) { |
| 101 | + captureException(e, { |
| 102 | + mechanism: { type: 'auto.http.tanstackstart', handled: false }, |
| 103 | + }); |
| 104 | + throw e; |
| 105 | + } |
| 106 | +} |
| 107 | + |
9 | 108 | /** |
10 | 109 | * This function can be used to wrap the server entry request handler to add tracing to server-side functionality. |
11 | 110 | * You must explicitly define a server entry point in your application for this to work. This is done by passing the request handler to the `createServerEntry` function. |
@@ -62,7 +161,7 @@ export function wrapFetchWithSentry(serverEntry: ServerEntry): ServerEntry { |
62 | 161 | ); |
63 | 162 | } |
64 | 163 |
|
65 | | - return await target.apply(thisArg, args); |
| 164 | + return injectMetaTagsInResponse(await target.apply(thisArg, args)); |
66 | 165 | } finally { |
67 | 166 | await flushIfServerless(); |
68 | 167 | } |
|
0 commit comments