@seip/bluebird-js 0.1.1 → 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 (49) hide show
  1. package/.vscode/extensions.json +7 -0
  2. package/.vscode/settings.json +10 -0
  3. package/AGENTS.md +124 -64
  4. package/README.md +236 -103
  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 +129 -22
  17. package/core/auth.js +1 -1
  18. package/core/cli/doctor.js +3 -4
  19. package/core/cli/init.js +153 -6
  20. package/core/cli/migrate.js +418 -145
  21. package/core/cli/view.js +76 -11
  22. package/core/config.js +35 -5
  23. package/core/csrf.js +1 -1
  24. package/core/database.js +52 -17
  25. package/core/debug.js +1 -1
  26. package/core/html.d.ts +54 -42
  27. package/core/html.js +473 -217
  28. package/core/index.d.ts +187 -85
  29. package/core/jsx.d.ts +38 -0
  30. package/core/jsx.js +1180 -0
  31. package/core/model.js +282 -61
  32. package/core/queue.js +7 -4
  33. package/core/render-cache.js +212 -212
  34. package/core/render.d.ts +20 -2
  35. package/core/render.js +320 -43
  36. package/core/upload.js +7 -1
  37. package/core/validate.js +1 -1
  38. package/core/view-loader.js +341 -19
  39. package/core/vite.js +1 -1
  40. package/package.json +9 -3
  41. package/public/js/bluebird-client.js +2313 -1049
  42. package/public/js/tailwind.js +8 -0
  43. package/app/views/about.js +0 -63
  44. package/app/views/dashboard.js +0 -147
  45. package/app/views/index.js +0 -80
  46. package/app/views/layouts/main/view.js +0 -81
  47. package/app/views/login.js +0 -80
  48. package/public/css/bluebird.min.css +0 -2
  49. package/public/js/bluebird.min.js +0 -1073
@@ -2,10 +2,21 @@ 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;
8
- const VIEWS_DIR = path.resolve(process.cwd(), process.env.VIEWS_PATH || "app/views");
12
+
13
+ /**
14
+ * Returns the base views directory dynamically relative to Config.appDir().
15
+ * @returns {string}
16
+ */
17
+ function getViewsDir() {
18
+ return path.resolve(Config.appDir(), process.env.VIEWS_PATH || "app/views");
19
+ }
9
20
 
10
21
  /**
11
22
  * Module and raw file cache for production mode.
@@ -20,12 +31,115 @@ const moduleCache = new Map();
20
31
  * @returns {boolean}
21
32
  */
22
33
  function isSafePath(candidatePath) {
23
- const resolved = path.resolve(VIEWS_DIR, candidatePath);
24
- return resolved.startsWith(VIEWS_DIR);
34
+ const viewsDir = getViewsDir();
35
+ const resolved = path.resolve(viewsDir, candidatePath);
36
+ return resolved.startsWith(viewsDir);
25
37
  }
26
38
 
27
39
  /**
28
- * Loads a JavaScript view module with development cache-busting 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.
29
143
  * @param {string} absolutePath
30
144
  * @returns {Promise<Object>}
31
145
  */
@@ -34,11 +148,53 @@ async function importModule(absolutePath) {
34
148
  return moduleCache.get(absolutePath);
35
149
  }
36
150
 
37
- const stats = fs.statSync(absolutePath);
38
- const fileUrl = pathToFileURL(absolutePath).href;
39
- const targetUrl = DEBUG ? `${fileUrl}?t=${stats.mtimeMs}` : fileUrl;
151
+ let targetPath = absolutePath;
152
+ if (absolutePath.includes(".asar")) {
153
+ const unpackedPath = absolutePath.replace(/\.asar(?=[\/\\]|$)/, ".asar.unpacked");
154
+ if (fs.existsSync(unpackedPath)) {
155
+ targetPath = unpackedPath;
156
+ }
157
+ }
158
+
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));
162
+
163
+ let mod;
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
+ }
195
+ }
196
+ }
40
197
 
41
- const mod = await import(targetUrl);
42
198
  if (!DEBUG) {
43
199
  moduleCache.set(absolutePath, mod);
44
200
  }
@@ -47,8 +203,8 @@ async function importModule(absolutePath) {
47
203
 
48
204
  /**
49
205
  * View loader and resolver for Bluebird.js.
50
- * Resolves modular directory views (e.g. "dashboard/view.js"), single-file views,
51
- * 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.
52
208
  */
53
209
  export class ViewLoader {
54
210
  /**
@@ -56,34 +212,48 @@ export class ViewLoader {
56
212
  * @returns {string}
57
213
  */
58
214
  static getViewsDir() {
59
- return VIEWS_DIR;
215
+ return getViewsDir();
60
216
  }
61
217
 
62
218
  /**
63
219
  * Resolves and loads a view by name.
64
- * Searches the modular convention first (<name>/view.js), then flat files,
65
- * subdirectories, and finally static HTML files.
220
+ * Searches React JSX (.jsx) and JS (.js) conventions first, then subdirectories,
221
+ * and finally static HTML files.
66
222
  *
67
223
  * @param {string} viewName - Logical view identifier.
68
- * @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>}
69
225
  */
70
226
  static async load(viewName) {
71
227
  if (!viewName || typeof viewName !== "string") {
72
228
  return null;
73
229
  }
74
230
 
75
- const clean = viewName.replace(/^\/+/, "").replace(/\.(js|html|ejs)$/, "");
231
+ await autoRegisterComponents();
232
+
233
+ const clean = viewName.replace(/^\/+/, "").replace(/\.(jsx|js|html|ejs)$/, "");
76
234
 
77
235
  const candidates = [
236
+ `${clean}.jsx`,
78
237
  `${clean}.js`,
238
+ `${clean}/view.jsx`,
79
239
  `${clean}/view.js`,
240
+ path.join("pages", `${clean}.jsx`),
80
241
  path.join("pages", `${clean}.js`),
242
+ path.join("pages", `${clean}/view.jsx`),
81
243
  path.join("pages", `${clean}/view.js`),
244
+ path.join("components", `${clean}.jsx`),
82
245
  path.join("components", `${clean}.js`),
246
+ path.join("components", `${clean}/view.jsx`),
83
247
  path.join("components", `${clean}/view.js`),
248
+ path.join("partials", `${clean}.jsx`),
84
249
  path.join("partials", `${clean}.js`),
250
+ path.join("partials", `${clean}/view.jsx`),
85
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`),
86
255
  path.join("layouts", `${clean}/view.js`),
256
+ path.join("layouts", `${clean}.jsx`),
87
257
  path.join("layouts", `${clean}.js`),
88
258
  `${clean}.html`,
89
259
  path.join("pages", `${clean}.html`),
@@ -92,11 +262,17 @@ export class ViewLoader {
92
262
  path.join("layouts", `${clean}.html`),
93
263
  ];
94
264
 
265
+ const viewsDir = getViewsDir();
95
266
  for (const candidate of candidates) {
96
267
  if (!isSafePath(candidate)) continue;
97
268
 
98
- const fullPath = path.resolve(VIEWS_DIR, candidate);
269
+ const fullPath = path.resolve(viewsDir, candidate);
99
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
+
100
276
  if (candidate.endsWith(".js")) {
101
277
  const mod = await importModule(fullPath);
102
278
  return { type: "js", module: mod, filePath: fullPath };
@@ -117,25 +293,35 @@ export class ViewLoader {
117
293
  * Defaults to searching in app/views/layouts/.
118
294
  *
119
295
  * @param {string} layoutName - Layout name (e.g. "main").
120
- * @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>}
121
297
  */
122
298
  static async loadLayout(layoutName) {
123
299
  if (!layoutName) return null;
124
- const clean = layoutName.replace(/^\/+/, "").replace(/\.(js|html)$/, "");
300
+ const clean = layoutName.replace(/^\/+/, "").replace(/\.(jsx|js|html)$/, "");
125
301
 
126
302
  const candidates = [
303
+ path.join("layouts", `${clean}/view.jsx`),
127
304
  path.join("layouts", `${clean}/view.js`),
305
+ path.join("layouts", `${clean}.jsx`),
128
306
  path.join("layouts", `${clean}.js`),
307
+ `${clean}/view.jsx`,
129
308
  `${clean}/view.js`,
309
+ `${clean}.jsx`,
130
310
  `${clean}.js`,
131
311
  path.join("layouts", `${clean}.html`),
132
312
  ];
133
313
 
314
+ const viewsDir = getViewsDir();
134
315
  for (const candidate of candidates) {
135
316
  if (!isSafePath(candidate)) continue;
136
317
 
137
- const fullPath = path.resolve(VIEWS_DIR, candidate);
318
+ const fullPath = path.resolve(viewsDir, candidate);
138
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
+
139
325
  if (candidate.endsWith(".js")) {
140
326
  const mod = await importModule(fullPath);
141
327
  return { type: "js", module: mod, filePath: fullPath };
@@ -157,6 +343,142 @@ export class ViewLoader {
157
343
  static clearCache() {
158
344
  moduleCache.clear();
159
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
+ }
160
482
  }
161
483
 
162
484
  export default ViewLoader;
package/core/vite.js CHANGED
@@ -36,7 +36,7 @@ class Vite {
36
36
  * @private
37
37
  */
38
38
  static getManifestPath() {
39
- const root = process.cwd();
39
+ const root = Config.appDir();
40
40
  const viteManifest = path.join(root, "public/dist/.vite/manifest.json");
41
41
  if (fs.existsSync(viteManifest)) return viteManifest;
42
42
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seip/bluebird-js",
3
- "version": "0.1.1",
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"