Skip to content

Commit 237737e

Browse files
authored
add ITEMS_MAX_LIMIT configuration to override hard-coded default of 10000 (#942)
* add @types/compression dependency so 'npm run test:unit' will run * add ITEMS_MAX_LIMIT variable to configure max items in a search response * handle invalid value for ITEMS_MAX_LIMIT better
1 parent 6b9fc1f commit 237737e

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
`ENABLE_RESPONSE_COMPRESSION` to `false`. If using post-hooks, you must update
1414
to hooks to handle compression or disable compression.
1515

16+
### Added
17+
18+
- The maximum limit for the number of items returned from the /search and
19+
/collections/{collection_id}/items endpoints can now be configured with the
20+
`ITEMS_MAX_LIMIT` variable. It is recommended that this be set to 100.
21+
1622
## [4.2.0] - 2025-05-05
1723

1824
### Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ There are some settings that should be reviewed and updated as needeed in the se
613613
| ENABLE_THUMBNAILS | Enables support for presigned thumbnails. | none (not enabled) |
614614
| ENABLE_INGEST_ACTION_TRUNCATE | Enables support for ingest action "truncate". | none (not enabled) |
615615
| ENABLE_RESPONSE_COMPRESSION | Enables response compression. Set to 'false' to disable. | enabled |
616+
| ITEMS_MAX_LIMIT | The maximum limit for the number of items returned from the /search and /collections/{collection_id}/items endpoints. It is recommended that this be set to 100. There is an absolute max limit of 10000 for this. | 10000 |
616617

617618
Additionally, the credential for OpenSearch must be configured, as decribed in the
618619
section [Populating and accessing credentials](#populating-and-accessing-credentials).

package-lock.json

Lines changed: 12 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
@@ -80,6 +80,7 @@
8080
"@stoplight/spectral-cli": "^6.15.0",
8181
"@tsconfig/node22": "^22.0.1",
8282
"@types/aws-lambda": "^8.10.149",
83+
"@types/compression": "^1.8.1",
8384
"@types/cors": "^2.8.17",
8485
"@types/express": "^4.17.21",
8586
"@types/http-errors": "^2.0.4",

src/lib/api.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,20 @@ export const extractLimit = function (params) {
9595

9696
if (Number.isNaN(limit) || limit <= 0) {
9797
throw new ValidationError(
98-
'Invalid limit value, must be a number between 1 and 10000 inclusive'
98+
'Invalid limit value, must be a positive number'
9999
)
100100
}
101-
if (limit > 10000) {
102-
limit = 10000
101+
102+
let itemsMaxLimit = Number(process.env['ITEMS_MAX_LIMIT'])
103+
if (Number.isNaN(itemsMaxLimit) || itemsMaxLimit <= 0) {
104+
itemsMaxLimit = Number.MAX_SAFE_INTEGER
103105
}
106+
limit = Math.min(
107+
itemsMaxLimit,
108+
limit || Number.MAX_SAFE_INTEGER,
109+
10000
110+
)
111+
104112
return limit
105113
}
106114
return undefined

tests/unit/test-api-limit.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import test from 'ava'
44
import { extractLimit } from '../../src/lib/api.js'
55
import { ValidationError } from '../../src/lib/errors.js'
66

7+
test.beforeEach(() => {
8+
delete process.env['ITEMS_MAX_LIMIT']
9+
})
10+
711
test('extractLimit undefined', (t) => {
812
t.falsy(extractLimit({}), 'Returns undefined when no limit parameter')
913
})
@@ -16,6 +20,21 @@ test('extractLimit when over max limit', (t) => {
1620
t.is(extractLimit({ limit: '10001' }), 10000)
1721
})
1822

23+
test('extractLimit when over max limit and ITEMS_MAX_LIMIT set', (t) => {
24+
process.env['ITEMS_MAX_LIMIT'] = '100'
25+
t.is(extractLimit({ limit: '8374' }), 100)
26+
})
27+
28+
test('extractLimit when over max limit and ITEMS_MAX_LIMIT set over absolute max limit', (t) => {
29+
process.env['ITEMS_MAX_LIMIT'] = '10001'
30+
t.is(extractLimit({ limit: '10002' }), 10000)
31+
})
32+
33+
test('extractLimit when ITEMS_MAX_LIMIT is invalid', (t) => {
34+
process.env['ITEMS_MAX_LIMIT'] = 'foo'
35+
t.is(extractLimit({ limit: '10002' }), 10000)
36+
})
37+
1938
test('extractLimit invalid values', (t) => {
2039
const invalidLimits = ['', '-1', '0', 'a', -1, 0]
2140

0 commit comments

Comments
 (0)