Comparing version
@@ -6,2 +6,4 @@ /* jshint node:true */ | ||
var clone = require('clone'); | ||
function Cache(config) { | ||
@@ -19,7 +21,7 @@ config = config || {}; | ||
if(self.storage.hasOwnProperty(key)) { | ||
storageObj = self.storage[key]; | ||
storageObj = clone(self.storage[key]); | ||
storageObj.expired = self.clock.now() >= storageObj.expirationTime; | ||
resolve(storageObj); | ||
} else { | ||
reject(); | ||
reject(new Error('cache entry not found')); | ||
} | ||
@@ -31,2 +33,3 @@ }); | ||
var self = this; | ||
storageObj = clone(storageObj); | ||
return new Promise(function(resolve, reject) { | ||
@@ -43,2 +46,5 @@ storageObj.expirationTime = self.clock.now() + (storageObj.expiresIn || 0); | ||
Cache.PREFIX_NET = 'net.'; | ||
Cache.PREFIX_PARSER = 'parser.'; | ||
module.exports = Cache; |
@@ -10,3 +10,4 @@ /* jshint node:true */ | ||
Cache = require('./cache'), | ||
DataProvider = require('./data-provider'); | ||
DataProvider = require('./data-provider'), | ||
Logger = require('./logger'); | ||
@@ -23,2 +24,3 @@ function ESI(config) { | ||
this.serializer = config.serializer || new parse5.Serializer(); | ||
this.logger = new Logger(config); | ||
} | ||
@@ -62,4 +64,29 @@ | ||
var self = this; | ||
return this.processParsed(this.parser.parseFragment(html), options).then(function(result) { | ||
return self.serializer.serialize(result); | ||
return self.getParsedHtml(html) | ||
.then(function(parsedHtml) { | ||
return self.processParsed(parsedHtml, options); | ||
}) | ||
.then(function(result) { | ||
return self.serializer.serialize(result); | ||
}); | ||
}; | ||
ESI.prototype.getParsedHtml = function(html) { | ||
var self = this, | ||
cacheKey = Cache.PREFIX_PARSER + html; | ||
return new Promise(function(resolve, reject) { | ||
self.cache.get(cacheKey) | ||
.then(function(cached) { | ||
resolve(cached.value); | ||
}) | ||
.catch(function() { | ||
var parsedHtml = self.parser.parseFragment(html); | ||
self.cache.set(cacheKey, { | ||
value: parsedHtml | ||
}); | ||
resolve(parsedHtml); | ||
}); | ||
}); | ||
@@ -111,5 +138,6 @@ }; | ||
src = self.dataProvider.toFullyQualifiedURL(src); | ||
var cacheKey = Cache.PREFIX_NET + src; | ||
return new Promise(function (resolve, reject) { | ||
self.cache.get(src) | ||
self.cache.get(cacheKey) | ||
// in cache | ||
@@ -120,3 +148,3 @@ .then(function (result) { | ||
.then(function (result) { | ||
self.setCacheResult(src, result); | ||
self.setCacheResult(cacheKey, result); | ||
}); | ||
@@ -130,3 +158,3 @@ } | ||
.then(function (result) { | ||
self.setCacheResult(src, result); | ||
self.setCacheResult(cacheKey, result); | ||
resolve(result.body); | ||
@@ -150,4 +178,4 @@ }) | ||
ESI.prototype.setCacheResult = function (src, result) { | ||
this.cache.set(src, { | ||
ESI.prototype.setCacheResult = function (key, result) { | ||
this.cache.set(key, { | ||
expiresIn: getCacheTime(result.response.headers['cache-control']), | ||
@@ -154,0 +182,0 @@ value: result.body |
{ | ||
"name": "nodesi", | ||
"version": "1.0.1", | ||
"description": "ESI: the good parts in node.js", | ||
"main": "esi.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha test/*.js" | ||
}, | ||
"engines": { | ||
"node": "0.12.x" | ||
}, | ||
"dependencies": { | ||
"parse5": "1.4.1", | ||
"request": "2.53.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "2.1.0" | ||
}, | ||
"keywords": [ | ||
"esi", | ||
"http", | ||
"rest" | ||
], | ||
"author": "", | ||
"license": "ISC" | ||
"name": "nodesi", | ||
"version": "1.0.4", | ||
"description": "ESI: the good parts in node.js", | ||
"main": "esi.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha test/*.js", | ||
"perf": "node perf.js" | ||
}, | ||
"engines": { | ||
"node": "0.12.x" | ||
}, | ||
"dependencies": { | ||
"clone": "^1.0.1", | ||
"node.extend": "^1.1.3", | ||
"parse5": "1.4.1", | ||
"request": "2.53.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "2.1.0", | ||
"express": "4.12.2" | ||
}, | ||
"keywords": [ | ||
"esi", | ||
"http", | ||
"rest" | ||
], | ||
"author": "", | ||
"license": "ISC" | ||
} |
@@ -15,5 +15,3 @@ /* jshint node:true */ | ||
// given | ||
var cache = new Cache({ | ||
clock: {} | ||
}); | ||
var cache = new Cache(); | ||
@@ -20,0 +18,0 @@ // when |
@@ -8,4 +8,8 @@ /* jshint node:true */ | ||
http = require('http'), | ||
fs = require('fs'), | ||
Clock = require('./clock'), | ||
parse5 = require('parse5'), | ||
parser = new parse5.Parser(), | ||
serializer = new parse5.Serializer(), | ||
@@ -272,3 +276,3 @@ ESI = require('../lib/esi'), | ||
processed.then(function (response) { | ||
return esi.cache.get('http://localhost:' + port + '/cacheme'); | ||
return esi.cache.get(Cache.PREFIX_NET + 'http://localhost:' + port + '/cacheme'); | ||
}).then(function (cached) { | ||
@@ -281,2 +285,30 @@ assert.equal(cached.value, 'hello'); | ||
it('should populate internal cache with parsed html', function (done) { | ||
// given | ||
server.addListener('request', function (req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end('hello'); | ||
}); | ||
var html = '<esi:include src="/cacheme"></esi:include>'; | ||
// when | ||
var esi = new ESI({ | ||
baseUrl: 'http://localhost:' + port | ||
}); | ||
var processed = esi.process(html); | ||
// then | ||
processed.then(function (response) { | ||
return esi.cache.get(Cache.PREFIX_PARSER + html); | ||
}).then(function (cached) { | ||
assert.equal(cached.value.childNodes[0].nodeName, 'esi:include'); | ||
assert.equal(cached.value.childNodes[0].nodeName, parser.parseFragment(html).childNodes[0].nodeName); | ||
done(); | ||
}).catch(done); | ||
}); | ||
it('should return data from the cache', function (done) { | ||
@@ -289,3 +321,3 @@ | ||
var html = '<esi:include src="/cacheme"></esi:include>'; | ||
cache.set('http://example.com/cacheme', { | ||
cache.set(Cache.PREFIX_NET + 'http://example.com/cacheme', { | ||
value: 'stuff' | ||
@@ -385,3 +417,3 @@ }); | ||
var html = '<esi:include src="/cacheme"></esi:include>'; | ||
cache.set('http://example.com/cacheme', { | ||
cache.set(Cache.PREFIX_NET + 'http://example.com/cacheme', { | ||
value: 'stuff', | ||
@@ -489,2 +521,44 @@ expiresIn: 1 | ||
it('should be able to use custom log output', function (done) { | ||
// given | ||
var esi = new ESI({ | ||
logTo: { | ||
write: function(log) { | ||
// then | ||
assert.equal(log, 'test'); | ||
done(); | ||
} | ||
} | ||
}); | ||
// when | ||
esi.logger.write('test'); | ||
}); | ||
it('should be able to log output to a file', function (done) { | ||
// given | ||
var PATH = './test/logger-test-output.txt'; | ||
var stream = fs.createWriteStream(PATH); | ||
var testStr = '' + Math.random(); | ||
var esi = new ESI({ | ||
logTo: stream | ||
}); | ||
// when | ||
esi.logger.write(testStr); | ||
// then | ||
fs.readFile(PATH, function(err, contents) { | ||
if(err) { | ||
done(err); | ||
} else { | ||
assert.equal(contents, testStr); | ||
fs.unlink(PATH, done); | ||
} | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
36111
20.48%16
23.08%936
18.63%0
-100%44
Infinity%4
100%2
100%5
150%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added