UglifyJS/test/fetch.js

34 lines
885 B
JavaScript
Raw Permalink Normal View History

var fs = require("fs");
2018-05-31 08:23:49 +00:00
var parse = require("url").parse;
var path = require("path");
try {
fs.mkdirSync("./tmp");
} catch (e) {
if (e.code != "EEXIST") throw e;
}
function local(url) {
return path.join("./tmp", encodeURIComponent(url));
}
function read(url) {
return fs.createReadStream(local(url));
}
module.exports = function(url, callback) {
var result = read(url);
result.on("error", function(e) {
if (e.code != "ENOENT") return callback(e);
2018-05-31 08:23:49 +00:00
var options = parse(url);
options.rejectUnauthorized = false;
require(options.protocol.slice(0, -1)).get(options, function(res) {
2018-08-08 08:15:45 +00:00
if (res.statusCode !== 200) return callback(res.statusCode);
res.pipe(fs.createWriteStream(local(url)));
callback(null, res);
});
}).on("open", function() {
callback(null, result);
});
};