itty-router 5.0.7 → 5.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AutoRouter.js +1 -115
- package/AutoRouter.js.map +1 -1
- package/AutoRouter.mjs +1 -113
- package/AutoRouter.mjs.map +1 -1
- package/IttyRouter.js +1 -41
- package/IttyRouter.js.map +1 -1
- package/IttyRouter.mjs +1 -39
- package/IttyRouter.mjs.map +1 -1
- package/Router.js +1 -59
- package/Router.js.map +1 -1
- package/Router.mjs +1 -57
- package/Router.mjs.map +1 -1
- package/StatusError.js +1 -12
- package/StatusError.js.map +1 -1
- package/StatusError.mjs +1 -10
- package/StatusError.mjs.map +1 -1
- package/cors.js +1 -65
- package/cors.js.map +1 -1
- package/cors.mjs +1 -63
- package/cors.mjs.map +1 -1
- package/createResponse.js +1 -11
- package/createResponse.js.map +1 -1
- package/createResponse.mjs +1 -9
- package/createResponse.mjs.map +1 -1
- package/error.js +1 -37
- package/error.js.map +1 -1
- package/error.mjs +1 -35
- package/error.mjs.map +1 -1
- package/html.js +1 -13
- package/html.js.map +1 -1
- package/html.mjs +1 -11
- package/html.mjs.map +1 -1
- package/index.js +1 -269
- package/index.js.map +1 -1
- package/index.mjs +1 -251
- package/index.mjs.map +1 -1
- package/jpeg.js +1 -13
- package/jpeg.js.map +1 -1
- package/jpeg.mjs +1 -11
- package/jpeg.mjs.map +1 -1
- package/json.js +1 -13
- package/json.js.map +1 -1
- package/json.mjs +1 -11
- package/json.mjs.map +1 -1
- package/package.json +1 -1
- package/png.js +1 -13
- package/png.js.map +1 -1
- package/png.mjs +1 -11
- package/png.mjs.map +1 -1
- package/status.js +1 -5
- package/status.js.map +1 -1
- package/status.mjs +1 -3
- package/status.mjs.map +1 -1
- package/text.js +1 -13
- package/text.js.map +1 -1
- package/text.mjs +1 -11
- package/text.mjs.map +1 -1
- package/webp.js +1 -13
- package/webp.js.map +1 -1
- package/webp.mjs +1 -11
- package/webp.mjs.map +1 -1
- package/websocket.js +1 -17
- package/websocket.js.map +1 -1
- package/websocket.mjs +1 -15
- package/websocket.mjs.map +1 -1
- package/withContent.js +1 -12
- package/withContent.js.map +1 -1
- package/withContent.mjs +1 -10
- package/withContent.mjs.map +1 -1
- package/withCookies.js +1 -11
- package/withCookies.js.map +1 -1
- package/withCookies.mjs +1 -9
- package/withCookies.mjs.map +1 -1
- package/withParams.js +1 -11
- package/withParams.js.map +1 -1
- package/withParams.mjs +1 -9
- package/withParams.mjs.map +1 -1
package/AutoRouter.js
CHANGED
|
@@ -1,116 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const Router = ({ base = '', routes = [], ...other } = {}) => ({
|
|
4
|
-
__proto__: new Proxy({}, {
|
|
5
|
-
// @ts-expect-error (we're adding an expected prop "path" to the get)
|
|
6
|
-
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
|
|
7
|
-
prop.toUpperCase?.(),
|
|
8
|
-
RegExp(`^${(path = (base + route)
|
|
9
|
-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
|
|
10
|
-
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
|
|
11
|
-
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
|
|
12
|
-
.replace(/\./g, '\\.') // dot in path
|
|
13
|
-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
|
|
14
|
-
}/*$`),
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
handlers, // embed handlers
|
|
17
|
-
path, // embed clean route path
|
|
18
|
-
]) && receiver
|
|
19
|
-
}),
|
|
20
|
-
routes,
|
|
21
|
-
...other,
|
|
22
|
-
async fetch(request, ...args) {
|
|
23
|
-
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
|
|
24
|
-
// 1. parse query params
|
|
25
|
-
for (let [k, v] of url.searchParams)
|
|
26
|
-
query[k] = query[k] ? [].concat(query[k], v) : v;
|
|
27
|
-
t: try {
|
|
28
|
-
for (let handler of other.before || [])
|
|
29
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
30
|
-
break t;
|
|
31
|
-
// 2. then test routes
|
|
32
|
-
outer: for (let [method, regex, handlers, path] of routes)
|
|
33
|
-
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
|
|
34
|
-
request.params = match.groups || {}; // embed params in request
|
|
35
|
-
request.route = path; // embed route path in request
|
|
36
|
-
for (let handler of handlers)
|
|
37
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
38
|
-
break outer;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
catch (err) {
|
|
42
|
-
if (!other.catch)
|
|
43
|
-
throw err;
|
|
44
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
45
|
-
}
|
|
46
|
-
try {
|
|
47
|
-
for (let handler of other.finally || [])
|
|
48
|
-
response = await handler(response, request.proxy ?? request, ...args) ?? response;
|
|
49
|
-
}
|
|
50
|
-
catch (err) {
|
|
51
|
-
if (!other.catch)
|
|
52
|
-
throw err;
|
|
53
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
54
|
-
}
|
|
55
|
-
return response;
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
|
|
60
|
-
if (body === undefined || body instanceof Response)
|
|
61
|
-
return body;
|
|
62
|
-
const response = new Response(transform?.(body) ?? body, options);
|
|
63
|
-
response.headers.set('content-type', format);
|
|
64
|
-
return response;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const json = createResponse('application/json; charset=utf-8', JSON.stringify);
|
|
68
|
-
|
|
69
|
-
const getMessage = (code) => ({
|
|
70
|
-
400: 'Bad Request',
|
|
71
|
-
401: 'Unauthorized',
|
|
72
|
-
403: 'Forbidden',
|
|
73
|
-
404: 'Not Found',
|
|
74
|
-
500: 'Internal Server Error',
|
|
75
|
-
})[code] || 'Unknown Error';
|
|
76
|
-
const error = (a = 500, b) => {
|
|
77
|
-
// handle passing an Error | StatusError directly in
|
|
78
|
-
if (a instanceof Error) {
|
|
79
|
-
const { message, ...err } = a;
|
|
80
|
-
a = a.status || 500;
|
|
81
|
-
b = {
|
|
82
|
-
error: message || getMessage(a),
|
|
83
|
-
...err,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
b = {
|
|
87
|
-
status: a,
|
|
88
|
-
...(typeof b === 'object' ? b : { error: b || getMessage(a) }),
|
|
89
|
-
};
|
|
90
|
-
return json(b, { status: a });
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const withParams = (request) => {
|
|
94
|
-
request.proxy = new Proxy(request.proxy || request, {
|
|
95
|
-
get: (obj, prop) => obj[prop] !== undefined
|
|
96
|
-
? obj[prop]?.bind?.(request) || obj[prop]
|
|
97
|
-
: obj?.params?.[prop]
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const AutoRouter = ({ format = json, missing = () => error(404), finally: f = [], before = [], ...options } = {}) => Router({
|
|
102
|
-
before: [
|
|
103
|
-
withParams,
|
|
104
|
-
...before
|
|
105
|
-
],
|
|
106
|
-
catch: error,
|
|
107
|
-
finally: [
|
|
108
|
-
(r, ...args) => r ?? missing(r, ...args),
|
|
109
|
-
format,
|
|
110
|
-
...f,
|
|
111
|
-
],
|
|
112
|
-
...options,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
exports.AutoRouter = AutoRouter;
|
|
1
|
+
"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});
|
|
116
2
|
//# sourceMappingURL=AutoRouter.js.map
|
package/AutoRouter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AutoRouter.js","sources":["../src/src/Router.ts","../src/src/
|
|
1
|
+
{"version":3,"file":"AutoRouter.js","sources":["../src/src/Router.ts","../src/src/json.ts","../src/src/createResponse.ts","../src/src/error.ts","../src/src/withParams.ts","../src/src/AutoRouter.ts"],"sourcesContent":[null,null,null,null,null,null],"names":["json","format","transform","body","options","undefined","Response","response","headers","set","createResponse","JSON","stringify","getMessage","code","error","a","b","Error","message","err","status","withParams","request","proxy","Proxy","get","obj","prop","bind","params","missing","finally","f","before","base","routes","other","__proto__","target","receiver","path","route","handlers","push","toUpperCase","RegExp","replace","fetch","args","match","url","URL","query","k","v","searchParams","concat","t","handler","outer","method","regex","pathname","groups","catch","Router","r"],"mappings":"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"}
|
package/AutoRouter.mjs
CHANGED
|
@@ -1,114 +1,2 @@
|
|
|
1
|
-
const
|
|
2
|
-
__proto__: new Proxy({}, {
|
|
3
|
-
// @ts-expect-error (we're adding an expected prop "path" to the get)
|
|
4
|
-
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
|
|
5
|
-
prop.toUpperCase?.(),
|
|
6
|
-
RegExp(`^${(path = (base + route)
|
|
7
|
-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
|
|
8
|
-
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
|
|
9
|
-
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
|
|
10
|
-
.replace(/\./g, '\\.') // dot in path
|
|
11
|
-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
|
|
12
|
-
}/*$`),
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
handlers, // embed handlers
|
|
15
|
-
path, // embed clean route path
|
|
16
|
-
]) && receiver
|
|
17
|
-
}),
|
|
18
|
-
routes,
|
|
19
|
-
...other,
|
|
20
|
-
async fetch(request, ...args) {
|
|
21
|
-
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
|
|
22
|
-
// 1. parse query params
|
|
23
|
-
for (let [k, v] of url.searchParams)
|
|
24
|
-
query[k] = query[k] ? [].concat(query[k], v) : v;
|
|
25
|
-
t: try {
|
|
26
|
-
for (let handler of other.before || [])
|
|
27
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
28
|
-
break t;
|
|
29
|
-
// 2. then test routes
|
|
30
|
-
outer: for (let [method, regex, handlers, path] of routes)
|
|
31
|
-
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
|
|
32
|
-
request.params = match.groups || {}; // embed params in request
|
|
33
|
-
request.route = path; // embed route path in request
|
|
34
|
-
for (let handler of handlers)
|
|
35
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
36
|
-
break outer;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
if (!other.catch)
|
|
41
|
-
throw err;
|
|
42
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
43
|
-
}
|
|
44
|
-
try {
|
|
45
|
-
for (let handler of other.finally || [])
|
|
46
|
-
response = await handler(response, request.proxy ?? request, ...args) ?? response;
|
|
47
|
-
}
|
|
48
|
-
catch (err) {
|
|
49
|
-
if (!other.catch)
|
|
50
|
-
throw err;
|
|
51
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
52
|
-
}
|
|
53
|
-
return response;
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
const createResponse = (format = 'text/plain; charset=utf-8', transform) => (body, { ...options } = {}) => {
|
|
58
|
-
if (body === undefined || body instanceof Response)
|
|
59
|
-
return body;
|
|
60
|
-
const response = new Response(transform?.(body) ?? body, options);
|
|
61
|
-
response.headers.set('content-type', format);
|
|
62
|
-
return response;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const json = createResponse('application/json; charset=utf-8', JSON.stringify);
|
|
66
|
-
|
|
67
|
-
const getMessage = (code) => ({
|
|
68
|
-
400: 'Bad Request',
|
|
69
|
-
401: 'Unauthorized',
|
|
70
|
-
403: 'Forbidden',
|
|
71
|
-
404: 'Not Found',
|
|
72
|
-
500: 'Internal Server Error',
|
|
73
|
-
})[code] || 'Unknown Error';
|
|
74
|
-
const error = (a = 500, b) => {
|
|
75
|
-
// handle passing an Error | StatusError directly in
|
|
76
|
-
if (a instanceof Error) {
|
|
77
|
-
const { message, ...err } = a;
|
|
78
|
-
a = a.status || 500;
|
|
79
|
-
b = {
|
|
80
|
-
error: message || getMessage(a),
|
|
81
|
-
...err,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
b = {
|
|
85
|
-
status: a,
|
|
86
|
-
...(typeof b === 'object' ? b : { error: b || getMessage(a) }),
|
|
87
|
-
};
|
|
88
|
-
return json(b, { status: a });
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const withParams = (request) => {
|
|
92
|
-
request.proxy = new Proxy(request.proxy || request, {
|
|
93
|
-
get: (obj, prop) => obj[prop] !== undefined
|
|
94
|
-
? obj[prop]?.bind?.(request) || obj[prop]
|
|
95
|
-
: obj?.params?.[prop]
|
|
96
|
-
});
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const AutoRouter = ({ format = json, missing = () => error(404), finally: f = [], before = [], ...options } = {}) => Router({
|
|
100
|
-
before: [
|
|
101
|
-
withParams,
|
|
102
|
-
...before
|
|
103
|
-
],
|
|
104
|
-
catch: error,
|
|
105
|
-
finally: [
|
|
106
|
-
(r, ...args) => r ?? missing(r, ...args),
|
|
107
|
-
format,
|
|
108
|
-
...f,
|
|
109
|
-
],
|
|
110
|
-
...options,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
export { AutoRouter };
|
|
1
|
+
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]})},a=({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,p]of e)if((t==r.method||"ALL"==t)&&(n=s.pathname.match(c))){r.params=n.groups||{},r.route=p;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});export{a as AutoRouter};
|
|
114
2
|
//# sourceMappingURL=AutoRouter.mjs.map
|
package/AutoRouter.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AutoRouter.mjs","sources":["../src/src/Router.ts","../src/src/
|
|
1
|
+
{"version":3,"file":"AutoRouter.mjs","sources":["../src/src/Router.ts","../src/src/json.ts","../src/src/createResponse.ts","../src/src/error.ts","../src/src/withParams.ts","../src/src/AutoRouter.ts"],"sourcesContent":[null,null,null,null,null,null],"names":["json","format","transform","body","options","undefined","Response","response","headers","set","createResponse","JSON","stringify","getMessage","code","error","a","b","Error","message","err","status","withParams","request","proxy","Proxy","get","obj","prop","bind","params","AutoRouter","missing","finally","f","before","base","routes","other","__proto__","target","receiver","path","route","handlers","push","toUpperCase","RegExp","replace","fetch","args","match","url","URL","query","k","v","searchParams","concat","t","handler","outer","method","regex","pathname","groups","catch","Router","r"],"mappings":"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"}
|
package/IttyRouter.js
CHANGED
|
@@ -1,42 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const IttyRouter = ({ base = '', routes = [], ...other } = {}) =>
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
({
|
|
6
|
-
__proto__: new Proxy({}, {
|
|
7
|
-
// @ts-expect-error (we're adding an expected prop "path" to the get)
|
|
8
|
-
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
|
|
9
|
-
prop.toUpperCase(),
|
|
10
|
-
RegExp(`^${(path = (base + route)
|
|
11
|
-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
|
|
12
|
-
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
|
|
13
|
-
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
|
|
14
|
-
.replace(/\./g, '\\.') // dot in path
|
|
15
|
-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
|
|
16
|
-
}/*$`),
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
handlers, // embed handlers
|
|
19
|
-
path, // embed clean route path
|
|
20
|
-
]) && receiver
|
|
21
|
-
}),
|
|
22
|
-
routes,
|
|
23
|
-
...other,
|
|
24
|
-
async fetch(request, ...args) {
|
|
25
|
-
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
|
|
26
|
-
// 1. parse query params
|
|
27
|
-
for (let [k, v] of url.searchParams)
|
|
28
|
-
query[k] = query[k] ? [].concat(query[k], v) : v;
|
|
29
|
-
// 2. then test routes
|
|
30
|
-
for (let [method, regex, handlers, path] of routes)
|
|
31
|
-
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
|
|
32
|
-
request.params = match.groups || {}; // embed params in request
|
|
33
|
-
request.route = path; // embed route path in request
|
|
34
|
-
for (let handler of handlers)
|
|
35
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
36
|
-
return response;
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
exports.IttyRouter = IttyRouter;
|
|
1
|
+
"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}}});
|
|
42
2
|
//# sourceMappingURL=IttyRouter.js.map
|
package/IttyRouter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IttyRouter.js","sources":["../src/src/IttyRouter.ts"],"sourcesContent":[null],"names":[
|
|
1
|
+
{"version":3,"file":"IttyRouter.js","sources":["../src/src/IttyRouter.ts"],"sourcesContent":[null],"names":["base","routes","other","__proto__","Proxy","get","target","prop","receiver","path","route","handlers","push","toUpperCase","RegExp","replace","fetch","request","args","response","match","url","URL","query","k","v","searchParams","concat","method","regex","pathname","params","groups","handler","proxy"],"mappings":"gCAQ0B,EAKtBA,OAAO,GAAIC,SAAS,MAAOC,GAA6B,CAAE,KAE5D,CACEC,UAAW,IAAIC,MAAM,GAAI,CAEvBC,IAAK,CAACC,EAAaC,EAAcC,EAAkBC,IACjD,CAACC,KAAkBC,IACjBV,EAAOW,KACL,CACEL,EAAKM,cACLC,OAAO,KAAKL,GAAQT,EAAOU,GACxBK,QAAQ,aAAc,OACtBA,QAAQ,oBAAqB,gBAC7BA,QAAQ,kBAAmB,uBAC3BA,QAAQ,MAAO,OACfA,QAAQ,WAAY,iBAGvBJ,EACAF,KAECD,IAEXP,YACGC,EACH,WAAMc,CAAOC,KAAyBC,GACpC,IAAIC,EACAC,EACAC,EAAM,IAAIC,IAAIL,EAAQI,KACtBE,EAA6BN,EAAQM,MAAQ,CAAEpB,UAAW,MAG9D,IAAK,IAAKqB,EAAGC,KAAMJ,EAAIK,aACrBH,EAAMC,GAAKD,EAAMC,GAAM,GAAgBG,OAAOJ,EAAMC,GAAIC,GAAKA,EAG/D,IAAK,IAAKG,EAAQC,EAAOlB,EAAUF,KAASR,EAC1C,IAAK2B,GAAUX,EAAQW,QAAoB,OAAVA,KAAqBR,EAAQC,EAAIS,SAASV,MAAMS,IAAS,CACxFZ,EAAQc,OAASX,EAAMY,QAAU,CAAA,EACjCf,EAAQP,MAAQD,EAChB,IAAK,IAAIwB,KAAWtB,EAClB,GAAqE,OAAhEQ,QAAiBc,EAAQhB,EAAQiB,OAASjB,KAAYC,IAAgB,OAAOC,CACrF,CACJ"}
|
package/IttyRouter.mjs
CHANGED
|
@@ -1,40 +1,2 @@
|
|
|
1
|
-
const
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
({
|
|
4
|
-
__proto__: new Proxy({}, {
|
|
5
|
-
// @ts-expect-error (we're adding an expected prop "path" to the get)
|
|
6
|
-
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
|
|
7
|
-
prop.toUpperCase(),
|
|
8
|
-
RegExp(`^${(path = (base + route)
|
|
9
|
-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
|
|
10
|
-
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
|
|
11
|
-
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
|
|
12
|
-
.replace(/\./g, '\\.') // dot in path
|
|
13
|
-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
|
|
14
|
-
}/*$`),
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
handlers, // embed handlers
|
|
17
|
-
path, // embed clean route path
|
|
18
|
-
]) && receiver
|
|
19
|
-
}),
|
|
20
|
-
routes,
|
|
21
|
-
...other,
|
|
22
|
-
async fetch(request, ...args) {
|
|
23
|
-
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
|
|
24
|
-
// 1. parse query params
|
|
25
|
-
for (let [k, v] of url.searchParams)
|
|
26
|
-
query[k] = query[k] ? [].concat(query[k], v) : v;
|
|
27
|
-
// 2. then test routes
|
|
28
|
-
for (let [method, regex, handlers, path] of routes)
|
|
29
|
-
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
|
|
30
|
-
request.params = match.groups || {}; // embed params in request
|
|
31
|
-
request.route = path; // embed route path in request
|
|
32
|
-
for (let handler of handlers)
|
|
33
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
34
|
-
return response;
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
export { IttyRouter };
|
|
1
|
+
const e=({base:e="",routes:r=[],...o}={})=>({__proto__:new Proxy({},{get:(o,t,a,p)=>(o,...l)=>r.push([t.toUpperCase(),RegExp(`^${(p=(e+o).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),l,p])&&a}),routes:r,...o,async fetch(e,...o){let t,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,c,s,u]of r)if((l==e.method||"ALL"==l)&&(a=p.pathname.match(c))){e.params=a.groups||{},e.route=u;for(let r of s)if(null!=(t=await r(e.proxy??e,...o)))return t}}});export{e as IttyRouter};
|
|
40
2
|
//# sourceMappingURL=IttyRouter.mjs.map
|
package/IttyRouter.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IttyRouter.mjs","sources":["../src/src/IttyRouter.ts"],"sourcesContent":[null],"names":[
|
|
1
|
+
{"version":3,"file":"IttyRouter.mjs","sources":["../src/src/IttyRouter.ts"],"sourcesContent":[null],"names":["IttyRouter","base","routes","other","__proto__","Proxy","get","target","prop","receiver","path","route","handlers","push","toUpperCase","RegExp","replace","fetch","request","args","response","match","url","URL","query","k","v","searchParams","concat","method","regex","pathname","params","groups","handler","proxy"],"mappings":"AAQa,MAAAA,EAAa,EAKtBC,OAAO,GAAIC,SAAS,MAAOC,GAA6B,CAAE,KAE5D,CACEC,UAAW,IAAIC,MAAM,GAAI,CAEvBC,IAAK,CAACC,EAAaC,EAAcC,EAAkBC,IACjD,CAACC,KAAkBC,IACjBV,EAAOW,KACL,CACEL,EAAKM,cACLC,OAAO,KAAKL,GAAQT,EAAOU,GACxBK,QAAQ,aAAc,OACtBA,QAAQ,oBAAqB,gBAC7BA,QAAQ,kBAAmB,uBAC3BA,QAAQ,MAAO,OACfA,QAAQ,WAAY,iBAGvBJ,EACAF,KAECD,IAEXP,YACGC,EACH,WAAMc,CAAOC,KAAyBC,GACpC,IAAIC,EACAC,EACAC,EAAM,IAAIC,IAAIL,EAAQI,KACtBE,EAA6BN,EAAQM,MAAQ,CAAEpB,UAAW,MAG9D,IAAK,IAAKqB,EAAGC,KAAMJ,EAAIK,aACrBH,EAAMC,GAAKD,EAAMC,GAAM,GAAgBG,OAAOJ,EAAMC,GAAIC,GAAKA,EAG/D,IAAK,IAAKG,EAAQC,EAAOlB,EAAUF,KAASR,EAC1C,IAAK2B,GAAUX,EAAQW,QAAoB,OAAVA,KAAqBR,EAAQC,EAAIS,SAASV,MAAMS,IAAS,CACxFZ,EAAQc,OAASX,EAAMY,QAAU,CAAA,EACjCf,EAAQP,MAAQD,EAChB,IAAK,IAAIwB,KAAWtB,EAClB,GAAqE,OAAhEQ,QAAiBc,EAAQhB,EAAQiB,OAASjB,KAAYC,IAAgB,OAAOC,CACrF,CACJ"}
|
package/Router.js
CHANGED
|
@@ -1,60 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const Router = ({ base = '', routes = [], ...other } = {}) => ({
|
|
4
|
-
__proto__: new Proxy({}, {
|
|
5
|
-
// @ts-expect-error (we're adding an expected prop "path" to the get)
|
|
6
|
-
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
|
|
7
|
-
prop.toUpperCase?.(),
|
|
8
|
-
RegExp(`^${(path = (base + route)
|
|
9
|
-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
|
|
10
|
-
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
|
|
11
|
-
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
|
|
12
|
-
.replace(/\./g, '\\.') // dot in path
|
|
13
|
-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
|
|
14
|
-
}/*$`),
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
handlers, // embed handlers
|
|
17
|
-
path, // embed clean route path
|
|
18
|
-
]) && receiver
|
|
19
|
-
}),
|
|
20
|
-
routes,
|
|
21
|
-
...other,
|
|
22
|
-
async fetch(request, ...args) {
|
|
23
|
-
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
|
|
24
|
-
// 1. parse query params
|
|
25
|
-
for (let [k, v] of url.searchParams)
|
|
26
|
-
query[k] = query[k] ? [].concat(query[k], v) : v;
|
|
27
|
-
t: try {
|
|
28
|
-
for (let handler of other.before || [])
|
|
29
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
30
|
-
break t;
|
|
31
|
-
// 2. then test routes
|
|
32
|
-
outer: for (let [method, regex, handlers, path] of routes)
|
|
33
|
-
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
|
|
34
|
-
request.params = match.groups || {}; // embed params in request
|
|
35
|
-
request.route = path; // embed route path in request
|
|
36
|
-
for (let handler of handlers)
|
|
37
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
38
|
-
break outer;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
catch (err) {
|
|
42
|
-
if (!other.catch)
|
|
43
|
-
throw err;
|
|
44
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
45
|
-
}
|
|
46
|
-
try {
|
|
47
|
-
for (let handler of other.finally || [])
|
|
48
|
-
response = await handler(response, request.proxy ?? request, ...args) ?? response;
|
|
49
|
-
}
|
|
50
|
-
catch (err) {
|
|
51
|
-
if (!other.catch)
|
|
52
|
-
throw err;
|
|
53
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
54
|
-
}
|
|
55
|
-
return response;
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
exports.Router = Router;
|
|
1
|
+
"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}});
|
|
60
2
|
//# sourceMappingURL=Router.js.map
|
package/Router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.js","sources":["../src/src/Router.ts"],"sourcesContent":[null],"names":[
|
|
1
|
+
{"version":3,"file":"Router.js","sources":["../src/src/Router.ts"],"sourcesContent":[null],"names":["base","routes","other","__proto__","Proxy","get","target","prop","receiver","path","route","handlers","push","toUpperCase","RegExp","replace","fetch","request","args","response","match","url","URL","query","k","v","searchParams","concat","t","handler","before","proxy","outer","method","regex","pathname","params","groups","err","catch","finally"],"mappings":"4BAQsB,EAIlBA,OAAO,GAAIC,SAAS,MAAOC,GAAyB,MACrD,CACCC,UAAW,IAAIC,MAAM,GAAI,CAEvBC,IAAK,CAACC,EAAaC,EAAcC,EAAkBC,IACjD,CAACC,KAAkBC,IACjBV,EAAOW,KACL,CACEL,EAAKM,gBACLC,OAAO,KAAKL,GAAQT,EAAOU,GACxBK,QAAQ,aAAc,OACtBA,QAAQ,oBAAqB,gBAC7BA,QAAQ,kBAAmB,uBAC3BA,QAAQ,MAAO,OACfA,QAAQ,WAAY,iBAGvBJ,EACAF,KAECD,IAEXP,YACGC,EACH,WAAMc,CAAOC,KAAyBC,GACpC,IAAIC,EACAC,EACAC,EAAM,IAAIC,IAAIL,EAAQI,KACtBE,EAA6BN,EAAQM,MAAQ,CAAEpB,UAAW,MAG9D,IAAK,IAAKqB,EAAGC,KAAMJ,EAAIK,aACrBH,EAAMC,GAAKD,EAAMC,GAAM,GAAgBG,OAAOJ,EAAMC,GAAIC,GAAKA,EAE/DG,EAAG,IACD,IAAK,IAAIC,KAAW3B,EAAM4B,QAAU,GAClC,GAAqE,OAAhEX,QAAiBU,EAAQZ,EAAQc,OAASd,KAAYC,IAAgB,MAAMU,EAGnFI,EAAO,IAAK,IAAKC,EAAQC,EAAOvB,EAAUF,KAASR,EACjD,IAAKgC,GAAUhB,EAAQgB,QAAoB,OAAVA,KAAqBb,EAAQC,EAAIc,SAASf,MAAMc,IAAS,CACxFjB,EAAQmB,OAAShB,EAAMiB,QAAU,CAAA,EACjCpB,EAAQP,MAAQD,EAEhB,IAAK,IAAIoB,KAAWlB,EAClB,GAAqE,OAAhEQ,QAAiBU,EAAQZ,EAAQc,OAASd,KAAYC,IAAgB,MAAMc,CACpF,CACJ,CAAC,MAAOM,GACP,IAAKpC,EAAMqC,MAAO,MAAMD,EACxBnB,QAAiBjB,EAAMqC,MAAMD,EAAKrB,EAAQc,OAASd,KAAYC,EAChE,CAED,IACE,IAAK,IAAIW,KAAW3B,EAAMsC,SAAW,GACnCrB,QAAiBU,EAAQV,EAAUF,EAAQc,OAASd,KAAYC,IAASC,CAC5E,CAAC,MAAMmB,GACN,IAAKpC,EAAMqC,MAAO,MAAMD,EACtBnB,QAAiBjB,EAAMqC,MAAMD,EAAKrB,EAAQc,OAASd,KAAYC,EAClE,CAED,OAAOC,CACR"}
|
package/Router.mjs
CHANGED
|
@@ -1,58 +1,2 @@
|
|
|
1
|
-
const
|
|
2
|
-
__proto__: new Proxy({}, {
|
|
3
|
-
// @ts-expect-error (we're adding an expected prop "path" to the get)
|
|
4
|
-
get: (target, prop, receiver, path) => (route, ...handlers) => routes.push([
|
|
5
|
-
prop.toUpperCase?.(),
|
|
6
|
-
RegExp(`^${(path = (base + route)
|
|
7
|
-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
|
|
8
|
-
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
|
|
9
|
-
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
|
|
10
|
-
.replace(/\./g, '\\.') // dot in path
|
|
11
|
-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
|
|
12
|
-
}/*$`),
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
handlers, // embed handlers
|
|
15
|
-
path, // embed clean route path
|
|
16
|
-
]) && receiver
|
|
17
|
-
}),
|
|
18
|
-
routes,
|
|
19
|
-
...other,
|
|
20
|
-
async fetch(request, ...args) {
|
|
21
|
-
let response, match, url = new URL(request.url), query = request.query = { __proto__: null };
|
|
22
|
-
// 1. parse query params
|
|
23
|
-
for (let [k, v] of url.searchParams)
|
|
24
|
-
query[k] = query[k] ? [].concat(query[k], v) : v;
|
|
25
|
-
t: try {
|
|
26
|
-
for (let handler of other.before || [])
|
|
27
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
28
|
-
break t;
|
|
29
|
-
// 2. then test routes
|
|
30
|
-
outer: for (let [method, regex, handlers, path] of routes)
|
|
31
|
-
if ((method == request.method || method == 'ALL') && (match = url.pathname.match(regex))) {
|
|
32
|
-
request.params = match.groups || {}; // embed params in request
|
|
33
|
-
request.route = path; // embed route path in request
|
|
34
|
-
for (let handler of handlers)
|
|
35
|
-
if ((response = await handler(request.proxy ?? request, ...args)) != null)
|
|
36
|
-
break outer;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
if (!other.catch)
|
|
41
|
-
throw err;
|
|
42
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
43
|
-
}
|
|
44
|
-
try {
|
|
45
|
-
for (let handler of other.finally || [])
|
|
46
|
-
response = await handler(response, request.proxy ?? request, ...args) ?? response;
|
|
47
|
-
}
|
|
48
|
-
catch (err) {
|
|
49
|
-
if (!other.catch)
|
|
50
|
-
throw err;
|
|
51
|
-
response = await other.catch(err, request.proxy ?? request, ...args);
|
|
52
|
-
}
|
|
53
|
-
return response;
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
export { Router };
|
|
1
|
+
const r=({base:r="",routes:e=[],...a}={})=>({__proto__:new Proxy({},{get:(a,t,o,c)=>(a,...l)=>e.push([t.toUpperCase?.(),RegExp(`^${(c=(r+a).replace(/\/+(\/|$)/g,"$1")).replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),l,c])&&o}),routes:e,...a,async fetch(r,...t){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 a.before||[])if(null!=(o=await e(r.proxy??r,...t)))break r;e:for(let[a,p,f,h]of e)if((a==r.method||"ALL"==a)&&(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,...t)))break e}}catch(e){if(!a.catch)throw e;o=await a.catch(e,r.proxy??r,...t)}try{for(let e of a.finally||[])o=await e(o,r.proxy??r,...t)??o}catch(e){if(!a.catch)throw e;o=await a.catch(e,r.proxy??r,...t)}return o}});export{r as Router};
|
|
58
2
|
//# sourceMappingURL=Router.mjs.map
|
package/Router.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.mjs","sources":["../src/src/Router.ts"],"sourcesContent":[null],"names":[
|
|
1
|
+
{"version":3,"file":"Router.mjs","sources":["../src/src/Router.ts"],"sourcesContent":[null],"names":["Router","base","routes","other","__proto__","Proxy","get","target","prop","receiver","path","route","handlers","push","toUpperCase","RegExp","replace","fetch","request","args","response","match","url","URL","query","k","v","searchParams","concat","t","handler","before","proxy","outer","method","regex","pathname","params","groups","err","catch","finally"],"mappings":"MAQaA,EAAS,EAIlBC,OAAO,GAAIC,SAAS,MAAOC,GAAyB,MACrD,CACCC,UAAW,IAAIC,MAAM,GAAI,CAEvBC,IAAK,CAACC,EAAaC,EAAcC,EAAkBC,IACjD,CAACC,KAAkBC,IACjBV,EAAOW,KACL,CACEL,EAAKM,gBACLC,OAAO,KAAKL,GAAQT,EAAOU,GACxBK,QAAQ,aAAc,OACtBA,QAAQ,oBAAqB,gBAC7BA,QAAQ,kBAAmB,uBAC3BA,QAAQ,MAAO,OACfA,QAAQ,WAAY,iBAGvBJ,EACAF,KAECD,IAEXP,YACGC,EACH,WAAMc,CAAOC,KAAyBC,GACpC,IAAIC,EACAC,EACAC,EAAM,IAAIC,IAAIL,EAAQI,KACtBE,EAA6BN,EAAQM,MAAQ,CAAEpB,UAAW,MAG9D,IAAK,IAAKqB,EAAGC,KAAMJ,EAAIK,aACrBH,EAAMC,GAAKD,EAAMC,GAAM,GAAgBG,OAAOJ,EAAMC,GAAIC,GAAKA,EAE/DG,EAAG,IACD,IAAK,IAAIC,KAAW3B,EAAM4B,QAAU,GAClC,GAAqE,OAAhEX,QAAiBU,EAAQZ,EAAQc,OAASd,KAAYC,IAAgB,MAAMU,EAGnFI,EAAO,IAAK,IAAKC,EAAQC,EAAOvB,EAAUF,KAASR,EACjD,IAAKgC,GAAUhB,EAAQgB,QAAoB,OAAVA,KAAqBb,EAAQC,EAAIc,SAASf,MAAMc,IAAS,CACxFjB,EAAQmB,OAAShB,EAAMiB,QAAU,CAAA,EACjCpB,EAAQP,MAAQD,EAEhB,IAAK,IAAIoB,KAAWlB,EAClB,GAAqE,OAAhEQ,QAAiBU,EAAQZ,EAAQc,OAASd,KAAYC,IAAgB,MAAMc,CACpF,CACJ,CAAC,MAAOM,GACP,IAAKpC,EAAMqC,MAAO,MAAMD,EACxBnB,QAAiBjB,EAAMqC,MAAMD,EAAKrB,EAAQc,OAASd,KAAYC,EAChE,CAED,IACE,IAAK,IAAIW,KAAW3B,EAAMsC,SAAW,GACnCrB,QAAiBU,EAAQV,EAAUF,EAAQc,OAASd,KAAYC,IAASC,CAC5E,CAAC,MAAMmB,GACN,IAAKpC,EAAMqC,MAAO,MAAMD,EACtBnB,QAAiBjB,EAAMqC,MAAMD,EAAKrB,EAAQc,OAASd,KAAYC,EAClE,CAED,OAAOC,CACR"}
|