Last active
December 7, 2018 15:34
-
-
Save fijimunkii/fa5850efefb27ab68413a7e5ea726a7b to your computer and use it in GitHub Desktop.
node native https gz promise with gzbomb protection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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