Skip to content

Commit

Permalink
WIP Brotli support
Browse files Browse the repository at this point in the history
may be related to #243
  • Loading branch information
nfriedly committed Dec 20, 2023
1 parent 2f76f10 commit 6d4ede8
Showing 1 changed file with 28 additions and 13 deletions.
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',

Check failure on line 9 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `'deflate'` with `"deflate"`

Check failure on line 9 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `'deflate'` with `"deflate"`
'gzip',

Check failure on line 10 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `'gzip'` with `"gzip"`

Check failure on line 10 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `'gzip'` with `"gzip"`
'br' // brotli

Check failure on line 11 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `'br'` with `"br",`

Check failure on line 11 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `'br'` with `"br",`
];

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

Check failure on line 16 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `'gzip'` with `"gzip"`

Check failure on line 16 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `'gzip'` with `"gzip"`
'br'

Check failure on line 17 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `'br'` with `"br",`

Check failure on line 17 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `'br'` with `"br",`
]

Check failure on line 18 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Insert `;`

Check failure on line 18 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Insert `;`

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"] || '')

Check failure on line 22 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `''` with `""`

Check failure on line 22 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `''` with `""`
.split(',')

Check failure on line 23 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `','` with `","`

Check failure on line 23 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `','` with `","`
.map(s => s.trim())

Check failure on line 24 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `s` with `(s)`

Check failure on line 24 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `s` with `(s)`
.filter(s => REQUESTED_ENCODINGS.includes(s));

Check failure on line 25 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

Replace `s` with `(s)`

Check failure on line 25 in lib/decompress.js

View workflow job for this annotation

GitHub Actions / build (latest)

Replace `s` with `(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

0 comments on commit 6d4ede8

Please sign in to comment.