Template:MDN:Common

  • Revision slug: Template:mdn:common
  • Revision title: Template:MDN:Common
  • Revision id: 297228
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment

Revision Content

<% module.exports = { /** * Given a set of strings like this: * { "en-US": "Foo", "de": "Bar", "es": "Baz" } * Return the one which matches the current locale. */ localString: function (strings) { var lang = env.locale; if (!(lang in strings)) lang = 'en-US'; return strings[lang]; }, /** * Accepts a relative URL or an attachment object * Returns the content of a given file. */ getFileContent: function(fileObjOrUrl) { var url = fileObjOrUrl.url || fileObjOrUrl; if(!url) return ''; var result = '', p = kuma.url.parse(env.url, true), base_url = p.protocol + '//' + p.host; url = base_url + url; return cacheFn('kuma:get_attachment_content:' + md5(url.toLowerCase()), 3600, function(next) { try { request({ method: 'GET', headers: { 'Cache-Control': env.cache_control }, url: url }, function(err, resp, body) { if(resp && 200 == resp.statusCode) { next(body); } }); } catch(e) { next("error: " + e); } }); }, /** * #### cacheFnIgnoreCacheControl * Cache a function, and use cached results no matter what * Cache-Control we get from a shift-refresh. */ cacheFnIgnoreCacheControl: function (key, tm_out, to_cache) { var result = null, err_result = null, f = new Future(), mc = memcached; mc.get(key, function (err, c_result) { if (c_result) { result = c_result; f['return'](); } else { try { to_cache(function (val) { mc.set(key, val, tm_out, function (err, c_result) { result = val; f['return'](); }); }); } catch (e) { err_result = e; result = ''; f['return'](); } } }); f.wait(); if (err_result) { throw err_result; } return result; }, // #### fetchJSONResource // Fetch an HTTP resource with JSON representation, parse the JSON and // return a JS object. fetchJSONResource: function (url, cache_tmout) { if (!cache_tmout) cache_tmout = 3600; var key = 'kuma:json_resource:' + md5(url.toLowerCase()); return cacheFn(key, 600, function (next) { var opts = { method: 'GET', headers: { 'Cache-Control': env.cache_control }, url: url }; try { request(opts, function (err, resp, body) { var result = null; if (resp && 200 == resp.statusCode) { result = JSON.parse(body); } next(result); }); } catch (e) { next(null); } }); } } %>

Revision Source

<% module.exports = {
    
    /**
     * Given a set of strings like this:
     *     { "en-US": "Foo", "de": "Bar", "es": "Baz" }
     * Return the one which matches the current locale.
     */
    localString: function (strings) {
        var lang = env.locale;
        if (!(lang in strings)) lang = 'en-US';
        return strings[lang];
    },

    /**
     * Accepts a relative URL or an attachment object
     * Returns the content of a given file.
     */
    getFileContent: function(fileObjOrUrl) {
        var url = fileObjOrUrl.url || fileObjOrUrl;
        if(!url) return '';
        
        var result = '',
            p = kuma.url.parse(env.url, true),
            base_url = p.protocol + '//' + p.host;
            
        url = base_url + url;
        return cacheFn('kuma:get_attachment_content:' + md5(url.toLowerCase()), 3600, function(next) {
            try {
                request({
                    method: 'GET',
                    headers: { 'Cache-Control': env.cache_control },
                    url: url
                }, function(err, resp, body) {
                    if(resp && 200 == resp.statusCode) {
                        next(body);
                    }
                });
            } catch(e) {
                next("error: " + e);
            }
        });
    },

    /**
     * #### cacheFnIgnoreCacheControl
     * Cache a function, and use cached results no matter what 
     * Cache-Control we get from a shift-refresh.
     */
    cacheFnIgnoreCacheControl: function (key, tm_out, to_cache) {
        var result = null,
            err_result = null,
            f = new Future(),
            mc = memcached;
        mc.get(key, function (err, c_result) {
            if (c_result) {
                result = c_result;
                f['return']();
            } else {
                try {
                    to_cache(function (val) {
                        mc.set(key, val, tm_out, function (err, c_result) {
                            result = val;
                            f['return']();
                        });
                    });
                } catch (e) {
                    err_result = e;
                    result = '';
                    f['return']();
                }
            }
        });
        f.wait();
        if (err_result) { throw err_result; }
        return result;
    },
    
    // #### fetchJSONResource
    // Fetch an HTTP resource with JSON representation, parse the JSON and 
    // return a JS object.
    fetchJSONResource: function (url, cache_tmout) {
        if (!cache_tmout) cache_tmout = 3600;
        var key = 'kuma:json_resource:' + md5(url.toLowerCase());
        return cacheFn(key, 600, function (next) {
            var opts = {
                method: 'GET',
                headers: { 'Cache-Control': env.cache_control },
                url: url
            };
            try {
                request(opts, function (err, resp, body) {
                    var result = null;
                    if (resp && 200 == resp.statusCode) {
                        result = JSON.parse(body);
                    }
                    next(result);
                });
            } catch (e) {
                next(null);
            }            
        });
    }
} %>
Revert to this revision