Created
March 29, 2018 15:40
-
-
Save furkan3ayraktar/d0c2d0d39d0f62e17811b1e881c0e65e to your computer and use it in GitHub Desktop.
Content downloader used in Lambda@Edge function.
This file contains hidden or 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'); | |
const downloadContent = (url, callback) => { | |
https.get(url, (res) => { | |
let response; | |
let body = ''; | |
if (res.headers['content-encoding'] === 'gzip') { | |
response = res.pipe(zlib.createGunzip()); | |
} else { | |
response = res; | |
} | |
response.on('data', (chunk) => { | |
body += chunk; | |
}); | |
response.on('end', () => { | |
callback(true, body, res.headers); | |
}); | |
}).on('error', (e) => callback(false, e)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment