Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/wincloud_gateway/node_modules/koa-compress/lib/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 'use strict'
/**
* Module dependencies.
*/
const compressible = require('compressible')
const isJSON = require('koa-is-json')
const status = require('statuses')
const Stream = require('stream')
const bytes = require('bytes')
const Encodings = require('./encodings')
/**
* Regex to match no-transform directive in a cache-control header
*/
const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
/**
* Compress middleware.
*
* @param {Object} [options]
* @return {Function}
* @api public
*/
module.exports = (options = {}) => {
let { filter = compressible, threshold = 1024, defaultEncoding = 'identity' } = options
if (typeof threshold === 'string') threshold = bytes(threshold)
// `options.br = false` would remove it as a preferred encoding
const preferredEncodings = Encodings.preferredEncodings.filter((encoding) => options[encoding] !== false && options[encoding] !== null)
const encodingOptions = {}
preferredEncodings.forEach((encoding) => {
encodingOptions[encoding] = {
...Encodings.encodingMethodDefaultOptions[encoding],
...(options[encoding] || {})
}
})
Object.assign(compressMiddleware, {
preferredEncodings,
encodingOptions
})
return compressMiddleware
async function compressMiddleware (ctx, next) {
ctx.vary('Accept-Encoding')
await next()
// early exit if there's no content body or the body is already encoded
let { body } = ctx
if (!body) return
if (ctx.res.headersSent || !ctx.writable) return
if (ctx.compress === false) return
if (ctx.request.method === 'HEAD') return
if (status.empty[ctx.response.status]) return
if (ctx.response.get('Content-Encoding')) return
// forced compression or implied
if (!(ctx.compress === true || filter(ctx.response.type))) return
// don't compress for Cache-Control: no-transform
// https://tools.ietf.org/html/rfc7234#section-5.2.1.6
const cacheControl = ctx.response.get('Cache-Control')
if (cacheControl && NO_TRANSFORM_REGEX.test(cacheControl)) return
// don't compress if the current response is below the threshold
if (threshold && ctx.response.length < threshold) return
// get the preferred content encoding
const encodings = new Encodings({
preferredEncodings
})
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || defaultEncoding)
const encoding = encodings.getPreferredContentEncoding()
// identity === no compression
if (encoding === 'identity') return
/** begin compression logic **/
// json
if (isJSON(body)) body = ctx.body = JSON.stringify(body)
ctx.set('Content-Encoding', encoding)
ctx.res.removeHeader('Content-Length')
const compress = Encodings.encodingMethods[encoding]
const stream = ctx.body = compress(encodingOptions[encoding])
if (body instanceof Stream) {
body.pipe(stream)
} else {
stream.end(body)
}
}
}
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0886 ]-- |