Socket
Socket
Sign inDemoInstall

itty-router

Package Overview
Dependencies
0
Maintainers
2
Versions
264
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.6.0 to 2.0.0

1

CHANGELOG.md
## Changelog
Until this library makes it to a production release of v1.x, **minor versions may contain breaking changes to the API**. After v1.x, semantic versioning will be honored, and breaking changes will only occur under the umbrella of a major version bump.
- **v2.0.0** - API break: `Router({ else: missingHandler })` has been replaced with `router.all('*', missingHandler)`, and now "all" channel respects order of entry
- **v1.6.0** - added { else: missingHandler } to options for 404 catch-alls (thanks to the discussion with [@martinffx](https://github.com/martinffx))

@@ -5,0 +6,0 @@ - **v1.5.0** - added '.all(route, handler)' handling for passthrough middleware

8

dist/itty-router.js

@@ -5,3 +5,3 @@ const Router = (o = {}) =>

? async (r, ...args) => {
for ([p, hs] of [t.all || [], t[(r.method || 'GET').toLowerCase()] || []].flat()) {
for ([, p, hs] of t.r.filter(r => r[0] === r.method || 'ALL')) {
if (m = (u = new URL(r.url)).pathname.match(p)) {

@@ -16,7 +16,7 @@ r.params = m.groups

}
if (o.else) return o.else(r, ...args)
}
: (p, ...hs) =>
(t[k] = t[k] || []).push([
`^${(o.base || '')+p
(t.r = t.r || []).push([
k.toUpperCase(),
`^${(t.base || '')+p
.replace(/(\/?)\*/g, '($1.*)?')

@@ -23,0 +23,0 @@ .replace(/\/$/, '')

@@ -1,1 +0,1 @@

const e=(e={})=>new Proxy(e,{get:(r,a,o)=>"handle"===a?async(a,...o)=>{for([p,hs]of[r.all||[],r[(a.method||"GET").toLowerCase()]||[]].flat())if(m=(u=new URL(a.url)).pathname.match(p))for(h of(a.params=m.groups,a.query=Object.fromEntries(u.searchParams.entries()),hs))if(void 0!==(s=await h(a,...o)))return s;if(e.else)return e.else(a,...o)}:(s,...t)=>(r[a]=r[a]||[]).push([`^${(e.base||"")+s.replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:([^\/\?\.]+)(\?)?/g,"$2(?<$1>[^/.]+)$2")}/*$`,t])&&o});module.exports={Router:e};
const e=(e={})=>new Proxy(e,{get:(e,r,a)=>"handle"===r?async(r,...a)=>{for([,p,hs]of e.r.filter((e=>e[0]===e.method||"ALL")))if(m=(u=new URL(r.url)).pathname.match(p))for(h of(r.params=m.groups,r.query=Object.fromEntries(u.searchParams.entries()),hs))if(void 0!==(s=await h(r,...a)))return s}:(s,...o)=>(e.r=e.r||[]).push([r.toUpperCase(),`^${(e.base||"")+s.replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:([^\/\?\.]+)(\?)?/g,"$2(?<$1>[^/.]+)$2")}/*$`,o])&&a});module.exports={Router:e};
{
"name": "itty-router",
"version": "1.6.0",
"version": "2.0.0",
"description": "Tiny, zero-dependency router with route param and query parsing.",

@@ -47,12 +47,12 @@ "main": "./dist/itty-router.min.js",

"chalk": "^4.1.0",
"coveralls": "^3.0.13",
"eslint": "^7.2.0",
"eslint-plugin-jest": "^23.13.2",
"fs-extra": "^9.0.0",
"coveralls": "^3.1.0",
"eslint": "^7.20.0",
"eslint-plugin-jest": "^24.1.5",
"fs-extra": "^9.1.0",
"gzip-size": "^6.0.0",
"jest": "^25.3.0",
"jest": "^26.6.3",
"rimraf": "^3.0.2",
"terser": "^4.6.11",
"yarn-release": "^1.10.2"
"terser": "^5.6.0",
"yarn-release": "^1.10.3"
}
}

@@ -35,3 +35,3 @@ ![Logo][logo-image]

// 404/Missing as final catch-all route
router.get('*', () => new Response('Not Found.', { status: 404 }))
router.all('*', () => new Response('Not Found.', { status: 404 }))

@@ -45,3 +45,3 @@ // attach the router handle to the event handler

# Features
- [x] tiny (<500 bytes) with zero dependencies
- [x] tiny (<450 bytes) with zero dependencies
- [x] route params, with optionals (e.g. `/api/:foo/:id?.:format?`)

@@ -64,3 +64,2 @@ - [x] bonus query parsing (e.g. `?page=3&foo-bar`)

| `base` | `string` | prefixes all routes with this string | `Router({ base: '/api' })`
| `else` | `function` | missing handler for uncaught routes | `Router({ else: () => new Response('That path was not found!', { status: 404 }) })`

@@ -79,3 +78,3 @@ # Usage

**Special Exception:** To allow nested routers (or middleware) to catch on all METHODS, **any routes on the "all" channel will be processed FIRST, no matter when they were registered in the flow**. If all routes are on "get", feel free to just use `.get`, but `.all` will allow routes/middleware/routers to match on ANY methods (useful when embedding nested routers that have full CRUD methods). For example: `router.all('/crud/*', crudRouter.handle)`
**Special Exception - the "all" channel:** Any routes on the "all" channel will match to ANY method (e.g. GET/POST/whatever), allowing for greater middleware support, nested routers, 404 catches, etc.

@@ -113,3 +112,3 @@ ```js

// create a parent router
const parentRouter = Router({ else: missingHandler })
const parentRouter = Router()

@@ -125,7 +124,9 @@ // and a child router

// then divert ALL requests to /todos/* into the child router
parentRouter.all('/todos/*', todosRouter.handle) // all /todos/* routes will route through the todosRouter
parentRouter
.all('/todos/*', todosRouter.handle) // all /todos/* routes will route through the todosRouter
.all('*', missingHandler)
// GET /todos --> Todos Index
// GET /todos/13 --> Todo #13
// POST /todos --> missingHandler (it will fail to catch inside todosRouter, and be caught by "else" on parentRouter)
// POST /todos --> missingHandler (it will fail to catch inside todosRouter, and be caught by final "all" on parentRouter)
// GET /foo --> missingHandler

@@ -184,18 +185,18 @@ ```

get: (t, k, c) => k === 'handle'
? async (q, ...args) => {
for ([p, hs] of [t.all || [], t[(q.method || 'GET').toLowerCase()] || []].flat()) {
if (m = (u = new URL(q.url)).pathname.match(p)) {
q.params = m.groups
q.query = Object.fromEntries(u.searchParams.entries())
? async (r, ...args) => {
for ([, p, hs] of t.r.filter(r => r[0] === r.method || 'ALL')) {
if (m = (u = new URL(r.url)).pathname.match(p)) {
r.params = m.groups
r.query = Object.fromEntries(u.searchParams.entries())
for (h of hs) {
if ((s = await h(q, ...args)) !== undefined) return s
if ((s = await h(r, ...args)) !== undefined) return s
}
}
}
if (o.else) return o.else(q, ...args)
}
: (p, ...hs) =>
(t[k] = t[k] || []).push([
`^${(o.base || '')+p
(t.r = t.r || []).push([
k.toUpperCase(),
`^${(t.base || '')+p
.replace(/(\/?)\*/g, '($1.*)?')

@@ -202,0 +203,0 @@ .replace(/\/$/, '')

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc