Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ function normalizeOptions (options, defaultType) {
var limit = typeof options?.limit === 'undefined' || options?.limit === null
? 102400 // 100kb default
: bytes.parse(options.limit)

if (limit !== null && limit < 0) {
throw new TypeError('option limit must be a non-negative number')
}

var type = options?.type || defaultType
var verify = options?.verify || false
var defaultCharset = options?.defaultCharset || 'utf-8'
Expand Down
22 changes: 22 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ describe('normalizeOptions(options, defaultType)', () => {
normalizeOptions({ limit: { foo: 'bar' } }, 'application/json')
}, /option limit "\[object Object\]" is invalid/)
})

it('should throw an error for negative string limit', () => {
assert.throws(() => {
normalizeOptions({ limit: '-100kb' }, 'application/json')
}, /option limit must be a non-negative number/)
})

it('should throw an error for negative number limit', () => {
assert.throws(() => {
normalizeOptions({ limit: -1024 }, 'application/json')
}, /option limit must be a non-negative number/)
})

it('should accept zero limit', () => {
const result = normalizeOptions({ limit: 0 }, 'application/json')
assert.strictEqual(result.limit, 0)
})

it('should accept zero string limit', () => {
const result = normalizeOptions({ limit: '0kb' }, 'application/json')
assert.strictEqual(result.limit, 0)
})
})

describe('type', () => {
Expand Down