@taujs/server 0.0.5 → 0.0.7

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.
@@ -21,7 +21,7 @@ type FetchConfig = {
21
21
  serviceMethod?: string;
22
22
  };
23
23
  type SSRServerOptions = {
24
- alias: Record<string, string>;
24
+ alias?: Record<string, string>;
25
25
  clientRoot: string;
26
26
  clientHtmlTemplate: string;
27
27
  clientEntryClient: string;
@@ -56,7 +56,7 @@ type RouteAttributes<Params = {}> = {
56
56
  }>;
57
57
  };
58
58
  type Route<Params = {}> = {
59
- attributes: RouteAttributes<Params>;
59
+ attributes?: RouteAttributes<Params>;
60
60
  path: string;
61
61
  };
62
62
  interface InitialRouteParams extends Record<string, unknown> {
package/dist/SSRServer.js CHANGED
@@ -117,7 +117,7 @@ var require_plugin = __commonJS({
117
117
 
118
118
  // src/SSRServer.ts
119
119
  var import_fastify_plugin = __toESM(require_plugin(), 1);
120
- import fs from "node:fs/promises";
120
+ import { readFile } from "node:fs/promises";
121
121
  import path from "node:path";
122
122
  import { createViteRuntime } from "vite";
123
123
 
@@ -171,7 +171,6 @@ function renderPreloadLinks(modules, manifest) {
171
171
  }
172
172
  function renderPreloadLink(file) {
173
173
  const fileType = file.match(/\.(js|css|woff2?|gif|jpe?g|png|svg)$/)?.[1];
174
- if (!fileType) return "";
175
174
  switch (fileType) {
176
175
  case "js":
177
176
  return `<link rel="modulepreload" href="${file}">`;
@@ -212,6 +211,23 @@ var fetchData = async ({ url, options }) => {
212
211
  }
213
212
  throw new Error("URL must be provided to fetch data");
214
213
  };
214
+ var fetchInitialData = async (attributes, params, serviceRegistry) => {
215
+ if (attributes && typeof attributes.fetch === "function") {
216
+ return attributes.fetch(params, {
217
+ headers: { "Content-Type": "application/json" },
218
+ params
219
+ }).then(async (data) => {
220
+ if (data.serviceName && data.serviceMethod) {
221
+ return await callServiceMethod(serviceRegistry, data.serviceName, data.serviceMethod, data.options?.params ?? {});
222
+ }
223
+ return await fetchData(data);
224
+ }).catch((error) => {
225
+ console.error("Error fetching initial data:", error);
226
+ throw error;
227
+ });
228
+ }
229
+ return Promise.resolve({});
230
+ };
215
231
  var matchRoute = (url, renderRoutes) => {
216
232
  for (const route of renderRoutes) {
217
233
  const matcher = match(route.path, {
@@ -246,12 +262,12 @@ var SSRServer = (0, import_fastify_plugin.default)(
246
262
  async (app, opts) => {
247
263
  const { alias, clientRoot, clientHtmlTemplate, clientEntryClient, clientEntryServer, routes, serviceRegistry, isDebug } = opts;
248
264
  const templateHtmlPath = path.join(clientRoot, clientHtmlTemplate);
249
- const templateHtml = !isDevelopment ? await fs.readFile(templateHtmlPath, "utf-8") : await fs.readFile(path.join(clientRoot, clientHtmlTemplate), "utf-8");
265
+ const templateHtml = !isDevelopment ? await readFile(templateHtmlPath, "utf-8") : await readFile(path.join(clientRoot, clientHtmlTemplate), "utf-8");
250
266
  const ssrManifestPath = path.join(clientRoot, ".vite/ssr-manifest.json");
251
- const ssrManifest = !isDevelopment ? JSON.parse(await fs.readFile(ssrManifestPath, "utf-8")) : void 0;
267
+ const ssrManifest = !isDevelopment ? JSON.parse(await readFile(ssrManifestPath, "utf-8")) : void 0;
252
268
  const manifestPath = path.join(clientRoot, ".vite/manifest.json");
253
- const manifest = !isDevelopment ? JSON.parse(await fs.readFile(manifestPath, "utf-8")) : void 0;
254
- const bootstrapModules = isDevelopment ? `/${clientEntryClient}.tsx` : `/${manifest[`${clientEntryClient}.tsx`].file}`;
269
+ const manifest = !isDevelopment ? JSON.parse(await readFile(manifestPath, "utf-8")) : void 0;
270
+ const bootstrapModules = isDevelopment ? `/${clientEntryClient}.tsx` : `/${manifest[`${clientEntryClient}.tsx`]?.file}`;
255
271
  const preloadLinks = !isDevelopment ? renderPreloadLinks(Object.keys(ssrManifest), ssrManifest) : void 0;
256
272
  const cssLinks = !isDevelopment ? getCssLinks(manifest) : void 0;
257
273
  let renderModule;
@@ -267,7 +283,6 @@ var SSRServer = (0, import_fastify_plugin.default)(
267
283
  plugins: [
268
284
  ...isDebug ? [
269
285
  {
270
- name: "taujs-ssr-server-debug-logging",
271
286
  configureServer(server) {
272
287
  console.log("\u03C4js debug ssr server started.");
273
288
  server.middlewares.use((req, res, next) => {
@@ -277,7 +292,8 @@ var SSRServer = (0, import_fastify_plugin.default)(
277
292
  });
278
293
  next();
279
294
  });
280
- }
295
+ },
296
+ name: "taujs-ssr-server-debug-logging"
281
297
  }
282
298
  ] : []
283
299
  ],
@@ -335,17 +351,7 @@ var SSRServer = (0, import_fastify_plugin.default)(
335
351
  const { attributes } = route;
336
352
  const [beforeBody = "", afterBody] = template.split("<!--ssr-html-->");
337
353
  const [beforeHead, afterHead] = beforeBody.split("<!--ssr-head-->");
338
- const initialDataPromise = attributes && typeof attributes.fetch === "function" ? attributes.fetch(params, {
339
- headers: { "Content-Type": "application/json" },
340
- params
341
- }).then((data) => {
342
- if (data.serviceName && data.serviceMethod)
343
- return callServiceMethod(serviceRegistry, data.serviceName, data.serviceMethod, data.options.params ?? {});
344
- return fetchData(data);
345
- }).catch((error) => {
346
- console.error("Error fetching initial data:", error);
347
- return {};
348
- }) : Promise.resolve({});
354
+ const initialDataPromise = fetchInitialData(attributes, params, serviceRegistry);
349
355
  reply.raw.writeHead(200, { "Content-Type": "text/html" });
350
356
  reply.raw.write(beforeHead);
351
357
  streamRender(
@@ -363,8 +369,7 @@ var SSRServer = (0, import_fastify_plugin.default)(
363
369
  reply.raw.end();
364
370
  },
365
371
  onError: (error) => {
366
- console.error("Critical rendering error:", error);
367
- if (!reply.raw.headersSent) reply.raw.writeHead(500, { "Content-Type": "text/plain" });
372
+ console.error("Critical rendering onError:", error);
368
373
  reply.raw.end("Internal Server Error");
369
374
  }
370
375
  },
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "type": "git",
21
21
  "url": "git+https://github.com/aoede3/taujs-server.git"
22
22
  },
23
- "version": "0.0.5",
23
+ "version": "0.0.7",
24
24
  "exports": {
25
25
  ".": {
26
26
  "import": "./dist/SSRServer.js",
@@ -60,10 +60,14 @@
60
60
  "@arethetypeswrong/cli": "^0.15.4",
61
61
  "@babel/preset-typescript": "^7.24.7",
62
62
  "@changesets/cli": "^2.27.7",
63
+ "@testing-library/dom": "^10.4.0",
64
+ "@testing-library/react": "^16.0.1",
63
65
  "@types/node": "^20.14.9",
64
66
  "@types/react": "^18.3.3",
65
67
  "@types/react-dom": "^18.3.0",
68
+ "@vitest/coverage-v8": "^2.1.0",
66
69
  "fastify": "^4.28.0",
70
+ "jsdom": "^25.0.0",
67
71
  "prettier": "^3.3.3",
68
72
  "react-dom": "^18.3.1",
69
73
  "tsup": "^8.2.4",
@@ -72,10 +76,8 @@
72
76
  "vitest": "^2.0.5"
73
77
  },
74
78
  "peerDependencies": {
75
- "@types/node": "^20.14.9",
76
- "@types/react": "^18.3.3",
77
79
  "@vitejs/plugin-react": "^4.3.1",
78
- "fastify": "^4.28.0",
80
+ "fastify": "^5.0.0",
79
81
  "react": "^18.3.1",
80
82
  "react-dom": "^18.3.1",
81
83
  "typescript": "^5.5.4",
@@ -86,6 +88,7 @@
86
88
  "ci": "npm run build && npm run check-format && npm run check-exports && npm run lint",
87
89
  "lint": "tsc",
88
90
  "test": "vitest run",
91
+ "coverage": "vitest run --coverage",
89
92
  "format": "prettier --write .",
90
93
  "check-format": "prettier --check .",
91
94
  "check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",