[Pkg-javascript-commits] [node-static] 18/151: (new) support for directory streaming with concatination
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:17:56 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 9c3f819abb2b9cf467b74d727d5fb97df02e4b29
Author: cloudhead <self at cloudhead.net>
Date: Tue Aug 3 17:14:09 2010 -0400
(new) support for directory streaming with concatination
---
lib/node-static.js | 108 +++++++++++++++++++++++++++++++++++------------------
1 file changed, 71 insertions(+), 37 deletions(-)
diff --git a/lib/node-static.js b/lib/node-static.js
index bc38a85..d7230ba 100644
--- a/lib/node-static.js
+++ b/lib/node-static.js
@@ -15,6 +15,7 @@ var serverInfo = 'node-static/' + this.version.join('.');
// In-memory file store
this.store = {};
+this.indexStore = {};
this.Server = function (root, options) {
if (root && (typeof(root) === 'object')) { options = root, root = null }
@@ -45,7 +46,7 @@ this.Server = function (root, options) {
}
};
-this.Server.prototype.serveFile = function (file, req, res, callback) {
+this.Server.prototype.serveFile = function (pathname, req, res, callback) {
var that = this,
promise = new(events.EventEmitter);
@@ -55,16 +56,46 @@ this.Server.prototype.serveFile = function (file, req, res, callback) {
return finish(405, { 'Allow': 'GET, HEAD' });
}
- file = path.normalize(path.join(that.root, file));
+ pathname = path.normalize(path.join(that.root, pathname));
// Make sure we're not trying to access a
// file outside of the root.
- if (new(RegExp)('^' + that.root).test(file)) {
- fs.stat(file, function (e, stat) {
- if (e || !stat.isFile()) {
+ if (new(RegExp)('^' + that.root).test(pathname)) {
+ fs.stat(pathname, function (e, stat) {
+ var htmlIndex = path.join(pathname, 'index.html');
+
+ if (e) {
finish(404, {});
+ } else if (stat.isFile()) {
+ // Stream a single file.
+ that.respond(null, [pathname], stat, req, res, finish);
+
+ } else if (stat.isDirectory()) {
+ path.exists(htmlIndex, function (exists) {
+ if (exists) {
+ that.respond(null, [htmlIndex], stat, req, res, finish);
+ } else {
+ if (pathname in exports.store) {
+ util.mstat(pathname, exports.indexStore[pathname].files, function (e, stat) {
+ that.respond(pathname, exports.indexStore[pathname].files, stat, req, res, finish);
+ });
+ } 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;
+
+ util.mstat(pathname, index.files, function (e, stat) {
+ that.respond(pathname, index.files, stat, req, res, finish);
+ });
+ });
+ }
+ }
+ });
} else {
- that.respond(file, stat, req, res, finish);
+ finish(400, {});
}
});
} else {
@@ -113,10 +144,6 @@ this.Server.prototype.serve = function (req, res, callback) {
process.nextTick(function () {
var file = url.parse(req.url).pathname;
- // If we're trying to load a directory, look for
- // an index.html inside of it.
- if (/\/$/.test(file)) { file += 'index.html' }
-
that.serveFile(file, req, res, callback).addListener('success', function (result) {
promise.emit('success', result);
}).addListener('error', function (result) {
@@ -128,8 +155,9 @@ this.Server.prototype.serve = function (req, res, callback) {
return promise;
};
-this.Server.prototype.respond = function (file, stat, req, res, finish) {
- var mtime = Date.parse(stat.mtime),
+this.Server.prototype.respond = function (pathname, files, stat, req, res, finish) {
+ var mtime = Date.parse(stat.mtime),
+ key = pathname || files[0],
headers = {};
// Copy default headers
@@ -149,18 +177,18 @@ this.Server.prototype.respond = function (file, stat, req, res, finish) {
finish(200, headers);
} else {
headers['Content-Length'] = stat.size;
- headers['Content-Type'] = mime.contentTypes[path.extname(file).slice(1)] ||
+ headers['Content-Type'] = mime.contentTypes[path.extname(files[0]).slice(1)] ||
'application/octet-stream';
res.writeHead(200, headers);
- if (this.cache && (file in exports.store)) {
- res.end(exports.store[file].buffer);
+ if (this.cache && (key in exports.store)) {
+ res.end(exports.store[key].buffer);
finish(200, {});
} else {
- this.stream(file, new(buffer.Buffer)(stat.size), res, function (e, buffer) {
+ this.stream(pathname, files, new(buffer.Buffer)(stat.size), res, function (e, buffer) {
if (e) { return finish(500, {}) }
- exports.store[file] = {
+ exports.store[key] = {
stat: stat,
buffer: buffer,
timestamp: Date.now()
@@ -171,24 +199,30 @@ this.Server.prototype.respond = function (file, stat, req, res, finish) {
}
};
-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);
- });
+this.Server.prototype.stream = function (pathname, files, buffer, res, callback) {
+ (function streamFile(files, offset) {
+ var file = files.shift();
+
+ if (file) {
+ // Stream the file to the client
+ fs.createReadStream(path.join(pathname || '.', 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 () {
+ streamFile(files, offset);
+ }).addListener('error', function (err) {
+ callback(err);
+ sys.error(err);
+ });
+ } else {
+ res.end();
+ callback(null, buffer, offset);
+ }
+ })(files.slice(0), 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