Skip to content

Commit 512b47a

Browse files
author
John Doherty
committed
replaced internal isUrl with node module to resolve bug using IP address as sourceUrl
1 parent 6fe617f commit 512b47a

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

lib/node-iframe-replacement.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
var cheerio = require('cheerio'), // used to convert raw html into jQuery style object to we can use css selectors
3+
var isUrl = require('is-url'), // module use to verify the sourceUrl is an actual URL
4+
cheerio = require('cheerio'), // used to convert raw html into jQuery style object to we can use css selectors
45
request = require('request-promise'), // promise based request object
56
NodeCache = require( "node-cache" ), // auto expiring in memory caching collection
67
cache = new NodeCache({ stdTTL: 300 }); // cache source HTML for 5 minutes, after which we fetch a new version
@@ -77,7 +78,7 @@ function resolveTemplate(sourceUrl){
7778
return new Promise(function(resolve, reject){
7879

7980
// if its a url and we have the contents in cache
80-
if (sourceUrl.isUrl() && cache.get(sourceUrl)){
81+
if (isUrl(sourceUrl) && cache.get(sourceUrl)){
8182

8283
// get source html from cache
8384
var html = cache.get(sourceUrl);
@@ -88,7 +89,7 @@ function resolveTemplate(sourceUrl){
8889
// return source as a jquery style object
8990
resolve($);
9091
}
91-
else if (sourceUrl.isUrl()){
92+
else if (isUrl(sourceUrl)){
9293

9394
// request the source url
9495
return request({ uri: sourceUrl }).then(function(html){
@@ -116,9 +117,4 @@ function resolveTemplate(sourceUrl){
116117
resolve(sourceUrl);
117118
}
118119
});
119-
}
120-
121-
// helper function to allow us to determine if the template is a url
122-
String.prototype.isUrl = function(){
123-
return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
124-
}
120+
};

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
},
3434
"dependencies": {
3535
"cheerio": "^0.20.0",
36-
"node-cache": "^3.2.1",
37-
"request-promise": "^3.0.0",
3836
"express": "^4.14.0",
39-
"express-handlebars": "^3.0.0"
37+
"express-handlebars": "^3.0.0",
38+
"is-url": "^1.2.2",
39+
"node-cache": "^3.2.1",
40+
"request-promise": "^3.0.0"
4041
},
4142
"devDependencies": {
4243
"jasmine-node": "^1.14.5",

0 commit comments

Comments
 (0)