@ditojs/router
Advanced tools
Comparing version 2.0.5 to 2.1.0
{ | ||
"name": "@ditojs/router", | ||
"version": "2.0.5", | ||
"version": "2.1.0", | ||
"type": "module", | ||
@@ -24,3 +24,3 @@ "description": "Dito.js Router – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js", | ||
"dependencies": { | ||
"@ditojs/utils": "^2.0.1" | ||
"@ditojs/utils": "^2.1.0" | ||
}, | ||
@@ -34,4 +34,4 @@ "keywords": [ | ||
], | ||
"gitHead": "ee7c025d76cf6a59825a407455d3b990e0f016ed", | ||
"gitHead": "5c5048b661395268e720b9ec402d4301bb4a34aa", | ||
"readme": "# Dito.js Router\n\nDito.js is a declarative and modern web framework with a focus on API driven\ndevelopment, based on Koa.js, Objection.js and Vue.js\n\nReleased in 2018 under the MIT license, with support by https://lineto.com/\n\nDito.js Router is a high performance, tree-based, framework agnostic HTTP\nrouter, based on [trek-router](https://github.com/trekjs/router), which in turn\nis inspired by [Echo](https://github.com/labstack/echo)'s Router.\n\n## How does it work?\n\nThe router relies on a tree structure which makes heavy use of common\nprefixes, essentially a [prefix tree](https://en.wikipedia.org/wiki/Trie).\n\n## Usage\n\n```js\nimport Koa from 'koa'\nimport Router from '@ditojs/router'\n\nconst app = new Koa()\nconst router = new Router()\n\n// static route\nrouter.get('/folders/files/bolt.gif', ctx => {\n ctx.body = `this ain't no GIF!`\n})\n\n// param route\nrouter.get('/users/:id', ctx => {\n ctx.body = `requesting user ${ctx.params.id}`\n})\n\n// match-any route\nrouter.get('/books/*', ctx => {\n ctx.body = `sub-route: ${ctx.params['*']}`\n})\n\n// Handler found\nlet { handler, params } = router.find('get', '/users/233')\nconsole.log(handler) // ctx => { ... }\n\n// Entry not Found\nlet { handler, params } = router.find('get', '/photos/233')\nconsole.log(handler) // null\n\n// Install router middleware\napp.use(async (ctx, next) => {\n const { method, path } = ctx\n const result = router.find(method, path)\n const { handler, params } = result\n if (handler) {\n ctx.params = params || {}\n return handler(ctx, next)\n } else {\n try {\n await next()\n } finally {\n if (ctx.body === undefined && ctx.status === 404) {\n ctx.status = result.status || 404\n if (ctx.status !== 404 && result.allowed) {\n ctx.set('Allow', result.allowed.join(', '))\n }\n }\n }\n }\n})\n\napp.listen(4040, () => console.log('Koa app listening on 4040'))\n```\n" | ||
} |
@@ -8,12 +8,16 @@ const SYMBOL_ALLOWED = Symbol('allowed') | ||
// 501: 'Method Not Implemented' (there is no tree for that method) | ||
this.status = { | ||
GET: 404, | ||
get: 404, | ||
options: 200, | ||
OPTIONS: 200 | ||
}[method] || (tree ? 405 : 501) | ||
this.status = ( | ||
{ | ||
GET: 404, | ||
get: 404, | ||
options: 200, | ||
OPTIONS: 200 | ||
}[method] || | ||
(tree ? 405 : 501) | ||
) | ||
// Getter for the `allowed` property, called once, then cached: | ||
this[SYMBOL_ALLOWED] = () => tree | ||
? router.getAllowedMethods(path, method) | ||
: router.getAllowedMethods() | ||
this[SYMBOL_ALLOWED] = () => | ||
tree | ||
? router.getAllowedMethods(path, method) | ||
: router.getAllowedMethods() | ||
} | ||
@@ -20,0 +24,0 @@ |
@@ -91,6 +91,8 @@ import Router from './Router.js' | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── folders/files/bolt.gif getHandler() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── folders/files/bolt.gif getHandler() children=0 | ||
`.trim() | ||
) | ||
@@ -128,7 +130,9 @@ result = router.find('GET', '/folders/files/bolt.gif') | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── static/ children=1 | ||
└── ** handler() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── static/ children=1 | ||
└── ** handler() children=0 | ||
`.trim() | ||
) | ||
@@ -157,8 +161,10 @@ result = router.find('GET', '/static') | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── prefix/ children=1 | ||
└── * children=1 | ||
└── /suffix handler() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── prefix/ children=1 | ||
└── * children=1 | ||
└── /suffix handler() children=0 | ||
`.trim() | ||
) | ||
@@ -187,10 +193,12 @@ result = router.find('GET', '/prefix') | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── static/ children=4 | ||
├── **/suffix handler() children=0 | ||
├── **/suffix/**/more handler() children=0 | ||
├── **/suffix/*/1/:param/2 handler() children=0 | ||
└── **/suffix/*/1/:param/2/**/end handler() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── static/ children=4 | ||
├── **/suffix handler() children=0 | ||
├── **/suffix/**/more handler() children=0 | ||
├── **/suffix/*/1/:param/2 handler() children=0 | ||
└── **/suffix/*/1/:param/2/**/end handler() children=0 | ||
`.trim() | ||
) | ||
@@ -242,3 +250,6 @@ result = router.find('GET', '/static') | ||
result = router.find('GET', `/static/one/two/suffix/three/1/bla/2/what/ever/end`) | ||
result = router.find( | ||
'GET', | ||
`/static/one/two/suffix/three/1/bla/2/what/ever/end` | ||
) | ||
expect(result.handler).toBe(handler) | ||
@@ -271,22 +282,24 @@ expect(result.params).toEqual({ | ||
expect(router.toString()).toBe(deindent` | ||
/ root() children=1 | ||
└── geocoder geocoder() children=1 | ||
└── / children=4 | ||
├── n children=2 | ||
│ ├── ew newGeocoder() children=0 | ||
│ └── otify notifyGeocoder() children=0 | ||
├── e children=2 | ||
│ ├── dit editGeocoder() children=1 | ||
│ │ └── / children=2 | ||
│ │ ├── email editEmailGeocoder() children=0 | ||
│ │ └── :item editItemGeocoder() children=0 | ||
│ └── xchange exchangeGeocoder() children=1 | ||
│ └── / children=2 | ||
│ ├── email exchangeEmailGeocoder() children=0 | ||
│ └── :item exchangeItemGeocoder() children=0 | ||
├── :action actionGeocoder() children=1 | ||
│ └── /echo echoGeocoder() children=0 | ||
└── ** anyGeocoder() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ root() children=1 | ||
└── geocoder geocoder() children=1 | ||
└── / children=4 | ||
├── n children=2 | ||
│ ├── ew newGeocoder() children=0 | ||
│ └── otify notifyGeocoder() children=0 | ||
├── e children=2 | ||
│ ├── dit editGeocoder() children=1 | ||
│ │ └── / children=2 | ||
│ │ ├── email editEmailGeocoder() children=0 | ||
│ │ └── :item editItemGeocoder() children=0 | ||
│ └── xchange exchangeGeocoder() children=1 | ||
│ └── / children=2 | ||
│ ├── email exchangeEmailGeocoder() children=0 | ||
│ └── :item exchangeItemGeocoder() children=0 | ||
├── :action actionGeocoder() children=1 | ||
│ └── /echo echoGeocoder() children=0 | ||
└── ** anyGeocoder() children=0 | ||
`.trim() | ||
) | ||
result = router.find('GET', '') | ||
@@ -386,26 +399,28 @@ expect(result.handler).not.toBeUndefined() | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── users users() children=1 | ||
└── / children=3 | ||
├── n children=2 | ||
│ ├── e children=2 | ||
│ │ ├── w newUser() children=0 | ||
│ │ └── i newUser() children=0 | ||
│ └── oi newUser() children=0 | ||
├── :userId user() children=1 | ||
│ └── / children=4 | ||
│ ├── edit editUser() children=0 | ||
│ ├── :action actionUser() children=1 | ||
│ │ └── / children=1 | ||
│ │ └── :good children=1 | ||
│ │ └── / children=1 | ||
│ │ └── :bad children=1 | ||
│ │ └── /ddd editUser() children=0 | ||
│ ├── photos/ children=1 | ||
│ │ └── :id photo() children=0 | ||
│ └── books/ children=1 | ||
│ └── :id book() children=0 | ||
└── ** anyUser() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── users users() children=1 | ||
└── / children=3 | ||
├── n children=2 | ||
│ ├── e children=2 | ||
│ │ ├── w newUser() children=0 | ||
│ │ └── i newUser() children=0 | ||
│ └── oi newUser() children=0 | ||
├── :userId user() children=1 | ||
│ └── / children=4 | ||
│ ├── edit editUser() children=0 | ||
│ ├── :action actionUser() children=1 | ||
│ │ └── / children=1 | ||
│ │ └── :good children=1 | ||
│ │ └── / children=1 | ||
│ │ └── :bad children=1 | ||
│ │ └── /ddd editUser() children=0 | ||
│ ├── photos/ children=1 | ||
│ │ └── :id photo() children=0 | ||
│ └── books/ children=1 | ||
│ └── :id book() children=0 | ||
└── ** anyUser() children=0 | ||
`.trim() | ||
) | ||
@@ -499,35 +514,37 @@ result = router.find('GET', '/users/610/books/987/edit') | ||
createRoutes(router, routes) | ||
expect(router.toString()).toBe(deindent` | ||
/ children=3 | ||
├── users users() children=1 | ||
│ └── / children=2 | ||
│ ├── new newUser() children=0 | ||
│ └── :id user() children=1 | ||
│ └── / children=3 | ||
│ ├── :action actionUser() children=0 | ||
│ ├── e children=2 | ||
│ │ ├── dit editUser() children=0 | ||
│ │ └── vent eventUser() children=0 | ||
│ └── change changeUser() children=0 | ||
├── photos photos() children=1 | ||
│ └── / children=2 | ||
│ ├── new newPhoto() children=0 | ||
│ └── :id photo() children=1 | ||
│ └── / children=3 | ||
│ ├── :action actionPhoto() children=0 | ||
│ ├── e children=2 | ||
│ │ ├── dit editPhoto() children=0 | ||
│ │ └── vent eventPhoto() children=0 | ||
│ └── change changePhoto() children=0 | ||
└── books books() children=1 | ||
└── / children=2 | ||
├── new newBook() children=0 | ||
└── :id book() children=1 | ||
└── / children=3 | ||
├── :action actionBook() children=0 | ||
├── e children=2 | ||
│ ├── dit editBook() children=0 | ||
│ └── vent eventBook() children=0 | ||
└── change changeBook() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=3 | ||
├── users users() children=1 | ||
│ └── / children=2 | ||
│ ├── new newUser() children=0 | ||
│ └── :id user() children=1 | ||
│ └── / children=3 | ||
│ ├── :action actionUser() children=0 | ||
│ ├── e children=2 | ||
│ │ ├── dit editUser() children=0 | ||
│ │ └── vent eventUser() children=0 | ||
│ └── change changeUser() children=0 | ||
├── photos photos() children=1 | ||
│ └── / children=2 | ||
│ ├── new newPhoto() children=0 | ||
│ └── :id photo() children=1 | ||
│ └── / children=3 | ||
│ ├── :action actionPhoto() children=0 | ||
│ ├── e children=2 | ||
│ │ ├── dit editPhoto() children=0 | ||
│ │ └── vent eventPhoto() children=0 | ||
│ └── change changePhoto() children=0 | ||
└── books books() children=1 | ||
└── / children=2 | ||
├── new newBook() children=0 | ||
└── :id book() children=1 | ||
└── / children=3 | ||
├── :action actionBook() children=0 | ||
├── e children=2 | ||
│ ├── dit editBook() children=0 | ||
│ └── vent eventBook() children=0 | ||
└── change changeBook() children=0 | ||
`.trim() | ||
) | ||
}) | ||
@@ -634,10 +651,12 @@ | ||
createRoutes(router, routes) | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── admin/articles articles() children=1 | ||
└── / children=2 | ||
├── new newArticle() children=0 | ||
└── :id article() children=1 | ||
└── /edit editArticle() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── admin/articles articles() children=1 | ||
└── / children=2 | ||
├── new newArticle() children=0 | ||
└── :id article() children=1 | ||
└── /edit editArticle() children=0 | ||
`.trim() | ||
) | ||
}) | ||
@@ -682,18 +701,20 @@ | ||
createRoutes(router, routes) | ||
expect(router.toString()).toBe(deindent` | ||
/ children=1 | ||
└── magazines/ children=1 | ||
└── :m_id children=1 | ||
└── / children=2 | ||
├── articles articles() children=1 | ||
│ └── / children=2 | ||
│ ├── new newArticle() children=0 | ||
│ └── :id article() children=1 | ||
│ └── /edit editArticle() children=0 | ||
└── photos photos() children=1 | ||
└── / children=2 | ||
├── new newPhoto() children=0 | ||
└── :id photo() children=1 | ||
└── /edit editPhoto() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=1 | ||
└── magazines/ children=1 | ||
└── :m_id children=1 | ||
└── / children=2 | ||
├── articles articles() children=1 | ||
│ └── / children=2 | ||
│ ├── new newArticle() children=0 | ||
│ └── :id article() children=1 | ||
│ └── /edit editArticle() children=0 | ||
└── photos photos() children=1 | ||
└── / children=2 | ||
├── new newPhoto() children=0 | ||
└── :id photo() children=1 | ||
└── /edit editPhoto() children=0 | ||
`.trim() | ||
) | ||
}) | ||
@@ -747,9 +768,11 @@ | ||
it('identifies unnamed handlers', () => { | ||
router.add('GET', '/function', function() {}) | ||
router.add('GET', '/function', function () {}) | ||
router.add('GET', '/closure', () => {}) | ||
expect(router.toString()).toBe(deindent` | ||
/ children=2 | ||
├── function ƒ() children=0 | ||
└── closure ƒ() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=2 | ||
├── function ƒ() children=0 | ||
└── closure ƒ() children=0 | ||
`.trim() | ||
) | ||
}) | ||
@@ -760,7 +783,9 @@ | ||
router.add('GET', '/wacky-path', () => {}) | ||
expect(router.toString()).toBe(deindent` | ||
/ children=2 | ||
├── users ƒ() children=0 | ||
└── wacky-path ƒ() children=0 | ||
`.trim()) | ||
expect(router.toString()).toBe( | ||
deindent` | ||
/ children=2 | ||
├── users ƒ() children=0 | ||
└── wacky-path ƒ() children=0 | ||
`.trim() | ||
) | ||
}) | ||
@@ -770,5 +795,4 @@ }) | ||
function createFunc(name) { | ||
return (new Function( | ||
`return function ${name}(){}` | ||
))() | ||
// eslint-disable-next-line no-new-func | ||
return new Function(`return function ${name}(){}`)() | ||
} | ||
@@ -775,0 +799,0 @@ |
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
47297
1072
Updated@ditojs/utils@^2.1.0