Skip to content

Commit

Permalink
Added more hardcore caching fixes socketio#558
Browse files Browse the repository at this point in the history
Added tests against it
Added vary header for gzip
  • Loading branch information
3rd-Eden committed Oct 10, 2011
1 parent 8107c1a commit 763fdd1
Show file tree
Hide file tree
Showing 3 changed files with 1,993 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var mime = {
* @api private
*/

var bundle = /\+((?:\+)?[\w\-]+)*(?:\.v\d+\.\d+\.\d+)?(?:\.js)$/g
var bundle = /\+((?:\+)?[\w\-]+)*(?:\.v\d+\.\d+\.\d+)?(?:\.js)$/
, versioning = /\.v\d+\.\d+\.\d+(?:\.js)$/;

/**
Expand Down Expand Up @@ -121,10 +121,14 @@ Static.prototype.init = function () {
build(self.manager.get('transports'), callback);
});

this.add('/socket.io.v', { mime: mime.js }, function (path, callback) {
build(self.manager.get('transports'), callback);
});

// allow custom builds based on url paths
this.add('/socket.io+', { mime: mime.js }, function (path, callback) {
var available = self.manager.get('transports')
, matches = bundle.exec(path)
, matches = path.match(bundle)
, transports = [];

if (!matches) return callback('No valid transports');
Expand Down Expand Up @@ -218,7 +222,7 @@ Static.prototype.has = function (path) {
, i = keys.length;

while (i--) {
if (!!~path.indexOf(keys[i])) return this.paths[keys[i]];
if (-~path.indexOf(keys[i])) return this.paths[keys[i]];
}

return false;
Expand Down Expand Up @@ -310,15 +314,16 @@ Static.prototype.write = function (path, req, res) {

// see if we need to set Expire headers because the path is versioned
if (versioned) {
var expires = self.manager.get('browser client expires')
headers['Cache-Control'] = 'public, max-age=' + expires;
var expires = self.manager.get('browser client expires');
headers['Cache-Control'] = 'private, x-gzip-ok="", max-age=' + expires;
headers['Date'] = new Date().toUTCString();
headers['Expires'] = new Date(Date.now() + (expires * 1000)).toUTCString();
}

if (gzip && reply.gzip) {
headers['Content-Length'] = reply.gzip.length;
headers['Content-Encoding'] = 'gzip';
headers['Vary'] = 'Accept-Encoding';
write(200, headers, reply.gzip.content, mime.encoding);
} else {
headers['Content-Length'] = reply.length;
Expand Down
49 changes: 48 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module.exports = {
, io = sio.listen(port);

(!!io.static.has('/socket.io.js')).should.be.true;
(!!io.static.has('/socket.io+')).should.be.true;
(!!io.static.has('/socket.io.v1.0.0.js')).should.be.true;
(!!io.static.has('/socket.io+xhr-polling.js')).should.be.true;
(!!io.static.has('/socket.io+xhr-polling.v1.0.0.js')).should.be.true;
(!!io.static.has('/static/flashsocket/WebSocketMain.swf')).should.be.true;
(!!io.static.has('/static/flashsocket/WebSocketMainInsecure.swf')).should.be.true;

Expand Down Expand Up @@ -472,5 +474,50 @@ module.exports = {
io.server.close()
done();
});
},

'test that a versioned client is served': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

cl.get('/socket.io/socket.io.v0.8.9.js', function (res, data) {
res.headers['content-type'].should.eql('application/javascript');
res.headers['content-length'].should.match(/([0-9]+)/);
res.headers['cache-control']
.indexOf(io.get('browser client expires')).should.be.above(-1);

data.should.match(/XMLHttpRequest/);

cl.end();
io.server.close();
done();
});
},

'test that a custom versioned build client is served': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

io.set('browser client expires', 1337);

cl.get('/socket.io/socket.io+websocket.v0.8.10.js', function (res, data) {
res.headers['content-type'].should.eql('application/javascript');
res.headers['content-length'].should.match(/([0-9]+)/);
res.headers['cache-control']
.indexOf(io.get('browser client expires')).should.be.above(-1);

data.should.match(/XMLHttpRequest/);
data.should.match(/WS\.prototype\.name/);
data.should.not.match(/Flashsocket\.prototype\.name/);
data.should.not.match(/HTMLFile\.prototype\.name/);
data.should.not.match(/JSONPPolling\.prototype\.name/);
data.should.not.match(/XHRPolling\.prototype\.name/);

cl.end();
io.server.close();
done();
});
}
};
Loading

0 comments on commit 763fdd1

Please sign in to comment.