express-turnout
Version:
Pre-rendering Single-Page-Application for crawlers.
120 lines (107 loc) • 3.52 kB
JavaScript
// Generated by CoffeeScript 1.9.3
var Promise, Turnout, debug, exec, phantomScript, querystring;
debug = (require('debug'))('express:turnout');
Promise = require('bluebird');
exec = (require('child_process')).exec;
phantomScript = require.resolve('../lib/turnout.phantom.js');
querystring = require('querystring');
Turnout = (function() {
function Turnout(options1) {
var base, base1, base2, base3, base4;
this.options = options1 != null ? options1 : {};
if ((base = this.options).ua == null) {
base.ua = ['Googlebot', 'Twitterbot'];
}
if ((base1 = this.options).blacklist == null) {
base1.blacklist = [];
}
if ((base2 = this.options).whitelist == null) {
base2.whitelist = [];
}
if ((base3 = this.options).timeout == null) {
base3.timeout = 3000;
}
if ((base4 = this.options).maxBuffer == null) {
base4.maxBuffer = 1024 * 1024;
}
debug('new Turnout', this.options);
}
Turnout.prototype.isBot = function(req) {
var bot, i, key, len, ref, ref1, ua;
ua = (ref = req.headers['user-agent']) != null ? ref : '';
bot = null;
ref1 = this.options.ua;
for (i = 0, len = ref1.length; i < len; i++) {
key = ref1[i];
if (bot == null) {
bot = ua.match(key);
}
}
if (bot == null) {
bot = req.query._escaped_fragment_ != null;
}
debug(bot, 'isBot', ua);
return bot;
};
Turnout.prototype.render = function(req) {
var options, uri;
uri = this.getUri(req);
options = this.options;
debug("Render " + uri + " Limit by " + options.timeout + "ms");
return new Promise(function(resolve, reject) {
var i, j, len, len1, matched, pattern, ref, ref1, script, url;
url = req.originalUrl.slice(1);
if (options.blacklist.length) {
ref = options.blacklist;
for (i = 0, len = ref.length; i < len; i++) {
pattern = ref[i];
debug(url.match(pattern), 'blacklisted', url, 'by', pattern);
if (url.match(pattern)) {
return reject('Disallow by blacklist "' + pattern + '"');
}
}
}
if (options.whitelist.length) {
matched = false;
ref1 = options.whitelist;
for (j = 0, len1 = ref1.length; j < len1; j++) {
pattern = ref1[j];
debug(url.match(pattern), 'whitelisted', url, 'by', pattern);
if (url.match(pattern)) {
matched = true;
break;
}
}
if (!matched) {
return reject('Disallow by whitelist');
}
}
script = "phantomjs " + phantomScript + " " + uri + " " + options.timeout;
debug('Execute ' + script);
return exec(script, {
maxBuffer: options.maxBuffer
}, function(error, stdout, stderr) {
var summary;
if (error == null) {
resolve(stdout.trim());
}
summary = stderr.trim();
if ((error != null ? error.message : void 0) === 'stdout maxBuffer exceeded.') {
summary = error;
}
return reject(summary);
});
});
};
Turnout.prototype.getUri = function(req) {
var params, qs, ref, uri, url;
uri = req.protocol + '://' + req.get('host') + req.originalUrl;
ref = uri.split('?', 2), url = ref[0], params = ref[1];
qs = querystring.parse(params);
delete qs._escaped_fragment_;
uri = url + '?' + querystring.stringify(qs);
return uri;
};
return Turnout;
})();
module.exports = Turnout;