Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Add configuration option for defer
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored and fusionjs-bot[bot] committed Apr 19, 2019
1 parent d99885a commit 08d5fe9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ export default function main() {
return app;
}
```

### Configuration

You can configure whether to run the middleware before or after await next. The default is running after await next.

```js
import {HttpHandlerConfigToken} from 'fusion-plugin-http-handler';

// Configure to run before await next
if (__NODE__) {
app.register(HttpHandlerConfigToken, {defer: false});
}
```
44 changes: 43 additions & 1 deletion src/__tests__/express-get.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import test from 'tape-cup';
import App from 'fusion-core';
import express from 'express';
import {HttpHandlerToken} from '../tokens.js';
import {HttpHandlerToken, HttpHandlerConfigToken} from '../tokens.js';
import HttpHandlerPlugin from '../server.js';
import {startServer} from '../test-util.js';

Expand Down Expand Up @@ -53,3 +53,45 @@ test('http handler with express', async t => {
server.close();
t.end();
});

test('http handler with express and defer false', async t => {
const app = new App('test', () => 'test');
app.middleware((ctx, next) => {
if (ctx.url === '/send') {
ctx.body = 'hello world';
}
return next();
});
app.register(HttpHandlerPlugin);
app.register(HttpHandlerConfigToken, {defer: false});
let hitExpressMiddleware = false;
const expressApp = express();
expressApp.use((req, res, next) => {
hitExpressMiddleware = true;
return next();
});
expressApp.get('/lol', (req, res) => {
res.status(200);
res.end('OK');
});

app.register(HttpHandlerToken, expressApp);

const {server, request} = await startServer(app.callback());

t.equal(
await request('/send'),
'hello world',
'can send response from koa middleware'
);
t.equal(
hitExpressMiddleware,
false,
'does not run through express middleware if response is sent by koa'
);

t.equal(await request('/lol'), 'OK', 'express routes can send responses');
t.equal(hitExpressMiddleware, true, 'express middleware hit');
server.close();
t.end();
});
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ import server from './server';
const plugin = __BROWSER__ ? browser : server;

export default plugin;
export {HttpHandlerToken} from './tokens.js';
export {HttpHandlerToken, HttpHandlerConfigToken} from './tokens.js';
20 changes: 15 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@

import {createPlugin} from 'fusion-core';
import type {FusionPlugin} from 'fusion-core';
import {HttpHandlerToken} from './tokens.js';
import {HttpHandlerToken, HttpHandlerConfigToken} from './tokens.js';
import type {DepsType} from './types.js';

const defaultConfig = {
defer: true,
};

const plugin =
__NODE__ &&
createPlugin({
deps: {
handler: HttpHandlerToken,
config: HttpHandlerConfigToken.optional,
},

middleware: deps => {
const {handler} = deps;
const {handler, config = defaultConfig} = deps;
return async (ctx, next) => {
await next();
if (config.defer) {
await next();
}
if (ctx.body) {
return;
return config.defer || next();
}
return new Promise((resolve, reject) => {
const {req, res} = ctx;
Expand Down Expand Up @@ -69,6 +75,10 @@ const plugin =
}
return resolve();
}
}).then(() => {
if (!config.defer) {
return next();
}
});
};
},
Expand Down
5 changes: 4 additions & 1 deletion src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import {createToken} from 'fusion-core';
import type {Token} from 'fusion-core';
import type {ServiceType} from './types.js';
import type {ServiceType, ConfigType} from './types.js';

export const HttpHandlerToken: Token<ServiceType> = createToken('HttpHandler');
export const HttpHandlerConfigToken: Token<ConfigType> = createToken(
'HttpHandlerConfigToken'
);
4 changes: 4 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export type DepsType = {
};

export type ServiceType = (mixed, mixed, (error?: any) => Promise<any>) => void;

export type ConfigType = {
defer: boolean,
};

0 comments on commit 08d5fe9

Please sign in to comment.