@seip/bluebird-js 0.1.7 → 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/AGENTS.md +727 -620
- package/README.md +670 -607
- package/app/models/user.js +19 -0
- package/app/resources/html/home.html +137 -0
- package/app/resources/html/layouts/main.html +55 -0
- package/app/resources/html/notFound.html +14 -0
- package/app/resources/html/table.html +15 -0
- package/app/resources/html/terms.html +46 -0
- package/app/resources/index.html +27 -0
- package/app/resources/layouts/AppLayout.jsx +92 -0
- package/app/resources/main.jsx +18 -0
- package/app/resources/pages/Dashboard.jsx +93 -0
- package/app/resources/pages/Home.jsx +140 -0
- package/app/resources/pages/Login.jsx +35 -0
- package/app/resources/pages/NotFound.jsx +16 -0
- package/app/resources/pages/Register.jsx +55 -0
- package/app/resources/pages/TableDemo.jsx +85 -0
- package/app/resources/routes.jsx +59 -0
- package/app/resources/spa.js +73 -0
- package/app/resources/styles.css +9 -0
- package/app/routes/api.js +129 -50
- package/app/routes/auth.js +209 -0
- package/app/routes/web.js +89 -47
- package/core/ai.js +30 -10
- package/core/app.d.ts +2 -18
- package/core/app.js +987 -801
- package/core/auth.d.ts +2 -13
- package/core/auth.js +444 -326
- package/core/cli/build.js +37 -0
- package/core/cli/dev.js +155 -0
- package/core/cli/doctor.js +37 -2
- package/core/cli/export.js +191 -0
- package/core/cli/init.js +91 -322
- package/core/cli/nginx.js +1 -1
- package/core/cli/templates.js +139 -0
- package/core/cli/view.js +98 -161
- package/core/config.js +44 -2
- package/core/csrf.js +156 -24
- package/core/database.js +1184 -1131
- package/core/debug.js +1 -1
- package/core/html.js +8 -1
- package/core/index.d.ts +126 -310
- package/core/paths.js +55 -0
- package/core/ratelimit.js +10 -1
- package/core/react/client/api.js +239 -0
- package/core/react/client/auth.js +236 -0
- package/core/react/client/components.js +1321 -0
- package/core/react/client/datatable.js +595 -0
- package/core/react/client/i18n.js +440 -0
- package/core/react/client/index.d.ts +314 -0
- package/core/react/client/index.js +50 -0
- package/core/react/client/router.js +26 -0
- package/core/react/client/theme.css +512 -0
- package/core/render-cache.js +250 -212
- package/core/render.d.ts +25 -21
- package/core/render.js +738 -736
- package/core/router.d.ts +2 -3
- package/core/router.js +171 -171
- package/core/seo.js +10 -0
- package/core/spa.js +599 -0
- package/core/view-loader.js +155 -613
- package/core/vite-plugin.d.ts +18 -0
- package/core/vite-plugin.js +162 -0
- package/core/vite.js +250 -145
- package/index.js +22 -18
- package/jsconfig.json +5 -3
- package/package.json +114 -92
- package/production.md +8 -3
- package/public/js/bluebird.js +1886 -0
- package/templates/_auth/app/database.js +19 -0
- package/templates/_auth/app/models/user.js +22 -0
- package/templates/_auth/app/routes/auth-router.js +304 -0
- package/templates/_base/app/resources/html/layouts/main.html +55 -0
- package/templates/_base/app/resources/html/notFound.html +14 -0
- package/templates/_base/app/resources/index.html +27 -0
- package/templates/_base/app/resources/styles.css +9 -0
- package/templates/api/app/models/note.js +18 -0
- package/templates/api/app/routes/api.js +82 -0
- package/templates/api/app/routes/auth.js +11 -0
- package/templates/api/index.js +19 -0
- package/templates/api/template.json +13 -0
- package/templates/auth/app/mail.js +11 -0
- package/templates/auth/app/models/password_reset.js +18 -0
- package/templates/auth/app/resources/html/home.html +21 -0
- package/templates/auth/app/resources/html/partials/nav.html +2 -0
- package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
- package/templates/auth/app/resources/main.jsx +18 -0
- package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
- package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
- package/templates/auth/app/resources/pages/Login.jsx +36 -0
- package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
- package/templates/auth/app/resources/pages/Profile.jsx +99 -0
- package/templates/auth/app/resources/pages/Register.jsx +50 -0
- package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
- package/templates/auth/app/resources/routes.jsx +54 -0
- package/templates/auth/app/resources/spa.js +29 -0
- package/templates/auth/app/routes/api.js +17 -0
- package/templates/auth/app/routes/auth.js +13 -0
- package/templates/auth/app/routes/web.js +21 -0
- package/templates/auth/index.js +19 -0
- package/templates/auth/template.json +15 -0
- package/templates/blank/app/models/.gitkeep +0 -0
- package/templates/blank/app/resources/html/home.html +27 -0
- package/templates/blank/app/resources/html/partials/nav.html +1 -0
- package/templates/blank/app/resources/main.jsx +16 -0
- package/templates/blank/app/resources/pages/Home.jsx +42 -0
- package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
- package/templates/blank/app/resources/routes.jsx +15 -0
- package/templates/blank/app/resources/spa.js +24 -0
- package/templates/blank/app/routes/api.js +11 -0
- package/templates/blank/app/routes/web.js +21 -0
- package/templates/blank/index.js +13 -0
- package/templates/blank/template.json +12 -0
- package/templates/blog/app/lib/blog.js +149 -0
- package/templates/blog/app/models/post.js +22 -0
- package/templates/blog/app/resources/html/home.html +28 -0
- package/templates/blog/app/resources/html/not-found.html +5 -0
- package/templates/blog/app/resources/html/partials/nav.html +2 -0
- package/templates/blog/app/resources/html/partials/post-card.html +9 -0
- package/templates/blog/app/resources/html/post.html +8 -0
- package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
- package/templates/blog/app/resources/main.jsx +18 -0
- package/templates/blog/app/resources/pages/Login.jsx +31 -0
- package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
- package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
- package/templates/blog/app/resources/pages/Posts.jsx +83 -0
- package/templates/blog/app/resources/pages/Register.jsx +43 -0
- package/templates/blog/app/resources/routes.jsx +51 -0
- package/templates/blog/app/resources/spa.js +25 -0
- package/templates/blog/app/resources/styles.css +40 -0
- package/templates/blog/app/routes/api.js +77 -0
- package/templates/blog/app/routes/auth.js +10 -0
- package/templates/blog/app/routes/web.js +114 -0
- package/templates/blog/index.js +21 -0
- package/templates/blog/template.json +14 -0
- package/vite.config.js +11 -0
- package/app/views/components/Header.jsx +0 -42
- package/app/views/components/LiveCounter.jsx +0 -67
- package/app/views/layouts/main/view.jsx +0 -201
- package/app/views/legal/terms.html +0 -29
- package/app/views/pages/about.jsx +0 -98
- package/app/views/pages/dashboard.jsx +0 -278
- package/app/views/pages/index.jsx +0 -123
- package/app/views/pages/login.jsx +0 -141
- package/core/actions.d.ts +0 -4
- package/core/actions.js +0 -355
- package/core/jsx.d.ts +0 -38
- package/core/jsx.js +0 -1597
- package/core/vite.d.ts +0 -3
- package/public/js/bluebird-client.js +0 -3265
- package/public/js/tailwind.js +0 -8
package/core/spa.js
ADDED
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import Config from "./config.js";
|
|
4
|
+
import Csrf from "./csrf.js";
|
|
5
|
+
import Seo from "./seo.js";
|
|
6
|
+
import RenderCache from "./render-cache.js";
|
|
7
|
+
import ViewLoader from "./view-loader.js";
|
|
8
|
+
import ViteBridge from "./vite.js";
|
|
9
|
+
import { escapeHtml, safeJson, SafeHtml } from "./html.js";
|
|
10
|
+
import { PATHS, defaultEntry } from "./paths.js";
|
|
11
|
+
import Render, {
|
|
12
|
+
applyCspNonce,
|
|
13
|
+
buildCacheKey,
|
|
14
|
+
detokenizeFromCache,
|
|
15
|
+
isAuthenticatedRequest,
|
|
16
|
+
tokenizeForCache,
|
|
17
|
+
} from "./render.js";
|
|
18
|
+
|
|
19
|
+
const DEBUG = Config.props().debug;
|
|
20
|
+
const ATTR_NAME = /^[A-Za-z_:][\w:.-]*$/;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Built-in SPA shell used when `app/resources/index.html` does not exist.
|
|
24
|
+
*/
|
|
25
|
+
export const DEFAULT_APP_HTML = `<!DOCTYPE html>
|
|
26
|
+
<html lang="{{ lang }}"{{{ htmlAttrs }}}>
|
|
27
|
+
<head>
|
|
28
|
+
<meta charset="UTF-8">
|
|
29
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
30
|
+
<meta name="color-scheme" content="light dark">
|
|
31
|
+
<script>(function(){try{var k="bb-theme",t=localStorage.getItem(k),m=document.querySelector('meta[name="color-scheme"]'),d=document.documentElement,r=t==="light"||t==="dark"?t:(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light");d.classList.toggle("dark",r==="dark");d.setAttribute("data-theme",r);if(m)m.content=t==="light"||t==="dark"?t:"light dark";}catch(e){}})();</script>
|
|
32
|
+
{{{ head }}}
|
|
33
|
+
</head>
|
|
34
|
+
<body{{{ bodyAttrs }}}>
|
|
35
|
+
<div id="root" class="{{ rootClass }}">{{{ skeleton }}}</div>
|
|
36
|
+
{{{ scripts }}}
|
|
37
|
+
</body>
|
|
38
|
+
</html>`;
|
|
39
|
+
|
|
40
|
+
const SKELETON_CSS = `<style data-bb-skeleton>
|
|
41
|
+
.bb-sk-root{width:100%;min-height:100vh;box-sizing:border-box;--bb-sk-bg:var(--muted,rgba(161,161,170,.16));--bb-sk-border:var(--border,rgba(228,228,231,.8));--bb-sk-surface:var(--card,rgba(255,255,255,.85))}
|
|
42
|
+
html.dark .bb-sk-root,html[data-theme="dark"] .bb-sk-root{--bb-sk-bg:var(--muted,rgba(63,63,70,.45));--bb-sk-border:var(--border,rgba(255,255,255,.09));--bb-sk-surface:var(--card,rgba(24,24,27,.8))}
|
|
43
|
+
@media (prefers-color-scheme:dark){html:not([data-theme="light"]):not(.light) .bb-sk-root{--bb-sk-bg:var(--muted,rgba(63,63,70,.45));--bb-sk-border:var(--border,rgba(255,255,255,.09));--bb-sk-surface:var(--card,rgba(24,24,27,.8))}}
|
|
44
|
+
@keyframes bb-sk-pulse{0%,100%{opacity:1}50%{opacity:.45}}
|
|
45
|
+
.bb-sk-bar,.bb-sk-box{background:var(--bb-sk-bg);border-radius:.625rem;animation:bb-sk-pulse 1.5s ease-in-out infinite}
|
|
46
|
+
.bb-sk-nav{height:3.5rem;border-bottom:1px solid var(--bb-sk-border);display:flex;align-items:center;justify-content:space-between;padding:0 1.5rem;width:100%;box-sizing:border-box}
|
|
47
|
+
.bb-sk-wrap{width:100%;max-width:72rem;margin:0 auto;padding:2.5rem 1.5rem;box-sizing:border-box}
|
|
48
|
+
.bb-sk-app{display:grid;grid-template-columns:1fr;gap:1.5rem}
|
|
49
|
+
.bb-sk-side{display:none}
|
|
50
|
+
@media (min-width:768px){.bb-sk-app{grid-template-columns:14rem 1fr}.bb-sk-side{display:flex;flex-direction:column;gap:.75rem}}
|
|
51
|
+
.bb-sk-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(14rem,1fr));gap:1rem;width:100%}
|
|
52
|
+
.bb-sk-auth{width:100%;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:1.5rem;box-sizing:border-box}
|
|
53
|
+
.bb-sk-card{width:100%;max-width:26rem;border:1px solid var(--bb-sk-border);background:var(--bb-sk-surface);border-radius:1rem;padding:1.5rem;box-sizing:border-box;display:flex;flex-direction:column;gap:1rem}
|
|
54
|
+
</style>`;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Escapes special regular expression characters.
|
|
58
|
+
* @param {string} value
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
function escapeRegex(value) {
|
|
62
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Joins two URL path segments cleanly.
|
|
67
|
+
* @param {string} base
|
|
68
|
+
* @param {string} sub
|
|
69
|
+
* @returns {string}
|
|
70
|
+
*/
|
|
71
|
+
export function joinPaths(base, sub) {
|
|
72
|
+
const cleanBase = String(base || "/").replace(/\/+$/, "") || "";
|
|
73
|
+
const cleanSub = String(sub || "/").replace(/^\/+/, "");
|
|
74
|
+
if (!cleanSub) return cleanBase || "/";
|
|
75
|
+
return `${cleanBase}/${cleanSub}` || "/";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Normalizes a meta key (relative to mount or absolute) into a canonical sub-path under the SPA mount.
|
|
80
|
+
* @param {string} rawKey
|
|
81
|
+
* @param {string} fullMount
|
|
82
|
+
* @returns {string}
|
|
83
|
+
*/
|
|
84
|
+
export function normalizeMetaKey(rawKey, fullMount) {
|
|
85
|
+
const trimmed = String(rawKey || "").trim();
|
|
86
|
+
if (trimmed === "*") return "*";
|
|
87
|
+
const mountClean = fullMount === "/" ? "/" : `/${fullMount.replace(/^\/+|\/+$/g, "")}`;
|
|
88
|
+
const clean = trimmed.replace(/^\/+|\/+$/g, "");
|
|
89
|
+
if (!clean) return "/";
|
|
90
|
+
const withSlash = `/${clean}`;
|
|
91
|
+
if (mountClean !== "/") {
|
|
92
|
+
if (withSlash === mountClean) return "/";
|
|
93
|
+
if (withSlash.startsWith(`${mountClean}/`)) {
|
|
94
|
+
return withSlash.slice(mountClean.length) || "/";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return withSlash;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Compiles a normalized sub-path pattern for route matching and specificity ranking.
|
|
102
|
+
* @param {string} pattern
|
|
103
|
+
* @returns {{ pattern: string, regex: RegExp, keys: string[], score: number }}
|
|
104
|
+
*/
|
|
105
|
+
function compileSubPattern(pattern) {
|
|
106
|
+
const keys = [];
|
|
107
|
+
let score = 0;
|
|
108
|
+
const parts = pattern
|
|
109
|
+
.split("/")
|
|
110
|
+
.filter(Boolean)
|
|
111
|
+
.map((seg) => {
|
|
112
|
+
if (seg.startsWith("*")) {
|
|
113
|
+
keys.push(seg.slice(1) || "wildcard");
|
|
114
|
+
score += 1;
|
|
115
|
+
return "(?:/(.*))?";
|
|
116
|
+
}
|
|
117
|
+
if (seg.startsWith(":")) {
|
|
118
|
+
keys.push(seg.slice(1));
|
|
119
|
+
score += 10;
|
|
120
|
+
return "/([^/]+)";
|
|
121
|
+
}
|
|
122
|
+
score += 100;
|
|
123
|
+
return `/${escapeRegex(seg)}`;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const source = parts.length ? `^${parts.join("")}/?$` : "^/?$";
|
|
127
|
+
return { pattern, regex: new RegExp(source), keys, score: score * 10 + parts.length };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Compiles and sorts the `meta` tree entries into a deterministic matcher list.
|
|
132
|
+
* @param {Record<string, any>} metaMap
|
|
133
|
+
* @param {string} fullMount
|
|
134
|
+
* @returns {{ wildcard: any, routes: Array<{ pattern: string, regex: RegExp, keys: string[], score: number, value: any }> }}
|
|
135
|
+
*/
|
|
136
|
+
export function compileMetaTree(metaMap = {}, fullMount = "/") {
|
|
137
|
+
let wildcard = null;
|
|
138
|
+
const routes = [];
|
|
139
|
+
|
|
140
|
+
for (const [rawKey, value] of Object.entries(metaMap || {})) {
|
|
141
|
+
const normalized = normalizeMetaKey(rawKey, fullMount);
|
|
142
|
+
if (normalized === "*") {
|
|
143
|
+
wildcard = value;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
routes.push({ ...compileSubPattern(normalized), value });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
routes.sort((a, b) => b.score - a.score);
|
|
150
|
+
return { wildcard, routes };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Matches a sub-path against the compiled meta routes.
|
|
155
|
+
* @param {Array<Object>} routes
|
|
156
|
+
* @param {string} subPath
|
|
157
|
+
* @returns {{ value: any, params: Record<string, string>, pattern: string }|null}
|
|
158
|
+
*/
|
|
159
|
+
function matchMetaRoute(routes, subPath) {
|
|
160
|
+
let decoded = subPath || "/";
|
|
161
|
+
try {
|
|
162
|
+
decoded = decodeURI(decoded);
|
|
163
|
+
} catch {}
|
|
164
|
+
for (const route of routes) {
|
|
165
|
+
const match = route.regex.exec(decoded);
|
|
166
|
+
if (!match) continue;
|
|
167
|
+
const params = {};
|
|
168
|
+
route.keys.forEach((key, i) => {
|
|
169
|
+
if (match[i + 1] !== undefined) {
|
|
170
|
+
try {
|
|
171
|
+
params[key] = decodeURIComponent(match[i + 1]);
|
|
172
|
+
} catch {
|
|
173
|
+
params[key] = match[i + 1];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return { value: route.value, params, pattern: route.pattern };
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Formats an HTML attribute dictionary into an attribute string.
|
|
184
|
+
* @param {Record<string, any>} attrs
|
|
185
|
+
* @param {Set<string>} [skip]
|
|
186
|
+
* @returns {string}
|
|
187
|
+
*/
|
|
188
|
+
function renderAttrs(attrs = {}, skip = new Set()) {
|
|
189
|
+
let out = "";
|
|
190
|
+
for (const [rawKey, value] of Object.entries(attrs || {})) {
|
|
191
|
+
if (value === false || value === null || value === undefined) continue;
|
|
192
|
+
const key = rawKey === "className" ? "class" : rawKey === "httpEquiv" ? "http-equiv" : rawKey;
|
|
193
|
+
if (skip.has(key) || !ATTR_NAME.test(key)) continue;
|
|
194
|
+
out += value === true ? ` ${key}` : ` ${key}="${escapeHtml(String(value))}"`;
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Normalizes a tag entry (string or tag descriptor object) into an HTML element string.
|
|
201
|
+
* @param {string|Object} item
|
|
202
|
+
* @param {"head"|"body"} target
|
|
203
|
+
* @returns {string}
|
|
204
|
+
*/
|
|
205
|
+
function renderCustomTag(item, target = "head") {
|
|
206
|
+
if (!item) return "";
|
|
207
|
+
if (typeof item === "string") return item;
|
|
208
|
+
if (typeof item === "object") {
|
|
209
|
+
const tag = String(item.tag || (target === "body" ? "script" : item.rel ? "link" : item.src || item.content ? "script" : "meta"));
|
|
210
|
+
const { tag: _tag, content, text, ...attrs } = item;
|
|
211
|
+
if (tag === "meta" || tag === "link") {
|
|
212
|
+
return `<${tag}${renderAttrs(attrs)}>`;
|
|
213
|
+
}
|
|
214
|
+
const inner = content !== undefined ? String(content) : text !== undefined ? escapeHtml(String(text)) : "";
|
|
215
|
+
const safeInner = tag === "script" ? inner.replace(/<\/script/gi, "<\\/script") : inner;
|
|
216
|
+
return `<${tag}${renderAttrs(attrs)}>${safeInner}</${tag}>`;
|
|
217
|
+
}
|
|
218
|
+
return "";
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Renders the inline skeleton markup for a given variant.
|
|
223
|
+
* @param {"app"|"auth"|"page"|string|false} variant
|
|
224
|
+
* @returns {string}
|
|
225
|
+
*/
|
|
226
|
+
export function renderSkeletonHtml(variant) {
|
|
227
|
+
if (variant === false || variant === null || variant === "") return "";
|
|
228
|
+
if (typeof variant === "string" && variant.trim().startsWith("<")) return variant;
|
|
229
|
+
|
|
230
|
+
if (variant === "auth") {
|
|
231
|
+
return `<div class="bb-sk-root bb-sk-auth" aria-busy="true" role="status"><div class="bb-sk-card"><div class="bb-sk-bar" style="height:1.75rem;width:55%"></div><div class="bb-sk-bar" style="height:1rem;width:80%"></div><div class="bb-sk-bar" style="height:2.5rem;width:100%"></div><div class="bb-sk-bar" style="height:2.5rem;width:100%"></div><div class="bb-sk-bar" style="height:2.5rem;width:100%"></div></div></div>`;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (variant === "page") {
|
|
235
|
+
return `<div class="bb-sk-root" aria-busy="true" role="status"><div class="bb-sk-nav"><div class="bb-sk-bar" style="height:1.5rem;width:8rem"></div><div style="display:flex;gap:.75rem"><div class="bb-sk-bar" style="height:1.5rem;width:4rem"></div><div class="bb-sk-bar" style="height:1.5rem;width:4rem"></div></div></div><div class="bb-sk-wrap"><div class="bb-sk-bar" style="height:2.5rem;width:60%;max-width:28rem;margin-bottom:1rem"></div><div class="bb-sk-bar" style="height:1rem;width:85%;max-width:40rem;margin-bottom:2rem"></div><div class="bb-sk-grid"><div class="bb-sk-box" style="height:9rem"></div><div class="bb-sk-box" style="height:9rem"></div><div class="bb-sk-box" style="height:9rem"></div></div></div></div>`;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return `<div class="bb-sk-root" aria-busy="true" role="status"><div class="bb-sk-nav"><div class="bb-sk-bar" style="height:1.5rem;width:8rem"></div><div style="display:flex;gap:.75rem"><div class="bb-sk-bar" style="height:1.5rem;width:5rem"></div><div class="bb-sk-bar" style="height:1.5rem;width:5rem"></div></div></div><div class="bb-sk-wrap bb-sk-app"><div class="bb-sk-side"><div class="bb-sk-bar" style="height:2rem;width:100%"></div><div class="bb-sk-bar" style="height:2rem;width:85%"></div><div class="bb-sk-bar" style="height:2rem;width:90%"></div><div class="bb-sk-bar" style="height:2rem;width:75%"></div></div><div style="display:flex;flex-direction:column;gap:1.25rem;width:100%"><div class="bb-sk-bar" style="height:2.25rem;width:45%;max-width:20rem"></div><div class="bb-sk-grid"><div class="bb-sk-box" style="height:8.5rem"></div><div class="bb-sk-box" style="height:8.5rem"></div><div class="bb-sk-box" style="height:8.5rem"></div></div><div class="bb-sk-box" style="height:14rem;width:100%"></div></div></div></div>`;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Merges base SPA options, wildcard `"*"` meta, and route-specific meta into one configuration object.
|
|
243
|
+
* @param {Object} base
|
|
244
|
+
* @param {Object} wildcard
|
|
245
|
+
* @param {Object} routeMeta
|
|
246
|
+
* @returns {Object}
|
|
247
|
+
*/
|
|
248
|
+
function mergeSpaMeta(base = {}, wildcard = {}, routeMeta = {}) {
|
|
249
|
+
const out = { ...base, ...wildcard, ...routeMeta };
|
|
250
|
+
const combine = (key) => {
|
|
251
|
+
const parts = [];
|
|
252
|
+
for (const src of [base[key], wildcard[key], routeMeta[key]]) {
|
|
253
|
+
if (!src) continue;
|
|
254
|
+
if (Array.isArray(src)) parts.push(...src);
|
|
255
|
+
else parts.push(src);
|
|
256
|
+
}
|
|
257
|
+
return parts;
|
|
258
|
+
};
|
|
259
|
+
out.head = combine("head");
|
|
260
|
+
out.body = combine("body");
|
|
261
|
+
out.metaTags = combine("metaTags");
|
|
262
|
+
out.links = combine("links");
|
|
263
|
+
out.htmlAttrs = { ...(base.htmlAttrs || {}), ...(wildcard.htmlAttrs || {}), ...(routeMeta.htmlAttrs || {}) };
|
|
264
|
+
out.bodyAttrs = { ...(base.bodyAttrs || {}), ...(wildcard.bodyAttrs || {}), ...(routeMeta.bodyAttrs || {}) };
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Builds `<head>` HTML tags from merged SPA metadata and Vite assets.
|
|
270
|
+
* @param {Object} merged
|
|
271
|
+
* @param {string} fullPath
|
|
272
|
+
* @param {{ head: string, body: string }} viteAssets
|
|
273
|
+
* @returns {string}
|
|
274
|
+
*/
|
|
275
|
+
function buildSpaHeadTags(merged, fullPath, viteAssets) {
|
|
276
|
+
const tags = [];
|
|
277
|
+
const rawTitle = merged.title ? String(merged.title) : "";
|
|
278
|
+
const title =
|
|
279
|
+
rawTitle && merged.titleTemplate && merged.titleTemplate.includes("%s")
|
|
280
|
+
? merged.titleTemplate.replace("%s", rawTitle)
|
|
281
|
+
: rawTitle || (merged.titleTemplate ? merged.titleTemplate.replace("%s", "").replace(/^[\s—|-]+|[\s—|-]+$/g, "") : "");
|
|
282
|
+
|
|
283
|
+
if (title) {
|
|
284
|
+
tags.push(`<title>${escapeHtml(title)}</title>`);
|
|
285
|
+
tags.push(`<meta property="og:title" content="${escapeHtml(title)}">`);
|
|
286
|
+
tags.push(`<meta name="twitter:title" content="${escapeHtml(title)}">`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (merged.description) {
|
|
290
|
+
const desc = escapeHtml(String(merged.description));
|
|
291
|
+
tags.push(`<meta name="description" content="${desc}">`);
|
|
292
|
+
tags.push(`<meta property="og:description" content="${desc}">`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (merged.keywords) {
|
|
296
|
+
const kw = Array.isArray(merged.keywords) ? merged.keywords.join(", ") : String(merged.keywords);
|
|
297
|
+
tags.push(`<meta name="keywords" content="${escapeHtml(kw)}">`);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (merged.author) {
|
|
301
|
+
tags.push(`<meta name="author" content="${escapeHtml(String(merged.author))}">`);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (merged.ogType) {
|
|
305
|
+
tags.push(`<meta property="og:type" content="${escapeHtml(String(merged.ogType))}">`);
|
|
306
|
+
} else if (title || merged.description) {
|
|
307
|
+
tags.push(`<meta property="og:type" content="website">`);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (merged.image) {
|
|
311
|
+
const img = escapeHtml(String(merged.image));
|
|
312
|
+
tags.push(`<meta property="og:image" content="${img}">`);
|
|
313
|
+
tags.push(`<meta name="twitter:card" content="summary_large_image">`);
|
|
314
|
+
tags.push(`<meta name="twitter:image" content="${img}">`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let canonical = "";
|
|
318
|
+
if (typeof merged.canonical === "string" && merged.canonical.length > 0) {
|
|
319
|
+
canonical = merged.canonical;
|
|
320
|
+
} else if (merged.canonical === true) {
|
|
321
|
+
const appUrl = (merged.baseUrl || Config.props().appUrl || "").replace(/\/$/, "");
|
|
322
|
+
canonical = `${appUrl}${fullPath}`;
|
|
323
|
+
}
|
|
324
|
+
if (canonical) {
|
|
325
|
+
tags.push(`<link rel="canonical" href="${escapeHtml(canonical)}">`);
|
|
326
|
+
tags.push(`<meta property="og:url" content="${escapeHtml(canonical)}">`);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (merged.robots !== undefined && merged.robots !== true) {
|
|
330
|
+
const robotsContent = merged.robots === false ? "noindex, nofollow" : String(merged.robots);
|
|
331
|
+
tags.push(`<meta name="robots" content="${escapeHtml(robotsContent)}">`);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
for (const m of merged.metaTags || []) {
|
|
335
|
+
if (m && typeof m === "object") tags.push(`<meta${renderAttrs(m)}>`);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
for (const l of merged.links || []) {
|
|
339
|
+
if (l && typeof l === "object") tags.push(`<link${renderAttrs(l)}>`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const ldRaw = merged.jsonld ?? merged.jsonLd;
|
|
343
|
+
if (ldRaw) {
|
|
344
|
+
const list = Array.isArray(ldRaw) ? ldRaw : [ldRaw];
|
|
345
|
+
for (const item of list) {
|
|
346
|
+
if (item && typeof item === "object") {
|
|
347
|
+
tags.push(`<script type="application/ld+json">${safeJson(item)}</script>`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (viteAssets.head) tags.push(viteAssets.head);
|
|
353
|
+
|
|
354
|
+
for (const custom of merged.head || []) {
|
|
355
|
+
const rendered = renderCustomTag(custom, "head");
|
|
356
|
+
if (rendered) tags.push(rendered);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (merged.skeleton !== false) {
|
|
360
|
+
tags.push(SKELETON_CSS);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return tags.join("\n ");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Registers SEO routes for static entries in a SPA meta tree when SEO indexing is enabled.
|
|
368
|
+
* @param {string} fullMount
|
|
369
|
+
* @param {Object} options
|
|
370
|
+
* @param {string[]} [languages=[]]
|
|
371
|
+
*/
|
|
372
|
+
export function registerSpaSeoRoutes(fullMount, options = {}, languages = []) {
|
|
373
|
+
const metaMap = options.meta || {};
|
|
374
|
+
const { wildcard, routes } = compileMetaTree(metaMap, fullMount);
|
|
375
|
+
const wildcardObj = typeof wildcard === "object" && wildcard !== null ? wildcard : {};
|
|
376
|
+
|
|
377
|
+
const isIndexable = (entry) => {
|
|
378
|
+
const mergedRobots = entry?.robots !== undefined ? entry.robots : (wildcardObj.robots !== undefined ? wildcardObj.robots : options.robots);
|
|
379
|
+
if (mergedRobots === false) return false;
|
|
380
|
+
if (typeof mergedRobots === "string" && mergedRobots.toLowerCase().includes("noindex")) return false;
|
|
381
|
+
return true;
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
if (routes.length === 0) {
|
|
385
|
+
if (isIndexable(options)) {
|
|
386
|
+
Seo.registerRoute("/", fullMount, languages);
|
|
387
|
+
}
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
let hasRoot = false;
|
|
392
|
+
for (const r of routes) {
|
|
393
|
+
if (r.pattern === "/") hasRoot = true;
|
|
394
|
+
if (r.pattern.includes(":") || r.pattern.includes("*")) continue;
|
|
395
|
+
const obj = typeof r.value === "object" && r.value !== null ? r.value : {};
|
|
396
|
+
if (!isIndexable(obj)) continue;
|
|
397
|
+
const target = r.pattern === "/" ? fullMount : joinPaths(fullMount, r.pattern);
|
|
398
|
+
Seo.registerRoute("/", target, languages);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (!hasRoot && isIndexable(options)) {
|
|
402
|
+
Seo.registerRoute("/", fullMount, languages);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
const templateCache = new Map();
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Loads the SPA HTML shell. Resolution order: `template` (project-relative or absolute path, then a view
|
|
411
|
+
* name inside `app/resources/html/`), `app/resources/index.html`, and finally the built-in default shell.
|
|
412
|
+
* Pass `template: false` to always use the built-in shell.
|
|
413
|
+
* @param {string|false} [template]
|
|
414
|
+
* @param {string} [root]
|
|
415
|
+
* @returns {string}
|
|
416
|
+
*/
|
|
417
|
+
export function loadSpaTemplate(template, root = Config.appDir()) {
|
|
418
|
+
if (template === false) return DEFAULT_APP_HTML;
|
|
419
|
+
const cacheKey = `${root}:${template || ""}`;
|
|
420
|
+
if (!DEBUG && templateCache.has(cacheKey)) return templateCache.get(cacheKey);
|
|
421
|
+
|
|
422
|
+
const isFile = (file) => {
|
|
423
|
+
try {
|
|
424
|
+
return fs.statSync(file).isFile();
|
|
425
|
+
} catch {
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
let found = null;
|
|
431
|
+
if (template) {
|
|
432
|
+
const direct = path.isAbsolute(template) ? template : path.join(root, template);
|
|
433
|
+
found = isFile(direct) ? direct : ViewLoader.resolve(template);
|
|
434
|
+
} else {
|
|
435
|
+
const fallback = path.join(root, PATHS.spaTemplate);
|
|
436
|
+
found = isFile(fallback) ? fallback : null;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const source = found ? fs.readFileSync(found, "utf-8") : DEFAULT_APP_HTML;
|
|
440
|
+
if (!DEBUG) templateCache.set(cacheKey, source);
|
|
441
|
+
return source;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Returns the Vite entries (extra stylesheets followed by the React entry) for merged SPA options.
|
|
446
|
+
* The React entry defaults to `app/resources/main.jsx`, which imports `./styles.css` itself.
|
|
447
|
+
* @param {Object} merged
|
|
448
|
+
* @param {string} [root]
|
|
449
|
+
* @returns {string[]}
|
|
450
|
+
*/
|
|
451
|
+
export function spaAssetEntries(merged = {}, root = Config.appDir()) {
|
|
452
|
+
const entry = merged.entry || defaultEntry(root) || PATHS.entries[0];
|
|
453
|
+
const styles = merged.styles === undefined || merged.styles === null ? [] : [].concat(merged.styles);
|
|
454
|
+
return [...styles, entry].filter(Boolean);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Resolves the merged metadata (`options` + `"*"` + matching route entry) for a sub-path under the SPA mount.
|
|
459
|
+
* @param {Object} options
|
|
460
|
+
* @param {{ wildcard: any, routes: Array<Object> }} compiled
|
|
461
|
+
* @param {string} subPath
|
|
462
|
+
* @param {import('express').Request|Object|null} [req]
|
|
463
|
+
* @returns {Promise<Object>}
|
|
464
|
+
*/
|
|
465
|
+
export async function resolveSpaMeta(options, compiled, subPath, req = null) {
|
|
466
|
+
const matched = matchMetaRoute(compiled.routes, subPath);
|
|
467
|
+
const params = matched?.params || {};
|
|
468
|
+
const wildcardObj =
|
|
469
|
+
typeof compiled.wildcard === "function"
|
|
470
|
+
? (await compiled.wildcard(req, params)) || {}
|
|
471
|
+
: compiled.wildcard || {};
|
|
472
|
+
const routeObj =
|
|
473
|
+
typeof matched?.value === "function"
|
|
474
|
+
? (await matched.value(req, params)) || {}
|
|
475
|
+
: matched?.value || {};
|
|
476
|
+
return mergeSpaMeta(options, wildcardObj, routeObj);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Renders the SPA HTML document (SEO head tags, skeleton, and scripts) from merged metadata.
|
|
481
|
+
* Shared by `router.spa()` and `npx bluebird-js export`.
|
|
482
|
+
* @param {Object} merged
|
|
483
|
+
* @param {Object} [ctx={}]
|
|
484
|
+
* @param {string} [ctx.fullPath="/"]
|
|
485
|
+
* @param {{ head: string, body: string }} [ctx.assets]
|
|
486
|
+
* @param {string} [ctx.csrfToken=""]
|
|
487
|
+
* @param {string} [ctx.lang]
|
|
488
|
+
* @param {string} [ctx.root]
|
|
489
|
+
* @param {import('express').Request} [ctx.req]
|
|
490
|
+
* @param {import('express').Response} [ctx.res]
|
|
491
|
+
* @param {ViteBridge} [ctx.vite]
|
|
492
|
+
* @returns {Promise<string>}
|
|
493
|
+
*/
|
|
494
|
+
export async function renderSpaDocument(merged, ctx = {}) {
|
|
495
|
+
const { fullPath = "/", assets = { head: "", body: "" }, csrfToken = "", req, res, vite } = ctx;
|
|
496
|
+
const lang = merged.lang || merged.htmlAttrs?.lang || ctx.lang || req?.lang || "en";
|
|
497
|
+
const headHtml = buildSpaHeadTags(merged, fullPath, assets);
|
|
498
|
+
const skeletonHtml = renderSkeletonHtml(merged.skeleton !== undefined ? merged.skeleton : "app");
|
|
499
|
+
const rootClass = merged.rootClass !== undefined ? String(merged.rootClass) : "w-full min-h-screen";
|
|
500
|
+
|
|
501
|
+
const bodyScripts = [];
|
|
502
|
+
for (const custom of merged.body || []) {
|
|
503
|
+
const rendered = renderCustomTag(custom, "body");
|
|
504
|
+
if (rendered) bodyScripts.push(rendered);
|
|
505
|
+
}
|
|
506
|
+
if (assets.body) bodyScripts.push(assets.body);
|
|
507
|
+
|
|
508
|
+
const templateData = {
|
|
509
|
+
lang,
|
|
510
|
+
title: merged.title || "",
|
|
511
|
+
description: merged.description || "",
|
|
512
|
+
keywords: Array.isArray(merged.keywords) ? merged.keywords.join(", ") : merged.keywords || "",
|
|
513
|
+
rootClass,
|
|
514
|
+
csrf: csrfToken,
|
|
515
|
+
csrfToken,
|
|
516
|
+
htmlAttrs: new SafeHtml(renderAttrs(merged.htmlAttrs || {}, new Set(["lang"]))),
|
|
517
|
+
bodyAttrs: new SafeHtml(renderAttrs(merged.bodyAttrs || {})),
|
|
518
|
+
head: new SafeHtml(headHtml),
|
|
519
|
+
skeleton: new SafeHtml(skeletonHtml),
|
|
520
|
+
scripts: new SafeHtml(bodyScripts.join("\n ")),
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
return Render.renderString(loadSpaTemplate(merged.template, ctx.root), templateData, { req, res, vite });
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Creates an Express middleware that serves the `app/resources/index.html` SPA shell with per-URL SEO metadata,
|
|
528
|
+
* full-width anti-CLS skeleton, Vite dev/production assets, optional response caching, and CSP nonces.
|
|
529
|
+
* @param {string} routerBasePath
|
|
530
|
+
* @param {string} mountPath
|
|
531
|
+
* @param {Object} [options={}]
|
|
532
|
+
* @returns {Function}
|
|
533
|
+
*/
|
|
534
|
+
export function createSpaMiddleware(routerBasePath, mountPath, options = {}) {
|
|
535
|
+
const subMount = mountPath === "/" ? "/" : `/${String(mountPath || "/").replace(/^\/+|\/+$/g, "")}`;
|
|
536
|
+
const fullMount = joinPaths(routerBasePath, subMount);
|
|
537
|
+
const compiled = compileMetaTree(options.meta || {}, fullMount);
|
|
538
|
+
const cacheTtl = Number(options.cache) || 0;
|
|
539
|
+
const cacheQuery = Array.isArray(options.cacheQuery) ? options.cacheQuery : [];
|
|
540
|
+
|
|
541
|
+
return async (req, res, next) => {
|
|
542
|
+
if (req.method !== "GET" && req.method !== "HEAD") return next();
|
|
543
|
+
|
|
544
|
+
const reqPath = req.path || "/";
|
|
545
|
+
const matchesMount =
|
|
546
|
+
subMount === "/" ? true : reqPath === subMount || reqPath.startsWith(`${subMount}/`);
|
|
547
|
+
if (!matchesMount) return next();
|
|
548
|
+
|
|
549
|
+
const fullPath = joinPaths(req.baseUrl || "/", reqPath);
|
|
550
|
+
if (
|
|
551
|
+
fullPath.startsWith("/api/") ||
|
|
552
|
+
fullPath === "/api" ||
|
|
553
|
+
fullPath.startsWith("/debug") ||
|
|
554
|
+
fullPath.startsWith("/_bb") ||
|
|
555
|
+
fullPath.startsWith("/@") ||
|
|
556
|
+
fullPath.startsWith("/build/") ||
|
|
557
|
+
fullPath.startsWith("/node_modules/")
|
|
558
|
+
) {
|
|
559
|
+
return next();
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (/\.[a-zA-Z0-9]{1,10}$/.test(reqPath)) return next();
|
|
563
|
+
|
|
564
|
+
const accept = req.headers?.accept || "";
|
|
565
|
+
if (accept && !accept.includes("text/html") && !accept.includes("*/*")) {
|
|
566
|
+
return next();
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
const canCache = cacheTtl > 0 && !DEBUG && !isAuthenticatedRequest(req);
|
|
570
|
+
const cacheKey = canCache ? buildCacheKey("spa", req, cacheQuery) : null;
|
|
571
|
+
if (cacheKey) {
|
|
572
|
+
const cached = await RenderCache.lookup(cacheKey);
|
|
573
|
+
if (cached) {
|
|
574
|
+
const token = typeof res.cookie === "function" ? Csrf.ensure(req, res) : "";
|
|
575
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
576
|
+
res.set("X-Bluebird-Cache", "HIT");
|
|
577
|
+
return res.send(applyCspNonce(detokenizeFromCache(cached, token), res));
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const csrfToken = typeof res.cookie === "function" ? Csrf.ensure(req, res) : "";
|
|
582
|
+
const subPath = subMount === "/" ? reqPath : reqPath.slice(subMount.length) || "/";
|
|
583
|
+
const merged = await resolveSpaMeta(options, compiled, subPath, req);
|
|
584
|
+
const bridge = ViteBridge.from(req) || ViteBridge.from(res) || new ViteBridge({ root: Config.appDir(), dev: DEBUG });
|
|
585
|
+
if (!DEBUG && !bridge.manifest) bridge.loadManifestSync();
|
|
586
|
+
const assets = bridge.assetTags(spaAssetEntries(merged));
|
|
587
|
+
|
|
588
|
+
let doc = await renderSpaDocument(merged, { fullPath, assets, csrfToken, req, res, vite: bridge });
|
|
589
|
+
doc = await bridge.transformHtml(req.originalUrl || fullPath, doc);
|
|
590
|
+
|
|
591
|
+
if (cacheKey) {
|
|
592
|
+
await RenderCache.store(cacheKey, tokenizeForCache(doc, csrfToken), cacheTtl);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
res.set("Content-Type", "text/html; charset=utf-8");
|
|
596
|
+
res.set("X-Bluebird-Cache", "MISS");
|
|
597
|
+
return res.send(applyCspNonce(doc, res));
|
|
598
|
+
};
|
|
599
|
+
}
|