docpad-plugin-cachr
Advanced tools
Comparing version 2.0.2 to 2.1.0
## History | ||
- v2.1.0 August 17, 2012 | ||
- Updated [request](https://github.com/mikeal/request) dependency from v2.9.x to v2.10.x | ||
- Added tests | ||
- Added `refreshCache` option (defaults to `false`) | ||
- Now halts generation until all files are cached | ||
- v2.0.2 August 10, 2012 | ||
@@ -4,0 +10,0 @@ - Re-added markdown files to npm distribution as they are required for the npm website |
@@ -7,6 +7,7 @@ // Generated by CoffeeScript 1.3.3 | ||
module.exports = function(BasePlugin) { | ||
var CachrPlugin, balUtil, pathUtil, request; | ||
var CachrPlugin, balUtil, fsUtil, pathUtil, request; | ||
balUtil = require('bal-util'); | ||
request = require('request'); | ||
pathUtil = require('path'); | ||
fsUtil = require('fs'); | ||
return CachrPlugin = (function(_super) { | ||
@@ -23,2 +24,3 @@ | ||
CachrPlugin.prototype.config = { | ||
refreshCache: false, | ||
urlPrefix: '/_docpad/plugins/cachr', | ||
@@ -36,3 +38,3 @@ pathPrefix: pathUtil.join('_docpad', 'plugins', 'cachr') | ||
config = this.config; | ||
name = pathUtil.basename(sourceUrl); | ||
name = require('crypto').createHash('md5').update(sourceUrl).digest('hex') + pathUtil.extname(sourceUrl).replace(/[\?\#].*$/, ''); | ||
details = { | ||
@@ -54,18 +56,18 @@ name: name, | ||
viaRequest = function() { | ||
var writeStream; | ||
docpad.logger.log('debug', "Cachr is fetching [" + details.sourceUrl + "] to [" + details.cachePath + "]"); | ||
writeStream = fs.createWriteStream(details.cachePath); | ||
docpad.log('debug', "Cachr is fetching [" + details.sourceUrl + "] to [" + details.cachePath + "]"); | ||
return request({ | ||
uri: details.sourceUrl | ||
}, function(err) { | ||
uri: details.sourceUrl, | ||
encoding: null | ||
}, function(err, response, body) { | ||
if (err) { | ||
++attempt; | ||
if (attempt === 3) { | ||
docpad.log('debug', "Cachr is gave up fetching [" + details.sourceUrl + "] to [" + details.cachePath + "]"); | ||
return balUtil.exists(details.cachePath, function(exists) { | ||
if (exists) { | ||
return fs.unlink(details.cachePath, function(err2) { | ||
return typeof next === "function" ? next(err) : void 0; | ||
return balUtil.unlink(details.cachePath, function(err2) { | ||
return next(err); | ||
}); | ||
} else { | ||
return typeof next === "function" ? next(err) : void 0; | ||
return next(err); | ||
} | ||
@@ -77,16 +79,23 @@ }); | ||
} else { | ||
return typeof next === "function" ? next() : void 0; | ||
docpad.log('debug', "Cachr fetched [" + details.sourceUrl + "] to [" + details.cachePath + "]"); | ||
return balUtil.writeFile(details.cachePath, body, function(err) { | ||
return next(err); | ||
}); | ||
} | ||
}).pipe(writeStream); | ||
}); | ||
}; | ||
balUtil.isPathOlderThan(details.cachePath, 1000 * 60 * 5, function(err, older) { | ||
if (err) { | ||
return typeof next === "function" ? next(err) : void 0; | ||
} | ||
if (older === null || older === true) { | ||
return viaRequest(); | ||
} else { | ||
return typeof next === "function" ? next() : void 0; | ||
} | ||
}); | ||
if (this.config.refreshCache) { | ||
viaRequest(); | ||
} else { | ||
balUtil.isPathOlderThan(details.cachePath, 1000 * 60 * 5, function(err, older) { | ||
if (err) { | ||
return next(err); | ||
} | ||
if (older === null || older === true) { | ||
return viaRequest(); | ||
} else { | ||
return next(); | ||
} | ||
}); | ||
} | ||
return this; | ||
@@ -104,5 +113,3 @@ }; | ||
}; | ||
if (typeof next === "function") { | ||
next(); | ||
} | ||
next(); | ||
return this; | ||
@@ -112,7 +119,6 @@ }; | ||
CachrPlugin.prototype.writeAfter = function(_arg, next) { | ||
var cachr, cachrPath, config, docpad, failures, logger, templateData, urlsToCache, urlsToCacheLength; | ||
var cachr, cachrPath, config, docpad, failures, templateData, urlsToCache, urlsToCacheLength; | ||
templateData = _arg.templateData; | ||
cachr = this; | ||
docpad = this.docpad; | ||
logger = this.docpad.logger; | ||
config = this.config; | ||
@@ -124,5 +130,5 @@ urlsToCache = this.urlsToCache; | ||
if (!urlsToCacheLength) { | ||
return typeof next === "function" ? next() : void 0; | ||
return next(); | ||
} | ||
logger.log('info', 'Cachr started caching...', (failures ? "with " + failures + " failures" : '')); | ||
docpad.log('info', "Cachr is caching " + urlsToCacheLength + " files...", (failures ? "with " + failures + " failures" : '')); | ||
balUtil.ensurePath(cachrPath, function(err) { | ||
@@ -132,6 +138,7 @@ var tasks, | ||
if (err) { | ||
return typeof next === "function" ? next(err) : void 0; | ||
return next(err); | ||
} | ||
tasks = new balUtil.Group(function(err) { | ||
return logger.log((failures ? 'warn' : 'info'), 'Cachr finished caching', (failures ? "with " + failures + " failures" : '')); | ||
docpad.log((failures ? 'warn' : 'info'), 'Cachr finished caching', (failures ? "with " + failures + " failures" : '')); | ||
return next(); | ||
}); | ||
@@ -142,3 +149,3 @@ balUtil.each(urlsToCache, function(details, sourceUrl) { | ||
if (err) { | ||
docpad.logger.log('warn', "Cachr failed to fetch: " + sourceUrl); | ||
docpad.log('warn', "Cachr failed to fetch: " + sourceUrl); | ||
docpad.error(err); | ||
@@ -152,3 +159,3 @@ ++failures; | ||
tasks.async(); | ||
return typeof next === "function" ? next() : void 0; | ||
return this; | ||
}); | ||
@@ -155,0 +162,0 @@ return this; |
{ | ||
"name": "docpad-plugin-cachr", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Caches remote resources locally", | ||
@@ -32,3 +32,3 @@ "homepage": "https://github.com/bevry/docpad-extras", | ||
"dependencies": { | ||
"request": "2.9.x", | ||
"request": "2.10.x", | ||
"bal-util": "1.13.x" | ||
@@ -39,3 +39,6 @@ }, | ||
}, | ||
"main": "./out/cachr.plugin.js" | ||
"main": "./out/cachr.plugin.js", | ||
"scripts": { | ||
"test": "node ./test/cachr.test.js" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9241
6
167
1
+ Addedrequest@2.10.0(transitive)
- Removedrequest@2.9.203(transitive)
Updatedrequest@2.10.x