@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
23 lines (22 loc) • 1.14 kB
JavaScript
// src/utils/responseStreaming.ts
/**
* Classifies a Response as streaming or non-streaming.
*
* Heuristics:
* - No body → not streaming
* - Known streaming Content-Types → streaming (SSE, NDJSON, JSON streaming)
* - text/plain or text/x-component without Content-Length → streaming (some AI APIs, streamed RSC)
* - Otherwise → not streaming (conservative default, including HTML/SSR)
*
* We avoid probing the stream to prevent blocking on transform streams (like injectTraceMetaTags)
* or SSR streams that may not have data ready immediately.
*/
function classifyResponseStreaming(res) {
if (!res.body) return { isStreaming: false };
const contentType = res.headers.get("content-type") ?? "";
const contentLength = res.headers.get("content-length");
if (/^text\/event-stream\b/i.test(contentType) || /^application\/(x-)?ndjson\b/i.test(contentType) || /^application\/stream\+json\b/i.test(contentType) || /^text\/(plain|x-component)\b/i.test(contentType) && !contentLength) return { isStreaming: true };
return { isStreaming: false };
}
export { classifyResponseStreaming };
//# sourceMappingURL=responseStreaming.js.map