Skip to content

Commit

Permalink
middleware update
Browse files Browse the repository at this point in the history
  • Loading branch information
Niputi committed Jan 29, 2022
1 parent b986fa4 commit 30cb9d6
Showing 1 changed file with 76 additions and 84 deletions.
160 changes: 76 additions & 84 deletions packages/vite/src/node/server/middlewares/compression.ts
Expand Up @@ -26,101 +26,93 @@ export default function compression() {
if (!zlib.createBrotliCompress) brotli = false

return (req, res, next = noop) => {
const brotliOpts = (typeof brotli === 'object' && brotli) || {}
const gzipOpts = (typeof gzip === 'object' && gzip) || {}

// disable Brotli on Node<12.7 where it is unsupported:
if (!zlib.createBrotliCompress) brotli = false

return (req, res, next = noop) => {
const accept = req.headers['accept-encoding'] + ''
const encoding = ((brotli && accept.match(/\bbr\b/)) ||
(gzip && accept.match(/\bgzip\b/)) ||
[])[0]

// skip if no response body or no supported encoding:
if (req.method === 'HEAD' || !encoding) return next()

/** @type {zlib.Gzip | zlib.BrotliCompress} */
let compress
let pendingStatus
/** @type {[string, function][]?} */
let pendingListeners = []
let started = false
let size = 0

function start() {
started = true
// @ts-ignore
size = res.getHeader('Content-Length') | 0 || size
const compressible = mimes.test(
String(res.getHeader('Content-Type') || 'text/plain')
)
const cleartext = !res.getHeader('Content-Encoding')
const listeners = pendingListeners || []
if (compressible && cleartext && size >= threshold) {
res.setHeader('Content-Encoding', encoding)
res.removeHeader('Content-Length')
if (encoding === 'br') {
const params = {
[zlib.constants.BROTLI_PARAM_QUALITY]: level,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: size
}
compress = zlib.createBrotliCompress({
params: Object.assign(params, brotliOpts)
})
} else {
compress = zlib.createGzip(Object.assign({ level }, gzipOpts))
const accept = req.headers['accept-encoding'] + ''
const encoding = ((brotli && accept.match(/\bbr\b/)) ||
(gzip && accept.match(/\bgzip\b/)) ||
[])[0]

// skip if no response body or no supported encoding:
if (req.method === 'HEAD' || !encoding) return next()

/** @type {zlib.Gzip | zlib.BrotliCompress} */
let compress
let pendingStatus
/** @type {[string, function][]?} */
let pendingListeners = []
let started = false
let size = 0

function start() {
started = true
// @ts-ignore
size = res.getHeader('Content-Length') | 0 || size
const compressible = mimes.test(
String(res.getHeader('Content-Type') || 'text/plain')
)
const cleartext = !res.getHeader('Content-Encoding')
const listeners = pendingListeners || []
if (compressible && cleartext && size >= threshold) {
res.setHeader('Content-Encoding', encoding)
res.removeHeader('Content-Length')
if (encoding === 'br') {
const params = {
[zlib.constants.BROTLI_PARAM_QUALITY]: level,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: size
}
// backpressure
compress.on(
'data',
(chunk) => write.call(res, chunk) === false && compress.pause()
)
on.call(res, 'drain', () => compress.resume())
compress.on('end', () => end.call(res))
listeners.forEach((p) => compress.on.apply(compress, p))
compress = zlib.createBrotliCompress({
params: Object.assign(params, brotliOpts)
})
} else {
pendingListeners = null
listeners.forEach((p) => on.apply(res, p))
compress = zlib.createGzip(Object.assign({ level }, gzipOpts))
}

writeHead.call(res, pendingStatus || res.statusCode)
// backpressure
compress.on(
'data',
(chunk) => write.call(res, chunk) === false && compress.pause()
)
on.call(res, 'drain', () => compress.resume())
compress.on('end', () => end.call(res))
listeners.forEach((p) => compress.on.apply(compress, p))
} else {
pendingListeners = null
listeners.forEach((p) => on.apply(res, p))
}

const { end, write, on, writeHead } = res
writeHead.call(res, pendingStatus || res.statusCode)
}

res.writeHead = function (status, reason, headers) {
if (typeof reason !== 'string') [headers, reason] = [reason, headers]
if (headers) for (let i in headers) res.setHeader(i, headers[i])
pendingStatus = status
return this
}
const { end, write, on, writeHead } = res

res.write = function (chunk, enc, cb) {
size += getChunkSize(chunk, enc)
if (!started) start()
if (!compress) return write.apply(this, arguments)
return compress.write.apply(compress, arguments)
}
res.writeHead = function (status, reason, headers) {
if (typeof reason !== 'string') [headers, reason] = [reason, headers]
if (headers) for (let i in headers) res.setHeader(i, headers[i])
pendingStatus = status
return this
}

res.end = function (chunk, enc, cb) {
if (arguments.length > 0 && typeof chunk !== 'function') {
size += getChunkSize(chunk, enc)
}
if (!started) start()
if (!compress) return end.apply(this, arguments)
return compress.end.apply(compress, arguments)
}
res.write = function (chunk, enc, cb) {
size += getChunkSize(chunk, enc)
if (!started) start()
if (!compress) return write.apply(this, arguments)
return compress.write.apply(compress, arguments)
}

res.on = function (type, listener) {
if (!pendingListeners || type !== 'drain') on.call(this, type, listener)
else if (compress) compress.on(type, listener)
else pendingListeners.push([type, listener])
return this
res.end = function (chunk, enc, cb) {
if (arguments.length > 0 && typeof chunk !== 'function') {
size += getChunkSize(chunk, enc)
}
if (!started) start()
if (!compress) return end.apply(this, arguments)
return compress.end.apply(compress, arguments)
}

next()
res.on = function (type, listener) {
if (!pendingListeners || type !== 'drain') on.call(this, type, listener)
else if (compress) compress.on(type, listener)
else pendingListeners.push([type, listener])
return this
}

next()
}
}

0 comments on commit 30cb9d6

Please sign in to comment.