@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
package/core/cli/view.js CHANGED
@@ -12,10 +12,12 @@ const args = rawArgs.filter(
12
12
  );
13
13
  const viewName = args.find((a) => !a.startsWith("-"));
14
14
  const isHtml = rawArgs.some((a) => a === "--html");
15
+ const isJs = rawArgs.some((a) => a === "--js");
16
+ const isJsx = !isHtml && !isJs;
15
17
 
16
18
  if (!viewName) {
17
19
  console.log(colors.red("[ERROR] Missing view name."));
18
- console.log("Usage: npx bluebird-js make:view <name> [--html]");
20
+ console.log("Usage: npx bluebird-js make:view <name> [--jsx | --js | --html]");
19
21
  process.exit(1);
20
22
  }
21
23
 
@@ -34,14 +36,24 @@ if (isHtml) {
34
36
  }
35
37
 
36
38
  const rawHtml = `<!DOCTYPE html>
37
- <html lang="en" data-theme="dark">
39
+ <html lang="en" class="dark" data-theme="dark">
38
40
  <head>
39
41
  <meta charset="UTF-8" />
42
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
40
43
  <title>${titleCase}</title>
41
- <link rel="stylesheet" href="/css/bluebird.min.css" />
44
+ <link rel="preconnect" href="https://fonts.googleapis.com">
45
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
46
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
47
+ <script src="/js/tailwind.js"></script>
48
+ <style type="text/tailwindcss">
49
+ @custom-variant dark (&:where(.dark, .dark *, [data-theme="dark"], [data-theme="dark"] *));
50
+ @theme {
51
+ --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
52
+ }
53
+ </style>
42
54
  </head>
43
- <body>
44
- <h1>${titleCase}</h1>
55
+ <body class="min-h-screen bg-slate-950 text-slate-100 font-sans p-8 max-w-4xl mx-auto">
56
+ <h1 class="text-3xl font-extrabold text-sky-400 mb-4">${titleCase}</h1>
45
57
  </body>
46
58
  </html>
47
59
  `;
@@ -52,6 +64,59 @@ if (isHtml) {
52
64
  process.exit(0);
53
65
  }
54
66
 
67
+ if (isJsx) {
68
+ const filePath = path.join(VIEWS_DIR, `${cleanName}.jsx`);
69
+ if (fs.existsSync(filePath)) {
70
+ console.log(colors.yellow(`[WARN] View '${cleanName}.jsx' already exists at app/views/${cleanName}.jsx`));
71
+ process.exit(0);
72
+ }
73
+
74
+ const jsxTemplate = `import { Client } from "@seip/bluebird-js/core/jsx.js";
75
+
76
+ export const layout = "main";
77
+
78
+ export const head = () => ({
79
+ title: "${titleCase} — Blue Bird",
80
+ description: "${titleCase} view rendered with Bluebird React SSR",
81
+ });
82
+
83
+ /**
84
+ * ${titleCase} React SSR view module.
85
+ * @param {Object} props
86
+ */
87
+ export default async function ${titleCase}View(props = {}) {
88
+ return (
89
+ <>
90
+ <section className="py-10 md:py-14 border-b border-slate-800/80">
91
+ <h1 className="text-3xl md:text-5xl font-extrabold tracking-tight bg-gradient-to-r from-sky-400 to-indigo-400 bg-clip-text text-transparent">
92
+ ${titleCase}
93
+ </h1>
94
+ <p className="mt-4 text-base md:text-lg text-slate-300">
95
+ Welcome to your new Bluebird React SSR view.
96
+ </p>
97
+ </section>
98
+
99
+ <div className="mt-8">
100
+ <article className="rounded-2xl border border-slate-800 bg-slate-900/60 p-6">
101
+ <p className="text-slate-300">
102
+ Rendered on the server with zero-build JSX and instant partial navigation.
103
+ </p>
104
+ </article>
105
+ </div>
106
+ </>
107
+ );
108
+ }
109
+ `;
110
+
111
+ fs.writeFileSync(filePath, jsxTemplate, "utf-8");
112
+ console.log(colors.green(`[OK] React JSX view created at app/views/${cleanName}.jsx`));
113
+ console.log("");
114
+ console.log(colors.cyan("To render this view in a route:"));
115
+ console.log(colors.gray(` import Render from "@seip/bluebird-js/core/render.js";`));
116
+ console.log(colors.gray(` routerWeb.get("/${cleanName}", Render.view("${cleanName}"));`));
117
+ process.exit(0);
118
+ }
119
+
55
120
  const filePath = path.join(VIEWS_DIR, `${cleanName}.js`);
56
121
  if (fs.existsSync(filePath)) {
57
122
  console.log(colors.yellow(`[WARN] View '${cleanName}.js' already exists at app/views/${cleanName}.js`));
@@ -74,14 +139,14 @@ export const head = () => ({
74
139
  */
75
140
  export default function ${titleCase}View(props = {}) {
76
141
  return html\`
77
- <section class="hero">
78
- <h1 class="text-gradient font-bold">${titleCase}</h1>
79
- <p class="lead">Welcome to your new Bluebird view.</p>
142
+ <section class="py-10 md:py-14 border-b border-slate-800/80">
143
+ <h1 class="text-3xl md:text-5xl font-extrabold tracking-tight bg-gradient-to-r from-sky-400 to-indigo-400 bg-clip-text text-transparent">${titleCase}</h1>
144
+ <p class="mt-4 text-base md:text-lg text-slate-300">Welcome to your new Bluebird view.</p>
80
145
  </section>
81
146
 
82
- <div class="mt-6">
83
- <article class="card">
84
- <p>Rendered with pure JavaScript ESM and zero build tools.</p>
147
+ <div class="mt-8">
148
+ <article class="rounded-2xl border border-slate-800 bg-slate-900/60 p-6">
149
+ <p class="text-slate-300">Rendered with pure JavaScript ESM and zero build tools.</p>
85
150
  </article>
86
151
  </div>
87
152
  \`;
package/core/config.js CHANGED
@@ -3,6 +3,7 @@ import os from "node:os";
3
3
 
4
4
  let _cachedProps = null;
5
5
  let _customUserDataDir = null;
6
+ let _customAppDir = null;
6
7
 
7
8
  /**
8
9
  * Configuration class to manage application-wide settings, environment variables,
@@ -10,13 +11,34 @@ let _customUserDataDir = null;
10
11
  */
11
12
  class Config {
12
13
  /**
13
- * Returns the base directory of the application (process.cwd()).
14
+ * Sets a custom base directory for application assets and views (e.g. Electron app.getAppPath()).
15
+ * @param {string} dirPath
16
+ */
17
+ static setAppDir(dirPath) {
18
+ _customAppDir = dirPath ? path.resolve(dirPath) : null;
19
+ _cachedProps = null;
20
+ }
21
+
22
+ /**
23
+ * Returns the base directory of the application package.
24
+ * In Electron or when appDir is customized, returns the configured application directory.
25
+ * Otherwise falls back to APP_DIR environment variable or process.cwd().
14
26
  * @returns {string}
15
27
  */
16
- static dirname() {
28
+ static appDir() {
29
+ if (_customAppDir) return _customAppDir;
30
+ if (process.env.APP_DIR) return path.resolve(process.env.APP_DIR);
17
31
  return process.cwd();
18
32
  }
19
33
 
34
+ /**
35
+ * Returns the base directory of the application package.
36
+ * @returns {string}
37
+ */
38
+ static dirname() {
39
+ return this.appDir();
40
+ }
41
+
20
42
  /**
21
43
  * Checks whether the application is running in Electron or Desktop mode.
22
44
  * @returns {boolean}
@@ -26,7 +48,8 @@ class Config {
26
48
  process.versions?.electron ||
27
49
  process.env.DESKTOP_MODE === "true" ||
28
50
  process.env.ELECTRON_RUN_AS_NODE ||
29
- _customUserDataDir
51
+ _customUserDataDir ||
52
+ _customAppDir
30
53
  );
31
54
  }
32
55
 
@@ -51,7 +74,13 @@ class Config {
51
74
  if (process.env.USER_DATA_PATH) return path.resolve(process.env.USER_DATA_PATH);
52
75
 
53
76
  const cwd = process.cwd();
54
- if (cwd.includes(".asar") || process.versions?.electron || process.env.DESKTOP_MODE === "true") {
77
+ const appDir = this.appDir();
78
+ if (
79
+ cwd.includes(".asar") ||
80
+ appDir.includes(".asar") ||
81
+ process.versions?.electron ||
82
+ process.env.DESKTOP_MODE === "true"
83
+ ) {
55
84
  const appName = (process.env.TITLE || "bluebird-app").toLowerCase().replace(/[^a-z0-9_-]/g, "-");
56
85
  const baseAppData =
57
86
  process.env.APPDATA ||
@@ -67,7 +96,7 @@ class Config {
67
96
  /**
68
97
  * Retrieves application properties from environment variables.
69
98
  * Results are cached after the first call.
70
- * @returns {{debug: boolean, desktop: boolean, userDataDir: string, title: string, description: string, version: string, host: string, appUrl: string, port: number, jwtSecret: string, langMeta: string, static: {path: string, options: Object}}} Configuration object.
99
+ * @returns {{debug: boolean, desktop: boolean, userDataDir: string, appDir: string, title: string, description: string, version: string, host: string, appUrl: string, port: number, jwtSecret: string, langMeta: string, static: {path: string, options: Object}}} Configuration object.
71
100
  */
72
101
  static props() {
73
102
  if (_cachedProps) return _cachedProps;
@@ -78,6 +107,7 @@ class Config {
78
107
  debug: process.env.DEBUG === "true",
79
108
  desktop: this.isDesktop(),
80
109
  userDataDir: this.userDataDir(),
110
+ appDir: this.appDir(),
81
111
  descriptionMeta: process.env.DESCRIPTION_META || "",
82
112
  keywordsMeta: process.env.KEYWORDS_META || "",
83
113
  titleMeta: process.env.TITLE_META || "",
package/core/csrf.js CHANGED
@@ -79,7 +79,7 @@ class Csrf {
79
79
  res.cookie(cookieName, token, {
80
80
  httpOnly: false,
81
81
  sameSite: "lax",
82
- secure: Boolean(!propsConfig.debug),
82
+ secure: Config.isDesktop() ? false : Boolean(!propsConfig.debug),
83
83
  path: "/",
84
84
  });
85
85
  }
package/core/database.js CHANGED
@@ -132,6 +132,8 @@ function toPostgresSql(sql) {
132
132
  return result;
133
133
  }
134
134
 
135
+ const _activeDatabases = new Set();
136
+
135
137
  /**
136
138
  * Database class wrapping native node:sqlite, mysql2, and pg with pooling and multi-level caching.
137
139
  */
@@ -224,6 +226,8 @@ class Database {
224
226
  console.error("[DATABASE ERROR] Failed to parse DATABASE_URL:", err.message);
225
227
  }
226
228
  }
229
+
230
+ _activeDatabases.add(this);
227
231
  }
228
232
 
229
233
  /**
@@ -270,12 +274,24 @@ class Database {
270
274
  fs.mkdirSync(dir, { recursive: true });
271
275
  }
272
276
 
273
- if (userDataBase !== process.cwd() && !fs.existsSync(dbFilePath)) {
274
- const bundledDbPath = path.resolve(process.cwd(), this.sqliteConfig.filename);
275
- if (fs.existsSync(bundledDbPath)) {
276
- try {
277
- fs.copyFileSync(bundledDbPath, dbFilePath);
278
- } catch {}
277
+ if (!fs.existsSync(dbFilePath)) {
278
+ const candidatePaths = [
279
+ path.resolve(Config.appDir(), this.sqliteConfig.filename),
280
+ process.resourcesPath ? path.join(process.resourcesPath, this.sqliteConfig.filename) : null,
281
+ process.resourcesPath ? path.join(process.resourcesPath, "database", path.basename(this.sqliteConfig.filename)) : null,
282
+ Config.appDir().includes(".asar")
283
+ ? path.resolve(Config.appDir().replace(/\.asar(?=[\/\\]|$)/, ".asar.unpacked"), this.sqliteConfig.filename)
284
+ : null,
285
+ path.resolve(process.cwd(), this.sqliteConfig.filename),
286
+ ].filter(Boolean);
287
+
288
+ for (const candidate of candidatePaths) {
289
+ if (fs.existsSync(candidate) && path.resolve(candidate) !== path.resolve(dbFilePath)) {
290
+ try {
291
+ fs.copyFileSync(candidate, dbFilePath);
292
+ break;
293
+ } catch {}
294
+ }
279
295
  }
280
296
  }
281
297
  }
@@ -407,13 +423,11 @@ class Database {
407
423
 
408
424
  if (isSelect || hasReturning) {
409
425
  const rows = res.rows || [];
426
+ const result = queryOptions.return_row ? (rows.length > 0 ? rows[0] : null) : rows;
410
427
  if (cacheKey && cacheTtl > 0) {
411
- await Cache.set(cacheKey, rows, cacheTtl);
412
- }
413
- if (queryOptions.return_row) {
414
- return rows.length > 0 ? rows[0] : null;
428
+ await Cache.set(cacheKey, result, cacheTtl);
415
429
  }
416
- return rows;
430
+ return result;
417
431
  }
418
432
 
419
433
  if (isInsert) {
@@ -429,13 +443,11 @@ class Database {
429
443
 
430
444
  if (isSelect || hasReturning) {
431
445
  const rows = Array.isArray(results) ? results : [];
446
+ const result = queryOptions.return_row ? (rows.length > 0 ? rows[0] : null) : rows;
432
447
  if (cacheKey && cacheTtl > 0) {
433
- await Cache.set(cacheKey, rows, cacheTtl);
448
+ await Cache.set(cacheKey, result, cacheTtl);
434
449
  }
435
- if (queryOptions.return_row) {
436
- return rows.length > 0 ? rows[0] : null;
437
- }
438
- return rows;
450
+ return result;
439
451
  }
440
452
 
441
453
  if (isInsert) {
@@ -615,11 +627,18 @@ class Database {
615
627
  }
616
628
 
617
629
  /**
618
- * Closes the database connection pool or SQLite instance.
630
+ * Closes the database connection pool or SQLite instance, checkpointing WAL if SQLite.
619
631
  */
620
632
  async close() {
621
633
  this._stmtCache.clear();
622
634
  if (this.db) {
635
+ try {
636
+ if (this._sqliteDriver === "node:sqlite") {
637
+ this.db.exec("PRAGMA wal_checkpoint(TRUNCATE);");
638
+ } else if (this._sqliteDriver === "better-sqlite3") {
639
+ this.db.pragma("wal_checkpoint(TRUNCATE)");
640
+ }
641
+ } catch {}
623
642
  this.db.close();
624
643
  this.db = null;
625
644
  }
@@ -629,6 +648,22 @@ class Database {
629
648
  }
630
649
  this.pool = null;
631
650
  }
651
+ _activeDatabases.delete(this);
652
+ }
653
+
654
+ /**
655
+ * Closes all active database instances across the application.
656
+ * @returns {Promise<void>}
657
+ */
658
+ static async closeAll() {
659
+ const list = Array.from(_activeDatabases);
660
+ for (const db of list) {
661
+ try {
662
+ await db.close();
663
+ } catch (err) {
664
+ console.error("[DATABASE] Error closing database connection:", err.message);
665
+ }
666
+ }
632
667
  }
633
668
  }
634
669
 
package/core/debug.js CHANGED
@@ -115,7 +115,7 @@ class Debug {
115
115
  <!DOCTYPE html>
116
116
  <html>
117
117
  <head>
118
- <script src="https://cdn.tailwindcss.com"></script>
118
+ <script src="/js/tailwind.js"></script>
119
119
  <title>Blue Bird Debug</title>
120
120
  </head>
121
121
  <body class="bg-gray-100 text-gray-800 p-10">
package/core/html.d.ts CHANGED
@@ -1,42 +1,54 @@
1
- export class SafeHtml {
2
- value: string;
3
- constructor(value: string);
4
- toString(): string;
5
- }
6
-
7
- export function isSafeHtml(val: any): val is SafeHtml;
8
-
9
- export function escapeHtml(value: any): string;
10
-
11
- export function safeJson(data: any): string;
12
-
13
- export interface HtmlTag {
14
- (strings: TemplateStringsArray, ...values: any[]): SafeHtml;
15
- raw(str: string): SafeHtml;
16
- json(obj: any): SafeHtml;
17
- script: typeof script;
18
- }
19
-
20
- export const html: HtmlTag;
21
-
22
- export interface ScriptTag {
23
- (strings: TemplateStringsArray, ...values: any[]): SafeHtml;
24
- (src: string): SafeHtml;
25
- (options: { src: string; defer?: boolean; module?: boolean }): SafeHtml;
26
- }
27
-
28
- export const script: ScriptTag;
29
-
30
- export function cached(
31
- key: string,
32
- options: number | { ttl?: number; vary?: string[] },
33
- renderFn: () => SafeHtml | string | Promise<SafeHtml | string>
34
- ): Promise<SafeHtml>;
35
-
36
- export function defer<T>(
37
- promise: Promise<T>,
38
- fallback: SafeHtml | string,
39
- renderer: (data: T) => SafeHtml | string | Promise<SafeHtml | string>
40
- ): { __bluebird_deferred__: true; promise: Promise<T>; fallback: string; renderer: Function };
41
-
42
- export default html;
1
+ export class SafeHtml {
2
+ value: string;
3
+ constructor(value: string);
4
+ toString(): string;
5
+ }
6
+
7
+ export function isSafeHtml(val: any): val is SafeHtml;
8
+
9
+ export function escapeHtml(value: any): string;
10
+
11
+ export function safeJson(data: any): string;
12
+
13
+ export function each<T>(
14
+ items: T[] | null | undefined,
15
+ renderFn: (item: T, index: number) => SafeHtml | string,
16
+ emptyFallback?: (() => SafeHtml | string) | SafeHtml | string
17
+ ): SafeHtml;
18
+
19
+ export interface HtmlTag {
20
+ (strings: TemplateStringsArray, ...values: any[]): SafeHtml;
21
+ raw(str: string): SafeHtml;
22
+ json(obj: any): SafeHtml;
23
+ script: typeof script;
24
+ each: typeof each;
25
+ map: typeof each;
26
+ code(text: string, className?: string): SafeHtml;
27
+ register(map: Record<string, Function>): void;
28
+ getComponent(name: string): Function | undefined;
29
+ }
30
+
31
+ export const html: HtmlTag;
32
+
33
+ export interface ScriptTag {
34
+ (strings: TemplateStringsArray, ...values: any[]): SafeHtml;
35
+ (src: string): SafeHtml;
36
+ (options: { src: string; defer?: boolean; module?: boolean }): SafeHtml;
37
+ (fn: Function, data?: any, options?: { onLoad?: boolean }): SafeHtml;
38
+ }
39
+
40
+ export const script: ScriptTag;
41
+
42
+ export function cached(
43
+ key: string,
44
+ options: number | { ttl?: number; vary?: string[] },
45
+ renderFn: () => SafeHtml | string | Promise<SafeHtml | string>
46
+ ): Promise<SafeHtml>;
47
+
48
+ export function defer<T>(
49
+ promise: Promise<T>,
50
+ fallback: SafeHtml | string,
51
+ renderer: (data: T) => SafeHtml | string | Promise<SafeHtml | string>
52
+ ): { __bluebird_deferred__: true; promise: Promise<T>; fallback: string; renderer: Function };
53
+
54
+ export default html;