find-my-way
Advanced tools
Comparing version 8.1.0 to 8.2.0
64
index.js
@@ -28,3 +28,3 @@ 'use strict' | ||
const assert = require('assert') | ||
const assert = require('node:assert') | ||
const querystring = require('fast-querystring') | ||
@@ -119,3 +119,3 @@ const isRegexSafe = require('safe-regex2') | ||
const pathFull = path.replace(OPTIONAL_PARAM_REGEXP, '$1$2') | ||
const pathOptional = path.replace(OPTIONAL_PARAM_REGEXP, '$2') | ||
const pathOptional = path.replace(OPTIONAL_PARAM_REGEXP, '$2') || '/' | ||
@@ -349,3 +349,2 @@ this.on(method, pathFull, opts, handler, store) | ||
const isEndOfNode = charCode === 47 || j === pattern.length | ||
if (isRegexParam || isStaticPart || isEndOfNode) { | ||
@@ -413,5 +412,3 @@ const paramName = pattern.slice(lastParamStartIndex, j) | ||
currentNode = currentNode.getWildcardChild() | ||
if (currentNode === null) { | ||
return null | ||
} | ||
parentNodePathIndex = i + 1 | ||
@@ -429,6 +426,2 @@ | ||
if (pattern === '*') { | ||
pattern = '/*' | ||
} | ||
for (const existRoute of this.routes) { | ||
@@ -444,3 +437,3 @@ const routeConstraints = existRoute.opts.constraints || {} | ||
store: existRoute.store, | ||
params: existRoute.params || [] | ||
params: existRoute.params | ||
} | ||
@@ -651,33 +644,32 @@ } | ||
if (currentNode.kind === NODE_TYPES.PARAMETRIC) { | ||
let paramEndIndex = originPath.indexOf('/', pathIndex) | ||
if (paramEndIndex === -1) { | ||
paramEndIndex = pathLen | ||
} | ||
// parametric node | ||
let paramEndIndex = originPath.indexOf('/', pathIndex) | ||
if (paramEndIndex === -1) { | ||
paramEndIndex = pathLen | ||
} | ||
let param = originPath.slice(pathIndex, paramEndIndex) | ||
if (shouldDecodeParam) { | ||
param = safeDecodeURIComponent(param) | ||
} | ||
let param = originPath.slice(pathIndex, paramEndIndex) | ||
if (shouldDecodeParam) { | ||
param = safeDecodeURIComponent(param) | ||
} | ||
if (currentNode.isRegex) { | ||
const matchedParameters = currentNode.regex.exec(param) | ||
if (matchedParameters === null) continue | ||
if (currentNode.isRegex) { | ||
const matchedParameters = currentNode.regex.exec(param) | ||
if (matchedParameters === null) continue | ||
for (let i = 1; i < matchedParameters.length; i++) { | ||
const matchedParam = matchedParameters[i] | ||
if (matchedParam.length > maxParamLength) { | ||
return null | ||
} | ||
params.push(matchedParam) | ||
} | ||
} else { | ||
if (param.length > maxParamLength) { | ||
for (let i = 1; i < matchedParameters.length; i++) { | ||
const matchedParam = matchedParameters[i] | ||
if (matchedParam.length > maxParamLength) { | ||
return null | ||
} | ||
params.push(param) | ||
params.push(matchedParam) | ||
} | ||
} else { | ||
if (param.length > maxParamLength) { | ||
return null | ||
} | ||
params.push(param) | ||
} | ||
pathIndex = paramEndIndex | ||
} | ||
pathIndex = paramEndIndex | ||
} | ||
@@ -752,4 +744,2 @@ } | ||
if (Router.prototype[methodName]) throw new Error('Method already exists: ' + methodName) | ||
Router.prototype[methodName] = function (path, handler, store) { | ||
@@ -756,0 +746,0 @@ return this.on(m, path, handler, store) |
@@ -5,3 +5,3 @@ 'use strict' | ||
const acceptHostStrategy = require('./strategies/accept-host') | ||
const assert = require('assert') | ||
const assert = require('node:assert') | ||
@@ -157,6 +157,4 @@ class Constrainer { | ||
lines.push(' version: req.headers[\'accept-version\'],') | ||
} else if (key === 'host') { | ||
} else { | ||
lines.push(' host: req.headers.host || req.headers[\':authority\'],') | ||
} else { | ||
throw new Error('unknown non-custom strategy for compiling constraint derivation function') | ||
} | ||
@@ -163,0 +161,0 @@ } else { |
@@ -50,4 +50,4 @@ 'use strict' | ||
const isMergedTree = constraintsNames.includes(httpMethodStrategy.name) | ||
if (!isMergedTree && this.handlers.length >= 32) { | ||
throw new Error('find-my-way supports a maximum of 32 route handlers per node when there are constraints, limit reached') | ||
if (!isMergedTree && this.handlers.length >= 31) { | ||
throw new Error('find-my-way supports a maximum of 31 route handlers per node when there are constraints, limit reached') | ||
} | ||
@@ -54,0 +54,0 @@ |
@@ -132,6 +132,3 @@ 'use strict' | ||
getWildcardChild () { | ||
if (this.wildcardChild) { | ||
return this.wildcardChild | ||
} | ||
return null | ||
return this.wildcardChild | ||
} | ||
@@ -138,0 +135,0 @@ |
'use strict' | ||
const assert = require('assert') | ||
const assert = require('node:assert') | ||
@@ -4,0 +4,0 @@ function HostStorage () { |
'use strict' | ||
const assert = require('assert') | ||
const assert = require('node:assert') | ||
@@ -23,3 +23,7 @@ function SemVerStore () { | ||
major = Number(major) || 0 | ||
if (isNaN(major)) { | ||
throw new TypeError('Major version must be a numeric value') | ||
} | ||
major = Number(major) | ||
minor = Number(minor) || 0 | ||
@@ -42,3 +46,3 @@ patch = Number(patch) || 0 | ||
if (patch >= (this.store[`${major}.${minor}`] || 0)) { | ||
if (patch >= (this.maxPatches[`${major}.${minor}`] || 0)) { | ||
this.maxPatches[`${major}.${minor}`] = patch | ||
@@ -45,0 +49,0 @@ this.store[`${major}.${minor}.x`] = store |
@@ -12,7 +12,4 @@ 'use strict' | ||
}, | ||
deriveConstraint: (req) => { | ||
/* istanbul ignore next */ | ||
return req.method | ||
}, | ||
deriveConstraint: /* istanbul ignore next */ (req) => req.method, | ||
mustMatchWhenDerived: true | ||
} |
{ | ||
"name": "find-my-way", | ||
"version": "8.1.0", | ||
"version": "8.2.0", | ||
"description": "Crazy fast http radix based router", | ||
@@ -43,2 +43,3 @@ "main": "index.js", | ||
"pre-commit": "^1.2.2", | ||
"proxyquire": "^2.1.3", | ||
"rfdc": "^1.3.0", | ||
@@ -54,3 +55,3 @@ "simple-git": "^3.7.1", | ||
"fast-querystring": "^1.0.0", | ||
"safe-regex2": "^2.0.0" | ||
"safe-regex2": "^3.1.0" | ||
}, | ||
@@ -57,0 +58,0 @@ "tsd": { |
@@ -446,4 +446,4 @@ # find-my-way | ||
```js | ||
#### lookup(request, response, [context], [done]) | ||
Start a new search, `request` and `response` are the server req/res objects.<br> | ||
@@ -450,0 +450,0 @@ If a route is found it will automatically call the handler, otherwise the default route will be called.<br> |
@@ -6,2 +6,3 @@ 'use strict' | ||
const FindMyWay = require('..') | ||
const rfdc = require('rfdc')({ proto: true }) | ||
@@ -27,8 +28,11 @@ const customHeaderConstraint = { | ||
test('should derive async constraint', t => { | ||
test('should derive multiple async constraints', t => { | ||
t.plan(2) | ||
const router = FindMyWay({ constraints: { requestedBy: customHeaderConstraint } }) | ||
router.on('GET', '/', { constraints: { requestedBy: 'node' } }, () => 'asyncHandler') | ||
const customHeaderConstraint2 = rfdc(customHeaderConstraint) | ||
customHeaderConstraint2.name = 'requestedBy2' | ||
const router = FindMyWay({ constraints: { requestedBy: customHeaderConstraint, requestedBy2: customHeaderConstraint2 } }) | ||
router.on('GET', '/', { constraints: { requestedBy: 'node', requestedBy2: 'node' } }, () => 'asyncHandler') | ||
router.lookup( | ||
@@ -35,0 +39,0 @@ { |
@@ -77,1 +77,30 @@ 'use strict' | ||
}) | ||
test('A route supports up to 31 host constraints', (t) => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay() | ||
for (let i = 0; i < 31; i++) { | ||
const host = `h${i.toString().padStart(2, '0')}` | ||
findMyWay.on('GET', '/', { constraints: { host } }, alpha) | ||
} | ||
t.equal(findMyWay.find('GET', '/', { host: 'h01' }).handler, alpha) | ||
}) | ||
test('A route throws when constraint limit exceeded', (t) => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay() | ||
for (let i = 0; i < 31; i++) { | ||
const host = `h${i.toString().padStart(2, '0')}` | ||
findMyWay.on('GET', '/', { constraints: { host } }, alpha) | ||
} | ||
t.throws( | ||
() => findMyWay.on('GET', '/', { constraints: { host: 'h31' } }, beta), | ||
'find-my-way supports a maximum of 31 route handlers per node when there are constraints, limit reached' | ||
) | ||
}) |
@@ -198,1 +198,21 @@ 'use strict' | ||
}) | ||
test('optional parameter on root', (t) => { | ||
t.plan(2) | ||
const findMyWay = FindMyWay({ | ||
defaultRoute: (req, res) => { | ||
t.fail('Should not be defaultRoute') | ||
} | ||
}) | ||
findMyWay.on('GET', '/:optional?', (req, res, params) => { | ||
if (params.optional) { | ||
t.equal(params.optional, 'foo') | ||
} else { | ||
t.equal(params.optional, undefined) | ||
} | ||
}) | ||
findMyWay.lookup({ method: 'GET', url: '/', headers: {} }, null) | ||
findMyWay.lookup({ method: 'GET', url: '/foo', headers: {} }, null) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
384117
92
9549
12
+ Addedret@0.4.3(transitive)
+ Addedsafe-regex2@3.1.0(transitive)
- Removedret@0.2.2(transitive)
- Removedsafe-regex2@2.0.0(transitive)
Updatedsafe-regex2@^3.1.0