Skip to content

Commit bad9443

Browse files
committed
fix: ensure server is booted only once and routes are committed once
1 parent 6473391 commit bad9443

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/router/main.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import type {
3535
MakeSignedUrlOptions,
3636
GetControllerHandlers,
3737
} from '../types/route.js'
38+
import debug from '../debug.js'
3839
import { parseRoutePattern } from './parser.js'
3940

4041
/**
@@ -50,6 +51,8 @@ import { parseRoutePattern } from './parser.js'
5051
* ```
5152
*/
5253
export class Router extends LookupStore {
54+
#commited: boolean = false
55+
5356
/**
5457
* Application is needed to resolve string based controller expressions
5558
*/
@@ -93,6 +96,15 @@ export class Router extends LookupStore {
9396
*/
9497
matchers = new Matchers()
9598

99+
/**
100+
* Check if routes have been committed to the store. Once
101+
* routes are committed, defining new set of routes will
102+
* have no impact
103+
*/
104+
get commited() {
105+
return this.#commited
106+
}
107+
96108
constructor(app: Application<any>, encryption: Encryption, qsParser: Qs) {
97109
super(encryption, qsParser)
98110
this.#app = app
@@ -328,6 +340,11 @@ export class Router extends LookupStore {
328340
* commit method is called.
329341
*/
330342
commit() {
343+
if (this.#commited) {
344+
return
345+
}
346+
347+
debug('Committing routes to the routes store')
331348
const routeNamesByDomain: Map<string, Set<string>> = new Map()
332349

333350
toRoutesJSON(this.routes).forEach((route) => {
@@ -367,6 +384,7 @@ export class Router extends LookupStore {
367384
this.routes = []
368385
this.#globalMatchers = {}
369386
this.#middleware = []
387+
this.#commited = true
370388
}
371389

372390
/**

src/server/main.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import { middlewareHandler } from './factories/middleware_handler.js'
4343
* registered routes.
4444
*/
4545
export class Server {
46+
#booted: boolean = false
47+
4648
/**
4749
* The default error handler to use
4850
*/
@@ -129,6 +131,13 @@ export class Server {
129131
return this.#resolvedErrorHandler.handle(error, ctx)
130132
}
131133

134+
/**
135+
* Check if the server has already been booted
136+
*/
137+
get booted() {
138+
return this.#booted
139+
}
140+
132141
/**
133142
* Know if async local storage is enabled or not.
134143
*/
@@ -250,6 +259,10 @@ export class Server {
250259
* - Resolve and construct the error handler.
251260
*/
252261
async boot() {
262+
if (this.#booted) {
263+
return
264+
}
265+
253266
debug('booting HTTP server')
254267

255268
/**
@@ -273,6 +286,8 @@ export class Server {
273286
const moduleExports = await this.#errorHandler()
274287
this.#resolvedErrorHandler = await this.#app.container.make(moduleExports.default)
275288
}
289+
290+
this.#booted = true
276291
}
277292

278293
/**

0 commit comments

Comments
 (0)