@sourceregistry/node-webserver 1.7.0 → 1.7.2

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/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
@@ -49,7 +49,7 @@ var E = class {
49
49
  this.cleanupInterval = setInterval(() => {
50
50
  let e = Date.now();
51
51
  for (let [t, { reset: n }] of this.data) e >= n && this.data.delete(t);
52
- }, Math.min(this.windowMs, 3e5));
52
+ }, Math.min(this.windowMs, 3e5)), this.cleanupInterval?.unref();
53
53
  }
54
54
  stop() {
55
55
  this.cleanupInterval && clearInterval(this.cleanupInterval);
@@ -77,7 +77,7 @@ var E = class {
77
77
  let r = e - this.windowMs, i = n.filter((e) => e > r);
78
78
  i.length === 0 ? this.data.delete(t) : this.data.set(t, i);
79
79
  }
80
- }, Math.min(this.windowMs, 3e5));
80
+ }, Math.min(this.windowMs, 3e5)), this.cleanupInterval.unref();
81
81
  }
82
82
  stop() {
83
83
  this.cleanupInterval && clearInterval(this.cleanupInterval);
@@ -85,13 +85,13 @@ var E = class {
85
85
  async resetAll() {
86
86
  this.data.clear();
87
87
  }
88
- }, ee = /* @__PURE__ */ S({
88
+ }, O = /* @__PURE__ */ S({
89
89
  MemoryStore: () => E,
90
90
  SlidingWindowStore: () => D,
91
- fixedWindowLimit: () => te,
92
- slidingWindowLimit: () => O
91
+ fixedWindowLimit: () => ee,
92
+ slidingWindowLimit: () => k
93
93
  });
94
- function O(e) {
94
+ function k(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 te(e) {
141
+ function ee(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 ne = /* @__PURE__ */ S({ policy: () => M }), re = [
190
+ var te = /* @__PURE__ */ S({ policy: () => N }), 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
- ], ie = [
198
+ ], re = [
199
199
  "Accept",
200
200
  "Accept-Language",
201
201
  "Content-Language",
202
202
  "Content-Type",
203
203
  "Range"
204
- ], k = [
204
+ ], A = [
205
205
  "Authorization",
206
206
  "X-Auth-Token",
207
207
  "X-Requested-With",
@@ -211,21 +211,21 @@ var ne = /* @__PURE__ */ S({ policy: () => M }), re = [
211
211
  "X-Real-IP",
212
212
  "X-Custom-Header"
213
213
  ];
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
- }
217
214
  function j(e, t) {
215
+ return !e || !t ? !1 : t === "*" ? !0 : t === "null" ? e === "null" : Array.isArray(t) ? t.some((t) => j(e, t)) : typeof t == "function" ? t(e) : t instanceof RegExp ? t.test(e) : e === t;
216
+ }
217
+ function M(e, t) {
218
218
  let { origin: n = "*" } = t;
219
- return e ? n === "*" ? t.credentials ? e : "*" : A(e, n) ? e : null : n === "*" ? "*" : null;
219
+ return e ? n === "*" ? t.credentials ? e : "*" : j(e, n) ? e : null : n === "*" ? "*" : null;
220
220
  }
221
- function M(e = {}) {
222
- let { methods: t = re, allowedHeaders: n = k, exposedHeaders: r, credentials: i = !1, maxAge: a = 86400, onResponse: o } = e, s = t.join(","), c = [...ie, ...n].join(","), l = [
221
+ function N(e = {}) {
222
+ let { methods: t = ne, allowedHeaders: n = A, 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 = j(i, e);
228
+ let r = t.request, i = r.headers.get("Origin"), a = r.method === "OPTIONS" && i !== null && r.headers.has("Access-Control-Request-Method"), s = M(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 M(e = {}) {
248
248
  }
249
249
  //#endregion
250
250
  //#region src/middlewares/security/index.ts
251
- var N = /* @__PURE__ */ S({ headers: () => F }), P = {
251
+ var P = /* @__PURE__ */ S({ headers: () => I }), F = {
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 N = /* @__PURE__ */ S({ headers: () => F }), P = {
257
257
  crossOriginResourcePolicy: "same-origin",
258
258
  strictTransportSecurity: !1
259
259
  };
260
- function F(e = {}) {
260
+ function I(e = {}) {
261
261
  let t = {
262
- ...P,
262
+ ...F,
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 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, {
269
+ return L(i, "content-security-policy", t.contentSecurityPolicy), L(i, "x-frame-options", t.frameOptions), L(i, "referrer-policy", t.referrerPolicy), L(i, "permissions-policy", t.permissionsPolicy), L(i, "cross-origin-opener-policy", t.crossOriginOpenerPolicy), L(i, "cross-origin-resource-policy", t.crossOriginResourcePolicy), L(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 I(e, t, n) {
276
+ function L(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 L = /* @__PURE__ */ S({ assign: () => R });
282
- function R(t = {}) {
281
+ var R = /* @__PURE__ */ S({ assign: () => z });
282
+ function z(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 (!z(t) || t.length > 512) return new Response("Invalid X-Client-Request-Id header", { status: 400 });
289
+ if (!B(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 R(t = {}) {
302
302
  });
303
303
  };
304
304
  }
305
- function z(e) {
305
+ function B(e) {
306
306
  return /^[\x00-\x7F]*$/.test(e);
307
307
  }
308
308
  //#endregion
309
309
  //#region src/middlewares/timeout/index.ts
310
- var B = /* @__PURE__ */ S({ deadline: () => V });
311
- function V(e) {
310
+ var V = /* @__PURE__ */ S({ deadline: () => H });
311
+ function H(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 V(e) {
333
333
  }
334
334
  //#endregion
335
335
  //#region src/types/RequestMethod.ts
336
- var H = [
336
+ var U = [
337
337
  "GET",
338
338
  "PUT",
339
339
  "POST",
@@ -341,7 +341,7 @@ var H = [
341
341
  "PATCH",
342
342
  "HEAD",
343
343
  "OPTIONS"
344
- ], U = class {
344
+ ], W = class {
345
345
  static {
346
346
  this.cache = /* @__PURE__ */ new Map();
347
347
  }
@@ -351,7 +351,7 @@ var H = [
351
351
  static set(e, t) {
352
352
  this.cache.set(e, t);
353
353
  }
354
- }, W = class e {
354
+ }, G = class e {
355
355
  constructor() {
356
356
  this._routes = [], this._wsRoutes = [], this._nestedRouters = [], this._middlewares = [], this._preHandlers = [], this._postHandlers = [], this.routesSorted = !1, this.wsRoutesSorted = !1;
357
357
  }
@@ -383,7 +383,7 @@ var H = [
383
383
  return this.addHandler("OPTIONS", e, t, n);
384
384
  }
385
385
  USE(e, t, ...n) {
386
- return H.forEach((r) => this.addHandler(r, e, t, n)), this;
386
+ return U.forEach((r) => this.addHandler(r, e, t, n)), this;
387
387
  }
388
388
  action(e, t, ...n) {
389
389
  return this.addHandler("POST", e, async (e) => {
@@ -415,6 +415,9 @@ var H = [
415
415
  useMiddleware(...e) {
416
416
  return this._middlewares.push(...e), this;
417
417
  }
418
+ middleware(...e) {
419
+ return this._middlewares.push(...e), this;
420
+ }
418
421
  pre(...e) {
419
422
  return this._preHandlers.push(...e), this;
420
423
  }
@@ -542,7 +545,7 @@ var H = [
542
545
  }), this.routesSorted = !1, this;
543
546
  }
544
547
  createPathRegex(e) {
545
- let t = U.get(e);
548
+ let t = W.get(e);
546
549
  if (t) return t;
547
550
  let n = [], r = !1, i, a = e.split("/").filter(Boolean);
548
551
  i = a.reduce((e, t) => t.startsWith("[...") ? e - 10 : t.startsWith("[[") ? e - 5 : t.startsWith("[") ? e - 1 : e + 1, 0);
@@ -565,7 +568,7 @@ var H = [
565
568
  isCatchAll: r,
566
569
  priority: i
567
570
  };
568
- return U.set(e, s), s;
571
+ return W.set(e, s), s;
569
572
  }
570
573
  createPrefixRegex(e) {
571
574
  let t = [], n = !1, r, i = e.split("/").filter(Boolean);
@@ -673,17 +676,17 @@ var H = [
673
676
  this._wsRoutes.sort((e, t) => t.priority - e.priority), this.wsRoutesSorted = !0;
674
677
  }
675
678
  formatActionResult(e) {
676
- return e instanceof Response ? e : e?.type === "failure" && "status" in e ? G.fail(e.status, e.data) : G.success(200, e ?? void 0);
679
+ return e instanceof Response ? e : e?.type === "failure" && "status" in e ? K.fail(e.status, e.data) : K.success(200, e ?? void 0);
677
680
  }
678
681
  handleActionError(e) {
679
- if (C(e)) return G.error(e.status, { message: e.statusText || "Error" });
682
+ if (C(e)) return K.error(e.status, { message: e.statusText || "Error" });
680
683
  if (w(e)) {
681
684
  let t = e.headers.get("Location") || "/";
682
- return G.redirect(e.status, t);
685
+ return K.redirect(e.status, t);
683
686
  }
684
- return console.error(e), G.error(500, { message: "Internal Server Error" });
687
+ return console.error(e), K.error(500, { message: "Internal Server Error" });
685
688
  }
686
- }, G = {
689
+ }, K = {
687
690
  success: (e = 200, t) => new Response(JSON.stringify({
688
691
  data: t,
689
692
  type: "success",
@@ -716,7 +719,7 @@ var H = [
716
719
  status: e,
717
720
  headers: { "Content-Type": "application/json" }
718
721
  })
719
- }, K = class {
722
+ }, q = class {
720
723
  constructor(e, t) {
721
724
  this.raw = e.headers.get("cookie") ?? "", this.setCookieHeader = t;
722
725
  }
@@ -738,11 +741,11 @@ var H = [
738
741
  maxAge: 0
739
742
  });
740
743
  }
741
- }, q = class extends Error {
744
+ }, J = class extends Error {
742
745
  constructor(e = "Payload Too Large") {
743
746
  super(e), this.status = 413, this.name = "PayloadTooLargeError";
744
747
  }
745
- }, J = class extends W {
748
+ }, Y = class extends G {
746
749
  constructor(e) {
747
750
  super(), this.upgradeHandlerInstalled = !1, this.config = e ?? {
748
751
  type: "http",
@@ -852,7 +855,7 @@ var H = [
852
855
  if (!t) return e;
853
856
  let n = 0, r = new a({ transform(e, r, i) {
854
857
  if (n += Buffer.byteLength(e), n > t) {
855
- i(new q());
858
+ i(new J());
856
859
  return;
857
860
  }
858
861
  i(null, e);
@@ -959,7 +962,7 @@ var H = [
959
962
  return Number.isFinite(r) && r <= t;
960
963
  }
961
964
  handleError(e) {
962
- if (e instanceof q) return new Response(e.message, { status: e.status });
965
+ if (e instanceof J) return new Response(e.message, { status: e.status });
963
966
  if (C(e)) return new Response(JSON.stringify({
964
967
  error: e.statusText || "Error",
965
968
  status: e.status
@@ -1057,7 +1060,7 @@ var H = [
1057
1060
  r && t.set(n, r);
1058
1061
  }
1059
1062
  toRequestEvent(e, t, n) {
1060
- let r = new K(e, n.pushSetCookie), i = this.config, a = {}, o = {
1063
+ let r = new q(e, n.pushSetCookie), i = this.config, a = {}, o = {
1061
1064
  name: "node-webserver",
1062
1065
  dev: this.isDevelopment()
1063
1066
  }, s = /* @__PURE__ */ new Set(), c = {
@@ -1085,7 +1088,7 @@ var H = [
1085
1088
  }, l = this.createEventFetch(c, n);
1086
1089
  return i.locals && Object.assign(a, i.locals(c)), i.platform && Object.assign(o, i.platform(c)), c;
1087
1090
  }
1088
- }, Y = {
1091
+ }, ie = {
1089
1092
  ".avif": "image/avif",
1090
1093
  ".css": "text/css; charset=utf-8",
1091
1094
  ".gif": "image/gif",
@@ -1180,7 +1183,7 @@ function Q(e, t) {
1180
1183
  return n === "" || !n.startsWith("..") && !g(n);
1181
1184
  }
1182
1185
  function le(e) {
1183
- return Y[h(e).toLowerCase()] ?? "application/octet-stream";
1186
+ return ie[h(e).toLowerCase()] ?? "application/octet-stream";
1184
1187
  }
1185
1188
  function ue(e) {
1186
1189
  return typeof e.params.path == "string" ? e.params.path : e.url.pathname.replace(/^\/+/, "");
@@ -1208,51 +1211,56 @@ function pe(e, t = {}) {
1208
1211
  }
1209
1212
  return `${n.join("\n")}\n\n`;
1210
1213
  }
1211
- var $ = (e, t = {}) => (n) => {
1212
- let r = new TextEncoder(), i = null, a = !1, o, s = async () => {
1213
- if (!a) {
1214
- a = !0;
1215
- try {
1216
- await o?.();
1217
- } 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);
1218
1220
  try {
1219
- i?.close();
1220
- } catch {}
1221
+ await s?.();
1222
+ } finally {
1223
+ try {
1224
+ a?.close();
1225
+ } catch {}
1226
+ }
1221
1227
  }
1222
- }
1223
- }, c = new ReadableStream({
1224
- async start(t) {
1225
- i = t;
1226
- let c = (e, n = {}) => {
1227
- a || t.enqueue(r.encode(pe(e, n)));
1228
- };
1229
- n.request.signal.addEventListener("abort", () => {
1230
- s();
1231
- }, { once: !0 });
1232
- try {
1233
- if (o = await e(n, c), n.request.signal.aborted) {
1234
- await s();
1235
- return;
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);
1236
1247
  }
1237
- o === void 0 && await s();
1238
- } catch (e) {
1239
- a || t.error(e);
1248
+ },
1249
+ async cancel() {
1250
+ await l();
1240
1251
  }
1241
- },
1242
- async cancel() {
1243
- await s();
1244
- }
1245
- });
1246
- return new Response(c, {
1247
- ...t,
1248
- headers: {
1249
- "content-type": "text/event-stream",
1250
- "cache-control": "no-cache",
1251
- connection: "keep-alive",
1252
- ...t.headers
1253
- }
1254
- });
1255
- }, 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) => {
1256
1264
  let n = JSON.stringify(await e);
1257
1265
  return new Response(n, {
1258
1266
  ...t,
@@ -1263,19 +1271,19 @@ var $ = (e, t = {}) => (n) => {
1263
1271
  }
1264
1272
  });
1265
1273
  };
1266
- function he(e, t) {
1274
+ function ge(e, t) {
1267
1275
  throw new Response(null, {
1268
1276
  status: e,
1269
1277
  headers: { location: t.toString() }
1270
1278
  });
1271
1279
  }
1272
- function ge(e, t) {
1280
+ function _e(e, t) {
1273
1281
  throw new Response(JSON.stringify(typeof t == "string" ? { message: t } : t), {
1274
1282
  status: e,
1275
1283
  headers: { "content-type": "application/json" }
1276
1284
  });
1277
1285
  }
1278
- var _e = async (e, t) => {
1286
+ var ve = async (e, t) => {
1279
1287
  let n = await e;
1280
1288
  return new Response(n, {
1281
1289
  ...t,
@@ -1285,7 +1293,7 @@ var _e = async (e, t) => {
1285
1293
  ...t?.headers
1286
1294
  }
1287
1295
  });
1288
- }, ve = async (e, t) => {
1296
+ }, ye = async (e, t) => {
1289
1297
  let n = await e;
1290
1298
  return new Response(n, {
1291
1299
  ...t,
@@ -1297,6 +1305,6 @@ var _e = async (e, t) => {
1297
1305
  });
1298
1306
  };
1299
1307
  //#endregion
1300
- export { G as Action, ne as CORS, ee as RateLimiter, L as RequestId, H as RequestMethods, W as Router, N as Security, B as Timeout, J as WebServer, de as dir, fe as enhance, ge as error, ve as html, C as isHttpError, w as isRedirect, T as isResponse, me as json, he as redirect, X as serveStatic, $ as sse, _e as text };
1308
+ export { K as Action, te as CORS, O as RateLimiter, R as RequestId, U as RequestMethods, G as Router, P as Security, V as Timeout, Y 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 };
1301
1309
 
1302
1310
  //# sourceMappingURL=index.es.js.map