@sourceregistry/node-webserver 1.7.1 → 1.7.3
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/dist/helpers/sse.d.ts +9 -1
- package/dist/index.cjs.js +4 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +110 -105
- package/dist/index.es.js.map +1 -1
- package/dist/types/Cookies.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -12,3 +12,4 @@ export declare const text: (body: MaybePromise<string>, init?: ResponseInit) =>
|
|
|
12
12
|
export declare const html: (html: MaybePromise<string>, init?: ResponseInit) => Promise<Response>;
|
|
13
13
|
export { default as dir } from './helpers/static';
|
|
14
14
|
export { default as sse } from './helpers/sse';
|
|
15
|
+
export type { SSEOptions, SSEHandler, SSEEmit, SSEEmitOptions } from './helpers/sse';
|
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { randomUUID as e } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
import { createServer as
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
2
|
+
import { parse as t, serialize as n } from "cookie";
|
|
3
|
+
import { createServer as r } from "http";
|
|
4
|
+
import { createServer as i } from "https";
|
|
5
|
+
import { TLSSocket as a } from "tls";
|
|
6
|
+
import { Readable as o, Transform as s, Writable as c } from "stream";
|
|
7
|
+
import { WebSocket as l, WebSocketServer as u } from "ws";
|
|
8
8
|
import { createReadStream as d } from "node:fs";
|
|
9
9
|
import { lstat as f, realpath as p, stat as m } from "node:fs/promises";
|
|
10
10
|
import { extname as h, isAbsolute as g, relative as _, resolve as v, sep as y } from "node:path";
|
|
@@ -85,13 +85,13 @@ var E = class {
|
|
|
85
85
|
async resetAll() {
|
|
86
86
|
this.data.clear();
|
|
87
87
|
}
|
|
88
|
-
},
|
|
88
|
+
}, O = /* @__PURE__ */ S({
|
|
89
89
|
MemoryStore: () => E,
|
|
90
90
|
SlidingWindowStore: () => D,
|
|
91
|
-
fixedWindowLimit: () =>
|
|
92
|
-
slidingWindowLimit: () =>
|
|
91
|
+
fixedWindowLimit: () => k,
|
|
92
|
+
slidingWindowLimit: () => ee
|
|
93
93
|
});
|
|
94
|
-
function
|
|
94
|
+
function ee(e) {
|
|
95
95
|
let { windowMs: t = 6e4, max: n, key: r = (e) => e.getClientAddress(), message: i = "Too many requests, please try again later.", statusCode: a = 429, headers: o = "include", onRateLimit: s, store: c = new D({ windowMs: t }) } = e;
|
|
96
96
|
return async (e, t) => {
|
|
97
97
|
let l = r(e);
|
|
@@ -138,7 +138,7 @@ function O(e) {
|
|
|
138
138
|
return t();
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
|
-
function
|
|
141
|
+
function k(e) {
|
|
142
142
|
let { windowMs: t = 6e4, max: n, key: r = (e) => e.getClientAddress(), message: i = "Too many requests, please try again later.", statusCode: a = 429, headers: o = "include", onRateLimit: s, store: c = new E({ windowMs: t }) } = e;
|
|
143
143
|
return async (e, t) => {
|
|
144
144
|
let l = r(e);
|
|
@@ -187,7 +187,7 @@ function te(e) {
|
|
|
187
187
|
}
|
|
188
188
|
//#endregion
|
|
189
189
|
//#region src/middlewares/cros/index.ts
|
|
190
|
-
var
|
|
190
|
+
var te = /* @__PURE__ */ S({ policy: () => M }), ne = [
|
|
191
191
|
"GET",
|
|
192
192
|
"POST",
|
|
193
193
|
"PUT",
|
|
@@ -195,13 +195,13 @@ var ne = /* @__PURE__ */ S({ policy: () => M }), re = [
|
|
|
195
195
|
"PATCH",
|
|
196
196
|
"HEAD",
|
|
197
197
|
"OPTIONS"
|
|
198
|
-
],
|
|
198
|
+
], re = [
|
|
199
199
|
"Accept",
|
|
200
200
|
"Accept-Language",
|
|
201
201
|
"Content-Language",
|
|
202
202
|
"Content-Type",
|
|
203
203
|
"Range"
|
|
204
|
-
],
|
|
204
|
+
], ie = [
|
|
205
205
|
"Authorization",
|
|
206
206
|
"X-Auth-Token",
|
|
207
207
|
"X-Requested-With",
|
|
@@ -219,7 +219,7 @@ function j(e, t) {
|
|
|
219
219
|
return e ? n === "*" ? t.credentials ? e : "*" : A(e, n) ? e : null : n === "*" ? "*" : null;
|
|
220
220
|
}
|
|
221
221
|
function M(e = {}) {
|
|
222
|
-
let { methods: t =
|
|
222
|
+
let { methods: t = ne, allowedHeaders: n = ie, exposedHeaders: r, credentials: i = !1, maxAge: a = 86400, onResponse: o } = e, s = t.join(","), c = [...re, ...n].join(","), l = [
|
|
223
223
|
["Vary", "Origin,Access-Control-Request-Method,Access-Control-Request-Headers"],
|
|
224
224
|
["Access-Control-Allow-Methods", s],
|
|
225
225
|
["Access-Control-Allow-Headers", c]
|
|
@@ -342,6 +342,28 @@ var H = [
|
|
|
342
342
|
"HEAD",
|
|
343
343
|
"OPTIONS"
|
|
344
344
|
], U = class {
|
|
345
|
+
constructor(e, t) {
|
|
346
|
+
this.raw = e.headers.get("cookie") ?? "", this.setCookieHeader = t;
|
|
347
|
+
}
|
|
348
|
+
get(e, n) {
|
|
349
|
+
return t(this.raw, n)[e];
|
|
350
|
+
}
|
|
351
|
+
getAll(e) {
|
|
352
|
+
return Object.entries(t(this.raw, e)).filter(([, e]) => e !== void 0).map(([e, t]) => ({
|
|
353
|
+
name: e,
|
|
354
|
+
value: t
|
|
355
|
+
}));
|
|
356
|
+
}
|
|
357
|
+
set(e, t, r) {
|
|
358
|
+
this.setCookieHeader(n(e, t, r));
|
|
359
|
+
}
|
|
360
|
+
delete(e, t) {
|
|
361
|
+
this.set(e, "", {
|
|
362
|
+
...t,
|
|
363
|
+
maxAge: 0
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}, W = class {
|
|
345
367
|
static {
|
|
346
368
|
this.cache = /* @__PURE__ */ new Map();
|
|
347
369
|
}
|
|
@@ -351,7 +373,7 @@ var H = [
|
|
|
351
373
|
static set(e, t) {
|
|
352
374
|
this.cache.set(e, t);
|
|
353
375
|
}
|
|
354
|
-
},
|
|
376
|
+
}, G = class e {
|
|
355
377
|
constructor() {
|
|
356
378
|
this._routes = [], this._wsRoutes = [], this._nestedRouters = [], this._middlewares = [], this._preHandlers = [], this._postHandlers = [], this.routesSorted = !1, this.wsRoutesSorted = !1;
|
|
357
379
|
}
|
|
@@ -545,7 +567,7 @@ var H = [
|
|
|
545
567
|
}), this.routesSorted = !1, this;
|
|
546
568
|
}
|
|
547
569
|
createPathRegex(e) {
|
|
548
|
-
let t =
|
|
570
|
+
let t = W.get(e);
|
|
549
571
|
if (t) return t;
|
|
550
572
|
let n = [], r = !1, i, a = e.split("/").filter(Boolean);
|
|
551
573
|
i = a.reduce((e, t) => t.startsWith("[...") ? e - 10 : t.startsWith("[[") ? e - 5 : t.startsWith("[") ? e - 1 : e + 1, 0);
|
|
@@ -568,7 +590,7 @@ var H = [
|
|
|
568
590
|
isCatchAll: r,
|
|
569
591
|
priority: i
|
|
570
592
|
};
|
|
571
|
-
return
|
|
593
|
+
return W.set(e, s), s;
|
|
572
594
|
}
|
|
573
595
|
createPrefixRegex(e) {
|
|
574
596
|
let t = [], n = !1, r, i = e.split("/").filter(Boolean);
|
|
@@ -676,17 +698,17 @@ var H = [
|
|
|
676
698
|
this._wsRoutes.sort((e, t) => t.priority - e.priority), this.wsRoutesSorted = !0;
|
|
677
699
|
}
|
|
678
700
|
formatActionResult(e) {
|
|
679
|
-
return e instanceof Response ? e : e?.type === "failure" && "status" in e ?
|
|
701
|
+
return e instanceof Response ? e : e?.type === "failure" && "status" in e ? K.fail(e.status, e.data) : K.success(200, e ?? void 0);
|
|
680
702
|
}
|
|
681
703
|
handleActionError(e) {
|
|
682
|
-
if (C(e)) return
|
|
704
|
+
if (C(e)) return K.error(e.status, { message: e.statusText || "Error" });
|
|
683
705
|
if (w(e)) {
|
|
684
706
|
let t = e.headers.get("Location") || "/";
|
|
685
|
-
return
|
|
707
|
+
return K.redirect(e.status, t);
|
|
686
708
|
}
|
|
687
|
-
return console.error(e),
|
|
709
|
+
return console.error(e), K.error(500, { message: "Internal Server Error" });
|
|
688
710
|
}
|
|
689
|
-
},
|
|
711
|
+
}, K = {
|
|
690
712
|
success: (e = 200, t) => new Response(JSON.stringify({
|
|
691
713
|
data: t,
|
|
692
714
|
type: "success",
|
|
@@ -719,38 +741,16 @@ var H = [
|
|
|
719
741
|
status: e,
|
|
720
742
|
headers: { "Content-Type": "application/json" }
|
|
721
743
|
})
|
|
722
|
-
}, K = class {
|
|
723
|
-
constructor(e, t) {
|
|
724
|
-
this.raw = e.headers.get("cookie") ?? "", this.setCookieHeader = t;
|
|
725
|
-
}
|
|
726
|
-
get(e, t) {
|
|
727
|
-
return l(this.raw, t)[e];
|
|
728
|
-
}
|
|
729
|
-
getAll(e) {
|
|
730
|
-
return Object.entries(l(this.raw, e)).filter(([, e]) => e !== void 0).map(([e, t]) => ({
|
|
731
|
-
name: e,
|
|
732
|
-
value: t
|
|
733
|
-
}));
|
|
734
|
-
}
|
|
735
|
-
set(e, t, n) {
|
|
736
|
-
this.setCookieHeader(u(e, t, n));
|
|
737
|
-
}
|
|
738
|
-
delete(e, t) {
|
|
739
|
-
this.set(e, "", {
|
|
740
|
-
...t,
|
|
741
|
-
maxAge: 0
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
744
|
}, q = class extends Error {
|
|
745
745
|
constructor(e = "Payload Too Large") {
|
|
746
746
|
super(e), this.status = 413, this.name = "PayloadTooLargeError";
|
|
747
747
|
}
|
|
748
|
-
}, J = class extends
|
|
748
|
+
}, J = class extends G {
|
|
749
749
|
constructor(e) {
|
|
750
750
|
super(), this.upgradeHandlerInstalled = !1, this.config = e ?? {
|
|
751
751
|
type: "http",
|
|
752
752
|
options: {}
|
|
753
|
-
}, this.wss = new
|
|
753
|
+
}, this.wss = new u({
|
|
754
754
|
noServer: !0,
|
|
755
755
|
maxPayload: this.config.security?.maxWebSocketPayload ?? 1024 * 1024
|
|
756
756
|
});
|
|
@@ -762,7 +762,7 @@ var H = [
|
|
|
762
762
|
console.error("Unhandled request error:", e), t.statusCode = 500, t.end("Internal Server Error");
|
|
763
763
|
});
|
|
764
764
|
};
|
|
765
|
-
this._server = this.config.type === "https" ?
|
|
765
|
+
this._server = this.config.type === "https" ? i(this.config.options, e) : r(this.config.options, e), this.configureServerTimeouts(this._server);
|
|
766
766
|
}
|
|
767
767
|
return this._server;
|
|
768
768
|
}
|
|
@@ -794,9 +794,9 @@ var H = [
|
|
|
794
794
|
}
|
|
795
795
|
this.wss.handleUpgrade(e, t, n, (e) => {
|
|
796
796
|
this.handleWebSocket(a, e).then((t) => {
|
|
797
|
-
!t && e.readyState ===
|
|
797
|
+
!t && e.readyState === l.OPEN && e.close(1008, "Route not found");
|
|
798
798
|
}).catch((t) => {
|
|
799
|
-
console.error("WebSocket routing error:", t), e.readyState ===
|
|
799
|
+
console.error("WebSocket routing error:", t), e.readyState === l.OPEN && e.close(1011, "Internal error");
|
|
800
800
|
});
|
|
801
801
|
});
|
|
802
802
|
}).catch(() => t.destroy());
|
|
@@ -842,18 +842,18 @@ var H = [
|
|
|
842
842
|
return this.toRequest(e, n, !1, t);
|
|
843
843
|
}
|
|
844
844
|
toRequest(e, t, n, r) {
|
|
845
|
-
let
|
|
845
|
+
let i = {
|
|
846
846
|
method: n ? "GET" : e.method,
|
|
847
847
|
headers: this.toHeaders(e.headers),
|
|
848
848
|
signal: r,
|
|
849
849
|
duplex: "half"
|
|
850
850
|
};
|
|
851
|
-
return !n && e.method !== "GET" && e.method !== "HEAD" && (
|
|
851
|
+
return !n && e.method !== "GET" && e.method !== "HEAD" && (i.body = o.toWeb(this.wrapRequestBody(e))), new Request(t, i);
|
|
852
852
|
}
|
|
853
853
|
wrapRequestBody(e) {
|
|
854
854
|
let t = this.config.security?.maxRequestBodySize;
|
|
855
855
|
if (!t) return e;
|
|
856
|
-
let n = 0, r = new
|
|
856
|
+
let n = 0, r = new s({ transform(e, r, i) {
|
|
857
857
|
if (n += Buffer.byteLength(e), n > t) {
|
|
858
858
|
i(new q());
|
|
859
859
|
return;
|
|
@@ -868,7 +868,7 @@ var H = [
|
|
|
868
868
|
}
|
|
869
869
|
resolveProtocol(e, t) {
|
|
870
870
|
let n = this.getTrustedForwardedHeader(e, "x-forwarded-proto")?.split(",")[0]?.trim().toLowerCase();
|
|
871
|
-
return n === "http" || n === "https" ? t ? n === "https" ? "wss" : "ws" : n : e.socket instanceof
|
|
871
|
+
return n === "http" || n === "https" ? t ? n === "https" ? "wss" : "ws" : n : e.socket instanceof a ? t ? "wss" : "https" : t ? "ws" : "http";
|
|
872
872
|
}
|
|
873
873
|
resolveAuthority(e) {
|
|
874
874
|
let t = this.getTrustedForwardedHeader(e, "x-forwarded-host") ?? e.headers.host, n = this.config.security?.trustHostHeader ? this.normalizeTrustedHost(t) : null;
|
|
@@ -986,12 +986,12 @@ var H = [
|
|
|
986
986
|
e.end();
|
|
987
987
|
return;
|
|
988
988
|
}
|
|
989
|
-
let n = t.body.getReader(), r =
|
|
989
|
+
let n = t.body.getReader(), r = c.toWeb(e).getWriter(), i = !1, a = async () => {
|
|
990
990
|
i || (i = !0, await n.cancel().catch(() => {}));
|
|
991
|
-
},
|
|
991
|
+
}, o = () => {
|
|
992
992
|
a();
|
|
993
993
|
};
|
|
994
|
-
e.once("close",
|
|
994
|
+
e.once("close", o);
|
|
995
995
|
try {
|
|
996
996
|
for (;;) {
|
|
997
997
|
let { done: e, value: t } = await n.read();
|
|
@@ -1002,7 +1002,7 @@ var H = [
|
|
|
1002
1002
|
} catch (e) {
|
|
1003
1003
|
if (await a(), !this.isPrematureCloseError(e)) throw e;
|
|
1004
1004
|
} finally {
|
|
1005
|
-
e.off("close",
|
|
1005
|
+
e.off("close", o), await r.close().catch(() => {});
|
|
1006
1006
|
}
|
|
1007
1007
|
}
|
|
1008
1008
|
isPrematureCloseError(e) {
|
|
@@ -1060,7 +1060,7 @@ var H = [
|
|
|
1060
1060
|
r && t.set(n, r);
|
|
1061
1061
|
}
|
|
1062
1062
|
toRequestEvent(e, t, n) {
|
|
1063
|
-
let r = new
|
|
1063
|
+
let r = new U(e, n.pushSetCookie), i = this.config, a = {}, o = {
|
|
1064
1064
|
name: "node-webserver",
|
|
1065
1065
|
dev: this.isDevelopment()
|
|
1066
1066
|
}, s = /* @__PURE__ */ new Set(), c = {
|
|
@@ -1211,51 +1211,56 @@ function pe(e, t = {}) {
|
|
|
1211
1211
|
}
|
|
1212
1212
|
return `${n.join("\n")}\n\n`;
|
|
1213
1213
|
}
|
|
1214
|
-
var $ = (e, t = {}) =>
|
|
1215
|
-
let
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
} finally {
|
|
1214
|
+
var $ = 15e3, me = (e, t = {}) => {
|
|
1215
|
+
let { keepaliveInterval: n = $, ...r } = t;
|
|
1216
|
+
return (t) => {
|
|
1217
|
+
let i = new TextEncoder(), a = null, o = !1, s, c = null, l = async () => {
|
|
1218
|
+
if (!o) {
|
|
1219
|
+
o = !0, c !== null && (clearInterval(c), c = null);
|
|
1221
1220
|
try {
|
|
1222
|
-
|
|
1223
|
-
}
|
|
1221
|
+
await s?.();
|
|
1222
|
+
} finally {
|
|
1223
|
+
try {
|
|
1224
|
+
a?.close();
|
|
1225
|
+
} catch {}
|
|
1226
|
+
}
|
|
1224
1227
|
}
|
|
1225
|
-
}
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
await
|
|
1238
|
-
|
|
1228
|
+
}, u = new ReadableStream({
|
|
1229
|
+
async start(r) {
|
|
1230
|
+
a = r;
|
|
1231
|
+
let u = (e, t = {}) => {
|
|
1232
|
+
o || r.enqueue(i.encode(pe(e, t)));
|
|
1233
|
+
};
|
|
1234
|
+
t.request.signal.addEventListener("abort", () => {
|
|
1235
|
+
l();
|
|
1236
|
+
}, { once: !0 }), n > 0 && (c = setInterval(() => {
|
|
1237
|
+
o || r.enqueue(i.encode(": keep-alive\n\n"));
|
|
1238
|
+
}, n), c?.unref());
|
|
1239
|
+
try {
|
|
1240
|
+
if (s = await e(t, u), t.request.signal.aborted) {
|
|
1241
|
+
await l();
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
s === void 0 && await l();
|
|
1245
|
+
} catch (e) {
|
|
1246
|
+
o || r.error(e);
|
|
1239
1247
|
}
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1248
|
+
},
|
|
1249
|
+
async cancel() {
|
|
1250
|
+
await l();
|
|
1243
1251
|
}
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
}
|
|
1257
|
-
});
|
|
1258
|
-
}, me = async (e, t) => {
|
|
1252
|
+
});
|
|
1253
|
+
return new Response(u, {
|
|
1254
|
+
...r,
|
|
1255
|
+
headers: {
|
|
1256
|
+
"content-type": "text/event-stream",
|
|
1257
|
+
"cache-control": "no-cache",
|
|
1258
|
+
connection: "keep-alive",
|
|
1259
|
+
...r.headers
|
|
1260
|
+
}
|
|
1261
|
+
});
|
|
1262
|
+
};
|
|
1263
|
+
}, he = async (e, t) => {
|
|
1259
1264
|
let n = JSON.stringify(await e);
|
|
1260
1265
|
return new Response(n, {
|
|
1261
1266
|
...t,
|
|
@@ -1266,19 +1271,19 @@ var $ = (e, t = {}) => (n) => {
|
|
|
1266
1271
|
}
|
|
1267
1272
|
});
|
|
1268
1273
|
};
|
|
1269
|
-
function
|
|
1274
|
+
function ge(e, t) {
|
|
1270
1275
|
throw new Response(null, {
|
|
1271
1276
|
status: e,
|
|
1272
1277
|
headers: { location: t.toString() }
|
|
1273
1278
|
});
|
|
1274
1279
|
}
|
|
1275
|
-
function
|
|
1280
|
+
function _e(e, t) {
|
|
1276
1281
|
throw new Response(JSON.stringify(typeof t == "string" ? { message: t } : t), {
|
|
1277
1282
|
status: e,
|
|
1278
1283
|
headers: { "content-type": "application/json" }
|
|
1279
1284
|
});
|
|
1280
1285
|
}
|
|
1281
|
-
var
|
|
1286
|
+
var ve = async (e, t) => {
|
|
1282
1287
|
let n = await e;
|
|
1283
1288
|
return new Response(n, {
|
|
1284
1289
|
...t,
|
|
@@ -1288,7 +1293,7 @@ var _e = async (e, t) => {
|
|
|
1288
1293
|
...t?.headers
|
|
1289
1294
|
}
|
|
1290
1295
|
});
|
|
1291
|
-
},
|
|
1296
|
+
}, ye = async (e, t) => {
|
|
1292
1297
|
let n = await e;
|
|
1293
1298
|
return new Response(n, {
|
|
1294
1299
|
...t,
|
|
@@ -1300,6 +1305,6 @@ var _e = async (e, t) => {
|
|
|
1300
1305
|
});
|
|
1301
1306
|
};
|
|
1302
1307
|
//#endregion
|
|
1303
|
-
export {
|
|
1308
|
+
export { K as Action, te as CORS, U as Cookies, O as RateLimiter, L as RequestId, H as RequestMethods, G as Router, N as Security, B as Timeout, J as WebServer, de as dir, fe as enhance, _e as error, ye as html, C as isHttpError, w as isRedirect, T as isResponse, he as json, ge as redirect, X as serveStatic, me as sse, ve as text };
|
|
1304
1309
|
|
|
1305
1310
|
//# sourceMappingURL=index.es.js.map
|