Socket
Socket
Sign inDemoInstall

bomb

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bomb - npm Package Compare versions

Comparing version 1.2.4 to 1.3.0

examples/express/public/dontsend.txt

1

examples/express/index.js

@@ -11,2 +11,3 @@ var Express = require('express');

regex: /\.(css|js|html)/
sendOptions: {}
});

@@ -13,0 +14,0 @@

61

lib/box.js

@@ -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'`
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc