connect-static
Advanced tools
Comparing version 1.3.3 to 1.4.0
58
index.js
@@ -33,12 +33,19 @@ var zlib = require('zlib'); | ||
var relName = '/' + path.relative(dir, usePath); | ||
var sink = new StreamSink(); | ||
var compressedSink = new StreamSink(); | ||
var uncompressedSink = new StreamSink(); | ||
var hashSink = new StreamSink(); | ||
var inStream = fs.createReadStream(file); | ||
var cacheObj; | ||
cache[relName] = cacheObj = { | ||
sink: sink, | ||
sink: null, | ||
mime: mime.lookup(relName), | ||
mtime: stat.mtime, | ||
hash: null, | ||
compressed: null, | ||
}; | ||
var gzipPendCb, hashPendCb; | ||
var fileDone = pend.hold(); | ||
var thisPend = new Pend(); | ||
var gzipPendCb = thisPend.hold(); | ||
var hashPendCb = thisPend.hold(); | ||
var uncompressedPendCb = thisPend.hold(); | ||
inStream.on('error', function(err) { | ||
@@ -48,2 +55,3 @@ if (err.code === 'EISDIR') { | ||
gzipPendCb(); | ||
uncompressedPendCb(); | ||
hashPendCb(); | ||
@@ -53,18 +61,28 @@ } else { | ||
gzipPendCb(err); | ||
uncompressedPendCb(err); | ||
hashPendCb(err); | ||
} | ||
}); | ||
pend.go(function(cb) { | ||
gzipPendCb = cb; | ||
inStream.pipe(zlib.createGzip()).pipe(sink); | ||
sink.on('finish', cb); | ||
inStream.pipe(zlib.createGzip()).pipe(compressedSink); | ||
compressedSink.on('finish', gzipPendCb); | ||
inStream.pipe(uncompressedSink); | ||
uncompressedSink.on('finish', uncompressedPendCb); | ||
inStream.pipe(crypto.createHash('sha1')).pipe(hashSink); | ||
hashSink.on('finish', function() { | ||
cacheObj.hash = hashSink.toString('base64'); | ||
hashPendCb(); | ||
}); | ||
pend.go(function(cb) { | ||
hashPendCb = cb; | ||
var hashSink = new StreamSink(); | ||
inStream.pipe(crypto.createHash('sha1')).pipe(hashSink); | ||
hashSink.on('finish', function() { | ||
cacheObj.hash = hashSink.toString('base64'); | ||
cb(); | ||
}); | ||
thisPend.wait(function(err) { | ||
if (err) return fileDone(err); | ||
var compressionRatio = compressedSink.length / uncompressedSink.length; | ||
if (compressionRatio >= 0.95) { | ||
// 95% of original size or worse. discard compressed sink | ||
cacheObj.sink = uncompressedSink; | ||
cacheObj.compressed = false; | ||
} else { | ||
// better than 95% of original size. discard uncompressed sink | ||
cacheObj.sink = compressedSink; | ||
cacheObj.compressed = true; | ||
} | ||
fileDone(); | ||
}); | ||
@@ -101,5 +119,11 @@ }); | ||
if (req.headers['accept-encoding'] == null) { | ||
sink.createReadStream().pipe(zlib.createGunzip()).pipe(resp); | ||
if (c.compressed) { | ||
sink.createReadStream().pipe(zlib.createGunzip()).pipe(resp); | ||
} else { | ||
sink.createReadStream().pipe(resp); | ||
} | ||
} else { | ||
resp.setHeader('Content-Encoding', 'gzip'); | ||
if (c.compressed) { | ||
resp.setHeader('Content-Encoding', 'gzip'); | ||
} | ||
sink.createReadStream().pipe(resp); | ||
@@ -106,0 +130,0 @@ } |
{ | ||
"name": "connect-static", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "static file server middleware for connect. loads files once at startup and saves gzipped versions in memory", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,3 +6,5 @@ # static caching gzipping file server middleware for connect | ||
will forever remain. When a request hits the middleware it never touches | ||
the file system. | ||
the file system. If gzipping a file results in >= 95% of the file size of | ||
the original file size, connect-static discards the gzipped data and instead | ||
serves the file directly. | ||
@@ -9,0 +11,0 @@ Are you looking for the middleware that used to ship with express and connect? |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9170
207
44