Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,6 @@ const createProxy = (
response.headers
.get('content-type')
?.startsWith('text/') &&
response.headers.get('transfer-encoding') ===
'chunked' &&
!response.headers.has('content-length')
)
data = streamResponse(response, {
Expand Down
61 changes: 61 additions & 0 deletions test/treaty2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,67 @@ function createChunkedSSEResponse(chunks: Array<string>): Response {
})
}

describe('Treaty2 - HTTP/2 streaming detection (#258)', () => {
function makeStreamingResponse(headers: HeadersInit): Response {
const encoder = new TextEncoder()
const stream = new ReadableStream<Uint8Array>({
async start(controller) {
controller.enqueue(encoder.encode('chunk-1'))
controller.enqueue(encoder.encode('chunk-2'))
controller.enqueue(encoder.encode('chunk-3'))
controller.close()
}
})
return new Response(stream, { headers })
}

it('streams text/plain when Transfer-Encoding is stripped (HTTP/2 / reverse proxy)', async () => {
// Reverse proxies serving HTTP/2 strip Transfer-Encoding (RFC 7540
// §8.1.2.2). Eden should still detect a stream when there's no
// Content-Length, which is the only legal way to signal a streaming
// body in either HTTP/1.1 (chunked, no length) or HTTP/2 (no length).
const client = treaty<any>('http://h2.test', {
fetcher: async () =>
makeStreamingResponse({ 'Content-Type': 'text/plain' })
})

const { data } = await (client as any).live.get()
expect(typeof data?.[Symbol.asyncIterator]).toBe('function')

const collected: unknown[] = []
for await (const chunk of data) collected.push(chunk)
expect(collected).toEqual(['chunk-1', 'chunk-2', 'chunk-3'])
})

it('streams text/plain with Transfer-Encoding: chunked (HTTP/1.1 direct)', async () => {
const client = treaty<any>('http://h1.test', {
fetcher: async () =>
makeStreamingResponse({
'Content-Type': 'text/plain',
'Transfer-Encoding': 'chunked'
})
})

const { data } = await (client as any).live.get()
expect(typeof data?.[Symbol.asyncIterator]).toBe('function')
})

it('still buffers text/plain when Content-Length is present (non-streaming)', async () => {
const client = treaty<any>('http://buffered.test', {
fetcher: async () =>
new Response('hello', {
headers: {
'Content-Type': 'text/plain',
'Content-Length': '5'
}
})
})

const { data } = await (client as any).hello.get()
expect(data).toBe('hello')
})
})

describe('Treaty2 - SSE Chunk Splitting (fast streaming edge cases)', () => {
it('handles SSE event split across chunks (data: broken mid-line)', async () => {
const chunks = ['event: message\nda', 'ta: hello world\n\n']
Expand Down