https://github.com/mishoo/UglifyJS
Raw File
Tip revision: 33ad0d258c8a78c477f0ec995a10b51a2a1ee95a authored by Alex Lam S.L on 02 July 2017, 11:04:15 UTC
harmony-v3.0.23
Tip revision: 33ad0d2
fetch.js
var fs = require("fs");
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);
        require(url.slice(0, url.indexOf(":"))).get(url, function(res) {
            if (res.statusCode !== 200) return callback(res);
            res.pipe(fs.createWriteStream(local(url)).on("close", function() {
                callback(null, read(url));
            }));
        });
    }).on("open", function() {
        callback(null, result);
    });
};
back to top