@sourceregistry/node-webserver 1.7.2 → 1.7.4
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/enhance.d.ts +5 -1
- package/dist/index.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +75 -75
- 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/examples/enhanced.ts +4 -0
- package/package.json +1 -1
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";
|
|
@@ -88,10 +88,10 @@ var E = class {
|
|
|
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 k(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 ee(e) {
|
|
|
187
187
|
}
|
|
188
188
|
//#endregion
|
|
189
189
|
//#region src/middlewares/cros/index.ts
|
|
190
|
-
var te = /* @__PURE__ */ S({ policy: () =>
|
|
190
|
+
var te = /* @__PURE__ */ S({ policy: () => M }), ne = [
|
|
191
191
|
"GET",
|
|
192
192
|
"POST",
|
|
193
193
|
"PUT",
|
|
@@ -201,7 +201,7 @@ var te = /* @__PURE__ */ S({ policy: () => N }), ne = [
|
|
|
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",
|
|
@@ -211,21 +211,21 @@ var te = /* @__PURE__ */ S({ policy: () => N }), ne = [
|
|
|
211
211
|
"X-Real-IP",
|
|
212
212
|
"X-Custom-Header"
|
|
213
213
|
];
|
|
214
|
-
function
|
|
215
|
-
return !e || !t ? !1 : t === "*" ? !0 : t === "null" ? e === "null" : Array.isArray(t) ? t.some((t) =>
|
|
214
|
+
function A(e, t) {
|
|
215
|
+
return !e || !t ? !1 : t === "*" ? !0 : t === "null" ? e === "null" : Array.isArray(t) ? t.some((t) => A(e, t)) : typeof t == "function" ? t(e) : t instanceof RegExp ? t.test(e) : e === t;
|
|
216
216
|
}
|
|
217
|
-
function
|
|
217
|
+
function j(e, t) {
|
|
218
218
|
let { origin: n = "*" } = t;
|
|
219
|
-
return e ? n === "*" ? t.credentials ? e : "*" :
|
|
219
|
+
return e ? n === "*" ? t.credentials ? e : "*" : A(e, n) ? e : null : n === "*" ? "*" : null;
|
|
220
220
|
}
|
|
221
|
-
function
|
|
222
|
-
let { methods: t = ne, allowedHeaders: n =
|
|
221
|
+
function M(e = {}) {
|
|
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]
|
|
226
226
|
];
|
|
227
227
|
return r && l.push(["Access-Control-Expose-Headers", r.join(",")]), i && l.push(["Access-Control-Allow-Credentials", "true"]), a && l.push(["Access-Control-Max-Age", a.toString()]), async (t, n) => {
|
|
228
|
-
let r = t.request, i = r.headers.get("Origin"), a = r.method === "OPTIONS" && i !== null && r.headers.has("Access-Control-Request-Method"), s =
|
|
228
|
+
let r = t.request, i = r.headers.get("Origin"), a = r.method === "OPTIONS" && i !== null && r.headers.has("Access-Control-Request-Method"), s = j(i, e);
|
|
229
229
|
if (a) {
|
|
230
230
|
if (!s) return new Response(null, { status: 403 });
|
|
231
231
|
let e = new Response(null, { status: 204 });
|
|
@@ -248,7 +248,7 @@ function N(e = {}) {
|
|
|
248
248
|
}
|
|
249
249
|
//#endregion
|
|
250
250
|
//#region src/middlewares/security/index.ts
|
|
251
|
-
var
|
|
251
|
+
var N = /* @__PURE__ */ S({ headers: () => F }), P = {
|
|
252
252
|
contentSecurityPolicy: "default-src 'self'; base-uri 'self'; frame-ancestors 'none'; object-src 'none'",
|
|
253
253
|
frameOptions: "DENY",
|
|
254
254
|
referrerPolicy: "no-referrer",
|
|
@@ -257,36 +257,36 @@ var P = /* @__PURE__ */ S({ headers: () => I }), F = {
|
|
|
257
257
|
crossOriginResourcePolicy: "same-origin",
|
|
258
258
|
strictTransportSecurity: !1
|
|
259
259
|
};
|
|
260
|
-
function
|
|
260
|
+
function F(e = {}) {
|
|
261
261
|
let t = {
|
|
262
|
-
...
|
|
262
|
+
...P,
|
|
263
263
|
...e
|
|
264
264
|
};
|
|
265
265
|
return async (e, n) => {
|
|
266
266
|
let r = await n();
|
|
267
267
|
if (!r) return;
|
|
268
268
|
let i = new Headers(r.headers);
|
|
269
|
-
return
|
|
269
|
+
return I(i, "content-security-policy", t.contentSecurityPolicy), I(i, "x-frame-options", t.frameOptions), I(i, "referrer-policy", t.referrerPolicy), I(i, "permissions-policy", t.permissionsPolicy), I(i, "cross-origin-opener-policy", t.crossOriginOpenerPolicy), I(i, "cross-origin-resource-policy", t.crossOriginResourcePolicy), I(i, "strict-transport-security", t.strictTransportSecurity), new Response(r.body, {
|
|
270
270
|
status: r.status,
|
|
271
271
|
statusText: r.statusText,
|
|
272
272
|
headers: i
|
|
273
273
|
});
|
|
274
274
|
};
|
|
275
275
|
}
|
|
276
|
-
function
|
|
276
|
+
function I(e, t, n) {
|
|
277
277
|
!n || e.has(t) || e.set(t, n);
|
|
278
278
|
}
|
|
279
279
|
//#endregion
|
|
280
280
|
//#region src/middlewares/requestid/index.ts
|
|
281
|
-
var
|
|
282
|
-
function
|
|
281
|
+
var L = /* @__PURE__ */ S({ assign: () => R });
|
|
282
|
+
function R(t = {}) {
|
|
283
283
|
let n = t.headerName?.toLowerCase() ?? "x-request-id", r = t.generate ?? e, i = t.clientRequestId ?? !1;
|
|
284
284
|
return async (e, t) => {
|
|
285
285
|
let a = e.request.headers.get(n) ?? r();
|
|
286
286
|
if (i) {
|
|
287
287
|
let t = e.request.headers.get("x-client-request-id");
|
|
288
288
|
if (t !== null) {
|
|
289
|
-
if (!
|
|
289
|
+
if (!z(t) || t.length > 512) return new Response("Invalid X-Client-Request-Id header", { status: 400 });
|
|
290
290
|
a = t;
|
|
291
291
|
}
|
|
292
292
|
}
|
|
@@ -302,13 +302,13 @@ function z(t = {}) {
|
|
|
302
302
|
});
|
|
303
303
|
};
|
|
304
304
|
}
|
|
305
|
-
function
|
|
305
|
+
function z(e) {
|
|
306
306
|
return /^[\x00-\x7F]*$/.test(e);
|
|
307
307
|
}
|
|
308
308
|
//#endregion
|
|
309
309
|
//#region src/middlewares/timeout/index.ts
|
|
310
|
-
var
|
|
311
|
-
function
|
|
310
|
+
var B = /* @__PURE__ */ S({ deadline: () => V });
|
|
311
|
+
function V(e) {
|
|
312
312
|
let { ms: t, status: n = 504, body: r = "Gateway Timeout", onTimeout: i } = e;
|
|
313
313
|
if (!Number.isFinite(t) || t <= 0) throw TypeError("Timeout.deadline requires a positive ms value");
|
|
314
314
|
return async (e, a) => {
|
|
@@ -333,7 +333,7 @@ function H(e) {
|
|
|
333
333
|
}
|
|
334
334
|
//#endregion
|
|
335
335
|
//#region src/types/RequestMethod.ts
|
|
336
|
-
var
|
|
336
|
+
var H = [
|
|
337
337
|
"GET",
|
|
338
338
|
"PUT",
|
|
339
339
|
"POST",
|
|
@@ -341,7 +341,29 @@ var U = [
|
|
|
341
341
|
"PATCH",
|
|
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
|
}
|
|
@@ -383,7 +405,7 @@ var U = [
|
|
|
383
405
|
return this.addHandler("OPTIONS", e, t, n);
|
|
384
406
|
}
|
|
385
407
|
USE(e, t, ...n) {
|
|
386
|
-
return
|
|
408
|
+
return H.forEach((r) => this.addHandler(r, e, t, n)), this;
|
|
387
409
|
}
|
|
388
410
|
action(e, t, ...n) {
|
|
389
411
|
return this.addHandler("POST", e, async (e) => {
|
|
@@ -719,38 +741,16 @@ var U = [
|
|
|
719
741
|
status: e,
|
|
720
742
|
headers: { "Content-Type": "application/json" }
|
|
721
743
|
})
|
|
722
|
-
}, q = 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
|
-
}, J = class extends Error {
|
|
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
|
-
},
|
|
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 U = [
|
|
|
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 U = [
|
|
|
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,20 +842,20 @@ var U = [
|
|
|
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
|
-
i(new
|
|
858
|
+
i(new q());
|
|
859
859
|
return;
|
|
860
860
|
}
|
|
861
861
|
i(null, e);
|
|
@@ -868,7 +868,7 @@ var U = [
|
|
|
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;
|
|
@@ -962,7 +962,7 @@ var U = [
|
|
|
962
962
|
return Number.isFinite(r) && r <= t;
|
|
963
963
|
}
|
|
964
964
|
handleError(e) {
|
|
965
|
-
if (e instanceof
|
|
965
|
+
if (e instanceof q) return new Response(e.message, { status: e.status });
|
|
966
966
|
if (C(e)) return new Response(JSON.stringify({
|
|
967
967
|
error: e.statusText || "Error",
|
|
968
968
|
status: e.status
|
|
@@ -986,12 +986,12 @@ var U = [
|
|
|
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 U = [
|
|
|
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 U = [
|
|
|
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 = {
|
|
@@ -1088,7 +1088,7 @@ var U = [
|
|
|
1088
1088
|
}, l = this.createEventFetch(c, n);
|
|
1089
1089
|
return i.locals && Object.assign(a, i.locals(c)), i.platform && Object.assign(o, i.platform(c)), c;
|
|
1090
1090
|
}
|
|
1091
|
-
},
|
|
1091
|
+
}, Y = {
|
|
1092
1092
|
".avif": "image/avif",
|
|
1093
1093
|
".css": "text/css; charset=utf-8",
|
|
1094
1094
|
".gif": "image/gif",
|
|
@@ -1183,7 +1183,7 @@ function Q(e, t) {
|
|
|
1183
1183
|
return n === "" || !n.startsWith("..") && !g(n);
|
|
1184
1184
|
}
|
|
1185
1185
|
function le(e) {
|
|
1186
|
-
return
|
|
1186
|
+
return Y[h(e).toLowerCase()] ?? "application/octet-stream";
|
|
1187
1187
|
}
|
|
1188
1188
|
function ue(e) {
|
|
1189
1189
|
return typeof e.params.path == "string" ? e.params.path : e.url.pathname.replace(/^\/+/, "");
|
|
@@ -1305,6 +1305,6 @@ var ve = async (e, t) => {
|
|
|
1305
1305
|
});
|
|
1306
1306
|
};
|
|
1307
1307
|
//#endregion
|
|
1308
|
-
export { K as Action, te as CORS, O as RateLimiter,
|
|
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 };
|
|
1309
1309
|
|
|
1310
1310
|
//# sourceMappingURL=index.es.js.map
|