koa-cache-static
Advanced tools
Comparing version 1.5.3 to 4.0.1
63
index.js
'use strict'; | ||
'use strict' | ||
@@ -8,6 +8,6 @@ /** | ||
const resolve = require('path').resolve; | ||
const assert = require('assert'); | ||
const debug = require('debug')('koa-static'); | ||
const send = require('koa-send-cache'); | ||
const debug = require('debug')('koa-static') | ||
const { resolve } = require('path') | ||
const assert = require('assert') | ||
const send = require('koa-send-cache') | ||
@@ -18,3 +18,3 @@ /** | ||
module.exports = serve; | ||
module.exports = serve | ||
@@ -30,30 +30,47 @@ /** | ||
function serve(root, opts) { | ||
opts = opts || {}; | ||
function serve (root, opts) { | ||
opts = opts || {} | ||
assert(root, 'root directory is required to serve files'); | ||
assert(root, 'root directory is required to serve files') | ||
// options | ||
debug('static "%s" %j', root, opts); | ||
opts.root = resolve(root); | ||
if (opts.index !== false) opts.index = opts.index || 'index.html'; | ||
debug('static "%s" %j', root, opts) | ||
opts.root = resolve(root) | ||
if (opts.index !== false) opts.index = opts.index || 'index.html' | ||
if (!opts.defer) { | ||
return function *serve(next){ | ||
if (this.method == 'HEAD' || this.method == 'GET') { | ||
if (yield send(this, this.path, opts)) return; | ||
return async function serve (ctx, next) { | ||
let done = false | ||
if (ctx.method === 'HEAD' || ctx.method === 'GET') { | ||
try { | ||
done = await send(ctx, ctx.path, opts) | ||
} catch (err) { | ||
if (err.status !== 404) { | ||
throw err | ||
} | ||
} | ||
} | ||
yield* next; | ||
}; | ||
if (!done) { | ||
await next() | ||
} | ||
} | ||
} | ||
return function *serve(next){ | ||
yield* next; | ||
return async function serve (ctx, next) { | ||
await next() | ||
if (this.method != 'HEAD' && this.method != 'GET') return; | ||
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return | ||
// response is already handled | ||
if (this.body != null || this.status != 404) return; | ||
if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line | ||
yield send(this, this.path, opts); | ||
}; | ||
try { | ||
await send(ctx, ctx.path, opts) | ||
} catch (err) { | ||
if (err.status !== 404) { | ||
throw err | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "koa-cache-static", | ||
"description": "Static file serving middleware for koa", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/koajs/static.git" | ||
}, | ||
"version": "1.5.3", | ||
"repository": "koajs/static", | ||
"version": "4.0.1", | ||
"keywords": [ | ||
@@ -20,13 +17,20 @@ "koa", | ||
"devDependencies": { | ||
"istanbul-harmony": "0", | ||
"koa": "1", | ||
"mocha": "2", | ||
"supertest": "1" | ||
"eslint": "^4.1.1", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.6.1", | ||
"eslint-plugin-node": "^5.1.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"istanbul": "^0.4.5", | ||
"koa": "^2.3.0", | ||
"mocha": "^3.4.2", | ||
"supertest": "^3.0.0" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"debug": "*", | ||
"koa-send-cache": "~2.0.4" | ||
"debug": "^2.6.8", | ||
"koa-send-cache": "^4.1.0" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --fix .", | ||
"test": "mocha --harmony --reporter spec", | ||
@@ -36,68 +40,7 @@ "test-cov": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha", | ||
}, | ||
"gitHead": "fe1800a9e0737247a2fea23ecaa11273a9cb9780", | ||
"bugs": { | ||
"url": "https://github.com/koajs/static/issues" | ||
"engines": { | ||
"node": ">= 7.6.0" | ||
}, | ||
"homepage": "https://github.com/koajs/static#readme", | ||
"_id": "koa-cache-static@1.5.2", | ||
"_shasum": "400c61e5669481a2f881ee85245abe4d73c1daef", | ||
"_from": "koa-cache-static@1.5.2", | ||
"_npmVersion": "3.3.6", | ||
"_nodeVersion": "5.0.0", | ||
"_npmUser": { | ||
"name": "coderhaoxin", | ||
"email": "coderhaoxin@outlook.com" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "aheckmann", | ||
"email": "aaron.heckmann+github@gmail.com" | ||
}, | ||
{ | ||
"name": "coderhaoxin", | ||
"email": "coderhaoxin@outlook.com" | ||
}, | ||
{ | ||
"name": "dead_horse", | ||
"email": "dead_horse@qq.com" | ||
}, | ||
{ | ||
"name": "eivifj", | ||
"email": "eivind.fjeldstad@gmail.com" | ||
}, | ||
{ | ||
"name": "fengmk2", | ||
"email": "fengmk2@gmail.com" | ||
}, | ||
{ | ||
"name": "jonathanong", | ||
"email": "jonathanrichardong@gmail.com" | ||
}, | ||
{ | ||
"name": "jongleberry", | ||
"email": "jonathanrichardong@gmail.com" | ||
}, | ||
{ | ||
"name": "juliangruber", | ||
"email": "julian@juliangruber.com" | ||
}, | ||
{ | ||
"name": "tejasmanohar", | ||
"email": "me@tejasmanohar.com" | ||
}, | ||
{ | ||
"name": "tjholowaychuk", | ||
"email": "tj@vision-media.ca" | ||
} | ||
], | ||
"dist": { | ||
"shasum": "400c61e5669481a2f881ee85245abe4d73c1daef", | ||
"size": 2275, | ||
"noattachment": false, | ||
"tarball": "http://registry.npm.taobao.org/koa-cache-static/download/koa-cache-static-1.5.2.tgz" | ||
}, | ||
"directories": {}, | ||
"publish_time": 1446603994610, | ||
"_cnpm_publish_time": 1446603994610, | ||
"_resolved": "https://registry.npm.taobao.org/koa-cache-static/download/koa-cache-static-1.5.2.tgz" | ||
} | ||
"_from": "koa-static@4.0.1", | ||
"_resolved": "http://registry.npm.alibaba-inc.com/koa-static/download/koa-static-4.0.1.tgz" | ||
} |
@@ -1,2 +0,2 @@ | ||
# koa-cache-static | ||
# koa-static | ||
@@ -10,9 +10,8 @@ [![NPM version][npm-image]][npm-url] | ||
Koa static file serving middleware, wrapper for [`koa-send`](https://github.com/koajs/send). | ||
Static file serving middleware. | ||
## Installation | ||
```bash | ||
$ npm install koa-cache-static | ||
$ npm install koa-static | ||
``` | ||
@@ -23,5 +22,5 @@ | ||
```js | ||
var koa = require('koa'); | ||
var app = koa(); | ||
app.use(require('koa-cache-static')(root, opts)); | ||
const Koa = require('koa'); | ||
const app = new Koa(); | ||
app.use(require('koa-static')(root, opts)); | ||
``` | ||
@@ -37,3 +36,5 @@ | ||
- `index` Default file name, defaults to 'index.html' | ||
- `defer` If true, serves after `yield next`, allowing any downstream middleware to respond first. | ||
- `defer` If true, serves after `return next()`, allowing any downstream middleware to respond first. | ||
- `gzip` Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true. | ||
- `extensions` Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults to `false`) | ||
@@ -43,5 +44,5 @@ ## Example | ||
```js | ||
var serve = require('koa-cache-static'); | ||
var koa = require('koa'); | ||
var app = koa(); | ||
const serve = require('koa-static'); | ||
const Koa = require('koa'); | ||
const app = new Koa(); | ||
@@ -62,2 +63,8 @@ // $ GET /package.json | ||
### See also | ||
- [koajs/conditional-get](https://github.com/koajs/conditional-get) Conditional GET support for koa | ||
- [koajs/compress](https://github.com/koajs/compress) Compress middleware for koa | ||
- [koajs/mount](https://github.com/koajs/mount) Mount `koa-static` to a specific path | ||
## License | ||
@@ -67,4 +74,4 @@ | ||
[npm-image]: https://img.shields.io/npm/v/koa-cache-static.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa-cache-static | ||
[npm-image]: https://img.shields.io/npm/v/koa-static.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa-static | ||
[github-tag]: http://img.shields.io/github/tag/koajs/static.svg?style=flat-square | ||
@@ -78,7 +85,7 @@ [github-url]: https://github.com/koajs/static/tags | ||
[david-url]: https://david-dm.org/koajs/static | ||
[license-image]: http://img.shields.io/npm/l/koa-cache-static.svg?style=flat-square | ||
[license-image]: http://img.shields.io/npm/l/koa-static.svg?style=flat-square | ||
[license-url]: LICENSE | ||
[downloads-image]: http://img.shields.io/npm/dm/koa-cache-static.svg?style=flat-square | ||
[downloads-url]: https://npmjs.org/package/koa-cache-static | ||
[downloads-image]: http://img.shields.io/npm/dm/koa-static.svg?style=flat-square | ||
[downloads-url]: https://npmjs.org/package/koa-static | ||
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square | ||
[gittip-url]: https://www.gittip.com/jonathanong/ |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
8170
4
58
1
85
10
3
1
1
+ Addeddebug@2.6.9(transitive)
+ Addedhttp-errors@1.8.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedkoa-send-cache@4.1.0(transitive)
+ Addedms@2.0.0(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedtoidentifier@1.0.1(transitive)
- Removeddebug@4.4.0(transitive)
- Removedkoa-send-cache@2.0.4(transitive)
- Removedms@2.1.3(transitive)
Updateddebug@^2.6.8
Updatedkoa-send-cache@^4.1.0