[Pkg-javascript-commits] [node-static] 04/151: Various improvements & additions

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:17:55 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository node-static.

commit e83c99cf3bf45a81915fae4dd52c6a5c533f4fd1
Author: cloudhead <self at cloudhead.net>
Date:   Tue Jul 27 14:57:07 2010 -0400

    Various improvements & additions
    
    - Event Listener support
    - Custom headers
    - Fixed cache when 0
    - node-style callbacks
---
 lib/node-static.js | 103 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 77 insertions(+), 26 deletions(-)

diff --git a/lib/node-static.js b/lib/node-static.js
index cedc7d0..f6a7949 100644
--- a/lib/node-static.js
+++ b/lib/node-static.js
@@ -1,24 +1,55 @@
 var fs = require('fs'),
     sys = require('sys'),
+    events = require('events'),
+    http = require('http'),
     url = require('url'),
     path = require('path');
 
+this.version = [0, 1, 0];
+
 var mime = require('./node-static/mime');
 
+var serverInfo = 'node-static/' + this.version.join('.');
+
 this.Server = function (root, options) {
+    if (root && (typeof(root) === 'object')) { options = root, root = null }
+
     this.root    = path.normalize(root || '.');
     this.options = options || {};
+    this.cache   = 3600;
+
+    this.defaultHeaders  = {};
+    this.options.headers = this.options.headers || {};
+
+    if ('cache' in this.options) {
+        if (typeof(this.options.cache) === 'number') {
+            this.cache = this.options.cache;
+        } else if (! this.options.cache) {
+            this.cache = false;
+        }
+    }
+
+    if (this.cache !== false) {
+        this.defaultHeaders['cache-control'] = 'max-age=' + this.cache;
+    }
+    this.defaultHeaders['Server'] = serverInfo;
+
+    for (var k in this.defaultHeaders) {
+        this.options.headers[k] = this.options.headers[k] ||
+                                  this.defaultHeaders[k];
+    }
 };
 
-this.Server.prototype.serve = function (req, res, errback) {
-    var that = this;
+this.Server.prototype.serve = function (req, res, callback) {
+    var that = this,
+        promise = new(events.EventEmitter);
 
     process.nextTick(function () {
         var file = url.parse(req.url).pathname;
 
         // Only allow GET and HEAD requests
         if (req.method !== 'GET' && req.method !== 'HEAD') {
-            error(405, { 'Allow': 'GET, HEAD' });
+            return finish(405, { 'Allow': 'GET, HEAD' });
         }
 
         // If we're trying to load a directory, look for
@@ -32,47 +63,68 @@ this.Server.prototype.serve = function (req, res, errback) {
         if (new(RegExp)('^' + that.root).test(file)) {
             fs.stat(file, function (e, stat) {
                 if (e || !stat.isFile()) {
-                    error(404, {});
+                    finish(404, {});
                 } else {
-                    that.stream(file, stat, req, res);
+                    that.stream(file, stat, req, res, finish);
                 }
             });
         } else {
             // Forbidden
-            error(403, {});
+            finish(403, {});
         }
     });
 
-    function error(status, headers) {
-        if (typeof(errback) === 'function') {
-            errback(status, headers);
+    return promise;
+
+    function finish(status, headers) {
+        var result = {
+            status:  status,
+            headers: headers,
+            message: http.STATUS_CODES[status]
+        };
+
+        headers['Server'] = serverInfo;
+
+        if (status >= 400) {
+            if (callback) {
+                callback(result);
+            } else {
+                res.writeHead(status, headers);
+                res.end();
+            }
+            promise.emit('error', result);
         } else {
-            res.writeHead(status, headers);
-            res.end();
+            // Don't end the request here, if we're streaming;
+            // it's taken care of in `prototype.stream`.
+            if (status !== 200 || req.method !== 'GET') {
+                res.writeHead(status, headers);
+                res.end();
+            }
+            callback && callback(null, result);
+            promise.emit('success', result);
         }
     }
 };
 
-this.Server.prototype.stream = function (file, stat, req, res) {
-    var mtime = Date.parse(stat.mtime);
-    var headers = {
-        'Etag':           JSON.stringify([stat.ino, stat.size, mtime].join('-')),
-        'Date':           new(Date)().toUTCString(),
-        'Server':        'node-static/' + exports.version.join('.'),
-        'cache-control': 'max-age=' + this.options.cache || 3600,
-        'last-modified':  new(Date)(stat.mtime).toUTCString()
-    };
+this.Server.prototype.stream = function (file, stat, req, res, finish) {
+    var mtime = Date.parse(stat.mtime),
+        headers = {};
+
+    // Copy default headers
+    for (var k in this.options.headers) { headers[k] = this.options.headers[k] }
+
+    headers['Etag']          = JSON.stringify([stat.ino, stat.size, mtime].join('-'));
+    headers['Date']          = new(Date)().toUTCString();
+    headers['last-modified'] = new(Date)(stat.mtime).toUTCString();
 
     // Conditional GET
     // If both the "If-Modified-Since" and "If-None-Match" headers
     // match the conditions, send a 304 Not Modified.
     if (req.headers['if-none-match'] === headers['Etag'] &&
         Date.parse(req.headers['if-modified-since']) > mtime) {
-        res.writeHead(304, headers);
-        res.end();
+        finish(304, headers);
     } else if (req.method === 'HEAD') {
-        res.writeHead(200, headers);
-        res.end();
+        finish(200, headers);
     } else {
         headers['Content-Length'] = stat.size;
         headers['Content-Type']   = mime.contentTypes[path.extname(file).slice(1)] ||
@@ -90,10 +142,9 @@ this.Server.prototype.stream = function (file, stat, req, res) {
             res.write(chunk, 'binary');
         }).addListener('close', function () {
             res.end();
+            finish(200, headers);
         }).addListener('error', function (err) {
             sys.error(err);
         });
     }
 };
-
-this.version = [0, 1, 0];

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-static.git



More information about the Pkg-javascript-commits mailing list