Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions lib/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ var zlib = require("zlib");
var contentTypes = require("./content-types.js");
var debug = require("debug")("unblocker:decompress");

const SUPPORTED_ENCODINGS = [
'deflate',
'gzip',
'br' // brotli
];

const REQUESTED_ENCODINGS = [
// deflate is tricky, so we don't request it
'gzip',
'br'
]

module.exports = function (config) {
function acceptableCompression(data) {
// deflate is tricky so we're only going to ask for gzip if the client allows it
if (
data.headers["accept-encoding"] &&
data.headers["accept-encoding"].includes("gzip")
) {
data.headers["accept-encoding"] = "gzip";
var encodings = (data.headers["accept-encoding"] || '')
.split(',')
.map(s => s.trim())
.filter(s => REQUESTED_ENCODINGS.includes(s));
if (encodings.length) {
data.headers["accept-encoding"] = encodings.join(', ');
} else {
delete data.headers["accept-encoding"];
}
Expand All @@ -31,11 +43,8 @@ module.exports = function (config) {
return false;
}

// decompress if it's gzipped or deflate'd
return (
headers["content-encoding"] == "gzip" ||
headers["content-encoding"] == "deflate"
);
// decompress if it's in a supported encoding
return SUPPORTED_ENCODINGS.includes(headers["content-encoding"]);
}

function decompressResponse(data) {
Expand All @@ -54,6 +63,11 @@ module.exports = function (config) {
var placeHolder = new PassThrough();
data.stream = placeHolder;

const encoding = data.headers["content-encoding"];

// we're decoding it here, so this won't by the time it gets to the client
delete data.headers["content-encoding"];

var handleData = function handleData() {
var firstChunk = sourceStream.read();

Expand All @@ -65,11 +79,13 @@ module.exports = function (config) {
}

var decompressStream;
if (data.headers["content-encoding"] == "deflate") {
if (encoding == "deflate") {
// https://github.com/nfriedly/node-unblocker/issues/12
// inflateRaw seems to work here wheras inflate and unzip do not.
// todo: validate this against other sites - if some require raw and others require non-raw, then maybe just rewrite the accept-encoding header to gzip only
decompressStream = zlib.createInflateRaw();
} else if (encoding == 'br') {
decompressStream = zlib.createBrotliDecompress();
} else {
decompressStream = zlib.createUnzip();
}
Expand All @@ -83,7 +99,6 @@ module.exports = function (config) {
// if we do get data, create a decompression stream and pipe through it
sourceStream.once("readable", handleData);

delete data.headers["content-encoding"];
}
}

Expand Down