https://github.com/mishoo/UglifyJS
Raw File
Tip revision: d535daa2c711a6a02f838e1ac390e8e73020abb2 authored by Alex Lam S.L on 29 October 2017, 10:58:58 UTC
harmony-v3.1.6
Tip revision: d535daa
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