@seip/bluebird-js 0.1.2 → 0.1.4

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 +92 -60
  4. package/README.md +224 -152
  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 +67 -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 +172 -84
  26. package/core/jsx.d.ts +38 -0
  27. package/core/jsx.js +1180 -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 +320 -43
  32. package/core/validate.js +1 -1
  33. package/core/view-loader.js +313 -22
  34. package/package.json +9 -3
  35. package/public/js/bluebird-client.js +2313 -1049
  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
@@ -2,6 +2,10 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { pathToFileURL } from "node:url";
4
4
  import Config from "./config.js";
5
+ import html from "./html.js";
6
+ import { compileJsx, registerJsxLoader } from "./jsx.js";
7
+
8
+ registerJsxLoader();
5
9
 
6
10
  const props = Config.props();
7
11
  const DEBUG = props.debug;
@@ -33,8 +37,109 @@ function isSafePath(candidatePath) {
33
37
  }
34
38
 
35
39
  /**
36
- * Loads a JavaScript view module with development cache-busting, .asar unpacked fallback,
37
- * data URL ESM import inside .asar archives, or production memoization.
40
+ * Parses JSX-like attribute strings inside legacy `html\`...\`` template literals.
41
+ * @param {string} attrs
42
+ * @returns {string}
43
+ */
44
+ function transformJsxAttributes(attrs) {
45
+ const trimmed = attrs.trim();
46
+ if (!trimmed) return "{}";
47
+
48
+ if (trimmed.startsWith("props=")) {
49
+ let propVal = trimmed.slice(6).trim();
50
+ if (propVal.startsWith("{{") && propVal.endsWith("}}")) {
51
+ return propVal.slice(1, -1);
52
+ }
53
+ if (propVal.startsWith("{") && propVal.endsWith("}")) {
54
+ const inner = propVal.slice(1, -1).trim();
55
+ if (inner.startsWith("{") && inner.endsWith("}")) {
56
+ return inner;
57
+ }
58
+ return `{ ${inner} }`;
59
+ }
60
+ return propVal;
61
+ }
62
+
63
+ const entries = [];
64
+ const regex = /([a-zA-Z0-9_\-]+)(?:\s*=\s*(?:\{([\s\S]*?)\}|"([^"]*)"|'([^']*)'))?/g;
65
+ let m;
66
+ let hasParsed = false;
67
+ while ((m = regex.exec(trimmed)) !== null) {
68
+ hasParsed = true;
69
+ const key = m[1];
70
+ if (key.startsWith("...")) {
71
+ entries.push(key);
72
+ continue;
73
+ }
74
+ if (m[2] !== undefined) {
75
+ entries.push(`${key}: (${m[2]})`);
76
+ } else if (m[3] !== undefined) {
77
+ entries.push(`${key}: "${m[3]}"`);
78
+ } else if (m[4] !== undefined) {
79
+ entries.push(`${key}: '${m[4]}'`);
80
+ } else {
81
+ entries.push(`${key}: true`);
82
+ }
83
+ }
84
+
85
+ if (!hasParsed) return "{}";
86
+ return `{ ${entries.join(", ")} }`;
87
+ }
88
+
89
+ /**
90
+ * Transforms legacy JSX component tags inside `html\`...\`` view source code into JavaScript calls.
91
+ * @param {string} code
92
+ * @returns {string}
93
+ */
94
+ function transformViewJsx(code) {
95
+ let transformed = code;
96
+ transformed = transformed.replace(
97
+ /\$\{\s*<([A-Z][a-zA-Z0-9_]*)([\s\S]*?)\/>\s*\}/g,
98
+ (match, compName, attrs) => {
99
+ return `\${${compName}(${transformJsxAttributes(attrs)})}`;
100
+ }
101
+ );
102
+ transformed = transformed.replace(
103
+ /<([A-Z][a-zA-Z0-9_]*)\s+props=\{\s*\{?([^}]*)\}?\s*\}\s*\/>/g,
104
+ (match, compName, inner) => {
105
+ return `\${${compName}({ ${inner.trim()} })}`;
106
+ }
107
+ );
108
+ return transformed;
109
+ }
110
+
111
+ /**
112
+ * Automatically discovers and registers components from views directories.
113
+ */
114
+ async function autoRegisterComponents() {
115
+ const viewsDir = getViewsDir();
116
+ const dirs = [path.join(viewsDir, "components"), path.join(viewsDir, "partials")];
117
+
118
+ for (const dir of dirs) {
119
+ if (!fs.existsSync(dir)) continue;
120
+ try {
121
+ const files = fs.readdirSync(dir);
122
+ for (const file of files) {
123
+ if ((!file.endsWith(".js") && !file.endsWith(".jsx")) || file.startsWith(".")) continue;
124
+ const ext = path.extname(file);
125
+ const compName = path.basename(file, ext);
126
+ if (html.getComponent(compName)) continue;
127
+ const fullPath = path.join(dir, file);
128
+ try {
129
+ const mod = await importModule(fullPath);
130
+ const compFn = mod.default || mod[compName];
131
+ if (typeof compFn === "function") {
132
+ html.register({ [compName]: compFn });
133
+ }
134
+ } catch {}
135
+ }
136
+ } catch {}
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Loads a JavaScript or React JSX view module with development cache-busting, .asar unpacked fallback,
142
+ * zero-disk in-memory JSX compilation, or production memoization.
38
143
  * @param {string} absolutePath
39
144
  * @returns {Promise<Object>}
40
145
  */
@@ -51,20 +156,42 @@ async function importModule(absolutePath) {
51
156
  }
52
157
  }
53
158
 
54
- const stats = fs.statSync(targetPath);
55
- const fileUrl = pathToFileURL(targetPath).href;
56
- const targetUrl = DEBUG ? `${fileUrl}?t=${stats.mtimeMs}` : fileUrl;
159
+ const isJsxFile = targetPath.endsWith(".jsx");
160
+ const rawCode = fs.readFileSync(targetPath, "utf-8");
161
+ const hasTemplateJsx = !isJsxFile && (/\$\{\s*<[A-Z]|<[A-Z][a-zA-Z0-9_]*\s+props=/.test(rawCode));
57
162
 
58
163
  let mod;
59
- try {
60
- mod = await import(targetUrl);
61
- } catch (err) {
62
- if (absolutePath.includes(".asar")) {
63
- const code = fs.readFileSync(absolutePath, "utf-8");
64
- const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString("base64")}`;
65
- mod = await import(dataUrl);
66
- } else {
67
- throw err;
164
+ if (hasTemplateJsx) {
165
+ const transformedCode = transformViewJsx(rawCode);
166
+ const uniqueSuffix = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
167
+ const tempFile = path.join(
168
+ path.dirname(targetPath),
169
+ `.bb-${uniqueSuffix}-${path.basename(targetPath)}`
170
+ );
171
+ fs.writeFileSync(tempFile, transformedCode, "utf-8");
172
+ try {
173
+ const fileUrl = pathToFileURL(tempFile).href;
174
+ mod = await import(fileUrl);
175
+ } finally {
176
+ try {
177
+ if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile);
178
+ } catch {}
179
+ }
180
+ } else {
181
+ const stats = fs.statSync(targetPath);
182
+ const fileUrl = pathToFileURL(targetPath).href;
183
+ const targetUrl = DEBUG ? `${fileUrl}?t=${stats.mtimeMs}` : fileUrl;
184
+
185
+ try {
186
+ mod = await import(targetUrl);
187
+ } catch (err) {
188
+ if (isJsxFile || absolutePath.includes(".asar")) {
189
+ const sourceToLoad = isJsxFile ? compileJsx(rawCode) : rawCode;
190
+ const dataUrl = `data:text/javascript;base64,${Buffer.from(sourceToLoad).toString("base64")}`;
191
+ mod = await import(dataUrl);
192
+ } else {
193
+ throw err;
194
+ }
68
195
  }
69
196
  }
70
197
 
@@ -76,8 +203,8 @@ async function importModule(absolutePath) {
76
203
 
77
204
  /**
78
205
  * View loader and resolver for Bluebird.js.
79
- * Resolves modular directory views (e.g. "dashboard/view.js"), single-file views,
80
- * components, and layouts.
206
+ * Resolves React JSX views (.jsx), modular directory views (e.g. "dashboard/view.jsx" or "dashboard/view.js"),
207
+ * single-file views, components, and layouts.
81
208
  */
82
209
  export class ViewLoader {
83
210
  /**
@@ -90,29 +217,43 @@ export class ViewLoader {
90
217
 
91
218
  /**
92
219
  * Resolves and loads a view by name.
93
- * Searches the modular convention first (<name>/view.js), then flat files,
94
- * subdirectories, and finally static HTML files.
220
+ * Searches React JSX (.jsx) and JS (.js) conventions first, then subdirectories,
221
+ * and finally static HTML files.
95
222
  *
96
223
  * @param {string} viewName - Logical view identifier.
97
- * @returns {Promise<{type: "js"|"html", module?: Object, filePath: string, content?: string}|null>}
224
+ * @returns {Promise<{type: "jsx"|"js"|"html", module?: Object, filePath: string, content?: string}|null>}
98
225
  */
99
226
  static async load(viewName) {
100
227
  if (!viewName || typeof viewName !== "string") {
101
228
  return null;
102
229
  }
103
230
 
104
- const clean = viewName.replace(/^\/+/, "").replace(/\.(js|html|ejs)$/, "");
231
+ await autoRegisterComponents();
232
+
233
+ const clean = viewName.replace(/^\/+/, "").replace(/\.(jsx|js|html|ejs)$/, "");
105
234
 
106
235
  const candidates = [
236
+ `${clean}.jsx`,
107
237
  `${clean}.js`,
238
+ `${clean}/view.jsx`,
108
239
  `${clean}/view.js`,
240
+ path.join("pages", `${clean}.jsx`),
109
241
  path.join("pages", `${clean}.js`),
242
+ path.join("pages", `${clean}/view.jsx`),
110
243
  path.join("pages", `${clean}/view.js`),
244
+ path.join("components", `${clean}.jsx`),
111
245
  path.join("components", `${clean}.js`),
246
+ path.join("components", `${clean}/view.jsx`),
112
247
  path.join("components", `${clean}/view.js`),
248
+ path.join("partials", `${clean}.jsx`),
113
249
  path.join("partials", `${clean}.js`),
250
+ path.join("partials", `${clean}/view.jsx`),
114
251
  path.join("partials", `${clean}/view.js`),
252
+ path.join("islands", `${clean}.jsx`),
253
+ path.join("islands", `${clean}.js`),
254
+ path.join("layouts", `${clean}/view.jsx`),
115
255
  path.join("layouts", `${clean}/view.js`),
256
+ path.join("layouts", `${clean}.jsx`),
116
257
  path.join("layouts", `${clean}.js`),
117
258
  `${clean}.html`,
118
259
  path.join("pages", `${clean}.html`),
@@ -127,6 +268,11 @@ export class ViewLoader {
127
268
 
128
269
  const fullPath = path.resolve(viewsDir, candidate);
129
270
  if (fs.existsSync(fullPath)) {
271
+ if (candidate.endsWith(".jsx")) {
272
+ const mod = await importModule(fullPath);
273
+ return { type: "jsx", module: mod, filePath: fullPath };
274
+ }
275
+
130
276
  if (candidate.endsWith(".js")) {
131
277
  const mod = await importModule(fullPath);
132
278
  return { type: "js", module: mod, filePath: fullPath };
@@ -147,16 +293,20 @@ export class ViewLoader {
147
293
  * Defaults to searching in app/views/layouts/.
148
294
  *
149
295
  * @param {string} layoutName - Layout name (e.g. "main").
150
- * @returns {Promise<{type: "js"|"html", module?: Object, filePath: string, content?: string}|null>}
296
+ * @returns {Promise<{type: "jsx"|"js"|"html", module?: Object, filePath: string, content?: string}|null>}
151
297
  */
152
298
  static async loadLayout(layoutName) {
153
299
  if (!layoutName) return null;
154
- const clean = layoutName.replace(/^\/+/, "").replace(/\.(js|html)$/, "");
300
+ const clean = layoutName.replace(/^\/+/, "").replace(/\.(jsx|js|html)$/, "");
155
301
 
156
302
  const candidates = [
303
+ path.join("layouts", `${clean}/view.jsx`),
157
304
  path.join("layouts", `${clean}/view.js`),
305
+ path.join("layouts", `${clean}.jsx`),
158
306
  path.join("layouts", `${clean}.js`),
307
+ `${clean}/view.jsx`,
159
308
  `${clean}/view.js`,
309
+ `${clean}.jsx`,
160
310
  `${clean}.js`,
161
311
  path.join("layouts", `${clean}.html`),
162
312
  ];
@@ -167,6 +317,11 @@ export class ViewLoader {
167
317
 
168
318
  const fullPath = path.resolve(viewsDir, candidate);
169
319
  if (fs.existsSync(fullPath)) {
320
+ if (candidate.endsWith(".jsx")) {
321
+ const mod = await importModule(fullPath);
322
+ return { type: "jsx", module: mod, filePath: fullPath };
323
+ }
324
+
170
325
  if (candidate.endsWith(".js")) {
171
326
  const mod = await importModule(fullPath);
172
327
  return { type: "js", module: mod, filePath: fullPath };
@@ -188,6 +343,142 @@ export class ViewLoader {
188
343
  static clearCache() {
189
344
  moduleCache.clear();
190
345
  }
346
+
347
+ /**
348
+ * Scans `app/views/` (excluding `layouts`, `components`, `partials`, `islands`, and `api`)
349
+ * to discover page view modules and their declarative route configurations.
350
+ *
351
+ * Supported exports in any view module:
352
+ * - `export const route = "/custom-path" | false` (defaults to file-based route path)
353
+ * - `export const cache = 300` (applies `Render.cache(300)` automatically)
354
+ * - `export const middlewares = [...]` (applies custom route middlewares)
355
+ * - `export const seo = true | false` (defaults to `true` for public routes without middlewares)
356
+ * - `export const layout = "main"`
357
+ *
358
+ * @returns {Promise<Array<{viewName: string, routePath: string, cache: number, middlewares: Function[], seo: boolean, module: Object}>>}
359
+ */
360
+ static async discoverRoutes() {
361
+ const viewsDir = getViewsDir();
362
+ if (!fs.existsSync(viewsDir)) return [];
363
+
364
+ const ignoredDirs = new Set([
365
+ "layouts",
366
+ "components",
367
+ "partials",
368
+ "islands",
369
+ "api",
370
+ "node_modules",
371
+ ]);
372
+
373
+ const discovered = [];
374
+ const seenViews = new Set();
375
+
376
+ /**
377
+ * Recursively walks a directory relative to viewsDir.
378
+ * @param {string} currentDir
379
+ * @param {string} relPrefix
380
+ */
381
+ const walk = async (currentDir, relPrefix = "") => {
382
+ let entries = [];
383
+ try {
384
+ entries = fs.readdirSync(currentDir, { withFileTypes: true });
385
+ } catch {
386
+ return;
387
+ }
388
+
389
+ entries.sort((a, b) => {
390
+ if (a.isFile() && b.isFile()) {
391
+ if (a.name.endsWith(".jsx") && !b.name.endsWith(".jsx")) return -1;
392
+ if (!a.name.endsWith(".jsx") && b.name.endsWith(".jsx")) return 1;
393
+ }
394
+ return a.name.localeCompare(b.name);
395
+ });
396
+
397
+ for (const entry of entries) {
398
+ if (entry.name.startsWith(".") || entry.name.startsWith("_")) continue;
399
+
400
+ const fullPath = path.join(currentDir, entry.name);
401
+ const relPath = relPrefix ? `${relPrefix}/${entry.name}` : entry.name;
402
+
403
+ if (entry.isDirectory()) {
404
+ if (ignoredDirs.has(entry.name.toLowerCase())) continue;
405
+ await walk(fullPath, relPath);
406
+ continue;
407
+ }
408
+
409
+ if (!entry.isFile()) continue;
410
+ if (!entry.name.endsWith(".jsx") && !entry.name.endsWith(".js")) continue;
411
+
412
+ const ext = path.extname(entry.name);
413
+ const rawViewName = relPath.slice(0, -ext.length).replace(/\\/g, "/");
414
+
415
+ if (seenViews.has(rawViewName)) continue;
416
+ seenViews.add(rawViewName);
417
+
418
+ let mod;
419
+ try {
420
+ mod = await importModule(fullPath);
421
+ } catch {
422
+ continue;
423
+ }
424
+
425
+ if (!mod || typeof mod.default !== "function") continue;
426
+ if (mod.route === false) continue;
427
+
428
+ let routePath;
429
+ if (typeof mod.route === "string" && mod.route.trim()) {
430
+ routePath = mod.route.trim();
431
+ if (!routePath.startsWith("/")) routePath = `/${routePath}`;
432
+ } else {
433
+ let normalized = rawViewName;
434
+ if (normalized.startsWith("pages/")) {
435
+ normalized = normalized.slice("pages/".length);
436
+ }
437
+ if (normalized === "index" || normalized === "view") {
438
+ routePath = "/";
439
+ } else if (normalized.endsWith("/index")) {
440
+ routePath = "/" + normalized.slice(0, -"/index".length);
441
+ } else if (normalized.endsWith("/view")) {
442
+ routePath = "/" + normalized.slice(0, -"/view".length);
443
+ } else {
444
+ routePath = "/" + normalized;
445
+ }
446
+
447
+ routePath = routePath.replace(/\[([^\]]+)\]/g, ":$1");
448
+ }
449
+
450
+ if (routePath === "/api" || routePath.startsWith("/api/")) {
451
+ continue;
452
+ }
453
+
454
+ const middlewares = Array.isArray(mod.middlewares)
455
+ ? mod.middlewares.filter((fn) => typeof fn === "function")
456
+ : typeof mod.middleware === "function"
457
+ ? [mod.middleware]
458
+ : [];
459
+
460
+ const cache = typeof mod.cache === "number" && mod.cache > 0 ? mod.cache : 0;
461
+ const seo = mod.seo !== undefined ? Boolean(mod.seo) : middlewares.length === 0;
462
+ const actions =
463
+ mod.actions && (typeof mod.actions === "object" || typeof mod.actions === "function")
464
+ ? mod.actions
465
+ : null;
466
+
467
+ discovered.push({
468
+ viewName: rawViewName,
469
+ routePath,
470
+ cache,
471
+ middlewares,
472
+ seo,
473
+ actions,
474
+ module: mod,
475
+ });
476
+ }
477
+ };
478
+
479
+ await walk(viewsDir, "");
480
+ return discovered;
481
+ }
191
482
  }
192
483
 
193
484
  export default ViewLoader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seip/bluebird-js",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Performance-first Node.js API and web framework with built-in JWT auth, HTML rendering, validation, BaseModel, and multi-level caching",
5
5
  "type": "module",
6
6
  "main": "core/app.js",
@@ -27,12 +27,17 @@
27
27
  "keywords": [
28
28
  "express",
29
29
  "framework",
30
+ "react",
31
+ "jsx",
32
+ "ssr",
30
33
  "html",
31
34
  "api",
32
35
  "jwt",
33
36
  "render",
34
37
  "seo",
35
- "model"
38
+ "model",
39
+ "sqlite",
40
+ "tailwind"
36
41
  ],
37
42
  "author": "Seip25",
38
43
  "license": "MIT",
@@ -68,7 +73,8 @@
68
73
  "AGENTS.md",
69
74
  "production.md",
70
75
  ".env_example",
71
- "jsconfig.json"
76
+ "jsconfig.json",
77
+ ".vscode"
72
78
  ],
73
79
  "dependencies": {
74
80
  "express": "^5.2.1"