Comparing version 2.0.0 to 2.0.1
2.0.0 / 2016-03-23 | ||
2.0.1 / 2017-02-25 | ||
================== | ||
NOTE: we hit a versioning snafu. `v2.0.0` was previously released, | ||
so `v2.0.1` is released as the first `v2.x` with a `latest` tag. | ||
* upgrade mocha #900 | ||
* add names to `application`'s request and response handlers #805 | ||
* breaking: remove unused `app.name` #899 | ||
* breaking: drop official support for node < 7.6 | ||
2.0.0 / ?????????? | ||
================== | ||
* Fix malformed content-type header causing exception on charset get (#898) | ||
* fix: subdomains should be [] if the host is an ip (#808) | ||
* don't pre-bound onerror [breaking change] (#800) | ||
* fix `ctx.flushHeaders()` to use `res.flushHeaders()` instead of `res.writeHead()` (#795) | ||
* fix(response): correct response.writable logic (#782) | ||
* merge v1.1.2 and v1.2.0 changes | ||
@@ -11,2 +27,27 @@ * include `koa-convert` so that generator functions still work | ||
2.0.0-alpha.8 / 2017-02-13 | ||
================== | ||
* Fix malformed content-type header causing exception on charset get (#898) | ||
2.0.0-alpha.7 / 2016-09-07 | ||
================== | ||
* fix: subdomains should be [] if the host is an ip (#808) | ||
2.0.0-alpha.6 / 2016-08-29 | ||
================== | ||
* don't pre-bound onerror [breaking change] | ||
2.0.0-alpha.5 / 2016-08-10 | ||
================== | ||
* fix `ctx.flushHeaders()` to use `res.flushHeaders()` instead of `res.writeHead()` | ||
2.0.0-alpha.4 / 2016-07-23 | ||
================== | ||
* fix `response.writeable` during pipelined requests | ||
1.2.0 / 2016-03-03 | ||
@@ -13,0 +54,0 @@ ================== |
@@ -108,5 +108,5 @@ | ||
if (isGeneratorFunction(fn)) { | ||
deprecate('Support for generators will been removed in v3. ' + | ||
deprecate('Support for generators will be removed in v3. ' + | ||
'See the documentation for examples of how to convert old middleware ' + | ||
'https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x'); | ||
'https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x---deprecated'); | ||
fn = convert(fn); | ||
@@ -132,8 +132,12 @@ } | ||
return (req, res) => { | ||
const handleRequest = (req, res) => { | ||
res.statusCode = 404; | ||
const ctx = this.createContext(req, res); | ||
onFinished(res, ctx.onerror); | ||
fn(ctx).then(() => respond(ctx)).catch(ctx.onerror); | ||
const onerror = err => ctx.onerror(err); | ||
const handleResponse = () => respond(ctx); | ||
onFinished(res, onerror); | ||
fn(ctx).then(handleResponse).catch(onerror); | ||
}; | ||
return handleRequest; | ||
} | ||
@@ -157,3 +161,2 @@ | ||
response.request = request; | ||
context.onerror = context.onerror.bind(context); | ||
context.originalUrl = request.originalUrl = req.url; | ||
@@ -164,2 +167,3 @@ context.cookies = new Cookies(req, res, { | ||
}); | ||
request.ip = request.ips[0] || req.socket.remoteAddress || ''; | ||
context.accept = request.accept = accepts(req); | ||
@@ -166,0 +170,0 @@ context.state = {}; |
@@ -8,2 +8,3 @@ | ||
const net = require('net'); | ||
const contentType = require('content-type'); | ||
@@ -318,6 +319,12 @@ const stringify = require('url').format; | ||
get charset() { | ||
const type = this.get('Content-Type'); | ||
let type = this.get('Content-Type'); | ||
if (!type) return ''; | ||
return contentType.parse(type).parameters.charset || ''; | ||
try { | ||
type = contentType.parse(type); | ||
} catch (e) { | ||
return ''; | ||
} | ||
return type.parameters.charset || ''; | ||
}, | ||
@@ -372,15 +379,2 @@ | ||
/** | ||
* Return the remote address, or when | ||
* `app.proxy` is `true` return | ||
* the upstream addr. | ||
* | ||
* @return {String} | ||
* @api public | ||
*/ | ||
get ip() { | ||
return this.ips[0] || this.socket.remoteAddress || ''; | ||
}, | ||
/** | ||
* When `app.proxy` is `true`, parse | ||
@@ -423,3 +417,5 @@ * the "X-Forwarded-For" ip address list. | ||
const offset = this.app.subdomainOffset; | ||
return (this.host || '') | ||
const hostname = this.hostname; | ||
if (net.isIP(hostname)) return []; | ||
return hostname | ||
.split('.') | ||
@@ -432,3 +428,3 @@ .reverse() | ||
* Check if the given `type(s)` is acceptable, returning | ||
* the best match when true, otherwise `undefined`, in which | ||
* the best match when true, otherwise `false`, in which | ||
* case you should respond with 406 "Not Acceptable". | ||
@@ -460,3 +456,3 @@ * | ||
* this.accepts('png'); | ||
* // => undefined | ||
* // => false | ||
* | ||
@@ -469,3 +465,3 @@ * // Accept: text/*;q=.5, application/json | ||
* @param {String|Array} type(s)... | ||
* @return {String|Array|Boolean} | ||
* @return {String|Array|false} | ||
* @api public | ||
@@ -472,0 +468,0 @@ */ |
@@ -167,3 +167,3 @@ | ||
onFinish(this.res, destroy.bind(null, val)); | ||
ensureErrorHandler(val, this.ctx.onerror); | ||
ensureErrorHandler(val, err => this.ctx.onerror(err)); | ||
@@ -495,4 +495,9 @@ // overwriting | ||
get writable() { | ||
// can't write any more after response finished | ||
if (this.res.finished) return false; | ||
const socket = this.res.socket; | ||
if (!socket) return false; | ||
// There are already pending outgoing res, but still writable | ||
// https://github.com/nodejs/node/blob/v4.4.7/lib/_http_server.js#L486 | ||
if (!socket) return true; | ||
return socket.writable; | ||
@@ -534,4 +539,4 @@ }, | ||
flushHeaders() { | ||
this.res.writeHead(this.res.statusCode); | ||
this.res.flushHeaders(); | ||
} | ||
}; |
{ | ||
"name": "koa", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Koa web app framework", | ||
@@ -46,10 +46,5 @@ "main": "lib/application.js", | ||
}, | ||
"publishConfig": { | ||
"tag": "next" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.1.2", | ||
"babel-eslint": "^5.0.0", | ||
"babel-plugin-transform-async-to-generator": "^6.0.14", | ||
"eslint": "~2.2.0", | ||
"babel-eslint": "^7.1.1", | ||
"eslint": "^2.5.3", | ||
"eslint-config-standard": "^5.1.0", | ||
@@ -60,3 +55,3 @@ "eslint-plugin-babel": "^3.1.0", | ||
"istanbul": "^0.4.0", | ||
"mocha": "^2.0.1", | ||
"mocha": "^3.2.0", | ||
"should": "^6.0.3", | ||
@@ -63,0 +58,0 @@ "should-http": "0.0.3", |
@@ -16,6 +16,7 @@ <img src="https://dl.dropboxusercontent.com/u/6396913/koa/logo.png" alt="koa middleware framework for nodejs" width="255px" /> | ||
## Installation | ||
Koa requires __node v4.0.0__ or higher for (partial) ES2015 support. | ||
Koa requires __node v7.6.0__ or higher for ES2015 and async function support. | ||
``` | ||
$ npm install koa@next | ||
$ npm install koa | ||
``` | ||
@@ -45,3 +46,3 @@ | ||
## Middleware | ||
Koa is an middleware framework, it can take 3 different kind function as middleware: | ||
Koa is a middleware framework that can take 3 different kinds of functions as middleware: | ||
@@ -52,9 +53,20 @@ * common function | ||
Here we write an logger middleware with different function. | ||
Here is an example of logger middleware with each of the different functions: | ||
### ___async___ functions (node v7.6+) | ||
```js | ||
app.use(async (ctx, next) => { | ||
const start = new Date(); | ||
await next(); | ||
const ms = new Date() - start; | ||
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); | ||
}); | ||
``` | ||
### Common function | ||
```js | ||
// Middleware normally take two parameters (ctx, next), ctx is the context for one request, | ||
// next is an function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion. | ||
// Middleware normally takes two parameters (ctx, next), ctx is the context for one request, | ||
// next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion. | ||
@@ -70,13 +82,2 @@ app.use((ctx, next) => { | ||
### ___async___ functions (Babel required) | ||
```js | ||
app.use(async (ctx, next) => { | ||
const start = new Date(); | ||
await next(); | ||
const ms = new Date() - start; | ||
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); | ||
}); | ||
``` | ||
### GeneratorFunction | ||
@@ -126,9 +127,7 @@ | ||
## Babel setup | ||
For Node 4.0 and Babel 6.0 you can setup like this | ||
For Node 4.0+ and Babel 6.0 you can setup like this: | ||
```bash | ||
// install babel and required presets | ||
$ npm install babel-core --save | ||
$ npm install babel-preset-es2015-node5 --save | ||
$ npm install babel-preset-stage-3 --save | ||
$ npm install babel-register babel-plugin-transform-async-to-generator --save | ||
``` | ||
@@ -138,7 +137,8 @@ | ||
// set babel in entry file | ||
require('babel-core/register')({ | ||
presets: ['es2015-node5', 'stage-3'] | ||
require('babel-register')({ | ||
plugins: ['transform-async-to-generator'] | ||
}); | ||
``` | ||
Check out an example in koa's [test](test/babel/index.js). | ||
@@ -145,0 +145,0 @@ ## Running tests |
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
Network access
Supply chain riskThis module accesses the network.
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
50170
12
1363
4