connect-static
Advanced tools
Comparing version 1.2.3 to 1.3.0
58
index.js
@@ -7,6 +7,6 @@ var zlib = require('zlib'); | ||
var Pend = require('pend'); | ||
var findit = require('findit'); | ||
var findit = require('findit2'); | ||
var mime = require('mime'); | ||
var url = require('url'); | ||
var BufferList = require('bl'); | ||
var StreamSink = require('streamsink'); | ||
var crypto = require('crypto'); | ||
@@ -21,6 +21,7 @@ | ||
var aliases = options.aliases || [['/', '/index.html']]; | ||
var followSymlinks = (options.followSymlinks == null) ? true : !!options.followSymlinks; | ||
var cache = {}; | ||
var pend = new Pend(); | ||
var walker = findit(dir); | ||
var walker = findit(dir, {followSymlinks: followSymlinks}); | ||
walker.on('error', function(err) { | ||
@@ -30,31 +31,38 @@ walker.stop(); | ||
}); | ||
walker.on('file', function(file, stat) { | ||
if (ignoreFile(file)) return; | ||
var relName = '/' + path.relative(dir, file); | ||
var bl = new BufferList(); | ||
walker.on('file', function(file, stat, linkPath) { | ||
var usePath = linkPath || file; | ||
if (ignoreFile(usePath)) return; | ||
var relName = '/' + path.relative(dir, usePath); | ||
var sink = new StreamSink(); | ||
var inStream = fs.createReadStream(file); | ||
var cacheObj; | ||
cache[relName] = cacheObj = { | ||
sink: sink, | ||
mime: mime.lookup(relName), | ||
mtime: stat.mtime, | ||
hash: null, | ||
}; | ||
var gzipPendCb, hashPendCb; | ||
inStream.on('error', function(err) { | ||
if (err.code === 'EISDIR') { | ||
delete cache[relName]; | ||
return; | ||
gzipPendCb(); | ||
hashPendCb(); | ||
} else { | ||
throw err; | ||
walker.stop(); | ||
gzipPendCb(err); | ||
hashPendCb(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); | ||
gzipPendCb = cb; | ||
inStream.pipe(zlib.createGzip()).pipe(sink); | ||
sink.on('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'); | ||
hashPendCb = cb; | ||
var hashSink = new StreamSink(); | ||
inStream.pipe(crypto.createHash('sha1')).pipe(hashSink); | ||
hashSink.on('finish', function() { | ||
cacheObj.hash = hashSink.toString('base64'); | ||
cb(); | ||
@@ -88,10 +96,10 @@ }); | ||
var bl = c.bl; | ||
var sink = c.sink; | ||
resp.setHeader('Content-Type', c.mime); | ||
resp.setHeader('ETag', c.hash); | ||
if (req.headers['accept-encoding'] == null) { | ||
bl.duplicate().pipe(zlib.createGunzip()).pipe(resp); | ||
sink.createReadStream().pipe(zlib.createGunzip()).pipe(resp); | ||
} else { | ||
resp.setHeader('Content-Encoding', 'gzip'); | ||
bl.duplicate().pipe(resp); | ||
sink.createReadStream().pipe(resp); | ||
} | ||
@@ -98,0 +106,0 @@ } |
{ | ||
"name": "connect-static", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "static file server middleware for connect. loads files once at startup and saves gzipped versions in memory", | ||
@@ -20,7 +20,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"bl": "~0.9.3", | ||
"findit": "~2.0.0", | ||
"findit2": "~2.2.0", | ||
"mime": "~1.2.11", | ||
"pend": "~1.1.3" | ||
"pend": "~1.1.3", | ||
"streamsink": "~1.2.0" | ||
} | ||
} |
@@ -35,2 +35,3 @@ # static caching gzipping file server middleware for connect | ||
}, | ||
followSymlinks: true, | ||
}; | ||
@@ -37,0 +38,0 @@ createStatic(options, function(err, middleware) { |
var createStatic = require('..'); | ||
var path = require('path'); | ||
var assert = require('assert'); | ||
var BufferList = require('bl'); | ||
var StreamSink = require('streamsink'); | ||
@@ -10,14 +10,78 @@ var dir = path.join(__dirname, "public"); | ||
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"); | ||
var tests = [ | ||
{ | ||
name: "basic", | ||
fn: testFooTxt, | ||
}, | ||
{ | ||
name: "symlink-dir", | ||
fn: testSymlinkDir, | ||
}, | ||
{ | ||
name: "symlink", | ||
fn: testSymlink, | ||
}, | ||
]; | ||
testOne(); | ||
function testOne() { | ||
var test = tests.shift(); | ||
if (!test) { | ||
process.stdout.write("done\n"); | ||
return; | ||
} | ||
process.stdout.write("testing " + test.name + "..."); | ||
test.fn(function(err) { | ||
if (err) throw err; | ||
process.stdout.write("OK\n"); | ||
testOne(); | ||
}); | ||
bl.setHeader = function(name, val) {}; | ||
} | ||
function testSymlink(cb) { | ||
var sink = new StreamSink(); | ||
sink.on('finish', function() { | ||
assert.strictEqual(sink.toString(), "aoeu1234\n") | ||
cb(); | ||
}); | ||
sink.setHeader = function(name, val) {}; | ||
middleware({ | ||
url: '/foo.txt', | ||
url: '/bar.txt', | ||
headers: {}, | ||
}, bl, assert.fail) | ||
}); | ||
}, sink, function(err) { | ||
throw new Error("unexpected call: " + err); | ||
}); | ||
} | ||
function testSymlinkDir(cb) { | ||
var sink = new StreamSink(); | ||
sink.on('finish', function() { | ||
assert.strictEqual(sink.toString(), "zzzz\n") | ||
cb(); | ||
}); | ||
sink.setHeader = function(name, val) {}; | ||
middleware({ | ||
url: '/dir/blah.txt', | ||
headers: {}, | ||
}, sink, function(err) { | ||
throw new Error("unexpected call: " + err); | ||
}) | ||
} | ||
function testFooTxt(cb) { | ||
middleware({url: '/unrelated'}, null, function() { | ||
var sink = new StreamSink(); | ||
sink.on('finish', function() { | ||
assert.strictEqual(sink.toString(), "hi\n") | ||
cb(); | ||
}); | ||
sink.setHeader = function(name, val) {}; | ||
middleware({ | ||
url: '/foo.txt', | ||
headers: {}, | ||
}, sink, assert.fail) | ||
}); | ||
} | ||
}); |
7979
9
182
42
+ Addedfindit2@~2.2.0
+ Addedstreamsink@~1.2.0
+ Addedfindit2@2.2.3(transitive)
+ Addedstreamsink@1.2.0(transitive)
- Removedbl@~0.9.3
- Removedfindit@~2.0.0
- Removedbl@0.9.5(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedfindit@2.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedstring_decoder@0.10.31(transitive)