Skip to content

Commit

Permalink
Merge pull request #54 from delvedor/max-param-length
Browse files Browse the repository at this point in the history
Added maxParamLength option
  • Loading branch information
delvedor committed Jan 28, 2018
2 parents 182868f + b18729b commit 5f08928
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ function handler (req, res, params) {
router.on('GET', '/foo/', handler)
```

You can set a custom length for parameters in parametric *(standard, regex and multi)* routes by using `maxParamLength` option, the default value is 100 characters.<br/>
*If the maximum length limit is reached, the default route will be invoked.*
```js
const router = require('find-my-way')({
maxParamLength: 500
})
```
<a name="on"></a>
#### on(method, path, handler, [store])
Register a new route.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function Router (opts) {
}

this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false
this.maxParamLength = opts.maxParamLength || 100
this.tree = new Node()
this.routes = []
}
Expand Down Expand Up @@ -264,6 +265,7 @@ Router.prototype.lookup = function lookup (req, res) {
}

Router.prototype.find = function find (method, path) {
var maxParamLength = this.maxParamLength
var currentNode = this.tree
var wildcardNode = null
var pathLenWildcard = 0
Expand Down Expand Up @@ -348,6 +350,7 @@ Router.prototype.find = function find (method, path) {
currentNode = node
i = 0
while (i < pathLen && path.charCodeAt(i) !== 47) i++
if (i > maxParamLength) return null
decoded = fastDecode(path.slice(0, i))
if (errored) {
return null
Expand All @@ -374,6 +377,7 @@ Router.prototype.find = function find (method, path) {
currentNode = node
i = 0
while (i < pathLen && path.charCodeAt(i) !== 47) i++
if (i > maxParamLength) return null
decoded = fastDecode(path.slice(0, i))
if (errored) {
return null
Expand All @@ -394,6 +398,7 @@ Router.prototype.find = function find (method, path) {
i = matchedParameter[1].length
} else {
while (i < pathLen && path.charCodeAt(i) !== 47 && path.charCodeAt(i) !== 45) i++
if (i > maxParamLength) return null
}
decoded = fastDecode(path.slice(0, i))
if (errored) {
Expand Down
45 changes: 45 additions & 0 deletions test/max-param-length.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

const t = require('tap')
const test = t.test
const FindMyWay = require('../')

test('maxParamLength default value is 500', t => {
t.plan(1)

const findMyWay = FindMyWay()
t.strictEqual(findMyWay.maxParamLength, 100)
})

test('maxParamLength should set the maximum length for a parametric route', t => {
t.plan(1)

const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param', () => {})
t.deepEqual(findMyWay.find('GET', '/test/123456789abcd'), null)
})

test('maxParamLength should set the maximum length for a parametric (regex) route', t => {
t.plan(1)

const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param(^\\d+$)', () => {})

t.deepEqual(findMyWay.find('GET', '/test/123456789abcd'), null)
})

test('maxParamLength should set the maximum length for a parametric (multi) route', t => {
t.plan(1)

const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param-bar', () => {})
t.deepEqual(findMyWay.find('GET', '/test/123456789abcd'), null)
})

test('maxParamLength should set the maximum length for a parametric (regex with suffix) route', t => {
t.plan(1)

const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param(^\\w{3})bar', () => {})
t.deepEqual(findMyWay.find('GET', '/test/123456789abcd'), null)
})

0 comments on commit 5f08928

Please sign in to comment.