@webtypen/webframez-react 0.0.49 → 0.0.51

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.cjs CHANGED
@@ -4,6 +4,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __commonJS = (cb, mod) => function __require() {
8
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
+ };
7
10
  var __export = (target, all) => {
8
11
  for (var name in all)
9
12
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -26,6 +29,60 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
29
  ));
27
30
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
31
 
32
+ // node_modules/@webtypen/webframez-core/dist/routing.js
33
+ var require_routing = __commonJS({
34
+ "node_modules/@webtypen/webframez-core/dist/routing.js"(exports2) {
35
+ "use strict";
36
+ Object.defineProperty(exports2, "__esModule", { value: true });
37
+ exports2.appRelativePath = exports2.appPath = exports2.getBasename = exports2.normalizeBasename = void 0;
38
+ function normalizeBasename2(value) {
39
+ if (value == null || value === "" || value === "/")
40
+ return "";
41
+ if (typeof value !== "string")
42
+ throw new Error("Router basename must be a string.");
43
+ const base = value.trim().replace(/\/+$/, "");
44
+ if (!base && value.trim().length <= 1)
45
+ return "";
46
+ if (!/^\/(?:[A-Za-z0-9_~-]+(?:\.[A-Za-z0-9_~-]+)*)(?:\/[A-Za-z0-9_~-]+(?:\.[A-Za-z0-9_~-]+)*)*$/.test(base)) {
47
+ throw new Error("Router basename must be empty or an absolute path such as /my-app.");
48
+ }
49
+ return base;
50
+ }
51
+ exports2.normalizeBasename = normalizeBasename2;
52
+ function getBasename2() {
53
+ var _a, _b, _c, _d;
54
+ const runtime = globalThis;
55
+ return (_d = (_c = (_b = (_a = runtime.__WEBFRAMEZ_ROUTING_CONTEXT__) === null || _a === void 0 ? void 0 : _a.getStore()) !== null && _b !== void 0 ? _b : runtime.__RSC_BASENAME) !== null && _c !== void 0 ? _c : runtime.__WEBFRAMEZ_ROUTER_BASENAME__) !== null && _d !== void 0 ? _d : "";
56
+ }
57
+ exports2.getBasename = getBasename2;
58
+ function appPath2(value, basename = getBasename2()) {
59
+ const base = normalizeBasename2(basename);
60
+ if (!base || !value.startsWith("/") || value.startsWith("//") || hasBasename(value, base))
61
+ return value;
62
+ return base + value;
63
+ }
64
+ exports2.appPath = appPath2;
65
+ function appRelativePath2(value, basename = getBasename2()) {
66
+ const base = normalizeBasename2(basename);
67
+ if (!base || !hasBasename(value, base))
68
+ return value;
69
+ const relative = value.slice(base.length);
70
+ return !relative || relative.startsWith("?") || relative.startsWith("#") ? "/" + relative : relative;
71
+ }
72
+ exports2.appRelativePath = appRelativePath2;
73
+ function hasBasename(value, base) {
74
+ return value === base || value.startsWith(base + "/") || value.startsWith(base + "?") || value.startsWith(base + "#");
75
+ }
76
+ }
77
+ });
78
+
79
+ // node_modules/@webtypen/webframez-core/routing.js
80
+ var require_routing2 = __commonJS({
81
+ "node_modules/@webtypen/webframez-core/routing.js"(exports2, module2) {
82
+ module2.exports = require_routing();
83
+ }
84
+ });
85
+
29
86
  // src/index.ts
30
87
  var src_exports = {};
31
88
  __export(src_exports, {
@@ -35,10 +92,12 @@ __export(src_exports, {
35
92
  createHTMLShell: () => createHTMLShell,
36
93
  createNodeRequestHandler: () => createNodeRequestHandler,
37
94
  createRSCHandler: () => createRSCHandler,
95
+ disposeReactHtmlRenderer: () => disposeReactHtmlRenderer,
38
96
  getRegisteredReactBuildTargets: () => getRegisteredReactBuildTargets,
39
97
  initWebframezReact: () => initWebframezReact,
40
98
  parseSearchParams: () => parseSearchParams,
41
99
  renderHeadToString: () => renderHeadToString,
100
+ renderReactToHtml: () => renderReactToHtml,
42
101
  resolveWebframezReactRouteOptions: () => resolveWebframezReactRouteOptions,
43
102
  sendRSC: () => sendRSC,
44
103
  setupWebframezCoreReactRoute: () => setupWebframezCoreReactRoute
@@ -376,8 +435,7 @@ var FORCED_PACKAGE_REQUESTS = [
376
435
  "react-server-dom-webpack",
377
436
  "react-server-dom-webpack/server",
378
437
  "react-server-dom-webpack/client",
379
- "react-server-dom-webpack/client.node",
380
- "scheduler"
438
+ "react-server-dom-webpack/client.node"
381
439
  ];
382
440
  var forcedPackageResolutionInstalled = false;
383
441
  function normalizePathname(pathname) {
@@ -901,6 +959,7 @@ function parseSearchParams(query) {
901
959
  }
902
960
 
903
961
  // src/http.ts
962
+ var import_node_async_hooks = require("node:async_hooks");
904
963
  var import_node_fs2 = __toESM(require("node:fs"), 1);
905
964
  var import_node_path3 = __toESM(require("node:path"), 1);
906
965
  var import_node_url = require("node:url");
@@ -1033,6 +1092,7 @@ const rootParent = {
1033
1092
  path: process.cwd(),
1034
1093
  paths: Module._nodeModulePaths(process.cwd()),
1035
1094
  };
1095
+ // Transitive dependencies (e.g. scheduler) must resolve from their importer.
1036
1096
  const forcedPackageRequests = [
1037
1097
  "@webtypen/webframez-core",
1038
1098
  "@webtypen/webframez-react",
@@ -1047,7 +1107,6 @@ const forcedPackageRequests = [
1047
1107
  "react-server-dom-webpack/server",
1048
1108
  "react-server-dom-webpack/client",
1049
1109
  "react-server-dom-webpack/client.node",
1050
- "scheduler"
1051
1110
  ];
1052
1111
 
1053
1112
  function shouldForcePackageResolution(request) {
@@ -1239,13 +1298,17 @@ async function renderHtmlFromFlightData(flightData, moduleMap) {
1239
1298
  ? payload.head.basename
1240
1299
  : "";
1241
1300
 
1242
- const previousBasename = globalThis.__RSC_BASENAME;
1243
- globalThis.__RSC_BASENAME = basename;
1244
- try {
1245
- return await renderHtml(model);
1246
- } finally {
1247
- globalThis.__RSC_BASENAME = previousBasename;
1248
- }
1301
+ const routingContext = globalThis.__WEBFRAMEZ_ROUTING_CONTEXT__ ??=
1302
+ new (require("node:async_hooks").AsyncLocalStorage)();
1303
+ return routingContext.run(basename, async () => {
1304
+ const previousBasename = globalThis.__RSC_BASENAME;
1305
+ globalThis.__RSC_BASENAME = basename;
1306
+ try {
1307
+ return await renderHtml(model);
1308
+ } finally {
1309
+ globalThis.__RSC_BASENAME = previousBasename;
1310
+ }
1311
+ });
1249
1312
  }
1250
1313
 
1251
1314
  process.on("message", async (message) => {
@@ -1481,24 +1544,10 @@ ${stderrBuffer.trim()}` : "";
1481
1544
  }
1482
1545
  };
1483
1546
  }
1547
+ var routingRuntime = globalThis;
1548
+ var basenameContext = routingRuntime.__WEBFRAMEZ_ROUTING_CONTEXT__ ??= new import_node_async_hooks.AsyncLocalStorage();
1484
1549
  function withRequestBasename(basename, fn) {
1485
- const target = globalThis;
1486
- const previous = target.__RSC_BASENAME;
1487
- target.__RSC_BASENAME = basename;
1488
- const finish = () => {
1489
- target.__RSC_BASENAME = previous;
1490
- };
1491
- try {
1492
- const result = fn();
1493
- if (result && typeof result.then === "function") {
1494
- return result.finally(finish);
1495
- }
1496
- finish();
1497
- return result;
1498
- } catch (error) {
1499
- finish();
1500
- throw error;
1501
- }
1550
+ return basenameContext.run(basename, fn);
1502
1551
  }
1503
1552
  function parseCookies(rawCookieHeader) {
1504
1553
  const raw = Array.isArray(rawCookieHeader) ? rawCookieHeader.join("; ") : rawCookieHeader ?? "";
@@ -1533,12 +1582,27 @@ function normalizeClientManifest(manifest, options) {
1533
1582
  }
1534
1583
  };
1535
1584
  for (const [key, value] of Object.entries(manifest)) {
1585
+ const hashIndex = key.indexOf("#");
1586
+ const moduleKey = hashIndex < 0 ? key : key.slice(0, hashIndex);
1587
+ const exportSuffix = hashIndex < 0 ? "" : key.slice(hashIndex);
1588
+ if (!import_node_path3.default.isAbsolute(moduleKey) && !moduleKey.includes(":") && !moduleKey.startsWith("file://")) {
1589
+ const runtimePath = import_node_path3.default.resolve(options.cwd, moduleKey);
1590
+ const allowedRoots = [options.distRootDir, ...candidateNodeModulesDirs];
1591
+ const insideArtifact = allowedRoots.some((root) => {
1592
+ const relative = import_node_path3.default.relative(import_node_path3.default.resolve(root), runtimePath);
1593
+ return relative !== "" && relative !== ".." && !relative.startsWith(`..${import_node_path3.default.sep}`) && !import_node_path3.default.isAbsolute(relative);
1594
+ });
1595
+ if (insideArtifact) {
1596
+ addAlias(`${runtimePath}${exportSuffix}`, value);
1597
+ addAlias(`${(0, import_node_url.pathToFileURL)(runtimePath).href}${exportSuffix}`, value);
1598
+ }
1599
+ }
1536
1600
  if (!key.startsWith("file://")) {
1537
1601
  continue;
1538
1602
  }
1539
1603
  let absolutePath = "";
1540
1604
  try {
1541
- absolutePath = (0, import_node_url.fileURLToPath)(key);
1605
+ absolutePath = (0, import_node_url.fileURLToPath)(moduleKey);
1542
1606
  } catch {
1543
1607
  continue;
1544
1608
  }
@@ -1549,13 +1613,13 @@ function normalizeClientManifest(manifest, options) {
1549
1613
  }
1550
1614
  const relativeModulePath = absolutePath.slice(markerIndex + marker.length);
1551
1615
  const relativeModulePathPosix = relativeModulePath.split(import_node_path3.default.sep).join("/");
1552
- addAlias(`./node_modules/${relativeModulePathPosix}`, value);
1553
- addAlias(`node_modules/${relativeModulePathPosix}`, value);
1554
- addAlias(absolutePath, value);
1616
+ addAlias(`./node_modules/${relativeModulePathPosix}${exportSuffix}`, value);
1617
+ addAlias(`node_modules/${relativeModulePathPosix}${exportSuffix}`, value);
1618
+ addAlias(`${absolutePath}${exportSuffix}`, value);
1555
1619
  for (const nodeModulesDir of candidateNodeModulesDirs) {
1556
1620
  const aliasPath = import_node_path3.default.join(nodeModulesDir, relativeModulePath);
1557
- addAlias(aliasPath, value);
1558
- addAlias((0, import_node_url.pathToFileURL)(aliasPath).href, value);
1621
+ addAlias(`${aliasPath}${exportSuffix}`, value);
1622
+ addAlias(`${(0, import_node_url.pathToFileURL)(aliasPath).href}${exportSuffix}`, value);
1559
1623
  }
1560
1624
  }
1561
1625
  return normalized;
@@ -1700,10 +1764,10 @@ function createNodeRequestHandler(options) {
1700
1764
  const manifestPath = import_node_path3.default.resolve(
1701
1765
  options.manifestPath ?? import_node_path3.default.join(distRootDir, "react-client-manifest.json")
1702
1766
  );
1703
- const assetsPrefix = options.assetsPrefix ?? "/assets/";
1704
- const rscPath = options.rscPath ?? "/rsc";
1705
- const clientScriptUrl = options.clientScriptUrl ?? "/assets/client.js";
1706
1767
  const basePath = normalizeBasePath(options.basePath);
1768
+ const assetsPrefix = options.assetsPrefix ?? `${basePath}/assets/`;
1769
+ const rscPath = options.rscPath ?? `${basePath}/rsc`;
1770
+ const clientScriptUrl = options.clientScriptUrl ?? `${basePath}/assets/client.js`;
1707
1771
  const nodeEnv = process.env.NODE_ENV || "";
1708
1772
  const runningInWatchMode = Array.isArray(process.execArgv) && process.execArgv.includes("--watch");
1709
1773
  const liveReloadEnabled = options.liveReloadPath !== false && (nodeEnv === "development" || runningInWatchMode);
@@ -1722,7 +1786,7 @@ function createNodeRequestHandler(options) {
1722
1786
  process.once("exit", disposeInitialHtmlWorker);
1723
1787
  process.once("SIGINT", disposeInitialHtmlWorker);
1724
1788
  process.once("SIGTERM", disposeInitialHtmlWorker);
1725
- return async function handleRequest(req, res) {
1789
+ const handleRequest = async (req, res) => {
1726
1790
  if (!req.url) {
1727
1791
  res.statusCode = 400;
1728
1792
  res.end("Bad request");
@@ -1786,6 +1850,7 @@ function createNodeRequestHandler(options) {
1786
1850
  request: requestContext
1787
1851
  })
1788
1852
  );
1853
+ resolved2.head = { ...resolved2.head, basename: resolved2.head.basename ?? basePath };
1789
1854
  attachResolvedContextToCoreRequest(req, resolved2.context);
1790
1855
  const payload = {
1791
1856
  model: resolved2.model,
@@ -1861,6 +1926,7 @@ function createNodeRequestHandler(options) {
1861
1926
  )
1862
1927
  })
1863
1928
  );
1929
+ resolved.head = { ...resolved.head, basename: resolved.head.basename ?? basePath };
1864
1930
  attachResolvedContextToCoreRequest(req, resolved.context);
1865
1931
  const initialPayload = {
1866
1932
  model: resolved.model,
@@ -1946,8 +2012,31 @@ function createNodeRequestHandler(options) {
1946
2012
  }
1947
2013
  );
1948
2014
  };
2015
+ return (req, res) => withRequestBasename(basePath, () => handleRequest(req, res));
2016
+ }
2017
+ var standaloneHtmlWorker;
2018
+ async function renderReactToHtml(element) {
2019
+ if (!standaloneHtmlWorker) {
2020
+ standaloneHtmlWorker = createInitialHtmlWorker(process.cwd());
2021
+ process.once("exit", disposeReactHtmlRenderer);
2022
+ }
2023
+ const flightData = await renderRSCToString({ model: element }, {
2024
+ moduleMap: {},
2025
+ onError: (error) => {
2026
+ console.error("[webframez-react] HTML render failed", error);
2027
+ }
2028
+ });
2029
+ return standaloneHtmlWorker.renderFromFlightData({ flightData, moduleMap: {} });
2030
+ }
2031
+ function disposeReactHtmlRenderer() {
2032
+ process.removeListener("exit", disposeReactHtmlRenderer);
2033
+ standaloneHtmlWorker?.dispose();
2034
+ standaloneHtmlWorker = void 0;
1949
2035
  }
1950
2036
 
2037
+ // src/paths.ts
2038
+ var import_routing = __toESM(require_routing2(), 1);
2039
+
1951
2040
  // src/webframez-core.ts
1952
2041
  var import_node_path4 = __toESM(require("node:path"), 1);
1953
2042
  function normalizeMountPath(path5) {
@@ -2035,6 +2124,17 @@ function buildRenderDefaults(routePathValue) {
2035
2124
  clientScriptUrl: `${mountPath}/assets/client.js`
2036
2125
  };
2037
2126
  }
2127
+ function runtimeRoutePaths(routePath, options, basename = (0, import_routing.getBasename)(), groupPrefix = "") {
2128
+ const localMount = normalizeMountPath(options.basePath ?? `${groupPrefix}${normalizeMountPath(routePath)}`);
2129
+ const basePath = normalizeMountPath((0, import_routing.appPath)(localMount, basename)).replace(/\/$/, "");
2130
+ return {
2131
+ basePath,
2132
+ assetsPrefix: options.assetsPrefix !== void 0 ? (0, import_routing.appPath)(options.assetsPrefix, basename) : `${basePath}/assets/`,
2133
+ rscPath: options.rscPath !== void 0 ? (0, import_routing.appPath)(options.rscPath, basename) : `${basePath}/rsc`,
2134
+ clientScriptUrl: options.clientScriptUrl !== void 0 ? (0, import_routing.appPath)(options.clientScriptUrl, basename) : `${basePath}/assets/client.js`,
2135
+ ...typeof options.liveReloadPath === "string" ? { liveReloadPath: (0, import_routing.appPath)(options.liveReloadPath, basename) } : {}
2136
+ };
2137
+ }
2038
2138
  async function waitForResponseFinish(res) {
2039
2139
  if (res.writableEnded || res.destroyed) {
2040
2140
  return;
@@ -2128,10 +2228,7 @@ function registerRouteRenderer(route, methodName) {
2128
2228
  distRootDir: resolvedTarget.distRootDir,
2129
2229
  pagesDir: resolvedTarget.pagesDir,
2130
2230
  manifestPath: resolvedTarget.manifestPath,
2131
- basePath: basePath ?? defaults.basePath,
2132
- assetsPrefix: assetsPrefix ?? defaults.assetsPrefix,
2133
- rscPath: rscPath ?? defaults.rscPath,
2134
- clientScriptUrl: clientScriptUrl ?? defaults.clientScriptUrl
2231
+ ...runtimeRoutePaths(routePathValue, options, route.basename ?? (0, import_routing.getBasename)(), route.tempGroupPrefix ?? "")
2135
2232
  });
2136
2233
  const methods = normalizeMethods(method);
2137
2234
  for (const currentMethod of methods) {
@@ -2173,13 +2270,10 @@ function resolveWebframezReactRouteOptions(routePathValue, options = {}) {
2173
2270
  distRootDir,
2174
2271
  pagesDir,
2175
2272
  manifestPath,
2176
- assetsPrefix: options.assetsPrefix ?? defaults.assetsPrefix,
2177
- rscPath: options.rscPath ?? defaults.rscPath,
2178
- clientScriptUrl: options.clientScriptUrl ?? defaults.clientScriptUrl,
2273
+ ...runtimeRoutePaths(routePathValue, options),
2179
2274
  clientEntryPath: options.clientEntryPath ? resolveFromProjectRoot(options.clientEntryPath) : void 0,
2180
2275
  styleSrcPath: options.styleSrcPath ? resolveFromProjectRoot(options.styleSrcPath) : defaults.styleSrcPath,
2181
- basePath: options.basePath ?? defaults.basePath,
2182
- liveReloadPath: options.liveReloadPath,
2276
+ liveReloadPath: typeof options.liveReloadPath === "string" ? (0, import_routing.appPath)(options.liveReloadPath) : options.liveReloadPath,
2183
2277
  onData: options.onData
2184
2278
  };
2185
2279
  }
@@ -2212,10 +2306,12 @@ var setupWebframezCoreReactRoute = initWebframezReact;
2212
2306
  createHTMLShell,
2213
2307
  createNodeRequestHandler,
2214
2308
  createRSCHandler,
2309
+ disposeReactHtmlRenderer,
2215
2310
  getRegisteredReactBuildTargets,
2216
2311
  initWebframezReact,
2217
2312
  parseSearchParams,
2218
2313
  renderHeadToString,
2314
+ renderReactToHtml,
2219
2315
  resolveWebframezReactRouteOptions,
2220
2316
  sendRSC,
2221
2317
  setupWebframezCoreReactRoute
package/dist/index.d.ts CHANGED
@@ -25,3 +25,5 @@ export function createRSCHandler(options: {
25
25
  export function createNodeRequestHandler(
26
26
  options: CreateNodeHandlerOptions
27
27
  ): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
28
+
29
+ export { renderReactToHtml, disposeReactHtmlRenderer } from "./http";