diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..79e6ea4 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +node_modules +test \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..a6152b0 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,48 @@ +{ + "extends": "airbnb-base", + "env": { + "es6": true, + "jasmine": true, + "node": true, + "browser": false + }, + "globals": { + "spyOn": true, + "window": true, + "document": true, + "app": true, + "helpers": true + }, + "rules": { + "brace-style": ["error", "stroustrup"], + "comma-dangle": ["error", "never"], + "func-names": 0, + "indent": ["error", 4, { "SwitchCase": 1 }], + "max-len": [2, 180, 4, { + "ignoreUrls": true, + "ignoreComments": false + }], + "new-cap": ["error", {"capIsNewExceptions": ["Router", "ObjectId", "DEBUG"], "properties": false}], + "no-underscore-dangle": 0, + "no-unused-vars": ["warn"], + "no-use-before-define": ["error", { "functions": false }], + "no-var": ["off"], + "one-var": ["off"], + "vars-on-top": ["off"], + "no-param-reassign": ["off"], + "padded-blocks": 0, + "prefer-template": ["off"], + "prefer-arrow-callback": ["off"], + "require-jsdoc": ["warn", { + "require": { + "FunctionDeclaration": true, + "MethodDefinition": true, + "ClassDeclaration": true + } + }], + "object-shorthand": ["error", "never"], + "space-before-function-paren": "off", + "strict": "off", + "valid-jsdoc": ["error"] + } +} \ No newline at end of file diff --git a/example/server.js b/example/server.js index ef26fe6..48da12b 100644 --- a/example/server.js +++ b/example/server.js @@ -29,7 +29,7 @@ function Server() { }); // start the server - app.listen(8080, function(){ + app.listen(8080, function() { console.log('Server running... Visit http://localhost:8080 in your browser'); }); } diff --git a/lib/node-iframe-replacement.js b/lib/node-iframe-replacement.js index e746cea..0d2e435 100644 --- a/lib/node-iframe-replacement.js +++ b/lib/node-iframe-replacement.js @@ -3,50 +3,50 @@ var isUrl = require('is-url'), // module use to verify the sourceUrl is an actual URL cheerio = require('cheerio'), // used to convert raw html into jQuery style object to we can use css selectors request = require('request-promise'), // promise based request object - NodeCache = require( "node-cache" ), // auto expiring in memory caching collection + NodeCache = require('node-cache'), // auto expiring in memory caching collection cache = new NodeCache({ stdTTL: 300 }); // cache source HTML for 5 minutes, after which we fetch a new version -module.exports = function(req, res, next){ +module.exports = function(req, res, next) { - // add res.merge to response object to allow us to fetch template url or use html template string - res.merge = function(view, model, callback){ - - if (!model.sourceUrl){ + // add res.merge to response object to allow us to fetch template url or use html template string + res.merge = function(view, model, callback) { + + if (!model.sourceUrl) { // no .sourceUrl, therefore process this as normal res.render(view, model, callback); - } + } else { // if no template selector provided, use body tag var sourcePlaceholder = model.sourcePlaceholder || 'body'; - + // resolve the template, either url to jquery object or html - resolveTemplate(model.sourceUrl).then(function($template){ - + resolveTemplate(model.sourceUrl).then(function($template) { + // if a transform method is provided, execute - if (model.transform){ + if (model.transform) { // pass a jquery version of the html along with a copy of the model model.transform($template, model); } - + // convert view into html to be inserted res.render(view, model, function(err, html) { if (err) next(err); - + // convert view into jquery object var $view = cheerio.load(html); - + // if the view contains a head, append its contents to the original var $viewHead = $view('head'); - if ($viewHead && $viewHead.length>0){ + if ($viewHead && $viewHead.length > 0) { // append meta, link, script and noscript to head $template('head').append($viewHead.html()); } - - // if the view has a body, use its contents otherwise use the entire content + + // if the view has a body, use its contents otherwise use the entire content var $viewBody = $view('body'); - if ($viewBody && $viewBody.length>0){ + if ($viewBody && $viewBody.length > 0) { // inject content into selector or body $template(sourcePlaceholder).html($viewBody.html()); } @@ -54,60 +54,61 @@ module.exports = function(req, res, next){ // no body tag in view, insert all content $template(sourcePlaceholder).html($view.html()); } - + // return merged content res.status(200).send($template.html()); }); }) - .catch(function(err){ + .catch(function(err) { // request failed, inform the user res.status(500).send(err).end(); }); } }; - + next(); -} +}; /** - * Converts source url to $ object + * @description Converts source url to $ object * @param {string} sourceUrl - url to external html content + * @returns {Promise} returns promise of html source from sourceUrl */ -function resolveTemplate(sourceUrl){ - - return new Promise(function(resolve, reject){ - +function resolveTemplate(sourceUrl) { + + return new Promise(function(resolve, reject) { + // if its a url and we have the contents in cache - if (isUrl(sourceUrl) && cache.get(sourceUrl)){ - + if (isUrl(sourceUrl) && cache.get(sourceUrl)) { + // get source html from cache var html = cache.get(sourceUrl); - + // covert html into jquery object var $ = cheerio.load(html); - + // return source as a jquery style object resolve($); } - else if (isUrl(sourceUrl)){ - + else if (isUrl(sourceUrl)) { + // request the source url return request({ uri: sourceUrl }).then(function(html){ - + // convert html into jquery style object so we can use selectors var $ = cheerio.load(html); - + // insert base tag to ensure links/scripts/styles load correctly $('head').prepend(''); - + // cache result as HTML so we dont have to keep getting it for future requests and it remains clean cache.set(sourceUrl, $.html()); - + // resolve with jquery object containing content resolve($); }) .catch(function(err) { - + // request failed reject('Unable to retrieve ' + sourceUrl); }); @@ -117,4 +118,4 @@ function resolveTemplate(sourceUrl){ resolve(sourceUrl); } }); -}; \ No newline at end of file +} diff --git a/package.json b/package.json index 723988e..3910931 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-iframe-replacement", - "version": "0.0.5", + "version": "0.0.6", "description": "NodeJS + Express replacement for the HTML iframe", "main": "index.js", "scripts": { @@ -40,6 +40,9 @@ "request-promise": "^3.0.0" }, "devDependencies": { + "eslint": "^3.12.2", + "eslint-config-airbnb-base": "^11.0.0", + "eslint-plugin-import": "^2.2.0", "jasmine-node": "^1.14.5", "nock": "^9.0.2", "supertest": "^2.0.1"