@mateosuarezdev/flash 0.0.2 → 0.0.3
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/dist/server.cjs +4 -4
- package/dist/server.js +62 -60
- package/package.json +1 -1
package/dist/server.cjs
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* See LICENSE file for full terms.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("node:fs/promises"),m=require("node:path");function
|
|
11
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("node:fs/promises"),m=require("node:path");function A(r){return r.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function b(r){return r.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}const v=new Set(["area","base","br","col","embed","hr","img","input","link","meta","source","track","wbr"]);function E(r){return/^on[A-Z]/.test(r)}function j(r){return Object.entries(r).map(([t,o])=>`${t.replace(/[A-Z]/g,n=>`-${n.toLowerCase()}`)}:${o}`).join(";")}function w(r,t=!0){const o=Object.entries(r).filter(([e,n])=>!(n==null||t&&e==="children"||E(e))).map(([e,n])=>(e==="className"&&(e="class"),e==="style"&&typeof n=="object"&&(n=j(n)),typeof n=="boolean"?n?e:"":`${e}="${b(String(n))}"`)).filter(Boolean).join(" ");return o?` ${o}`:""}function $(r){return r?Array.isArray(r)?r:[r]:[]}function D(r){return r&&typeof r.then=="function"}function h(r,t){return`<div id="${r}" data-server-component="suspended" data-component-name="${t}">Loading...</div>`}const a=new Map;let d=0;function H(r){return a.clear(),d=0,s(r)}async function T(r){a.clear(),d=0;let t=s(r);for(;a.size>0;){console.log(`🔄 Resolving ${a.size} async components in parallel...`);const o=Array.from(a.entries());a.clear();const e=await Promise.allSettled(o.map(([n,{promise:i}])=>i));for(let n=0;n<o.length;n++){const i=o[n];if(!i)continue;const[l,{componentName:c,errorFallback:u}]=i,p=e[n];if(!p)continue;const y=h(l,c);if(p.status==="fulfilled")try{console.log(`✅ Resolved: ${c} (${l})`);const f=s(p.value);t=t.replace(y,f)}catch(f){console.error(`❌ Error rendering ${c}:`,f);const S=u?s(typeof u=="function"?u(f):u):`<div class="error">Error rendering ${c}</div>`;t=t.replace(y,S)}else{console.error(`❌ Error resolving ${c}:`,p.reason);const f=u?s(typeof u=="function"?u(p.reason):u):`<div class="error">Error loading ${c}</div>`;t=t.replace(y,f)}}}return console.log("✅ All async components resolved!"),t}async function*P(r){for(a.clear(),d=0,yield s(r);a.size>0;){const o=Array.from(a.entries());a.clear();const e=o.map(async([n,{promise:i}])=>{try{const l=await i,c=s(l);return`<script>
|
|
12
12
|
(function() {
|
|
13
13
|
const el = document.getElementById('${n}');
|
|
14
14
|
if (el) {
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
el.innerHTML = '<div class="error">Error loading component</div>';
|
|
23
23
|
}
|
|
24
24
|
})();
|
|
25
|
-
<\/script>`}});for(const n of e)yield await n}}function s(
|
|
26
|
-
Example: const ${i} = suspend(async (props) => { ... }, <Loading />, <Error />);`),console.log(`⏳ Suspending: ${i} (${n})`),a.set(n,{id:n,promise:e,componentName:i}),h(n,i)}return s(e)}if(typeof
|
|
27
|
-
<html${e}>${i}</html>`:`<${
|
|
25
|
+
<\/script>`}});for(const n of e)yield await n}}function s(r){if(typeof r=="function"){const e=r();return Array.isArray(e)?`<!--reactive-list-->${e.map(i=>s(i)).join("")}`:s(e)}if(typeof r=="string")return A(r);if(typeof r=="number")return String(r);if(Array.isArray(r))return r.map(e=>s(e)).join("");if(!r||typeof r!="object")return"";const{type:t,props:o}=r;if(t===Symbol.for("jsx.fragment"))return $(o.children).map(n=>s(n)).join("");if(typeof t=="string"&&t==="suspended"){const e=r,n=`async-${d++}`;return console.log(`⏳ Suspending: ${e._componentName} (${n})`),a.set(n,{id:n,promise:e.promise,componentName:e._componentName,errorFallback:e.errorFallback}),h(n,e._componentName)}if(typeof t=="function"){const e=t(o);if(D(e)){const n=`async-${d++}`,i=t.name||"AsyncComponent";return console.warn(`⚠️ DEPRECATED: Direct async component "${i}" detected. Please use suspend() HOF instead for better type safety and custom fallbacks.
|
|
26
|
+
Example: const ${i} = suspend(async (props) => { ... }, <Loading />, <Error />);`),console.log(`⏳ Suspending: ${i} (${n})`),a.set(n,{id:n,promise:e,componentName:i}),h(n,i)}return s(e)}if(typeof t=="string"){const e=w(o);if(v.has(t))return`<${t}${e} />`;const i=$(o.children).map(l=>{if(Array.isArray(l))return l.map(c=>s(c)).join("");if(typeof l=="function"){const c=l();return Array.isArray(c)?`<!--reactive-list-->${c.map(p=>s(p)).join("")}`:`<!--reactive-->${s(c)}`}return s(l)}).join("");return t==="html"?`<!DOCTYPE html>
|
|
27
|
+
<html${e}>${i}</html>`:`<${t}${e}>${i}</${t}>`}return""}class C{cacheDir;useDirectoryStructure;constructor(t={}){this.cacheDir=t.cacheDir||"./cache/html",this.useDirectoryStructure=t.useDirectoryStructure??!1}async save(t,o){const e=this.getFilepath(t);return await g.mkdir(m.dirname(e),{recursive:!0}),await Bun.write(e,o),console.log(`✅ Pre-rendered: ${t} → ${e}`),e}async load(t){const o=this.getFilepath(t),e=Bun.file(o);return await e.exists()?e:null}async clear(){await g.rm(this.cacheDir,{recursive:!0,force:!0}),await g.mkdir(this.cacheDir,{recursive:!0}),console.log("🗑️ Cache cleared")}async exists(t){const o=this.getFilepath(t);return await Bun.file(o).exists()}getFilepath(t){const o=t.replace(/^\/+|\/+$/g,"");if(this.useDirectoryStructure)return o===""?m.join(this.cacheDir,"index.html"):m.join(this.cacheDir,o,"index.html");{const e=o===""?"index.html":`${o.replace(/\//g,"-")}.html`;return m.join(this.cacheDir,e)}}}exports.PreRenderer=C;exports.renderToStream=P;exports.renderToString=H;exports.renderToStringAsync=T;
|
package/dist/server.js
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
* See LICENSE file for full terms.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import { mkdir as h, rm as
|
|
11
|
+
import { mkdir as h, rm as S } from "node:fs/promises";
|
|
12
12
|
import { dirname as b, join as y } from "node:path";
|
|
13
|
-
function v(
|
|
14
|
-
return
|
|
13
|
+
function v(r) {
|
|
14
|
+
return r.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
15
15
|
}
|
|
16
|
-
function E(
|
|
17
|
-
return
|
|
16
|
+
function E(r) {
|
|
17
|
+
return r.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
18
18
|
}
|
|
19
19
|
const w = /* @__PURE__ */ new Set([
|
|
20
20
|
"area",
|
|
@@ -31,33 +31,33 @@ const w = /* @__PURE__ */ new Set([
|
|
|
31
31
|
"track",
|
|
32
32
|
"wbr"
|
|
33
33
|
]);
|
|
34
|
-
function D(
|
|
35
|
-
return /^on[A-Z]/.test(
|
|
34
|
+
function D(r) {
|
|
35
|
+
return /^on[A-Z]/.test(r);
|
|
36
36
|
}
|
|
37
|
-
function H(
|
|
38
|
-
return Object.entries(
|
|
37
|
+
function H(r) {
|
|
38
|
+
return Object.entries(r).map(([t, o]) => `${t.replace(/[A-Z]/g, (n) => `-${n.toLowerCase()}`)}:${o}`).join(";");
|
|
39
39
|
}
|
|
40
|
-
function j(
|
|
41
|
-
const o = Object.entries(
|
|
40
|
+
function j(r, t = !0) {
|
|
41
|
+
const o = Object.entries(r).filter(([e, n]) => !(n == null || t && e === "children" || D(e))).map(([e, n]) => (e === "className" && (e = "class"), e === "style" && typeof n == "object" && (n = H(n)), typeof n == "boolean" ? n ? e : "" : `${e}="${E(String(n))}"`)).filter(Boolean).join(" ");
|
|
42
42
|
return o ? ` ${o}` : "";
|
|
43
43
|
}
|
|
44
|
-
function $(
|
|
45
|
-
return
|
|
44
|
+
function $(r) {
|
|
45
|
+
return r ? Array.isArray(r) ? r : [r] : [];
|
|
46
46
|
}
|
|
47
|
-
function P(
|
|
48
|
-
return
|
|
47
|
+
function P(r) {
|
|
48
|
+
return r && typeof r.then == "function";
|
|
49
49
|
}
|
|
50
|
-
function g(
|
|
51
|
-
return `<div id="${
|
|
50
|
+
function g(r, t) {
|
|
51
|
+
return `<div id="${r}" data-server-component="suspended" data-component-name="${t}">Loading...</div>`;
|
|
52
52
|
}
|
|
53
53
|
const a = /* @__PURE__ */ new Map();
|
|
54
54
|
let d = 0;
|
|
55
|
-
function N(
|
|
56
|
-
return a.clear(), d = 0, s(
|
|
55
|
+
function N(r) {
|
|
56
|
+
return a.clear(), d = 0, s(r);
|
|
57
57
|
}
|
|
58
|
-
async function T(
|
|
58
|
+
async function T(r) {
|
|
59
59
|
a.clear(), d = 0;
|
|
60
|
-
let
|
|
60
|
+
let t = s(r);
|
|
61
61
|
for (; a.size > 0; ) {
|
|
62
62
|
console.log(
|
|
63
63
|
`🔄 Resolving ${a.size} async components in parallel...`
|
|
@@ -77,27 +77,27 @@ async function T(t) {
|
|
|
77
77
|
try {
|
|
78
78
|
console.log(`✅ Resolved: ${c} (${l})`);
|
|
79
79
|
const f = s(p.value);
|
|
80
|
-
|
|
80
|
+
t = t.replace(m, f);
|
|
81
81
|
} catch (f) {
|
|
82
82
|
console.error(`❌ Error rendering ${c}:`, f);
|
|
83
|
-
const
|
|
83
|
+
const A = u ? s(
|
|
84
84
|
typeof u == "function" ? u(f) : u
|
|
85
85
|
) : `<div class="error">Error rendering ${c}</div>`;
|
|
86
|
-
|
|
86
|
+
t = t.replace(m, A);
|
|
87
87
|
}
|
|
88
88
|
else {
|
|
89
89
|
console.error(`❌ Error resolving ${c}:`, p.reason);
|
|
90
90
|
const f = u ? s(
|
|
91
91
|
typeof u == "function" ? u(p.reason) : u
|
|
92
92
|
) : `<div class="error">Error loading ${c}</div>`;
|
|
93
|
-
|
|
93
|
+
t = t.replace(m, f);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
return console.log("✅ All async components resolved!"),
|
|
97
|
+
return console.log("✅ All async components resolved!"), t;
|
|
98
98
|
}
|
|
99
|
-
async function* F(
|
|
100
|
-
for (a.clear(), d = 0, yield s(
|
|
99
|
+
async function* F(r) {
|
|
100
|
+
for (a.clear(), d = 0, yield s(r); a.size > 0; ) {
|
|
101
101
|
const o = Array.from(a.entries());
|
|
102
102
|
a.clear();
|
|
103
103
|
const e = o.map(
|
|
@@ -128,22 +128,24 @@ async function* F(t) {
|
|
|
128
128
|
yield await n;
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
function s(
|
|
132
|
-
if (typeof
|
|
133
|
-
const e =
|
|
131
|
+
function s(r) {
|
|
132
|
+
if (typeof r == "function") {
|
|
133
|
+
const e = r();
|
|
134
134
|
return Array.isArray(e) ? `<!--reactive-list-->${e.map((i) => s(i)).join("")}` : s(e);
|
|
135
135
|
}
|
|
136
|
-
if (typeof
|
|
137
|
-
return v(
|
|
138
|
-
if (typeof
|
|
139
|
-
return String(
|
|
140
|
-
if (
|
|
136
|
+
if (typeof r == "string")
|
|
137
|
+
return v(r);
|
|
138
|
+
if (typeof r == "number")
|
|
139
|
+
return String(r);
|
|
140
|
+
if (Array.isArray(r))
|
|
141
|
+
return r.map((e) => s(e)).join("");
|
|
142
|
+
if (!r || typeof r != "object")
|
|
141
143
|
return "";
|
|
142
|
-
const { type:
|
|
143
|
-
if (
|
|
144
|
+
const { type: t, props: o } = r;
|
|
145
|
+
if (t === Symbol.for("jsx.fragment"))
|
|
144
146
|
return $(o.children).map((n) => s(n)).join("");
|
|
145
|
-
if (typeof
|
|
146
|
-
const e =
|
|
147
|
+
if (typeof t == "string" && t === "suspended") {
|
|
148
|
+
const e = r, n = `async-${d++}`;
|
|
147
149
|
return console.log(
|
|
148
150
|
`⏳ Suspending: ${e._componentName} (${n})`
|
|
149
151
|
), a.set(n, {
|
|
@@ -154,10 +156,10 @@ function s(t) {
|
|
|
154
156
|
// Store custom error fallback
|
|
155
157
|
}), g(n, e._componentName);
|
|
156
158
|
}
|
|
157
|
-
if (typeof
|
|
158
|
-
const e =
|
|
159
|
+
if (typeof t == "function") {
|
|
160
|
+
const e = t(o);
|
|
159
161
|
if (P(e)) {
|
|
160
|
-
const n = `async-${d++}`, i =
|
|
162
|
+
const n = `async-${d++}`, i = t.name || "AsyncComponent";
|
|
161
163
|
return console.warn(
|
|
162
164
|
`⚠️ DEPRECATED: Direct async component "${i}" detected. Please use suspend() HOF instead for better type safety and custom fallbacks.
|
|
163
165
|
Example: const ${i} = suspend(async (props) => { ... }, <Loading />, <Error />);`
|
|
@@ -169,10 +171,10 @@ Example: const ${i} = suspend(async (props) => { ... }, <Loading />, <Error />);
|
|
|
169
171
|
}
|
|
170
172
|
return s(e);
|
|
171
173
|
}
|
|
172
|
-
if (typeof
|
|
174
|
+
if (typeof t == "string") {
|
|
173
175
|
const e = j(o);
|
|
174
|
-
if (w.has(
|
|
175
|
-
return `<${
|
|
176
|
+
if (w.has(t))
|
|
177
|
+
return `<${t}${e} />`;
|
|
176
178
|
const i = $(o.children).map((l) => {
|
|
177
179
|
if (Array.isArray(l))
|
|
178
180
|
return l.map((c) => s(c)).join("");
|
|
@@ -182,34 +184,34 @@ Example: const ${i} = suspend(async (props) => { ... }, <Loading />, <Error />);
|
|
|
182
184
|
}
|
|
183
185
|
return s(l);
|
|
184
186
|
}).join("");
|
|
185
|
-
return
|
|
186
|
-
<html${e}>${i}</html>` : `<${
|
|
187
|
+
return t === "html" ? `<!DOCTYPE html>
|
|
188
|
+
<html${e}>${i}</html>` : `<${t}${e}>${i}</${t}>`;
|
|
187
189
|
}
|
|
188
190
|
return "";
|
|
189
191
|
}
|
|
190
192
|
class z {
|
|
191
193
|
cacheDir;
|
|
192
194
|
useDirectoryStructure;
|
|
193
|
-
constructor(
|
|
194
|
-
this.cacheDir =
|
|
195
|
+
constructor(t = {}) {
|
|
196
|
+
this.cacheDir = t.cacheDir || "./cache/html", this.useDirectoryStructure = t.useDirectoryStructure ?? !1;
|
|
195
197
|
}
|
|
196
|
-
async save(
|
|
197
|
-
const e = this.getFilepath(
|
|
198
|
-
return await h(b(e), { recursive: !0 }), await Bun.write(e, o), console.log(`✅ Pre-rendered: ${
|
|
198
|
+
async save(t, o) {
|
|
199
|
+
const e = this.getFilepath(t);
|
|
200
|
+
return await h(b(e), { recursive: !0 }), await Bun.write(e, o), console.log(`✅ Pre-rendered: ${t} → ${e}`), e;
|
|
199
201
|
}
|
|
200
|
-
async load(
|
|
201
|
-
const o = this.getFilepath(
|
|
202
|
+
async load(t) {
|
|
203
|
+
const o = this.getFilepath(t), e = Bun.file(o);
|
|
202
204
|
return await e.exists() ? e : null;
|
|
203
205
|
}
|
|
204
206
|
async clear() {
|
|
205
|
-
await
|
|
207
|
+
await S(this.cacheDir, { recursive: !0, force: !0 }), await h(this.cacheDir, { recursive: !0 }), console.log("🗑️ Cache cleared");
|
|
206
208
|
}
|
|
207
|
-
async exists(
|
|
208
|
-
const o = this.getFilepath(
|
|
209
|
+
async exists(t) {
|
|
210
|
+
const o = this.getFilepath(t);
|
|
209
211
|
return await Bun.file(o).exists();
|
|
210
212
|
}
|
|
211
|
-
getFilepath(
|
|
212
|
-
const o =
|
|
213
|
+
getFilepath(t) {
|
|
214
|
+
const o = t.replace(/^\/+|\/+$/g, "");
|
|
213
215
|
if (this.useDirectoryStructure)
|
|
214
216
|
return o === "" ? y(this.cacheDir, "index.html") : y(this.cacheDir, o, "index.html");
|
|
215
217
|
{
|