koa-static-cache
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -0,1 +1,8 @@ | ||
1.0.7 / 2014-03-26 | ||
================== | ||
* add options.gzip to control gzip, support stream's gzip | ||
* add gzip support for buffers | ||
1.0.3 / 2014-01-14 | ||
@@ -9,2 +16,2 @@ ================== | ||
* use `yield* next` | ||
* use `yield* next` |
72
index.js
var crypto = require('crypto') | ||
var fs = require('fs') | ||
var zlib = require('zlib') | ||
var path = require('path') | ||
@@ -9,2 +10,14 @@ var mime = require('mime') | ||
var stat = function (file) { | ||
return function (done) { | ||
fs.stat(file, done) | ||
} | ||
} | ||
function gzip(buf) { | ||
return function (done) { | ||
zlib.gzip(buf, done) | ||
} | ||
} | ||
module.exports = function staticCache(dir, options, files) { | ||
@@ -16,2 +29,3 @@ if (typeof dir !== 'string') | ||
files = files || options.files || Object.create(null) | ||
var enableGzip = !!options.gzip | ||
@@ -34,3 +48,2 @@ readDir(dir).forEach(function (name) { | ||
debug('file: ' + JSON.stringify(obj, null, 2)) | ||
if (options.buffer) | ||
@@ -63,4 +76,15 @@ obj.buffer = buffer | ||
this.status = 200 | ||
if (!file.buffer) { | ||
var stats = yield stat(file.path) | ||
if (stats.mtime > new Date(file.mtime)) { | ||
file.mtime = stats.mtime.toUTCString() | ||
file.md5 = null | ||
file.length = stats.size | ||
} | ||
} | ||
this.response.lastModified = file.mtime | ||
this.response.etag = file.md5 | ||
if (file.md5) this.response.etag = file.md5 | ||
if (this.fresh) | ||
@@ -70,5 +94,5 @@ return this.status = 304 | ||
this.type = file.type | ||
this.length = file.length | ||
this.length = file.zipBuffer ? file.zipBuffer.length : file.length | ||
this.set('Cache-Control', file.cacheControl || 'public, max-age=' + file.maxAge) | ||
this.set('Content-MD5', file.md5) | ||
if (file.md5) this.set('Content-MD5', file.md5) | ||
@@ -78,10 +102,40 @@ if (this.method === 'HEAD') | ||
if (file.zipBuffer) { | ||
this.set('Content-Encoding', 'gzip') | ||
this.body = file.zipBuffer | ||
return | ||
} | ||
if (file.buffer) { | ||
this.body = file.buffer | ||
if (enableGzip && this.acceptsEncodings('gzip') === 'gzip') { | ||
file.zipBuffer = yield gzip(file.buffer) | ||
this.set('Content-Encoding', 'gzip') | ||
this.body = file.zipBuffer | ||
} else { | ||
this.body = file.buffer | ||
} | ||
return | ||
} | ||
var stream = fs.createReadStream(file.path) | ||
stream.on('error', this.onerror) | ||
// update file hash | ||
if (!file.md5) { | ||
var hash = crypto.createHash('md5') | ||
stream.on('data', hash.update.bind(hash)) | ||
stream.on('end', function () { | ||
file.md5 = hash.digest('base64') | ||
}) | ||
} | ||
// enable gzip will remove content length | ||
if (enableGzip && this.acceptsEncodings('gzip') === 'gzip') { | ||
this.remove('Content-Length') | ||
this.set('Content-Encoding', 'gzip') | ||
this.body = stream.pipe(zlib.createGzip()) | ||
} else { | ||
var stream = this.body = fs.createReadStream(file.path) | ||
stream.on('error', this.onerror) | ||
onFinished(this, stream.destroy.bind(stream)) | ||
this.body = stream | ||
} | ||
onFinished(this, stream.destroy.bind(stream)) | ||
return | ||
@@ -88,0 +142,0 @@ case 'OPTIONS': |
{ | ||
"name": "koa-static-cache", | ||
"description": "Static cache for koa", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jonathan Ong", |
@@ -35,2 +35,3 @@ # Koa Static Cache [![Build Status](https://travis-ci.org/koajs/static-cache.png)](https://travis-ci.org/koajs/static-cache) | ||
- `options.buffer` (bool) - store the files in memory instead of streaming from the filesystem on each request. | ||
- `options.gzip` (bool) - when request's accept-encoding include gzip, files will compressed by gzip. | ||
- `options.alias` (obj) - object map of aliases. See below. | ||
@@ -37,0 +38,0 @@ - `files` (obj) - optional files object. See below. |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
8943
123
117
1