[Pkg-javascript-commits] [node-static] 11/151: (new) in-memory file caching

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 184c944750794d888328177cf9dc9bcb46ec0956
Author: cloudhead <self at cloudhead.net>
Date:   Mon Aug 2 14:56:50 2010 -0400

    (new) in-memory file caching
---
 lib/node-static.js | 58 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 42 insertions(+), 16 deletions(-)

diff --git a/lib/node-static.js b/lib/node-static.js
index bb1c59e..dd41c65 100644
--- a/lib/node-static.js
+++ b/lib/node-static.js
@@ -1,6 +1,7 @@
 var fs = require('fs'),
     sys = require('sys'),
     events = require('events'),
+    buffer = require('buffer'),
     http = require('http'),
     url = require('url'),
     path = require('path');
@@ -11,6 +12,9 @@ var mime = require('./node-static/mime');
 
 var serverInfo = 'node-static/' + this.version.join('.');
 
+// In-memory file store
+this.store = {};
+
 this.Server = function (root, options) {
     if (root && (typeof(root) === 'object')) { options = root, root = null }
 
@@ -59,7 +63,7 @@ this.Server.prototype.serveFile = function (file, req, res, callback) {
                 if (e || !stat.isFile()) {
                     finish(404, {});
                 } else {
-                    that.stream(file, stat, req, res, finish);
+                    that.respond(file, stat, req, res, finish);
                 }
             });
         } else {
@@ -123,7 +127,7 @@ this.Server.prototype.serve = function (req, res, callback) {
     return promise;
 };
 
-this.Server.prototype.stream = function (file, stat, req, res, finish) {
+this.Server.prototype.respond = function (file, stat, req, res, finish) {
     var mtime = Date.parse(stat.mtime),
         headers = {};
 
@@ -149,19 +153,41 @@ this.Server.prototype.stream = function (file, stat, req, res, finish) {
 
         res.writeHead(200, headers);
 
-        // Stream the file to the client
-        fs.createReadStream(file, {
-            flags: 'r',
-            encoding: 'binary',
-            mode: 0666,
-            bufferSize: 4096
-        }).addListener('data', function (chunk) {
-            res.write(chunk, 'binary');
-        }).addListener('close', function () {
-            res.end();
-            finish(200, headers);
-        }).addListener('error', function (err) {
-            sys.error(err);
-        });
+        if (this.cache && (file in exports.store)) {
+            res.end(exports.store[file].buffer);
+            finish(200, {});
+        } else {
+            this.stream(file, new(buffer.Buffer)(stat.size), res, function (e, buffer) {
+                if (e) { return finish(500, {}) }
+                exports.store[file] = {
+                    stat:      stat,
+                    buffer:    buffer,
+                    timestamp: Date.now()
+                };
+                finish(200, {});
+            });
+        }
     }
 };
+
+this.Server.prototype.stream = function (file, buffer, res, callback) {
+    var offset = 0;
+
+    // Stream the file to the client
+    fs.createReadStream(file, {
+        flags: 'r',
+        encoding: 'binary',
+        mode: 0666,
+        bufferSize: 4096
+    }).addListener('data', function (chunk) {
+        chunk.copy (buffer, offset, 0);
+        res.write  (chunk, 'binary');
+        offset   += chunk.length;
+    }).addListener('close', function () {
+        res.end();
+        callback(null, buffer, offset);
+    }).addListener('error', function (err) {
+        callback(err);
+        sys.error(err);
+    });
+};

-- 
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