Comparing version 1.2.4 to 1.3.0
@@ -11,2 +11,3 @@ var Express = require('express'); | ||
regex: /\.(css|js|html)/ | ||
sendOptions: {} | ||
}); | ||
@@ -13,0 +14,0 @@ |
@@ -6,2 +6,3 @@ var Fs = require('fs'); | ||
var Path = require('path'); | ||
var Send = require('send'); | ||
var Wrench = require('wrench'); | ||
@@ -23,2 +24,3 @@ | ||
regex: Joi.object().type(RegExp).required().default(/.*/), | ||
sendOptions: Joi.object() | ||
}; | ||
@@ -41,8 +43,5 @@ | ||
self.naturalMap = {}; | ||
self.hashedsMap = {}; | ||
self.contentMap = []; | ||
self.urlsMap = {}; | ||
self.readFiles(); | ||
self.calcHases(); | ||
}); | ||
@@ -54,3 +53,3 @@ | ||
// Reads all the files into buffers in memory and associates the indices to original paths | ||
// Reads all the files associates the indices to original paths | ||
@@ -60,20 +59,9 @@ internals.Box.prototype.readFiles = function () { | ||
for (var i = 0; i < this.files.length; i++) { | ||
this.naturalMap[Path.join(this.options.url, this.files[i])] = i; | ||
this.contentMap[i] = { | ||
contents: Fs.readFileSync(Path.join(this.options.path, this.files[i])), | ||
contentType: Mime.lookup(Path.extname(this.files[i])) | ||
}; | ||
} | ||
}; | ||
// Calculates the hashes for each file | ||
internals.Box.prototype.calcHases = function () { | ||
for (var i = 0; i < this.files.length; i++) { | ||
var hash = Md5(this.contentMap[i].contents); | ||
var path = Path.join(Path.resolve(this.options.path), this.files[i]); | ||
var file = this.files[i]; | ||
var hash = Md5(Fs.readFileSync(path)); | ||
var hashedPath = Path.join( | ||
var naturalUrl = Path.join(this.options.url, this.files[i]); | ||
var hashedUrl = Path.join( | ||
this.options.url, | ||
@@ -83,3 +71,4 @@ Path.dirname(file), | ||
this.hashedsMap[hashedPath] = i; | ||
this.urlsMap[naturalUrl] = {path: path, hashed: hashedUrl}; | ||
this.urlsMap[hashedUrl] = {path: path, natural: naturalUrl}; | ||
} | ||
@@ -91,11 +80,22 @@ }; | ||
internals.Box.prototype.url = function (url) { | ||
internals.Box.prototype.getHashedUrl = function (url) { | ||
if (this.naturalMap.hasOwnProperty(url)) { | ||
return this.hashedsMap[this.naturalMap[url]]; | ||
if (this.urlsMap.hasOwnProperty(url) && typeof this.urlsMap[url].hashed === 'string') { | ||
return this.urlsMap[url].hashed; | ||
} | ||
return false; | ||
return null; | ||
}; | ||
// Take a hashes URL and returns the natural version | ||
internals.Box.prototype.getNaturalUrl = function (url) { | ||
if (this.urlsMap.hasOwnProperty(url) && typeof this.urlsMap[url].natural === 'string') { | ||
return this.urlsMap[url].natural; | ||
} | ||
return null; | ||
}; | ||
// The middleware function used by express - serves the files | ||
@@ -107,10 +107,7 @@ | ||
if (this.naturalMap.hasOwnProperty(url)) { | ||
res.header('content-type', this.contentMap[this.naturalMap[url]].contentType); | ||
return res.end(this.contentMap[this.naturalMap[url]].contents); | ||
} | ||
if (this.urlsMap.hasOwnProperty(url)) { | ||
if (this.hashedsMap.hasOwnProperty(url)) { | ||
res.header('content-type', this.contentMap[this.hashedsMap[url]].contentType); | ||
return res.end(this.contentMap[this.hashedsMap[url]].contents); | ||
console.log(this.getHashedUrl(url)); | ||
return Send(req, this.urlsMap[url].path, this.options.sendOptions).pipe(res); | ||
} | ||
@@ -117,0 +114,0 @@ |
{ | ||
"name": "bomb", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "A frontend cache-busting static middleware for express", | ||
@@ -29,4 +29,5 @@ "main": "index.js", | ||
"mime": "^1.3.4", | ||
"send": "^0.12.2", | ||
"wrench": "^1.5.8" | ||
} | ||
} |
@@ -37,2 +37,3 @@ # bomb | ||
regex: /\.(css|js|html)/ // only serve files that match regex | ||
sendOptions: {} // options for the `send` module | ||
}); | ||
@@ -53,4 +54,8 @@ | ||
To get the hashed URL from a natural one, just call `box.url(url)`: | ||
To get the hashed URL from a natural one, just call `box.getHashedUrl(url)`: | ||
`box.url('/public/example.css') === '/public/example-4591dd5c9fd5aab8f5d7df6ac939441c.css'` | ||
`box.getHashedUrl('/public/example.css') === '/public/example-4591dd5c9fd5aab8f5d7df6ac939441c.css'` | ||
To get the natural URL from a hashed one, just call `box.getNaturalUrl(url)`: | ||
`box.getNaturalUrl('/public/example-4591dd5c9fd5aab8f5d7df6ac939441c.css') === '/public/example.css'` |
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
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
7424
13
60
5
81
+ Addedsend@^0.12.2
+ Addedcrc@3.2.1(transitive)
+ Addeddebug@2.2.0(transitive)
+ Addeddepd@1.0.1(transitive)
+ Addeddestroy@1.0.3(transitive)
+ Addedee-first@1.1.0(transitive)
+ Addedescape-html@1.0.1(transitive)
+ Addedetag@1.6.0(transitive)
+ Addedfresh@0.2.4(transitive)
+ Addedmime@1.3.4(transitive)
+ Addedms@0.7.1(transitive)
+ Addedon-finished@2.2.1(transitive)
+ Addedrange-parser@1.0.3(transitive)
+ Addedsend@0.12.3(transitive)