Skip to content

Commit 9384f3a

Browse files
Multiple issues fixed
1 parent d299a7d commit 9384f3a

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

src/Router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Router {
5151
// Write the route handler and route options object with fallback to the default options
5252
const handler = callbacks.pop()
5353
options = options || {
54-
middlewares: method === 'any' ? undefined : new Map()
54+
middlewares: new Map()
5555
}
5656

5757
if (Array.isArray(options.middlewares)) options.middlewares = new Map(options.middlewares.map((middleware, index) => [index, middleware]))
@@ -63,7 +63,7 @@ class Router {
6363
const record = {
6464
method,
6565
pattern,
66-
options: new Map(Object.entries(options)),
66+
options: options instanceof Map ? options : new Map(Object.entries(options)),
6767
handler
6868
}
6969

src/Server.js

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ class Server extends Router {
150150
listen (port, host = '127.0.0.1') {
151151
// Adding not found handler (404)
152152
this.any('/*', (request, response) => this.handlers.get('on_not_found')(request, response))
153+
// Lock routes modification
153154
this._routes_locked = true
155+
// Attach middleware
156+
this._attach_middlewares()
154157

155158
return new Promise((resolve, reject) =>
156159
this.uws_instance.listen(host, port, (listenSocket) => {
@@ -235,30 +238,15 @@ class Server extends Router {
235238
*/
236239
_create_route (record) {
237240
// Do not allow route creation once it is locked after a not found handler has been bound
238-
if (this._routes_locked === true) {
239-
throw new Error(`Routes/Routers must not be created or used after the set_not_found_handler() has been set due to uWebsockets.js's internal router not allowing for this to occur. [${record.method.toUpperCase()} ${record.pattern}]`)
241+
if (this._routes_locked) {
242+
throw new Error(`Routes/Routers can not be created when server is started. [${record.method.toUpperCase()} ${record.pattern}]`)
240243
}
241244

242245
// Do not allow duplicate routes for performance/stability reasons
243-
if (this._routes.get(record.method).get(record.pattern)) throw new Error(`Failed to create route as duplicate routes are not allowed. Ensure that you do not have any routers or routes that try to handle requests at the same pattern. [${record.method.toUpperCase()} ${record.pattern}]`)
244-
245-
// Process and combine middlewares for routes that support middlewares
246-
// Initialize route-specific middlewares if they do not exist
247-
if (!(record.options.get('middlewares') instanceof Map)) record.options.set('middlewares', new Map())
248-
249-
// Parse middlewares that apply to this route based on execution pattern
250-
this._middlewares.forEach((middleware, pattern) => {
251-
if (pattern !== '__GLOBAL__' && pattern !== '/' && record.pattern.startsWith(pattern)) middleware.forEach((object) => record.options.get('middlewares').set(record.options.get('middlewares').size, object))
252-
})
253-
254-
// Map all user specified route specific middlewares with a priority of 2 + combine matched middlewares with route middlewares
255-
record.options.get('middlewares').forEach((middleware, index) => record.options.get('middlewares').set(index, {
256-
priority: 2,
257-
...middleware
258-
}))
246+
if (this._routes.get(record.method).has(record.pattern)) throw new Error(`Failed to create route as duplicate routes are not allowed. Ensure that you do not have any routers or routes that try to handle requests at the same pattern. [${record.method.toUpperCase()} ${record.pattern}]`)
259247

260248
const route = new Route({
261-
app: this,
249+
appOptions: this._options,
262250
method: record.method,
263251
pattern: record.pattern,
264252
options: record.options,
@@ -311,6 +299,10 @@ class Server extends Router {
311299
* @param {Object} record
312300
*/
313301
_create_middleware (record) {
302+
// Do not allow middleware creation once routes is locked
303+
if (this._routes_locked) {
304+
throw new Error(`Middlewares can not be created when server is started. [${record.pattern}]`)
305+
}
314306
// Initialize middlewares array for specified pattern
315307
if (!this._middlewares.has(record.pattern)) this._middlewares.set(record.pattern, new Map())
316308

@@ -322,31 +314,20 @@ class Server extends Router {
322314

323315
// Store middleware object in its pattern branch
324316
this._middlewares.get(record.pattern).set(this._middlewares.get(record.pattern).size, map)
317+
}
325318

326-
// Inject middleware into all routes that match its execution pattern if it is non global
327-
if (map.get('priority') !== 0) {
328-
this._routes.forEach((method) => {
329-
method.forEach((route, pattern) => {
330-
if ((record.pattern === '/' && pattern === record.pattern) || (record.pattern !== '/' && pattern.startsWith(record.pattern))) route.use(map)
331-
})
332-
})
333-
}
334-
/* const object = {
335-
priority: record.pattern === '__GLOBAL__' ? 0 : 1, // 0 priority are global middlewares
336-
middleware: record.middleware
337-
}
338-
339-
// Store middleware object in its pattern branch
340-
this._middlewares.get(record.pattern).set(this._middlewares.get(record.pattern).size, object)
341-
342-
// Inject middleware into all routes that match its execution pattern if it is non global
343-
if (object.priority !== 0) {
344-
this._routes.forEach((method) => {
345-
method.forEach((route, pattern) => {
346-
if ((record.pattern === '/' && pattern === record.pattern) || (record.pattern !== '/' && pattern.startsWith(record.pattern))) route.use(object)
347-
})
319+
_attach_middlewares () {
320+
this._middlewares.forEach((middlewares, middlewarePattern) => {
321+
middlewares.forEach((middleware) => {
322+
if (middleware.get('priority') !== 0) {
323+
this._routes.forEach((method) => {
324+
method.forEach((route, routePattern) => {
325+
if ((middlewarePattern === '/' && routePattern === middlewarePattern) || (middlewarePattern !== '/' && routePattern.startsWith(middlewarePattern))) route.use(middleware)
326+
})
327+
})
328+
}
348329
})
349-
} */
330+
})
350331
}
351332

352333
/* uWS -> Server Request/Response Handling Logic */

src/helpers/accepts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Accepts.prototype.types = function (types_) {
9797
}
9898

9999
// no accept header, return first given type
100-
if (!this.headers.accept) {
100+
if (!this.headers.has('accept')) {
101101
return types[0]
102102
}
103103

0 commit comments

Comments
 (0)