find-my-way
Advanced tools
Comparing version 1.10.1 to 1.10.2
43
index.js
@@ -28,2 +28,4 @@ 'use strict' | ||
this.defaultRoute = opts.defaultRoute | ||
} else { | ||
this.defaultRoute = null | ||
} | ||
@@ -155,3 +157,9 @@ | ||
// split the node in the radix tree and add it to the parent | ||
node = new Node(prefix.slice(len), currentNode.children, currentNode.kind, Object.assign({}, currentNode.map), currentNode.regex) | ||
node = new Node( | ||
prefix.slice(len), | ||
currentNode.children, | ||
currentNode.kind, | ||
new Node.Handlers(currentNode.handlers), | ||
currentNode.regex | ||
) | ||
if (currentNode.wildcardChild !== null) { | ||
@@ -166,3 +174,3 @@ node.wildcardChild = currentNode.wildcardChild | ||
currentNode.label = currentNode.prefix[0] | ||
currentNode.map = {} | ||
currentNode.handlers = new Node.Handlers() | ||
currentNode.kind = NODE_TYPES.STATIC | ||
@@ -259,3 +267,3 @@ currentNode.regex = null | ||
var handle = this.find(req.method, sanitizeUrl(req.url)) | ||
if (!handle) return this._defaultRoute(req, res) | ||
if (handle === null) return this._defaultRoute(req, res) | ||
return handle.handler(req, res, handle.params, handle.store) | ||
@@ -274,2 +282,3 @@ } | ||
var i = 0 | ||
var isStatic = true | ||
@@ -285,4 +294,4 @@ while (true) { | ||
if (pathLen === 0 || path === prefix) { | ||
var handle = currentNode.getHandler(method) | ||
if (handle !== undefined) { | ||
var handle = currentNode.handlers[method] | ||
if (handle !== null) { | ||
var paramsObj = {} | ||
@@ -309,3 +318,3 @@ if (handle.paramsLength > 0) { | ||
if (len === prefixLen) { | ||
if ((isStatic === true && len > 0) || len === prefixLen) { | ||
path = path.slice(len) | ||
@@ -342,2 +351,4 @@ pathLen = path.length | ||
isStatic = false | ||
// if exist, save the wildcard child | ||
@@ -356,3 +367,3 @@ if (currentNode.wildcardChild !== null) { | ||
decoded = fastDecode(path.slice(0, i)) | ||
if (errored) { | ||
if (errored === true) { | ||
return null | ||
@@ -368,3 +379,3 @@ } | ||
decoded = fastDecode(path) | ||
if (errored) { | ||
if (errored === true) { | ||
return null | ||
@@ -385,3 +396,3 @@ } | ||
decoded = fastDecode(path.slice(0, i)) | ||
if (errored) { | ||
if (errored === true) { | ||
return null | ||
@@ -399,5 +410,5 @@ } | ||
i = 0 | ||
if (node.regex) { | ||
if (node.regex !== null) { | ||
var matchedParameter = path.match(node.regex) | ||
if (!matchedParameter) return null | ||
if (matchedParameter === null) return null | ||
i = matchedParameter[1].length | ||
@@ -409,3 +420,3 @@ } else { | ||
decoded = fastDecode(path.slice(0, i)) | ||
if (errored) { | ||
if (errored === true) { | ||
return null | ||
@@ -423,3 +434,3 @@ } | ||
Router.prototype._defaultRoute = function (req, res) { | ||
if (this.defaultRoute) { | ||
if (this.defaultRoute !== null) { | ||
this.defaultRoute(req, res) | ||
@@ -500,7 +511,7 @@ } else { | ||
var decoded = fastDecode(path.slice(-len)) | ||
if (errored) { | ||
if (errored === true) { | ||
return null | ||
} | ||
var handle = node.getHandler(method) | ||
if (handle !== undefined) { | ||
var handle = node.handlers[method] | ||
if (handle !== null) { | ||
return { | ||
@@ -507,0 +518,0 @@ handler: handle.handler, |
30
node.js
@@ -12,3 +12,3 @@ 'use strict' | ||
function Node (prefix, children, kind, map, regex) { | ||
function Node (prefix, children, kind, handlers, regex) { | ||
this.prefix = prefix || '/' | ||
@@ -19,3 +19,3 @@ this.label = this.prefix[0] | ||
this.kind = kind || this.types.STATIC | ||
this.map = map || {} | ||
this.handlers = handlers || new Handlers() | ||
this.regex = regex || null | ||
@@ -70,3 +70,3 @@ this.wildcardChild = null | ||
var child = this.children[i] | ||
if (child.numberOfChildren !== 0 || child.map[method]) { | ||
if (child.numberOfChildren !== 0 || child.handlers[method] !== null) { | ||
if (child.label === label && child.kind === 0) { | ||
@@ -84,3 +84,3 @@ return child | ||
this.map[method] = { | ||
this.handlers[method] = { | ||
handler: handler, | ||
@@ -94,3 +94,3 @@ params: params, | ||
Node.prototype.getHandler = function (method) { | ||
return this.map[method] | ||
return this.handlers[method] | ||
} | ||
@@ -100,8 +100,8 @@ | ||
var paramName = '' | ||
var map = this.map || {} | ||
var methods = Object.keys(map).filter(method => map[method].handler) | ||
var handlers = this.handlers || {} | ||
var methods = Object.keys(handlers).filter(method => handlers[method] && handlers[method].handler) | ||
if (this.prefix === ':') { | ||
methods.forEach((method, index) => { | ||
var params = this.map[method].params | ||
var params = this.handlers[method].params | ||
var param = params[params.length - 1] | ||
@@ -135,2 +135,16 @@ if (methods.length > 1) { | ||
function Handlers (handlers) { | ||
handlers = handlers || {} | ||
this.DELETE = handlers.DELETE || null | ||
this.GET = handlers.GET || null | ||
this.HEAD = handlers.HEAD || null | ||
this.PATCH = handlers.PATCH || null | ||
this.POST = handlers.POST || null | ||
this.PUT = handlers.PUT || null | ||
this.OPTIONS = handlers.OPTIONS || null | ||
this.TRACE = handlers.TRACE || null | ||
this.CONNECT = handlers.CONNECT || null | ||
} | ||
module.exports = Node | ||
module.exports.Handlers = Handlers |
{ | ||
"name": "find-my-way", | ||
"version": "1.10.1", | ||
"version": "1.10.2", | ||
"description": "Crazy fast http radix based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -166,22 +166,2 @@ # find-my-way | ||
##### Caveats | ||
* Since *static* routes have greater priority than *parametric* routes, when you register a static route and a dynamic route, which have part of their path equal, the static route shadows the parametric route, that becomes not accessible. For example: | ||
```js | ||
const assert = require('assert') | ||
const router = require('find-my-way')({ | ||
defaultRoute: (req, res) => { | ||
assert(req.url === '/example/shared/nested/oops') | ||
} | ||
}) | ||
router.on('GET', '/example/shared/nested/test', (req, res, params) => { | ||
assert.fail('We should not be here') | ||
}) | ||
router.on('GET', '/example/:param/nested/oops', (req, res, params) => { | ||
assert.fail('We should not be here') | ||
}) | ||
router.lookup({ method: 'GET', url: '/example/shared/nested/oops' }, null) | ||
``` | ||
* It's not possible to register two routes which differs only for their parameters, because internally they would be seen as the same route. In a such case you'll get an early error during the route registration phase. An example is worth thousand words: | ||
@@ -188,0 +168,0 @@ ```js |
@@ -64,2 +64,21 @@ 'use strict' | ||
test('Parametric and static with shared prefix and different suffix', t => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay({ | ||
defaultRoute: (req, res) => { | ||
t.fail('We should not be here') | ||
} | ||
}) | ||
findMyWay.on('GET', '/example/shared/nested/test', (req, res, params) => { | ||
t.fail('We should not be here') | ||
}) | ||
findMyWay.on('GET', '/example/:param/nested/other', (req, res, params) => { | ||
t.ok('We should be here') | ||
}) | ||
findMyWay.lookup({ method: 'GET', url: '/example/shared/nested/other' }, null) | ||
}) | ||
test('Parametric and static with shared prefix (with wildcard)', t => { | ||
@@ -66,0 +85,0 @@ t.plan(1) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105629
24
3106
244