Browse files

Separate parse adaptor

  • Loading branch information...
1 parent 2273249 commit 2fac8dbf09c89fbeb338e412395a731c21db1674 @mikepugh committed Feb 17, 2014
Showing with 107 additions and 41 deletions.
  1. +20 −0 adaptors/prerender-parse.js
  2. +44 −0 adaptors/test/prerender-parse-tests.js
  3. +43 −41 index.js
View
20 adaptors/prerender-parse.js
@@ -0,0 +1,20 @@
+
+var parseAdaptor = module.exports = function(Parse) {
+ return function(options, callback) {
+ Parse.Cloud.httpRequest({
+ url: options.href,
+ headers: options.headers,
+ success: function(res) {
+ res.body = res.text;
+ res.statusCode = res.status;
+ callback(res);
+ },
+ error: function(res) {
+ console.error('Request failed with code ' + res.status);
+ console.error(res);
+ callback(null);
+ }
+ });
+ };
+};
+
View
44 adaptors/test/prerender-parse-tests.js
@@ -0,0 +1,44 @@
+var ParseMock = {
+ Cloud: {
+ config: {},
+ httpRequest: function(options) {
+ config = options;
+ }
+ }
+};
+
+var assert = require('assert')
+ , sinon = require('sinon')
+ , prerender = require('../../index')
+ , parseAdaptor = require('../prerender-parse')
+ , user = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36';
+
+
+describe('Prerender Parse Adaptor', function() {
+ var adaptor = parseAdaptor(ParseMock);
+
+ it('should set parse adaptor', function() {
+ assert.notEqual(prerender.adaptor, adaptor);
+ prerender.setAdaptor(adaptor);
+ assert.equal(prerender.adaptor, adaptor);
+ });
+
+ it('should make parse http call', function() {
+ var req = {
+ method: 'GET',
+ url: '/path?_escaped_fragment_=',
+ headers: { 'user-agent': user },
+ protocol: 'https',
+ get: function(v) {
+ if(v === 'host') return 'google.com';
+ }
+ };
+
+ var spy = sinon.spy(ParseMock.Cloud, "httpRequest");
+ prerender.setAdaptor(adaptor);
+ prerender.getPrerenderedPageResponse(req);
+ assert(spy.called);
+ ParseMock.Cloud.httpRequest.restore();
+ });
+
+});
View
84 index.js
@@ -4,10 +4,6 @@ var http = require('http'),
url = require('url');
-// Parse doesn't expose this process object, so create a dummy obj to avoid null refs
-var proc = process || {};
-proc.env = proc.env || {};
-
var prerender = module.exports = function(req, res, next) {
if(!prerender.shouldShowPrerenderedPage(req)) return next();
@@ -31,7 +27,22 @@ var prerender = module.exports = function(req, res, next) {
});
};
-prerender.useParseCloud = typeof Parse !== 'undefined';
+// Helper method to get the prerenderServiceUrl from the env variable.
+prerender.getEnvServiceUrl = function() {
+ if(typeof process !== 'undefined' && typeof process.env !== 'undefined') {
+ return process.env.PRERENDER_SERVICE_URL;
+ }
+ return undefined;
+};
+
+// Can't set prerenderServiceUrl below since it needs to be changed later
+// on in a unit test, after this init code. Created method above to handle
+// scenario.
+if(typeof process !== 'undefined' && typeof process.env !== 'undefined') {
+ prerender.prerenderToken = process.env.PRERENDER_TOKEN;
+}
+
+
// googlebot, yahoo, and bingbot are not in this list because
// we support _escaped_fragment_ and want to ensure people aren't
@@ -137,49 +148,40 @@ prerender.shouldShowPrerenderedPage = function(req) {
return isRequestingPrerenderedPage;
};
+// Default node http adaptor
+prerender.adaptor = function(options, callback) {
+ http.get(options, function(res) {
+ var pageData = "";
+ res.on('data', function (chunk) {
+ pageData += chunk;
+ });
+
+ res.on('end', function(){
+ res.body = pageData;
+ callback(res);
+ });
+ }).on('error', function(e) {
+ callback(null);
+ });
+};
+
+prerender.setAdaptor = function(adaptor) {
+ if(adaptor) {
+ this.adaptor = adaptor;
+ }
+ return this;
+};
prerender.getPrerenderedPageResponse = function(req, callback) {
var options = url.parse(prerender.buildApiUrl(req));
- if(this.prerenderToken || proc.env.PRERENDER_TOKEN) {
+ if(this.prerenderToken) {
options.headers = {
- 'X-Prerender-Token': this.prerenderToken || proc.env.PRERENDER_TOKEN,
+ 'X-Prerender-Token': this.prerenderToken,
'User-Agent': req.headers['user-agent']
};
}
- if(prerender.useParseCloud) {
- // Use Parse's Cloud Code httpRequest method
- Parse.Cloud.httpRequest({
- url: options.href,
- headers: options.headers,
- success: function(res) {
- res.body = res.text;
- res.statusCode = res.status;
- callback(res);
- },
- error: function(res) {
- console.error('Request failed with code ' + res.status);
- console.error(res);
- callback(null);
- }
- });
- } else {
- // Not on Parse, use http api
- http.get(options, function(res) {
-
- var pageData = "";
- res.on('data', function (chunk) {
- pageData += chunk;
- });
-
- res.on('end', function(){
- res.body = pageData;
- callback(res);
- });
- }).on('error', function(e) {
- callback(null);
- });
- }
+ this.adaptor(options, callback);
};
@@ -199,7 +201,7 @@ prerender.buildApiUrl = function(req) {
prerender.getPrerenderServiceUrl = function() {
- return this.prerenderServiceUrl || proc.env.PRERENDER_SERVICE_URL || 'http://service.prerender.io/';
+ return this.prerenderServiceUrl || this.getEnvServiceUrl() || 'http://service.prerender.io/';
};
prerender.beforeRenderFn = function(req, done) {

0 comments on commit 2fac8db

Please sign in to comment.