UNPKG

rjweb-server

Version:

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

102 lines (101 loc) 3.59 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var getCompressMethod_exports = {}; __export(getCompressMethod_exports, { default: () => getCompressMethod }); module.exports = __toCommonJS(getCompressMethod_exports); const write = (res, chunk) => { let result = false; res.cork(() => result = res.write(chunk)); return result; }; const tryEnd = (res, chunk, totalSize) => { let result = [false, false]; res.cork(() => result = res.tryEnd(chunk, totalSize)); return result[0] || result[1]; }; function getCompressMethod(doCompress, header, res, totalSize, ctg) { if (!doCompress || !ctg.options.httpCompression.enabled || ctg.options.httpCompression.maxSize < totalSize) return ["none", void 0, (chunk) => tryEnd(res, chunk, totalSize)]; 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 (ctg.options.httpCompression.disabledAlgorithms.includes(tempValue)) prio = NaN; switch (tempValue) { case "br": if (isNaN(prio)) { if (!ctg.options.httpCompression.disabledAlgorithms.includes("br")) prio = -0.1; else prio = -Infinity; break; } else break; case "gzip": if (isNaN(prio)) { if (!ctg.options.httpCompression.disabledAlgorithms.includes("gzip")) prio = -0.2; else prio = -Infinity; break; } else break; case "deflate": if (isNaN(prio)) { if (!ctg.options.httpCompression.disabledAlgorithms.includes("deflate")) prio = -0.3; 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++; } return [highestValue, highestValue === "none" ? void 0 : highestValue, highestValue !== "none" ? (chunk) => write(res, chunk) : (chunk) => tryEnd(res, chunk, totalSize)]; }