Unverified Commit 41d4aa04 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

allow Edge Functions to stream a compressed `fetch` response (#39608)

When a user tried to have the following Edge Function:

```ts
export default () => fetch("https://example.vercel.sh");
```

The Edge Function were failing.

Why is that?

When `fetch` was called, an implicit `Accept-Encoding` header was added
to allow the origin to return a compressed response. Then, the origin
will set the `Content-Encoding` header in the response, to let the
client know that the body needs to be decompressed in order to be read.

That creates an issue though: `response.body` will be a
`ReadableStream<Uint8Array>`, or, a stream that contains binary data
that decodes into _the uncompressed data_ (or, plain text!).

What it means, is that `response.body` is uncompressed data, while
`response.headers.get('content-encoding')` is marking the response body
as compressed payload. This confuses the HTTP clients and makes them fail.

This commit removes the `content-encoding`, `transfer-encoding` and
`content-length` headers from the response, as the Next.js server _always_
streams Edge Function responses.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent bf82a473
......@@ -6,6 +6,12 @@ import { requestToBodyStream } from '../../body-streams'
export const ErrorSource = Symbol('SandboxError')
const FORBIDDEN_HEADERS = [
'content-length',
'content-encoding',
'transfer-encoding',
]
type RunnerFn = (params: {
name: string
env: string[]
......@@ -74,12 +80,16 @@ export const run = withTaggedErrors(async (params) => {
: undefined
try {
return await edgeFunction({
const result = await edgeFunction({
request: {
...params.request,
body: cloned && requestToBodyStream(runtime.context, cloned),
},
})
for (const headerName of FORBIDDEN_HEADERS) {
result.response.headers.delete(headerName)
}
return result
} finally {
await params.request.body?.finalize()
}
......
......@@ -38,20 +38,14 @@ const handlers = new Map([
'remote-full',
async () => {
const url = new URL('https://example.vercel.sh')
const response = await fetch(url)
const headers = new Headers(response.headers)
headers.delete('content-encoding')
return new Response(response.body, { headers, status: response.status })
return fetch(url)
},
],
[
'remote-with-base',
async () => {
const url = new URL('/', 'https://example.vercel.sh')
const response = await fetch(url)
const headers = new Headers(response.headers)
headers.delete('content-encoding')
return new Response(response.body, { headers, status: response.status })
return fetch(url)
},
],
])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment