Skip to content

Commit

Permalink
bubble up zlib errors (mikeal#102)
Browse files Browse the repository at this point in the history
* bubble up zlib errors

* add 2nd on('error') handler

* add tests for zlib errors

Co-authored-by: Ben Garnett <[email protected]>
  • Loading branch information
mbgarne and Ben Garnett authored May 7, 2020
1 parent 99e7c1f commit 9860b28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const getResponse = resp => {
while (encodings.length) {
const enc = encodings.shift()
if (compression[enc]) {
resp = resp.pipe(compression[enc]())
const decompress = compression[enc]()
decompress.on('error', (e) => ret.emit('error', new Error('ZBufError', e)))
resp = resp.pipe(decompress)
} else {
break
}
Expand Down Expand Up @@ -123,6 +125,7 @@ const mkrequest = (statusCodes, method, encoding, headers, baseurl) => (_url, bo
return new Promise((resolve, reject) => {
const req = h.request(request, async res => {
res = getResponse(res)
res.on('error', reject)
decodings(res)
res.status = res.statusCode
if (!statusCodes.has(res.statusCode)) {
Expand Down
28 changes: 26 additions & 2 deletions test/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
const bent = require('../')
const tsame = require('tsame')
const assert = require('assert')
const zlib = require('zlib')
const ttype = (e, str) => same(e.constructor.name, str)
const qs = require('querystring')

const test = it
const same = (x, y) => assert.ok(tsame(x, y))

const ttype = (e, str) => same(e.constructor.name, str)

test('Invalid encoding', done => {
try {
bent('blah')
Expand Down Expand Up @@ -98,3 +99,26 @@ test('error decodings', async () => {
e = await getError()
same(await e.json(), 'asdf')
})

if (!process.browser) {
test('Z_BUF_ERROR error', async () => {
const request = bent('json')
try {
await request('https://echo-server.mikeal.now.sh/src/echo.js?headers=content-encoding%3Agzip%2Ccontent-type%3Aapplication%2Fjson')
} catch (e) {
ttype(e, 'Error')
return e
}
})
test('gzip json compresssion SyntaxError', async () => {
const request = bent('json')
const base64 = zlib.gzipSync('ok').toString('base64')
const headers = 'content-encoding:gzip,content-type:application/json'
try {
await request(`https://echo-server.mikeal.now.sh/src/echo.js?${qs.stringify({ base64, headers })}`)
} catch (e) {
ttype(e, 'SyntaxError')
return e
}
})
}

0 comments on commit 9860b28

Please sign in to comment.