Skip to content

Instantly share code, notes, and snippets.

@fijimunkii
Last active December 7, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fijimunkii/fa5850efefb27ab68413a7e5ea726a7b to your computer and use it in GitHub Desktop.
Save fijimunkii/fa5850efefb27ab68413a7e5ea726a7b to your computer and use it in GitHub Desktop.
node native https gz promise with gzbomb protection
const https = require('https');
const zlib = require('zlib');
return new Promise((resolve, reject) => {
const options = {
hostname: 'fullstory.com',
port: 443,
path: '/s/fs.js',
method: 'GET'
};
const req = https.request(options, async (res) => {
const maxbuffer = 41943040; //5mb
let totalLength = 0;
let buffer = [];
const unzip = zlib.createGunzip();
res.pipe(unzip);
try {
for await (const chunk of unzip) {
totalLength += chunk.length;
if (totalLength > maxbuffer)
throw 'max buffer size reached';
buffer.push(chunk);
}
} catch(e) { reject(e); }
resolve(buffer);
});
req.on('error', (e) => {
console.error(e);
reject(e);
});
req.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment