Socket
Socket
Sign inDemoInstall

itty-router

Package Overview
Dependencies
Maintainers
2
Versions
265
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

itty-router - npm Package Compare versions

Comparing version 3.0.11-next.40 to 3.0.11-next.41

4

index.d.ts

@@ -6,2 +6,6 @@ export * from './Router';

export * from './status';
export * from './badRequest';
export * from './forbidden';
export * from './notFound';
export * from './unauthorized';
export * from './text';

@@ -8,0 +12,0 @@ export * from './json';

2

index.js

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

const e=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(s,o,r)=>(s,...n)=>t.push([o.toUpperCase(),RegExp(`^${(e+"/"+s).replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),n])&&r}),routes:t,async handle(e,...s){let o,r,n=new URL(e.url),a=e.query={};for(let[e,t]of n.searchParams)a[e]=void 0===a[e]?t:[a[e],t].flat();for(let[a,c,l]of t)if((a===e.method||"ALL"===a)&&(r=n.pathname.match(c))){e.params=r.groups||{};for(let t of l)if(void 0!==(o=await t(e.proxy||e,...s)))return o}}});class t extends Error{status;constructor(e=500,t){"object"==typeof t?(super(t.error),Object.assign(this,t)):super(t),this.status=e}}const s=(e="text/plain; charset=utf-8",t)=>(s,o={})=>{const{headers:r={},...n}=o;return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...r},...n})},o=s("application/json; charset=utf-8",JSON.stringify),r=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),n=(e=500,t)=>{if(e instanceof Error){const{message:s,...o}=e;e=e.status||500,t={error:s||r(e),...o}}return t={status:e,..."object"==typeof t?t:{error:t||r(e)}},o(t,{status:e})},a=e=>new Response(null,{status:e}),c=(e,t)=>new Response(e,t),l=s("text/html"),i=s("image/jpeg"),p=s("image/png"),u=s("image/webp"),d=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},h=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,s])=>(e[t]=s,e)),{})},g=e=>{e.proxy=new Proxy(e.proxy||e,{get:(e,t)=>void 0!==e[t]?e[t]:e?.params?.[t]})},f=(e={})=>{const{origins:t=["*"],maxAge:s,methods:o=["GET"],headers:r={}}=e;let n;const a={"content-type":"application/json","Access-Control-Allow-Methods":o.join(", "),...r};s&&(a["Access-Control-Max-Age"]=s);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:s,body:o}=e;if([101,301,302,308].includes(s))return e;const r=Object.fromEntries(t);return r["access-control-allow-origin"]?e:new Response(o,{status:s,headers:{...r,...a,...n,"content-type":t.get("content-type")}})},preflight:e=>{const s=[...new Set(["OPTIONS",...o])],r=e.headers.get("origin")||"";if(n=(t.includes(r)||t.includes("*"))&&{"Access-Control-Allow-Origin":r},"OPTIONS"===e.method){if(null!==e.headers.get("Origin")&&null!==e.headers.get("Access-Control-Request-Method")&&null!==e.headers.get("Access-Control-Request-Headers")){const t={...a,"Access-Control-Allow-Methods":s.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...n};return new Response(null,{headers:t})}return new Response(null,{headers:{Allow:s.join(", ")}})}}}};export{e as Router,t as StatusError,f as createCors,s as createResponse,n as error,l as html,i as jpeg,o as json,p as png,a as status,c as text,u as webp,d as withContent,h as withCookies,g as withParams};
const e=({base:e="",routes:t=[]}={})=>({__proto__:new Proxy({},{get:(s,o,r)=>(s,...n)=>t.push([o.toUpperCase(),RegExp(`^${(e+"/"+s).replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),n])&&r}),routes:t,async handle(e,...s){let o,r,n=new URL(e.url),a=e.query={};for(let[e,t]of n.searchParams)a[e]=void 0===a[e]?t:[a[e],t].flat();for(let[a,c,l]of t)if((a===e.method||"ALL"===a)&&(r=n.pathname.match(c))){e.params=r.groups||{};for(let t of l)if(void 0!==(o=await t(e.proxy||e,...s)))return o}}});class t extends Error{status;constructor(e=500,t){"object"==typeof t?(super(t.error),Object.assign(this,t)):super(t),this.status=e}}const s=(e="text/plain; charset=utf-8",t)=>(s,o={})=>{const{headers:r={},...n}=o;return"Response"===s?.constructor.name?s:new Response(t?t(s):s,{headers:{"content-type":e,...r},...n})},o=s("application/json; charset=utf-8",JSON.stringify),r=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),n=(e=500,t)=>{if(e instanceof Error){const{message:s,...o}=e;e=e.status||500,t={error:s||r(e),...o}}return t={status:e,..."object"==typeof t?t:{error:t||r(e)}},o(t,{status:e})},a=e=>new Response(null,{status:e}),c=e=>n(400,e),l=e=>n(403,e),i=e=>n(404,e),p=e=>n(401,e),u=(e,t)=>new Response(e,t),d=s("text/html"),h=s("image/jpeg"),g=s("image/png"),f=s("image/webp"),w=async e=>{e.headers.get("content-type")?.includes("json")&&(e.content=await e.json())},y=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,s])=>(e[t]=s,e)),{})},m=e=>{e.proxy=new Proxy(e.proxy||e,{get:(e,t)=>void 0!==e[t]?e[t]:e?.params?.[t]})},A=(e={})=>{const{origins:t=["*"],maxAge:s,methods:o=["GET"],headers:r={}}=e;let n;const a={"content-type":"application/json","Access-Control-Allow-Methods":o.join(", "),...r};s&&(a["Access-Control-Max-Age"]=s);return{corsify:e=>{if(!e)throw new Error("No fetch handler responded and no upstream to proxy to specified.");const{headers:t,status:s,body:o}=e;if([101,301,302,308].includes(s))return e;const r=Object.fromEntries(t);return r["access-control-allow-origin"]?e:new Response(o,{status:s,headers:{...r,...a,...n,"content-type":t.get("content-type")}})},preflight:e=>{const s=[...new Set(["OPTIONS",...o])],r=e.headers.get("origin")||"";if(n=(t.includes(r)||t.includes("*"))&&{"Access-Control-Allow-Origin":r},"OPTIONS"===e.method){if(null!==e.headers.get("Origin")&&null!==e.headers.get("Access-Control-Request-Method")&&null!==e.headers.get("Access-Control-Request-Headers")){const t={...a,"Access-Control-Allow-Methods":s.join(", "),"Access-Control-Allow-Headers":e.headers.get("Access-Control-Request-Headers"),...n};return new Response(null,{headers:t})}return new Response(null,{headers:{Allow:s.join(", ")}})}}}};export{e as Router,t as StatusError,c as badRequest,A as createCors,s as createResponse,n as error,l as forbidden,d as html,h as jpeg,o as json,i as notFound,g as png,a as status,u as text,p as unauthorized,f as webp,w as withContent,y as withCookies,m as withParams};
{
"name": "itty-router",
"version": "3.0.11-next.40",
"version": "3.0.11-next.41",
"description": "Tiny, zero-dependency API router - built for Cloudflare Workers, but works everywhere!",

@@ -5,0 +5,0 @@ "type": "module",

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