Skip to content

Commit a31b093

Browse files
committed
refactor: handle of performance improvements
1 parent d806f22 commit a31b093

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

npm-audit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h5 class="card-title">
5555
<div class="card">
5656
<div class="card-body">
5757
<h5 class="card-title">
58-
July 22nd 2020, 7:05:03 am
58+
July 22nd 2020, 9:46:15 am
5959
</h5>
6060
<p class="card-text">Last updated</p>
6161
</div>

src/Server/PreCompiler/index.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import haye from 'haye'
1313
import { Middleware } from 'co-compose'
1414
import { Exception } from '@poppinss/utils'
1515
import { RouteNode } from '@ioc:Adonis/Core/Route'
16-
import { MiddlewareStoreContract } from '@ioc:Adonis/Core/Middleware'
1716
import { IocContract, IocResolverContract } from '@adonisjs/fold'
1817
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
18+
import { MiddlewareStoreContract, ResolvedMiddlewareHandler } from '@ioc:Adonis/Core/Middleware'
1919

2020
import { useReturnValue } from '../../helpers'
2121

@@ -31,53 +31,51 @@ export class PreCompiler {
3131
/**
3232
* This function is used by reference to execute the route handler
3333
*/
34-
private finalRouteHandler = async function finalRouteHandler(ctx: HttpContextContract) {
35-
let data: any = {}
36-
37-
let requestProfiler = ctx.profiler
34+
private runRouteHandler = async (ctx: HttpContextContract) => {
35+
const routeHandler = ctx.route!.meta.resolvedHandler!
3836

3937
/*
4038
* Passing a child to the route handler, so that all internal
4139
* actions can have their own child row
4240
*/
43-
ctx.profiler = ctx.profiler.create('http:route:handler', data)
44-
45-
const routeHandler = ctx.route!.meta.resolvedHandler!
4641
let returnValue: any
4742

4843
try {
4944
if (routeHandler.type === 'function') {
5045
returnValue = await routeHandler.handler(ctx)
5146
} else {
52-
data.controller = routeHandler.namespace
53-
data.method = routeHandler.method
5447
returnValue = await this.resolver.call(routeHandler, ctx.route!.meta.namespace, [ctx])
5548
}
5649

5750
if (useReturnValue(returnValue, ctx)) {
5851
ctx.response.send(returnValue)
5952
}
60-
61-
ctx.profiler.end()
62-
ctx.profiler = requestProfiler
6353
} catch (error) {
64-
ctx.profiler.end()
65-
ctx.profiler = requestProfiler
6654
throw error
6755
}
68-
}.bind(this)
56+
}
57+
58+
/**
59+
* Method to execute middleware using the middleware store
60+
*/
61+
private executeMiddleware = (
62+
middleware: ResolvedMiddlewareHandler,
63+
params: [HttpContextContract, () => Promise<void>]
64+
) => {
65+
return this.middlewareStore.invokeMiddleware(middleware, params)
66+
}
6967

7068
/**
7169
* This function is used by reference to execute the route middleware + route handler
7270
*/
73-
private routeMiddlewareHandler = async function routeMiddlewareHandler(ctx: HttpContextContract) {
74-
await new Middleware()
71+
private runRouteMiddleware = (ctx: HttpContextContract) => {
72+
return new Middleware()
7573
.register(ctx.route!.meta.resolvedMiddleware!)
7674
.runner()
77-
.executor(this.middlewareStore.invokeMiddleware.bind(this.middlewareStore))
78-
.finalHandler(this.finalRouteHandler, [ctx])
75+
.executor(this.executeMiddleware)
76+
.finalHandler(this.runRouteHandler, [ctx])
7977
.run([ctx])
80-
}.bind(this)
78+
}
8179

8280
/**
8381
* The resolver used to resolve the controllers from IoC container
@@ -138,9 +136,9 @@ export class PreCompiler {
138136
*/
139137
private setFinalHandler(route: RouteNode) {
140138
if (route.meta.resolvedMiddleware && route.meta.resolvedMiddleware.length) {
141-
route.meta.finalHandler = this.routeMiddlewareHandler
139+
route.meta.finalHandler = this.runRouteMiddleware
142140
} else {
143-
route.meta.finalHandler = this.finalRouteHandler
141+
route.meta.finalHandler = this.runRouteHandler
144142
}
145143
}
146144

src/Server/RequestHandler/index.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@
99

1010
/// <reference path="../../../adonis-typings/index.ts" />
1111

12-
import { Exception } from '@poppinss/utils'
1312
import { Middleware } from 'co-compose'
14-
import { MiddlewareStoreContract } from '@ioc:Adonis/Core/Middleware'
15-
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
13+
import { Exception } from '@poppinss/utils'
1614
import { RouterContract } from '@ioc:Adonis/Core/Route'
15+
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
16+
import { MiddlewareStoreContract, ResolvedMiddlewareHandler } from '@ioc:Adonis/Core/Middleware'
1717

1818
/**
1919
* Handles the request by invoking it's middleware chain, along with the
2020
* route finalHandler
2121
*/
2222
export class RequestHandler {
2323
private globalMiddleware: Middleware
24+
private handleRequest: (ctx: HttpContextContract) => Promise<void>
25+
2426
constructor(private middlewareStore: MiddlewareStoreContract, private router: RouterContract) {}
2527

2628
/**
27-
* Executes the middleware chain, followed by the route handler.
29+
* Function to invoke global middleware
2830
*/
29-
private async invokeHandler(ctx: HttpContextContract) {
30-
await this.globalMiddleware
31-
.runner()
32-
.executor(this.middlewareStore.invokeMiddleware.bind(this.middlewareStore))
33-
.finalHandler(ctx.route!.meta.finalHandler, [ctx])
34-
.run([ctx])
31+
private executeMiddleware = (
32+
middleware: ResolvedMiddlewareHandler,
33+
params: [HttpContextContract, () => Promise<void>]
34+
) => {
35+
return this.middlewareStore.invokeMiddleware(middleware, params)
3536
}
3637

3738
/**
@@ -71,18 +72,27 @@ export class RequestHandler {
7172
*/
7273
public async handle(ctx: HttpContextContract) {
7374
this.findRoute(ctx)
74-
await this.invokeHandler(ctx)
75+
return this.handleRequest(ctx)
7576
}
7677

7778
/**
7879
* Computing certain methods to optimize for runtime performance
7980
*/
8081
public commit() {
8182
const middleware = this.middlewareStore.get()
82-
if (middleware.length) {
83-
this.globalMiddleware = new Middleware().register(middleware)
84-
} else {
85-
this.invokeHandler = async (ctx) => ctx.route!.meta.finalHandler(ctx)
83+
84+
if (!middleware.length) {
85+
this.handleRequest = (ctx) => ctx.route!.meta.finalHandler(ctx)
86+
return
87+
}
88+
89+
this.globalMiddleware = new Middleware().register(middleware)
90+
this.handleRequest = (ctx) => {
91+
return this.globalMiddleware
92+
.runner()
93+
.executor(this.executeMiddleware)
94+
.finalHandler(ctx.route!.meta.finalHandler, [ctx])
95+
.run([ctx])
8696
}
8797
}
8898
}

src/Server/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ export class Server implements ServerContract {
9393
* Start with before hooks upfront. If they raise error
9494
* then execute error handler.
9595
*/
96-
const shortcircuit = await this.hooks.executeBefore(ctx)
97-
if (!shortcircuit) {
98-
return this.requestHandler.handle(ctx)
99-
}
96+
return this.hooks.executeBefore(ctx).then((shortcircuit) => {
97+
if (!shortcircuit) {
98+
return this.requestHandler.handle(ctx)
99+
}
100+
})
100101
}
101102

102103
/**

0 commit comments

Comments
 (0)