Skip to content

Commit

Permalink
fix: make sure metadata sent for bodyless responses
Browse files Browse the repository at this point in the history
The streaming server was not returning the response metadata unless the
body had some content to write to the stream. The fix is to always
force a write with an empty string.
  • Loading branch information
H0R5E committed Sep 9, 2024
1 parent 9d3f692 commit 9554610
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lambda/serverless_streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ export const handler = awslambda.streamifyResponse(

responseStream = awslambda.HttpResponseStream.from(responseStream, metadata)

// Call write on the stream to trigger metadata to be sent
// https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/2ce88619fd176a5823bc5f38c5484d1cbdf95717/src/HttpResponseStream.js#L22
responseStream.write('')

if (rendered) {
setResponse(responseStream, rendered)
} else {
responseStream.end()
}
}
},
)

// Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors) (MIT)
Expand Down Expand Up @@ -116,7 +120,7 @@ export async function setResponse(res, response) {
if (response.body.locked) {
res.end(
'Fatal error: Response body is locked. ' +
`This can happen when the response was already read (for example through 'response.json()' or 'response.text()').`
`This can happen when the response was already read (for example through 'response.json()' or 'response.text()').`,
)
return
}
Expand Down

0 comments on commit 9554610

Please sign in to comment.