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 5.0.7 to 5.0.8

116

AutoRouter.js

@@ -1,116 +0,2 @@

'use strict';
const Router = ({ base = '', routes = [], ...other } = {}) => ({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
prop.toUpperCase?.(),
RegExp(`^${(path = (base + route)
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-ignore
handlers, // embed handlers
path, // embed clean route path
]) && receiver
}),
routes,
...other,
async fetch(request, ...args) {
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
// 1. parse query params
for (let [k, v] of url.searchParams)
query[k] = query[k] ? [].concat(query[k], v) : v;
t: try {
for (let handler of other.before || [])
if ((response = await handler(request.proxy ?? request, ...args)) != null)
break t;
// 2. then test routes
outer: for (let [method, regex, handlers, path] of routes)
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
request.params = match.groups || {}; // embed params in request
request.route = path; // embed route path in request
for (let handler of handlers)
if ((response = await handler(request.proxy ?? request, ...args)) != null)
break outer;
}
}
catch (err) {
if (!other.catch)
throw err;
response = await other.catch(err, request.proxy ?? request, ...args);
}
try {
for (let handler of other.finally || [])
response = await handler(response, request.proxy ?? request, ...args) ?? response;
}
catch (err) {
if (!other.catch)
throw err;
response = await other.catch(err, request.proxy ?? request, ...args);
}
return response;
},
});
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const json = createResponse('application/json; charset=utf-8', JSON.stringify);
const getMessage = (code) => ({
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
500: 'Internal Server Error',
})[code] || 'Unknown Error';
const error = (a = 500, b) => {
// handle passing an Error | StatusError directly in
if (a instanceof Error) {
const { message, ...err } = a;
a = a.status || 500;
b = {
error: message || getMessage(a),
...err,
};
}
b = {
status: a,
...(typeof b === 'object' ? b : { error: b || getMessage(a) }),
};
return json(b, { status: a });
};
const withParams = (request) => {
request.proxy = new Proxy(request.proxy || request, {
get: (obj, prop) => obj[prop] !== undefined
? obj[prop]?.bind?.(request) || obj[prop]
: obj?.params?.[prop]
});
};
const AutoRouter = ({ format = json, missing = () => error(404), finally: f = [], before = [], ...options } = {}) => Router({
before: [
withParams,
...before
],
catch: error,
finally: [
(r, ...args) => r ?? missing(r, ...args),
format,
...f,
],
...options,
});
exports.AutoRouter = AutoRouter;
"use strict";const r=((r="text/plain; charset=utf-8",e)=>(t,{...o}={})=>{if(void 0===t||t instanceof Response)return t;const a=new Response(e?.(t)??t,o);return a.headers.set("content-type",r),a})("application/json; charset=utf-8",JSON.stringify),e=r=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[r]||"Unknown Error"),t=(t=500,o)=>{if(t instanceof Error){const{message:r,...a}=t;t=t.status||500,o={error:r||e(t),...a}}return o={status:t,..."object"==typeof o?o:{error:o||e(t)}},r(o,{status:t})},o=r=>{r.proxy=new Proxy(r.proxy||r,{get:(e,t)=>void 0!==e[t]?e[t]?.bind?.(r)||e[t]:e?.params?.[t]})};exports.AutoRouter=({format:e=r,missing:a=(()=>t(404)),finally:n=[],before:s=[],...c}={})=>(({base:r="",routes:e=[],...t}={})=>({__proto__:new Proxy({},{get:(t,o,a,n)=>(t,...s)=>e.push([o.toUpperCase?.(),RegExp(`^${(n=(r+t).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),s,n])&&a}),routes:e,...t,async fetch(r,...o){let a,n,s=new URL(r.url),c=r.query={__proto__:null};for(let[r,e]of s.searchParams)c[r]=c[r]?[].concat(c[r],e):e;r:try{for(let e of t.before||[])if(null!=(a=await e(r.proxy??r,...o)))break r;e:for(let[t,c,f,i]of e)if((t==r.method||"ALL"==t)&&(n=s.pathname.match(c))){r.params=n.groups||{},r.route=i;for(let e of f)if(null!=(a=await e(r.proxy??r,...o)))break e}}catch(e){if(!t.catch)throw e;a=await t.catch(e,r.proxy??r,...o)}try{for(let e of t.finally||[])a=await e(a,r.proxy??r,...o)??a}catch(e){if(!t.catch)throw e;a=await t.catch(e,r.proxy??r,...o)}return a}}))({before:[o,...s],catch:t,finally:[(r,...e)=>r??a(r,...e),e,...n],...c});
//# sourceMappingURL=AutoRouter.js.map

@@ -1,66 +0,2 @@

'use strict';
// Create CORS function with default options.
const cors = (options = {}) => {
// Destructure and set defaults for options.
const { origin = '*', credentials = false, allowMethods = '*', allowHeaders, exposeHeaders, maxAge, } = options;
// create generic CORS headers
const corsHeaders = {
'access-control-allow-headers': allowHeaders?.join?.(',') ?? allowHeaders, // include allowed headers
// @ts-expect-error
'access-control-expose-headers': exposeHeaders?.join?.(',') ?? exposeHeaders, // include allowed headers
// @ts-expect-error
'access-control-allow-methods': allowMethods?.join?.(',') ?? allowMethods, // include allowed methods
'access-control-max-age': maxAge,
'access-control-allow-credentials': credentials,
};
const getAccessControlOrigin = (request) => {
const requestOrigin = request?.headers.get('origin'); // may be null if no request passed
// @ts-expect-error
if (origin === true)
return requestOrigin;
// @ts-expect-error
if (origin instanceof RegExp)
return origin.test(requestOrigin) ? requestOrigin : undefined;
// @ts-expect-error
if (Array.isArray(origin))
return origin.includes(requestOrigin) ? requestOrigin : undefined;
// @ts-expect-error
if (origin instanceof Function)
return origin(requestOrigin);
// @ts-expect-error
return origin == '*' && credentials
? requestOrigin
: origin;
};
const preflight = (request) => {
if (request.method == 'OPTIONS') {
return new Response(null, {
status: 204,
headers: Object.entries({
'access-control-allow-origin': getAccessControlOrigin(request),
...corsHeaders,
}).filter(v => v[1]),
});
} // otherwise ignore
};
const corsify = (response, request) => {
// ignore if already has CORS headers
if (response?.headers?.get('access-control-allow-origin')
|| response.status == 101)
return response;
const origin = getAccessControlOrigin(request);
if (origin)
response.headers.append('access-control-allow-origin', origin);
for (const [key, value] of Object.entries(corsHeaders)) {
if (value)
response.headers.append(key, value);
}
return response;
};
// Return corsify and preflight methods.
return { corsify, preflight };
};
exports.cors = cors;
"use strict";exports.cors=(e={})=>{const{origin:s="*",credentials:o=!1,allowMethods:r="*",allowHeaders:t,exposeHeaders:n,maxAge:c}=e,a={"access-control-allow-headers":t?.join?.(",")??t,"access-control-expose-headers":n?.join?.(",")??n,"access-control-allow-methods":r?.join?.(",")??r,"access-control-max-age":c,"access-control-allow-credentials":o},l=e=>{const r=e?.headers.get("origin");return!0===s?r:s instanceof RegExp?s.test(r)?r:void 0:Array.isArray(s)?s.includes(r)?r:void 0:s instanceof Function?s(r):"*"==s&&o?r:s};return{corsify:(e,s)=>{if(e?.headers?.get("access-control-allow-origin")||101==e.status)return e;const o=l(s);o&&e.headers.append("access-control-allow-origin",o);for(const[s,o]of Object.entries(a))o&&e.headers.append(s,o);return e},preflight:e=>{if("OPTIONS"==e.method)return new Response(null,{status:204,headers:Object.entries({"access-control-allow-origin":l(e),...a}).filter((e=>e[1]))})}}};
//# sourceMappingURL=cors.js.map

@@ -1,12 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
exports.createResponse = createResponse;
"use strict";exports.createResponse=(e="text/plain; charset=utf-8",t)=>(s,{...n}={})=>{if(void 0===s||s instanceof Response)return s;const r=new Response(t?.(s)??s,n);return r.headers.set("content-type",e),r};
//# sourceMappingURL=createResponse.js.map

@@ -1,38 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const json = createResponse('application/json; charset=utf-8', JSON.stringify);
const getMessage = (code) => ({
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
500: 'Internal Server Error',
})[code] || 'Unknown Error';
const error = (a = 500, b) => {
// handle passing an Error | StatusError directly in
if (a instanceof Error) {
const { message, ...err } = a;
a = a.status || 500;
b = {
error: message || getMessage(a),
...err,
};
}
b = {
status: a,
...(typeof b === 'object' ? b : { error: b || getMessage(a) }),
};
return json(b, { status: a });
};
exports.error = error;
"use strict";const r=((r="text/plain; charset=utf-8",t)=>(e,{...n}={})=>{if(void 0===e||e instanceof Response)return e;const s=new Response(t?.(e)??e,n);return s.headers.set("content-type",r),s})("application/json; charset=utf-8",JSON.stringify),t=r=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[r]||"Unknown Error");exports.error=(e=500,n)=>{if(e instanceof Error){const{message:r,...s}=e;e=e.status||500,n={error:r||t(e),...s}}return n={status:e,..."object"==typeof n?n:{error:n||t(e)}},r(n,{status:e})};
//# sourceMappingURL=error.js.map

@@ -1,14 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const html = createResponse('text/html');
exports.html = html;
"use strict";const t=((t="text/plain; charset=utf-8",e)=>(s,{...n}={})=>{if(void 0===s||s instanceof Response)return s;const o=new Response(e?.(s)??s,n);return o.headers.set("content-type",t),o})("text/html");exports.html=t;
//# sourceMappingURL=html.js.map

@@ -1,270 +0,2 @@

'use strict';
const IttyRouter = ({ base = '', routes = [], ...other } = {}) =>
// @ts-ignore
({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
prop.toUpperCase(),
RegExp(`^${(path = (base + route)
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-ignore
handlers, // embed handlers
path, // embed clean route path
]) && receiver
}),
routes,
...other,
async fetch(request, ...args) {
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
// 1. parse query params
for (let [k, v] of url.searchParams)
query[k] = query[k] ? [].concat(query[k], v) : v;
// 2. then test routes
for (let [method, regex, handlers, path] of routes)
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
request.params = match.groups || {}; // embed params in request
request.route = path; // embed route path in request
for (let handler of handlers)
if ((response = await handler(request.proxy ?? request, ...args)) != null)
return response;
}
},
});
const Router = ({ base = '', routes = [], ...other } = {}) => ({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
prop.toUpperCase?.(),
RegExp(`^${(path = (base + route)
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-ignore
handlers, // embed handlers
path, // embed clean route path
]) && receiver
}),
routes,
...other,
async fetch(request, ...args) {
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
// 1. parse query params
for (let [k, v] of url.searchParams)
query[k] = query[k] ? [].concat(query[k], v) : v;
t: try {
for (let handler of other.before || [])
if ((response = await handler(request.proxy ?? request, ...args)) != null)
break t;
// 2. then test routes
outer: for (let [method, regex, handlers, path] of routes)
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
request.params = match.groups || {}; // embed params in request
request.route = path; // embed route path in request
for (let handler of handlers)
if ((response = await handler(request.proxy ?? request, ...args)) != null)
break outer;
}
}
catch (err) {
if (!other.catch)
throw err;
response = await other.catch(err, request.proxy ?? request, ...args);
}
try {
for (let handler of other.finally || [])
response = await handler(response, request.proxy ?? request, ...args) ?? response;
}
catch (err) {
if (!other.catch)
throw err;
response = await other.catch(err, request.proxy ?? request, ...args);
}
return response;
},
});
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const json = createResponse('application/json; charset=utf-8', JSON.stringify);
const getMessage = (code) => ({
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
500: 'Internal Server Error',
})[code] || 'Unknown Error';
const error = (a = 500, b) => {
// handle passing an Error | StatusError directly in
if (a instanceof Error) {
const { message, ...err } = a;
a = a.status || 500;
b = {
error: message || getMessage(a),
...err,
};
}
b = {
status: a,
...(typeof b === 'object' ? b : { error: b || getMessage(a) }),
};
return json(b, { status: a });
};
const withParams = (request) => {
request.proxy = new Proxy(request.proxy || request, {
get: (obj, prop) => obj[prop] !== undefined
? obj[prop]?.bind?.(request) || obj[prop]
: obj?.params?.[prop]
});
};
const AutoRouter = ({ format = json, missing = () => error(404), finally: f = [], before = [], ...options } = {}) => Router({
before: [
withParams,
...before
],
catch: error,
finally: [
(r, ...args) => r ?? missing(r, ...args),
format,
...f,
],
...options,
});
class StatusError extends Error {
status;
constructor(status = 500, body) {
super(typeof body === 'object' ? body.error : body);
typeof body === 'object' && Object.assign(this, body);
this.status = status;
}
}
const status = (status, options) => new Response(null, { ...options, status });
const text = createResponse('text/plain; charset=utf-8', String);
const html = createResponse('text/html');
const jpeg = createResponse('image/jpeg');
const png = createResponse('image/png');
const webp = createResponse('image/webp');
// withContent - embeds any request body as request.content
const withContent = async (request) => {
request.content = request.body
? await request.clone().json()
.catch(() => request.clone().formData())
.catch(() => request.text())
: undefined;
};
// withCookies - embeds cookies object into the request
const withCookies = (r) => {
r.cookies = (r.headers.get('Cookie') || '')
.split(/;\s*/)
.map((p) => p.split(/=(.+)/))
.reduce((a, [k, v]) => (v ? ((a[k] = v), a) : a), {});
};
// Create CORS function with default options.
const cors = (options = {}) => {
// Destructure and set defaults for options.
const { origin = '*', credentials = false, allowMethods = '*', allowHeaders, exposeHeaders, maxAge, } = options;
// create generic CORS headers
const corsHeaders = {
'access-control-allow-headers': allowHeaders?.join?.(',') ?? allowHeaders, // include allowed headers
// @ts-expect-error
'access-control-expose-headers': exposeHeaders?.join?.(',') ?? exposeHeaders, // include allowed headers
// @ts-expect-error
'access-control-allow-methods': allowMethods?.join?.(',') ?? allowMethods, // include allowed methods
'access-control-max-age': maxAge,
'access-control-allow-credentials': credentials,
};
const getAccessControlOrigin = (request) => {
const requestOrigin = request?.headers.get('origin'); // may be null if no request passed
// @ts-expect-error
if (origin === true)
return requestOrigin;
// @ts-expect-error
if (origin instanceof RegExp)
return origin.test(requestOrigin) ? requestOrigin : undefined;
// @ts-expect-error
if (Array.isArray(origin))
return origin.includes(requestOrigin) ? requestOrigin : undefined;
// @ts-expect-error
if (origin instanceof Function)
return origin(requestOrigin);
// @ts-expect-error
return origin == '*' && credentials
? requestOrigin
: origin;
};
const preflight = (request) => {
if (request.method == 'OPTIONS') {
return new Response(null, {
status: 204,
headers: Object.entries({
'access-control-allow-origin': getAccessControlOrigin(request),
...corsHeaders,
}).filter(v => v[1]),
});
} // otherwise ignore
};
const corsify = (response, request) => {
// ignore if already has CORS headers
if (response?.headers?.get('access-control-allow-origin')
|| response.status == 101)
return response;
const origin = getAccessControlOrigin(request);
if (origin)
response.headers.append('access-control-allow-origin', origin);
for (const [key, value] of Object.entries(corsHeaders)) {
if (value)
response.headers.append(key, value);
}
return response;
};
// Return corsify and preflight methods.
return { corsify, preflight };
};
exports.AutoRouter = AutoRouter;
exports.IttyRouter = IttyRouter;
exports.Router = Router;
exports.StatusError = StatusError;
exports.cors = cors;
exports.createResponse = createResponse;
exports.error = error;
exports.html = html;
exports.jpeg = jpeg;
exports.json = json;
exports.png = png;
exports.status = status;
exports.text = text;
exports.webp = webp;
exports.withContent = withContent;
exports.withCookies = withCookies;
exports.withParams = withParams;
"use strict";const e=({base:e="",routes:t=[],...r}={})=>({__proto__:new Proxy({},{get:(r,o,s,a)=>(r,...n)=>t.push([o.toUpperCase?.(),RegExp(`^${(a=(e+r).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),n,a])&&s}),routes:t,...r,async fetch(e,...o){let s,a,n=new URL(e.url),c=e.query={__proto__:null};for(let[e,t]of n.searchParams)c[e]=c[e]?[].concat(c[e],t):t;e:try{for(let t of r.before||[])if(null!=(s=await t(e.proxy??e,...o)))break e;t:for(let[r,c,p,l]of t)if((r==e.method||"ALL"==r)&&(a=n.pathname.match(c))){e.params=a.groups||{},e.route=l;for(let t of p)if(null!=(s=await t(e.proxy??e,...o)))break t}}catch(t){if(!r.catch)throw t;s=await r.catch(t,e.proxy??e,...o)}try{for(let t of r.finally||[])s=await t(s,e.proxy??e,...o)??s}catch(t){if(!r.catch)throw t;s=await r.catch(t,e.proxy??e,...o)}return s}}),t=(e="text/plain; charset=utf-8",t)=>(r,{...o}={})=>{if(void 0===r||r instanceof Response)return r;const s=new Response(t?.(r)??r,o);return s.headers.set("content-type",e),s},r=t("application/json; charset=utf-8",JSON.stringify),o=e=>({400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error"}[e]||"Unknown Error"),s=(e=500,t)=>{if(e instanceof Error){const{message:r,...s}=e;e=e.status||500,t={error:r||o(e),...s}}return t={status:e,..."object"==typeof t?t:{error:t||o(e)}},r(t,{status:e})},a=e=>{e.proxy=new Proxy(e.proxy||e,{get:(t,r)=>void 0!==t[r]?t[r]?.bind?.(e)||t[r]:t?.params?.[r]})};class n extends Error{status;constructor(e=500,t){super("object"==typeof t?t.error:t),"object"==typeof t&&Object.assign(this,t),this.status=e}}const c=t("text/plain; charset=utf-8",String),p=t("text/html"),l=t("image/jpeg"),i=t("image/png"),u=t("image/webp");exports.AutoRouter=({format:t=r,missing:o=(()=>s(404)),finally:n=[],before:c=[],...p}={})=>e({before:[a,...c],catch:s,finally:[(e,...t)=>e??o(e,...t),t,...n],...p}),exports.IttyRouter=({base:e="",routes:t=[],...r}={})=>({__proto__:new Proxy({},{get:(r,o,s,a)=>(r,...n)=>t.push([o.toUpperCase(),RegExp(`^${(a=(e+r).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),n,a])&&s}),routes:t,...r,async fetch(e,...r){let o,s,a=new URL(e.url),n=e.query={__proto__:null};for(let[e,t]of a.searchParams)n[e]=n[e]?[].concat(n[e],t):t;for(let[n,c,p,l]of t)if((n==e.method||"ALL"==n)&&(s=a.pathname.match(c))){e.params=s.groups||{},e.route=l;for(let t of p)if(null!=(o=await t(e.proxy??e,...r)))return o}}}),exports.Router=e,exports.StatusError=n,exports.cors=(e={})=>{const{origin:t="*",credentials:r=!1,allowMethods:o="*",allowHeaders:s,exposeHeaders:a,maxAge:n}=e,c={"access-control-allow-headers":s?.join?.(",")??s,"access-control-expose-headers":a?.join?.(",")??a,"access-control-allow-methods":o?.join?.(",")??o,"access-control-max-age":n,"access-control-allow-credentials":r},p=e=>{const o=e?.headers.get("origin");return!0===t?o:t instanceof RegExp?t.test(o)?o:void 0:Array.isArray(t)?t.includes(o)?o:void 0:t instanceof Function?t(o):"*"==t&&r?o:t};return{corsify:(e,t)=>{if(e?.headers?.get("access-control-allow-origin")||101==e.status)return e;const r=p(t);r&&e.headers.append("access-control-allow-origin",r);for(const[t,r]of Object.entries(c))r&&e.headers.append(t,r);return e},preflight:e=>{if("OPTIONS"==e.method)return new Response(null,{status:204,headers:Object.entries({"access-control-allow-origin":p(e),...c}).filter((e=>e[1]))})}}},exports.createResponse=t,exports.error=s,exports.html=p,exports.jpeg=l,exports.json=r,exports.png=i,exports.status=(e,t)=>new Response(null,{...t,status:e}),exports.text=c,exports.webp=u,exports.withContent=async e=>{e.content=e.body?await e.clone().json().catch((()=>e.clone().formData())).catch((()=>e.text())):void 0},exports.withCookies=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[t,r])=>r?(e[t]=r,e):e),{})},exports.withParams=a;
//# sourceMappingURL=index.js.map

@@ -1,42 +0,2 @@

'use strict';
const IttyRouter = ({ base = '', routes = [], ...other } = {}) =>
// @ts-ignore
({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
prop.toUpperCase(),
RegExp(`^${(path = (base + route)
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-ignore
handlers, // embed handlers
path, // embed clean route path
]) && receiver
}),
routes,
...other,
async fetch(request, ...args) {
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
// 1. parse query params
for (let [k, v] of url.searchParams)
query[k] = query[k] ? [].concat(query[k], v) : v;
// 2. then test routes
for (let [method, regex, handlers, path] of routes)
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
request.params = match.groups || {}; // embed params in request
request.route = path; // embed route path in request
for (let handler of handlers)
if ((response = await handler(request.proxy ?? request, ...args)) != null)
return response;
}
},
});
exports.IttyRouter = IttyRouter;
"use strict";exports.IttyRouter=({base:e="",routes:r=[],...t}={})=>({__proto__:new Proxy({},{get:(t,o,a,p)=>(t,...l)=>r.push([o.toUpperCase(),RegExp(`^${(p=(e+t).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),l,p])&&a}),routes:r,...t,async fetch(e,...t){let o,a,p=new URL(e.url),l=e.query={__proto__:null};for(let[e,r]of p.searchParams)l[e]=l[e]?[].concat(l[e],r):r;for(let[l,s,c,u]of r)if((l==e.method||"ALL"==l)&&(a=p.pathname.match(s))){e.params=a.groups||{},e.route=u;for(let r of c)if(null!=(o=await r(e.proxy??e,...t)))return o}}});
//# sourceMappingURL=IttyRouter.js.map

@@ -1,14 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const jpeg = createResponse('image/jpeg');
exports.jpeg = jpeg;
"use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{...n}={})=>{if(void 0===s||s instanceof Response)return s;const o=new Response(t?.(s)??s,n);return o.headers.set("content-type",e),o})("image/jpeg");exports.jpeg=e;
//# sourceMappingURL=jpeg.js.map

@@ -1,14 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const json = createResponse('application/json; charset=utf-8', JSON.stringify);
exports.json = json;
"use strict";const t=((t="text/plain; charset=utf-8",e)=>(s,{...n}={})=>{if(void 0===s||s instanceof Response)return s;const o=new Response(e?.(s)??s,n);return o.headers.set("content-type",t),o})("application/json; charset=utf-8",JSON.stringify);exports.json=t;
//# sourceMappingURL=json.js.map

2

package.json
{
"name": "itty-router",
"version": "5.0.7",
"version": "5.0.8",
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",

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

@@ -1,14 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const png = createResponse('image/png');
exports.png = png;
"use strict";const e=((e="text/plain; charset=utf-8",t)=>(n,{...s}={})=>{if(void 0===n||n instanceof Response)return n;const o=new Response(t?.(n)??n,s);return o.headers.set("content-type",e),o})("image/png");exports.png=e;
//# sourceMappingURL=png.js.map

@@ -1,60 +0,2 @@

'use strict';
const Router = ({ base = '', routes = [], ...other } = {}) => ({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
prop.toUpperCase?.(),
RegExp(`^${(path = (base + route)
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-ignore
handlers, // embed handlers
path, // embed clean route path
]) && receiver
}),
routes,
...other,
async fetch(request, ...args) {
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
// 1. parse query params
for (let [k, v] of url.searchParams)
query[k] = query[k] ? [].concat(query[k], v) : v;
t: try {
for (let handler of other.before || [])
if ((response = await handler(request.proxy ?? request, ...args)) != null)
break t;
// 2. then test routes
outer: for (let [method, regex, handlers, path] of routes)
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
request.params = match.groups || {}; // embed params in request
request.route = path; // embed route path in request
for (let handler of handlers)
if ((response = await handler(request.proxy ?? request, ...args)) != null)
break outer;
}
}
catch (err) {
if (!other.catch)
throw err;
response = await other.catch(err, request.proxy ?? request, ...args);
}
try {
for (let handler of other.finally || [])
response = await handler(response, request.proxy ?? request, ...args) ?? response;
}
catch (err) {
if (!other.catch)
throw err;
response = await other.catch(err, request.proxy ?? request, ...args);
}
return response;
},
});
exports.Router = Router;
"use strict";exports.Router=({base:r="",routes:e=[],...t}={})=>({__proto__:new Proxy({},{get:(t,a,o,c)=>(t,...l)=>e.push([a.toUpperCase?.(),RegExp(`^${(c=(r+t).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),l,c])&&o}),routes:e,...t,async fetch(r,...a){let o,c,l=new URL(r.url),p=r.query={__proto__:null};for(let[r,e]of l.searchParams)p[r]=p[r]?[].concat(p[r],e):e;r:try{for(let e of t.before||[])if(null!=(o=await e(r.proxy??r,...a)))break r;e:for(let[t,p,f,h]of e)if((t==r.method||"ALL"==t)&&(c=l.pathname.match(p))){r.params=c.groups||{},r.route=h;for(let e of f)if(null!=(o=await e(r.proxy??r,...a)))break e}}catch(e){if(!t.catch)throw e;o=await t.catch(e,r.proxy??r,...a)}try{for(let e of t.finally||[])o=await e(o,r.proxy??r,...a)??o}catch(e){if(!t.catch)throw e;o=await t.catch(e,r.proxy??r,...a)}return o}});
//# sourceMappingURL=Router.js.map

@@ -1,6 +0,2 @@

'use strict';
const status = (status, options) => new Response(null, { ...options, status });
exports.status = status;
"use strict";exports.status=(s,t)=>new Response(null,{...t,status:s});
//# sourceMappingURL=status.js.map

@@ -1,13 +0,2 @@

'use strict';
class StatusError extends Error {
status;
constructor(status = 500, body) {
super(typeof body === 'object' ? body.error : body);
typeof body === 'object' && Object.assign(this, body);
this.status = status;
}
}
exports.StatusError = StatusError;
"use strict";class t extends Error{status;constructor(t=500,s){super("object"==typeof s?s.error:s),"object"==typeof s&&Object.assign(this,s),this.status=t}}exports.StatusError=t;
//# sourceMappingURL=StatusError.js.map

@@ -1,14 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const text = createResponse('text/plain; charset=utf-8', String);
exports.text = text;
"use strict";const t=((t="text/plain; charset=utf-8",e)=>(n,{...s}={})=>{if(void 0===n||n instanceof Response)return n;const r=new Response(e?.(n)??n,s);return r.headers.set("content-type",t),r})("text/plain; charset=utf-8",String);exports.text=t;
//# sourceMappingURL=text.js.map

@@ -1,14 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const webp = createResponse('image/webp');
exports.webp = webp;
"use strict";const e=((e="text/plain; charset=utf-8",t)=>(s,{...n}={})=>{if(void 0===s||s instanceof Response)return s;const o=new Response(t?.(s)??s,n);return o.headers.set("content-type",e),o})("image/webp");exports.webp=e;
//# sourceMappingURL=webp.js.map

@@ -1,18 +0,2 @@

'use strict';
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
if (body === undefined || body instanceof Response)
return body;
const response = new Response(transform?.(body) ?? body, options);
response.headers.set('content-type', format);
return response;
};
const websocket = (client, options = {}) => createResponse()(null, {
status: 101,
webSocket: client,
...options,
});
exports.websocket = websocket;
"use strict";exports.websocket=(e,t={})=>((e="text/plain; charset=utf-8",t)=>(s,{...n}={})=>{if(void 0===s||s instanceof Response)return s;const o=new Response(t?.(s)??s,n);return o.headers.set("content-type",e),o})()(null,{status:101,webSocket:e,...t});
//# sourceMappingURL=websocket.js.map

@@ -1,13 +0,2 @@

'use strict';
// withContent - embeds any request body as request.content
const withContent = async (request) => {
request.content = request.body
? await request.clone().json()
.catch(() => request.clone().formData())
.catch(() => request.text())
: undefined;
};
exports.withContent = withContent;
"use strict";exports.withContent=async t=>{t.content=t.body?await t.clone().json().catch((()=>t.clone().formData())).catch((()=>t.text())):void 0};
//# sourceMappingURL=withContent.js.map

@@ -1,12 +0,2 @@

'use strict';
// withCookies - embeds cookies object into the request
const withCookies = (r) => {
r.cookies = (r.headers.get('Cookie') || '')
.split(/;\s*/)
.map((p) => p.split(/=(.+)/))
.reduce((a, [k, v]) => (v ? ((a[k] = v), a) : a), {});
};
exports.withCookies = withCookies;
"use strict";exports.withCookies=e=>{e.cookies=(e.headers.get("Cookie")||"").split(/;\s*/).map((e=>e.split(/=(.+)/))).reduce(((e,[s,i])=>i?(e[s]=i,e):e),{})};
//# sourceMappingURL=withCookies.js.map

@@ -1,12 +0,2 @@

'use strict';
const withParams = (request) => {
request.proxy = new Proxy(request.proxy || request, {
get: (obj, prop) => obj[prop] !== undefined
? obj[prop]?.bind?.(request) || obj[prop]
: obj?.params?.[prop]
});
};
exports.withParams = withParams;
"use strict";exports.withParams=r=>{r.proxy=new Proxy(r.proxy||r,{get:(o,s)=>void 0!==o[s]?o[s]?.bind?.(r)||o[s]:o?.params?.[s]})};
//# sourceMappingURL=withParams.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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