@flight-framework/cli 0.4.4 → 0.4.5

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.js CHANGED
@@ -945,25 +945,71 @@ async function devCommand(options) {
945
945
  }
946
946
  return;
947
947
  }
948
- const proxyReq = http.request(
949
- {
950
- hostname: "localhost",
951
- port: transformPort,
952
- path: url,
953
- method: req.method,
954
- headers: req.headers
955
- },
956
- (proxyRes) => {
957
- res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
958
- proxyRes.pipe(res);
948
+ const proxyRequest = (targetUrl, originalRes, attemptExtensions = false) => {
949
+ const proxyReq = http.request(
950
+ {
951
+ hostname: "localhost",
952
+ port: transformPort,
953
+ path: targetUrl,
954
+ method: req.method,
955
+ headers: req.headers
956
+ },
957
+ (proxyRes) => {
958
+ if (proxyRes.statusCode !== 404 || !attemptExtensions) {
959
+ res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
960
+ proxyRes.pipe(res);
961
+ return;
962
+ }
963
+ proxyRes.resume();
964
+ const commonExtensions = [".mjs", ".js", ".ts", ".tsx", ".jsx", "/index.js"];
965
+ const tryNextExtension = (index) => {
966
+ if (index >= commonExtensions.length) {
967
+ res.writeHead(404, proxyRes.headers);
968
+ res.end("Not Found");
969
+ return;
970
+ }
971
+ const ext = commonExtensions[index];
972
+ const fallbackUrl = targetUrl + ext;
973
+ const fallbackReq = http.request(
974
+ {
975
+ hostname: "localhost",
976
+ port: transformPort,
977
+ path: fallbackUrl,
978
+ method: "GET",
979
+ // Always GET for fallbacks
980
+ headers: req.headers
981
+ },
982
+ (fallbackRes) => {
983
+ if (fallbackRes.statusCode === 200) {
984
+ res.writeHead(200, fallbackRes.headers);
985
+ fallbackRes.pipe(res);
986
+ } else {
987
+ fallbackRes.resume();
988
+ tryNextExtension(index + 1);
989
+ }
990
+ }
991
+ );
992
+ fallbackReq.on("error", () => tryNextExtension(index + 1));
993
+ fallbackReq.end();
994
+ };
995
+ tryNextExtension(0);
996
+ }
997
+ );
998
+ proxyReq.on("error", (err) => {
999
+ console.error("[CLI Proxy Error]", err.message);
1000
+ if (!res.headersSent) {
1001
+ res.writeHead(502, { "Content-Type": "text/plain" });
1002
+ res.end("FlightPack transform server unavailable");
1003
+ }
1004
+ });
1005
+ if (req.method !== "GET" && req.method !== "HEAD") {
1006
+ req.pipe(proxyReq);
1007
+ } else {
1008
+ proxyReq.end();
959
1009
  }
960
- );
961
- proxyReq.on("error", (err) => {
962
- console.error("[CLI Proxy Error]", err.message);
963
- res.writeHead(502, { "Content-Type": "text/plain" });
964
- res.end("FlightPack transform server unavailable");
965
- });
966
- req.pipe(proxyReq);
1010
+ };
1011
+ const shouldTryExtensions = !url.includes(".") && !url.endsWith("/");
1012
+ proxyRequest(url, res, shouldTryExtensions);
967
1013
  });
968
1014
  const cliPort = port;
969
1015
  await new Promise((resolve10) => server.listen(cliPort, () => resolve10()));