koa-static-cache
Advanced tools
Comparing version 2.0.2 to 3.0.0
3.0.0 / 2015-01-06 | ||
================== | ||
* fix(test): typo | ||
* fix(buffer): keep the old logic of treat unbuffered file | ||
* feat: add opt.buffer false to serve file not cache at all | ||
* fix: support load file dynamic, close #30 | ||
2.0.2 / 2015-01-05 | ||
@@ -3,0 +11,0 @@ ================== |
82
index.js
@@ -24,20 +24,3 @@ var crypto = require('crypto') | ||
readDir(dir).forEach(function (name) { | ||
var pathname = options.prefix + name | ||
var obj = files[pathname] = {} | ||
var filename = obj.path = path.join(dir, name) | ||
var stats = fs.statSync(filename) | ||
var buffer = fs.readFileSync(filename) | ||
obj.cacheControl = options.cacheControl | ||
obj.maxAge = options.maxAge || 0 | ||
obj.type = obj.mime = mime.lookup(pathname) || 'application/octet-stream' | ||
obj.mtime = stats.mtime.toUTCString() | ||
obj.length = stats.size | ||
obj.md5 = crypto.createHash('md5').update(buffer).digest('base64') | ||
debug('file: ' + JSON.stringify(obj, null, 2)) | ||
if (options.buffer) | ||
obj.buffer = buffer | ||
buffer = null | ||
loadFile(name, dir, options, files) | ||
}) | ||
@@ -58,8 +41,22 @@ | ||
return function* staticCache(next) { | ||
var file = files[safeDecodeURIComponent(path.normalize(this.path))] | ||
if (!file) | ||
return yield* next | ||
// only accept HEAD and GET | ||
if (this.method !== 'HEAD' && this.method !== 'GET') return yield* next; | ||
// decode for `/%E4%B8%AD%E6%96%87` | ||
// normalize for `//index` | ||
var filename = safeDecodeURIComponent(path.normalize(this.path)) | ||
var file = files[filename] | ||
// try to load file | ||
if (!file) { | ||
// hidden file | ||
if (path.basename(filename)[0] === '.') return yield* next | ||
var isExists = yield exists(path.join(dir, filename)) | ||
if (!isExists) return yield* next | ||
file = loadFile(filename, dir, options, files) | ||
} | ||
this.status = 200 | ||
@@ -160,1 +157,42 @@ | ||
} | ||
function exists(filename) { | ||
return function (done) { | ||
fs.exists(filename, function(exists) { | ||
done(null, exists) | ||
}) | ||
} | ||
} | ||
/** | ||
* load file and add file content to cache | ||
* | ||
* @param {String} name | ||
* @param {String} dir | ||
* @param {Object} options | ||
* @param {Object} files | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function loadFile(name, dir, options, files) { | ||
var pathname = options.prefix + name | ||
var obj = files[pathname] = {} | ||
var filename = obj.path = path.join(dir, name) | ||
var stats = fs.statSync(filename) | ||
var buffer = fs.readFileSync(filename) | ||
obj.cacheControl = options.cacheControl | ||
obj.maxAge = options.maxAge || 0 | ||
obj.type = obj.mime = mime.lookup(pathname) || 'application/octet-stream' | ||
obj.mtime = stats.mtime.toUTCString() | ||
obj.length = stats.size | ||
obj.md5 = crypto.createHash('md5').update(buffer).digest('base64') | ||
debug('file: ' + JSON.stringify(obj, null, 2)) | ||
if (options.buffer) | ||
obj.buffer = buffer | ||
buffer = null | ||
return obj | ||
} |
{ | ||
"name": "koa-static-cache", | ||
"description": "Static cache for koa", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jonathan Ong", |
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
12078
160