koa-static
Advanced tools
Comparing version 3.0.0 to 4.0.0
4.0.0 / 2017-07-03 | ||
================== | ||
* upgrade to koa-send@4 | ||
* use async function | ||
3.0.0 / 2016-03-24 | ||
@@ -3,0 +9,0 @@ ================== |
64
index.js
'use strict'; | ||
'use strict' | ||
@@ -8,6 +8,6 @@ /** | ||
const debug = require('debug')('koa-static'); | ||
const resolve = require('path').resolve; | ||
const assert = require('assert'); | ||
const send = require('koa-send'); | ||
const debug = require('debug')('koa-static') | ||
const { resolve } = require('path') | ||
const assert = require('assert') | ||
const send = require('koa-send') | ||
@@ -18,3 +18,3 @@ /** | ||
module.exports = serve; | ||
module.exports = serve | ||
@@ -30,34 +30,40 @@ /** | ||
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(ctx, next){ | ||
if (ctx.method == 'HEAD' || ctx.method == 'GET') { | ||
return send(ctx, ctx.path, opts).then(done => { | ||
if (!done) { | ||
return next(); | ||
} | ||
}); | ||
return async function serve (ctx, next) { | ||
if (ctx.method === 'HEAD' || ctx.method === 'GET') { | ||
try { | ||
await send(ctx, ctx.path, opts) | ||
} catch (err) { | ||
await next() | ||
} | ||
return | ||
} | ||
return next(); | ||
}; | ||
await next() | ||
} | ||
} | ||
return function serve(ctx, next){ | ||
return next().then(() => { | ||
if (ctx.method != 'HEAD' && ctx.method != 'GET') return; | ||
// response is already handled | ||
if (ctx.body != null || ctx.status != 404) return; | ||
return async function serve (ctx, next) { | ||
await next() | ||
return send(ctx, ctx.path, opts); | ||
}); | ||
}; | ||
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return | ||
// response is already handled | ||
if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line | ||
try { | ||
await send(ctx, ctx.path, opts) | ||
} catch (err) { | ||
ctx.status = err.status || 500 | ||
} | ||
} | ||
} |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/static", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"keywords": [ | ||
@@ -18,17 +18,27 @@ "koa", | ||
"devDependencies": { | ||
"istanbul": "0", | ||
"koa": "2", | ||
"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": "^3.2.0" | ||
"debug": "^2.6.8", | ||
"koa-send": "^4.1.0" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --fix .", | ||
"test": "mocha --harmony --reporter spec", | ||
"test-cov": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha", | ||
"test-travis": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly" | ||
}, | ||
"engines": { | ||
"node": ">= 7.6.0" | ||
} | ||
} |
@@ -21,4 +21,4 @@ # koa-static | ||
```js | ||
var koa = require('koa'); | ||
var app = koa(); | ||
const Koa = require('koa'); | ||
const app = new Koa(); | ||
app.use(require('koa-static')(root, opts)); | ||
@@ -37,2 +37,3 @@ ``` | ||
- `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`) | ||
@@ -42,5 +43,5 @@ ## Example | ||
```js | ||
var serve = require('koa-static'); | ||
var koa = require('koa'); | ||
var app = koa(); | ||
const serve = require('koa-static'); | ||
const Koa = require('koa'); | ||
const app = new Koa(); | ||
@@ -47,0 +48,0 @@ // $ GET /package.json |
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
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
6963
52
0
85
10
+ Addedhttp-errors@1.8.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedkoa-send@4.1.3(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedtoidentifier@1.0.1(transitive)
- Removedco@4.6.0(transitive)
- Removeddebug@4.3.7(transitive)
- Removedkoa-send@3.3.0(transitive)
- Removedms@2.1.3(transitive)
Updateddebug@^2.6.8
Updatedkoa-send@^4.1.0