+36
-37
@@ -12,54 +12,53 @@ /* | ||
| var $ = require('cheerio'), | ||
| var cheerio = require('cheerio'), | ||
| MD5 = require('MD5'), | ||
| fs = require('fs'); | ||
| exports.busted = function(fileContents, options) { | ||
| var self = this; | ||
| function loadAttribute(content) { | ||
| if (content.name.toLowerCase() === 'link') { | ||
| return content.attribs.href; | ||
| } | ||
| options.basePath = options.basePath || ""; | ||
| if (options.type === 'timestamp') { | ||
| self.timestamp = new Date().getTime(); | ||
| if (content.name.toLowerCase() === 'script') { | ||
| return content.attribs.src; | ||
| } | ||
| var protocolRegEx = /^http(s)?/, | ||
| scripts = $(fileContents).find('script'), | ||
| styles = $(fileContents).find('link[rel=stylesheet]'), | ||
| data; | ||
| throw "No content awaited in this step of process"; | ||
| } | ||
| // Loop the stylesheet hrefs | ||
| for (var i = 0; i < styles.length; i++) { | ||
| var origHref = styles[i].attribs.href, | ||
| styleHref = styles[i].attribs.href.split("?")[0]; | ||
| // Test for http(s) and don't cache bust if (assumed) served from CDN | ||
| if (!protocolRegEx.test(styleHref)) { | ||
| if (options.type === 'timestamp') { | ||
| fileContents = fileContents.replace(origHref, styleHref + '?t=' + self.timestamp); | ||
| } else { | ||
| data = fs.readFileSync(options.basePath + styleHref).toString(); | ||
| fileContents = fileContents.replace(origHref, styleHref + '?v=' + MD5(data)); | ||
| } | ||
| } | ||
| } | ||
| exports.busted = function(fileContents, options) { | ||
| var self = this, $ = cheerio.load(fileContents); | ||
| // Loop the script srcs | ||
| for (var j = 0; j < scripts.length; j++) { | ||
| var origSrc = scripts[j].attribs.src, | ||
| scriptSrc = scripts[j].attribs.src.split("?")[0]; | ||
| self.MD5 = function(fileContents, originalAttrValue, options) { | ||
| var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0], | ||
| hash = MD5(fs.readFileSync(options.basePath + originalAttrValueWithoutCacheBusting).toString()); | ||
| return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?v=' + hash); | ||
| }; | ||
| self.timestamp = function(fileContents, originalAttrValue, options) { | ||
| var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0]; | ||
| return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?t=' + options.currentTimestamp); | ||
| }; | ||
| options = { | ||
| basePath : options.basePath || "", | ||
| type : options.type || "MD5", | ||
| currentTimestamp : new Date().getTime() | ||
| }; | ||
| var protocolRegEx = /^http(s)?/, elements = $('script[src], link[rel=stylesheet][href]'); | ||
| for (var i = 0, len = elements.length; i < len; i++) { | ||
| var originalAttrValue = loadAttribute(elements[i]); | ||
| // Test for http(s) and don't cache bust if (assumed) served from CDN | ||
| if (!protocolRegEx.test(scriptSrc)) { | ||
| if (options.type === 'timestamp') { | ||
| fileContents = fileContents.replace(origSrc, scriptSrc + '?t=' + self.timestamp); | ||
| } else { | ||
| data = fs.readFileSync(options.basePath + scriptSrc).toString(); | ||
| fileContents = fileContents.replace(origSrc, scriptSrc + '?v=' + MD5(data)); | ||
| } | ||
| if (!protocolRegEx.test(originalAttrValue)) { | ||
| fileContents = self[options.type](fileContents, originalAttrValue, options); | ||
| } | ||
| } | ||
| return fileContents; | ||
| }; |
+3
-3
| { | ||
| "name": "cachebust", | ||
| "version": "1.1.0", | ||
| "version": "2.0.0", | ||
| "main": "lib/cachebust.js", | ||
@@ -27,4 +27,4 @@ "description": "Append a query string to your assets to bust that cache!", | ||
| "dependencies": { | ||
| "MD5": "^1.2.1", | ||
| "cheerio": "^0.17.0" | ||
| "md5": "^2.0.0", | ||
| "cheerio": "^0.19.0" | ||
| }, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
@@ -6,2 +6,3 @@ <!DOCTYPE html> | ||
| <title>Index</title> | ||
| <link rel="stylesheet">Foo !</link> | ||
| <link rel="stylesheet" href="styles/main.css" /> | ||
@@ -12,2 +13,5 @@ <link rel="stylesheet" href="http://cdn.com/styles/main.css" /> | ||
| <body> | ||
| <script type="text/javascript"> | ||
| console.log("Bar !"); | ||
| </script> | ||
| <script src="scripts/moar.js"></script> | ||
@@ -14,0 +18,0 @@ <script src="http://best-cdn-ever.com/scripts/evenMoar.js"></script> |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
9167
1.17%171
-2.84%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated