@sourceregistry/node-webserver 1.0.0 → 1.1.0
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/README.md +31 -29
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +172 -175
- package/dist/index.es.js.map +1 -1
- package/dist/types/router.d.ts +1 -1
- package/dist/types/server.d.ts +5 -6
- package/examples/simple.ts +2 -2
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -31,28 +31,28 @@ const Q = {
|
|
|
31
31
|
cacheControl: "public, max-age=0",
|
|
32
32
|
dotFiles: "ignore"
|
|
33
33
|
};
|
|
34
|
-
async function ee(
|
|
35
|
-
const s =
|
|
34
|
+
async function ee(o, e, t = {}) {
|
|
35
|
+
const s = ae(e), r = {
|
|
36
36
|
...Z,
|
|
37
37
|
...t
|
|
38
|
-
}, n = await te(
|
|
39
|
-
if (
|
|
40
|
-
return
|
|
41
|
-
const i =
|
|
42
|
-
if (!v(n,
|
|
38
|
+
}, n = await te(o), a = se(s, r.dotFiles);
|
|
39
|
+
if (a instanceof Response)
|
|
40
|
+
return a;
|
|
41
|
+
const i = a.length > 0 ? a.join(D) : "", l = O(n, i);
|
|
42
|
+
if (!v(n, l))
|
|
43
43
|
return new Response("Forbidden", { status: 403 });
|
|
44
|
-
const c = await re(
|
|
44
|
+
const c = await re(l, n, r.index);
|
|
45
45
|
if (c instanceof Response)
|
|
46
46
|
return c;
|
|
47
|
-
const
|
|
48
|
-
"content-length": String(
|
|
47
|
+
const u = await W(c), d = new Headers({
|
|
48
|
+
"content-length": String(u.size),
|
|
49
49
|
"content-type": ne(c),
|
|
50
50
|
"cache-control": r.cacheControl,
|
|
51
|
-
"last-modified":
|
|
51
|
+
"last-modified": u.mtime.toUTCString(),
|
|
52
52
|
"x-content-type-options": "nosniff"
|
|
53
53
|
});
|
|
54
54
|
if (t.headers) {
|
|
55
|
-
const h = typeof t.headers == "function" ? t.headers(c,
|
|
55
|
+
const h = typeof t.headers == "function" ? t.headers(c, u) : t.headers;
|
|
56
56
|
new Headers(h).forEach((f, y) => {
|
|
57
57
|
d.set(y, f);
|
|
58
58
|
});
|
|
@@ -62,13 +62,13 @@ async function ee(a, e, t = {}) {
|
|
|
62
62
|
headers: d
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
|
-
async function te(
|
|
66
|
-
return L(
|
|
65
|
+
async function te(o) {
|
|
66
|
+
return L(o);
|
|
67
67
|
}
|
|
68
|
-
function se(
|
|
69
|
-
if (
|
|
68
|
+
function se(o, e) {
|
|
69
|
+
if (o.includes("\0"))
|
|
70
70
|
return new Response("Bad Request", { status: 400 });
|
|
71
|
-
const t =
|
|
71
|
+
const t = o.replace(/\\/g, "/").split("/").filter(Boolean), s = [];
|
|
72
72
|
for (const r of t) {
|
|
73
73
|
let n;
|
|
74
74
|
try {
|
|
@@ -90,42 +90,42 @@ function se(a, e) {
|
|
|
90
90
|
}
|
|
91
91
|
return s;
|
|
92
92
|
}
|
|
93
|
-
async function re(
|
|
93
|
+
async function re(o, e, t) {
|
|
94
94
|
try {
|
|
95
|
-
if ((await I(
|
|
96
|
-
const r = O(
|
|
95
|
+
if ((await I(o)).isDirectory()) {
|
|
96
|
+
const r = O(o, t);
|
|
97
97
|
return T(r, e);
|
|
98
98
|
}
|
|
99
|
-
return T(
|
|
99
|
+
return T(o, e);
|
|
100
100
|
} catch {
|
|
101
101
|
return new Response("Not Found", { status: 404 });
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
async function T(
|
|
104
|
+
async function T(o, e) {
|
|
105
105
|
try {
|
|
106
|
-
const t = await L(
|
|
106
|
+
const t = await L(o);
|
|
107
107
|
return v(e, t) ? (await W(t)).isFile() ? t : new Response("Not Found", { status: 404 }) : new Response("Forbidden", { status: 403 });
|
|
108
108
|
} catch {
|
|
109
109
|
return new Response("Not Found", { status: 404 });
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
-
function v(
|
|
113
|
-
const t = $(
|
|
112
|
+
function v(o, e) {
|
|
113
|
+
const t = $(o, e);
|
|
114
114
|
return t === "" || !t.startsWith("..") && !U(t);
|
|
115
115
|
}
|
|
116
|
-
function ne(
|
|
117
|
-
return Q[F(
|
|
116
|
+
function ne(o) {
|
|
117
|
+
return Q[F(o).toLowerCase()] ?? "application/octet-stream";
|
|
118
118
|
}
|
|
119
|
-
function
|
|
120
|
-
return typeof
|
|
119
|
+
function ae(o) {
|
|
120
|
+
return typeof o.params.path == "string" ? o.params.path : o.url.pathname.replace(/^\/+/, "");
|
|
121
121
|
}
|
|
122
|
-
function _(
|
|
123
|
-
return
|
|
122
|
+
function _(o) {
|
|
123
|
+
return o instanceof Response && o.status >= 400 && o.status < 600;
|
|
124
124
|
}
|
|
125
|
-
function N(
|
|
126
|
-
return
|
|
125
|
+
function N(o) {
|
|
126
|
+
return o instanceof Response && o.status >= 300 && o.status < 400;
|
|
127
127
|
}
|
|
128
|
-
class
|
|
128
|
+
class oe {
|
|
129
129
|
constructor(e) {
|
|
130
130
|
this.data = /* @__PURE__ */ new Map(), this.windowMs = e.windowMs, this.startCleanup();
|
|
131
131
|
}
|
|
@@ -148,26 +148,26 @@ class ae {
|
|
|
148
148
|
this.data.clear();
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
-
function ie(
|
|
151
|
+
function ie(o) {
|
|
152
152
|
const {
|
|
153
153
|
windowMs: e = 6e4,
|
|
154
154
|
max: t,
|
|
155
155
|
key: s = (c) => c.getClientAddress(),
|
|
156
156
|
message: r = "Too many requests, please try again later.",
|
|
157
157
|
statusCode: n = 429,
|
|
158
|
-
headers:
|
|
158
|
+
headers: a = "include",
|
|
159
159
|
onRateLimit: i,
|
|
160
|
-
store:
|
|
161
|
-
} =
|
|
162
|
-
return async (c,
|
|
163
|
-
const d = `rl:${s(c)}`, { current: h, reset: f } = await
|
|
160
|
+
store: l = new oe({ windowMs: e })
|
|
161
|
+
} = o;
|
|
162
|
+
return async (c, u) => {
|
|
163
|
+
const d = `rl:${s(c)}`, { current: h, reset: f } = await l.incr(d), y = Math.ceil((f - Date.now()) / 1e3);
|
|
164
164
|
if (h > t) {
|
|
165
165
|
i && i(c, { current: h, max: t, key: d });
|
|
166
166
|
const w = {
|
|
167
167
|
status: n,
|
|
168
168
|
headers: new Headers()
|
|
169
169
|
}, p = w.headers;
|
|
170
|
-
|
|
170
|
+
a === "include" && (p.set("X-RateLimit-Limit", String(t)), p.set("X-RateLimit-Remaining", "0"), p.set("X-RateLimit-Reset", String(Math.floor(f / 1e3))), p.set("Retry-After", String(y)));
|
|
171
171
|
let m;
|
|
172
172
|
return typeof r == "string" ? (m = r, p.set("Content-Type", "text/plain")) : (m = JSON.stringify(r), p.set("Content-Type", "application/json")), new Response(m, w);
|
|
173
173
|
}
|
|
@@ -176,7 +176,7 @@ function ie(a) {
|
|
|
176
176
|
limit: t,
|
|
177
177
|
reset: new Date(f),
|
|
178
178
|
remaining: t - h
|
|
179
|
-
},
|
|
179
|
+
}, a === "include") {
|
|
180
180
|
const w = {
|
|
181
181
|
"X-RateLimit-Limit": String(t),
|
|
182
182
|
"X-RateLimit-Remaining": String(t - h),
|
|
@@ -189,7 +189,7 @@ function ie(a) {
|
|
|
189
189
|
});
|
|
190
190
|
}, p(w);
|
|
191
191
|
}
|
|
192
|
-
return
|
|
192
|
+
return u();
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
195
|
const Ee = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
@@ -203,13 +203,13 @@ const Ee = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
|
203
203
|
"PATCH",
|
|
204
204
|
"HEAD",
|
|
205
205
|
"OPTIONS"
|
|
206
|
-
],
|
|
206
|
+
], le = [
|
|
207
207
|
"Accept",
|
|
208
208
|
"Accept-Language",
|
|
209
209
|
"Content-Language",
|
|
210
210
|
"Content-Type",
|
|
211
211
|
"Range"
|
|
212
|
-
],
|
|
212
|
+
], ue = [
|
|
213
213
|
"Authorization",
|
|
214
214
|
"X-Auth-Token",
|
|
215
215
|
"X-Requested-With",
|
|
@@ -219,33 +219,33 @@ const Ee = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
|
219
219
|
"X-Real-IP",
|
|
220
220
|
"X-Custom-Header"
|
|
221
221
|
];
|
|
222
|
-
function j(
|
|
223
|
-
return !
|
|
222
|
+
function j(o, e) {
|
|
223
|
+
return !o || !e ? !1 : e === "*" ? !0 : e === "null" ? o === "null" : Array.isArray(e) ? e.some((t) => j(o, t)) : typeof e == "function" ? e(o) : e instanceof RegExp ? e.test(o) : o === e;
|
|
224
224
|
}
|
|
225
|
-
function de(
|
|
225
|
+
function de(o, e) {
|
|
226
226
|
const { origin: t = "*" } = e;
|
|
227
|
-
return
|
|
227
|
+
return o ? t === "*" ? e.credentials ? o : "*" : j(o, t) ? o : null : t === "*" ? "*" : null;
|
|
228
228
|
}
|
|
229
|
-
function he(
|
|
229
|
+
function he(o = {}) {
|
|
230
230
|
const {
|
|
231
231
|
methods: e = ce,
|
|
232
|
-
allowedHeaders: t =
|
|
232
|
+
allowedHeaders: t = ue,
|
|
233
233
|
exposedHeaders: s,
|
|
234
234
|
credentials: r = !1,
|
|
235
235
|
maxAge: n = 86400,
|
|
236
|
-
onResponse:
|
|
237
|
-
} =
|
|
236
|
+
onResponse: a
|
|
237
|
+
} = o, i = "Origin,Access-Control-Request-Method,Access-Control-Request-Headers", l = e.join(","), c = [...le, ...t].join(","), u = [
|
|
238
238
|
["Vary", i],
|
|
239
|
-
["Access-Control-Allow-Methods",
|
|
239
|
+
["Access-Control-Allow-Methods", l],
|
|
240
240
|
["Access-Control-Allow-Headers", c]
|
|
241
241
|
];
|
|
242
|
-
return s &&
|
|
243
|
-
const f = d.request, y = f.headers.get("Origin"), w = f.method === "OPTIONS" && y !== null && f.headers.has("Access-Control-Request-Method"), p = de(y,
|
|
242
|
+
return s && u.push(["Access-Control-Expose-Headers", s.join(",")]), r && u.push(["Access-Control-Allow-Credentials", "true"]), n && u.push(["Access-Control-Max-Age", n.toString()]), async (d, h) => {
|
|
243
|
+
const f = d.request, y = f.headers.get("Origin"), w = f.method === "OPTIONS" && y !== null && f.headers.has("Access-Control-Request-Method"), p = de(y, o);
|
|
244
244
|
if (w) {
|
|
245
245
|
if (!p)
|
|
246
246
|
return new Response(null, { status: 403 });
|
|
247
247
|
const g = new Response(null, { status: 204 });
|
|
248
|
-
for (const [H, M] of
|
|
248
|
+
for (const [H, M] of u)
|
|
249
249
|
g.headers.set(H, M);
|
|
250
250
|
return g.headers.set("Access-Control-Allow-Origin", p), g;
|
|
251
251
|
}
|
|
@@ -253,12 +253,12 @@ function he(a = {}) {
|
|
|
253
253
|
if (!m) return;
|
|
254
254
|
if (!p) return m;
|
|
255
255
|
const S = new Response(m.body, m);
|
|
256
|
-
for (const [g, H] of
|
|
256
|
+
for (const [g, H] of u)
|
|
257
257
|
S.headers.set(g, H);
|
|
258
258
|
S.headers.set("Access-Control-Allow-Origin", p);
|
|
259
259
|
let A = S;
|
|
260
|
-
if (
|
|
261
|
-
const g =
|
|
260
|
+
if (a) {
|
|
261
|
+
const g = a(A);
|
|
262
262
|
g && (A = g);
|
|
263
263
|
}
|
|
264
264
|
return A;
|
|
@@ -319,26 +319,26 @@ class x {
|
|
|
319
319
|
action(e, t, ...s) {
|
|
320
320
|
const r = async (n) => {
|
|
321
321
|
try {
|
|
322
|
-
const
|
|
323
|
-
return this.formatActionResult(
|
|
324
|
-
} catch (
|
|
325
|
-
return this.handleActionError(
|
|
322
|
+
const a = await t(n);
|
|
323
|
+
return this.formatActionResult(a);
|
|
324
|
+
} catch (a) {
|
|
325
|
+
return this.handleActionError(a);
|
|
326
326
|
}
|
|
327
327
|
};
|
|
328
328
|
return this.addHandler("POST", e, r, s);
|
|
329
329
|
}
|
|
330
330
|
use(e, t, ...s) {
|
|
331
|
-
let r, n,
|
|
332
|
-
Array.isArray(e) ? ([r, n] = e,
|
|
333
|
-
const i = this.normalizePrefix(r), { regex:
|
|
331
|
+
let r, n, a = s;
|
|
332
|
+
Array.isArray(e) ? ([r, n] = e, a = e.length > 2 ? e.slice(2) : []) : (r = e, n = t);
|
|
333
|
+
const i = this.normalizePrefix(r), { regex: l, paramNames: c, isCatchAll: u, priority: d } = this.createPrefixRegex(i);
|
|
334
334
|
return this._nestedRouters.push({
|
|
335
335
|
prefix: i,
|
|
336
336
|
router: n,
|
|
337
|
-
regex:
|
|
337
|
+
regex: l,
|
|
338
338
|
paramNames: c,
|
|
339
|
-
isCatchAll:
|
|
339
|
+
isCatchAll: u,
|
|
340
340
|
priority: d,
|
|
341
|
-
middlewares:
|
|
341
|
+
middlewares: a
|
|
342
342
|
}), this;
|
|
343
343
|
}
|
|
344
344
|
// Global middleware
|
|
@@ -359,12 +359,12 @@ class x {
|
|
|
359
359
|
}
|
|
360
360
|
// WebSocket route
|
|
361
361
|
WS(e, t, ...s) {
|
|
362
|
-
const { regex: r, paramNames: n, isCatchAll:
|
|
362
|
+
const { regex: r, paramNames: n, isCatchAll: a, priority: i } = this.createPathRegex(e);
|
|
363
363
|
return this._wsRoutes.push({
|
|
364
364
|
path: e,
|
|
365
365
|
regex: r,
|
|
366
366
|
paramNames: n,
|
|
367
|
-
isCatchAll:
|
|
367
|
+
isCatchAll: a,
|
|
368
368
|
priority: i,
|
|
369
369
|
handler: t,
|
|
370
370
|
middlewares: s
|
|
@@ -383,11 +383,11 @@ class x {
|
|
|
383
383
|
for (const s of [...this._nestedRouters].sort((r, n) => n.priority - r.priority)) {
|
|
384
384
|
const r = t.match(s.regex);
|
|
385
385
|
if (!r || r.index !== 0) continue;
|
|
386
|
-
const n = r[0],
|
|
386
|
+
const n = r[0], a = t.slice(n.length) || "/", i = {
|
|
387
387
|
...e,
|
|
388
388
|
params: { ...e.params, ...this.extractPrefixParams(s, n) }
|
|
389
389
|
};
|
|
390
|
-
if (await s.router.canHandleWebSocketAtPath(i,
|
|
390
|
+
if (await s.router.canHandleWebSocketAtPath(i, a))
|
|
391
391
|
return !0;
|
|
392
392
|
}
|
|
393
393
|
for (const s of this._wsRoutes)
|
|
@@ -401,28 +401,28 @@ class x {
|
|
|
401
401
|
}
|
|
402
402
|
async handleWebSocketAtPath(e, t, s) {
|
|
403
403
|
this.wsRoutesSorted || this.sortWsRoutes();
|
|
404
|
-
for (const r of [...this._nestedRouters].sort((n,
|
|
404
|
+
for (const r of [...this._nestedRouters].sort((n, a) => a.priority - n.priority)) {
|
|
405
405
|
const n = s.match(r.regex);
|
|
406
406
|
if (!n || n.index !== 0) continue;
|
|
407
|
-
const
|
|
407
|
+
const a = n[0], i = s.slice(a.length) || "/", l = this.extractPrefixParams(r, a), c = {
|
|
408
408
|
...e,
|
|
409
|
-
params: { ...e.params, ...
|
|
410
|
-
},
|
|
411
|
-
if (await this.applyMiddlewaresWithList(c,
|
|
409
|
+
params: { ...e.params, ...l }
|
|
410
|
+
}, u = [...this._middlewares, ...r.middlewares], d = () => r.router.handleWebSocketAtPath(c, t, i);
|
|
411
|
+
if (await this.applyMiddlewaresWithList(c, u, d)) return !0;
|
|
412
412
|
}
|
|
413
413
|
for (const r of this._wsRoutes) {
|
|
414
414
|
if (!r.regex.test(s)) continue;
|
|
415
415
|
const n = s.match(r.regex);
|
|
416
416
|
if (!n) continue;
|
|
417
|
-
const
|
|
418
|
-
r.paramNames.map((
|
|
417
|
+
const a = Object.fromEntries(
|
|
418
|
+
r.paramNames.map((u, d) => [u, n[d + 1] || ""])
|
|
419
419
|
), i = {
|
|
420
420
|
...e,
|
|
421
|
-
params: { ...e.params, ...
|
|
421
|
+
params: { ...e.params, ...a },
|
|
422
422
|
route: { ...e.route, id: r.path },
|
|
423
423
|
websocket: t
|
|
424
|
-
},
|
|
425
|
-
if (await this.applyMiddlewaresWithList(i,
|
|
424
|
+
}, l = [...this._middlewares, ...r.middlewares];
|
|
425
|
+
if (await this.applyMiddlewaresWithList(i, l, () => r.handler(i)) === void 0)
|
|
426
426
|
return !0;
|
|
427
427
|
}
|
|
428
428
|
return !1;
|
|
@@ -435,18 +435,18 @@ class x {
|
|
|
435
435
|
const s = e.request.method;
|
|
436
436
|
let r = await this.runPreHandlers(e);
|
|
437
437
|
if (!r) {
|
|
438
|
-
const
|
|
438
|
+
const a = async () => {
|
|
439
439
|
const i = await this.handleNestedRouters(e, t);
|
|
440
440
|
return i || (this.routesSorted || this.sortRoutes(), this.handleLocalRoutes(e, s, t));
|
|
441
441
|
};
|
|
442
|
-
r = await this.applyMiddlewaresWithList(e, this._middlewares,
|
|
442
|
+
r = await this.applyMiddlewaresWithList(e, this._middlewares, a);
|
|
443
443
|
}
|
|
444
444
|
const n = r || new Response("No Content", { status: 204 });
|
|
445
445
|
return this.runPostHandlers(e, n);
|
|
446
446
|
}
|
|
447
447
|
// Apply middlewares utility
|
|
448
448
|
async applyMiddlewaresWithList(e, t, s) {
|
|
449
|
-
const r = [...t], n = async (
|
|
449
|
+
const r = [...t], n = async (a) => a >= r.length ? s() : r[a](e, () => n(a + 1));
|
|
450
450
|
return n(0);
|
|
451
451
|
}
|
|
452
452
|
async runPreHandlers(e) {
|
|
@@ -466,14 +466,14 @@ class x {
|
|
|
466
466
|
}
|
|
467
467
|
// Add route handler
|
|
468
468
|
addHandler(e, t, s, r = []) {
|
|
469
|
-
const { regex: n, paramNames:
|
|
469
|
+
const { regex: n, paramNames: a, isCatchAll: i, priority: l } = this.createPathRegex(t);
|
|
470
470
|
return this._routes.push({
|
|
471
471
|
method: e,
|
|
472
472
|
path: t,
|
|
473
473
|
regex: n,
|
|
474
|
-
paramNames:
|
|
474
|
+
paramNames: a,
|
|
475
475
|
isCatchAll: i,
|
|
476
|
-
priority:
|
|
476
|
+
priority: l,
|
|
477
477
|
handler: s,
|
|
478
478
|
middlewares: r
|
|
479
479
|
}), this.routesSorted = !1, this;
|
|
@@ -484,47 +484,47 @@ class x {
|
|
|
484
484
|
if (t) return t;
|
|
485
485
|
const s = [];
|
|
486
486
|
let r = !1, n;
|
|
487
|
-
const
|
|
488
|
-
n =
|
|
487
|
+
const a = e.split("/").filter(Boolean);
|
|
488
|
+
n = a.reduce((c, u) => u.startsWith("[...") ? c - 10 : u.startsWith("[[") ? c - 5 : u.startsWith("[") ? c - 1 : c + 1, 0);
|
|
489
489
|
let i = "^";
|
|
490
|
-
for (const c of
|
|
490
|
+
for (const c of a)
|
|
491
491
|
if (c.startsWith("[...") && c.endsWith("]")) {
|
|
492
492
|
r = !0;
|
|
493
|
-
const
|
|
494
|
-
s.push(
|
|
493
|
+
const u = c.slice(4, -1);
|
|
494
|
+
s.push(u), i += "/(.+)";
|
|
495
495
|
} else if (c.startsWith("[[") && c.endsWith("]]")) {
|
|
496
|
-
const
|
|
497
|
-
s.push(
|
|
496
|
+
const u = c.slice(2, -2);
|
|
497
|
+
s.push(u), i += "(?:/([^/]+))?";
|
|
498
498
|
} else if (c.startsWith("[") && c.endsWith("]")) {
|
|
499
|
-
const
|
|
500
|
-
s.push(
|
|
499
|
+
const u = c.slice(1, -1);
|
|
500
|
+
s.push(u), i += "/([^/]+)";
|
|
501
501
|
} else
|
|
502
502
|
i += "/" + c.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
503
503
|
i += "/?$";
|
|
504
|
-
const
|
|
505
|
-
return b.set(e,
|
|
504
|
+
const l = { regex: new RegExp(i), paramNames: s, isCatchAll: r, priority: n };
|
|
505
|
+
return b.set(e, l), l;
|
|
506
506
|
}
|
|
507
507
|
// Create regex for prefix
|
|
508
508
|
createPrefixRegex(e) {
|
|
509
509
|
const t = [];
|
|
510
510
|
let s = !1, r;
|
|
511
511
|
const n = e.split("/").filter(Boolean);
|
|
512
|
-
r = n.reduce((i,
|
|
513
|
-
let
|
|
512
|
+
r = n.reduce((i, l) => l.startsWith("[...") ? i - 10 : l.startsWith("[[") ? i - 5 : l.startsWith("[") ? i - 1 : i + 1, 0);
|
|
513
|
+
let a = "^";
|
|
514
514
|
for (const i of n)
|
|
515
515
|
if (i.startsWith("[...") && i.endsWith("]")) {
|
|
516
516
|
s = !0;
|
|
517
|
-
const
|
|
518
|
-
t.push(
|
|
517
|
+
const l = i.slice(4, -1);
|
|
518
|
+
t.push(l), a += "/(.+)";
|
|
519
519
|
} else if (i.startsWith("[[") && i.endsWith("]]")) {
|
|
520
|
-
const
|
|
521
|
-
t.push(
|
|
520
|
+
const l = i.slice(2, -2);
|
|
521
|
+
t.push(l), a += "(?:/([^/]+))?";
|
|
522
522
|
} else if (i.startsWith("[") && i.endsWith("]")) {
|
|
523
|
-
const
|
|
524
|
-
t.push(
|
|
523
|
+
const l = i.slice(1, -1);
|
|
524
|
+
t.push(l), a += "/([^/]+)";
|
|
525
525
|
} else
|
|
526
|
-
|
|
527
|
-
return
|
|
526
|
+
a += "/" + i.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
527
|
+
return a += "(?=/|$)", { regex: new RegExp(a), paramNames: t, isCatchAll: s, priority: r };
|
|
528
528
|
}
|
|
529
529
|
normalizePrefix(e) {
|
|
530
530
|
return e.startsWith("/") ? e.replace(/\/$/, "") : `/${e.replace(/\/$/, "")}`;
|
|
@@ -533,8 +533,8 @@ class x {
|
|
|
533
533
|
const s = t.match(e.regex);
|
|
534
534
|
if (!s) return {};
|
|
535
535
|
const r = {};
|
|
536
|
-
return e.isCatchAll && e.paramNames.length === 1 ? r[e.paramNames[0]] = s[1]?.replace(/^\//, "") || "" : e.paramNames.forEach((n,
|
|
537
|
-
r[n] = s[
|
|
536
|
+
return e.isCatchAll && e.paramNames.length === 1 ? r[e.paramNames[0]] = s[1]?.replace(/^\//, "") || "" : e.paramNames.forEach((n, a) => {
|
|
537
|
+
r[n] = s[a + 1] || "";
|
|
538
538
|
}), r;
|
|
539
539
|
}
|
|
540
540
|
// FIXED: Nested router handling without duplicate middleware application
|
|
@@ -543,10 +543,10 @@ class x {
|
|
|
543
543
|
for (const r of s) {
|
|
544
544
|
const n = t.match(r.regex);
|
|
545
545
|
if (!n || n.index !== 0) continue;
|
|
546
|
-
const
|
|
546
|
+
const a = n[0], i = t.slice(a.length) || "/", l = this.extractPrefixParams(r, a), c = {
|
|
547
547
|
...e,
|
|
548
|
-
params: { ...e.params, ...
|
|
549
|
-
},
|
|
548
|
+
params: { ...e.params, ...l }
|
|
549
|
+
}, u = async () => await r.router.handleAtPath(c, i), d = await this.applyMiddlewaresWithList(c, r.middlewares, u);
|
|
550
550
|
if (d) return d;
|
|
551
551
|
}
|
|
552
552
|
return null;
|
|
@@ -555,25 +555,25 @@ class x {
|
|
|
555
555
|
async handleLocalRoutes(e, t, s) {
|
|
556
556
|
const r = /* @__PURE__ */ new Set();
|
|
557
557
|
let n = !1;
|
|
558
|
-
for (const
|
|
559
|
-
if (!
|
|
560
|
-
const
|
|
561
|
-
if (!
|
|
558
|
+
for (const a of this._routes) {
|
|
559
|
+
if (!a.regex.test(s) || (r.add(a.method), a.method === "GET" && (n = !0, r.add("HEAD")), !(a.method === t || t === "HEAD" && a.method === "GET"))) continue;
|
|
560
|
+
const l = s.match(a.regex);
|
|
561
|
+
if (!l) continue;
|
|
562
562
|
const c = Object.fromEntries(
|
|
563
|
-
|
|
563
|
+
a.paramNames.map((d, h) => [d, l[h + 1] || ""])
|
|
564
564
|
);
|
|
565
|
-
e.params = { ...e.params, ...c }, e.route = { ...e.route, id:
|
|
566
|
-
const
|
|
567
|
-
return await this.applyMiddlewaresWithList(e,
|
|
565
|
+
e.params = { ...e.params, ...c }, e.route = { ...e.route, id: a.path };
|
|
566
|
+
const u = () => a.handler(e);
|
|
567
|
+
return await this.applyMiddlewaresWithList(e, a.middlewares, u);
|
|
568
568
|
}
|
|
569
569
|
if (r.size > 0 || t === "HEAD" && n) {
|
|
570
|
-
const
|
|
570
|
+
const a = [...r].join(", ");
|
|
571
571
|
return t === "OPTIONS" ? new Response(null, {
|
|
572
572
|
status: 200,
|
|
573
|
-
headers: { Allow:
|
|
573
|
+
headers: { Allow: a }
|
|
574
574
|
}) : new Response("Method Not Allowed", {
|
|
575
575
|
status: 405,
|
|
576
|
-
headers: { Allow:
|
|
576
|
+
headers: { Allow: a }
|
|
577
577
|
});
|
|
578
578
|
}
|
|
579
579
|
return new Response("Not Found", { status: 404 });
|
|
@@ -601,20 +601,20 @@ class x {
|
|
|
601
601
|
}
|
|
602
602
|
}
|
|
603
603
|
const R = {
|
|
604
|
-
success: (
|
|
605
|
-
status:
|
|
604
|
+
success: (o = 200, e) => new Response(JSON.stringify({ data: e, type: "success", status: o }), {
|
|
605
|
+
status: o,
|
|
606
606
|
headers: { "Content-Type": "application/json" }
|
|
607
607
|
}),
|
|
608
|
-
redirect: (
|
|
609
|
-
status:
|
|
608
|
+
redirect: (o = 302, e) => new Response(JSON.stringify({ location: e, type: "redirect", status: o }), {
|
|
609
|
+
status: o,
|
|
610
610
|
headers: { "Content-Type": "application/json" }
|
|
611
611
|
}),
|
|
612
|
-
error: (
|
|
613
|
-
status:
|
|
612
|
+
error: (o = 500, e) => new Response(JSON.stringify({ error: e, type: "error", status: o }), {
|
|
613
|
+
status: o,
|
|
614
614
|
headers: { "Content-Type": "application/json" }
|
|
615
615
|
}),
|
|
616
|
-
fail: (
|
|
617
|
-
status:
|
|
616
|
+
fail: (o = 400, e) => new Response(JSON.stringify({ data: e, type: "failure", status: o }), {
|
|
617
|
+
status: o,
|
|
618
618
|
headers: { "Content-Type": "application/json" }
|
|
619
619
|
})
|
|
620
620
|
};
|
|
@@ -640,9 +640,9 @@ class C extends Error {
|
|
|
640
640
|
super(e), this.status = 413, this.name = "PayloadTooLargeError";
|
|
641
641
|
}
|
|
642
642
|
}
|
|
643
|
-
class be {
|
|
643
|
+
class be extends x {
|
|
644
644
|
constructor(e) {
|
|
645
|
-
this.upgradeHandlerInstalled = !1, this.config = e ?? { type: "http", options: {} }, this.
|
|
645
|
+
super(), this.upgradeHandlerInstalled = !1, this.config = e ?? { type: "http", options: {} }, this.wss = new K({
|
|
646
646
|
noServer: !0,
|
|
647
647
|
maxPayload: this.config.security?.maxWebSocketPayload ?? 1024 * 1024
|
|
648
648
|
});
|
|
@@ -658,9 +658,6 @@ class be {
|
|
|
658
658
|
}
|
|
659
659
|
return this._server;
|
|
660
660
|
}
|
|
661
|
-
discard(e, t) {
|
|
662
|
-
return this.router.discard(e, t), this;
|
|
663
|
-
}
|
|
664
661
|
listen(...e) {
|
|
665
662
|
return this.upgradeHandlerInstalled || (this.installUpgradeHandler(), this.upgradeHandlerInstalled = !0), this.server.listen(...e), this;
|
|
666
663
|
}
|
|
@@ -677,23 +674,23 @@ class be {
|
|
|
677
674
|
t.destroy();
|
|
678
675
|
return;
|
|
679
676
|
}
|
|
680
|
-
const
|
|
677
|
+
const a = this.toRequestEvent(n, r, {
|
|
681
678
|
getClientAddress: () => e.socket.remoteAddress ?? "127.0.0.1",
|
|
682
679
|
setHeader: () => {
|
|
683
680
|
},
|
|
684
681
|
pushSetCookie: () => {
|
|
685
682
|
}
|
|
686
683
|
});
|
|
687
|
-
this.
|
|
684
|
+
this.canHandleWebSocket(a).then((i) => {
|
|
688
685
|
if (!i || !this.isAllowedWebSocketOrigin(e)) {
|
|
689
686
|
t.destroy();
|
|
690
687
|
return;
|
|
691
688
|
}
|
|
692
|
-
this.wss.handleUpgrade(e, t, s, (
|
|
693
|
-
this.
|
|
694
|
-
!c &&
|
|
689
|
+
this.wss.handleUpgrade(e, t, s, (l) => {
|
|
690
|
+
this.handleWebSocket(a, l).then((c) => {
|
|
691
|
+
!c && l.readyState === P.OPEN && l.close(1008, "Route not found");
|
|
695
692
|
}).catch((c) => {
|
|
696
|
-
console.error("WebSocket routing error:", c),
|
|
693
|
+
console.error("WebSocket routing error:", c), l.readyState === P.OPEN && l.close(1011, "Internal error");
|
|
697
694
|
});
|
|
698
695
|
});
|
|
699
696
|
}).catch(() => t.destroy());
|
|
@@ -715,24 +712,24 @@ class be {
|
|
|
715
712
|
t.statusCode = 413, t.end("Payload Too Large");
|
|
716
713
|
return;
|
|
717
714
|
}
|
|
718
|
-
const s = this.toWebRequest(e), r = new URL(s.url), n = {},
|
|
715
|
+
const s = this.toWebRequest(e), r = new URL(s.url), n = {}, a = [], i = this.toRequestEvent(s, r, {
|
|
719
716
|
getClientAddress: () => e.socket.remoteAddress ?? "127.0.0.1",
|
|
720
|
-
setHeader: (c,
|
|
721
|
-
n[c.toLowerCase()] =
|
|
717
|
+
setHeader: (c, u) => {
|
|
718
|
+
n[c.toLowerCase()] = u;
|
|
722
719
|
},
|
|
723
720
|
pushSetCookie: (c) => {
|
|
724
|
-
|
|
721
|
+
a.push(c);
|
|
725
722
|
}
|
|
726
723
|
});
|
|
727
|
-
let
|
|
724
|
+
let l;
|
|
728
725
|
try {
|
|
729
|
-
|
|
726
|
+
l = await this.handle(i);
|
|
730
727
|
} catch (c) {
|
|
731
|
-
|
|
728
|
+
l = this.handleError(c);
|
|
732
729
|
}
|
|
733
|
-
for (const [c,
|
|
734
|
-
t.setHeader(c,
|
|
735
|
-
|
|
730
|
+
for (const [c, u] of Object.entries(n))
|
|
731
|
+
t.setHeader(c, u);
|
|
732
|
+
a.length > 0 && t.setHeader("Set-Cookie", a), await this.sendWebResponse(t, l);
|
|
736
733
|
}
|
|
737
734
|
toWebRequest(e) {
|
|
738
735
|
const t = this.toURL(e, !1);
|
|
@@ -753,7 +750,7 @@ class be {
|
|
|
753
750
|
return e;
|
|
754
751
|
let s = 0;
|
|
755
752
|
const r = new J({
|
|
756
|
-
transform(n,
|
|
753
|
+
transform(n, a, i) {
|
|
757
754
|
if (s += Buffer.byteLength(n), s > t) {
|
|
758
755
|
i(new C());
|
|
759
756
|
return;
|
|
@@ -832,8 +829,8 @@ class be {
|
|
|
832
829
|
return console.error("Unhandled error:", e), new Response("Internal Server Error", { status: 500 });
|
|
833
830
|
}
|
|
834
831
|
async sendWebResponse(e, t) {
|
|
835
|
-
if (e.statusCode = t.status, t.headers.forEach((n,
|
|
836
|
-
e.setHeader(
|
|
832
|
+
if (e.statusCode = t.status, t.headers.forEach((n, a) => {
|
|
833
|
+
e.setHeader(a, n);
|
|
837
834
|
}), e.hasHeader("Server") || e.setHeader("Server", "WebHTTPServer"), !t.body || this.shouldOmitResponseBody(t, e.req?.method)) {
|
|
838
835
|
e.end();
|
|
839
836
|
return;
|
|
@@ -841,9 +838,9 @@ class be {
|
|
|
841
838
|
const s = t.body.getReader(), r = V.toWeb(e).getWriter();
|
|
842
839
|
try {
|
|
843
840
|
for (; ; ) {
|
|
844
|
-
const { done: n, value:
|
|
841
|
+
const { done: n, value: a } = await s.read();
|
|
845
842
|
if (n) break;
|
|
846
|
-
await r.write(
|
|
843
|
+
await r.write(a);
|
|
847
844
|
}
|
|
848
845
|
} finally {
|
|
849
846
|
await r.close().catch(() => {
|
|
@@ -860,35 +857,35 @@ class be {
|
|
|
860
857
|
return s ? this.matchesValue(t, s) : !0;
|
|
861
858
|
}
|
|
862
859
|
toRequestEvent(e, t, s) {
|
|
863
|
-
const r = new pe(e, s.pushSetCookie), n = this.config,
|
|
860
|
+
const r = new pe(e, s.pushSetCookie), n = this.config, a = {}, i = { name: "WebHTTPServer" }, l = /* @__PURE__ */ new Set(), c = {
|
|
864
861
|
request: e,
|
|
865
862
|
url: t,
|
|
866
863
|
cookies: r,
|
|
867
864
|
getClientAddress: s.getClientAddress,
|
|
868
865
|
get locals() {
|
|
869
|
-
return
|
|
866
|
+
return a;
|
|
870
867
|
},
|
|
871
868
|
get platform() {
|
|
872
869
|
return i;
|
|
873
870
|
},
|
|
874
871
|
params: {},
|
|
875
872
|
route: { id: "" },
|
|
876
|
-
setHeaders: (
|
|
877
|
-
for (const [d, h] of Object.entries(
|
|
873
|
+
setHeaders: (u) => {
|
|
874
|
+
for (const [d, h] of Object.entries(u)) {
|
|
878
875
|
const f = d.toLowerCase();
|
|
879
876
|
if (f === "set-cookie")
|
|
880
877
|
throw new TypeError("Use event.cookies for Set-Cookie headers");
|
|
881
|
-
if (
|
|
878
|
+
if (l.has(f))
|
|
882
879
|
throw new TypeError(`Header "${d}" has already been set`);
|
|
883
|
-
|
|
880
|
+
l.add(f), s.setHeader(d, h);
|
|
884
881
|
}
|
|
885
882
|
}
|
|
886
883
|
};
|
|
887
|
-
return n.locals && Object.assign(
|
|
884
|
+
return n.locals && Object.assign(a, n.locals(c)), n.platform && Object.assign(i, n.platform(c)), c;
|
|
888
885
|
}
|
|
889
886
|
}
|
|
890
|
-
const Ce = async (
|
|
891
|
-
const t = JSON.stringify(await
|
|
887
|
+
const Ce = async (o, e) => {
|
|
888
|
+
const t = JSON.stringify(await o);
|
|
892
889
|
return new Response(t, {
|
|
893
890
|
...e,
|
|
894
891
|
headers: {
|
|
@@ -897,8 +894,8 @@ const Ce = async (a, e) => {
|
|
|
897
894
|
...e?.headers
|
|
898
895
|
}
|
|
899
896
|
});
|
|
900
|
-
}, We = async (
|
|
901
|
-
const t = await
|
|
897
|
+
}, We = async (o, e) => {
|
|
898
|
+
const t = await o;
|
|
902
899
|
return new Response(t, {
|
|
903
900
|
...e,
|
|
904
901
|
headers: {
|
|
@@ -907,8 +904,8 @@ const Ce = async (a, e) => {
|
|
|
907
904
|
...e?.headers
|
|
908
905
|
}
|
|
909
906
|
});
|
|
910
|
-
}, Le = async (
|
|
911
|
-
const t = await
|
|
907
|
+
}, Le = async (o, e) => {
|
|
908
|
+
const t = await o;
|
|
912
909
|
return new Response(t, {
|
|
913
910
|
...e,
|
|
914
911
|
headers: {
|
|
@@ -917,7 +914,7 @@ const Ce = async (a, e) => {
|
|
|
917
914
|
...e?.headers
|
|
918
915
|
}
|
|
919
916
|
});
|
|
920
|
-
}, Oe = (
|
|
917
|
+
}, Oe = (o, e = {}) => (t) => ee(o, t, e);
|
|
921
918
|
export {
|
|
922
919
|
R as Action,
|
|
923
920
|
Te as CORS,
|