Skip to content

Commit 0ac4ff0

Browse files
committedMar 22, 2016
Convert generator-mw with deprecation warning
1 parent a1aec3d commit 0ac4ff0

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed
 

‎Readme.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,24 @@ app.use(co.wrap(function *(ctx, next) {
8888
}));
8989
```
9090

91-
### Old signature middleware (v1.x)
91+
### Old signature middleware (v1.x) - Deprecated
9292

93-
If you want to use old signature or be compatible with old middleware, you must use [koa-convert](https://github.com/gyson/koa-convert) to convert legacy generator middleware to promise middleware.
93+
**Old signature middleware (v1.x) support will be removed in v3**
94+
95+
Koa v2.x will try to convert legacy signature, generator middleware on `app.use`, using [koa-convert](https://github.com/koajs/convert).
96+
It is however recommended that you choose to migrate all v1.x middleware as soon as possible.
97+
98+
```js
99+
// Koa will convert
100+
app.use(function *(next) {
101+
const start = new Date();
102+
yield next;
103+
const ms = new Date() - start;
104+
console.log(`${this.method} ${this.url} - ${ms}ms`);
105+
});
106+
```
107+
108+
You could do it manually as well, in which case Koa will not convert.
94109

95110
```js
96111
const convert = require('koa-convert');
@@ -103,7 +118,6 @@ app.use(convert(function *(next) {
103118
}));
104119
```
105120

106-
107121
## Babel setup
108122
For Node 4.0 and Babel 6.0 you can setup like this
109123

‎lib/application.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const assert = require('assert');
2121
const Stream = require('stream');
2222
const http = require('http');
2323
const only = require('only');
24+
const convert = require('koa-convert');
25+
const deprecate = require('depd')('koa');
2426

2527
/**
2628
* Expose `Application` class.
@@ -93,14 +95,21 @@ module.exports = class Application extends Emitter {
9395
/**
9496
* Use the given middleware `fn`.
9597
*
98+
* Old-style middleware will be converted.
99+
*
96100
* @param {Function} fn
97101
* @return {Application} self
98102
* @api public
99103
*/
100104

101105
use(fn) {
102106
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
103-
if (isGeneratorFunction(fn)) throw new TypeError('Support for generators has been removed. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x');
107+
if (isGeneratorFunction(fn)) {
108+
deprecate('Support for generators will been removed in v3. ' +
109+
'See the documentation for examples of how to convert old middleware ' +
110+
'https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x');
111+
fn = convert(fn);
112+
}
104113
debug('use %s', fn._name || fn.name || '-');
105114
this.middleware.push(fn);
106115
return this;

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"cookies": "~0.6.1",
2525
"debug": "*",
2626
"delegates": "^1.0.0",
27+
"depd": "^1.1.0",
2728
"destroy": "^1.0.3",
2829
"error-inject": "~1.0.0",
2930
"escape-html": "~1.0.1",
@@ -32,6 +33,7 @@
3233
"http-errors": "^1.2.8",
3334
"is-generator-function": "^1.0.3",
3435
"koa-compose": "^3.0.0",
36+
"koa-convert": "^1.2.0",
3537
"koa-is-json": "^1.0.0",
3638
"mime-types": "^2.0.7",
3739
"on-finished": "^2.1.0",

‎test/application/use.js

+58-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const request = require('supertest');
5+
const assert = require('assert');
56
const Koa = require('../..');
67

78
describe('app.use(fn)', () => {
@@ -42,6 +43,43 @@ describe('app.use(fn)', () => {
4243
});
4344
});
4445

46+
it('should compose mixed middleware', done => {
47+
process.once('deprecation', () => {}); // silence deprecation message
48+
const app = new Koa();
49+
const calls = [];
50+
51+
app.use((ctx, next) => {
52+
calls.push(1);
53+
return next().then(() => {
54+
calls.push(6);
55+
});
56+
});
57+
58+
app.use(function *(next){
59+
calls.push(2);
60+
yield next;
61+
calls.push(5);
62+
});
63+
64+
app.use((ctx, next) => {
65+
calls.push(3);
66+
return next().then(() => {
67+
calls.push(4);
68+
});
69+
});
70+
71+
const server = app.listen();
72+
73+
request(server)
74+
.get('/')
75+
.expect(404)
76+
.end(err => {
77+
if (err) return done(err);
78+
calls.should.eql([1, 2, 3, 4, 5, 6]);
79+
done();
80+
});
81+
});
82+
4583
// https://github.com/koajs/koa/pull/530#issuecomment-148138051
4684
it('should catch thrown errors in non-async functions', done => {
4785
const app = new Koa();
@@ -54,16 +92,33 @@ describe('app.use(fn)', () => {
5492
.end(done);
5593
});
5694

95+
it('should accept both generator and function middleware', done => {
96+
process.once('deprecation', () => {}); // silence deprecation message
97+
const app = new Koa();
98+
99+
app.use((ctx, next) => { return next(); });
100+
app.use(function *(next){ this.body = 'generator'; });
101+
102+
request(app.listen())
103+
.get('/')
104+
.expect(200)
105+
.expect('generator', done);
106+
});
107+
57108
it('should throw error for non function', () => {
58109
const app = new Koa();
59110

60111
[null, undefined, 0, false, 'not a function'].forEach(v => (() => app.use(v)).should.throw('middleware must be a function!'));
61112
});
62113

63-
it('should throw error for generator', () => {
64-
const app = new Koa();
114+
it('should output deprecation message for generator functions', done => {
115+
process.once('deprecation', message => {
116+
assert(/Support for generators will been removed/.test(message));
117+
done();
118+
});
65119

66-
(() => app.use(function *(){})).should.throw(/.+/);
120+
const app = new Koa();
121+
app.use(function *(){});
67122
});
68123

69124
it('should throw error for non function', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.