Permalink
Browse files

Cleaned up exports in main file. Corrected package version number. Ad…

…ded decodeURI test back in.
  • Loading branch information...
1 parent 5c8a24f commit a2a3371e15ccb1a865daed8df1253de1e096ae5c @dubiousdavid dubiousdavid committed Apr 1, 2013
Showing with 57 additions and 37 deletions.
  1. +39 −23 lib/node-static.js
  2. +1 −1 package.json
  3. +17 −13 test/integration/node-static-test.js
View
@@ -4,16 +4,15 @@ var fs = require('fs')
, http = require('http')
, url = require('url')
, path = require('path')
- , mime = require('mime');
+ , mime = require('mime')
+ , util = require('./node-static/util');
-exports.version = [0, 6, 7];
-
-var util = require('./node-static/util');
+// Current version
+var version = [0, 6, 7];
// In-memory file store
-exports.store = {};
-exports.indexStore = {};
-exports.mime = mime;
+var store = {};
+var indexStore = {};
Server = function (root, options) {
if (root && (typeof(root) === 'object')) { options = root; root = null }
@@ -36,7 +35,7 @@ Server = function (root, options) {
if ('serverInfo' in this.options) {
this.serverInfo = this.options.serverInfo.toString();
} else {
- this.serverInfo = 'node-static/' + exports.version.join('.');
+ this.serverInfo = 'node-static/' + version.join('.');
}
this.defaultHeaders['server'] = this.serverInfo;
@@ -59,14 +58,14 @@ Server.prototype.serveDir = function (pathname, req, res, finish) {
if (!e) {
that.respond(null, 200, {}, [htmlIndex], stat, req, res, finish);
} else {
- if (pathname in exports.indexStore) {
- streamFiles(exports.indexStore[pathname].files);
+ if (pathname in indexStore) {
+ streamFiles(indexStore[pathname].files);
} else {
// Stream a directory of files as a single file.
fs.readFile(path.join(pathname, 'index.json'), function (e, contents) {
if (e) { return finish(404, {}) }
var index = JSON.parse(contents);
- exports.indexStore[pathname] = index;
+ indexStore[pathname] = index;
streamFiles(index.files);
});
}
@@ -168,14 +167,22 @@ Server.prototype.resolve = function (pathname) {
};
Server.prototype.serve = function (req, res, callback) {
- var that = this,
- promise = new(events.EventEmitter);
+ var that = this,
+ promise = new(events.EventEmitter),
+ pathname;
- var pathname = decodeURI(url.parse(req.url).pathname);
-
var finish = function (status, headers) {
that.finish(status, headers, req, res, promise, callback);
};
+
+ try {
+ pathname = decodeURI(url.parse(req.url).pathname);
+ }
+ catch(e) {
+ return process.nextTick(function() {
+ return finish(400, {});
+ });
+ }
process.nextTick(function () {
that.servePath(pathname, 200, {}, req, res, finish).on('success', function (result) {
@@ -188,9 +195,9 @@ Server.prototype.serve = function (req, res, callback) {
};
Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
- var mtime = Date.parse(stat.mtime),
- key = pathname || files[0],
- headers = {},
+ var mtime = Date.parse(stat.mtime),
+ key = pathname || files[0],
+ headers = {},
clientETag = req.headers['if-none-match'],
clientMTime = Date.parse(req.headers['if-modified-since']);
@@ -224,14 +231,14 @@ Server.prototype.respond = function (pathname, status, _headers, files, stat, re
// If the file was cached and it's not older
// than what's on disk, serve the cached version.
- if (this.cache && (key in exports.store) &&
- exports.store[key].stat.mtime >= stat.mtime) {
- res.end(exports.store[key].buffer);
+ if (this.cache && (key in store) &&
+ store[key].stat.mtime >= stat.mtime) {
+ res.end(store[key].buffer);
finish(status, headers);
} else {
this.stream(pathname, files, new(buffer.Buffer)(stat.size), res, function (e, buffer) {
if (e) { return finish(500, {}) }
- exports.store[key] = {
+ store[key] = {
stat: stat,
buffer: buffer,
timestamp: Date.now()
@@ -269,4 +276,13 @@ Server.prototype.stream = function (pathname, files, buffer, res, callback) {
})(files.slice(0), 0);
};
-exports.Server = Server;
+// Exports
+exports.Server = Server;
+exports.version = version;
+exports.mime = mime;
+exports.store = store;
+exports.indexStore = indexStore;
+
+
+
+
View
@@ -31,7 +31,7 @@
"request": "latest",
"vows": "latest"
},
- "version" : "0.6.5",
+ "version" : "0.6.7",
"engines" : { "node": ">= 0.4.1" }
}
@@ -7,10 +7,22 @@ var fileServer = new(static.Server)(__dirname + '/../fixtures', {serverInfo: 'cu
var suite = vows.describe('node-static');
-var TEST_PORT = 8080;
+var TEST_PORT = 8080;
var TEST_SERVER = 'http://localhost:' + TEST_PORT;
var server;
var callback;
+var version = static.version.join('.');
+
+headers = {
+ 'requesting headers': {
+ topic : function(){
+ request.head(TEST_SERVER + '/index.html', this.callback);
+ }
+ }
+}
+headers['requesting headers']['should respond with node-static/' + version] = function(error, response, body){
+ assert.equal(response.headers['server'], 'node-static/' + version);
+}
suite.addBatch({
'once an http server is listening with a callback': {
@@ -75,7 +87,7 @@ suite.addBatch({
}
}
})
-/*.addBatch({
+.addBatch({
'requesting a malformed URI': {
topic: function(){
request.get(TEST_SERVER + '/a%AFc', this.callback);
@@ -85,7 +97,6 @@ suite.addBatch({
}
}
})
-*/
.addBatch({
'serving hello.txt': {
topic : function(){
@@ -176,16 +187,9 @@ suite.addBatch({
assert.isUndefined(body);
}
}
-}).addBatch({
- 'requesting headers': {
- topic : function(){
- request.head(TEST_SERVER + '/index.html', this.callback);
- },
- 'should respond with node-static/0.6.7' : function(error, response, body){
- assert.equal(response.headers['server'], 'node-static/0.6.7');
- }
- }
-}).addBatch({
+})
+.addBatch(headers)
+.addBatch({
'addings custom mime types': {
topic : function(){
static.mime.define({'application/font-woff': ['woff']});

0 comments on commit a2a3371

Please sign in to comment.