/
index.js
529 lines (442 loc) · 17.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
'use strict'
const fp = require('fastify-plugin')
const zlib = require('zlib')
const pump = require('pump')
const mimedb = require('mime-db')
const isStream = require('is-stream')
const intoStream = require('into-stream')
const peek = require('peek-stream')
const Minipass = require('minipass')
const pumpify = require('pumpify')
const isGzip = require('is-gzip')
const isZip = require('is-zip')
const unZipper = require('unzipper')
const isDeflate = require('is-deflate')
const encodingNegotiator = require('encoding-negotiator')
const { inherits, format } = require('util')
const InvalidRequestEncodingError = createError('FST_CP_ERR_INVALID_CONTENT_ENCODING', 'Unsupported Content-Encoding: %s', 415)
const InvalidRequestCompressedPayloadError = createError('FST_CP_ERR_INVALID_CONTENT', 'Could not decompress the request payload using the provided encoding', 400)
function compressPlugin (fastify, opts, next) {
const globalCompressParams = processCompressParams(opts)
const globalDecompressParams = processDecompressParams(opts)
if (opts.encodings && opts.encodings.length < 1) {
next(new Error('The `encodings` option array must have at least 1 item.'))
return
}
if (opts.requestEncodings && opts.requestEncodings.length < 1) {
next(new Error('The `requestEncodings` option array must have at least 1 item.'))
return
}
if (globalCompressParams.encodings.length < 1) {
next(new Error('None of the passed `encodings` were supported — compression not possible.'))
return
}
if (globalDecompressParams.encodings.length < 1) {
next(new Error('None of the passed `requestEncodings` were supported — request decompression not possible.'))
return
}
if (globalDecompressParams.forceEncoding && !globalDecompressParams.encodings.includes(globalDecompressParams.forceEncoding)) {
next(new Error(`Unsupported decompression encoding ${opts.forceRequestEncoding}.`))
return
}
fastify.decorateReply('compress', null)
// add onSend hook onto each route as needed
fastify.addHook('onRoute', (routeOptions) => {
// Manage compression options
if (routeOptions.config && typeof routeOptions.config.compress !== 'undefined') {
if (typeof routeOptions.config.compress === 'object') {
const mergedCompressParams = Object.assign(
{}, globalCompressParams, processCompressParams(routeOptions.config.compress)
)
// if the current endpoint has a custom compress configuration ...
buildRouteCompress(fastify, mergedCompressParams, routeOptions)
} else if (routeOptions.config.compress === false) {
// don't apply any compress settings
} else {
throw new Error('Unknown value for route compress configuration')
}
} else if (globalCompressParams.global) {
// if the plugin is set globally ( meaning that all the routes will be compressed )
// As the endpoint, does not have a custom rateLimit configuration, use the global one.
buildRouteCompress(fastify, globalCompressParams, routeOptions)
} else {
// if no options are specified and the plugin is not global, then we still want to decorate
// the reply in this case
buildRouteCompress(fastify, globalCompressParams, routeOptions, true)
}
// Manage decompression options
if (routeOptions.config && typeof routeOptions.config.decompress !== 'undefined') {
if (typeof routeOptions.config.decompress === 'object') {
// if the current endpoint has a custom compress configuration ...
const mergedDecompressParams = Object.assign(
{}, globalDecompressParams, processDecompressParams(routeOptions.config.decompress)
)
buildRouteDecompress(fastify, mergedDecompressParams, routeOptions)
} else if (routeOptions.config.decompress === false) {
// don't apply any decompress settings
} else {
throw new Error('Unknown value for route decompress configuration')
}
} else if (globalDecompressParams.global) {
// if the plugin is set globally ( meaning that all the routes will be decompressed )
// As the endpoint, does not have a custom rateLimit configuration, use the global one.
buildRouteDecompress(fastify, globalDecompressParams, routeOptions)
}
})
next()
}
function processCompressParams (opts) {
/* istanbul ignore next */
if (!opts) {
return
}
const params = {
global: (typeof opts.global === 'boolean') ? opts.global : true
}
params.brotliOptions = opts.brotliOptions
params.zlibOptions = opts.zlibOptions
params.onUnsupportedEncoding = opts.onUnsupportedEncoding
params.inflateIfDeflated = opts.inflateIfDeflated === true
params.threshold = typeof opts.threshold === 'number' ? opts.threshold : 1024
params.compressibleTypes = opts.customTypes instanceof RegExp ? opts.customTypes : /^text\/|\+json$|\+text$|\+xml$|octet-stream$/
params.compressStream = {
br: () => ((opts.zlib || zlib).createBrotliCompress || zlib.createBrotliCompress)(params.brotliOptions),
gzip: () => ((opts.zlib || zlib).createGzip || zlib.createGzip)(params.zlibOptions),
deflate: () => ((opts.zlib || zlib).createDeflate || zlib.createDeflate)(params.zlibOptions)
}
params.uncompressStream = {
br: () => ((opts.zlib || zlib).createBrotliDecompress || zlib.createBrotliDecompress)(params.brotliOptions),
gzip: () => ((opts.zlib || zlib).createGunzip || zlib.createGunzip)(params.zlibOptions),
deflate: () => ((opts.zlib || zlib).createInflate || zlib.createInflate)(params.zlibOptions)
}
const supportedEncodings = ['br', 'gzip', 'deflate', 'identity']
params.encodings = Array.isArray(opts.encodings)
? supportedEncodings
.filter(encoding => opts.encodings.includes(encoding))
.sort((a, b) => opts.encodings.indexOf(a) - supportedEncodings.indexOf(b))
: supportedEncodings
return params
}
function processDecompressParams (opts) {
/* istanbul ignore next */
if (!opts) {
return
}
const customZlib = opts.zlib || zlib
const params = {
global: (typeof opts.global === 'boolean') ? opts.global : true,
onUnsupportedRequestEncoding: opts.onUnsupportedRequestEncoding,
onInvalidRequestPayload: opts.onInvalidRequestPayload,
decompressStream: {
br: customZlib.createBrotliDecompress || zlib.createBrotliDecompress,
gzip: customZlib.createGunzip || zlib.createGunzip,
deflate: customZlib.createInflate || zlib.createInflate
},
encodings: [],
forceEncoding: null
}
const supportedEncodings = ['br', 'gzip', 'deflate', 'identity']
params.encodings = Array.isArray(opts.requestEncodings)
? supportedEncodings
.filter(encoding => opts.requestEncodings.includes(encoding))
.sort((a, b) => opts.requestEncodings.indexOf(a) - supportedEncodings.indexOf(b))
: supportedEncodings
if (opts.forceRequestEncoding) {
params.forceEncoding = opts.forceRequestEncoding
if (params.encodings.includes(opts.forceRequestEncoding)) {
params.encodings = [opts.forceRequestEncoding]
}
}
return params
}
function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
// In order to provide a compress method with the same parameter set as the route itself has
// we do the decorate the reply at the start of the request
if (Array.isArray(routeOptions.onRequest)) {
routeOptions.onRequest.push(onRequest)
} else if (typeof routeOptions.onRequest === 'function') {
routeOptions.onRequest = [routeOptions.onRequest, onRequest]
} else {
routeOptions.onRequest = [onRequest]
}
const compressFn = compress(params)
function onRequest (req, reply, next) {
reply.compress = compressFn
next()
}
if (decorateOnly) {
return
}
if (Array.isArray(routeOptions.onSend)) {
routeOptions.onSend.push(onSend)
} else if (typeof routeOptions.onSend === 'function') {
routeOptions.onSend = [routeOptions.onSend, onSend]
} else {
routeOptions.onSend = [onSend]
}
function onSend (req, reply, payload, next) {
if (payload == null) {
reply.log.debug('compress: missing payload')
return next()
}
var stream, encoding
var noCompress =
// don't compress on x-no-compression header
(req.headers['x-no-compression'] !== undefined) ||
// don't compress if not one of the indicated compressible types
(shouldCompress(reply.getHeader('Content-Type') || 'application/json', params.compressibleTypes) === false) ||
// don't compress on missing or identity `accept-encoding` header
((encoding = getEncodingHeader(params.encodings, req)) == null || encoding === 'identity')
if (encoding == null && params.onUnsupportedEncoding != null) {
var encodingHeader = req.headers['accept-encoding']
try {
var errorPayload = params.onUnsupportedEncoding(encodingHeader, reply.request, reply)
return next(null, errorPayload)
} catch (err) {
return next(err)
}
}
if (noCompress) {
if (params.inflateIfDeflated && isStream(stream = maybeUnzip(payload))) {
encoding === undefined
? reply.removeHeader('Content-Encoding')
: reply.header('Content-Encoding', 'identity')
pump(stream, payload = unzipStream(params.uncompressStream), onEnd.bind(reply))
}
return next(null, payload)
}
if (typeof payload.pipe !== 'function') {
if (Buffer.byteLength(payload) < params.threshold) {
return next()
}
payload = intoStream(payload)
}
reply
.header('Content-Encoding', encoding)
.removeHeader('content-length')
stream = zipStream(params.compressStream, encoding)
pump(payload, stream, onEnd.bind(reply))
next(null, stream)
}
}
function buildRouteDecompress (fastify, params, routeOptions) {
// Add our decompress handler in the preParsing hook
if (Array.isArray(routeOptions.preParsing)) {
routeOptions.preParsing.unshift(preParsing)
} else if (typeof routeOptions.preParsing === 'function') {
routeOptions.preParsing = [preParsing, routeOptions.preParsing]
} else {
routeOptions.preParsing = [preParsing]
}
function preParsing (request, reply, raw, next) {
// Get the encoding from the options or from the headers
let encoding = params.forceEncoding
if (!encoding) {
encoding = request.headers['content-encoding']
}
// The request is not compressed, nothing to do here
if (!encoding) {
return next(null, raw)
}
// Check that encoding is supported
if (!params.encodings.includes(encoding)) {
let errorPayload
if (params.onUnsupportedRequestEncoding) {
try {
errorPayload = params.onUnsupportedRequestEncoding(encoding, request)
} catch (ex) {
errorPayload = undefined
}
}
if (!errorPayload) {
errorPayload = new InvalidRequestEncodingError(encoding)
}
return next(errorPayload)
}
// No action on identity
if (encoding === 'identity') {
return next(null, raw)
}
// Prepare decompression - If there is an decompress error, prepare the error for fastify handing
const decompresser = params.decompressStream[encoding]()
decompresser.receivedEncodedLength = 0
decompresser.on('error', onDecompressError.bind(this, request, params, encoding))
decompresser.pause()
// Track length of encoded length to handle receivedEncodedLength
raw.on('data', trackEncodedLength.bind(decompresser))
raw.on('end', removeEncodedLengthTracking)
next(null, pump(raw, decompresser))
}
}
function compress (params) {
return function (payload) {
if (payload == null) {
this.send(new Error('Internal server error'))
return
}
var stream, encoding
var noCompress =
// don't compress on x-no-compression header
(this.request.headers['x-no-compression'] !== undefined) ||
// don't compress if not one of the indicated compressible types
(shouldCompress(this.getHeader('Content-Type') || 'application/json', params.compressibleTypes) === false) ||
// don't compress on missing or identity `accept-encoding` header
((encoding = getEncodingHeader(params.encodings, this.request)) == null || encoding === 'identity')
if (encoding == null && params.onUnsupportedEncoding != null) {
var encodingHeader = this.request.headers['accept-encoding']
var errorPayload
try {
errorPayload = params.onUnsupportedEncoding(encodingHeader, this.request, this)
} catch (ex) {
errorPayload = ex
}
return this.send(errorPayload)
}
if (noCompress) {
if (params.inflateIfDeflated && isStream(stream = maybeUnzip(payload, this.serialize.bind(this)))) {
encoding === undefined
? this.removeHeader('Content-Encoding')
: this.header('Content-Encoding', 'identity')
pump(stream, payload = unzipStream(params.uncompressStream), onEnd.bind(this))
}
return this.send(payload)
}
if (typeof payload.pipe !== 'function') {
if (!Buffer.isBuffer(payload) && typeof payload !== 'string') {
payload = this.serialize(payload)
}
}
if (typeof payload.pipe !== 'function') {
if (Buffer.byteLength(payload) < params.threshold) {
return this.send(payload)
}
payload = intoStream(payload)
}
if (this.hasHeader('Vary')) {
const varyHeader = Array.isArray(this.getHeader('Vary')) ? this.getHeader('Vary') : [this.getHeader('Vary')]
if (!varyHeader.includes('accept-encoding')) {
this.header('Vary', [...varyHeader, 'accept-encoding'])
}
} else {
this.header('Vary', 'accept-encoding')
}
this
.header('Content-Encoding', encoding)
.removeHeader('content-length')
stream = zipStream(params.compressStream, encoding)
pump(payload, stream, onEnd.bind(this))
this.send(stream)
}
}
function onEnd (err) {
if (err) this.log.error(err)
}
function trackEncodedLength (chunk) {
this.receivedEncodedLength += chunk.length
}
function removeEncodedLengthTracking () {
this.removeListener('data', trackEncodedLength)
this.removeListener('end', removeEncodedLengthTracking)
}
function onDecompressError (request, params, encoding, error) {
this.log.debug(`compress: invalid request payload - ${error}`)
let errorPayload
if (params.onInvalidRequestPayload) {
try {
errorPayload = params.onInvalidRequestPayload(encoding, request, error)
} catch (ex) {
errorPayload = undefined
}
}
if (!errorPayload) {
errorPayload = new InvalidRequestCompressedPayloadError()
}
error.decompressError = error
Object.assign(error, errorPayload)
}
function getEncodingHeader (encodings, request) {
let header = request.headers['accept-encoding']
if (header != null) {
header = header.toLowerCase()
.replace('*', 'gzip') // consider the no-preference token as gzip for downstream compat
return encodingNegotiator.negotiate(header, encodings)
} else {
return undefined
}
}
function shouldCompress (type, compressibleTypes) {
if (compressibleTypes.test(type)) return true
var data = mimedb[type.split(';', 1)[0].trim().toLowerCase()]
if (data === undefined) return false
return data.compressible === true
}
function isCompressed (data) {
if (isGzip(data)) return 1
if (isDeflate(data)) return 2
if (isZip(data)) return 3
return 0
}
function maybeUnzip (payload, serialize) {
if (isStream(payload)) return payload
var buf = payload; var result = payload
if (ArrayBuffer.isView(payload)) {
// Cast non-Buffer DataViews into a Buffer
buf = result = Buffer.from(
payload.buffer,
payload.byteOffset,
payload.byteLength
)
} else if (serialize && typeof payload !== 'string') {
buf = result = serialize(payload)
}
// handle case where serialize doesn't return a string or Buffer
if (!Buffer.isBuffer(buf)) return result
if (isCompressed(buf) === 0) return result
return intoStream(result)
}
function zipStream (deflate, encoding) {
return peek({ newline: false, maxBuffer: 10 }, function (data, swap) {
switch (isCompressed(data)) {
case 1: return swap(null, new Minipass())
case 2: return swap(null, new Minipass())
}
return swap(null, deflate[encoding]())
})
}
function unzipStream (inflate, maxRecursion) {
if (!(maxRecursion >= 0)) maxRecursion = 3
return peek({ newline: false, maxBuffer: 10 }, function (data, swap) {
if (maxRecursion < 0) return swap(new Error('Maximum recursion reached'))
switch (isCompressed(data)) {
case 1: return swap(null, pumpify(inflate.gzip(), unzipStream(inflate, maxRecursion - 1)))
case 2: return swap(null, pumpify(inflate.deflate(), unzipStream(inflate, maxRecursion - 1)))
case 3: return swap(null, pumpify(unZipper.ParseOne(), unzipStream(inflate, maxRecursion - 1)))
}
return swap(null, new Minipass())
})
}
function createError (code, message, statusCode) {
code = code.toUpperCase()
function FastifyCompressError (a) {
Error.captureStackTrace(this, FastifyCompressError)
this.name = 'FastifyCompressError'
this.code = code
if (a) {
this.message = format(message, a)
} else {
this.message = message
}
this.statusCode = statusCode
}
FastifyCompressError.prototype[Symbol.toStringTag] = 'Error'
/* istanbul ignore next */
FastifyCompressError.prototype.toString = function () {
return `${this.name} [${this.code}]: ${this.message}`
}
inherits(FastifyCompressError, Error)
return FastifyCompressError
}
module.exports = fp(compressPlugin, {
fastify: '3.x',
name: 'fastify-compress'
})