Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

find-my-way

Package Overview
Dependencies
Maintainers
2
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-my-way - npm Package Compare versions

Comparing version 1.8.2 to 1.9.0

39

index.js

@@ -37,2 +37,3 @@ 'use strict'

this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false
this.tree = new Node()

@@ -43,9 +44,24 @@ this.routes = []

Router.prototype.on = function on (method, path, handler, store) {
const register = (m, p, h, s) => {
if (!this.ignoreTrailingSlash || path === '/' || path.endsWith('*')) {
return this._on(m, p, h, s)
}
if (this.ignoreTrailingSlash && p.endsWith('/')) {
this._on(m, p, h, s)
this._on(m, p.slice(0, -1), h, s)
return
}
this._on(m, p, h, s)
this._on(m, p + '/', h, s)
}
if (Array.isArray(method)) {
for (var k = 0; k < method.length; k++) {
this.on(method[k], path, handler, store)
register(method[k], path, handler, store)
}
return
}
register(method, path, handler, store)
}
Router.prototype._on = function _on (method, path, handler, store) {
// method validation

@@ -219,5 +235,24 @@ assert(typeof method === 'string', 'Method should be a string')

// Rebuild tree without the specific route
const ignoreTrailingSlash = this.ignoreTrailingSlash
var newRoutes = self.routes.filter(function (route) {
return !(method === route.method && path === route.path)
if (!ignoreTrailingSlash) {
return !(method === route.method && path === route.path)
}
if (path.endsWith('/')) {
const routeMatches = path === route.path || path.slice(0, -1) === route.path
return !(method === route.method && routeMatches)
}
const routeMatches = path === route.path || (path + '/') === route.path
return !(method === route.method && routeMatches)
})
if (ignoreTrailingSlash) {
newRoutes = newRoutes.filter(function (route, i, ar) {
if (route.path.endsWith('/') && i < ar.length - 1) {
return route.path.slice(0, -1) !== ar[i + 1].path
} else if (route.path.endsWith('/') === false && i < ar.length - 1) {
return (route.path + '/') !== ar[i + 1].path
}
return true
})
}
self.reset()

@@ -224,0 +259,0 @@ newRoutes.forEach(function (route) {

2

package.json
{
"name": "find-my-way",
"version": "1.8.2",
"version": "1.9.0",
"description": "Crazy fast http radix based router",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -7,3 +7,3 @@ # find-my-way

If you want to see a benchmark comparison with the most commonly used routers, see [here](https://github.com/delvedor/router-benchmark).
If you want to see a benchmark comparison with the most commonly used routers, see [here](https://github.com/delvedor/router-benchmark).<br>
Do you need a real-world example that uses this router? Check out [Fastify](https://github.com/fastify/fastify).

@@ -41,3 +41,3 @@

#### FindMyway([options])
Instance a new router.
Instance a new router.<br>
You can pass a default route with the option `defaultRoute`.

@@ -53,2 +53,14 @@ ```js

Trailing slashes can be ignored by supplying the `ignoreTrailingSlash` option:
```js
const router = require('find-my-way')({
ignoreTrailingSlash: true
})
function handler (req, res, params) {
res.send('foo')
}
// maps "/foo/" and "/foo" to `handler`
router.on('GET', '/foo/', handler)
```
<a name="on"></a>

@@ -203,4 +215,4 @@ #### on(method, path, handler, [store])

#### lookup(request, response)
Start a new search, `request` and `response` are the server req/res objects.
If a route is found it will automatically called the handler, otherwise the default route will be called.
Start a new search, `request` and `response` are the server req/res objects.<br>
If a route is found it will automatically called the handler, otherwise the default route will be called.<br>
The url is sanitized internally, all the parameters and wildcards are decoded automatically.

@@ -213,3 +225,3 @@ ```js

#### find(method, path)
Return (if present) the route registered in *method:path*.
Return (if present) the route registered in *method:path*.<br>
The path must be sanitized, all the parameters and wildcards are decoded automatically.

@@ -240,3 +252,3 @@ ```js

This project is kindly sponsored by [LetzDoIt](http://www.letzdoitapp.com/).
This project is kindly sponsored by [LetzDoIt](http://www.letzdoitapp.com/).<br>
It is inspired by the [echo](https://github.com/labstack/echo) router, some parts have been extracted from [trekjs](https://github.com/trekjs) router.

@@ -246,5 +258,5 @@

## License
**[find-my-way - MIT](https://github.com/delvedor/find-my-way/blob/master/LICENSE)**
**[find-my-way - MIT](https://github.com/delvedor/find-my-way/blob/master/LICENSE)**<br>
**[trekjs/router - MIT](https://github.com/trekjs/router/blob/master/LICENSE)**
Copyright © 2017 Tomas Della Vedova

@@ -70,2 +70,15 @@ 'use strict'

test('does not register /test/*/ when ignoreTrailingSlash is true', t => {
t.plan(1)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true
})
findMyWay.on('GET', '/test/*', () => {})
t.is(
findMyWay.routes.filter((r) => r.path.includes('/test')).length,
1
)
})
test('off throws for invalid method', t => {

@@ -132,2 +145,29 @@ t.plan(1)

test('off removes all routes when ignoreTrailingSlash is true', t => {
t.plan(6)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true
})
findMyWay.on('GET', '/test1/', () => {})
t.is(findMyWay.routes.length, 2)
findMyWay.on('GET', '/test2', () => {})
t.is(findMyWay.routes.length, 4)
findMyWay.off('GET', '/test1')
t.is(findMyWay.routes.length, 2)
t.is(
findMyWay.routes.filter((r) => r.path === '/test2').length,
1
)
t.is(
findMyWay.routes.filter((r) => r.path === '/test2/').length,
1
)
findMyWay.off('GET', '/test2/')
t.is(findMyWay.routes.length, 0)
})
test('deregister a route without children', t => {

@@ -134,0 +174,0 @@ t.plan(2)

@@ -115,1 +115,153 @@ 'use strict'

})
test('maps two routes when trailing slash should be trimmed', t => {
t.plan(25)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true
})
findMyWay.on('GET', '/test/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
findMyWay.on('GET', '/othertest', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('othertest')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
request({
method: 'GET',
uri: baseURL + '/test/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, 'test')
})
request({
method: 'GET',
uri: baseURL + '/test'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, 'test')
})
request({
method: 'GET',
uri: baseURL + '/othertest'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, 'othertest')
})
request({
method: 'GET',
uri: baseURL + '/othertest/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, 'othertest')
})
})
})
test('does not trim trailing slash when ignoreTrailingSlash is false', t => {
t.plan(9)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false
})
findMyWay.on('GET', '/test/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
request({
method: 'GET',
uri: baseURL + '/test/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, 'test')
})
request({
method: 'GET',
uri: baseURL + '/test'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
})
})
})
test('does not map // when ignoreTrailingSlash is true', t => {
t.plan(9)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false
})
findMyWay.on('GET', '/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
request({
method: 'GET',
uri: baseURL + '/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, 'test')
})
request({
method: 'GET',
uri: baseURL + '//'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc