@matthesketh/utopia-server 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matt Hesketh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # @matthesketh/utopia-server
2
+
3
+ Server-side rendering for UtopiaJS. Provides `renderToString`, `renderToStream`, server-side routing, and an HTTP request handler. The SSR runtime is a drop-in replacement for `@matthesketh/utopia-runtime` that builds VNode trees instead of real DOM.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @matthesketh/utopia-server
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { renderToString } from '@matthesketh/utopia-server';
15
+ import App from './App.utopia';
16
+
17
+ const { html, css } = renderToString(App, { title: 'Hello' });
18
+ ```
19
+
20
+ Streaming:
21
+
22
+ ```ts
23
+ import { renderToStream } from '@matthesketh/utopia-server';
24
+ import App from './App.utopia';
25
+
26
+ const stream = renderToStream(App);
27
+ stream.pipe(res);
28
+ ```
29
+
30
+ ## API
31
+
32
+ | Export | Description |
33
+ |--------|-------------|
34
+ | `renderToString(component, props?)` | Render a component to `{ html, css }` synchronously. |
35
+ | `renderToStream(component, props?)` | Render to a Node.js `Readable` stream. |
36
+ | `createServerRouter(routes, url)` | Match a URL against routes on the server. |
37
+ | `createHandler(options)` | Create a Node.js HTTP request handler for SSR. |
38
+
39
+ **Subpath export:** `@matthesketh/utopia-server/ssr-runtime` -- drop-in replacement for `@matthesketh/utopia-runtime` used during SSR builds. The Vite plugin handles this swap automatically.
40
+
41
+ **VNode types:** `VElement`, `VText`, `VComment`, `VNode`.
42
+
43
+ See [docs/ssr.md](../../docs/ssr.md) for the full SSR architecture, hydration details, and setup guide.
44
+
45
+ ## License
46
+
47
+ MIT
@@ -0,0 +1,216 @@
1
+ // src/ssr-runtime.ts
2
+ import { signal, computed, batch, untrack } from "@matthesketh/utopia-core";
3
+ var collectedStyles = [];
4
+ function flushStyles() {
5
+ const styles = collectedStyles;
6
+ collectedStyles = [];
7
+ return styles;
8
+ }
9
+ function createElement(tag) {
10
+ return { type: 1, tag, attrs: {}, children: [] };
11
+ }
12
+ function createTextNode(text) {
13
+ return { type: 2, text: String(text) };
14
+ }
15
+ function createComment(text) {
16
+ return { type: 3, text };
17
+ }
18
+ function setText(node, value) {
19
+ node.text = value == null ? "" : String(value);
20
+ }
21
+ function setAttr(el, name, value) {
22
+ if (name === "class") {
23
+ if (value == null || value === false) {
24
+ delete el.attrs["class"];
25
+ } else if (typeof value === "string") {
26
+ el.attrs["class"] = value;
27
+ } else if (typeof value === "object") {
28
+ const classes = [];
29
+ for (const key of Object.keys(value)) {
30
+ if (value[key]) classes.push(key);
31
+ }
32
+ el.attrs["class"] = classes.join(" ");
33
+ }
34
+ return;
35
+ }
36
+ if (name === "style") {
37
+ if (value == null || value === false) {
38
+ delete el.attrs["style"];
39
+ } else if (typeof value === "string") {
40
+ el.attrs["style"] = value;
41
+ } else if (typeof value === "object") {
42
+ const parts = [];
43
+ for (const prop of Object.keys(value)) {
44
+ if (value[prop] != null) {
45
+ const cssName = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
46
+ parts.push(`${cssName}: ${value[prop]}`);
47
+ }
48
+ }
49
+ el.attrs["style"] = parts.join("; ");
50
+ }
51
+ return;
52
+ }
53
+ const BOOLEAN_ATTRS = /* @__PURE__ */ new Set([
54
+ "disabled",
55
+ "checked",
56
+ "readonly",
57
+ "hidden",
58
+ "selected",
59
+ "required",
60
+ "multiple",
61
+ "autofocus",
62
+ "autoplay",
63
+ "controls",
64
+ "loop",
65
+ "muted",
66
+ "open",
67
+ "novalidate"
68
+ ]);
69
+ if (BOOLEAN_ATTRS.has(name)) {
70
+ if (value) {
71
+ el.attrs[name] = "";
72
+ } else {
73
+ delete el.attrs[name];
74
+ }
75
+ return;
76
+ }
77
+ if (value == null || value === false) {
78
+ delete el.attrs[name];
79
+ } else {
80
+ el.attrs[name] = value === true ? "" : String(value);
81
+ }
82
+ }
83
+ function addEventListener(_el, _event, _handler) {
84
+ return () => {
85
+ };
86
+ }
87
+ function appendChild(parent, child) {
88
+ child._parent = parent;
89
+ parent.children.push(child);
90
+ }
91
+ function insertBefore(parent, node, anchor) {
92
+ node._parent = parent;
93
+ if (anchor === null) {
94
+ parent.children.push(node);
95
+ return;
96
+ }
97
+ const idx = parent.children.indexOf(anchor);
98
+ if (idx === -1) {
99
+ parent.children.push(node);
100
+ } else {
101
+ parent.children.splice(idx, 0, node);
102
+ }
103
+ }
104
+ function removeNode(node) {
105
+ if (node._parent) {
106
+ const idx = node._parent.children.indexOf(node);
107
+ if (idx !== -1) {
108
+ node._parent.children.splice(idx, 1);
109
+ }
110
+ node._parent = void 0;
111
+ }
112
+ }
113
+ function effect(fn) {
114
+ untrack(() => fn());
115
+ return () => {
116
+ };
117
+ }
118
+ function createEffect(fn) {
119
+ return effect(fn);
120
+ }
121
+ function createIf(anchor, condition, renderTrue, renderFalse) {
122
+ const parent = anchor._parent;
123
+ if (!parent) return () => {
124
+ };
125
+ const truthy = !!untrack(condition);
126
+ if (truthy) {
127
+ const node = untrack(renderTrue);
128
+ insertBefore(parent, node, anchor);
129
+ } else if (renderFalse) {
130
+ const node = untrack(renderFalse);
131
+ insertBefore(parent, node, anchor);
132
+ }
133
+ return () => {
134
+ };
135
+ }
136
+ function createFor(anchor, list, renderItem) {
137
+ const parent = anchor._parent;
138
+ if (!parent) return () => {
139
+ };
140
+ const items = untrack(list);
141
+ for (let i = 0; i < items.length; i++) {
142
+ const node = untrack(() => renderItem(items[i], i));
143
+ insertBefore(parent, node, anchor);
144
+ }
145
+ return () => {
146
+ };
147
+ }
148
+ function createComponent(Component, props, children) {
149
+ const ctx = Component.setup ? untrack(() => Component.setup(props ?? {})) : {};
150
+ const renderCtx = {
151
+ ...ctx,
152
+ $slots: children ?? {}
153
+ };
154
+ const el = untrack(() => Component.render(renderCtx));
155
+ if (Component.styles) {
156
+ collectedStyles.push(Component.styles);
157
+ }
158
+ return el;
159
+ }
160
+ function createComponentInstance(definition, props) {
161
+ return {
162
+ el: null,
163
+ props: props ?? {},
164
+ slots: {},
165
+ mount(_target) {
166
+ const ctx = definition.setup ? untrack(() => definition.setup(this.props)) : {};
167
+ const renderCtx = {
168
+ ...ctx,
169
+ $slots: this.slots
170
+ };
171
+ this.el = untrack(() => definition.render(renderCtx));
172
+ if (definition.styles) {
173
+ collectedStyles.push(definition.styles);
174
+ }
175
+ },
176
+ unmount() {
177
+ this.el = null;
178
+ }
179
+ };
180
+ }
181
+ function mount(component, _target) {
182
+ const instance = createComponentInstance(component);
183
+ instance.mount(null);
184
+ return instance;
185
+ }
186
+ function queueJob(_fn) {
187
+ }
188
+ function nextTick() {
189
+ return Promise.resolve();
190
+ }
191
+
192
+ export {
193
+ signal,
194
+ computed,
195
+ batch,
196
+ untrack,
197
+ flushStyles,
198
+ createElement,
199
+ createTextNode,
200
+ createComment,
201
+ setText,
202
+ setAttr,
203
+ addEventListener,
204
+ appendChild,
205
+ insertBefore,
206
+ removeNode,
207
+ effect,
208
+ createEffect,
209
+ createIf,
210
+ createFor,
211
+ createComponent,
212
+ createComponentInstance,
213
+ mount,
214
+ queueJob,
215
+ nextTick
216
+ };
@@ -0,0 +1,216 @@
1
+ // src/ssr-runtime.ts
2
+ import { signal, computed, batch, untrack } from "@utopia/core";
3
+ var collectedStyles = [];
4
+ function flushStyles() {
5
+ const styles = collectedStyles;
6
+ collectedStyles = [];
7
+ return styles;
8
+ }
9
+ function createElement(tag) {
10
+ return { type: 1, tag, attrs: {}, children: [] };
11
+ }
12
+ function createTextNode(text) {
13
+ return { type: 2, text: String(text) };
14
+ }
15
+ function createComment(text) {
16
+ return { type: 3, text };
17
+ }
18
+ function setText(node, value) {
19
+ node.text = value == null ? "" : String(value);
20
+ }
21
+ function setAttr(el, name, value) {
22
+ if (name === "class") {
23
+ if (value == null || value === false) {
24
+ delete el.attrs["class"];
25
+ } else if (typeof value === "string") {
26
+ el.attrs["class"] = value;
27
+ } else if (typeof value === "object") {
28
+ const classes = [];
29
+ for (const key of Object.keys(value)) {
30
+ if (value[key]) classes.push(key);
31
+ }
32
+ el.attrs["class"] = classes.join(" ");
33
+ }
34
+ return;
35
+ }
36
+ if (name === "style") {
37
+ if (value == null || value === false) {
38
+ delete el.attrs["style"];
39
+ } else if (typeof value === "string") {
40
+ el.attrs["style"] = value;
41
+ } else if (typeof value === "object") {
42
+ const parts = [];
43
+ for (const prop of Object.keys(value)) {
44
+ if (value[prop] != null) {
45
+ const cssName = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
46
+ parts.push(`${cssName}: ${value[prop]}`);
47
+ }
48
+ }
49
+ el.attrs["style"] = parts.join("; ");
50
+ }
51
+ return;
52
+ }
53
+ const BOOLEAN_ATTRS = /* @__PURE__ */ new Set([
54
+ "disabled",
55
+ "checked",
56
+ "readonly",
57
+ "hidden",
58
+ "selected",
59
+ "required",
60
+ "multiple",
61
+ "autofocus",
62
+ "autoplay",
63
+ "controls",
64
+ "loop",
65
+ "muted",
66
+ "open",
67
+ "novalidate"
68
+ ]);
69
+ if (BOOLEAN_ATTRS.has(name)) {
70
+ if (value) {
71
+ el.attrs[name] = "";
72
+ } else {
73
+ delete el.attrs[name];
74
+ }
75
+ return;
76
+ }
77
+ if (value == null || value === false) {
78
+ delete el.attrs[name];
79
+ } else {
80
+ el.attrs[name] = value === true ? "" : String(value);
81
+ }
82
+ }
83
+ function addEventListener(_el, _event, _handler) {
84
+ return () => {
85
+ };
86
+ }
87
+ function appendChild(parent, child) {
88
+ child._parent = parent;
89
+ parent.children.push(child);
90
+ }
91
+ function insertBefore(parent, node, anchor) {
92
+ node._parent = parent;
93
+ if (anchor === null) {
94
+ parent.children.push(node);
95
+ return;
96
+ }
97
+ const idx = parent.children.indexOf(anchor);
98
+ if (idx === -1) {
99
+ parent.children.push(node);
100
+ } else {
101
+ parent.children.splice(idx, 0, node);
102
+ }
103
+ }
104
+ function removeNode(node) {
105
+ if (node._parent) {
106
+ const idx = node._parent.children.indexOf(node);
107
+ if (idx !== -1) {
108
+ node._parent.children.splice(idx, 1);
109
+ }
110
+ node._parent = void 0;
111
+ }
112
+ }
113
+ function effect(fn) {
114
+ untrack(() => fn());
115
+ return () => {
116
+ };
117
+ }
118
+ function createEffect(fn) {
119
+ return effect(fn);
120
+ }
121
+ function createIf(anchor, condition, renderTrue, renderFalse) {
122
+ const parent = anchor._parent;
123
+ if (!parent) return () => {
124
+ };
125
+ const truthy = !!untrack(condition);
126
+ if (truthy) {
127
+ const node = untrack(renderTrue);
128
+ insertBefore(parent, node, anchor);
129
+ } else if (renderFalse) {
130
+ const node = untrack(renderFalse);
131
+ insertBefore(parent, node, anchor);
132
+ }
133
+ return () => {
134
+ };
135
+ }
136
+ function createFor(anchor, list, renderItem) {
137
+ const parent = anchor._parent;
138
+ if (!parent) return () => {
139
+ };
140
+ const items = untrack(list);
141
+ for (let i = 0; i < items.length; i++) {
142
+ const node = untrack(() => renderItem(items[i], i));
143
+ insertBefore(parent, node, anchor);
144
+ }
145
+ return () => {
146
+ };
147
+ }
148
+ function createComponent(Component, props, children) {
149
+ const ctx = Component.setup ? untrack(() => Component.setup(props ?? {})) : {};
150
+ const renderCtx = {
151
+ ...ctx,
152
+ $slots: children ?? {}
153
+ };
154
+ const el = untrack(() => Component.render(renderCtx));
155
+ if (Component.styles) {
156
+ collectedStyles.push(Component.styles);
157
+ }
158
+ return el;
159
+ }
160
+ function createComponentInstance(definition, props) {
161
+ return {
162
+ el: null,
163
+ props: props ?? {},
164
+ slots: {},
165
+ mount(_target) {
166
+ const ctx = definition.setup ? untrack(() => definition.setup(this.props)) : {};
167
+ const renderCtx = {
168
+ ...ctx,
169
+ $slots: this.slots
170
+ };
171
+ this.el = untrack(() => definition.render(renderCtx));
172
+ if (definition.styles) {
173
+ collectedStyles.push(definition.styles);
174
+ }
175
+ },
176
+ unmount() {
177
+ this.el = null;
178
+ }
179
+ };
180
+ }
181
+ function mount(component, _target) {
182
+ const instance = createComponentInstance(component);
183
+ instance.mount(null);
184
+ return instance;
185
+ }
186
+ function queueJob(_fn) {
187
+ }
188
+ function nextTick() {
189
+ return Promise.resolve();
190
+ }
191
+
192
+ export {
193
+ signal,
194
+ computed,
195
+ batch,
196
+ untrack,
197
+ flushStyles,
198
+ createElement,
199
+ createTextNode,
200
+ createComment,
201
+ setText,
202
+ setAttr,
203
+ addEventListener,
204
+ appendChild,
205
+ insertBefore,
206
+ removeNode,
207
+ effect,
208
+ createEffect,
209
+ createIf,
210
+ createFor,
211
+ createComponent,
212
+ createComponentInstance,
213
+ mount,
214
+ queueJob,
215
+ nextTick
216
+ };