@seip/bluebird-js 0.1.2 → 0.1.5

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 (43) hide show
  1. package/.vscode/extensions.json +7 -0
  2. package/.vscode/settings.json +10 -0
  3. package/AGENTS.md +495 -454
  4. package/README.md +528 -353
  5. package/app/routes/web.js +47 -55
  6. package/app/views/about.jsx +98 -0
  7. package/app/views/components/Header.jsx +42 -0
  8. package/app/views/components/LiveCounter.jsx +66 -0
  9. package/app/views/dashboard.jsx +300 -0
  10. package/app/views/index.jsx +123 -0
  11. package/app/views/layouts/main/view.jsx +201 -0
  12. package/app/views/legal/terms.html +29 -19
  13. package/app/views/login.jsx +141 -0
  14. package/core/actions.d.ts +4 -0
  15. package/core/actions.js +329 -0
  16. package/core/app.js +38 -6
  17. package/core/cli/doctor.js +3 -4
  18. package/core/cli/init.js +20 -6
  19. package/core/cli/migrate.js +418 -145
  20. package/core/cli/view.js +76 -11
  21. package/core/database.js +6 -10
  22. package/core/debug.js +1 -1
  23. package/core/html.d.ts +54 -42
  24. package/core/html.js +473 -217
  25. package/core/index.d.ts +1385 -1190
  26. package/core/jsx.d.ts +38 -0
  27. package/core/jsx.js +1432 -0
  28. package/core/model.js +271 -61
  29. package/core/render-cache.js +212 -212
  30. package/core/render.d.ts +20 -2
  31. package/core/render.js +459 -51
  32. package/core/validate.js +1 -1
  33. package/core/view-loader.js +360 -24
  34. package/package.json +9 -3
  35. package/public/js/bluebird-client.js +3160 -1780
  36. package/public/js/tailwind.js +8 -0
  37. package/app/views/about.js +0 -63
  38. package/app/views/dashboard.js +0 -147
  39. package/app/views/index.js +0 -80
  40. package/app/views/layouts/main/view.js +0 -81
  41. package/app/views/login.js +0 -80
  42. package/public/css/bluebird.min.css +0 -2
  43. package/public/js/bluebird.min.js +0 -1073
package/core/html.js CHANGED
@@ -1,217 +1,473 @@
1
- import RenderCache from "./render-cache.js";
2
-
3
- const SAFE_HTML_KEY = Symbol.for("Bluebird.SafeHtml");
4
-
5
- /**
6
- * Wrapper class representing a trusted HTML string that bypasses escaping.
7
- */
8
- export class SafeHtml {
9
- /**
10
- * @param {string} value
11
- */
12
- constructor(value) {
13
- this.value = typeof value === "string" ? value : String(value ?? "");
14
- this[SAFE_HTML_KEY] = true;
15
- }
16
-
17
- /**
18
- * Returns the raw inner HTML string.
19
- * @returns {string}
20
- */
21
- toString() {
22
- return this.value;
23
- }
24
- }
25
-
26
- /**
27
- * Checks whether a given value is an instance of SafeHtml.
28
- * @param {any} val
29
- * @returns {boolean}
30
- */
31
- export function isSafeHtml(val) {
32
- return Boolean(val && (val instanceof SafeHtml || val[SAFE_HTML_KEY] === true));
33
- }
34
-
35
- /**
36
- * Escapes HTML special characters to prevent XSS.
37
- * @param {any} value
38
- * @returns {string}
39
- */
40
- export function escapeHtml(value) {
41
- if (value === null || value === undefined) return "";
42
- const str = typeof value === "string" ? value : String(value);
43
- return str
44
- .replace(/&/g, "&")
45
- .replace(/</g, "&lt;")
46
- .replace(/>/g, "&gt;")
47
- .replace(/"/g, "&quot;")
48
- .replace(/'/g, "&#39;");
49
- }
50
-
51
- /**
52
- * Serializes data into a secure JSON string suitable for script tags and HTML attributes.
53
- * Prevents HTML/script closing tag injection.
54
- * @param {any} data
55
- * @returns {string}
56
- */
57
- export function safeJson(data) {
58
- return JSON.stringify(data)
59
- .replace(/</g, "\\u003c")
60
- .replace(/>/g, "\\u003e")
61
- .replace(/&/g, "\\u0026")
62
- .replace(/\u2028/g, "\\u2028")
63
- .replace(/\u2029/g, "\\u2029");
64
- }
65
-
66
- /**
67
- * Flattens and stringifies a template literal value.
68
- * @param {any} val
69
- * @returns {string}
70
- */
71
- function resolveValue(val) {
72
- if (val === null || val === undefined || val === false) {
73
- return "";
74
- }
75
- if (val === true) {
76
- return "";
77
- }
78
- if (isSafeHtml(val)) {
79
- return val.toString();
80
- }
81
- if (Array.isArray(val)) {
82
- return val.map(resolveValue).join("");
83
- }
84
- if (typeof val === "function") {
85
- throw new TypeError(
86
- "[BLUEBIRD HTML] Functions cannot be interpolated directly in html`...`. Did you mean to call it like ${myFunc()}?"
87
- );
88
- }
89
- return escapeHtml(val);
90
- }
91
-
92
- /**
93
- * Tagged template literal for creating secure HTML strings.
94
- * All interpolated values are escaped by default unless wrapped in SafeHtml or html.raw().
95
- *
96
- * @param {TemplateStringsArray} strings
97
- * @param {...any} values
98
- * @returns {SafeHtml}
99
- */
100
- export function html(strings, ...values) {
101
- let result = "";
102
- const len = strings.length;
103
-
104
- for (let i = 0; i < len; i++) {
105
- result += strings[i];
106
- if (i < values.length) {
107
- result += resolveValue(values[i]);
108
- }
109
- }
110
-
111
- return new SafeHtml(result);
112
- }
113
-
114
- /**
115
- * Marks a string as explicitly safe, bypassing HTML escaping.
116
- * @param {string} str
117
- * @returns {SafeHtml}
118
- */
119
- html.raw = function raw(str) {
120
- return new SafeHtml(str);
121
- };
122
-
123
- /**
124
- * Generates an escaped JSON string for embedding inside HTML attributes or scripts.
125
- * @param {any} obj
126
- * @returns {SafeHtml}
127
- */
128
- html.json = function json(obj) {
129
- return new SafeHtml(safeJson(obj));
130
- };
131
-
132
- /**
133
- * Tagged template literal and helper for creating inline client scripts safely.
134
- * Interpolated values are safely converted to JSON literals to prevent code injection.
135
- * Escapes closing </script> tags automatically.
136
- *
137
- * @param {TemplateStringsArray|string|Object} first
138
- * @param {...any} values
139
- * @returns {SafeHtml}
140
- */
141
- export function script(first, ...values) {
142
- if (Array.isArray(first) && "raw" in first) {
143
- let code = "";
144
- for (let i = 0; i < first.length; i++) {
145
- code += first[i];
146
- if (i < values.length) {
147
- const val = values[i];
148
- if (isSafeHtml(val)) {
149
- code += val.toString();
150
- } else {
151
- code += safeJson(val);
152
- }
153
- }
154
- }
155
- const safeCode = code.replace(/<\/script/gi, "<\\/script");
156
- return new SafeHtml(`<script>${safeCode}</script>`);
157
- }
158
-
159
- if (typeof first === "string") {
160
- const src = first.replace(/"/g, "&quot;");
161
- return new SafeHtml(`<script type="module" src="${src}"></script>`);
162
- }
163
-
164
- if (first && typeof first === "object" && first.src) {
165
- const src = String(first.src).replace(/"/g, "&quot;");
166
- const defer = first.defer ? " defer" : "";
167
- const type = first.module ? ' type="module"' : "";
168
- return new SafeHtml(`<script${type} src="${src}"${defer}></script>`);
169
- }
170
-
171
- return new SafeHtml("");
172
- }
173
-
174
- html.script = script;
175
-
176
- /**
177
- * Cached fragment helper. Evaluates renderFn only on cache miss.
178
- *
179
- * @param {string} key - Cache key identifier.
180
- * @param {number|{ttl?: number, vary?: string[]}} options - TTL in seconds or options object.
181
- * @param {Function} renderFn - Callback returning SafeHtml or string.
182
- * @returns {Promise<SafeHtml>}
183
- */
184
- export async function cached(key, options, renderFn) {
185
- const ttl = typeof options === "number" ? options : options?.ttl || 300;
186
- const cacheKey = `fragment:${key}`;
187
-
188
- const existing = await RenderCache.lookup(cacheKey);
189
- if (existing !== null) {
190
- return new SafeHtml(existing);
191
- }
192
-
193
- const output = await renderFn();
194
- const renderedString = output instanceof SafeHtml ? output.toString() : String(output ?? "");
195
- await RenderCache.store(cacheKey, renderedString, ttl);
196
-
197
- return new SafeHtml(renderedString);
198
- }
199
-
200
- /**
201
- * Deferred streaming token for progressive chunked rendering.
202
- *
203
- * @param {Promise<any>} promise - Asynchronous data promise.
204
- * @param {SafeHtml|string} fallback - Initial placeholder HTML chunk.
205
- * @param {Function} renderer - Callback taking resolved data and returning SafeHtml or string.
206
- * @returns {Object}
207
- */
208
- export function defer(promise, fallback, renderer) {
209
- return {
210
- __bluebird_deferred__: true,
211
- promise,
212
- fallback: fallback instanceof SafeHtml ? fallback.toString() : String(fallback ?? ""),
213
- renderer,
214
- };
215
- }
216
-
217
- export default html;
1
+ import RenderCache from "./render-cache.js";
2
+
3
+ const SAFE_HTML_KEY = Symbol.for("Bluebird.SafeHtml");
4
+
5
+ /**
6
+ * Wrapper class representing a trusted HTML string that bypasses escaping.
7
+ */
8
+ export class SafeHtml {
9
+ /**
10
+ * @param {string} value
11
+ */
12
+ constructor(value) {
13
+ this.value = typeof value === "string" ? value : String(value ?? "");
14
+ this[SAFE_HTML_KEY] = true;
15
+ }
16
+
17
+ /**
18
+ * Returns the raw inner HTML string.
19
+ * @returns {string}
20
+ */
21
+ toString() {
22
+ return this.value;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Checks whether a given value is an instance of SafeHtml.
28
+ * @param {any} val
29
+ * @returns {boolean}
30
+ */
31
+ export function isSafeHtml(val) {
32
+ return Boolean(val && (val instanceof SafeHtml || val[SAFE_HTML_KEY] === true));
33
+ }
34
+
35
+ /**
36
+ * Escapes HTML special characters to prevent XSS.
37
+ * @param {any} value
38
+ * @returns {string}
39
+ */
40
+ export function escapeHtml(value) {
41
+ if (value === null || value === undefined) return "";
42
+ const str = typeof value === "string" ? value : String(value);
43
+ return str
44
+ .replace(/&/g, "&amp;")
45
+ .replace(/</g, "&lt;")
46
+ .replace(/>/g, "&gt;")
47
+ .replace(/"/g, "&quot;")
48
+ .replace(/'/g, "&#39;");
49
+ }
50
+
51
+ /**
52
+ * Serializes data into a secure JSON string suitable for script tags and HTML attributes.
53
+ * Prevents HTML/script closing tag injection.
54
+ * @param {any} data
55
+ * @returns {string}
56
+ */
57
+ export function safeJson(data) {
58
+ return JSON.stringify(data)
59
+ .replace(/</g, "\\u003c")
60
+ .replace(/>/g, "\\u003e")
61
+ .replace(/&/g, "\\u0026")
62
+ .replace(/\u2028/g, "\\u2028")
63
+ .replace(/\u2029/g, "\\u2029");
64
+ }
65
+
66
+ const componentRegistry = new Map();
67
+
68
+ /**
69
+ * Parses static HTML attributes from a string chunk into a props object.
70
+ * @param {string} str
71
+ * @param {Record<string, any>} props
72
+ */
73
+ function parseStaticAttrs(str, props) {
74
+ if (!str) return;
75
+ const regex = /([a-zA-Z0-9_\-]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
76
+ let m;
77
+ while ((m = regex.exec(str)) !== null) {
78
+ const key = m[1];
79
+ if (key.endsWith("=") || (key === "props" && !m[2] && !m[3] && !m[4])) continue;
80
+ const val = m[2] !== undefined ? m[2] : m[3] !== undefined ? m[3] : m[4] !== undefined ? m[4] : true;
81
+ props[key] = val;
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Flattens and stringifies a template literal value.
87
+ * @param {any} val
88
+ * @returns {string}
89
+ */
90
+ function resolveValue(val) {
91
+ if (val === null || val === undefined || val === false) {
92
+ return "";
93
+ }
94
+ if (val === true) {
95
+ return "";
96
+ }
97
+ if (isSafeHtml(val)) {
98
+ return val.toString();
99
+ }
100
+ if (Array.isArray(val)) {
101
+ return val.map(resolveValue).join("");
102
+ }
103
+ if (typeof val === "function") {
104
+ try {
105
+ return resolveValue(val({}));
106
+ } catch {
107
+ return "";
108
+ }
109
+ }
110
+ return escapeHtml(val);
111
+ }
112
+
113
+ /**
114
+ * Tagged template literal for creating secure HTML strings.
115
+ * All interpolated values are escaped by default unless wrapped in SafeHtml or html.raw().
116
+ * Supports component functions, registered tags, and props.
117
+ *
118
+ * @param {TemplateStringsArray} strings
119
+ * @param {...any} values
120
+ * @returns {SafeHtml}
121
+ */
122
+ export function html(strings, ...values) {
123
+ let hasSpecial = false;
124
+ for (let i = 0; i < values.length; i++) {
125
+ if (typeof values[i] === "function") {
126
+ hasSpecial = true;
127
+ break;
128
+ }
129
+ }
130
+
131
+ if (!hasSpecial && componentRegistry.size === 0) {
132
+ let result = "";
133
+ const len = strings.length;
134
+ for (let i = 0; i < len; i++) {
135
+ result += strings[i];
136
+ if (i < values.length) {
137
+ result += resolveValue(values[i]);
138
+ }
139
+ }
140
+ return new SafeHtml(result);
141
+ }
142
+
143
+ let result = "";
144
+ const len = strings.length;
145
+ let i = 0;
146
+ let str = strings[0] || "";
147
+
148
+ while (i < len) {
149
+ /**
150
+ * Check for component function interpolation: <${Component} ... />
151
+ */
152
+ if (i < values.length && typeof values[i] === "function" && /<\s*$/.test(str)) {
153
+ const compFn = values[i];
154
+ const tagOpenIndex = str.lastIndexOf("<");
155
+ result += str.slice(0, tagOpenIndex);
156
+
157
+ const props = {};
158
+ i++;
159
+ let closedOnChunk = false;
160
+
161
+ while (i < len) {
162
+ const curStr = strings[i];
163
+ const selfCloseIdx = curStr.indexOf("/>");
164
+ const regularCloseIdx = curStr.indexOf(">");
165
+
166
+ if (selfCloseIdx !== -1 && (regularCloseIdx === -1 || selfCloseIdx < regularCloseIdx)) {
167
+ const before = curStr.slice(0, selfCloseIdx);
168
+ parseStaticAttrs(before, props);
169
+ const after = curStr.slice(selfCloseIdx + 2);
170
+
171
+ const rendered = compFn(props);
172
+ result += resolveValue(rendered);
173
+
174
+ str = after;
175
+ closedOnChunk = true;
176
+ break;
177
+ }
178
+
179
+ const attrMatch = curStr.match(/([a-zA-Z0-9_\-]+)\s*=\s*["']?$/);
180
+ if (attrMatch && i < values.length) {
181
+ const attrName = attrMatch[1];
182
+ const beforeAttr = curStr.slice(0, attrMatch.index);
183
+ parseStaticAttrs(beforeAttr, props);
184
+
185
+ const val = values[i];
186
+ if (attrName === "props" && val && typeof val === "object" && !Array.isArray(val)) {
187
+ Object.assign(props, val);
188
+ props.props = val;
189
+ } else {
190
+ props[attrName] = val;
191
+ }
192
+ i++;
193
+ continue;
194
+ }
195
+
196
+ if (curStr.trimEnd().endsWith("...") && i < values.length) {
197
+ const val = values[i];
198
+ if (val && typeof val === "object") {
199
+ Object.assign(props, val);
200
+ }
201
+ i++;
202
+ continue;
203
+ }
204
+
205
+ parseStaticAttrs(curStr, props);
206
+ if (i < values.length) {
207
+ i++;
208
+ } else {
209
+ break;
210
+ }
211
+ }
212
+
213
+ if (!closedOnChunk) {
214
+ i++;
215
+ str = i < len ? strings[i] : "";
216
+ }
217
+ continue;
218
+ }
219
+
220
+ /**
221
+ * Check for registered component tag: <Header ... />
222
+ */
223
+ if (componentRegistry.size > 0 && /<\s*([A-Z][a-zA-Z0-9_]*)(?:\s+|>|$)/.test(str)) {
224
+ const regMatch = str.match(/<\s*([A-Z][a-zA-Z0-9_]*)/);
225
+ if (regMatch) {
226
+ const compName = regMatch[1];
227
+ const compFn = componentRegistry.get(compName);
228
+ if (compFn) {
229
+ const tagOpenIndex = str.indexOf(regMatch[0]);
230
+ result += str.slice(0, tagOpenIndex);
231
+ const restOfStr = str.slice(tagOpenIndex + regMatch[0].length);
232
+
233
+ const props = {};
234
+ let curChunk = restOfStr;
235
+ let closedOnChunk = false;
236
+
237
+ while (true) {
238
+ const selfCloseIdx = curChunk.indexOf("/>");
239
+ if (selfCloseIdx !== -1) {
240
+ const before = curChunk.slice(0, selfCloseIdx);
241
+ parseStaticAttrs(before, props);
242
+ const after = curChunk.slice(selfCloseIdx + 2);
243
+
244
+ const rendered = compFn(props);
245
+ result += resolveValue(rendered);
246
+
247
+ str = after;
248
+ closedOnChunk = true;
249
+ break;
250
+ }
251
+
252
+ const attrMatch = curChunk.match(/([a-zA-Z0-9_\-]+)\s*=\s*["']?$/);
253
+ if (attrMatch && i < values.length) {
254
+ const attrName = attrMatch[1];
255
+ const beforeAttr = curChunk.slice(0, attrMatch.index);
256
+ parseStaticAttrs(beforeAttr, props);
257
+
258
+ const val = values[i];
259
+ if (attrName === "props" && val && typeof val === "object" && !Array.isArray(val)) {
260
+ Object.assign(props, val);
261
+ props.props = val;
262
+ } else {
263
+ props[attrName] = val;
264
+ }
265
+ i++;
266
+ if (i < len) {
267
+ curChunk = strings[i];
268
+ continue;
269
+ }
270
+ break;
271
+ }
272
+
273
+ parseStaticAttrs(curChunk, props);
274
+ if (i < len - 1) {
275
+ i++;
276
+ curChunk = strings[i];
277
+ } else {
278
+ break;
279
+ }
280
+ }
281
+
282
+ if (!closedOnChunk) {
283
+ i++;
284
+ str = i < len ? strings[i] : "";
285
+ }
286
+ continue;
287
+ }
288
+ }
289
+ }
290
+
291
+ result += str;
292
+ if (i < values.length) {
293
+ result += resolveValue(values[i]);
294
+ }
295
+ i++;
296
+ str = i < len ? strings[i] : "";
297
+ }
298
+
299
+ return new SafeHtml(result);
300
+ }
301
+
302
+ /**
303
+ * Registers one or more UI components globally by name.
304
+ * @param {Record<string, Function>} map
305
+ */
306
+ html.register = function register(map) {
307
+ if (map && typeof map === "object") {
308
+ for (const [k, fn] of Object.entries(map)) {
309
+ if (typeof fn === "function") {
310
+ componentRegistry.set(k, fn);
311
+ }
312
+ }
313
+ }
314
+ };
315
+
316
+ /**
317
+ * Returns a globally registered component function by name.
318
+ * @param {string} name
319
+ * @returns {Function|undefined}
320
+ */
321
+ html.getComponent = function getComponent(name) {
322
+ return componentRegistry.get(name);
323
+ };
324
+
325
+ /**
326
+ * Marks a string as explicitly safe, bypassing HTML escaping.
327
+ * @param {string} str
328
+ * @returns {SafeHtml}
329
+ */
330
+ html.raw = function raw(str) {
331
+ return new SafeHtml(str);
332
+ };
333
+
334
+ /**
335
+ * Generates an escaped JSON string for embedding inside HTML attributes or scripts.
336
+ * @param {any} obj
337
+ * @returns {SafeHtml}
338
+ */
339
+ html.json = function json(obj) {
340
+ return new SafeHtml(safeJson(obj));
341
+ };
342
+
343
+ /**
344
+ * Renders a list of items into SafeHtml without nested backtick clutter.
345
+ * @template T
346
+ * @param {T[]|null|undefined} items
347
+ * @param {(item: T, index: number) => SafeHtml|string} renderFn
348
+ * @param {(() => SafeHtml|string)|SafeHtml|string} [emptyFallback=""]
349
+ * @returns {SafeHtml}
350
+ */
351
+ export function each(items, renderFn, emptyFallback = "") {
352
+ if (!Array.isArray(items) || items.length === 0) {
353
+ const fallbackVal = typeof emptyFallback === "function" ? emptyFallback() : emptyFallback;
354
+ return isSafeHtml(fallbackVal) ? fallbackVal : new SafeHtml(resolveValue(fallbackVal));
355
+ }
356
+ let out = "";
357
+ for (let i = 0; i < items.length; i++) {
358
+ out += resolveValue(renderFn(items[i], i));
359
+ }
360
+ return new SafeHtml(out);
361
+ }
362
+
363
+ html.each = each;
364
+ html.map = each;
365
+
366
+ /**
367
+ * Renders an inline <code> block with automatic escaping.
368
+ * @param {string} text
369
+ * @param {string} [className=""]
370
+ * @returns {SafeHtml}
371
+ */
372
+ html.code = function code(text, className = "") {
373
+ const cls = className ? ` class="${escapeHtml(className)}"` : "";
374
+ return new SafeHtml(`<code${cls}>${escapeHtml(text)}</code>`);
375
+ };
376
+
377
+ /**
378
+ * Tagged template literal and helper for creating inline client scripts safely.
379
+ * Accepts functions directly with serialized data for zero-escape syntax.
380
+ *
381
+ * @param {TemplateStringsArray|string|Function|Object} first
382
+ * @param {...any} values
383
+ * @returns {SafeHtml}
384
+ */
385
+ export function script(first, ...values) {
386
+ if (typeof first === "function") {
387
+ const data = values[0] !== undefined ? safeJson(values[0]) : "undefined";
388
+ const opts = values[1] || {};
389
+ const fnSource = first.toString().replace(/<\/script/gi, "<\\/script");
390
+ if (opts.onLoad) {
391
+ return new SafeHtml(
392
+ `<script>window.Bluebird ? window.Bluebird.onPageLoad(() => (${fnSource})(${data})) : (${fnSource})(${data});</script>`
393
+ );
394
+ }
395
+ return new SafeHtml(`<script>(${fnSource})(${data});</script>`);
396
+ }
397
+
398
+ if (Array.isArray(first) && "raw" in first) {
399
+ let code = "";
400
+ for (let i = 0; i < first.length; i++) {
401
+ code += first[i];
402
+ if (i < values.length) {
403
+ const val = values[i];
404
+ if (isSafeHtml(val)) {
405
+ code += val.toString();
406
+ } else {
407
+ code += safeJson(val);
408
+ }
409
+ }
410
+ }
411
+ const safeCode = code.replace(/<\/script/gi, "<\\/script");
412
+ return new SafeHtml(`<script>${safeCode}</script>`);
413
+ }
414
+
415
+ if (typeof first === "string") {
416
+ const src = first.replace(/"/g, "&quot;");
417
+ return new SafeHtml(`<script type="module" src="${src}"></script>`);
418
+ }
419
+
420
+ if (first && typeof first === "object" && first.src) {
421
+ const src = String(first.src).replace(/"/g, "&quot;");
422
+ const defer = first.defer ? " defer" : "";
423
+ const type = first.module ? ' type="module"' : "";
424
+ return new SafeHtml(`<script${type} src="${src}"${defer}></script>`);
425
+ }
426
+
427
+ return new SafeHtml("");
428
+ }
429
+
430
+ html.script = script;
431
+
432
+ /**
433
+ * Cached fragment helper. Evaluates renderFn only on cache miss.
434
+ *
435
+ * @param {string} key - Cache key identifier.
436
+ * @param {number|{ttl?: number, vary?: string[]}} options - TTL in seconds or options object.
437
+ * @param {Function} renderFn - Callback returning SafeHtml or string.
438
+ * @returns {Promise<SafeHtml>}
439
+ */
440
+ export async function cached(key, options, renderFn) {
441
+ const ttl = typeof options === "number" ? options : options?.ttl || 300;
442
+ const cacheKey = `fragment:${key}`;
443
+
444
+ const existing = await RenderCache.lookup(cacheKey);
445
+ if (existing !== null) {
446
+ return new SafeHtml(existing);
447
+ }
448
+
449
+ const output = await renderFn();
450
+ const renderedString = output instanceof SafeHtml ? output.toString() : String(output ?? "");
451
+ await RenderCache.store(cacheKey, renderedString, ttl);
452
+
453
+ return new SafeHtml(renderedString);
454
+ }
455
+
456
+ /**
457
+ * Deferred streaming token for progressive chunked rendering.
458
+ *
459
+ * @param {Promise<any>} promise - Asynchronous data promise.
460
+ * @param {SafeHtml|string} fallback - Initial placeholder HTML chunk.
461
+ * @param {Function} renderer - Callback taking resolved data and returning SafeHtml or string.
462
+ * @returns {Object}
463
+ */
464
+ export function defer(promise, fallback, renderer) {
465
+ return {
466
+ __bluebird_deferred__: true,
467
+ promise,
468
+ fallback: fallback instanceof SafeHtml ? fallback.toString() : String(fallback ?? ""),
469
+ renderer,
470
+ };
471
+ }
472
+
473
+ export default html;