@lengkapp/edge 0.0.51 → 0.0.53
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/LICENSE +1 -1
- package/README.md +7 -3
- package/dist/LICENSE +1 -1
- package/dist/README.md +7 -3
- package/dist/edge-client.js +2 -2
- package/dist/edge-server.d.ts +48 -111
- package/dist/edge-server.js +3 -3
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ npm run deploy
|
|
|
102
102
|
|
|
103
103
|
### Why `export default app`?
|
|
104
104
|
|
|
105
|
-
The `Edge` instance exposes a `fetch(request, env)` method, which matches the Workers module-syntax contract.
|
|
105
|
+
The `Edge` instance exposes a `fetch(request, env, executionCtx?)` method, which matches the Workers module-syntax contract. If the Worker runtime passes an `ExecutionContext` as the third argument, the router threads it into every handler's `Context` as `c.ctx` (alias `c.executionCtx`), so route handlers can call `c.waitUntil(...)` and `c.passThroughOnException()` without stashing `ctx` in a module-level variable. Handlers that don't use it are unaffected.
|
|
106
106
|
|
|
107
107
|
### JSX pragma
|
|
108
108
|
|
|
@@ -182,6 +182,8 @@ Every handler receives a `Context` instance.
|
|
|
182
182
|
| `c.query` | `URLSearchParams` | `c.url.searchParams` |
|
|
183
183
|
| `c.headers` | `Headers` | Response headers, pre-filled with security defaults |
|
|
184
184
|
| `c.status` | `number` | Default status used by `text`/`json`/`html`/`page` (default `200`) |
|
|
185
|
+
| `c.ctx` | `ExecutionContext \| null` | The Worker's `ExecutionContext`, or `null` outside a Worker |
|
|
186
|
+
| `c.executionCtx` | `ExecutionContext \| null` | Alias of `c.ctx` (matches `WorkerEntrypoint` naming) |
|
|
185
187
|
|
|
186
188
|
### Response helpers
|
|
187
189
|
|
|
@@ -192,6 +194,8 @@ await c.html(<div>hi</div>); // text/html; charset=utf-8
|
|
|
192
194
|
await c.page(<html>…</html>); // same, but prepends <!DOCTYPE html> if missing
|
|
193
195
|
c.redirect('/login', 302);
|
|
194
196
|
c.redirect('https://example.com', 301);
|
|
197
|
+
c.waitUntil(promise); // forwards to ExecutionContext.waitUntil; no-op when absent
|
|
198
|
+
c.passThroughOnException(); // forwards to ExecutionContext.passThroughOnException; no-op when absent
|
|
195
199
|
```
|
|
196
200
|
|
|
197
201
|
`html()` and `page()` accept:
|
|
@@ -387,7 +391,7 @@ app.scheduled(async (event, env, ctx) => {
|
|
|
387
391
|
});
|
|
388
392
|
|
|
389
393
|
export default {
|
|
390
|
-
fetch: (request, env) => app.fetch(request, env),
|
|
394
|
+
fetch: (request, env, ctx) => app.fetch(request, env, ctx),
|
|
391
395
|
scheduled: (event, env, ctx) => app.scheduledHandler?.(event, env, ctx),
|
|
392
396
|
};
|
|
393
397
|
```
|
|
@@ -670,7 +674,7 @@ app.get('/api/theme', (c) => c.json({ theme: c.getCookie('theme') || 'light' }))
|
|
|
670
674
|
* Export *
|
|
671
675
|
* ------------------------------------------------------------------ */
|
|
672
676
|
export default {
|
|
673
|
-
fetch: (request, env) => app.fetch(request, env),
|
|
677
|
+
fetch: (request, env, ctx) => app.fetch(request, env, ctx),
|
|
674
678
|
scheduled: (event, env, ctx) => app.scheduledHandler?.(event, env, ctx),
|
|
675
679
|
};
|
|
676
680
|
```
|
package/dist/LICENSE
CHANGED
package/dist/README.md
CHANGED
|
@@ -102,7 +102,7 @@ npm run deploy
|
|
|
102
102
|
|
|
103
103
|
### Why `export default app`?
|
|
104
104
|
|
|
105
|
-
The `Edge` instance exposes a `fetch(request, env)` method, which matches the Workers module-syntax contract.
|
|
105
|
+
The `Edge` instance exposes a `fetch(request, env, executionCtx?)` method, which matches the Workers module-syntax contract. If the Worker runtime passes an `ExecutionContext` as the third argument, the router threads it into every handler's `Context` as `c.ctx` (alias `c.executionCtx`), so route handlers can call `c.waitUntil(...)` and `c.passThroughOnException()` without stashing `ctx` in a module-level variable. Handlers that don't use it are unaffected.
|
|
106
106
|
|
|
107
107
|
### JSX pragma
|
|
108
108
|
|
|
@@ -182,6 +182,8 @@ Every handler receives a `Context` instance.
|
|
|
182
182
|
| `c.query` | `URLSearchParams` | `c.url.searchParams` |
|
|
183
183
|
| `c.headers` | `Headers` | Response headers, pre-filled with security defaults |
|
|
184
184
|
| `c.status` | `number` | Default status used by `text`/`json`/`html`/`page` (default `200`) |
|
|
185
|
+
| `c.ctx` | `ExecutionContext \| null` | The Worker's `ExecutionContext`, or `null` outside a Worker |
|
|
186
|
+
| `c.executionCtx` | `ExecutionContext \| null` | Alias of `c.ctx` (matches `WorkerEntrypoint` naming) |
|
|
185
187
|
|
|
186
188
|
### Response helpers
|
|
187
189
|
|
|
@@ -192,6 +194,8 @@ await c.html(<div>hi</div>); // text/html; charset=utf-8
|
|
|
192
194
|
await c.page(<html>…</html>); // same, but prepends <!DOCTYPE html> if missing
|
|
193
195
|
c.redirect('/login', 302);
|
|
194
196
|
c.redirect('https://example.com', 301);
|
|
197
|
+
c.waitUntil(promise); // forwards to ExecutionContext.waitUntil; no-op when absent
|
|
198
|
+
c.passThroughOnException(); // forwards to ExecutionContext.passThroughOnException; no-op when absent
|
|
195
199
|
```
|
|
196
200
|
|
|
197
201
|
`html()` and `page()` accept:
|
|
@@ -387,7 +391,7 @@ app.scheduled(async (event, env, ctx) => {
|
|
|
387
391
|
});
|
|
388
392
|
|
|
389
393
|
export default {
|
|
390
|
-
fetch: (request, env) => app.fetch(request, env),
|
|
394
|
+
fetch: (request, env, ctx) => app.fetch(request, env, ctx),
|
|
391
395
|
scheduled: (event, env, ctx) => app.scheduledHandler?.(event, env, ctx),
|
|
392
396
|
};
|
|
393
397
|
```
|
|
@@ -670,7 +674,7 @@ app.get('/api/theme', (c) => c.json({ theme: c.getCookie('theme') || 'light' }))
|
|
|
670
674
|
* Export *
|
|
671
675
|
* ------------------------------------------------------------------ */
|
|
672
676
|
export default {
|
|
673
|
-
fetch: (request, env) => app.fetch(request, env),
|
|
677
|
+
fetch: (request, env, ctx) => app.fetch(request, env, ctx),
|
|
674
678
|
scheduled: (event, env, ctx) => app.scheduledHandler?.(event, env, ctx),
|
|
675
679
|
};
|
|
676
680
|
```
|
package/dist/edge-client.js
CHANGED
package/dist/edge-server.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @lengkapp/edge v0.0.
|
|
2
|
+
* @lengkapp/edge v0.0.53
|
|
3
3
|
* MIT License
|
|
4
4
|
*
|
|
5
|
-
* LengkApp
|
|
5
|
+
* LengkApp License
|
|
6
6
|
*
|
|
7
7
|
* Copyright (c) LengkApp — Yasir Haris
|
|
8
8
|
* Contact: yh@lengk.app / yasir.haris@gmail.com
|
|
@@ -33,13 +33,6 @@
|
|
|
33
33
|
/* ------------------------------------------------------------------ *
|
|
34
34
|
* JSX runtime *
|
|
35
35
|
* ------------------------------------------------------------------ */
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Runtime value is `Symbol('Fragment')`. The function signature is a
|
|
39
|
-
* type-level shim so `<></>` works with the automatic JSX runtime.
|
|
40
|
-
* Never called directly — `jsx(Fragment, …)` passes it through and
|
|
41
|
-
* `renderToString` compares by identity.
|
|
42
|
-
*/
|
|
43
36
|
export const Fragment: (props: { children?: any }) => JSXNode;
|
|
44
37
|
|
|
45
38
|
export interface JSXNode {
|
|
@@ -62,7 +55,7 @@ export function jsx(
|
|
|
62
55
|
* The optional `ctx` is normally supplied by `Context.html`/`Context.page`;
|
|
63
56
|
* user code never needs to pass it.
|
|
64
57
|
*/
|
|
65
|
-
export function renderToString<Env =
|
|
58
|
+
export function renderToString<Env = any>(
|
|
66
59
|
node: any,
|
|
67
60
|
ctx?: Context<Env>
|
|
68
61
|
): Promise<string>;
|
|
@@ -75,17 +68,27 @@ export function renderToString<Env = unknown>(
|
|
|
75
68
|
* Returns the Context of the request whose render is currently executing.
|
|
76
69
|
*
|
|
77
70
|
* MUST be called synchronously at the top of a component — before any
|
|
78
|
-
* `await`. After an `await`, use the captured local reference
|
|
79
|
-
* ambient slot is no longer guaranteed to belong to your request.
|
|
71
|
+
* `await`. After an `await`, use the captured local reference.
|
|
80
72
|
*
|
|
81
73
|
* Throws if called outside an active render (module top-level, scheduled
|
|
82
74
|
* handler, `waitUntil` callback, or after an `await`).
|
|
83
|
-
*
|
|
84
|
-
* NOTE: `useCtx()` cannot infer your app's `Env` — the ambient stack is
|
|
85
|
-
* erased at runtime. Pass the type explicitly when you need it:
|
|
86
|
-
* const c = useCtx<MyEnv>();
|
|
87
75
|
*/
|
|
88
|
-
export function useCtx<Env =
|
|
76
|
+
export function useCtx<Env = any>(): Context<Env>;
|
|
77
|
+
|
|
78
|
+
/* ------------------------------------------------------------------ *
|
|
79
|
+
* ExecutionContext (structural, so it works outside Workers too) *
|
|
80
|
+
* ------------------------------------------------------------------ */
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Minimal structural shape of Cloudflare's `ExecutionContext`. A real
|
|
84
|
+
* `ExecutionContext` (passed to `WorkerEntrypoint.fetch(request, env, ctx)`
|
|
85
|
+
* or `export default { fetch(request, env, ctx) { ... } }`) is assignable
|
|
86
|
+
* to this type, and so is any test double with the same two methods.
|
|
87
|
+
*/
|
|
88
|
+
export interface ExecutionContextLike {
|
|
89
|
+
waitUntil(promise: Promise<any>): void;
|
|
90
|
+
passThroughOnException(): void;
|
|
91
|
+
}
|
|
89
92
|
|
|
90
93
|
/* ------------------------------------------------------------------ *
|
|
91
94
|
* Context *
|
|
@@ -101,37 +104,51 @@ export interface CookieOptions {
|
|
|
101
104
|
sameSite?: 'Strict' | 'Lax' | 'None' | string;
|
|
102
105
|
}
|
|
103
106
|
|
|
104
|
-
export class Context<Env =
|
|
107
|
+
export class Context<Env = any> {
|
|
105
108
|
constructor(
|
|
106
109
|
request: Request,
|
|
107
110
|
env: Env,
|
|
108
111
|
params?: Record<string, string>,
|
|
109
|
-
parsedUrl?: URL | null
|
|
112
|
+
parsedUrl?: URL | null,
|
|
113
|
+
executionCtx?: ExecutionContextLike | null
|
|
110
114
|
);
|
|
111
115
|
|
|
112
116
|
req: Request;
|
|
113
|
-
env: Env;
|
|
117
|
+
env: Env;
|
|
114
118
|
params: Record<string, string>;
|
|
115
119
|
status: number;
|
|
116
120
|
headers: Headers;
|
|
117
121
|
url: URL;
|
|
118
122
|
|
|
123
|
+
/** The request's `ExecutionContext`, or `null` outside a Worker. */
|
|
124
|
+
ctx: ExecutionContextLike | null;
|
|
125
|
+
/** Alias of `ctx` (matches the `WorkerEntrypoint` naming). */
|
|
126
|
+
executionCtx: ExecutionContextLike | null;
|
|
127
|
+
|
|
119
128
|
getCookie(name: string): string | null;
|
|
120
129
|
readonly query: URLSearchParams;
|
|
121
130
|
setCookie(name: string, value: string, options?: CookieOptions): void;
|
|
122
131
|
deleteCookie(name: string, options?: CookieOptions): void;
|
|
123
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Passthrough to `ctx.waitUntil(promise)`. No-op when `ctx` is absent
|
|
135
|
+
* (e.g. tests calling `app.fetch(req, env)` with no execution context).
|
|
136
|
+
* Returns the promise so callers can `await` it in tests.
|
|
137
|
+
*/
|
|
138
|
+
waitUntil(promise: Promise<any>): Promise<any>;
|
|
139
|
+
|
|
140
|
+
/** Passthrough to `ctx.passThroughOnException()`. No-op when absent. */
|
|
141
|
+
passThroughOnException(): void;
|
|
142
|
+
|
|
124
143
|
text(data: string, status?: number, headers?: Record<string, string>): Response;
|
|
125
144
|
json(data: any, status?: number, headers?: Record<string, string>): Response;
|
|
126
145
|
|
|
127
|
-
/** Async — JSX trees may contain async components. */
|
|
128
146
|
html(
|
|
129
147
|
data: string | JSXNode | JSXNode[] | null | undefined,
|
|
130
148
|
status?: number,
|
|
131
149
|
headers?: Record<string, string>
|
|
132
150
|
): Promise<Response>;
|
|
133
151
|
|
|
134
|
-
/** Async — JSX trees may contain async components. */
|
|
135
152
|
page(
|
|
136
153
|
data: string | JSXNode | JSXNode[] | null | undefined,
|
|
137
154
|
status?: number,
|
|
@@ -145,95 +162,38 @@ export class Context<Env = unknown> {
|
|
|
145
162
|
* Edge router *
|
|
146
163
|
* ------------------------------------------------------------------ */
|
|
147
164
|
|
|
148
|
-
export type RouteHandler<Env =
|
|
165
|
+
export type RouteHandler<Env = any> =
|
|
149
166
|
(ctx: Context<Env>) => any | Promise<any>;
|
|
150
167
|
|
|
151
|
-
export type ScheduledHandler<Env =
|
|
152
|
-
event:
|
|
168
|
+
export type ScheduledHandler<Env = any> = (
|
|
169
|
+
event: any,
|
|
153
170
|
env: Env,
|
|
154
|
-
ctx:
|
|
171
|
+
ctx: any
|
|
155
172
|
) => void | Promise<void>;
|
|
156
173
|
|
|
157
|
-
|
|
158
|
-
* Invoked when no route matches the incoming request.
|
|
159
|
-
*
|
|
160
|
-
* Receives a full Context (same shape as a route handler) so it can
|
|
161
|
-
* set `c.status` and return a Response, or a JSX tree (which is
|
|
162
|
-
* coerced to an HTML response at that status).
|
|
163
|
-
*/
|
|
164
|
-
export type NotFoundHandler<Env = unknown> =
|
|
174
|
+
export type NotFoundHandler<Env = any> =
|
|
165
175
|
(ctx: Context<Env>) => any | Promise<any>;
|
|
166
176
|
|
|
167
|
-
|
|
168
|
-
* Invoked when a route handler (or the not-found handler) throws.
|
|
169
|
-
*
|
|
170
|
-
* The thrown value is `unknown` because nothing enforces that handlers
|
|
171
|
-
* throw `Error` specifically. Returning a Response uses it verbatim;
|
|
172
|
-
* returning JSX renders it as a 500 HTML response. Errors thrown by
|
|
173
|
-
* the error handler itself are swallowed and replaced with a
|
|
174
|
-
* plain-text 500.
|
|
175
|
-
*/
|
|
176
|
-
export type ErrorHandler<Env = unknown> = (
|
|
177
|
+
export type ErrorHandler<Env = any> = (
|
|
177
178
|
ctx: Context<Env>,
|
|
178
179
|
err: unknown
|
|
179
180
|
) => any | Promise<any>;
|
|
180
181
|
|
|
181
|
-
export class Edge<Env =
|
|
182
|
-
/**
|
|
183
|
-
* @param prefix Optional mount prefix for this Edge. `''` and `'/'`
|
|
184
|
-
* both mean "no prefix". Joined with every registered route path at
|
|
185
|
-
* registration time — never stripped or re-applied per request.
|
|
186
|
-
*/
|
|
182
|
+
export class Edge<Env = any> {
|
|
187
183
|
constructor(prefix?: string);
|
|
188
184
|
|
|
189
|
-
/**
|
|
190
|
-
* Normalized mount prefix for this Edge. `''` means "no prefix".
|
|
191
|
-
* Set once at construction; applied to every route registered on this
|
|
192
|
-
* Edge (and merged from sub-apps) so lookups at request time are
|
|
193
|
-
* plain map/trie hits with no prefix stripping.
|
|
194
|
-
*/
|
|
195
185
|
prefix: string;
|
|
196
186
|
|
|
197
187
|
readonly staticRoutes: Map<string, Map<string, RouteHandler<Env>>>;
|
|
198
188
|
readonly dynamicTries: Record<string, any>;
|
|
199
189
|
|
|
200
|
-
/**
|
|
201
|
-
* Set via `.scheduled()`. `null` until `.scheduled()` is called.
|
|
202
|
-
* Read it only from the Worker entry
|
|
203
|
-
* (`export default { fetch, scheduled }`) if you wire that up yourself.
|
|
204
|
-
*/
|
|
205
190
|
scheduledHandler: ScheduledHandler<Env> | null;
|
|
206
191
|
|
|
207
|
-
/* ---- Fallback handlers ------------------------------------------ *
|
|
208
|
-
* Exposed as public fields to mirror the runtime shape; normally *
|
|
209
|
-
* set via `.notFound()` / `.onError()` rather than assigned *
|
|
210
|
-
* directly. */
|
|
211
192
|
notFoundHandler: NotFoundHandler<Env> | null;
|
|
212
193
|
errorHandler: ErrorHandler<Env> | null;
|
|
213
194
|
|
|
214
|
-
/* ---- Sub-app mounting ------------------------------------------- */
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Assigning runs a one-time merge: every route on each sub-app is
|
|
218
|
-
* copied into this Edge's own tables, prefixed with `this.prefix`
|
|
219
|
-
* combined with the sub-app's own prefix. After the merge the hot
|
|
220
|
-
* path is unchanged — no per-request dispatch, no path rewriting.
|
|
221
|
-
*
|
|
222
|
-
* const signIn = new Edge('/sign-in');
|
|
223
|
-
* signIn.get('/login', h);
|
|
224
|
-
* const app = new Edge();
|
|
225
|
-
* app.routes = [signIn]; // GET /sign-in/login
|
|
226
|
-
*
|
|
227
|
-
* Non-`Edge` entries throw. Mount is eager and one-way: routes
|
|
228
|
-
* registered on a sub-app *after* it was mounted do not propagate.
|
|
229
|
-
*/
|
|
230
195
|
set routes(apps: Edge<Env> | Edge<Env>[]);
|
|
231
196
|
|
|
232
|
-
/* ---- Route registration ----------------------------------------- *
|
|
233
|
-
* Paths are joined with this Edge's `prefix` (if any) at *
|
|
234
|
-
* registration time; the resulting full path is what the router *
|
|
235
|
-
* matches against on each request. */
|
|
236
|
-
|
|
237
197
|
get(path: string, handler: RouteHandler<Env>): void;
|
|
238
198
|
post(path: string, handler: RouteHandler<Env>): void;
|
|
239
199
|
put(path: string, handler: RouteHandler<Env>): void;
|
|
@@ -242,38 +202,15 @@ export class Edge<Env = unknown> {
|
|
|
242
202
|
options(path: string, handler: RouteHandler<Env>): void;
|
|
243
203
|
head(path: string, handler: RouteHandler<Env>): void;
|
|
244
204
|
|
|
245
|
-
/* ---- Scheduled handler ------------------------------------------ */
|
|
246
205
|
scheduled(handler: ScheduledHandler<Env>): void;
|
|
247
206
|
|
|
248
|
-
/* ---- Fallback registration -------------------------------------- */
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Register a handler invoked when no route matches the incoming
|
|
252
|
-
* request. The handler runs with a full Context (headers, cookies,
|
|
253
|
-
* `c.page`, `c.html`).
|
|
254
|
-
*
|
|
255
|
-
* `OPTIONS` requests are short-circuited to a 204 (with base security
|
|
256
|
-
* headers, no body) before this handler runs, so preflights never
|
|
257
|
-
* render an HTML error page.
|
|
258
|
-
*/
|
|
259
207
|
notFound(handler: NotFoundHandler<Env>): void;
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Register a handler invoked when a route handler (including the
|
|
263
|
-
* not-found handler) throws. Returning a Response uses it verbatim;
|
|
264
|
-
* returning JSX renders it as a 500 HTML response. Errors thrown by
|
|
265
|
-
* the error handler itself are swallowed and replaced with a
|
|
266
|
-
* plain-text 500.
|
|
267
|
-
*/
|
|
268
208
|
onError(handler: ErrorHandler<Env>): void;
|
|
269
209
|
|
|
270
|
-
/* ---- Entry point ------------------------------------------------ *
|
|
271
|
-
* The Workers module contract also passes an `ExecutionContext` as
|
|
272
|
-
* the third argument; the router ignores it. Declare only what the
|
|
273
|
-
* implementation consumes. */
|
|
274
210
|
fetch(
|
|
275
211
|
request: Request,
|
|
276
|
-
env: Env
|
|
212
|
+
env: Env,
|
|
213
|
+
executionCtx?: ExecutionContextLike
|
|
277
214
|
): Promise<Response>;
|
|
278
215
|
}
|
|
279
216
|
|
package/dist/edge-server.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @lengkapp/edge v0.0.
|
|
2
|
+
* @lengkapp/edge v0.0.53
|
|
3
3
|
* MIT License
|
|
4
4
|
*
|
|
5
|
-
* LengkApp
|
|
5
|
+
* LengkApp License
|
|
6
6
|
*
|
|
7
7
|
* Copyright (c) LengkApp — Yasir Haris
|
|
8
8
|
* Contact: yh@lengk.app / yasir.haris@gmail.com
|
|
@@ -30,4 +30,4 @@
|
|
|
30
30
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
31
31
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
32
32
|
*/
|
|
33
|
-
function t(t){const e=[],n=t.length;let r=1;for(let o=1;o<=n;o++)o!==n&&47!==t.charCodeAt(o)||(o>r&&e.push(t.slice(r,o)),r=o+1);return e}function e(t){const e=t.length;return e>1&&47===t.charCodeAt(e-1)?t.slice(0,-1):t}function n(t,e){if(!e)return t||"/";if(!t||"/"===t)return 47===e.charCodeAt(0)?e:"/"+e;const n=t.charCodeAt(t.length-1),r=e.charCodeAt(0);let o;o=47===n?47===r?t+e.slice(1):t+e:47===r?t+e:t+"/"+e;const s=o.length;return s>1&&47===o.charCodeAt(s-1)?o.slice(0,-1):o}var r=new Set(["__proto__","constructor","prototype"]);function o(t){return"string"==typeof t&&!r.has(t)}function s(t){const e=t.length;let n=0;for(;n<e;){const e=t.charCodeAt(n);if(38===e||60===e||62===e||34===e||39===e)break;n++}if(n===e)return t;let r=t.slice(0,n),o=n;for(;n<e;n++){const e=t.charCodeAt(n);let s;if(38===e)s="&";else if(60===e)s="<";else if(62===e)s=">";else if(34===e)s=""";else{if(39!==e)continue;s="'"}n>o&&(r+=t.slice(o,n)),r+=s,o=n+1}return o<e&&(r+=t.slice(o)),r}function i(t){return t>=65&&t<=90||t>=97&&t<=122}function c(t){return t>=48&&t<=57}function a(t){const e=t.length;if(0===e)return!1;const n=t.charCodeAt(0);if(!i(n)&&95!==n&&58!==n)return!1;for(let n=1;n<e;n++){const e=t.charCodeAt(n);if(!i(e)&&!c(e)&&95!==e&&58!==e&&46!==e&&45!==e)return!1}return!0}function l(t){const e=t.charCodeAt(0),n=t.charCodeAt(1);return!(111!==e&&79!==e||110!==n&&78!==n)}var Fragment=Symbol("Fragment");function jsx(t,e,...n){return{type:t,props:e||{},children:n,__isJSX:!0}}var u=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),f=new Set(["allowfullscreen","async","autofocus","autoplay","checked","controls","default","defer","disabled","formnovalidate","hidden","inert","ismap","itemscope","loop","multiple","muted","nomodule","novalidate","open","playsinline","readonly","required","reversed","selected"]),h=new Set(["animation-iteration-count","border-image-outset","border-image-slice","border-image-width","box-flex","box-flex-group","box-ordinal-group","column-count","columns","flex","flex-grow","flex-positive","flex-shrink","flex-negative","flex-order","grid-row","grid-row-end","grid-row-span","grid-row-start","grid-column","grid-column-end","grid-column-span","grid-column-start","font-weight","line-clamp","line-height","opacity","order","orphans","tab-size","widows","z-index","zoom","fill-opacity","flood-opacity","stop-opacity","stroke-dasharray","stroke-dashoffset","stroke-miterlimit","stroke-opacity","stroke-width"]),p=new Map;function d(t){const e=p.get(t);if(void 0!==e)return e;const n=t.replace(/([A-Z])/g,"-$1").toLowerCase().replace(/^-/,"");return p.size>=2e3&&p.clear(),p.set(t,n),n}function y(t){if(!t||"object"!=typeof t)return"";let e="";for(const n in t){if(!Object.prototype.hasOwnProperty.call(t,n))continue;const r=d(n);let o=t[n];"number"!=typeof o||h.has(r)||(o+="px"),e&&(e+=";"),e+=r+":"+o}return e}var w=new Set(["href","src","xlink:href","action","formaction"]),m=[];function useCtx(){const t=m[m.length-1];if(!t)throw new Error("useCtx() must be called synchronously at the top of a component (before any await). It is not available after an await, nor outside a render.");return t}async function renderToString(t,e){if(void 0===e&&(e=m[m.length-1]),null==t||"boolean"==typeof t)return"";const n=typeof t;if("string"===n)return s(t);if("number"===n)return s(String(t));if("object"===n&&"function"==typeof t.then)return renderToString(await t,e);if(Array.isArray(t)){let n="";for(let r=0;r<t.length;r++)n+=await renderToString(t[r],e);return n}if(!t.__isJSX)return s(String(t));const{type:r,props:h,children:p}=t;if(r===Fragment){let t="";for(let n=0;n<p.length;n++)t+=await renderToString(p[n],e);return t}if("symbol"==typeof r)return"";if("function"==typeof r){let t;if(e){m.push(e);try{t=r({...h,children:p})}finally{m.pop()}}else t=r({...h,children:p});return renderToString(await t,e)}if("string"!=typeof r||!function(t){const e=t.length;if(0===e||!i(t.charCodeAt(0)))return!1;for(let n=1;n<e;n++){const e=t.charCodeAt(n);if(!i(e)&&!c(e)&&45!==e)return!1}return!0}(r))return"";const d=u.has(r);let g="";for(const t in h){if(!Object.prototype.hasOwnProperty.call(h,t))continue;if("children"===t||"key"===t||"ref"===t)continue;const e=h[t];if(null==e||!1===e)continue;if("dangerouslySetInnerHTML"===t)continue;let n=t;if("className"===t||"class"===t?n="class":"htmlFor"!==t&&"for"!==t||(n="for"),o(n)&&!l(n)&&a(n)){if(w.has(n)){const t=String(e).replace(/[\u0000-\u0020]/g,"").toLowerCase();if(t.startsWith("javascript:")||t.startsWith("vbscript:")||t.startsWith("data:text/html"))continue}if(f.has(n))!0===e&&(g+=" "+n);else if(!0!==e){if("style"===n&&"object"==typeof e){const t=y(e);t&&(g+=' style="'+s(t)+'"');continue}g+=" "+n+'="'+s(String(e))+'"'}else g+=" "+n+'="true"'}}let b="";const S=h.dangerouslySetInnerHTML;if(S&&null!=S.__html)b=S.__html;else for(let t=0;t<p.length;t++)b+=await renderToString(p[t],e);return d?"<"+r+g+"/>":"<"+r+g+">"+b+"</"+r+">"}var g=Object.freeze([["X-Content-Type-Options","nosniff"],["X-Frame-Options","DENY"],["Referrer-Policy","strict-origin-when-cross-origin"],["Permissions-Policy","geolocation=(), microphone=(), camera=()"]]),b=Object.freeze(Object.create(null)),Context=class{constructor(t,e,n={},r=null){this.req=t,this.env=e,this.params=n,this.status=200,this.headers=new Headers(g),this.t=null,this.url=r||new URL(t.url)}o(){if(null!==this.t)return this.t;const t=this.req.headers.get("Cookie");if(!t)return this.t=b,b;const e=Object.create(null);let n=0;const r=t.length;for(;n<r;){let s=t.indexOf(";",n);-1===s&&(s=r);let i=n,c=s;for(;i<c&&32===t.charCodeAt(i);)i++;for(;c>i&&32===t.charCodeAt(c-1);)c--;if(c>i){const n=t.indexOf("=",i);if(n>i&&n<c)try{const r=decodeURIComponent(t.slice(i,n));o(r)&&(e[r]=decodeURIComponent(t.slice(n+1,c)))}catch{}}n=s+1}return this.t=e,e}getCookie(t){const e=this.o()[t];return void 0===e?null:e}get query(){return this.url.searchParams}setCookie(t,e,n={}){if("string"!=typeof t||!o(t)||/[\r\n;=]/.test(t))return;let r=encodeURIComponent(t)+"="+encodeURIComponent(e);n.path&&(r+="; Path="+n.path),n.domain&&(r+="; Domain="+n.domain),void 0!==n.maxAge&&(r+="; Max-Age="+n.maxAge),n.expires&&(r+="; Expires="+n.expires.toUTCString()),n.secure&&(r+="; Secure"),n.httpOnly&&(r+="; HttpOnly"),n.sameSite&&(r+="; SameSite="+n.sameSite),this.headers.append("Set-Cookie",r)}deleteCookie(t,e={}){this.setCookie(t,"",{...e,maxAge:0,expires:new Date(0)})}i(t){if(!t)return this.headers;const e=Object.keys(t);if(0===e.length)return this.headers;const n=new Headers(this.headers);for(let r=0;r<e.length;r++)n.set(e[r],t[e[r]]);return n}text(t,e=this.status,n={}){const r=this.i(n);return r.has("Content-Type")||r.set("Content-Type","text/plain; charset=utf-8"),new Response(t,{status:e,headers:r})}json(t,e=this.status,n={}){const r=this.i(n);return r.has("Content-Type")||r.set("Content-Type","application/json; charset=utf-8"),new Response(JSON.stringify(t),{status:e,headers:r})}async html(t,e=this.status,n={}){const r=this.i(n);let o;return r.has("Content-Type")||r.set("Content-Type","text/html; charset=utf-8"),o="string"==typeof t?t:t&&t.__isJSX||Array.isArray(t)?await renderToString(t,this):null==t?"":String(t),new Response(o,{status:e,headers:r})}async page(t,e=this.status,n={}){const r=this.i(n);let o;return r.has("Content-Type")||r.set("Content-Type","text/html; charset=utf-8"),o="string"==typeof t?t:t&&t.__isJSX||Array.isArray(t)?await renderToString(t,this):null==t?"":String(t),/^\s*<!doctype\s/i.test(o)||(o="<!DOCTYPE html>"+o),new Response(o,{status:e,headers:r})}redirect(t,e=302){const n=new Headers(this.headers);return n.set("Location",String(t)),n.delete("Content-Type"),new Response(null,{status:e,headers:n})}},S=class{constructor(){this.children=null,this.paramChild=null,this.handler=null}},C=class{constructor(){this.root=new S}add(t,e){let n=this.root;const r=[];for(let e=0;e<t.length;e++){const s=t[e];if(58===s.charCodeAt(0)){const t=s.slice(1);r.push(o(t)?t:"_p"+e),n.paramChild||(n.paramChild=new S),n=n.paramChild}else{n.children||(n.children=new Map);let t=n.children.get(s);t||(t=new S,n.children.set(s,t)),n=t}}n.handler={handler:e,paramNames:r}}match(t){const e=[],n=this.l(this.root,t,0,e);if(!n)return null;const{handler:r,paramNames:o}=n,s=Object.create(null);for(let t=0;t<o.length;t++)s[o[t]]=e[t];return{handler:r,params:s}}l(t,e,n,r){if(n===e.length)return t.handler;const o=e[n],s=t.children;if(s){const t=s.get(o);if(t){const o=this.l(t,e,n+1,r);if(o)return o}}const i=t.paramChild;if(i){r.push(o);const t=this.l(i,e,n+1,r);if(t)return t;r.pop()}return null}},x=["GET","POST","PUT","DELETE","PATCH","OPTIONS","HEAD"],Edge=class r{constructor(t=""){this.prefix=t&&"/"!==t?e(t):"",this.staticRoutes=new Map;for(let t=0;t<x.length;t++)this.staticRoutes.set(x[t],new Map);this.dynamicTries={};for(let t=0;t<x.length;t++)this.dynamicTries[x[t]]=new C;this.u=[],this.scheduledHandler=null,this.notFoundHandler=null,this.errorHandler=null}set routes(t){const e=Array.isArray(t)?t:t?[t]:[];for(let t=0;t<e.length;t++){const n=e[t];if(!(n instanceof r))throw new Error("app.routes entries must be Edge instances");this.h(n)}}h(t){const e=t.u,r=this.prefix;for(let t=0;t<e.length;t++){const o=e[t],s=n(r,o.path);this.u.push({method:o.method,path:s,handler:o.handler}),this.p(o.method,s,o.handler)}}m(t,e,r,o){if("function"==typeof r&&(o=r),"function"!=typeof o)throw new Error("Handler for "+t+" "+e+" must be a function");const s=n(this.prefix,e);this.u.push({method:t,path:s,handler:o}),this.p(t,s,o)}p(e,n,r){-1===n.indexOf(":")?this.staticRoutes.get(e).set(n,r):this.dynamicTries[e].add(t(n),r)}get(t,e,n){this.m("GET",t,e,n)}post(t,e,n){this.m("POST",t,e,n)}put(t,e,n){this.m("PUT",t,e,n)}delete(t,e,n){this.m("DELETE",t,e,n)}patch(t,e,n){this.m("PATCH",t,e,n)}options(t,e,n){this.m("OPTIONS",t,e,n)}head(t,e,n){this.m("HEAD",t,e,n)}scheduled(t){this.scheduledHandler=t}notFound(t){if("function"!=typeof t)throw new Error("notFound(handler) requires a function");this.notFoundHandler=t}onError(t){if("function"!=typeof t)throw new Error("onError(handler) requires a function");this.errorHandler=t}S(t,e){const n=new Headers(e.headers);t.headers.forEach((t,e)=>{"set-cookie"!==e.toLowerCase()&&(n.has(e)||n.set(e,t))});const r="function"==typeof t.headers.getSetCookie?t.headers.getSetCookie():t.headers.get("Set-Cookie")?[t.headers.get("Set-Cookie")]:[];if(r.length){const t="function"==typeof n.getSetCookie?n.getSetCookie():n.get("Set-Cookie")?[n.get("Set-Cookie")]:[],e=new Set(t);for(let t=0;t<r.length;t++){const o=r[t];e.has(o)||(n.append("Set-Cookie",o),e.add(o))}}const o=204===e.status||304===e.status?null:e.body;return new Response(o,{status:e.status,statusText:e.statusText,headers:n})}async fetch(n,r){const o=new URL(n.url),s=e(o.pathname),i=n.method,c=this.staticRoutes.get(i);if(c){const t=c.get(s);if(t)return this.C(t,n,r,Object.create(null),o)}const a=this.dynamicTries[i];if(a){const e=a.match(t(s));if(e)return this.C(e.handler,n,r,e.params,o)}if("OPTIONS"===i){const t=new Context(n,r,Object.create(null),o);return this.S(t,new Response(null,{status:204}))}if(this.notFoundHandler)return this.C(this.notFoundHandler,n,r,Object.create(null),o);const l=new Context(n,r,Object.create(null),o);return this.S(l,new Response("Not Found",{status:404}))}async O(t,e){const n=this.errorHandler;if(!n)return t.text("Internal Server Error",500);try{const r=await n(t,e);return r instanceof Response?this.S(t,r):r&&r.__isJSX||Array.isArray(r)?t.html(r,500):t.text("Internal Server Error",500)}catch{return t.text("Internal Server Error",500)}}async C(t,e,n,r,o){const s=new Context(e,n,r,o);let i;try{const e=await t(s);i=e instanceof Response?this.S(s,e):e&&e.__isJSX?await s.html(e):s.text("OK")}catch(t){i=await this.O(s,t)}return i}},O=Edge;export{Context,Edge,Fragment,O as default,jsx,renderToString,useCtx};
|
|
33
|
+
function t(t){const e=[],n=t.length;let r=1;for(let o=1;o<=n;o++)o!==n&&47!==t.charCodeAt(o)||(o>r&&e.push(t.slice(r,o)),r=o+1);return e}function e(t){const e=t.length;return e>1&&47===t.charCodeAt(e-1)?t.slice(0,-1):t}function n(t,e){if(!e)return t||"/";if(!t||"/"===t)return 47===e.charCodeAt(0)?e:"/"+e;const n=t.charCodeAt(t.length-1),r=e.charCodeAt(0);let o;o=47===n?47===r?t+e.slice(1):t+e:47===r?t+e:t+"/"+e;const s=o.length;return s>1&&47===o.charCodeAt(s-1)?o.slice(0,-1):o}var r=new Set(["__proto__","constructor","prototype"]);function o(t){return"string"==typeof t&&!r.has(t)}function s(t){const e=t.length;let n=0;for(;n<e;){const e=t.charCodeAt(n);if(38===e||60===e||62===e||34===e||39===e)break;n++}if(n===e)return t;let r=t.slice(0,n),o=n;for(;n<e;n++){const e=t.charCodeAt(n);let s;if(38===e)s="&";else if(60===e)s="<";else if(62===e)s=">";else if(34===e)s=""";else{if(39!==e)continue;s="'"}n>o&&(r+=t.slice(o,n)),r+=s,o=n+1}return o<e&&(r+=t.slice(o)),r}function i(t){return t>=65&&t<=90||t>=97&&t<=122}function c(t){return t>=48&&t<=57}function a(t){const e=t.length;if(0===e)return!1;const n=t.charCodeAt(0);if(!i(n)&&95!==n&&58!==n)return!1;for(let n=1;n<e;n++){const e=t.charCodeAt(n);if(!i(e)&&!c(e)&&95!==e&&58!==e&&46!==e&&45!==e)return!1}return!0}function l(t){const e=t.charCodeAt(0),n=t.charCodeAt(1);return!(111!==e&&79!==e||110!==n&&78!==n)}var Fragment=Symbol("Fragment");function jsx(t,e,...n){return{type:t,props:e||{},children:n,__isJSX:!0}}var u=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),f=new Set(["allowfullscreen","async","autofocus","autoplay","checked","controls","default","defer","disabled","formnovalidate","hidden","inert","ismap","itemscope","loop","multiple","muted","nomodule","novalidate","open","playsinline","readonly","required","reversed","selected"]),h=new Set(["animation-iteration-count","border-image-outset","border-image-slice","border-image-width","box-flex","box-flex-group","box-ordinal-group","column-count","columns","flex","flex-grow","flex-positive","flex-shrink","flex-negative","flex-order","grid-row","grid-row-end","grid-row-span","grid-row-start","grid-column","grid-column-end","grid-column-span","grid-column-start","font-weight","line-clamp","line-height","opacity","order","orphans","tab-size","widows","z-index","zoom","fill-opacity","flood-opacity","stop-opacity","stroke-dasharray","stroke-dashoffset","stroke-miterlimit","stroke-opacity","stroke-width"]),p=new Map;function d(t){const e=p.get(t);if(void 0!==e)return e;const n=t.replace(/([A-Z])/g,"-$1").toLowerCase().replace(/^-/,"");return p.size>=2e3&&p.clear(),p.set(t,n),n}function y(t){if(!t||"object"!=typeof t)return"";let e="";for(const n in t){if(!Object.prototype.hasOwnProperty.call(t,n))continue;const r=d(n);let o=t[n];"number"!=typeof o||h.has(r)||(o+="px"),e&&(e+=";"),e+=r+":"+o}return e}var w=new Set(["href","src","xlink:href","action","formaction"]),m=[];function useCtx(){const t=m[m.length-1];if(!t)throw new Error("useCtx() must be called synchronously at the top of a component (before any await). It is not available after an await, nor outside a render.");return t}async function renderToString(t,e){if(void 0===e&&(e=m[m.length-1]),null==t||"boolean"==typeof t)return"";const n=typeof t;if("string"===n)return s(t);if("number"===n)return s(String(t));if("object"===n&&"function"==typeof t.then)return renderToString(await t,e);if(Array.isArray(t)){let n="";for(let r=0;r<t.length;r++)n+=await renderToString(t[r],e);return n}if(!t.__isJSX)return s(String(t));const{type:r,props:h,children:p}=t;if(r===Fragment){let t="";for(let n=0;n<p.length;n++)t+=await renderToString(p[n],e);return t}if("symbol"==typeof r)return"";if("function"==typeof r){let t;if(e){m.push(e);try{t=r({...h,children:p})}finally{m.pop()}}else t=r({...h,children:p});return renderToString(await t,e)}if("string"!=typeof r||!function(t){const e=t.length;if(0===e||!i(t.charCodeAt(0)))return!1;for(let n=1;n<e;n++){const e=t.charCodeAt(n);if(!i(e)&&!c(e)&&45!==e)return!1}return!0}(r))return"";const d=u.has(r);let g="";for(const t in h){if(!Object.prototype.hasOwnProperty.call(h,t))continue;if("children"===t||"key"===t||"ref"===t)continue;const e=h[t];if(null==e||!1===e)continue;if("dangerouslySetInnerHTML"===t)continue;let n=t;if("className"===t||"class"===t?n="class":"htmlFor"!==t&&"for"!==t||(n="for"),o(n)&&!l(n)&&a(n)){if(w.has(n)){const t=String(e).replace(/[\u0000-\u0020]/g,"").toLowerCase();if(t.startsWith("javascript:")||t.startsWith("vbscript:")||t.startsWith("data:text/html"))continue}if(f.has(n))!0===e&&(g+=" "+n);else if(!0!==e){if("style"===n&&"object"==typeof e){const t=y(e);t&&(g+=' style="'+s(t)+'"');continue}g+=" "+n+'="'+s(String(e))+'"'}else g+=" "+n+'="true"'}}let b="";const S=h.dangerouslySetInnerHTML;if(S&&null!=S.__html)b=S.__html;else for(let t=0;t<p.length;t++)b+=await renderToString(p[t],e);return d?"<"+r+g+"/>":"<"+r+g+">"+b+"</"+r+">"}var g=Object.freeze([["X-Content-Type-Options","nosniff"],["X-Frame-Options","DENY"],["Referrer-Policy","strict-origin-when-cross-origin"],["Permissions-Policy","geolocation=(), microphone=(), camera=()"]]),b=Object.freeze(Object.create(null)),Context=class{constructor(t,e,n={},r=null,o=null){this.req=t,this.env=e,this.ctx=o,this.executionCtx=o,this.params=n,this.status=200,this.headers=new Headers(g),this.t=null,this.url=r||new URL(t.url)}o(){if(null!==this.t)return this.t;const t=this.req.headers.get("Cookie");if(!t)return this.t=b,b;const e=Object.create(null);let n=0;const r=t.length;for(;n<r;){let s=t.indexOf(";",n);-1===s&&(s=r);let i=n,c=s;for(;i<c&&32===t.charCodeAt(i);)i++;for(;c>i&&32===t.charCodeAt(c-1);)c--;if(c>i){const n=t.indexOf("=",i);if(n>i&&n<c)try{const r=decodeURIComponent(t.slice(i,n));o(r)&&(e[r]=decodeURIComponent(t.slice(n+1,c)))}catch{}}n=s+1}return this.t=e,e}getCookie(t){const e=this.o()[t];return void 0===e?null:e}get query(){return this.url.searchParams}waitUntil(t){return this.ctx&&"function"==typeof this.ctx.waitUntil&&this.ctx.waitUntil(t),t}passThroughOnException(){this.ctx&&"function"==typeof this.ctx.passThroughOnException&&this.ctx.passThroughOnException()}setCookie(t,e,n={}){if("string"!=typeof t||!o(t)||/[\r\n;=]/.test(t))return;let r=encodeURIComponent(t)+"="+encodeURIComponent(e);n.path&&(r+="; Path="+n.path),n.domain&&(r+="; Domain="+n.domain),void 0!==n.maxAge&&(r+="; Max-Age="+n.maxAge),n.expires&&(r+="; Expires="+n.expires.toUTCString()),n.secure&&(r+="; Secure"),n.httpOnly&&(r+="; HttpOnly"),n.sameSite&&(r+="; SameSite="+n.sameSite),this.headers.append("Set-Cookie",r)}deleteCookie(t,e={}){this.setCookie(t,"",{...e,maxAge:0,expires:new Date(0)})}i(t){if(!t)return this.headers;const e=Object.keys(t);if(0===e.length)return this.headers;const n=new Headers(this.headers);for(let r=0;r<e.length;r++)n.set(e[r],t[e[r]]);return n}text(t,e=this.status,n={}){const r=this.i(n);return r.has("Content-Type")||r.set("Content-Type","text/plain; charset=utf-8"),new Response(t,{status:e,headers:r})}json(t,e=this.status,n={}){const r=this.i(n);return r.has("Content-Type")||r.set("Content-Type","application/json; charset=utf-8"),new Response(JSON.stringify(t),{status:e,headers:r})}async html(t,e=this.status,n={}){const r=this.i(n);let o;return r.has("Content-Type")||r.set("Content-Type","text/html; charset=utf-8"),o="string"==typeof t?t:t&&t.__isJSX||Array.isArray(t)?await renderToString(t,this):null==t?"":String(t),new Response(o,{status:e,headers:r})}async page(t,e=this.status,n={}){const r=this.i(n);let o;return r.has("Content-Type")||r.set("Content-Type","text/html; charset=utf-8"),o="string"==typeof t?t:t&&t.__isJSX||Array.isArray(t)?await renderToString(t,this):null==t?"":String(t),/^\s*<!doctype\s/i.test(o)||(o="<!DOCTYPE html>"+o),new Response(o,{status:e,headers:r})}redirect(t,e=302){const n=new Headers(this.headers);return n.set("Location",String(t)),n.delete("Content-Type"),new Response(null,{status:e,headers:n})}},S=class{constructor(){this.children=null,this.paramChild=null,this.handler=null}},C=class{constructor(){this.root=new S}add(t,e){let n=this.root;const r=[];for(let e=0;e<t.length;e++){const s=t[e];if(58===s.charCodeAt(0)){const t=s.slice(1);r.push(o(t)?t:"_p"+e),n.paramChild||(n.paramChild=new S),n=n.paramChild}else{n.children||(n.children=new Map);let t=n.children.get(s);t||(t=new S,n.children.set(s,t)),n=t}}n.handler={handler:e,paramNames:r}}match(t){const e=[],n=this.l(this.root,t,0,e);if(!n)return null;const{handler:r,paramNames:o}=n,s=Object.create(null);for(let t=0;t<o.length;t++)s[o[t]]=e[t];return{handler:r,params:s}}l(t,e,n,r){if(n===e.length)return t.handler;const o=e[n],s=t.children;if(s){const t=s.get(o);if(t){const o=this.l(t,e,n+1,r);if(o)return o}}const i=t.paramChild;if(i){r.push(o);const t=this.l(i,e,n+1,r);if(t)return t;r.pop()}return null}},x=["GET","POST","PUT","DELETE","PATCH","OPTIONS","HEAD"],Edge=class r{constructor(t=""){this.prefix=t&&"/"!==t?e(t):"",this.staticRoutes=new Map;for(let t=0;t<x.length;t++)this.staticRoutes.set(x[t],new Map);this.dynamicTries={};for(let t=0;t<x.length;t++)this.dynamicTries[x[t]]=new C;this.u=[],this.scheduledHandler=null,this.notFoundHandler=null,this.errorHandler=null}set routes(t){const e=Array.isArray(t)?t:t?[t]:[];for(let t=0;t<e.length;t++){const n=e[t];if(!(n instanceof r))throw new Error("app.routes entries must be Edge instances");this.h(n)}}h(t){const e=t.u,r=this.prefix;for(let t=0;t<e.length;t++){const o=e[t],s=n(r,o.path);this.u.push({method:o.method,path:s,handler:o.handler}),this.p(o.method,s,o.handler)}}m(t,e,r,o){if("function"==typeof r&&(o=r),"function"!=typeof o)throw new Error("Handler for "+t+" "+e+" must be a function");const s=n(this.prefix,e);this.u.push({method:t,path:s,handler:o}),this.p(t,s,o)}p(e,n,r){-1===n.indexOf(":")?this.staticRoutes.get(e).set(n,r):this.dynamicTries[e].add(t(n),r)}get(t,e,n){this.m("GET",t,e,n)}post(t,e,n){this.m("POST",t,e,n)}put(t,e,n){this.m("PUT",t,e,n)}delete(t,e,n){this.m("DELETE",t,e,n)}patch(t,e,n){this.m("PATCH",t,e,n)}options(t,e,n){this.m("OPTIONS",t,e,n)}head(t,e,n){this.m("HEAD",t,e,n)}scheduled(t){this.scheduledHandler=t}notFound(t){if("function"!=typeof t)throw new Error("notFound(handler) requires a function");this.notFoundHandler=t}onError(t){if("function"!=typeof t)throw new Error("onError(handler) requires a function");this.errorHandler=t}S(t,e){const n=new Headers(e.headers);t.headers.forEach((t,e)=>{"set-cookie"!==e.toLowerCase()&&(n.has(e)||n.set(e,t))});const r="function"==typeof t.headers.getSetCookie?t.headers.getSetCookie():t.headers.get("Set-Cookie")?[t.headers.get("Set-Cookie")]:[];if(r.length){const t="function"==typeof n.getSetCookie?n.getSetCookie():n.get("Set-Cookie")?[n.get("Set-Cookie")]:[],e=new Set(t);for(let t=0;t<r.length;t++){const o=r[t];e.has(o)||(n.append("Set-Cookie",o),e.add(o))}}const o=204===e.status||304===e.status?null:e.body;return new Response(o,{status:e.status,statusText:e.statusText,headers:n})}async fetch(n,r,o){const s=new URL(n.url),i=e(s.pathname),c=n.method,a=this.staticRoutes.get(c);if(a){const t=a.get(i);if(t)return this.C(t,n,r,Object.create(null),s,o)}const l=this.dynamicTries[c];if(l){const e=l.match(t(i));if(e)return this.C(e.handler,n,r,e.params,s,o)}if("OPTIONS"===c){const t=new Context(n,r,Object.create(null),s,o);return this.S(t,new Response(null,{status:204}))}if(this.notFoundHandler)return this.C(this.notFoundHandler,n,r,Object.create(null),s,o);const u=new Context(n,r,Object.create(null),s,o);return this.S(u,new Response("Not Found",{status:404}))}async O(t,e){const n=this.errorHandler;if(!n)return t.text("Internal Server Error",500);try{const r=await n(t,e);return r instanceof Response?this.S(t,r):r&&r.__isJSX||Array.isArray(r)?t.html(r,500):t.text("Internal Server Error",500)}catch{return t.text("Internal Server Error",500)}}async C(t,e,n,r,o,s){const i=new Context(e,n,r,o,s);let c;try{const e=await t(i);c=e instanceof Response?this.S(i,e):e&&e.__isJSX?await i.html(e):i.text("OK")}catch(t){c=await this.O(i,t)}return c}},O=Edge;export{Context,Edge,Fragment,O as default,jsx,renderToString,useCtx};
|