Skip to content

Commit

Permalink
Add now stores the request details and has returns the details for st…
Browse files Browse the repository at this point in the history
…atic content
  • Loading branch information
3rd-Eden committed Jul 15, 2011
1 parent 9e97e6c commit c2d98bd
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,27 @@

var client = require('socket.io-client')
, cp = require('child_process')
, fs = require('fs');
, fs = require('fs')
, util = require('./util');


/**
* File type details
*/

var mime = {
js: {
type: 'application/javascript'
, encoding: 'utf8'
, gzip: true
}
, swf: {
type: 'application/x-shockwave-flash'
, encoding: 'binary'
, gzip: false
}
};


/**
* Export the constructor
Expand All @@ -25,7 +45,8 @@ exports = module.exports = Static;
* @api public
*/

function Static () {
function Static (manager) {
this.manager = manager;
this.cache = {};
this.paths = {};
this.etag = client.version;
Expand All @@ -49,7 +70,7 @@ Static.prototype.gzip = function (data, callback) {
});

gzip.stderr.on('data', function (data) {
err = data +'';
err = data +'';Ma
buffer.length = 0;
});

Expand Down Expand Up @@ -91,7 +112,17 @@ Static.prototype.gzip = function (data, callback) {
*/

Static.prototype.has = function (path) {
// fast case
if (this.paths[path]) return this.paths[path];

var keys = Object.keys(this.paths)
, i = keys.length;

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

return false;
};

/**
Expand All @@ -105,7 +136,15 @@ Static.prototype.has = function (path) {
*/

Static.prototype.add = function (path, options, callback) {
var extension = /(?:\.(\w{1,4}))$/.exec(path)
, mime = options.mime || (extension ? mime[extension[1]] : false);

if (callback) options.callback = callback;
if (!options.file || !options.callback) return false;

this.paths[path] = options;

return true;
};

/**
Expand Down Expand Up @@ -158,7 +197,7 @@ Static.prototype.write = function (path, req, res) {
}

// check if we can send gzip data
if (gzip && reply.gzip) {
if (gzip && mime.gzip) {
headers['Content-Length'] = reply.gzip.length;
headers['Content-Encoding'] = 'gzip';
write(200, headers, reply.gzip.content, mime.encoding);
Expand Down

0 comments on commit c2d98bd

Please sign in to comment.