UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

91 lines (90 loc) 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@rjweb/utils"); /** * Get the best compression using a header * @since 8.0.0 */ function getCompressMethod(doCompress, header, size, proxied, global) { if (!doCompress || !global.options.compression.http.enabled || size > global.options.compression.http.maxSize || size < global.options.compression.http.minSize || (proxied && !global.options.proxy.compress)) return null; if (global.cache.compressionMethods.has(header)) return global.cache.compressionMethods.get(header); let highestValue = 'none', highestPriority = -Infinity, tempValue = '', tempPriority = ''; header += ','; let progress = 0, mode = 'value'; while (progress < header.length) { if (!header[progress] || header[progress] === ' ') { progress++; continue; } if (header[progress] === ',') { let prio = parseInt(tempPriority); if (!global.options.compression.http.preferOrder.includes(tempValue === 'br' ? 'brotli' : (0, utils_1.as)(tempValue))) prio = NaN; switch ((0, utils_1.as)(tempValue)) { case "br": if (isNaN(prio)) { if (global.options.compression.http.preferOrder.includes('brotli')) prio = -(global.options.compression.http.preferOrder.indexOf('brotli') / 10); else prio = -Infinity; break; } else break; case "gzip": if (isNaN(prio)) { if (global.options.compression.http.preferOrder.includes('gzip')) prio = -(global.options.compression.http.preferOrder.indexOf('gzip') / 10); else prio = -Infinity; break; } else break; case "deflate": if (isNaN(prio)) { if (global.options.compression.http.preferOrder.includes('deflate')) prio = -(global.options.compression.http.preferOrder.indexOf('deflate') / 10); else prio = -Infinity; break; } else break; default: prio = -Infinity; break; } mode = 'value'; tempValue = tempValue; highestValue = prio > highestPriority ? tempValue : highestValue; highestPriority = prio > highestPriority ? prio : highestPriority; tempPriority = ''; tempValue = ''; } else if (header[progress] === ';') { mode = 'prio'; } else { if (mode === 'value') { tempValue += header[progress]; } else { if (header[progress] !== 'q' && header[progress] !== '=') { tempPriority += header[progress]; } } } progress++; } const result = highestValue === 'none' ? null : (0, utils_1.as)(highestValue === 'br' ? 'brotli' : highestValue); if (result) global.cache.compressionMethods.set(header, result); return result; } exports.default = getCompressMethod;