Skip to content

Commit 2c3211f

Browse files
authored
fix logger reference in onEnd (#119)
1 parent b670c3b commit 2c3211f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ function compress (params) {
382382
}
383383

384384
function onEnd (err) {
385-
if (err) this.raw.log.error(err)
385+
if (err) this.log.error(err)
386386
}
387387

388388
function trackEncodedLength (chunk) {

test/test-global-compress.js

+43
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const test = t.test
55
const zlib = require('zlib')
66
const fs = require('fs')
77
const JSONStream = require('jsonstream')
8+
const { Readable, Writable } = require('stream')
89
const createReadStream = fs.createReadStream
910
const readFileSync = fs.readFileSync
1011
const Fastify = require('fastify')
@@ -1692,3 +1693,45 @@ test('Should send data gzipped according to zlibOptions', t => {
16921693
t.same(res.rawPayload, zlib.gzipSync(fileBuffer, zlibOptions))
16931694
})
16941695
})
1696+
1697+
test('stream onEnd handler should log an error if exists', t => {
1698+
t.plan(1)
1699+
1700+
let actual = null
1701+
const logger = new Writable({
1702+
write (chunk, encoding, callback) {
1703+
actual = JSON.parse(chunk.toString())
1704+
}
1705+
})
1706+
1707+
const fastify = Fastify({
1708+
global: false,
1709+
logger: {
1710+
level: 'error',
1711+
stream: logger
1712+
}
1713+
})
1714+
1715+
fastify.register(compressPlugin)
1716+
1717+
const expect = new Error('something wrong')
1718+
1719+
fastify.get('/', (req, reply) => {
1720+
const stream = new Readable({
1721+
read (size) {
1722+
this.destroy(expect)
1723+
}
1724+
})
1725+
reply.type('text/plain').compress(stream)
1726+
})
1727+
1728+
fastify.inject({
1729+
url: '/',
1730+
method: 'GET',
1731+
headers: {
1732+
'accept-encoding': 'gzip'
1733+
}
1734+
}, (_, res) => {
1735+
t.equal(actual.msg, expect.message)
1736+
})
1737+
})

0 commit comments

Comments
 (0)