Skip to content

Commit 5907df6

Browse files
authored
Merge pull request #17 from fastify/update-fastify
Updated to support the latest version of fastify
2 parents 73f9324 + 455bb7e commit 5907df6

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function compressPlugin (fastify, opts, next) {
2828
next()
2929

3030
function compress (payload) {
31-
if (!payload) {
31+
if (payload == null) {
3232
this.res.log.warn('compress: missing payload')
3333
this.send(new Error('Internal server error'))
3434
return
@@ -55,7 +55,7 @@ function compressPlugin (fastify, opts, next) {
5555
return
5656
}
5757

58-
if (payload._readableState === undefined) {
58+
if (typeof payload.pipe !== 'function') {
5959
if (typeof payload !== 'string') {
6060
payload = this.serialize(payload)
6161
}
@@ -72,7 +72,7 @@ function compressPlugin (fastify, opts, next) {
7272
}
7373

7474
function onSend (req, reply, payload, next) {
75-
if (!payload) {
75+
if (payload == null) {
7676
reply.res.log.warn('compress: missing payload')
7777
return next()
7878
}
@@ -99,10 +99,7 @@ function compressPlugin (fastify, opts, next) {
9999
return next()
100100
}
101101

102-
if (payload._readableState === undefined) {
103-
if (typeof payload !== 'string') {
104-
payload = reply.serialize(payload)
105-
}
102+
if (typeof payload.pipe !== 'function') {
106103
if (Buffer.byteLength(payload) < threshold) {
107104
return next()
108105
}
@@ -153,6 +150,6 @@ function shouldCompress (type) {
153150
}
154151

155152
module.exports = fp(compressPlugin, {
156-
fastify: '>=0.39.1',
153+
fastify: '>=0.40.0',
157154
name: 'fastify-compress'
158155
})

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"dependencies": {
77
"fastify-plugin": "^0.2.1",
88
"mime-db": "^1.32.0",
9-
"pump": "^2.0.0",
9+
"pump": "^2.0.1",
1010
"string-to-stream": "^1.1.0"
1111
},
1212
"devDependencies": {
13-
"fastify": "^0.39.1",
13+
"fastify": "^0.40.0",
1414
"iltorb": "^2.0.3",
15+
"jsonstream": "^1.0.3",
1516
"standard": "^10.0.3",
1617
"tap": "^11.0.1"
1718
},

test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const test = t.test
55
const brotli = require('iltorb')
66
const zlib = require('zlib')
77
const fs = require('fs')
8+
const JSONStream = require('jsonstream')
89
const createReadStream = fs.createReadStream
910
const readFileSync = fs.readFileSync
1011
const Fastify = require('fastify')
@@ -639,3 +640,55 @@ test('identity header (hook)', t => {
639640
t.deepEqual({ hello: 'world' }, payload)
640641
})
641642
})
643+
644+
test('should support stream1 (reply compress)', t => {
645+
t.plan(3)
646+
const fastify = Fastify()
647+
fastify.register(compressPlugin, { global: false })
648+
649+
fastify.get('/', (req, reply) => {
650+
const stream = JSONStream.stringify()
651+
reply.type('text/plain').compress(stream)
652+
stream.write({ hello: 'world' })
653+
stream.end({ a: 42 })
654+
})
655+
656+
fastify.inject({
657+
url: '/',
658+
method: 'GET',
659+
headers: {
660+
'accept-encoding': 'gzip'
661+
}
662+
}, (err, res) => {
663+
t.error(err)
664+
t.strictEqual(res.headers['content-encoding'], 'gzip')
665+
const payload = zlib.gunzipSync(res.rawPayload)
666+
t.deepEqual(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }])
667+
})
668+
})
669+
670+
test('should support stream1 (global hook)', t => {
671+
t.plan(3)
672+
const fastify = Fastify()
673+
fastify.register(compressPlugin, { threshold: 0 })
674+
675+
fastify.get('/', (req, reply) => {
676+
const stream = JSONStream.stringify()
677+
reply.type('text/plain').send(stream)
678+
stream.write({ hello: 'world' })
679+
stream.end({ a: 42 })
680+
})
681+
682+
fastify.inject({
683+
url: '/',
684+
method: 'GET',
685+
headers: {
686+
'accept-encoding': 'gzip'
687+
}
688+
}, (err, res) => {
689+
t.error(err)
690+
t.strictEqual(res.headers['content-encoding'], 'gzip')
691+
const payload = zlib.gunzipSync(res.rawPayload)
692+
t.deepEqual(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }])
693+
})
694+
})

0 commit comments

Comments
 (0)