From 5e372304a92805ff1258a67b0d4869e1e3a667ef Mon Sep 17 00:00:00 2001 From: shfshanyue Date: Thu, 26 Aug 2021 00:19:07 +0800 Subject: [PATCH] chore: allow the compose function to be configurable --- __tests__/application/compose.js | 71 ++++++++++++++++++++++++++++++++ __tests__/application/index.js | 6 +++ lib/application.js | 3 +- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 __tests__/application/compose.js diff --git a/__tests__/application/compose.js b/__tests__/application/compose.js new file mode 100644 index 000000000..7fa3fea69 --- /dev/null +++ b/__tests__/application/compose.js @@ -0,0 +1,71 @@ + +'use strict' + +const request = require('supertest') +const assert = require('assert') +const Koa = require('../..') + +describe('app.compose', () => { + it('should work with default compose ', async () => { + const app = new Koa() + const calls = [] + + app.use((ctx, next) => { + calls.push(1) + return next().then(() => { + calls.push(4) + }) + }) + + app.use((ctx, next) => { + calls.push(2) + return next().then(() => { + calls.push(3) + }) + }) + + const server = app.listen() + + await request(server) + .get('/') + .expect(404) + + assert.deepStrictEqual(calls, [1, 2, 3, 4]) + }) + + it('should work with configurable compose', async () => { + const calls = [] + let count = 0 + const app = new Koa({ + compose (fns){ + return async (ctx) => { + const dispatch = async () => { + count++ + const fn = fns.shift() + fn && fn(ctx, dispatch) + } + dispatch() + } + } + }) + + app.use((ctx, next) => { + calls.push(1) + next() + calls.push(4) + }) + app.use((ctx, next) => { + calls.push(2) + next() + calls.push(3) + }) + + const server = app.listen() + + await request(server) + .get('/') + + assert.deepStrictEqual(calls, [1, 2, 3, 4]) + assert.equal(count, 3) + }) +}) diff --git a/__tests__/application/index.js b/__tests__/application/index.js index 4470ffb85..15e15f348 100644 --- a/__tests__/application/index.js +++ b/__tests__/application/index.js @@ -78,6 +78,12 @@ describe('app', () => { assert.strictEqual(app.subdomainOffset, subdomainOffset) }) + it('should set compose from the constructor', () => { + const compose = () => (ctx) => {} + const app = new Koa({ compose }) + assert.strictEqual(app.compose, compose) + }) + it('should have a static property exporting `HttpError` from http-errors library', () => { const CreateError = require('http-errors') diff --git a/lib/application.js b/lib/application.js index f30045466..37bad5db4 100644 --- a/lib/application.js +++ b/lib/application.js @@ -51,6 +51,7 @@ module.exports = class Application extends Emitter { this.proxyIpHeader = options.proxyIpHeader || 'X-Forwarded-For' this.maxIpsCount = options.maxIpsCount || 0 this.env = options.env || process.env.NODE_ENV || 'development' + this.compose = options.compose || compose if (options.keys) this.keys = options.keys this.middleware = [] this.context = Object.create(context) @@ -132,7 +133,7 @@ module.exports = class Application extends Emitter { */ callback () { - const fn = compose(this.middleware) + const fn = this.compose(this.middleware) if (!this.listenerCount('error')) this.on('error', this.onerror)