@taujs/server 0.4.8 → 0.5.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 (2) hide show
  1. package/dist/index.js +46 -42
  2. package/package.json +2 -1
package/dist/index.js CHANGED
@@ -99,7 +99,7 @@ var require_picocolors = __commonJS({
99
99
 
100
100
  // src/CreateServer.ts
101
101
  var import_picocolors4 = __toESM(require_picocolors(), 1);
102
- import path7 from "path";
102
+ import path5 from "path";
103
103
  import { performance as performance3 } from "perf_hooks";
104
104
  import Fastify from "fastify";
105
105
 
@@ -168,8 +168,8 @@ var extractRoutes = (taujsConfig) => {
168
168
  apps.push({ appId: app.appId, routeCount: appRoutes.length });
169
169
  allRoutes.push(...appRoutes);
170
170
  }
171
- for (const [path9, appIds] of pathTracker.entries()) {
172
- if (appIds.length > 1) warnings.push(`Route path "${path9}" is declared in multiple apps: ${appIds.join(", ")}`);
171
+ for (const [path7, appIds] of pathTracker.entries()) {
172
+ if (appIds.length > 1) warnings.push(`Route path "${path7}" is declared in multiple apps: ${appIds.join(", ")}`);
173
173
  }
174
174
  const sortedRoutes = allRoutes.sort((a, b) => computeScore(b.path) - computeScore(a.path));
175
175
  const durationMs = performance.now() - t0;
@@ -253,8 +253,8 @@ function printContractReport(logger, report) {
253
253
  }
254
254
  }
255
255
  }
256
- var computeScore = (path9) => {
257
- return path9.split("/").filter(Boolean).reduce((score, segment) => score + (segment.startsWith(":") ? 1 : 10), 0);
256
+ var computeScore = (path7) => {
257
+ return path7.split("/").filter(Boolean).reduce((score, segment) => score + (segment.startsWith(":") ? 1 : 10), 0);
258
258
  };
259
259
 
260
260
  // src/logging/AppError.ts
@@ -760,7 +760,6 @@ var verifyContracts = (app, routes, contracts, security) => {
760
760
  };
761
761
 
762
762
  // src/SSRServer.ts
763
- import path6 from "path";
764
763
  import fp3 from "fastify-plugin";
765
764
 
766
765
  // src/logging/utils/index.ts
@@ -858,15 +857,15 @@ var safeDecode = (value) => {
858
857
  return value;
859
858
  }
860
859
  };
861
- var cleanPath = (path9) => {
862
- if (!path9) return "/";
863
- const basePart = path9.split("?")[0];
860
+ var cleanPath = (path7) => {
861
+ if (!path7) return "/";
862
+ const basePart = path7.split("?")[0];
864
863
  const base = basePart ? basePart.split("#")[0] : "/";
865
864
  return base || "/";
866
865
  };
867
- var calculateSpecificity = (path9) => {
866
+ var calculateSpecificity = (path7) => {
868
867
  let score = 0;
869
- const segments = path9.split("/").filter(Boolean);
868
+ const segments = path7.split("/").filter(Boolean);
870
869
  for (const segment of segments) {
871
870
  if (segment.startsWith(":")) {
872
871
  score += 1;
@@ -891,9 +890,9 @@ var createRouteMatchers = (routes) => {
891
890
  });
892
891
  };
893
892
  var matchRoute = (url, routeMatchers) => {
894
- const path9 = cleanPath(url);
893
+ const path7 = cleanPath(url);
895
894
  for (const { route, matcher, keys } of routeMatchers) {
896
- const match2 = matcher(path9);
895
+ const match2 = matcher(path7);
897
896
  if (match2) {
898
897
  return {
899
898
  route,
@@ -1033,9 +1032,9 @@ var mergeDirectives = (base, override) => {
1033
1032
  }
1034
1033
  return merged;
1035
1034
  };
1036
- var findMatchingRoute = (routeMatchers, path9) => {
1035
+ var findMatchingRoute = (routeMatchers, path7) => {
1037
1036
  if (!routeMatchers) return null;
1038
- const match2 = matchRoute(path9, routeMatchers);
1037
+ const match2 = matchRoute(path7, routeMatchers);
1039
1038
  return match2 ? { route: match2.route, params: match2.params } : null;
1040
1039
  };
1041
1040
  var cspPlugin = fp(
@@ -1365,7 +1364,6 @@ var loadAssets = async (processedConfigs, baseClientRoot, bootstrapModules, cssL
1365
1364
  debug: opts.debug,
1366
1365
  includeContext: true
1367
1366
  });
1368
- const projectRoot = opts.projectRoot ?? path2.resolve(process.cwd());
1369
1367
  for (const config of processedConfigs) {
1370
1368
  const { clientRoot, entryClient, entryServer, htmlTemplate, entryPoint } = config;
1371
1369
  try {
@@ -1376,12 +1374,14 @@ var loadAssets = async (processedConfigs, baseClientRoot, bootstrapModules, cssL
1376
1374
  const adjustedRelativePath = relativeBasePath ? `/${relativeBasePath}` : "";
1377
1375
  if (!isDevelopment) {
1378
1376
  try {
1379
- const clientDistPath = path2.resolve(projectRoot, "client", entryPoint);
1377
+ const distRoot = path2.dirname(baseClientRoot);
1378
+ const ssrRoot = path2.join(distRoot, "ssr");
1379
+ const clientDistPath = path2.join(baseClientRoot, entryPoint);
1380
1380
  const manifestPath = path2.join(clientDistPath, ".vite/manifest.json");
1381
1381
  const manifestContent = await readFile(manifestPath, "utf-8");
1382
1382
  const manifest = JSON.parse(manifestContent);
1383
1383
  manifests.set(clientRoot, manifest);
1384
- const ssrDistPath = path2.resolve(projectRoot, "ssr", entryPoint);
1384
+ const ssrDistPath = path2.join(ssrRoot, entryPoint);
1385
1385
  const ssrManifestPath = path2.join(ssrDistPath, ".vite/ssr-manifest.json");
1386
1386
  const ssrManifestContent = await readFile(ssrManifestPath, "utf-8");
1387
1387
  const ssrManifest = JSON.parse(ssrManifestContent);
@@ -1902,7 +1902,6 @@ async function resolveRouteData(url, opts) {
1902
1902
  }
1903
1903
 
1904
1904
  // src/utils/StaticAssets.ts
1905
- import path5 from "path";
1906
1905
  function normaliseStaticAssets(reg) {
1907
1906
  if (!reg) return [];
1908
1907
  return Array.isArray(reg) ? reg : [reg];
@@ -1911,14 +1910,11 @@ function prefixWeight(prefix) {
1911
1910
  if (typeof prefix !== "string" || prefix === "/" || prefix.length === 0) return 0;
1912
1911
  return prefix.split("/").filter(Boolean).length;
1913
1912
  }
1914
- async function registerStaticAssets(app, baseClientRoot, reg, defaults, projectRoot) {
1915
- const isDevelopment2 = process.env.NODE_ENV === "development";
1916
- const effectiveProjectRoot = projectRoot ?? path5.resolve(process.cwd());
1917
- const staticRoot = isDevelopment2 ? baseClientRoot : path5.resolve(effectiveProjectRoot, "client");
1913
+ async function registerStaticAssets(app, clientRoot, reg, defaults) {
1918
1914
  const entries = normaliseStaticAssets(reg).map(({ plugin, options }) => ({
1919
1915
  plugin,
1920
1916
  options: {
1921
- root: staticRoot,
1917
+ root: clientRoot,
1922
1918
  prefix: "/",
1923
1919
  index: false,
1924
1920
  wildcard: false,
@@ -1935,7 +1931,7 @@ async function registerStaticAssets(app, baseClientRoot, reg, defaults, projectR
1935
1931
  // src/SSRServer.ts
1936
1932
  var SSRServer = fp3(
1937
1933
  async (app, opts) => {
1938
- const { alias, configs, routes, serviceRegistry = {}, clientRoot: baseClientRoot, security } = opts;
1934
+ const { alias, configs, routes, serviceRegistry = {}, clientRoot, security } = opts;
1939
1935
  const logger = createLogger({
1940
1936
  debug: opts.debug,
1941
1937
  context: { component: "ssr-server" },
@@ -1944,13 +1940,12 @@ var SSRServer = fp3(
1944
1940
  singleLine: true
1945
1941
  });
1946
1942
  const maps = createMaps();
1947
- const processedConfigs = processConfigs(configs, baseClientRoot, TEMPLATE);
1943
+ const processedConfigs = processConfigs(configs, clientRoot, TEMPLATE);
1948
1944
  const routeMatchers = createRouteMatchers(routes);
1949
1945
  let viteDevServer;
1950
- const projectRoot = path6.resolve(baseClientRoot, "..");
1951
1946
  await loadAssets(
1952
1947
  processedConfigs,
1953
- baseClientRoot,
1948
+ clientRoot,
1954
1949
  maps.bootstrapModules,
1955
1950
  maps.cssLinks,
1956
1951
  maps.manifests,
@@ -1960,11 +1955,14 @@ var SSRServer = fp3(
1960
1955
  maps.templates,
1961
1956
  {
1962
1957
  debug: opts.debug,
1963
- logger,
1964
- projectRoot
1958
+ logger
1965
1959
  }
1966
1960
  );
1967
- if (opts.staticAssets) await registerStaticAssets(app, baseClientRoot, opts.staticAssets, void 0, projectRoot);
1961
+ if (!isDevelopment && !opts.staticAssets) {
1962
+ const fastifyStatic = await import("@fastify/static");
1963
+ await registerStaticAssets(app, clientRoot, { plugin: fastifyStatic.default });
1964
+ }
1965
+ if (opts.staticAssets) await registerStaticAssets(app, clientRoot, opts.staticAssets);
1968
1966
  if (security?.csp?.reporting) {
1969
1967
  app.register(cspReportPlugin, {
1970
1968
  path: security.csp.reporting.endpoint,
@@ -1979,7 +1977,7 @@ var SSRServer = fp3(
1979
1977
  routeMatchers,
1980
1978
  debug: opts.debug
1981
1979
  });
1982
- if (isDevelopment) viteDevServer = await setupDevServer(app, baseClientRoot, alias, opts.debug, opts.devNet);
1980
+ if (isDevelopment) viteDevServer = await setupDevServer(app, clientRoot, alias, opts.debug, opts.devNet);
1983
1981
  app.addHook("onRequest", createAuthHook(routeMatchers, logger));
1984
1982
  app.get("/__taujs/data", async (req, reply) => {
1985
1983
  const query = req.query;
@@ -2051,9 +2049,15 @@ var SSRServer = fp3(
2051
2049
  );
2052
2050
 
2053
2051
  // src/CreateServer.ts
2052
+ var resolveClientRoot = (userClientRoot) => {
2053
+ if (userClientRoot) return path5.isAbsolute(userClientRoot) ? userClientRoot : path5.resolve(process.cwd(), userClientRoot);
2054
+ const cwd = process.cwd();
2055
+ if (process.env.NODE_ENV === "production") return path5.resolve(cwd, "dist/client");
2056
+ return path5.resolve(cwd, "src/client");
2057
+ };
2054
2058
  var createServer = async (opts) => {
2055
2059
  const t0 = performance3.now();
2056
- const clientRoot = opts.clientRoot ?? path7.resolve(process.cwd(), "client");
2060
+ const clientRoot = resolveClientRoot(opts.clientRoot);
2057
2061
  const app = opts.fastify ?? Fastify({ logger: false });
2058
2062
  const fastifyLogger = app.log && app.log.level && app.log.level !== "silent" ? app.log : void 0;
2059
2063
  const logger = createLogger({
@@ -2123,7 +2127,7 @@ ${import_picocolors4.default.bgGreen(import_picocolors4.default.black(` ${CONTEN
2123
2127
 
2124
2128
  // src/Build.ts
2125
2129
  import { existsSync } from "fs";
2126
- import path8 from "path";
2130
+ import path6 from "path";
2127
2131
  import { build } from "vite";
2128
2132
  function resolveInputs(isSSRBuild, mainExists, paths) {
2129
2133
  if (isSSRBuild) return { server: paths.server };
@@ -2295,7 +2299,7 @@ async function taujsBuild({
2295
2299
  }) {
2296
2300
  const deleteDist = async () => {
2297
2301
  const { rm } = await import("fs/promises");
2298
- const distPath = path8.resolve(projectRoot, "dist");
2302
+ const distPath = path6.resolve(projectRoot, "dist");
2299
2303
  try {
2300
2304
  await rm(distPath, { recursive: true, force: true });
2301
2305
  console.log("Deleted the dist directory\n");
@@ -2316,17 +2320,17 @@ async function taujsBuild({
2316
2320
  if (!isSSRBuild) await deleteDist();
2317
2321
  for (const appConfig of configsToBuild) {
2318
2322
  const { appId, entryPoint, clientRoot, entryClient, entryServer, htmlTemplate, plugins = [] } = appConfig;
2319
- const outDir = path8.resolve(projectRoot, isSSRBuild ? `dist/ssr/${entryPoint}` : `dist/client/${entryPoint}`);
2320
- const root = entryPoint ? path8.resolve(clientBaseDir, entryPoint) : clientBaseDir;
2323
+ const outDir = path6.resolve(projectRoot, isSSRBuild ? `dist/ssr/${entryPoint}` : `dist/client/${entryPoint}`);
2324
+ const root = entryPoint ? path6.resolve(clientBaseDir, entryPoint) : clientBaseDir;
2321
2325
  const defaultAlias = {
2322
2326
  "@client": root,
2323
- "@server": path8.resolve(projectRoot, "src/server"),
2324
- "@shared": path8.resolve(projectRoot, "src/shared")
2327
+ "@server": path6.resolve(projectRoot, "src/server"),
2328
+ "@shared": path6.resolve(projectRoot, "src/shared")
2325
2329
  };
2326
2330
  const resolvedAlias = { ...defaultAlias, ...userAlias ?? {} };
2327
- const server = path8.resolve(clientRoot, `${entryServer}.tsx`);
2328
- const client = path8.resolve(clientRoot, `${entryClient}.tsx`);
2329
- const main = path8.resolve(clientRoot, htmlTemplate);
2331
+ const server = path6.resolve(clientRoot, `${entryServer}.tsx`);
2332
+ const client = path6.resolve(clientRoot, `${entryClient}.tsx`);
2333
+ const main = path6.resolve(clientRoot, htmlTemplate);
2330
2334
  const inputs = resolveInputs(isSSRBuild, !isSSRBuild && existsSync(main), { server, client, main });
2331
2335
  const nodeVersion = process.versions.node.split(".")[0];
2332
2336
  const frameworkConfig = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/server",
3
- "version": "0.4.8",
3
+ "version": "0.5.0",
4
4
  "description": "τjs [ taujs ]",
5
5
  "author": "John Smith | Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",
@@ -44,6 +44,7 @@
44
44
  "dist"
45
45
  ],
46
46
  "dependencies": {
47
+ "@fastify/static": "^8.3.0",
47
48
  "fastify": "^5.6.1",
48
49
  "fastify-plugin": "^5.1.0",
49
50
  "path-to-regexp": "^8.1.0",