Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
54 lines (45 loc) · 1.78 KB
/
Copy pathmiddleware.ts
File metadata and controls
54 lines (45 loc) · 1.78 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { addNonEnumerableProperty, getTraceMetaTags } from '@sentry/core';
import type { ResponseMiddleware } from '@solidjs/start/middleware';
import type { FetchEvent } from '@solidjs/start/server';
export type ResponseMiddlewareResponse = Parameters<ResponseMiddleware>[1] & {
__sentry_wrapped__?: boolean;
};
function addMetaTagToHead(html: string): string {
const metaTags = getTraceMetaTags();
if (!metaTags) {
return html;
}
const content = `<head>\n${metaTags}\n`;
return html.replace('<head>', content);
}
/**
* Returns an `onBeforeResponse` solid start middleware handler that adds tracing data as
* <meta> tags to a page on pageload to enable distributed tracing.
*/
export function sentryBeforeResponseMiddleware() {
return async function onBeforeResponse(event: FetchEvent, response: ResponseMiddlewareResponse) {
if (!response.body || response.__sentry_wrapped__) {
return;
}
// Ensure we don't double-wrap, in case a user has added the middleware twice
// e.g. once manually, once via the wizard
addNonEnumerableProperty(response, '__sentry_wrapped__', true);
const contentType = event.response.headers.get('content-type');
const isPageloadRequest = contentType?.startsWith('text/html');
if (!isPageloadRequest) {
return;
}
const body = response.body as NodeJS.ReadableStream;
const decoder = new TextDecoder();
response.body = new ReadableStream({
start: async controller => {
for await (const chunk of body) {
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
const modifiedHtml = addMetaTagToHead(html);
controller.enqueue(new TextEncoder().encode(modifiedHtml));
}
controller.close();
},
});
};
}