Skip to content

Commit

Permalink
added eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doherty committed Jan 2, 2017
1 parent 0fb5220 commit f9dff9d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test
48 changes: 48 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -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"]
}
}
2 changes: 1 addition & 1 deletion example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
Expand Down
79 changes: 40 additions & 39 deletions lib/node-iframe-replacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,112 @@
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());
}
else {
// 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('<base href="' + sourceUrl + '">');

// 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);
});
Expand All @@ -117,4 +118,4 @@ function resolveTemplate(sourceUrl){
resolve(sourceUrl);
}
});
};
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f9dff9d

Please sign in to comment.