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.7.1 to 1.8.0

40

index.js

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

this.tree = new Node()
this.routes = []
}

@@ -62,2 +63,9 @@

this.routes.push({
method: method,
path: path,
handler: handler,
store: store
})
for (var i = 0, len = path.length; i < len; i++) {

@@ -188,2 +196,34 @@ // search for parametric or wildcard routes

Router.prototype.reset = function reset () {
this.tree = new Node()
this.routes = []
}
Router.prototype.off = function off (method, path) {
var self = this
if (Array.isArray(method)) {
return method.map(function (method) {
return self.off(method, path)
})
}
// method validation
assert(typeof method === 'string', 'Method should be a string')
assert(httpMethods.indexOf(method) !== -1, `Method '${method}' is not an http method.`)
// path validation
assert(typeof path === 'string', 'Path should be a string')
assert(path.length > 0, 'The path could not be empty')
assert(path[0] === '/' || path[0] === '*', 'The first character of a path should be `/` or `*`')
// Rebuild tree without the specific route
var newRoutes = self.routes.filter(function (route) {
return !(method === route.method && path === route.path)
})
self.reset()
newRoutes.forEach(function (route) {
self.on(route.method, route.path, route.handler, route.store)
})
}
Router.prototype.lookup = function lookup (req, res) {

@@ -190,0 +230,0 @@ var handle = this.find(req.method, sanitizeUrl(req.url))

2

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

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

@@ -120,2 +120,27 @@ # find-my-way

<a name="off"></a>
#### off(method, path)
Deregister a route.
```js
router.off('GET', '/example')
// => { handler: Function, params: Object, store: Object}
// => null
```
##### off(methods[], path, handler, [store])
Deregister a route for each method specified in the `methods` array.
It comes handy when you need to deregister multiple routes with the same path but different methods.
```js
router.off(['GET', 'POST'], '/example')
// => [{ handler: Function, params: Object, store: Object}]
// => null
```
<a name="reset"></a>
#### reset()
Empty router.
```js
router.reset()
```
##### Caveats

@@ -122,0 +147,0 @@ * 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:

@@ -8,3 +8,3 @@ 'use strict'

test('the router is an object with methods', t => {
t.plan(3)
t.plan(4)

@@ -14,2 +14,3 @@ const findMyWay = FindMyWay()

t.is(typeof findMyWay.on, 'function')
t.is(typeof findMyWay.off, 'function')
t.is(typeof findMyWay.lookup, 'function')

@@ -19,2 +20,31 @@ t.is(typeof findMyWay.find, 'function')

test('on throws for invalid method', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.throws(() => {
findMyWay.on('INVALID', '/a/b')
})
})
test('on throws for invalid path', t => {
t.plan(3)
const findMyWay = FindMyWay()
// Non string
t.throws(() => {
findMyWay.on('GET', 1)
})
// Empty
t.throws(() => {
findMyWay.on('GET', '')
})
// Doesn't start with / or *
t.throws(() => {
findMyWay.on('GET', 'invalid')
})
})
test('register a route', t => {

@@ -43,2 +73,120 @@ t.plan(1)

test('off throws for invalid method', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.throws(() => {
findMyWay.off('INVALID', '/a/b')
})
})
test('off throws for invalid path', t => {
t.plan(3)
const findMyWay = FindMyWay()
// Non string
t.throws(() => {
findMyWay.off('GET', 1)
})
// Empty
t.throws(() => {
findMyWay.off('GET', '')
})
// Doesn't start with / or *
t.throws(() => {
findMyWay.off('GET', 'invalid')
})
})
test('off with nested wildcards with parametric and static', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.is(params['*'], '/foo2/first/second')
})
findMyWay.on('GET', '/foo1/*', () => {})
findMyWay.on('GET', '/foo2/*', () => {})
findMyWay.on('GET', '/foo3/:param', () => {})
findMyWay.on('GET', '/foo3/*', () => {})
findMyWay.on('GET', '/foo4/param/hello/test/long/route', () => {})
var route1 = findMyWay.find('GET', '/foo3/first/second')
t.is(route1.params['*'], 'first/second')
findMyWay.off('GET', '/foo3/*')
var route2 = findMyWay.find('GET', '/foo3/first/second')
t.is(route2.params['*'], '/foo3/first/second')
findMyWay.off('GET', '/foo2/*')
findMyWay.lookup(
{ method: 'GET', url: '/foo2/first/second' },
null
)
})
test('deregister a route without children', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', () => {})
findMyWay.on('GET', '/a/b', () => {})
findMyWay.off('GET', '/a/b')
t.ok(findMyWay.find('GET', '/a'))
t.notOk(findMyWay.find('GET', '/a/b'))
})
test('deregister a route with children', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', () => {})
findMyWay.on('GET', '/a/b', () => {})
findMyWay.off('GET', '/a')
t.notOk(findMyWay.find('GET', '/a'))
t.ok(findMyWay.find('GET', '/a/b'))
})
test('deregister a route by method', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/a', () => {})
findMyWay.off('GET', '/a')
t.notOk(findMyWay.find('GET', '/a'))
t.ok(findMyWay.find('POST', '/a'))
})
test('deregister a route with multiple methods', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/a', () => {})
findMyWay.off(['GET', 'POST'], '/a')
t.notOk(findMyWay.find('GET', '/a'))
t.notOk(findMyWay.find('POST', '/a'))
})
test('reset a router', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/a', () => {})
findMyWay.reset()
t.notOk(findMyWay.find('GET', '/a'))
t.notOk(findMyWay.find('POST', '/a'))
})
test('default route', t => {

@@ -45,0 +193,0 @@ t.plan(1)

Sorry, the diff of this file is not supported yet

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