diff --git a/README.md b/README.md index d972d92c..8956fa77 100644 --- a/README.md +++ b/README.md @@ -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.
+*If the maximum length limit is reached, the default route will be invoked.* +```js +const router = require('find-my-way')({ + maxParamLength: 500 +}) +``` #### on(method, path, handler, [store]) Register a new route. diff --git a/index.js b/index.js index 29f2d92c..f32d4a23 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,7 @@ function Router (opts) { } this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false + this.maxParamLength = opts.maxParamLength || 100 this.tree = new Node() this.routes = [] } @@ -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 @@ -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 @@ -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 @@ -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) { diff --git a/test/max-param-length.test.js b/test/max-param-length.test.js new file mode 100644 index 00000000..380a9bf4 --- /dev/null +++ b/test/max-param-length.test.js @@ -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) +})