Skip to content

Commit 6b9fc1f

Browse files
authored
add ENABLE_RESPONSE_COMPRESSION option (#919)
* add ENABLE_RESPONSE_COMPRESSION option * add note on compression to README * add audit exceptions with expiry * update changelog
1 parent 8987101 commit 6b9fc1f

File tree

8 files changed

+100
-3
lines changed

8 files changed

+100
-3
lines changed

.nsprc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"1105443": {
3+
"active": true,
4+
"notes": "Ignored, check back for updated deps",
5+
"expiry": "2026-05-01"
6+
},
7+
"1105444": {
8+
"active": true,
9+
"notes": "Ignored, check back for updated deps",
10+
"expiry": "2026-05-01"
11+
}
12+
}

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased] - Unreleased
9+
10+
### Changed
11+
12+
- Compression is enabled by default, can be disabled by setting
13+
`ENABLE_RESPONSE_COMPRESSION` to `false`. If using post-hooks, you must update
14+
to hooks to handle compression or disable compression.
15+
816
## [4.2.0] - 2025-05-05
917

1018
### Added

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,10 @@ There are some settings that should be reviewed and updated as needeed in the se
609609
| CORS_CREDENTIALS | Configure whether or not to send the `Access-Control-Allow-Credentials` CORS header. Header will be sent if set to `true`. | none |
610610
| CORS_METHODS | Configure whether or not to send the `Access-Control-Allow-Methods` CORS header. Expects a comma-delimited string, e.g., `GET,PUT,POST`. | `GET,HEAD,PUT,PATCH,POST,DELETE` |
611611
| CORS_HEADERS | Configure whether or not to send the `Access-Control-Allow-Headers` CORS header. Expects a comma-delimited string, e.g., `Content-Type,Authorization`. If not specified, defaults to reflecting the headers specified in the request’s `Access-Control-Request-Headers` header. | none |
612-
| ENABLE_COLLECTIONS_AUTHX | Enables support for hidden `_collections` query parameter / field when set to `true`. | none (not enabled) |
613-
| ENABLE_THUMBNAILS | Enables support for presigned thumbnails. | none (not enabled) |
614-
| ENABLE_INGEST_ACTION_TRUNCATE | Enables support for ingest action "truncate". | none (not enabled) |
612+
| ENABLE_COLLECTIONS_AUTHX | Enables support for hidden `_collections` query parameter / field when set to `true`. | none (not enabled) |
613+
| ENABLE_THUMBNAILS | Enables support for presigned thumbnails. | none (not enabled) |
614+
| ENABLE_INGEST_ACTION_TRUNCATE | Enables support for ingest action "truncate". | none (not enabled) |
615+
| ENABLE_RESPONSE_COMPRESSION | Enables response compression. Set to 'false' to disable. | enabled |
615616

616617
Additionally, the credential for OpenSearch must be configured, as decribed in the
617618
section [Populating and accessing credentials](#populating-and-accessing-credentials).
@@ -1293,6 +1294,10 @@ The post-hook Lambda configuration may reference any Lambda, not only one deploy
12931294
of this stack. There is an example post-hook Lambda that can be included with this stack,
12941295
which provides an example of how to interact with the response, but does not modify it.
12951296

1297+
If compression is enabled with `ENABLE_RESPONSE_COMPRESSION`, you should ensure that the
1298+
post-hook deployed handles compressed responses, or for the example post-hook lambda,
1299+
disable compression.
1300+
12961301
To enable this example post-hook:
12971302

12981303
- Modify bin/build.sh to not exclude the "post-hook" package from being built.

bin/system-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export AWS_ACCESS_KEY_ID='none'
88
export AWS_SECRET_ACCESS_KEY='none'
99
export ENABLE_TRANSACTIONS_EXTENSION=true
1010
export REQUEST_LOGGING_ENABLED=false
11+
# export ENABLE_RESPONSE_COMPRESSION=false
1112

1213
echo "Running tests"
1314
set +e

package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@mapbox/extent": "^0.4.0",
6060
"@opensearch-project/opensearch": "^2.13.0",
6161
"@redocly/cli": "^1.34.2",
62+
"compression": "^1.8.0",
6263
"cors": "^2.8.5",
6364
"express": "^4.21.2",
6465
"got": "^13.0",

src/lambdas/api/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cors from 'cors'
22
import createError from 'http-errors'
33
import express from 'express'
4+
import compression from 'compression'
45
import morgan from 'morgan'
56
import path from 'path'
67
import { fileURLToPath } from 'url'
@@ -47,6 +48,10 @@ app.use(cors({
4748

4849
app.use(express.json({ limit: '1mb' }))
4950

51+
if (process.env['ENABLE_RESPONSE_COMPRESSION'] !== 'false') {
52+
app.use(compression())
53+
}
54+
5055
app.use(addEndpoint)
5156

5257
app.get('/', async (req, res, next) => {

tests/system/test-api-get-root.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,13 @@ test('GET / returns links with the correct endpoint if `X-STAC-Endpoint` is set'
9999
t.not(apiLink, undefined)
100100
t.is(apiLink.href, `${url}/api`)
101101
})
102+
103+
test('GET / returns a compressed response if ENABLE_RESPONSE_COMPRESSION', async (t) => {
104+
const response = await t.context.api.client.get('', { resolveBodyOnly: false })
105+
106+
if (process.env['ENABLE_RESPONSE_COMPRESSION'] !== 'false') {
107+
t.is(response.headers['content-encoding'], 'br')
108+
} else {
109+
t.true(response.headers['content-encoding'] === undefined)
110+
}
111+
})

0 commit comments

Comments
 (0)