Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow Edge Functions to stream a compressed fetch response #39608

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/next/server/web/sandbox/sandbox.ts
Expand Up @@ -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[]
Expand Down Expand Up @@ -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()
}
Expand Down
Expand Up @@ -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)
Copy link
Contributor Author

@Schniz Schniz Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests, including the test changes introduced in this PR, were failing prior to the code change

},
],
[
'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)
},
],
])
Expand Down