@sourceregistry/node-webserver 1.7.1 → 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
@@ -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) => {
@@ -545,7 +545,7 @@ var H = [
545
545
  }), this.routesSorted = !1, this;
546
546
  }
547
547
  createPathRegex(e) {
548
- let t = U.get(e);
548
+ let t = W.get(e);
549
549
  if (t) return t;
550
550
  let n = [], r = !1, i, a = e.split("/").filter(Boolean);
551
551
  i = a.reduce((e, t) => t.startsWith("[...") ? e - 10 : t.startsWith("[[") ? e - 5 : t.startsWith("[") ? e - 1 : e + 1, 0);
@@ -568,7 +568,7 @@ var H = [
568
568
  isCatchAll: r,
569
569
  priority: i
570
570
  };
571
- return U.set(e, s), s;
571
+ return W.set(e, s), s;
572
572
  }
573
573
  createPrefixRegex(e) {
574
574
  let t = [], n = !1, r, i = e.split("/").filter(Boolean);
@@ -676,17 +676,17 @@ var H = [
676
676
  this._wsRoutes.sort((e, t) => t.priority - e.priority), this.wsRoutesSorted = !0;
677
677
  }
678
678
  formatActionResult(e) {
679
- 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);
680
680
  }
681
681
  handleActionError(e) {
682
- 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" });
683
683
  if (w(e)) {
684
684
  let t = e.headers.get("Location") || "/";
685
- return G.redirect(e.status, t);
685
+ return K.redirect(e.status, t);
686
686
  }
687
- return console.error(e), G.error(500, { message: "Internal Server Error" });
687
+ return console.error(e), K.error(500, { message: "Internal Server Error" });
688
688
  }
689
- }, G = {
689
+ }, K = {
690
690
  success: (e = 200, t) => new Response(JSON.stringify({
691
691
  data: t,
692
692
  type: "success",
@@ -719,7 +719,7 @@ var H = [
719
719
  status: e,
720
720
  headers: { "Content-Type": "application/json" }
721
721
  })
722
- }, K = class {
722
+ }, q = class {
723
723
  constructor(e, t) {
724
724
  this.raw = e.headers.get("cookie") ?? "", this.setCookieHeader = t;
725
725
  }
@@ -741,11 +741,11 @@ var H = [
741
741
  maxAge: 0
742
742
  });
743
743
  }
744
- }, q = class extends Error {
744
+ }, J = 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 W {
748
+ }, Y = class extends G {
749
749
  constructor(e) {
750
750
  super(), this.upgradeHandlerInstalled = !1, this.config = e ?? {
751
751
  type: "http",
@@ -855,7 +855,7 @@ var H = [
855
855
  if (!t) return e;
856
856
  let n = 0, r = new a({ transform(e, r, i) {
857
857
  if (n += Buffer.byteLength(e), n > t) {
858
- i(new q());
858
+ i(new J());
859
859
  return;
860
860
  }
861
861
  i(null, e);
@@ -962,7 +962,7 @@ var H = [
962
962
  return Number.isFinite(r) && r <= t;
963
963
  }
964
964
  handleError(e) {
965
- 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 });
966
966
  if (C(e)) return new Response(JSON.stringify({
967
967
  error: e.statusText || "Error",
968
968
  status: e.status
@@ -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 K(e, n.pushSetCookie), i = this.config, a = {}, o = {
1063
+ let r = new q(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 H = [
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
- }, Y = {
1091
+ }, ie = {
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 Y[h(e).toLowerCase()] ?? "application/octet-stream";
1186
+ return ie[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(/^\/+/, "");
@@ -1211,51 +1211,56 @@ function pe(e, t = {}) {
1211
1211
  }
1212
1212
  return `${n.join("\n")}\n\n`;
1213
1213
  }
1214
- var $ = (e, t = {}) => (n) => {
1215
- let r = new TextEncoder(), i = null, a = !1, o, s = async () => {
1216
- if (!a) {
1217
- a = !0;
1218
- try {
1219
- await o?.();
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
- i?.close();
1223
- } catch {}
1221
+ await s?.();
1222
+ } finally {
1223
+ try {
1224
+ a?.close();
1225
+ } catch {}
1226
+ }
1224
1227
  }
1225
- }
1226
- }, c = new ReadableStream({
1227
- async start(t) {
1228
- i = t;
1229
- let c = (e, n = {}) => {
1230
- a || t.enqueue(r.encode(pe(e, n)));
1231
- };
1232
- n.request.signal.addEventListener("abort", () => {
1233
- s();
1234
- }, { once: !0 });
1235
- try {
1236
- if (o = await e(n, c), n.request.signal.aborted) {
1237
- await s();
1238
- 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);
1239
1247
  }
1240
- o === void 0 && await s();
1241
- } catch (e) {
1242
- a || t.error(e);
1248
+ },
1249
+ async cancel() {
1250
+ await l();
1243
1251
  }
1244
- },
1245
- async cancel() {
1246
- await s();
1247
- }
1248
- });
1249
- return new Response(c, {
1250
- ...t,
1251
- headers: {
1252
- "content-type": "text/event-stream",
1253
- "cache-control": "no-cache",
1254
- connection: "keep-alive",
1255
- ...t.headers
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 he(e, t) {
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 ge(e, t) {
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 _e = async (e, t) => {
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
- }, ve = async (e, t) => {
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 { 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 };
1304
1309
 
1305
1310
  //# sourceMappingURL=index.es.js.map