Skip to content

Commit 81c1cb9

Browse files
committed
refactor: small improvements in the server
1 parent 31a4441 commit 81c1cb9

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

src/helpers.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { BriskRoute } from './router/brisk.js'
1313
import { RouteGroup } from './router/group.js'
1414
import type { RouteJSON } from './types/route.js'
1515
import { RouteResource } from './router/resource.js'
16-
import type { HttpContext } from './http_context/main.js'
1716

1817
const proxyCache = new Cache({ max: 200 })
1918

@@ -77,15 +76,3 @@ export function trustProxy(
7776
proxyCache.set(remoteAddress, result)
7877
return result
7978
}
80-
81-
/**
82-
* Returns a boolean telling if the return value should be used as
83-
* the response body or not
84-
*/
85-
export function shouldUseReturnValue(returnValue: any, ctx: HttpContext) {
86-
return (
87-
returnValue !== undefined && // Return value is explicitly defined
88-
returnValue !== ctx.response && // Return value is not the instance of response object
89-
!ctx.response.hasLazyBody // Lazy body is not set
90-
)
91-
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @adonisjs/http-server
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import type { HttpContext } from '../../http_context/main.js'
11+
12+
/**
13+
* A factory function that uses the return value of the request
14+
* pipeline as the response
15+
*/
16+
export function useReturnValue(ctx: HttpContext) {
17+
return function (value: any) {
18+
if (
19+
value !== undefined && // Return value is explicitly defined
20+
!ctx.response.hasLazyBody && // Lazy body is not set
21+
value !== ctx.response // Return value is not the instance of response object
22+
) {
23+
ctx.response.send(value)
24+
}
25+
}
26+
}

src/server/main.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import string from '@poppinss/utils/string'
1111
import Middleware from '@poppinss/middleware'
12+
import { RuntimeException } from '@poppinss/utils'
1213
import type Encryption from '@adonisjs/encryption'
1314
import type { ContainerResolver } from '@adonisjs/fold'
1415
import type { Application } from '@adonisjs/application'
@@ -24,11 +25,11 @@ import debug from '../debug.js'
2425
import { Request } from '../request.js'
2526
import { Response } from '../response.js'
2627
import { Router } from '../router/main.js'
27-
import { shouldUseReturnValue } from '../helpers.js'
2828
import { HttpContext } from '../http_context/main.js'
2929
import { MiddlewareStore } from '../middleware/store.js'
3030
import { finalHandler } from './factories/final_handler.js'
3131
import { writeResponse } from './factories/write_response.js'
32+
import { useReturnValue } from './factories/use_return_value.js'
3233
import { asyncLocalStorage } from '../http_context/local_storage.js'
3334
import { middlewareHandler } from './factories/middleware_handler.js'
3435

@@ -177,8 +178,24 @@ export class Server<NamedMiddleware extends Record<string, LazyImport<Middleware
177178
*/
178179
async boot() {
179180
debug('booting HTTP server')
180-
this.router!.commit()
181181

182+
/**
183+
* Ensure middleware are registered
184+
*/
185+
if (!this.router) {
186+
throw new RuntimeException(
187+
'Cannot boot HTTP server. Register middleware using "server.use" first'
188+
)
189+
}
190+
191+
/**
192+
* Commit routes
193+
*/
194+
this.router.commit()
195+
196+
/**
197+
* Register custom error handler
198+
*/
182199
if (this.#errorHandler) {
183200
if (debug.enabled) {
184201
debug('using custom error handler "%s"', this.#errorHandler)
@@ -196,11 +213,7 @@ export class Server<NamedMiddleware extends Record<string, LazyImport<Middleware
196213
return this.#serverMiddlewareStack!.runner()
197214
.finalHandler(finalHandler(this.router!, resolver, ctx))
198215
.run(middlewareHandler(resolver, ctx))
199-
.then((value) => {
200-
if (shouldUseReturnValue(value, ctx)) {
201-
ctx.response.send(value)
202-
}
203-
})
216+
.then(useReturnValue(ctx))
204217
.catch((error) => this.#resolvedErrorHandler.handle(error, ctx))
205218
.finally(writeResponse(ctx))
206219
}

0 commit comments

Comments
 (0)