@flight-framework/cli 0.4.3 → 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
@@ -914,6 +914,111 @@ async function devCommand(options) {
914
914
  if (bundler && typeof bundler.createDevServer === "function") {
915
915
  consola2.info(`Using bundler: ${bundler.name || bundler.bundler || "custom"}`);
916
916
  const devServer = await bundler.createDevServer(config);
917
+ const transformPort = devServer.port;
918
+ const http = await import("http");
919
+ const fs = await import("fs/promises");
920
+ const path = await import("path");
921
+ const indexHtmlPath = path.join(root, "index.html");
922
+ const publicIndexPath = path.join(root, "public", "index.html");
923
+ const server = http.createServer(async (req, res) => {
924
+ const url = req.url || "/";
925
+ if (url === "/" || url === "/index.html" || !url.includes(".") && !url.startsWith("/@") && !url.startsWith("/__")) {
926
+ try {
927
+ let htmlContent;
928
+ try {
929
+ htmlContent = await fs.readFile(indexHtmlPath, "utf-8");
930
+ } catch {
931
+ htmlContent = await fs.readFile(publicIndexPath, "utf-8");
932
+ }
933
+ if (!htmlContent.includes("/__flightpack/hmr-runtime.js")) {
934
+ htmlContent = htmlContent.replace(
935
+ "</body>",
936
+ `<script src="/__flightpack/hmr-runtime.js"></script>
937
+ </body>`
938
+ );
939
+ }
940
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
941
+ res.end(htmlContent);
942
+ } catch (err) {
943
+ res.writeHead(404, { "Content-Type": "text/plain" });
944
+ res.end(`index.html not found in ${root}`);
945
+ }
946
+ return;
947
+ }
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();
1009
+ }
1010
+ };
1011
+ const shouldTryExtensions = !url.includes(".") && !url.endsWith("/");
1012
+ proxyRequest(url, res, shouldTryExtensions);
1013
+ });
1014
+ const cliPort = port;
1015
+ await new Promise((resolve10) => server.listen(cliPort, () => resolve10()));
1016
+ console.log(`
1017
+ \u26A1 Flight dev server running
1018
+ `);
1019
+ console.log(` Bundler: FlightPack`);
1020
+ console.log(` Local: http://localhost:${cliPort}`);
1021
+ console.log(``);
917
1022
  const elapsed = Date.now() - startTime;
918
1023
  consola2.success(`Flight dev server ready in ${elapsed}ms`);
919
1024
  devServer.printUrls?.();