Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributable cache for files #496

Merged
merged 2 commits into from
Feb 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/response/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ var Mime = require('mime');
var Err = require('../error');
var Utils = require('../utils');
var Stream = require('./stream');
var LRU = require('lru-cache');
var Crypto = require('crypto');


// Declare internals

var internals = {};

internals.fileEtags = LRU();

// File response (Base -> Generic -> Stream -> File)

Expand Down Expand Up @@ -49,8 +51,23 @@ internals.File.prototype._prepare = function (request, callback) {
Stream.call(self, stream);
self._headers['Content-Type'] = Mime.lookup(self._filePath) || 'application/octet-stream';
self._headers['Content-Length'] = stat.size;
self._headers['Last-Modified'] = new Date(stat.mtime).toUTCString();
self._headers.etag = JSON.stringify([stat.ino, stat.size, Date.parse(stat.mtime)].join('-'));

// Use stat info for an LRU cache key.
var cachekey = [self._filePath, stat.ino, stat.size, Date.parse(stat.mtime)].join('-');

// The etag must hash the file contents in order to be consistent between nodes.
if (internals.fileEtags.has(cachekey)) {
self._headers.etag = JSON.stringify(internals.fileEtags.get(cachekey));
} else {
var hash = Crypto.createHash('md5');
stream.on('data', function (chunk) {
hash.update(chunk);
})
stream.on('end', function () {
internals.fileEtags.set(cachekey, hash.digest("hex"));
})
}

self._headers['Content-Disposition'] = 'inline; filename=' + encodeURIComponent(fileName);

return Stream.prototype._prepare.call(self, request, callback);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"catbox": "0.1.x",
"cryptiles": "0.0.x",
"iron": "0.1.x",
"semver": "1.1.0"
"semver": "1.1.0",
"lru-cache": "~2.2.2"
},
"devDependencies": {
"mocha": "1.x.x",
Expand Down
17 changes: 0 additions & 17 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,23 +546,6 @@ describe('Response', function () {
});
});

it('returns a 304 when the request has a future modifed-since', function (done) {

server.start(function () {

var date = new Date(Date.now());
var headers = {
'if-modified-since': new Date(date.setFullYear(date.getFullYear() + 1)).toUTCString()
};

Request.get({ url: 'http://localhost:17082/file', headers: headers }, function (err, res) {

expect(res.statusCode).to.equal(304);
done();
});
});
});

it('returns a gzipped file in the response when the request accepts gzip', function (done) {

server.start(function () {
Expand Down