From c5545cd918bc6429c5c8417e0dfa41a62cda3990 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Wed, 17 Dec 2014 18:06:45 -0800 Subject: [PATCH] add experimental async function support --- Makefile | 3 ++- lib/application.js | 11 ++++++++--- package.json | 4 +++- test/application.js | 9 +++++++-- test/experimental/async.js | 23 +++++++++++++++++++++++ test/experimental/index.js | 4 ++++ 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 test/experimental/async.js create mode 100644 test/experimental/index.js diff --git a/Makefile b/Makefile index af3deba..87c7140 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,8 @@ endif TESTS = test/application \ test/context/* \ test/request/* \ - test/response/* + test/response/* \ + test/experimental/index.js test: @NODE_ENV=test $(BIN) $(FLAGS) \ diff --git a/lib/application.js b/lib/application.js index fb59f26..e353a81 100644 --- a/lib/application.js +++ b/lib/application.js @@ -4,6 +4,7 @@ var debug = require('debug')('koa:application'); var Emitter = require('events').EventEmitter; +var compose_es7 = require('composition'); var onFinished = require('on-finished'); var response = require('./response'); var compose = require('koa-compose'); @@ -94,7 +95,10 @@ app.toJSON = function(){ */ app.use = function(fn){ - assert(fn && 'GeneratorFunction' == fn.constructor.name, 'app.use() requires a generator function'); + if (!this.experimental) { + // es7 async functions are allowed + assert(fn && 'GeneratorFunction' == fn.constructor.name, 'app.use() requires a generator function'); + } debug('use %s', fn._name || fn.name || '-'); this.middleware.push(fn); return this; @@ -110,8 +114,9 @@ app.use = function(fn){ app.callback = function(){ var mw = [respond].concat(this.middleware); - var gen = compose(mw); - var fn = co.wrap(gen); + var fn = this.experimental + ? compose_es7(mw) + : co.wrap(compose(mw)); var self = this; if (!this.listeners('error').length) this.on('error', this.onerror); diff --git a/package.json b/package.json index 33dfb07..67fd70f 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "license": "MIT", "dependencies": { "accepts": "^1.2.2", - "co": "^4.1.0", + "co": "^4.4.0", + "composition": "^2.1.1", "content-disposition": "~0.5.0", "content-type": "^1.0.0", "cookies": "~0.5.0", @@ -42,6 +43,7 @@ "vary": "^1.0.0" }, "devDependencies": { + "6to5": "^3.6.5", "istanbul-harmony": "~0.3.0", "make-lint": "^1.0.1", "mocha": "^2.0.1", diff --git a/test/application.js b/test/application.js index 8bfef59..0158874 100644 --- a/test/application.js +++ b/test/application.js @@ -104,16 +104,21 @@ describe('app.use(fn)', function(){ }); }) - it('should error when a non-generator function is passed', function(done){ + it('should error when a non-generator function is passed', function(){ var app = koa(); try { app.use(function(){}); } catch (err) { err.message.should.equal('app.use() requires a generator function'); - done(); } }) + + it('should not error when a non-generator function is passed when .experimental=true', function(){ + var app = koa(); + app.experimental = true; + app.use(function(){}); + }) }) describe('app.onerror(err)', function(){ diff --git a/test/experimental/async.js b/test/experimental/async.js new file mode 100644 index 0000000..ed401ae --- /dev/null +++ b/test/experimental/async.js @@ -0,0 +1,23 @@ + +/** + * Separate file primarily because we use `require('6to5/register')`. + */ + +var request = require('supertest'); +var koa = require('../..'); + +describe('.experimental=true', function () { + it('should support async functions', function (done) { + var app = koa(); + app.experimental = true; + app.use(async function (next) { + var string = await Promise.resolve('asdf'); + this.body = string; + }); + + request(app.callback()) + .get('/') + .expect('asdf') + .expect(200, done); + }) +}) diff --git a/test/experimental/index.js b/test/experimental/index.js new file mode 100644 index 0000000..0e18c8c --- /dev/null +++ b/test/experimental/index.js @@ -0,0 +1,4 @@ +require('6to5/register')({ + optional: ['asyncToGenerator'] +}); +require('./async');