@teamvelix/velix 5.1.6 → 5.2.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.
Files changed (42) hide show
  1. package/README.md +1 -1
  2. package/dist/build/index.js +3 -2
  3. package/dist/chunk-4S5YQJHB.js +11 -0
  4. package/dist/chunk-4S5YQJHB.js.map +1 -0
  5. package/dist/{chunk-TGNK4MY5.js → chunk-DTF3KTLR.js} +116 -53
  6. package/dist/chunk-DTF3KTLR.js.map +1 -0
  7. package/dist/{chunk-OIZNYND3.js → chunk-JI2HJFUT.js} +2 -2
  8. package/dist/chunk-JI2HJFUT.js.map +1 -0
  9. package/dist/{chunk-RFWV4CDG.js → chunk-QIPVMOXL.js} +4 -4
  10. package/dist/chunk-QIPVMOXL.js.map +1 -0
  11. package/dist/{chunk-INOZP2VD.js → chunk-QLFG6PW3.js} +3 -2
  12. package/dist/chunk-QLFG6PW3.js.map +1 -0
  13. package/dist/{chunk-BXDKUP2L.js → chunk-XXPHGRLC.js} +3 -3
  14. package/dist/{chunk-BXDKUP2L.js.map → chunk-XXPHGRLC.js.map} +1 -1
  15. package/dist/client/index.d.ts +2 -1
  16. package/dist/client/index.js +2 -1
  17. package/dist/config.d.ts +2 -2
  18. package/dist/config.js +1 -0
  19. package/dist/{image-optimizer-I6TWWH6W.js → image-optimizer-KPRXE5VI.js} +3 -3
  20. package/dist/image-optimizer-KPRXE5VI.js.map +1 -0
  21. package/dist/index-8CqitL9K.d.ts +300 -0
  22. package/dist/{index-C4udNtpZ.d.ts → index-ChyKLf1W.d.ts} +18 -3
  23. package/dist/index.d.ts +104 -226
  24. package/dist/index.js +12 -6
  25. package/dist/index.js.map +1 -1
  26. package/dist/islands/index.d.ts +22 -10
  27. package/dist/islands/index.js +2 -1
  28. package/dist/runtime/start-build.js +2 -2
  29. package/dist/runtime/start-build.js.map +1 -1
  30. package/dist/runtime/start-dev.js +117 -46
  31. package/dist/runtime/start-dev.js.map +1 -1
  32. package/dist/runtime/start-prod.js +117 -46
  33. package/dist/runtime/start-prod.js.map +1 -1
  34. package/dist/server/index.d.ts +3 -1
  35. package/dist/server/index.js +4 -3
  36. package/package.json +60 -58
  37. package/dist/chunk-INOZP2VD.js.map +0 -1
  38. package/dist/chunk-OIZNYND3.js.map +0 -1
  39. package/dist/chunk-RFWV4CDG.js.map +0 -1
  40. package/dist/chunk-TGNK4MY5.js.map +0 -1
  41. package/dist/image-optimizer-I6TWWH6W.js.map +0 -1
  42. package/dist/index-DYgxL3mE.d.ts +0 -107
@@ -8,12 +8,16 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
 
11
- // ../../node_modules/.pnpm/tsup@8.5.1_jiti@1.21.7_post_49c9ba138cf4e7dd637766b727229610/node_modules/tsup/assets/esm_shims.js
11
+ // ../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postc_36112cdf9ab6125b194fc7941daee874/node_modules/tsup/assets/esm_shims.js
12
12
  import path from "path";
13
13
  import { fileURLToPath } from "url";
14
+ var getFilename, getDirname, __dirname;
14
15
  var init_esm_shims = __esm({
15
- "../../node_modules/.pnpm/tsup@8.5.1_jiti@1.21.7_post_49c9ba138cf4e7dd637766b727229610/node_modules/tsup/assets/esm_shims.js"() {
16
+ "../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postc_36112cdf9ab6125b194fc7941daee874/node_modules/tsup/assets/esm_shims.js"() {
16
17
  "use strict";
18
+ getFilename = () => fileURLToPath(import.meta.url);
19
+ getDirname = () => path.dirname(getFilename());
20
+ __dirname = /* @__PURE__ */ getDirname();
17
21
  }
18
22
  });
19
23
 
@@ -273,7 +277,7 @@ function buildRouteTree(appDir) {
273
277
  pages: [],
274
278
  api: [],
275
279
  layouts: /* @__PURE__ */ new Map(),
276
- tree: {},
280
+ tree: { children: {}, routes: [] },
277
281
  appRoutes: []
278
282
  };
279
283
  if (fs3.existsSync(appDir)) {
@@ -453,20 +457,23 @@ init_esm_shims();
453
457
  import fs4 from "fs";
454
458
  import path4 from "path";
455
459
  async function loadMiddleware(projectRoot) {
456
- const middlewareDir = path4.join(projectRoot, "middleware");
457
460
  const fns = [];
458
- if (!fs4.existsSync(middlewareDir)) return fns;
459
- const files = fs4.readdirSync(middlewareDir).filter((f) => /\.(ts|js)$/.test(f)).sort();
460
- for (const file of files) {
461
+ const possibleFiles = ["proxy.ts", "proxy.js"];
462
+ for (const file of possibleFiles) {
463
+ const filePath = path4.join(projectRoot, file);
464
+ if (!fs4.existsSync(filePath)) continue;
461
465
  try {
462
- const filePath = path4.join(middlewareDir, file);
463
466
  const { pathToFileURL: pathToFileURL4 } = await import("url");
464
467
  const url = pathToFileURL4(filePath).href;
465
468
  const mod = await import(`${url}?t=${Date.now()}`);
466
- const fn = mod.default || mod.middleware;
467
- if (typeof fn === "function") fns.push(fn);
469
+ const fn = mod.default || mod.proxy || mod.middleware;
470
+ if (typeof fn === "function") {
471
+ fns.push(fn);
472
+ break;
473
+ }
468
474
  } catch (err) {
469
- console.warn(`\u26A0 Failed to load middleware ${file}: ${err.message}`);
475
+ const error = err;
476
+ console.warn(`\u26A0 Failed to load proxy ${file}: ${error.message}`);
470
477
  }
471
478
  }
472
479
  return fns;
@@ -643,11 +650,12 @@ async function loadPlugins(projectRoot, config) {
643
650
  console.warn(`\u26A0 Plugin not found: ${entry}`);
644
651
  }
645
652
  }
646
- } else if (typeof entry === "object" && entry.name) {
653
+ } else if (typeof entry === "object" && entry !== null && "name" in entry) {
647
654
  pluginManager.register(entry);
648
655
  }
649
656
  } catch (err) {
650
- console.warn(`\u26A0 Failed to load plugin: ${err.message}`);
657
+ const error = err;
658
+ console.warn(`\u26A0 Failed to load plugin: ${error?.message || String(error)}`);
651
659
  }
652
660
  }
653
661
  }
@@ -662,10 +670,11 @@ var builtinPlugins = {
662
670
  name: "velix:security",
663
671
  hooks: {
664
672
  [PluginHooks.RESPONSE]: (_req, res) => {
665
- if (!res.headersSent) {
666
- res.setHeader("X-Content-Type-Options", "nosniff");
667
- res.setHeader("X-Frame-Options", "DENY");
668
- res.setHeader("X-XSS-Protection", "1; mode=block");
673
+ const response = res;
674
+ if (!response.headersSent) {
675
+ response.setHeader("X-Content-Type-Options", "nosniff");
676
+ response.setHeader("X-Frame-Options", "DENY");
677
+ response.setHeader("X-XSS-Protection", "1; mode=block");
669
678
  }
670
679
  }
671
680
  }
@@ -677,7 +686,8 @@ var builtinPlugins = {
677
686
  name: "velix:logger",
678
687
  hooks: {
679
688
  [PluginHooks.RESPONSE]: (req, _res, duration) => {
680
- console.log(` ${req.method} ${req.url} ${duration}ms`);
689
+ const request = req;
690
+ console.log(` ${request.method} ${request.url} ${duration}ms`);
681
691
  }
682
692
  }
683
693
  })
@@ -691,7 +701,7 @@ init_esm_shims();
691
701
 
692
702
  // version.ts
693
703
  init_esm_shims();
694
- var VERSION = "5.1.4";
704
+ var VERSION = "5.1.7";
695
705
 
696
706
  // logger.ts
697
707
  var colors = {
@@ -1220,7 +1230,8 @@ async function executeAction(actionId, args, context) {
1220
1230
  try {
1221
1231
  const result = await action(...args);
1222
1232
  return { success: true, data: result };
1223
- } catch (error) {
1233
+ } catch (err) {
1234
+ const error = err;
1224
1235
  if (error instanceof RedirectError) return { success: true, redirect: error.url };
1225
1236
  if (error instanceof NotFoundError) return { success: false, error: "Not found" };
1226
1237
  return { success: false, error: error.message || "Action failed" };
@@ -2116,6 +2127,14 @@ function generate500Page(options) {
2116
2127
  }
2117
2128
 
2118
2129
  // server/index.ts
2130
+ import {
2131
+ defineRoute,
2132
+ VelixHttpError,
2133
+ defineLoader,
2134
+ serverAction,
2135
+ buildApiManifest,
2136
+ handleApiRequest
2137
+ } from "@teamvelix/velix-core";
2119
2138
  function getProjectVersion(dep, projectRoot) {
2120
2139
  try {
2121
2140
  const projectRequire = createRequire(pathToFileURL3(path7.join(projectRoot, "package.json")).href);
@@ -2226,14 +2245,15 @@ async function createServer(options = {}) {
2226
2245
  res.end(generate404Page(pathname));
2227
2246
  if (isDev) logger_default.request(req.method || "GET", pathname, 404, Date.now() - requestStart);
2228
2247
  } catch (error) {
2248
+ const err = error;
2229
2249
  console.error("Server error:", error);
2230
2250
  if (!res.headersSent) {
2231
2251
  res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
2232
2252
  res.end(generate500Page({
2233
2253
  statusCode: 500,
2234
2254
  title: "Server Error",
2235
- message: error.message || "An unexpected error occurred",
2236
- stack: isDev ? error.stack : void 0,
2255
+ message: err.message || "An unexpected error occurred",
2256
+ stack: isDev ? err.stack : void 0,
2237
2257
  isDev,
2238
2258
  pathname
2239
2259
  }));
@@ -2360,26 +2380,26 @@ async function handleApiRoute(route, req, res, url) {
2360
2380
  res.end(JSON.stringify({ error: "Method not allowed" }));
2361
2381
  return;
2362
2382
  }
2363
- let body;
2383
+ let rawBody;
2364
2384
  if (["POST", "PUT", "PATCH"].includes(method)) {
2365
- body = await parseRequestBody(req);
2385
+ rawBody = await parseRawBody(req);
2366
2386
  }
2367
- const request = {
2368
- method,
2369
- url: req.url,
2370
- headers: req.headers,
2371
- params: route.params || {},
2372
- query: Object.fromEntries(url.searchParams),
2373
- body,
2374
- json: () => body
2375
- };
2376
- const response = await handler(request);
2387
+ const headers2 = new Headers();
2388
+ for (const [key, val] of Object.entries(req.headers)) {
2389
+ if (Array.isArray(val)) val.forEach((v) => headers2.append(key, v));
2390
+ else if (val) headers2.set(key, val);
2391
+ }
2392
+ const reqInit = { method, headers: headers2 };
2393
+ if (rawBody !== void 0) reqInit.body = rawBody;
2394
+ const request = new Request(url.href, reqInit);
2395
+ request.velixUrl = url;
2396
+ const response = await handler(request, { params: route.params || {} });
2377
2397
  if (response instanceof Response) {
2378
- const headers2 = {};
2398
+ const headers3 = {};
2379
2399
  response.headers.forEach((v, k) => {
2380
- headers2[k] = v;
2400
+ headers3[k] = v;
2381
2401
  });
2382
- res.writeHead(response.status, headers2);
2402
+ res.writeHead(response.status, headers3);
2383
2403
  const text = await response.text();
2384
2404
  res.end(text);
2385
2405
  } else if (typeof response === "object") {
@@ -2390,8 +2410,9 @@ async function handleApiRoute(route, req, res, url) {
2390
2410
  res.end(String(response));
2391
2411
  }
2392
2412
  } catch (err) {
2413
+ const error = err;
2393
2414
  res.writeHead(500, { "Content-Type": "application/json" });
2394
- res.end(JSON.stringify({ error: err.message }));
2415
+ res.end(JSON.stringify({ error: error.message }));
2395
2416
  }
2396
2417
  }
2397
2418
  async function handleServerAction(req, res) {
@@ -2407,8 +2428,9 @@ async function handleServerAction(req, res) {
2407
2428
  res.writeHead(200, { "Content-Type": "application/json" });
2408
2429
  res.end(JSON.stringify(result));
2409
2430
  } catch (err) {
2431
+ const error = err;
2410
2432
  res.writeHead(500, { "Content-Type": "application/json" });
2411
- res.end(JSON.stringify({ success: false, error: err.message }));
2433
+ res.end(JSON.stringify({ success: false, error: error.message }));
2412
2434
  }
2413
2435
  }
2414
2436
  async function handlePageRoute(route, routes, req, res, url, config, isDev, projectRoot) {
@@ -2473,9 +2495,14 @@ async function handlePageRoute(route, routes, req, res, url, config, isDev, proj
2473
2495
  const headInjectionsHtml = `
2474
2496
  ${headInjections}
2475
2497
  `;
2498
+ const hmrScript = isDev ? `
2499
+ <script type="module">
2500
+ import { initHMRClient, VelixDevOverlay } from '/__velix/hmr-client.js';
2501
+ initHMRClient();
2502
+ </script>` : "";
2476
2503
  const bodyInjectionsHtml = `
2477
2504
  <div id="__velix-islands"></div>
2478
- ${hydrationScript}${devToolsHtml}
2505
+ ${hydrationScript}${devToolsHtml}${hmrScript}
2479
2506
  `;
2480
2507
  if (finalHtml.includes("<html")) {
2481
2508
  const headEnd = finalHtml.lastIndexOf("</head>");
@@ -2511,13 +2538,14 @@ async function handlePageRoute(route, routes, req, res, url, config, isDev, proj
2511
2538
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
2512
2539
  res.end(finalHtml);
2513
2540
  } catch (err) {
2514
- logger_default.error(`Render error: ${route.path}`, err);
2541
+ const error = err;
2542
+ logger_default.error(`Render error: ${route.path}`, error);
2515
2543
  res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
2516
2544
  res.end(generate500Page({
2517
2545
  statusCode: 500,
2518
2546
  title: "Render Error",
2519
- message: err.message || "Failed to render page",
2520
- stack: isDev ? err.stack : void 0,
2547
+ message: error.message || "Failed to render page",
2548
+ stack: isDev ? error.stack : void 0,
2521
2549
  isDev,
2522
2550
  pathname: route.path
2523
2551
  }));
@@ -2560,6 +2588,38 @@ async function serveVelixInternal(pathname, req, res, projectRoot) {
2560
2588
  }
2561
2589
  return;
2562
2590
  }
2591
+ if (pathname === "/__velix/hmr-client.js") {
2592
+ try {
2593
+ const candidates = [
2594
+ path7.join(process.cwd(), "packages", "velix-react", "src", "hmr", "hmr-client.ts"),
2595
+ path7.join(process.cwd(), "node_modules", "@teamvelix", "velix-react", "src", "hmr", "hmr-client.ts"),
2596
+ path7.join(__dirname, "..", "..", "velix-react", "src", "hmr", "hmr-client.ts")
2597
+ ];
2598
+ const hmrClientSrc = candidates.find((p) => fs7.existsSync(p));
2599
+ if (!hmrClientSrc) {
2600
+ res.writeHead(404);
2601
+ res.end();
2602
+ return;
2603
+ }
2604
+ const result = await esbuild.build({
2605
+ entryPoints: [hmrClientSrc],
2606
+ bundle: true,
2607
+ format: "esm",
2608
+ platform: "browser",
2609
+ target: ["es2022"],
2610
+ minify: true,
2611
+ write: false
2612
+ });
2613
+ res.writeHead(200, { "Content-Type": "application/javascript", "Cache-Control": "no-cache" });
2614
+ res.end(result.outputFiles[0].text);
2615
+ return;
2616
+ } catch (err) {
2617
+ console.error("HMR Client build failed", err);
2618
+ res.writeHead(500);
2619
+ res.end();
2620
+ return;
2621
+ }
2622
+ }
2563
2623
  if (pathname.startsWith("/__velix/islands/") && pathname.endsWith(".js")) {
2564
2624
  const componentName = pathname.replace("/__velix/islands/", "").replace(".js", "");
2565
2625
  try {
@@ -2600,9 +2660,10 @@ async function serveVelixInternal(pathname, req, res, projectRoot) {
2600
2660
  res.end(result.outputFiles[0].text);
2601
2661
  return;
2602
2662
  } catch (err) {
2603
- logger_default.error(`Island bundling failed: ${componentName}`, err);
2663
+ const error = err;
2664
+ logger_default.error(`Island bundling failed: ${componentName}`, error);
2604
2665
  res.writeHead(500);
2605
- res.end(`console.error("Island bundling failed: ${err.message}");`);
2666
+ res.end(`console.error("Island bundling failed: ${error.message}");`);
2606
2667
  return;
2607
2668
  }
2608
2669
  }
@@ -2627,7 +2688,7 @@ async function serveVelixInternal(pathname, req, res, projectRoot) {
2627
2688
  return;
2628
2689
  }
2629
2690
  }
2630
- res.writeHead(404);
2691
+ res.writeHead(404, { "Content-Type": "text/plain" });
2631
2692
  res.end("Not found");
2632
2693
  }
2633
2694
  function parseRequestBody(req) {
@@ -2648,6 +2709,16 @@ function parseRequestBody(req) {
2648
2709
  req.on("error", reject);
2649
2710
  });
2650
2711
  }
2712
+ function parseRawBody(req) {
2713
+ return new Promise((resolve, reject) => {
2714
+ let body = "";
2715
+ req.on("data", (chunk) => {
2716
+ body += chunk;
2717
+ });
2718
+ req.on("end", () => resolve(body));
2719
+ req.on("error", reject);
2720
+ });
2721
+ }
2651
2722
 
2652
2723
  // runtime/start-prod.ts
2653
2724
  async function startProd() {