UNPKG

testingbot-tunnel-launcher

Version:
33 lines (29 loc) 1.03 kB
'use strict'; var https = require('https'); var fs = require('fs'); /** * Downloads a file from the given URL to the specified destination. * @param {string} url - The URL to download the file from. * @param {Object} options - The options object containing the destination. * @param {Function} cb - The callback function to handle success or error. */ exports.get = function (url, options, cb) { var dest = options.destination; https.get(url, function (response) { if (response.statusCode >= 400) { return cb(new Error('Could not download ' + url + ', statusCode: ' + response.statusCode.toString()), null); } var file = fs.createWriteStream(dest); response.pipe(file); file.on('finish', function () { file.close(); cb(null, dest); }); file.on('error', function (err) { fs.unlink(dest, function () {}); cb(err, null); }); }).on('error', function (err) { cb(err, null); }); };