Skip to content

Commit

Permalink
fix bug with encoded responses (honojs#11)
Browse files Browse the repository at this point in the history
This change fixes how we write out a response body so that we can support encoded responses

Previously we would cause a content encoding error by calling `res.text()` on responses even if they are encoded (`br`,`gzip` etc).
Now we check if the response has the `content-encoding` header and use the read/write stream instead if it does.
  • Loading branch information
tavvy authored Dec 1, 2022
1 parent 5a6ccc9 commit 116e4da
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const getRequestListener = (fetchCallback: FetchCallback) => {
}

const contentType = res.headers.get('content-type') || ''
const contentEncoding = res.headers.get('content-encoding')

for (const [k, v] of res.headers) {
if (k === 'set-cookie') {
Expand All @@ -75,9 +76,9 @@ const getRequestListener = (fetchCallback: FetchCallback) => {
outgoing.statusCode = res.status

if (res.body) {
if (contentType.startsWith('text')) {
if (!contentEncoding && contentType.startsWith('text')) {
outgoing.end(await res.text())
} else if (contentType.startsWith('application/json')) {
} else if (!contentEncoding && contentType.startsWith('application/json')) {
outgoing.end(await res.text())
} else {
await writeReadableStreamToWritable(res.body, outgoing)
Expand Down

0 comments on commit 116e4da

Please sign in to comment.