[Pkg-javascript-commits] [node-connect-static] 01/02: Imported Upstream version 1.2.0

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Mon Jun 30 05:31:51 UTC 2014


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

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

commit d5c85a2315e1f6a14b33c8a17e78679bb29eac92
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Mon Jun 30 05:18:54 2014 +0000

    Imported Upstream version 1.2.0
---
 .gitignore          |  1 +
 README.md           | 39 +++++++++++++++++++++
 index.js            | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json        | 25 ++++++++++++++
 test/public/foo.txt |  1 +
 test/test.js        | 22 ++++++++++++
 6 files changed, 185 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..07e6e47
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/node_modules
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..22f3801
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# static caching gzipping file server middleware for connect
+
+When you create the middleware, it will immediately scan the requested
+directory, gzip all the files, and save the cache into memory, where it
+will forever remain. When a request hits the middleware it never touches
+the file system.
+
+Are you looking for the middleware that used to ship with express and connect?
+That project is called [serve-static](https://github.com/expressjs/serve-static)
+
+## Supported HTTP Headers
+
+ * `ETag`
+ * `If-None-Match`
+ * `If-Modified-Since`
+ * `Accept-Encoding`
+ * `Content-Encoding`
+
+## Usage
+
+```js
+var createStatic = require('connect-static');
+
+// These are all defaults. If you leave any options out, this is what they
+// will be.
+var options = {
+  dir: "public",
+  aliases: [
+    ['/', '/index.html'],
+  ],
+  ignoreFile: function(fullPath) {
+    var basename = path.basename(fullPath);
+    return /^\./.test(basename) || /~$/.test(basename);
+  },
+};
+createStatic(options, function(err, middleware) {
+  app.use('/', middleware);
+});
+```
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..928d5dc
--- /dev/null
+++ b/index.js
@@ -0,0 +1,97 @@
+var zlib = require('zlib');
+var fs = require('fs');
+var stream = require('stream');
+var util = require('util');
+var path = require('path');
+var Pend = require('pend');
+var findit = require('findit');
+var mime = require('mime');
+var url = require('url');
+var BufferList = require('bl');
+var crypto = require('crypto');
+
+module.exports = createGzipStaticMiddleware;
+
+function createGzipStaticMiddleware(options, cb) {
+  options = options || {};
+  var dir = options.dir || "public";
+  var ignoreFile = options.ignoreFile || defaultIgnoreFile;
+  var aliases = options.aliases || [['/', '/index.html']];
+
+  var cache = {};
+  var pend = new Pend();
+  var walker = findit(dir);
+  walker.on('file', function(file, stat) {
+    if (ignoreFile(file)) return;
+    var relName = '/' + path.relative(dir, file);
+    var bl = new BufferList();
+    var inStream = fs.createReadStream(file);
+    inStream.on('error', function(err) {
+      if (err.code === 'EISDIR') {
+        delete cache[relName];
+        return;
+      } else {
+        throw err;
+      }
+    });
+    var cacheObj;
+    cache[relName] = cacheObj = {
+      bl: bl,
+      mime: mime.lookup(relName),
+      mtime: stat.mtime,
+      hash: null,
+    };
+    pend.go(function(cb) {
+      inStream.pipe(zlib.createGzip()).pipe(bl);
+      bl.once('finish', cb);
+    });
+    pend.go(function(cb) {
+      var hashBl = new BufferList();
+      inStream.pipe(crypto.createHash('sha1')).pipe(hashBl);
+      hashBl.once('finish', function() {
+        cacheObj.hash = hashBl.toString('base64');
+        cb();
+      });
+    });
+  });
+  walker.on('end', function() {
+    pend.wait(function(err) {
+      if (err) return cb(err);
+      aliases.forEach(function(alias) {
+        cache[alias[0]] = cache[alias[1]];
+      });
+      cb(null, middleware);
+    });
+    function middleware(req, resp, next) {
+      var parsedUrl = url.parse(req.url);
+      var c = cache[parsedUrl.pathname];
+      if (!c) return next();
+      if (req.headers['if-none-match'] === c.hash) {
+        resp.statusCode = 304;
+        resp.end();
+        return;
+      }
+      var ifModifiedSince = new Date(req.headers['if-modified-since']);
+      if (!isNaN(ifModifiedSince) && c.mtime <= ifModifiedSince) {
+        resp.statusCode = 304;
+        resp.end();
+        return;
+      }
+
+      var bl = c.bl;
+      resp.setHeader('Content-Type', c.mime);
+      resp.setHeader('ETag', c.hash);
+      if (req.headers['accept-encoding'] == null) {
+        bl.duplicate().pipe(zlib.createGunzip()).pipe(resp);
+      } else {
+        resp.setHeader('Content-Encoding', 'gzip');
+        bl.duplicate().pipe(resp);
+      }
+    }
+  });
+}
+
+function defaultIgnoreFile(file) {
+  var basename = path.basename(file);
+  return /^\./.test(basename) || /~$/.test(basename);
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..12b6a8e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,25 @@
+{
+  "name": "connect-static",
+  "version": "1.2.0",
+  "description": "static file server middleware for connect. loads files once at startup and saves gzipped versions in memory",
+  "main": "index.js",
+  "scripts": {
+    "test": "node test/test.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/andrewrk/connect-static.git"
+  },
+  "author": "Andrew Kelley <superjoe30 at gmail.com>",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/andrewrk/connect-static/issues"
+  },
+  "homepage": "https://github.com/andrewrk/connect-static",
+  "dependencies": {
+    "bl": "~0.8.2",
+    "findit": "~1.1.1",
+    "mime": "~1.2.11",
+    "pend": "~1.1.1"
+  }
+}
diff --git a/test/public/foo.txt b/test/public/foo.txt
new file mode 100644
index 0000000..45b983b
--- /dev/null
+++ b/test/public/foo.txt
@@ -0,0 +1 @@
+hi
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..a5e08ef
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,22 @@
+var createStatic = require('..');
+var path = require('path');
+var assert = require('assert');
+var BufferList = require('bl');
+
+var dir = path.join(__dirname, "public");
+
+createStatic({dir: dir}, function(err, middleware) {
+  if (err) throw err;
+  middleware({url: '/unrelated'}, null, function() {
+    var bl = new BufferList();
+    bl.on('finish', function() {
+      assert.strictEqual(bl._bufs[0].toString(), "hi\n")
+      console.log("OK");
+    });
+    bl.setHeader = function(name, val) {};
+    middleware({
+      url: '/foo.txt',
+      headers: {},
+    }, bl, assert.fail)
+  });
+});

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



More information about the Pkg-javascript-commits mailing list