|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fp = require('fastify-plugin') |
| 4 | +const zlib = require('zlib') |
| 5 | +const pump = require('pump') |
| 6 | +const sts = require('string-to-stream') |
| 7 | +const mimedb = require('mime-db') |
| 8 | +const supportedEncodings = ['deflate', 'gzip', 'br', 'identity'] |
| 9 | +const compressibleTypes = /^text\/|\+json$|\+text$|\+xml$/ |
| 10 | + |
| 11 | +function compressPlugin (fastify, opts, next) { |
| 12 | + fastify.decorateReply('compress', compress) |
| 13 | + |
| 14 | + if (opts.global !== false) { |
| 15 | + fastify.addHook('onSend', onSend) |
| 16 | + } |
| 17 | + |
| 18 | + const threshold = typeof opts.threshold === 'number' ? opts.threshold : 1024 |
| 19 | + const compressStream = { |
| 20 | + gzip: zlib.createGzip, |
| 21 | + deflate: zlib.createDeflate |
| 22 | + } |
| 23 | + |
| 24 | + if (opts.brotli) { |
| 25 | + compressStream.br = opts.brotli.compressStream |
| 26 | + } |
| 27 | + |
| 28 | + next() |
| 29 | + |
| 30 | + function compress (payload) { |
| 31 | + if (!payload) { |
| 32 | + this.res.log.warn('compress: missing payload') |
| 33 | + this.send(new Error('Internal server error')) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if (this.request.headers['x-no-compression'] !== undefined) { |
| 38 | + return this.send(payload) |
| 39 | + } |
| 40 | + |
| 41 | + var type = this.res.getHeader('Content-Type') || 'application/json' |
| 42 | + if (shouldCompress(type) === false) { |
| 43 | + return this.send(payload) |
| 44 | + } |
| 45 | + |
| 46 | + var encoding = getEncodingHeader(this.request) |
| 47 | + |
| 48 | + if (encoding === undefined) { |
| 49 | + closeStream(payload) |
| 50 | + this.code(400).send(new Error('Missing `accept encoding` header')) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + if (encoding === 'identity') { |
| 55 | + return this.send(payload) |
| 56 | + } |
| 57 | + |
| 58 | + if (encoding === null) { |
| 59 | + closeStream(payload) |
| 60 | + this.code(406).send(new Error('Unsupported encoding')) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if (payload._readableState === undefined) { |
| 65 | + if (typeof payload !== 'string') { |
| 66 | + payload = this.serialize(payload) |
| 67 | + } |
| 68 | + if (Buffer.byteLength(payload) < threshold) { |
| 69 | + return this.send(payload) |
| 70 | + } |
| 71 | + payload = sts(payload) |
| 72 | + } |
| 73 | + |
| 74 | + this.header('Content-Encoding', encoding) |
| 75 | + this.send(pump( |
| 76 | + payload, |
| 77 | + compressStream[encoding](), |
| 78 | + onEnd.bind(this)) |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + function onSend (req, reply, payload, next) { |
| 83 | + if (!payload) { |
| 84 | + reply.res.log.warn('compress: missing payload') |
| 85 | + return next() |
| 86 | + } |
| 87 | + |
| 88 | + if (req.headers['x-no-compression'] !== undefined) { |
| 89 | + return next() |
| 90 | + } |
| 91 | + |
| 92 | + var type = reply.res.getHeader('Content-Type') || 'application/json' |
| 93 | + if (shouldCompress(type) === false) { |
| 94 | + return next() |
| 95 | + } |
| 96 | + |
| 97 | + var encoding = getEncodingHeader(req) |
| 98 | + |
| 99 | + if (encoding === undefined) { |
| 100 | + closeStream(payload) |
| 101 | + reply.code(400) |
| 102 | + next(new Error('Missing `accept encoding` header')) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + if (encoding === null) { |
| 107 | + closeStream(payload) |
| 108 | + reply.code(406) |
| 109 | + next(new Error('Unsupported encoding')) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + if (encoding === 'identity') { |
| 114 | + return next() |
| 115 | + } |
| 116 | + |
| 117 | + if (payload._readableState === undefined) { |
| 118 | + if (typeof payload !== 'string') { |
| 119 | + payload = reply.serialize(payload) |
| 120 | + } |
| 121 | + if (Buffer.byteLength(payload) < threshold) { |
| 122 | + return next() |
| 123 | + } |
| 124 | + payload = sts(payload) |
| 125 | + } |
| 126 | + |
| 127 | + reply.header('Content-Encoding', encoding) |
| 128 | + next(null, pump( |
| 129 | + payload, |
| 130 | + compressStream[encoding](), |
| 131 | + onEnd.bind(reply)) |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +function onEnd (err) { |
| 137 | + if (err) this.res.log.error(err) |
| 138 | +} |
| 139 | + |
| 140 | +function closeStream (payload) { |
| 141 | + if (typeof payload.close === 'function') { |
| 142 | + payload.close() |
| 143 | + } else if (typeof payload.destroy === 'function') { |
| 144 | + payload.destroy() |
| 145 | + } else if (typeof payload.abort === 'function') { |
| 146 | + payload.abort() |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +function getEncodingHeader (request) { |
| 151 | + var header = request.headers['accept-encoding'] |
| 152 | + if (!header) return undefined |
| 153 | + var acceptEncodings = header.split(',') |
| 154 | + for (var i = 0; i < acceptEncodings.length; i++) { |
| 155 | + if (supportedEncodings.indexOf(acceptEncodings[i]) > -1) { |
| 156 | + return acceptEncodings[i] |
| 157 | + } |
| 158 | + } |
| 159 | + return null |
| 160 | +} |
| 161 | + |
| 162 | +function shouldCompress (type) { |
| 163 | + if (compressibleTypes.test(type)) return true |
| 164 | + var data = mimedb[type.split(';', 1)[0].trim().toLowerCase()] |
| 165 | + if (data === undefined) return false |
| 166 | + return data.compressible |
| 167 | +} |
| 168 | + |
| 169 | +module.exports = fp(compressPlugin, '>=0.20.0') |
0 commit comments