@taujs/server 0.1.9 → 0.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/server",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "description": "taujs | τjs",
5
5
  "author": "Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",
@@ -58,7 +58,7 @@
58
58
  "react-dom": "^19.0.0",
59
59
  "tsup": "^8.2.4",
60
60
  "typescript": "^5.5.4",
61
- "vite": "^5.4.2",
61
+ "vite": "^6.3.5",
62
62
  "vitest": "^2.0.5"
63
63
  },
64
64
  "peerDependencies": {
@@ -66,7 +66,7 @@
66
66
  "react": "^19.0.0",
67
67
  "react-dom": "^19.0.0",
68
68
  "typescript": "^5.5.4",
69
- "vite": "^6.2.5"
69
+ "vite": "^6.3.5"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "tsup",
package/dist/data.d.ts DELETED
@@ -1,49 +0,0 @@
1
- import React from 'react';
2
- import { ServerResponse } from 'node:http';
3
-
4
- type SSRStore<T> = {
5
- getSnapshot: () => T;
6
- getServerSnapshot: () => T;
7
- setData: (newData: T) => void;
8
- subscribe: (callback: () => void) => () => void;
9
- };
10
- declare const createSSRStore: <T>(initialDataOrPromise: T | Promise<T>) => SSRStore<T>;
11
- declare const SSRStoreProvider: React.FC<React.PropsWithChildren<{
12
- store: SSRStore<Record<string, unknown>>;
13
- }>>;
14
- declare const useSSRStore: <T>() => T;
15
-
16
- type HydrateAppOptions = {
17
- appComponent: React.ReactElement;
18
- initialDataKey?: keyof Window;
19
- rootElementId?: string;
20
- debug?: boolean;
21
- };
22
- declare const hydrateApp: ({ appComponent, initialDataKey, rootElementId, debug }: HydrateAppOptions) => void;
23
-
24
- type RendererOptions = {
25
- appComponent: (props: {
26
- location: string;
27
- }) => React.ReactElement;
28
- headContent: string | ((data: Record<string, unknown>) => string);
29
- };
30
- type RenderCallbacks = {
31
- onHead: (headContent: string) => void;
32
- onFinish: (initialDataResolved: unknown) => void;
33
- onError: (error: unknown) => void;
34
- };
35
- declare const resolveHeadContent: (headContent: string | ((meta: Record<string, unknown>) => string), meta?: Record<string, unknown>) => string;
36
- declare const createRenderer: ({ appComponent, headContent }: RendererOptions) => {
37
- renderSSR: (initialDataResolved: Record<string, unknown>, location: string, meta?: Record<string, unknown>) => Promise<{
38
- headContent: string;
39
- appHtml: string;
40
- }>;
41
- renderStream: (serverResponse: ServerResponse, callbacks: RenderCallbacks, initialDataResolved: Record<string, unknown>, location: string, bootstrapModules?: string, meta?: Record<string, unknown>) => void;
42
- };
43
- declare const createRenderStream: (serverResponse: ServerResponse, { onHead, onFinish, onError }: RenderCallbacks, { appComponent, headContent, initialDataResolved, location, bootstrapModules, }: RendererOptions & {
44
- initialDataResolved: Record<string, unknown>;
45
- location: string;
46
- bootstrapModules?: string;
47
- }) => void;
48
-
49
- export { type SSRStore, SSRStoreProvider, createRenderStream, createRenderer, createSSRStore, hydrateApp, resolveHeadContent, useSSRStore };
package/dist/data.js DELETED
@@ -1,182 +0,0 @@
1
- // src/SSRDataStore.tsx
2
- import { createContext, useContext, useSyncExternalStore } from "react";
3
- import { jsx } from "react/jsx-runtime";
4
- var createSSRStore = (initialDataOrPromise) => {
5
- let currentData;
6
- let status;
7
- const subscribers = /* @__PURE__ */ new Set();
8
- let serverDataPromise;
9
- if (initialDataOrPromise instanceof Promise) {
10
- status = "pending";
11
- serverDataPromise = initialDataOrPromise.then((data) => {
12
- currentData = data;
13
- status = "success";
14
- subscribers.forEach((callback) => callback());
15
- }).catch((error) => {
16
- console.error("Failed to load initial data:", error);
17
- status = "error";
18
- }).then(() => {
19
- });
20
- } else {
21
- currentData = initialDataOrPromise;
22
- status = "success";
23
- serverDataPromise = Promise.resolve();
24
- }
25
- const setData = (newData) => {
26
- currentData = newData;
27
- status = "success";
28
- subscribers.forEach((callback) => callback());
29
- };
30
- const subscribe = (callback) => {
31
- subscribers.add(callback);
32
- return () => subscribers.delete(callback);
33
- };
34
- const getSnapshot = () => {
35
- if (status === "pending") {
36
- throw serverDataPromise;
37
- } else if (status === "error") {
38
- throw new Error("An error occurred while fetching the data.");
39
- }
40
- return currentData;
41
- };
42
- const getServerSnapshot = () => {
43
- if (status === "pending") {
44
- throw serverDataPromise;
45
- } else if (status === "error") {
46
- throw new Error("Data is not available on the server.");
47
- }
48
- return currentData;
49
- };
50
- return { getSnapshot, getServerSnapshot, setData, subscribe };
51
- };
52
- var SSRStoreContext = createContext(null);
53
- var SSRStoreProvider = ({ store, children }) => /* @__PURE__ */ jsx(SSRStoreContext.Provider, { value: store, children });
54
- var useSSRStore = () => {
55
- const store = useContext(SSRStoreContext);
56
- if (!store) throw new Error("useSSRStore must be used within a SSRStoreProvider");
57
- return useSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot);
58
- };
59
-
60
- // src/SSRHydration.tsx
61
- import React2 from "react";
62
- import { createRoot, hydrateRoot } from "react-dom/client";
63
-
64
- // src/utils/Logger.ts
65
- var createLogger = (debug) => ({
66
- log: (...args) => {
67
- if (debug) console.log(...args);
68
- },
69
- warn: (...args) => {
70
- if (debug) console.warn(...args);
71
- },
72
- error: (...args) => {
73
- if (debug) console.error(...args);
74
- }
75
- });
76
-
77
- // src/SSRHydration.tsx
78
- import { jsx as jsx2 } from "react/jsx-runtime";
79
- var hydrateApp = ({ appComponent, initialDataKey = "__INITIAL_DATA__", rootElementId = "root", debug = false }) => {
80
- const { log, warn, error } = createLogger(debug);
81
- const bootstrap = () => {
82
- log("Hydration started");
83
- const rootElement = document.getElementById(rootElementId);
84
- if (!rootElement) {
85
- error(`Root element with id "${rootElementId}" not found.`);
86
- return;
87
- }
88
- const initialData = window[initialDataKey];
89
- if (!initialData) {
90
- warn(`Initial data key "${initialDataKey}" is undefined on window. Defaulting to SPA createRoot`);
91
- const root = createRoot(rootElement);
92
- root.render(/* @__PURE__ */ jsx2(React2.StrictMode, { children: appComponent }));
93
- } else {
94
- log("Initial data loaded:", initialData);
95
- const initialDataPromise = Promise.resolve(initialData);
96
- const store = createSSRStore(initialDataPromise);
97
- log("Store created:", store);
98
- hydrateRoot(
99
- rootElement,
100
- /* @__PURE__ */ jsx2(React2.StrictMode, { children: /* @__PURE__ */ jsx2(SSRStoreProvider, { store, children: appComponent }) })
101
- );
102
- log("Hydration completed");
103
- }
104
- };
105
- if (document.readyState !== "loading") {
106
- bootstrap();
107
- } else {
108
- document.addEventListener("DOMContentLoaded", bootstrap);
109
- }
110
- };
111
-
112
- // src/SSRRender.tsx
113
- import "http";
114
- import { Writable } from "stream";
115
- import "react";
116
- import { renderToPipeableStream, renderToString } from "react-dom/server";
117
- import { jsx as jsx3 } from "react/jsx-runtime";
118
- var resolveHeadContent = (headContent, meta = {}) => typeof headContent === "function" ? headContent(meta) : headContent;
119
- var createRenderer = ({ appComponent, headContent }) => {
120
- const renderSSR = async (initialDataResolved, location, meta = {}) => {
121
- const dataForHeadContent = Object.keys(initialDataResolved).length > 0 ? initialDataResolved : meta;
122
- const dynamicHeadContent = resolveHeadContent(headContent, dataForHeadContent);
123
- const appHtml = renderToString(/* @__PURE__ */ jsx3(SSRStoreProvider, { store: createSSRStore(initialDataResolved), children: appComponent({ location }) }));
124
- return {
125
- headContent: dynamicHeadContent,
126
- appHtml
127
- };
128
- };
129
- const renderStream = (serverResponse, callbacks, initialDataResolved, location, bootstrapModules, meta = {}) => {
130
- const dynamicHeadContent = resolveHeadContent(headContent, meta);
131
- createRenderStream(serverResponse, callbacks, {
132
- appComponent: (props) => appComponent({ ...props, location }),
133
- headContent: dynamicHeadContent,
134
- initialDataResolved,
135
- location,
136
- bootstrapModules
137
- });
138
- };
139
- return { renderSSR, renderStream };
140
- };
141
- var createRenderStream = (serverResponse, { onHead, onFinish, onError }, {
142
- appComponent,
143
- headContent,
144
- initialDataResolved,
145
- location,
146
- bootstrapModules
147
- }) => {
148
- const store = createSSRStore(initialDataResolved);
149
- const appElement = /* @__PURE__ */ jsx3(SSRStoreProvider, { store, children: appComponent({ location }) });
150
- const { pipe } = renderToPipeableStream(appElement, {
151
- bootstrapModules: bootstrapModules ? [bootstrapModules] : void 0,
152
- onShellReady() {
153
- const dynamicHeadContent = resolveHeadContent(headContent, initialDataResolved);
154
- onHead(dynamicHeadContent);
155
- pipe(
156
- new Writable({
157
- write(chunk, _encoding, callback) {
158
- serverResponse.write(chunk, callback);
159
- },
160
- final(callback) {
161
- onFinish(store.getSnapshot());
162
- callback();
163
- }
164
- })
165
- );
166
- },
167
- onAllReady() {
168
- },
169
- onError(error) {
170
- onError(error);
171
- }
172
- });
173
- };
174
- export {
175
- SSRStoreProvider,
176
- createRenderStream,
177
- createRenderer,
178
- createSSRStore,
179
- hydrateApp,
180
- resolveHeadContent,
181
- useSSRStore
182
- };