Skip to content

Commit

Permalink
feat: add in no-transform support
Browse files Browse the repository at this point in the history
closes #1252
  • Loading branch information
willfarrell committed Oct 26, 2024
1 parent fc85b3e commit 53fec85
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
74 changes: 59 additions & 15 deletions packages/http-content-encoding/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('It should encode string using br', async (t) => {
httpContentEncoding()
)

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

Expand All @@ -37,7 +37,7 @@ test('It should encode stream using br', async (t) => {
body: createReadableStream(body)
})).use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })
response.body = await streamToBuffer(response.body)
Expand All @@ -55,7 +55,7 @@ test('It should encode string using gzip', async (t) => {
httpContentEncoding()
)

const event = {}
const event = { headers: {} }

const response = await handler(event, {
...context,
Expand All @@ -77,7 +77,7 @@ test('It should encode stream using gzip', async (t) => {
body: createReadableStream(body)
})).use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, {
...context,
Expand All @@ -97,7 +97,7 @@ test('It should encode string using deflate', async (t) => {
const handler = middy((event, context) => ({ statusCode: 200, body }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, {
...context,
Expand All @@ -119,7 +119,7 @@ test('It should encode stream using deflate', async (t) => {
body: createReadableStream(body)
})).use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, {
...context,
Expand Down Expand Up @@ -149,7 +149,7 @@ test('It should encode using br when context.preferredEncoding is gzip, but has
})
)

const event = {}
const event = { headers: {} }

const response = await handler(event, {
...context,
Expand All @@ -170,7 +170,7 @@ test('It should not encode when missing context.preferredEncoding', async (t) =>
const handler = middy((event, context) => ({ statusCode: 200, body }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, context)

Expand All @@ -182,7 +182,7 @@ test('It should not encode when missing context.preferredEncoding === `identity`
const handler = middy((event, context) => ({ statusCode: 200, body }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, {
...context,
Expand All @@ -202,7 +202,7 @@ test('It should not encode when response.isBase64Encoded is already set to true'
}))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

Expand All @@ -219,7 +219,7 @@ test('It should not encode when response.body is not a string', async (t) => {
const handler = middy((event, context) => ({ statusCode: 200, body }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

Expand All @@ -231,7 +231,7 @@ test('It should not encode when response.body is empty string', async (t) => {
const handler = middy((event, context) => ({ statusCode: 200, body }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

Expand All @@ -243,7 +243,7 @@ test('It should not encode when response.body is different type', async (t) => {
const handler = middy((event, context) => ({ statusCode: 200, body }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

Expand All @@ -254,20 +254,64 @@ test('It should not encode when response.body is undefined', async (t) => {
const handler = middy((event, context) => ({ statusCode: 200 }))
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

deepEqual(response, { statusCode: 200, headers: {} })
})

test('It should not encode when response.headers["Cache-Control"] is `no-transform`', async (t) => {
const handler = middy((event, context) => ({
statusCode: 200,
headers: {
'Cache-Control': 'no-transform'
},
body: 'body'
}))
handler.use(httpContentEncoding())

const event = { headers: {} }

const response = await handler(event, { ...context, preferredEncoding: 'br' })

deepEqual(response, {
statusCode: 200,
headers: { 'Cache-Control': 'no-transform' },
body: 'body'
})
})

test('It should not encode when event.headers["Cache-Control"] is `no-transform`', async (t) => {
const handler = middy((event, context) => ({
statusCode: 200,

body: 'body'
}))
handler.use(httpContentEncoding())

const event = {
headers: {
'Cache-Control': 'no-transform'
}
}

const response = await handler(event, { ...context, preferredEncoding: 'br' })

deepEqual(response, {
statusCode: 200,
headers: { 'Cache-Control': 'no-transform' },
body: 'body'
})
})

test('It should not encode when error is not handled', async (t) => {
const handler = middy((event, context) => {
throw new Error('error')
})
handler.use(httpContentEncoding())

const event = {}
const event = { headers: {} }

try {
await handler(event, context)
Expand Down
13 changes: 11 additions & 2 deletions packages/http-content-encoding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ const httpContentEncodingMiddleware = (opts) => {
response
} = request

// Encoding not supported OR already encoded
// Encoding not supported, already encoded, or doesn't need to'
const eventCacheControl =
request.event.headers['cache-control'] ??
request.event.headers['Cache-Control']
if (eventCacheControl === 'no-transform') {
response.headers['Cache-Control'] ??= 'no-transform'
}
const responseCacheControl =
response.headers['Cache-Control'] ?? response.headers['cache-control']
if (
response.isBase64Encoded ||
!preferredEncoding ||
!supportedContentEncodings.includes(preferredEncoding) ||
!response.body
!response.body ||
responseCacheControl === 'no-transform'
) {
return
}
Expand Down
4 changes: 1 addition & 3 deletions website/docs/upgrade/4-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ No change
### [http-content-encoding](/docs/middlewares/http-content-encoding)

- Use `preferredLanguage` from `context` instead of `event` (See http-content-negotiation). **Breaking Change**
- Add in `Vary` support ([#1253](https://github.com/middyjs/middy/issues/1253)) **Breaking Change**

### [http-content-negotiation](/docs/middlewares/http-content-negotiation)

Expand Down Expand Up @@ -99,8 +98,7 @@ No change

### [http-security-headers](/docs/middlewares/http-security-headers)

- Add in support for `Content-Security-Policy-Report-Only` ([#1248](https://github.com/middyjs/middy/issues/1248))
- Add in support for `Reporting-Endpoints` ([#1249](https://github.com/middyjs/middy/issues/1249))
No change

### [http-urlencode-body-parser](/docs/middlewares/http-urlencode-body-parser)

Expand Down
6 changes: 4 additions & 2 deletions website/docs/upgrade/5-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ No change

### [http-content-encoding](/docs/middlewares/http-content-encoding)

No change
- Add in `Vary` support ([#1253](https://github.com/middyjs/middy/issues/1253)) **Breaking Change**
- Add in support to skip encoding when `Cache-Control: no-transform` is used ([#1252](https://github.com/middyjs/middy/issues/1252)) **breaking Change**

### [http-content-negotiation](/docs/middlewares/http-content-negotiation)

Expand Down Expand Up @@ -87,7 +88,8 @@ No change

### [http-security-headers](/docs/middlewares/http-security-headers)

No change
- Add in support for `Content-Security-Policy-Report-Only` ([#1248](https://github.com/middyjs/middy/issues/1248))
- Add in support for `Reporting-Endpoints` ([#1249](https://github.com/middyjs/middy/issues/1249))

### [http-urlencode-body-parser](/docs/middlewares/http-urlencode-body-parser)

Expand Down

0 comments on commit 53fec85

Please sign in to comment.