Skip to content

Commit 2322e11

Browse files
Phillip9587ctcpip
andauthored
fix: improve limit option validation (#698)
Co-authored-by: Chris de Almeida <ctcpip@users.noreply.github.com>
1 parent f644408 commit 2322e11

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

lib/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ function normalizeOptions (options, defaultType) {
6060
}
6161

6262
var inflate = options?.inflate !== false
63-
var limit = typeof options?.limit !== 'number'
64-
? bytes.parse(options?.limit || '100kb')
65-
: options?.limit
63+
var limit = typeof options?.limit === 'undefined' || options?.limit === null
64+
? 102400 // 100kb default
65+
: bytes.parse(options.limit)
6666
var type = options?.type || defaultType
6767
var verify = options?.verify || false
6868
var defaultCharset = options?.defaultCharset || 'utf-8'
6969

70+
if (limit === null) {
71+
throw new TypeError(`option limit "${String(options.limit)}" is invalid`)
72+
}
73+
7074
if (verify !== false && typeof verify !== 'function') {
7175
throw new TypeError('option verify must be function')
7276
}

test/utils.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ describe('normalizeOptions(options, defaultType)', () => {
7171
assert.strictEqual(result.limit, 100 * 1024) // 100kb in bytes
7272
})
7373

74+
it('should return the default limit if limit is undefined', () => {
75+
const result = normalizeOptions({ limit: undefined }, 'application/json')
76+
assert.strictEqual(result.limit, 100 * 1024) // 100kb in bytes
77+
})
78+
79+
it('should return the default limit if limit is null', () => {
80+
const result = normalizeOptions({ limit: null }, 'application/json')
81+
assert.strictEqual(result.limit, 100 * 1024) // 100kb in bytes
82+
})
83+
84+
it('should accept zero as valid limit', () => {
85+
const result = normalizeOptions({ limit: 0 }, 'application/json')
86+
assert.strictEqual(result.limit, 0)
87+
})
88+
7489
it('should accept a number limit', () => {
7590
const result = normalizeOptions({ limit: 1234 }, 'application/json')
7691
assert.strictEqual(result.limit, 1234)
@@ -81,9 +96,39 @@ describe('normalizeOptions(options, defaultType)', () => {
8196
assert.strictEqual(result.limit, 200 * 1024) // 200kb in bytes
8297
})
8398

84-
it('should return null for an invalid limit', () => {
85-
const result = normalizeOptions({ limit: 'invalid' }, 'application/json')
86-
assert.strictEqual(result.limit, null)
99+
it('should parse a string limit without a unit', () => {
100+
const result = normalizeOptions({ limit: '200' }, 'application/json')
101+
assert.strictEqual(result.limit, 200) // 200 bytes
102+
})
103+
104+
it('should throw an error for an invalid string limit', () => {
105+
assert.throws(() => {
106+
normalizeOptions({ limit: 'invalid' }, 'application/json')
107+
}, /option limit "invalid" is invalid/)
108+
assert.throws(() => {
109+
normalizeOptions({ limit: '' }, 'application/json')
110+
}, /option limit "" is invalid/)
111+
})
112+
113+
it('should throw an error for a NaN limit', () => {
114+
assert.throws(() => {
115+
normalizeOptions({ limit: NaN }, 'application/json')
116+
}, /option limit "NaN" is invalid/)
117+
})
118+
119+
it('should throw an error for a boolean limit', () => {
120+
assert.throws(() => {
121+
normalizeOptions({ limit: true }, 'application/json')
122+
}, /option limit "true" is invalid/)
123+
assert.throws(() => {
124+
normalizeOptions({ limit: false }, 'application/json')
125+
}, /option limit "false" is invalid/)
126+
})
127+
128+
it('should throw an error for an object limit', () => {
129+
assert.throws(() => {
130+
normalizeOptions({ limit: { foo: 'bar' } }, 'application/json')
131+
}, /option limit "\[object Object\]" is invalid/)
87132
})
88133
})
89134

0 commit comments

Comments
 (0)