Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: koajs/static
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f98805a40b6c5502b2b7e6acb64040ef6217f640
Choose a base ref
...
head repository: koajs/static
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9b11d848086cd56ac8552b983a22d7386049aca8
Choose a head ref

Commits on Mar 24, 2016

  1. support koa 2.x

    haoxin committed Mar 24, 2016

    Verified

    This commit was signed with the committer’s verified signature.
    bagder Daniel Stenberg
    Copy the full SHA
    043efc8 View commit details
  2. travis: test node@4+

    haoxin committed Mar 24, 2016
    Copy the full SHA
    2be1924 View commit details
  3. 3.0.0

    haoxin committed Mar 24, 2016
    Copy the full SHA
    855a20c View commit details

Commits on Jul 10, 2016

  1. Updated README.md for koa as a constructor

    PR  (#81)
    PlasmaPower authored and haoxin committed Jul 10, 2016
    Copy the full SHA
    6c4d63d View commit details

Commits on Jul 26, 2016

  1. Update readme for issue #86

    PR #87
    hiwanz authored and haoxin committed Jul 26, 2016
    Copy the full SHA
    993295d View commit details

Commits on Feb 3, 2017

  1. travis - add node v6, v7

    haoxin committed Feb 3, 2017
    Copy the full SHA
    8dea452 View commit details
  2. doc - add extensions option

    haoxin committed Feb 3, 2017
    Copy the full SHA
    d2b4aeb View commit details

Commits on Feb 26, 2017

  1. update readme

    haoxin committed Feb 26, 2017
    Copy the full SHA
    7deaf2d View commit details

Commits on Jun 30, 2017

  1. travis: test on node 7,8

    haoxin committed Jun 30, 2017
    Copy the full SHA
    6164ed6 View commit details
  2. gitignore: add package-lock.json

    haoxin committed Jun 30, 2017
    Copy the full SHA
    bb8b135 View commit details
  3. bump deps

    haoxin committed Jun 30, 2017
    Copy the full SHA
    de1a34e View commit details
  4. lint

    haoxin committed Jun 30, 2017
    Copy the full SHA
    943a8cc View commit details
  5. add more tests

    haoxin committed Jun 30, 2017
    Copy the full SHA
    a94f34c View commit details
  6. upgrade to koa-send@4

    haoxin committed Jun 30, 2017
    Copy the full SHA
    be299ae View commit details

Commits on Jul 3, 2017

  1. 4.0.0

    haoxin committed Jul 3, 2017
    Copy the full SHA
    d2b6dbb View commit details

Commits on Jul 9, 2017

  1. fix index: false bug if path is directory

    fix `index: false` bug if path is directory
    
    Revert "fix `index: false` bug if path is directory"
    
    This reverts commit b952196.
    
    fix `index: false` bug if path is directory
    
    closes #104
    Cap32 authored and haoxin committed Jul 9, 2017
    Copy the full SHA
    1328f78 View commit details
  2. throw error if error status not 404

    closes #106
    haoxin committed Jul 9, 2017
    Copy the full SHA
    e23cfca View commit details
  3. 4.0.1

    haoxin committed Jul 9, 2017
    Copy the full SHA
    e663be6 View commit details

Commits on Nov 13, 2017

  1. docs: add option - setHeaders

    closes #95
    haoxin authored Nov 13, 2017
    Copy the full SHA
    d8be914 View commit details

Commits on Nov 15, 2017

  1. fix: serve mutates opts argument so it cannot be reused (#117)

    * fix: `serve` mutates `opts` argument so it cannot be reused
    
    * Simplify assign expression
    Daniel Levett authored and haoxin committed Nov 15, 2017
    Copy the full SHA
    2efdc31 View commit details

Commits on Nov 16, 2017

  1. 4.0.2

    haoxin committed Nov 16, 2017
    Copy the full SHA
    e4e907d View commit details

Commits on Dec 18, 2017

  1. update readme (#119)

    I just refer on https://github.com/koajs/send 
    
    at first I wanted to write issue:  why not working sending brotli. As it turned out, koa-send works fine and even raises the priority to `.br`. It's all browser conventional requirement of https.
    mishelen authored and haoxin committed Dec 18, 2017
    Copy the full SHA
    ef9379c View commit details

Commits on May 17, 2018

  1. bump deps

    haoxin committed May 17, 2018
    Copy the full SHA
    026c8dd View commit details
  2. npm: disable package-lock

    haoxin committed May 17, 2018
    Copy the full SHA
    9fbb147 View commit details
  3. 4.0.3

    haoxin committed May 17, 2018
    Copy the full SHA
    c74836c View commit details

Commits on Jun 19, 2018

  1. travis: add node@10

    haoxin committed Jun 19, 2018
    Copy the full SHA
    36831b6 View commit details
  2. bump deps

    haoxin committed Jun 19, 2018
    Copy the full SHA
    b0eff0b View commit details
  3. 5.0.0

    haoxin committed Jun 19, 2018
    Copy the full SHA
    9b11d84 View commit details
Showing with 350 additions and 211 deletions.
  1. +2 −0 .eslintrc
  2. +1 −0 .gitignore
  3. +1 −0 .npmrc
  4. +2 −6 .travis.yml
  5. +34 −0 History.md
  6. +9 −6 Readme.md
  7. +13 −12 example.js
  8. +40 −23 index.js
  9. +20 −10 package.json
  10. +3 −0 test/.eslintrc
  11. +225 −154 test/index.js
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
extends: standard
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
package-lock.json
node_modules
coverage
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
node_js:
- "0.12"
- "1"
- "2"
- "3"
- "4"
- "5"
- 8
- 10
language: node_js
script: "npm run-script test-travis"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
34 changes: 34 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@

5.0.0 / 2018-06-19
==================

* bump deps

4.0.3 / 2018-05-17
==================

* npm: disable package-lock
* bump deps

4.0.2 / 2017-11-16
==================

* Fix: `serve` mutates `opts` argument so it cannot be reused (#117)

4.0.1 / 2017-07-09
==================

* Fix: throw error if error status not 404
* fix `index: false` bug if path is directory

4.0.0 / 2017-07-03
==================

* upgrade to koa-send@4
* use async function

3.0.0 / 2016-03-24
==================

* support koa 2.x
* travis: test node@4+

2.0.0 / 2016-01-07
==================

15 changes: 9 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ $ npm install koa-static
## API

```js
var koa = require('koa');
var app = koa();
const Koa = require('koa');
const app = new Koa();
app.use(require('koa-static')(root, opts));
```

@@ -31,15 +31,18 @@ app.use(require('koa-static')(root, opts));
- `maxage` Browser cache max-age in milliseconds. defaults to 0
- `hidden` Allow transfer of hidden files. defaults to false
- `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.
- `br` Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.
- [setHeaders](https://github.com/koajs/send#setheaders) Function to set custom headers on response.
- `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`)

## 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();

// $ GET /package.json
app.use(serve('.'));
25 changes: 13 additions & 12 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@

'use strict';
'use strict'

const serve = require('./');
const koa = require('koa');
const app = koa();
const serve = require('./')
const Koa = require('koa')
const app = new Koa()

// $ GET /package.json
// $ GET /

app.use(serve('.'));
app.use(serve('.'))

app.use(function *(next){
yield next;
if ('/' == this.path) {
this.body = 'Try `GET /package.json`';
}
app.use((ctx, next) => {
return next().then(() => {
if (ctx.path === '/') {
ctx.body = 'Try `GET /package.json`'
}
})
})

app.listen(3000);
app.listen(3000)

console.log('listening on port 3000');
console.log('listening on port 3000')
63 changes: 40 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

'use strict';
'use strict'

/**
* Module dependencies.
*/

const resolve = require('path').resolve;
const assert = require('assert');
const debug = require('debug')('koa-static');
const send = require('koa-send');
const debug = require('debug')('koa-static')
const { resolve } = require('path')
const assert = require('assert')
const send = require('koa-send')

/**
* Expose `serve()`.
*/

module.exports = serve;
module.exports = serve

/**
* Serve static files from `root`.
@@ -25,32 +25,49 @@ module.exports = serve;
* @api public
*/

function serve(root, opts) {
opts = opts || {};
function serve (root, opts) {
opts = Object.assign({}, 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
}
}
}

if (!done) {
await next()
}
yield* 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
}
}
}
}
30 changes: 20 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "koa-static",
"description": "Static file serving middleware for koa",
"repository": "koajs/static",
"version": "2.0.0",
"version": "5.0.0",
"keywords": [
"koa",
"middleware",
@@ -14,19 +14,29 @@
"index.js"
],
"devDependencies": {
"istanbul": "0",
"koa": "1",
"mocha": "2",
"supertest": "1"
"eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-standard": "^3.1.0",
"istanbul": "^0.4.5",
"koa": "^2.5.1",
"mocha": "^5.2.0",
"supertest": "^3.1.0"
},
"license": "MIT",
"dependencies": {
"debug": "*",
"koa-send": "~3.1.0"
"debug": "^3.1.0",
"koa-send": "^5.0.0"
},
"scripts": {
"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"
"lint": "eslint --fix .",
"test": "mocha --harmony --reporter spec --exit",
"test-cov": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --exit",
"test-travis": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --exit"
},
"engines": {
"node": ">= 7.6.0"
}
}
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
env:
mocha: true
379 changes: 225 additions & 154 deletions test/index.js

Large diffs are not rendered by default.