koa-static-cache
Advanced tools
Comparing version 4.0.0 to 5.0.0
5.0.0 / 2017-04-01 | ||
================== | ||
* Support Koa 2 (#57) | ||
4.0.0 / 2017-02-21 | ||
@@ -3,0 +8,0 @@ ================== |
68
index.js
@@ -48,11 +48,11 @@ var crypto = require('crypto') | ||
return function* staticCache(next) { | ||
return async (ctx, next) => { | ||
// only accept HEAD and GET | ||
if (this.method !== 'HEAD' && this.method !== 'GET') return yield next | ||
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return await next() | ||
// check prefix first to avoid calculate | ||
if (this.path.indexOf(options.prefix) !== 0) return yield next | ||
if (ctx.path.indexOf(options.prefix) !== 0) return await next() | ||
// decode for `/%E4%B8%AD%E6%96%87` | ||
// normalize for `//index` | ||
var filename = safeDecodeURIComponent(path.normalize(this.path)) | ||
var filename = safeDecodeURIComponent(path.normalize(ctx.path)) | ||
@@ -63,4 +63,4 @@ var file = files[filename] | ||
if (!file) { | ||
if (!options.dynamic) return yield next | ||
if (path.basename(filename)[0] === '.') return yield next | ||
if (!options.dynamic) return await next() | ||
if (path.basename(filename)[0] === '.') return await next() | ||
if (filename.charAt(0) === path.sep) filename = filename.slice(1) | ||
@@ -70,3 +70,3 @@ | ||
if (options.prefix !== '/') { | ||
if (filename.indexOf(filePrefix) !== 0) return yield next | ||
if (filename.indexOf(filePrefix) !== 0) return await next() | ||
filename = filename.slice(filePrefix.length) | ||
@@ -77,7 +77,7 @@ } | ||
try { | ||
s = yield fs.stat(path.join(dir, filename)) | ||
s = await fs.stat(path.join(dir, filename)) | ||
} catch (err) { | ||
return yield next | ||
return await next() | ||
} | ||
if (!s.isFile()) return yield next | ||
if (!s.isFile()) return await next() | ||
@@ -87,8 +87,8 @@ file = loadFile(filename, dir, options, files) | ||
this.status = 200 | ||
ctx.status = 200 | ||
if (enableGzip) this.vary('Accept-Encoding') | ||
if (enableGzip) ctx.vary('Accept-Encoding') | ||
if (!file.buffer) { | ||
var stats = yield fs.stat(file.path) | ||
var stats = await fs.stat(file.path) | ||
if (stats.mtime > file.mtime) { | ||
@@ -101,24 +101,24 @@ file.mtime = stats.mtime | ||
this.response.lastModified = file.mtime | ||
if (file.md5) this.response.etag = file.md5 | ||
ctx.response.lastModified = file.mtime | ||
if (file.md5) ctx.response.etag = file.md5 | ||
if (this.fresh) | ||
return this.status = 304 | ||
if (ctx.fresh) | ||
return ctx.status = 304 | ||
this.type = file.type | ||
this.length = file.zipBuffer ? file.zipBuffer.length : file.length | ||
this.set('cache-control', file.cacheControl || 'public, max-age=' + file.maxAge) | ||
if (file.md5) this.set('content-md5', file.md5) | ||
ctx.type = file.type | ||
ctx.length = file.zipBuffer ? file.zipBuffer.length : file.length | ||
ctx.set('cache-control', file.cacheControl || 'public, max-age=' + file.maxAge) | ||
if (file.md5) ctx.set('content-md5', file.md5) | ||
if (this.method === 'HEAD') | ||
if (ctx.method === 'HEAD') | ||
return | ||
var acceptGzip = this.acceptsEncodings('gzip') === 'gzip' | ||
var acceptGzip = ctx.acceptsEncodings('gzip') === 'gzip' | ||
if (file.zipBuffer) { | ||
if (acceptGzip) { | ||
this.set('content-encoding', 'gzip') | ||
this.body = file.zipBuffer | ||
ctx.set('content-encoding', 'gzip') | ||
ctx.body = file.zipBuffer | ||
} else { | ||
this.body = file.buffer | ||
ctx.body = file.buffer | ||
} | ||
@@ -140,8 +140,8 @@ return | ||
} else { | ||
file.zipBuffer = yield zlib.gzip(file.buffer) | ||
file.zipBuffer = await zlib.gzip(file.buffer) | ||
} | ||
this.set('content-encoding', 'gzip') | ||
this.body = file.zipBuffer | ||
ctx.set('content-encoding', 'gzip') | ||
ctx.body = file.zipBuffer | ||
} else { | ||
this.body = file.buffer | ||
ctx.body = file.buffer | ||
} | ||
@@ -162,8 +162,8 @@ return | ||
this.body = stream | ||
ctx.body = stream | ||
// enable gzip will remove content length | ||
if (shouldGzip) { | ||
this.remove('content-length') | ||
this.set('content-encoding', 'gzip') | ||
this.body = stream.pipe(zlib.createGzip()) | ||
ctx.remove('content-length') | ||
ctx.set('content-encoding', 'gzip') | ||
ctx.body = stream.pipe(zlib.createGzip()) | ||
} | ||
@@ -170,0 +170,0 @@ } |
{ | ||
"name": "koa-static-cache", | ||
"description": "Static cache for koa", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"author": { | ||
@@ -56,3 +56,3 @@ "name": "Jonathan Ong", | ||
"istanbul": "~0.4.1", | ||
"koa": "1", | ||
"koa": "2", | ||
"mocha": "2", | ||
@@ -67,4 +67,4 @@ "should": "8", | ||
"engines": { | ||
"node": "> 0.12.0" | ||
"node": "> 7.6.0" | ||
} | ||
} |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
14726