Skip to content

Commit

Permalink
feat: add in Vary support
Browse files Browse the repository at this point in the history
closes: #1253
  • Loading branch information
willfarrell committed Oct 26, 2024
1 parent ba24146 commit 01e049b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/http-content-encoding/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('It should encode string using br', async (t) => {
deepEqual(response, {
statusCode: 200,
body: brotliCompressSync(body).toString('base64'),
headers: { 'Content-Encoding': 'br' },
headers: { 'Content-Encoding': 'br', Vary: 'Accept-Encoding' },
isBase64Encoded: true
})
})
Expand All @@ -45,7 +45,7 @@ test('It should encode stream using br', async (t) => {
deepEqual(response, {
statusCode: 200,
body: brotliCompressSync(body).toString('base64'),
headers: { 'Content-Encoding': 'br' }
headers: { 'Content-Encoding': 'br', Vary: 'Accept-Encoding' }
})
})

Expand All @@ -65,7 +65,7 @@ test('It should encode string using gzip', async (t) => {
deepEqual(response, {
statusCode: 200,
body: gzipSync(body).toString('base64'),
headers: { 'Content-Encoding': 'gzip' },
headers: { 'Content-Encoding': 'gzip', Vary: 'Accept-Encoding' },
isBase64Encoded: true
})
})
Expand All @@ -88,7 +88,7 @@ test('It should encode stream using gzip', async (t) => {
deepEqual(response, {
statusCode: 200,
body: gzipSync(body).toString('base64'),
headers: { 'Content-Encoding': 'gzip' }
headers: { 'Content-Encoding': 'gzip', Vary: 'Accept-Encoding' }
})
})

Expand All @@ -107,7 +107,7 @@ test('It should encode string using deflate', async (t) => {
deepEqual(response, {
statusCode: 200,
body: deflateSync(body).toString('base64'),
headers: { 'Content-Encoding': 'deflate' },
headers: { 'Content-Encoding': 'deflate', Vary: 'Accept-Encoding' },
isBase64Encoded: true
})
})
Expand All @@ -130,13 +130,19 @@ test('It should encode stream using deflate', async (t) => {
deepEqual(response, {
statusCode: 200,
body: deflateSync(body).toString('base64'),
headers: { 'Content-Encoding': 'deflate' }
headers: { 'Content-Encoding': 'deflate', Vary: 'Accept-Encoding' }
})
})

test('It should encode using br when context.preferredEncoding is gzip, but has overridePreferredEncoding set', async (t) => {
const body = compressibleBody
const handler = middy((event, context) => ({ statusCode: 200, body }))
const handler = middy((event, context) => ({
statusCode: 200,
body,
headers: {
Vary: 'Something'
}
}))
handler.use(
httpContentEncoding({
overridePreferredEncoding: ['br', 'gzip', 'deflate']
Expand All @@ -154,7 +160,7 @@ test('It should encode using br when context.preferredEncoding is gzip, but has
deepEqual(response, {
statusCode: 200,
body: brotliCompressSync(body).toString('base64'),
headers: { 'Content-Encoding': 'br' },
headers: { 'Content-Encoding': 'br', Vary: 'Something, Accept-Encoding' },
isBase64Encoded: true
})
})
Expand Down
12 changes: 12 additions & 0 deletions packages/http-content-encoding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const httpContentEncodingMiddleware = (opts) => {
if (response.body?._readableState) {
request.response.headers['Content-Encoding'] = contentEncoding
request.response.body = request.response.body.pipe(contentEncodingStream)
addVary(response)
return
}

Expand All @@ -87,6 +88,7 @@ const httpContentEncodingMiddleware = (opts) => {
response.headers['Content-Encoding'] = contentEncoding
response.body = body
response.isBase64Encoded = true
addVary(response)
}

request.response = response
Expand All @@ -103,4 +105,14 @@ const httpContentEncodingMiddleware = (opts) => {
}
}

const addVary = (response) => {
// See #1253
if (response.headers.Vary) {
response.headers.Vary += ', Accept-Encoding'
} else if (response.headers.vary) {
response.headers.vary += ', Accept-Encoding'
} else {
response.headers.Vary = 'Accept-Encoding'
}
}
export default httpContentEncodingMiddleware

0 comments on commit 01e049b

Please sign in to comment.