@moonsjs/vite 0.5.0 → 0.7.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.
@@ -24,6 +24,7 @@ import {
24
24
  runHandlerWithTimeout,
25
25
  isAbortLike,
26
26
  textResponseWithHeaders,
27
+ parseRangeHeader,
27
28
  applyStaticHeaders,
28
29
  readBodyWithLimit,
29
30
  resolveStaticFile2,
@@ -34,7 +35,7 @@ import {
34
35
  toWebRequest2,
35
36
  sendNodeResponse2,
36
37
  installDevMiddleware2
37
- } from "../index-sdm38v1p.js";
38
+ } from "../index-h92zw0gs.js";
38
39
 
39
40
  // src/server/handler.ts
40
41
  import {
@@ -679,7 +680,7 @@ function startNodeServer(handler, options = {}) {
679
680
  const requestStartedAt = performance.now();
680
681
  if (accessLogger !== null) {
681
682
  res.once("finish", () => {
682
- const ip = resolveClientIp(req, options.trustProxy === true);
683
+ const ip = resolveClientIp(req, options.trustProxy === true, options.clientIpHeader ?? "x-forwarded-for");
683
684
  accessLogger({
684
685
  msg: "request",
685
686
  method: req.method ?? "GET",
@@ -769,6 +770,22 @@ function startNodeServer(handler, options = {}) {
769
770
  res.end();
770
771
  return;
771
772
  }
773
+ res.setHeader("accept-ranges", "bytes");
774
+ const rawRange = req.headers["range"];
775
+ const range = parseRangeHeader(Array.isArray(rawRange) ? rawRange[0] ?? null : rawRange ?? null, stat.size);
776
+ if (req.method === "GET" && range !== null) {
777
+ if (range.kind === "unsatisfiable") {
778
+ res.statusCode = 416;
779
+ res.setHeader("content-range", `bytes */${stat.size}`);
780
+ res.end();
781
+ return;
782
+ }
783
+ const slice = body.subarray(range.start, range.end + 1);
784
+ res.statusCode = 206;
785
+ res.setHeader("content-range", `bytes ${range.start}-${range.end}/${stat.size}`);
786
+ res.end(slice);
787
+ return;
788
+ }
772
789
  let payload = body;
773
790
  const encodings = req.headers["accept-encoding"];
774
791
  const acceptsGzip = acceptsGzipHeader(Array.isArray(encodings) ? encodings[0] ?? null : encodings ?? null);
@@ -794,6 +811,7 @@ function startNodeServer(handler, options = {}) {
794
811
  }
795
812
  const request = await toWebRequest2(req, {
796
813
  trustProxy: options.trustProxy === true,
814
+ ...options.clientIpHeader === undefined ? {} : { clientIpHeader: options.clientIpHeader },
797
815
  ...isRpc ? { maxBodyBytes: rpcLimit } : {}
798
816
  });
799
817
  const response = await runHandlerWithTimeout(handler, request, options.requestTimeoutMs);
@@ -1007,6 +1025,7 @@ function startBunServer(handler, options = {}) {
1007
1025
  const port = options.port ?? Number(process.env["PORT"] ?? 3000);
1008
1026
  const trustProxy = options.trustProxy === true;
1009
1027
  const requestTimeoutMs = options.requestTimeoutMs;
1028
+ const clientIpHeader = options.clientIpHeader ?? "x-forwarded-for";
1010
1029
  const idleTimeout = requestTimeoutMs === undefined || requestTimeoutMs <= 0 ? 0 : Math.ceil(requestTimeoutMs / 1000) + 5;
1011
1030
  const accessLogger = options.logging?.accessLog === true ? createLogger({
1012
1031
  json: options.logging.json !== false,
@@ -1065,6 +1084,18 @@ function startBunServer(handler, options = {}) {
1065
1084
  if (ifNoneMatch !== null && ifNoneMatch.split(",").some((value) => value.trim() === etag)) {
1066
1085
  return new Response(null, { status: 304, headers });
1067
1086
  }
1087
+ headers.set("accept-ranges", "bytes");
1088
+ const range = request.method === "GET" ? parseRangeHeader(request.headers.get("range"), stat.size) : null;
1089
+ if (range !== null) {
1090
+ if (range.kind === "unsatisfiable") {
1091
+ headers.set("content-range", `bytes */${stat.size}`);
1092
+ return new Response(null, { status: 416, headers });
1093
+ }
1094
+ const full = readFileSync3(staticFile);
1095
+ const slice = full.subarray(range.start, range.end + 1);
1096
+ headers.set("content-range", `bytes ${range.start}-${range.end}/${stat.size}`);
1097
+ return new Response(slice, { status: 206, headers });
1098
+ }
1068
1099
  let payload = request.method === "HEAD" ? null : readFileSync3(staticFile);
1069
1100
  if (payload !== null && COMPRESSIBLE_FILE.test(staticFile) && payload.byteLength >= GZIP_MIN_BYTES && acceptsGzipHeader(request.headers.get("accept-encoding"))) {
1070
1101
  payload = gzipCached(staticFile, stat.mtimeMs, Buffer.from(payload));
@@ -1125,7 +1156,7 @@ function startBunServer(handler, options = {}) {
1125
1156
  }
1126
1157
  }
1127
1158
  const remoteAddress = bunServer.requestIP(request)?.address ?? null;
1128
- clientIp = resolveClientIpValues(request.headers.get("x-forwarded-for") ?? undefined, remoteAddress, trustProxy);
1159
+ clientIp = resolveClientIpValues(request.headers.get(clientIpHeader) ?? undefined, remoteAddress, trustProxy);
1129
1160
  const needsRewrite = clientIp !== null || url.toString() !== request.url;
1130
1161
  const webRequest = (request.method === "GET" || request.method === "HEAD") && !needsRewrite ? request : await toBoundedRequest(request, url, clientIp, isRpc ? rpcLimit : MAX_TRANSPORT_BODY_BYTES);
1131
1162
  const response = await runHandlerWithTimeout(handler, webRequest, requestTimeoutMs);
@@ -1599,4 +1630,4 @@ export {
1599
1630
  toWebRequest2 as toWebRequest
1600
1631
  };
1601
1632
 
1602
- //# debugId=C3DF08B69DE9E6D964756E2164756E21
1633
+ //# debugId=01F6AD8AA11E371C64756E2164756E21