https://github.com/mishoo/UglifyJS
Raw File
Tip revision: 567cb0e4e33f6c967aa66add9dcaf79a34d55fb6 authored by Alex Lam S.L on 19 November 2017, 07:53:22 UTC
harmony-v3.1.10
Tip revision: 567cb0e
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