diff --git a/src/core/request-capability.ts b/src/core/request-capability.ts index 1dd9e7b..f69c46a 100644 --- a/src/core/request-capability.ts +++ b/src/core/request-capability.ts @@ -54,13 +54,9 @@ export abstract class RequestCapability * @returns New request processing handler. */ for(handler: RequestHandler): RequestHandler { - return async ({ next, modifiedBy }) => { - if (modifiedBy(this[RequestModifier__symbol])) { - await next(handler); - } else { - await next(handler, this); - } - }; + return async ({ next, modifiedBy }) => modifiedBy(this[RequestModifier__symbol]) + ? next(handler) + : next(handler, this); } /** diff --git a/src/core/request-handler.spec.ts b/src/core/request-handler.spec.ts index 791f54a..92cb0f8 100644 --- a/src/core/request-handler.spec.ts +++ b/src/core/request-handler.spec.ts @@ -45,7 +45,7 @@ describe('requestHandler', () => { const error = new Error('test'); const calls: number[] = []; - const call = async (): Promise => await requestHandler([ + const call = async (): Promise => await requestHandler([ () => { calls.push(1); }, () => { calls.push(2); throw error; }, () => { calls.push(3); }, @@ -57,7 +57,7 @@ describe('requestHandler', () => { it('stops handlers execution once response written', async () => { const calls: number[] = []; - const call = async (): Promise => await requestHandler([ + const call = async (): Promise => await requestHandler([ () => { calls.push(1); }, () => { calls.push(2); (response as any).writableEnded = true; }, () => { calls.push(3); }, diff --git a/src/core/request-handler.ts b/src/core/request-handler.ts index 892add0..84091b6 100644 --- a/src/core/request-handler.ts +++ b/src/core/request-handler.ts @@ -25,7 +25,7 @@ export type RequestHandler = ( this: void, context: RequestContext, - ) => PromiseLike | void; + ) => PromiseLike | void; /** * Request processing method signature. @@ -45,7 +45,7 @@ export type RequestHandlerMethod = ( this: TThis, context: RequestContext, - ) => PromiseLike | void; + ) => PromiseLike | void; /** * Builds a request processing handler that delegates request processing to other handlers. diff --git a/src/core/request-modifier.spec.ts b/src/core/request-modifier.spec.ts index b6123d3..9967c1d 100644 --- a/src/core/request-modifier.spec.ts +++ b/src/core/request-modifier.spec.ts @@ -54,14 +54,10 @@ describe('RequestModifier', () => { expect(JSON.parse(await response.body())).toEqual({ test: 'modified' }); }); it('alters subsequent requests', async () => { - server.listener.mockImplementation(httpListener(Rendering.for(async ({ next }) => { - await next( - async ({ next }) => { - await next(({ renderJson, test }) => renderJson({ test }), { test: 'another' }); - }, - modifier, - ); - }))); + server.listener.mockImplementation(httpListener(Rendering.for(async ({ next }) => next( + ({ next }) => next(({ renderJson, test }) => renderJson({ test }), { test: 'another' }), + modifier, + )))); const response = await server.get('/'); diff --git a/src/http/dispatch/dispatch-by-accepted.handler.ts b/src/http/dispatch/dispatch-by-accepted.handler.ts index b533f70..4bba545 100644 --- a/src/http/dispatch/dispatch-by-accepted.handler.ts +++ b/src/http/dispatch/dispatch-by-accepted.handler.ts @@ -70,9 +70,9 @@ export function dispatchByAccepted( if (handler) { addResponseHeader(response, 'Vary', 'Accept'); - await next(handler.bind(mimeTypes)); - } else { - await next(fallback); + return next(handler.bind(mimeTypes)); } + + return next(fallback); }; } diff --git a/src/http/dispatch/dispatch-by-language.handler.ts b/src/http/dispatch/dispatch-by-language.handler.ts index 55d9cd2..22b6890 100644 --- a/src/http/dispatch/dispatch-by-language.handler.ts +++ b/src/http/dispatch/dispatch-by-language.handler.ts @@ -54,7 +54,7 @@ export function dispatchByLanguage( const negotiator = httpLanguageNegotiator(languages); - return async ({ request, response, next }) => { + return ({ request, response, next }) => { const { 'accept-language': acceptLanguage = '*' } = request.headers; const handler = negotiator(acceptLanguage); @@ -65,6 +65,6 @@ export function dispatchByLanguage( addResponseHeader(response, 'Vary', 'Accept-Language'); - await next(handler.bind(languages)); + return next(handler.bind(languages)); }; } diff --git a/src/http/http-listener.ts b/src/http/http-listener.ts index cf985b2..ba7191d 100644 --- a/src/http/http-listener.ts +++ b/src/http/http-listener.ts @@ -147,7 +147,7 @@ function incomingHttpHandler> = async ({ + const incomingHandler: RequestHandler> = ({ request, response, next, @@ -173,7 +173,7 @@ function incomingHttpHandler( config: HttpConfig>, handler: RequestHandler>, -): - RequestHandler> { +): RequestHandler> { + const defaultHandler = defaultHttpHandler(config); const errorHandler = httpErrorHandler(config); return async ( { next }: RequestContext>, - ): Promise => { + ) => { try { if (!await next(handler)) { await next(defaultHandler); @@ -246,9 +246,9 @@ function httpErrorHandler( const onError = errorHandler === true ? renderHttpError : errorHandler; - return async context => { + return context => { logHttpError(context); - await context.next(onError); + return context.next(onError); }; }