-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathstreaming.ts
More file actions
37 lines (33 loc) · 1.18 KB
/
streaming.ts
File metadata and controls
37 lines (33 loc) · 1.18 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
export type StreamingGuess = {
isStreaming: boolean;
};
/**
* Classifies a Response as streaming or non-streaming.
*
* Heuristics:
* - No body → not streaming
* - Known streaming Content-Types → streaming (SSE, NDJSON, JSON streaming)
* - 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.
*/
export function classifyResponseStreaming(res: Response): StreamingGuess {
if (!res.body) {
return { isStreaming: false };
}
const contentType = res.headers.get('content-type') ?? '';
// Streaming: Known streaming content types
// - text/event-stream: Server-Sent Events (Vercel AI SDK, real-time APIs)
// - application/x-ndjson, application/ndjson: Newline-delimited JSON
// - application/stream+json: JSON streaming
if (
/^text\/event-stream\b/i.test(contentType) ||
/^application\/(x-)?ndjson\b/i.test(contentType) ||
/^application\/stream\+json\b/i.test(contentType)
) {
return { isStreaming: true };
}
// Default: treat as non-streaming
return { isStreaming: false };
}