@constela/server 0.1.2
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 +21 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +328 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Constela Contributors
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CompiledProgram } from '@constela/compiler';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SSR Renderer
|
|
5
|
+
*
|
|
6
|
+
* Renders CompiledProgram to HTML string for Server-Side Rendering.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Renders a CompiledProgram to an HTML string.
|
|
11
|
+
*
|
|
12
|
+
* @param program - The compiled program to render
|
|
13
|
+
* @returns Promise that resolves to HTML string representation
|
|
14
|
+
*/
|
|
15
|
+
declare function renderToString(program: CompiledProgram): Promise<string>;
|
|
16
|
+
|
|
17
|
+
export { renderToString };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// src/markdown.ts
|
|
2
|
+
import { marked } from "marked";
|
|
3
|
+
import DOMPurify from "isomorphic-dompurify";
|
|
4
|
+
marked.setOptions({ gfm: true, breaks: false });
|
|
5
|
+
function parseMarkdownSSR(content) {
|
|
6
|
+
const rawHtml = marked.parse(content, { async: false });
|
|
7
|
+
return DOMPurify.sanitize(rawHtml, {
|
|
8
|
+
USE_PROFILES: { html: true },
|
|
9
|
+
FORBID_TAGS: ["script", "style", "iframe"],
|
|
10
|
+
FORBID_ATTR: ["onerror", "onload", "onclick"]
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/code.ts
|
|
15
|
+
import { createHighlighter } from "shiki";
|
|
16
|
+
|
|
17
|
+
// src/utils/escape.ts
|
|
18
|
+
var HTML_ESCAPE_MAP = {
|
|
19
|
+
"&": "&",
|
|
20
|
+
"<": "<",
|
|
21
|
+
">": ">",
|
|
22
|
+
'"': """,
|
|
23
|
+
"'": "'"
|
|
24
|
+
};
|
|
25
|
+
var HTML_ESCAPE_REGEX = /[&<>"']/g;
|
|
26
|
+
function escapeHtml(str) {
|
|
27
|
+
return str.replace(HTML_ESCAPE_REGEX, (char) => HTML_ESCAPE_MAP[char] ?? char);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/code.ts
|
|
31
|
+
var highlighter = null;
|
|
32
|
+
var PRELOAD_LANGS = [
|
|
33
|
+
"javascript",
|
|
34
|
+
"typescript",
|
|
35
|
+
"json",
|
|
36
|
+
"html",
|
|
37
|
+
"css",
|
|
38
|
+
"python",
|
|
39
|
+
"rust",
|
|
40
|
+
"go",
|
|
41
|
+
"java",
|
|
42
|
+
"bash",
|
|
43
|
+
"markdown"
|
|
44
|
+
];
|
|
45
|
+
async function getHighlighter() {
|
|
46
|
+
if (!highlighter) {
|
|
47
|
+
highlighter = await createHighlighter({
|
|
48
|
+
themes: ["github-dark"],
|
|
49
|
+
langs: PRELOAD_LANGS
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return highlighter;
|
|
53
|
+
}
|
|
54
|
+
async function renderCodeSSR(code, language) {
|
|
55
|
+
const hl = await getHighlighter();
|
|
56
|
+
const loadedLangs = hl.getLoadedLanguages();
|
|
57
|
+
try {
|
|
58
|
+
if (language && !loadedLangs.includes(language) && language !== "text") {
|
|
59
|
+
await hl.loadLanguage(language);
|
|
60
|
+
}
|
|
61
|
+
const langToUse = language || "text";
|
|
62
|
+
return hl.codeToHtml(code, { lang: langToUse, theme: "github-dark" });
|
|
63
|
+
} catch {
|
|
64
|
+
const escapedCode = escapeHtml(code);
|
|
65
|
+
const langClass = language ? ` class="language-${language}"` : "";
|
|
66
|
+
return `<pre><code${langClass}>${escapedCode}</code></pre>`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/renderer.ts
|
|
71
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
72
|
+
"area",
|
|
73
|
+
"base",
|
|
74
|
+
"br",
|
|
75
|
+
"col",
|
|
76
|
+
"embed",
|
|
77
|
+
"hr",
|
|
78
|
+
"img",
|
|
79
|
+
"input",
|
|
80
|
+
"link",
|
|
81
|
+
"meta",
|
|
82
|
+
"param",
|
|
83
|
+
"source",
|
|
84
|
+
"track",
|
|
85
|
+
"wbr"
|
|
86
|
+
]);
|
|
87
|
+
function isEventHandler(value) {
|
|
88
|
+
return typeof value === "object" && value !== null && "event" in value && "action" in value;
|
|
89
|
+
}
|
|
90
|
+
function evaluate(expr, ctx) {
|
|
91
|
+
switch (expr.expr) {
|
|
92
|
+
case "lit":
|
|
93
|
+
return expr.value;
|
|
94
|
+
case "state":
|
|
95
|
+
return ctx.state.get(expr.name);
|
|
96
|
+
case "var": {
|
|
97
|
+
let varName = expr.name;
|
|
98
|
+
let pathParts = [];
|
|
99
|
+
if (varName.includes(".")) {
|
|
100
|
+
const parts = varName.split(".");
|
|
101
|
+
varName = parts[0];
|
|
102
|
+
pathParts = parts.slice(1);
|
|
103
|
+
}
|
|
104
|
+
if (expr.path) {
|
|
105
|
+
pathParts = pathParts.concat(expr.path.split("."));
|
|
106
|
+
}
|
|
107
|
+
const forbiddenKeys = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
108
|
+
for (const part of pathParts) {
|
|
109
|
+
if (forbiddenKeys.has(part)) {
|
|
110
|
+
return void 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let value = ctx.locals[varName];
|
|
114
|
+
for (const part of pathParts) {
|
|
115
|
+
if (value == null) break;
|
|
116
|
+
value = value[part];
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
case "bin":
|
|
121
|
+
return evaluateBinary(expr.op, expr.left, expr.right, ctx);
|
|
122
|
+
case "not":
|
|
123
|
+
return !evaluate(expr.operand, ctx);
|
|
124
|
+
case "cond":
|
|
125
|
+
return evaluate(expr.if, ctx) ? evaluate(expr.then, ctx) : evaluate(expr.else, ctx);
|
|
126
|
+
case "get": {
|
|
127
|
+
const baseValue = evaluate(expr.base, ctx);
|
|
128
|
+
if (baseValue == null) return void 0;
|
|
129
|
+
const pathParts = expr.path.split(".");
|
|
130
|
+
const forbiddenKeys = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
131
|
+
let value = baseValue;
|
|
132
|
+
for (const part of pathParts) {
|
|
133
|
+
if (forbiddenKeys.has(part)) return void 0;
|
|
134
|
+
if (value == null) return void 0;
|
|
135
|
+
value = value[part];
|
|
136
|
+
}
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
default: {
|
|
140
|
+
const _exhaustiveCheck = expr;
|
|
141
|
+
throw new Error(`Unknown expression type: ${JSON.stringify(_exhaustiveCheck)}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function evaluateBinary(op, left, right, ctx) {
|
|
146
|
+
if (op === "&&") {
|
|
147
|
+
const leftVal2 = evaluate(left, ctx);
|
|
148
|
+
if (!leftVal2) return leftVal2;
|
|
149
|
+
return evaluate(right, ctx);
|
|
150
|
+
}
|
|
151
|
+
if (op === "||") {
|
|
152
|
+
const leftVal2 = evaluate(left, ctx);
|
|
153
|
+
if (leftVal2) return leftVal2;
|
|
154
|
+
return evaluate(right, ctx);
|
|
155
|
+
}
|
|
156
|
+
const leftVal = evaluate(left, ctx);
|
|
157
|
+
const rightVal = evaluate(right, ctx);
|
|
158
|
+
switch (op) {
|
|
159
|
+
case "+":
|
|
160
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
161
|
+
return leftVal + rightVal;
|
|
162
|
+
}
|
|
163
|
+
return String(leftVal) + String(rightVal);
|
|
164
|
+
case "-":
|
|
165
|
+
return (typeof leftVal === "number" ? leftVal : 0) - (typeof rightVal === "number" ? rightVal : 0);
|
|
166
|
+
case "*":
|
|
167
|
+
return (typeof leftVal === "number" ? leftVal : 0) * (typeof rightVal === "number" ? rightVal : 0);
|
|
168
|
+
case "/": {
|
|
169
|
+
const dividend = typeof leftVal === "number" ? leftVal : 0;
|
|
170
|
+
const divisor = typeof rightVal === "number" ? rightVal : 0;
|
|
171
|
+
if (divisor === 0) {
|
|
172
|
+
return dividend === 0 ? NaN : dividend > 0 ? Infinity : -Infinity;
|
|
173
|
+
}
|
|
174
|
+
return dividend / divisor;
|
|
175
|
+
}
|
|
176
|
+
case "==":
|
|
177
|
+
return leftVal === rightVal;
|
|
178
|
+
case "!=":
|
|
179
|
+
return leftVal !== rightVal;
|
|
180
|
+
case "<":
|
|
181
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
182
|
+
return leftVal < rightVal;
|
|
183
|
+
}
|
|
184
|
+
return String(leftVal) < String(rightVal);
|
|
185
|
+
case "<=":
|
|
186
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
187
|
+
return leftVal <= rightVal;
|
|
188
|
+
}
|
|
189
|
+
return String(leftVal) <= String(rightVal);
|
|
190
|
+
case ">":
|
|
191
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
192
|
+
return leftVal > rightVal;
|
|
193
|
+
}
|
|
194
|
+
return String(leftVal) > String(rightVal);
|
|
195
|
+
case ">=":
|
|
196
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
197
|
+
return leftVal >= rightVal;
|
|
198
|
+
}
|
|
199
|
+
return String(leftVal) >= String(rightVal);
|
|
200
|
+
default:
|
|
201
|
+
throw new Error("Unknown binary operator: " + op);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function formatValue(value) {
|
|
205
|
+
if (value === null || value === void 0) {
|
|
206
|
+
return "";
|
|
207
|
+
}
|
|
208
|
+
if (typeof value === "object") {
|
|
209
|
+
return JSON.stringify(value);
|
|
210
|
+
}
|
|
211
|
+
return String(value);
|
|
212
|
+
}
|
|
213
|
+
async function renderNode(node, ctx) {
|
|
214
|
+
switch (node.kind) {
|
|
215
|
+
case "element":
|
|
216
|
+
return await renderElement(node, ctx);
|
|
217
|
+
case "text":
|
|
218
|
+
return renderText(node, ctx);
|
|
219
|
+
case "if":
|
|
220
|
+
return await renderIf(node, ctx);
|
|
221
|
+
case "each":
|
|
222
|
+
return await renderEach(node, ctx);
|
|
223
|
+
case "markdown":
|
|
224
|
+
return renderMarkdown(node, ctx);
|
|
225
|
+
case "code":
|
|
226
|
+
return await renderCode(node, ctx);
|
|
227
|
+
default: {
|
|
228
|
+
const _exhaustiveCheck = node;
|
|
229
|
+
throw new Error(`Unknown node kind: ${JSON.stringify(_exhaustiveCheck)}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async function renderElement(node, ctx) {
|
|
234
|
+
const tag = node.tag;
|
|
235
|
+
const isVoid = VOID_ELEMENTS.has(tag);
|
|
236
|
+
let attrs = "";
|
|
237
|
+
if (node.props) {
|
|
238
|
+
for (const [propName, propValue] of Object.entries(node.props)) {
|
|
239
|
+
if (isEventHandler(propValue)) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const value = evaluate(propValue, ctx);
|
|
243
|
+
if (value === false) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (value === true) {
|
|
247
|
+
attrs += ` ${propName}`;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (value === null || value === void 0) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
attrs += ` ${propName}="${escapeHtml(String(value))}"`;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (isVoid) {
|
|
257
|
+
return `<${tag}${attrs} />`;
|
|
258
|
+
}
|
|
259
|
+
let childrenHtml = "";
|
|
260
|
+
if (node.children) {
|
|
261
|
+
for (const child of node.children) {
|
|
262
|
+
childrenHtml += await renderNode(child, ctx);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return `<${tag}${attrs}>${childrenHtml}</${tag}>`;
|
|
266
|
+
}
|
|
267
|
+
function renderText(node, ctx) {
|
|
268
|
+
const value = evaluate(node.value, ctx);
|
|
269
|
+
return escapeHtml(formatValue(value));
|
|
270
|
+
}
|
|
271
|
+
async function renderIf(node, ctx) {
|
|
272
|
+
const condition = evaluate(node.condition, ctx);
|
|
273
|
+
if (condition) {
|
|
274
|
+
return await renderNode(node.then, ctx);
|
|
275
|
+
}
|
|
276
|
+
if (node.else) {
|
|
277
|
+
return await renderNode(node.else, ctx);
|
|
278
|
+
}
|
|
279
|
+
return "";
|
|
280
|
+
}
|
|
281
|
+
async function renderEach(node, ctx) {
|
|
282
|
+
const items = evaluate(node.items, ctx);
|
|
283
|
+
if (!Array.isArray(items)) {
|
|
284
|
+
return "";
|
|
285
|
+
}
|
|
286
|
+
let result = "";
|
|
287
|
+
for (let index = 0; index < items.length; index++) {
|
|
288
|
+
const item = items[index];
|
|
289
|
+
const itemLocals = {
|
|
290
|
+
...ctx.locals,
|
|
291
|
+
[node.as]: item
|
|
292
|
+
};
|
|
293
|
+
if (node.index) {
|
|
294
|
+
itemLocals[node.index] = index;
|
|
295
|
+
}
|
|
296
|
+
const itemCtx = {
|
|
297
|
+
...ctx,
|
|
298
|
+
locals: itemLocals
|
|
299
|
+
};
|
|
300
|
+
result += await renderNode(node.body, itemCtx);
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
function renderMarkdown(node, ctx) {
|
|
305
|
+
const content = evaluate(node.content, ctx);
|
|
306
|
+
const html = parseMarkdownSSR(formatValue(content));
|
|
307
|
+
return `<div class="constela-markdown">${html}</div>`;
|
|
308
|
+
}
|
|
309
|
+
async function renderCode(node, ctx) {
|
|
310
|
+
const language = formatValue(evaluate(node.language, ctx));
|
|
311
|
+
const content = formatValue(evaluate(node.content, ctx));
|
|
312
|
+
const html = await renderCodeSSR(content, language);
|
|
313
|
+
return `<div class="constela-code">${html}</div>`;
|
|
314
|
+
}
|
|
315
|
+
async function renderToString(program) {
|
|
316
|
+
const state = /* @__PURE__ */ new Map();
|
|
317
|
+
for (const [name, field] of Object.entries(program.state)) {
|
|
318
|
+
state.set(name, field.initial);
|
|
319
|
+
}
|
|
320
|
+
const ctx = {
|
|
321
|
+
state,
|
|
322
|
+
locals: {}
|
|
323
|
+
};
|
|
324
|
+
return await renderNode(program.view, ctx);
|
|
325
|
+
}
|
|
326
|
+
export {
|
|
327
|
+
renderToString
|
|
328
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@constela/server",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Server-side rendering for Constela UI framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@constela/compiler": "^0.4.0"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"isomorphic-dompurify": "^2.35.0",
|
|
22
|
+
"marked": "^17.0.1",
|
|
23
|
+
"shiki": "^1.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.10.0",
|
|
27
|
+
"tsup": "^8.0.0",
|
|
28
|
+
"typescript": "^5.3.0",
|
|
29
|
+
"vitest": "^2.0.0",
|
|
30
|
+
"@constela/compiler": "0.4.0"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20.0.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
38
|
+
"type-check": "tsc --noEmit",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:watch": "vitest",
|
|
41
|
+
"clean": "rm -rf dist"
|
|
42
|
+
}
|
|
43
|
+
}
|