@seip/bluebird-js 0.1.5 → 0.1.6

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/core/app.js CHANGED
@@ -9,7 +9,8 @@ import Logger from "./logger.js";
9
9
  import Debug from "./debug.js";
10
10
  import WebSocketManager from "./ws.js";
11
11
  import RateLimit from "./ratelimit.js";
12
- import Seo from "./seo.js";
12
+ import Seo from "./seo.js";
13
+ import AI, { ai } from "./ai.js";
13
14
 
14
15
  const props = Config.props();
15
16
  const SERVER_BOOT_ID = Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
@@ -762,6 +763,15 @@ class App {
762
763
  * @param {Object} [options={}] - Rate limit options (windowMs, max, message, driver, keyGenerator).
763
764
  * @returns {Function} Express middleware.
764
765
  */
766
+ /**
767
+ * Returns a new or default unified AI instance.
768
+ * @param {Object} [options={}] - AI configuration options.
769
+ * @returns {AI}
770
+ */
771
+ static ai(options = {}) {
772
+ return Object.keys(options).length > 0 ? new AI(options) : ai;
773
+ }
774
+
765
775
  static rateLimit(options = {}) {
766
776
  return RateLimit.middleware(options);
767
777
  }
@@ -787,5 +797,5 @@ export class AppError extends Error {
787
797
  }
788
798
  }
789
799
 
790
- export { App };
800
+ export { App, AI, ai };
791
801
  export default App;
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import readline from "node:readline/promises";
6
+ import colors from "../util/colors.js";
7
+ import { Database, DB_TYPE } from "../database.js";
8
+ import Config from "../config.js";
9
+
10
+ try {
11
+ process.loadEnvFile?.();
12
+ } catch {}
13
+
14
+ /**
15
+ * Prompts user for yes/no confirmation via CLI.
16
+ * @param {string} question
17
+ * @param {boolean} [defaultYes=false]
18
+ * @returns {Promise<boolean>}
19
+ */
20
+ async function promptConfirm(question, defaultYes = false) {
21
+ if (!process.stdin.isTTY) {
22
+ return defaultYes;
23
+ }
24
+ const rl = readline.createInterface({
25
+ input: process.stdin,
26
+ output: process.stdout,
27
+ });
28
+ try {
29
+ const answer = await rl.question(colors.cyan(question));
30
+ const trimmed = answer.trim().toLowerCase();
31
+ if (!trimmed) return defaultYes;
32
+ return ["y", "yes", "s", "si", "true", "1"].includes(trimmed);
33
+ } finally {
34
+ rl.close();
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Formats byte size into human-readable string.
40
+ * @param {number} bytes
41
+ * @returns {string}
42
+ */
43
+ function formatSize(bytes) {
44
+ if (bytes < 1024) return `${bytes} B`;
45
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
46
+ return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
47
+ }
48
+
49
+ /**
50
+ * Executes database backup CLI command.
51
+ */
52
+ export async function runBackup() {
53
+ const args = process.argv.slice(2);
54
+ let keep = 10;
55
+ let outDir = null;
56
+ let compress = true;
57
+
58
+ for (let i = 0; i < args.length; i++) {
59
+ if (args[i] === "--keep" && args[i + 1]) {
60
+ keep = parseInt(args[i + 1], 10) || 10;
61
+ i++;
62
+ } else if (args[i].startsWith("--keep=")) {
63
+ keep = parseInt(args[i].split("=")[1], 10) || 10;
64
+ } else if (args[i] === "--out" && args[i + 1]) {
65
+ outDir = args[i + 1];
66
+ i++;
67
+ } else if (args[i].startsWith("--out=")) {
68
+ outDir = args[i].split("=")[1];
69
+ } else if (args[i] === "--no-compress") {
70
+ compress = false;
71
+ }
72
+ }
73
+
74
+ console.log(colors.cyan(`[INFO] Creating atomic database backup (${DB_TYPE})...`));
75
+
76
+ try {
77
+ const result = await Database.backup({
78
+ out: outDir,
79
+ keep,
80
+ compress,
81
+ });
82
+
83
+ console.log(colors.green(`[OK] Database backup created successfully:`));
84
+ console.log(` ${colors.bold("File:")} ${result.file}`);
85
+ console.log(` ${colors.bold("Size:")} ${formatSize(result.size)}`);
86
+ console.log(` ${colors.bold("Type:")} ${result.dbType}`);
87
+ if (result.rotated > 0) {
88
+ console.log(` ${colors.bold("Rotated:")} ${result.rotated} older backup(s) purged (retention: ${keep})`);
89
+ }
90
+ } catch (err) {
91
+ console.error(colors.red(`[ERROR] Database backup failed:`), err.message);
92
+ process.exit(1);
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Executes database restore CLI command.
98
+ */
99
+ export async function runRestore() {
100
+ const args = process.argv.slice(2);
101
+ let targetFile = null;
102
+ let isAutoYes = false;
103
+
104
+ for (let i = 0; i < args.length; i++) {
105
+ const arg = args[i];
106
+ if (arg === "--yes" || arg === "-y") {
107
+ isAutoYes = true;
108
+ } else if (arg !== "db:restore" && arg !== "restore" && !arg.startsWith("--")) {
109
+ targetFile = arg;
110
+ }
111
+ }
112
+
113
+ if (!targetFile) {
114
+ const backupsDir = path.resolve(Config.userDataDir(), "database/backups");
115
+ console.log(colors.yellow("[WARN] No backup file specified."));
116
+ if (fs.existsSync(backupsDir)) {
117
+ const files = fs.readdirSync(backupsDir).filter((f) => !f.startsWith("."));
118
+ if (files.length > 0) {
119
+ console.log(colors.cyan("\nAvailable backups in database/backups:"));
120
+ for (const f of files.slice(0, 10)) {
121
+ console.log(` - ${f}`);
122
+ }
123
+ console.log(colors.gray("\nUsage: npx bluebird-js db:restore <filename> [--yes]"));
124
+ }
125
+ }
126
+ process.exit(1);
127
+ }
128
+
129
+ if (!isAutoYes) {
130
+ const confirmed = await promptConfirm(
131
+ `⚠️ WARNING: Restoring will overwrite the active database.\nA safety snapshot of the current database will be saved automatically.\nAre you sure you want to proceed? (y/N): `,
132
+ false
133
+ );
134
+ if (!confirmed) {
135
+ console.log(colors.yellow("[CANCELLED] Database restore cancelled by user."));
136
+ return;
137
+ }
138
+ }
139
+
140
+ console.log(colors.cyan(`[INFO] Restoring database from '${targetFile}'...`));
141
+
142
+ try {
143
+ const result = await Database.restore(targetFile, {
144
+ safetyBackup: true,
145
+ verify: true,
146
+ });
147
+
148
+ console.log(colors.green(`[OK] Database restored successfully:`));
149
+ console.log(` ${colors.bold("Source:")} ${result.restoredFrom}`);
150
+ console.log(` ${colors.bold("Target:")} ${result.targetFile}`);
151
+ console.log(` ${colors.bold("Size:")} ${formatSize(result.size)}`);
152
+ if (result.preRestoreBackup) {
153
+ console.log(` ${colors.bold("Snapshot:")} ${result.preRestoreBackup} (pre-restore safety backup)`);
154
+ }
155
+ } catch (err) {
156
+ console.error(colors.red(`[ERROR] Database restore failed:`), err.message);
157
+ process.exit(1);
158
+ }
159
+ }
160
+
161
+ const action = process.argv[2];
162
+ if (action === "db:restore" || action === "restore") {
163
+ runRestore();
164
+ } else {
165
+ runBackup();
166
+ }
167
+
package/core/cli/init.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import fs from "node:fs";
4
4
  import path from "node:path";
@@ -10,7 +10,7 @@ import colors from "../util/colors.js";
10
10
 
11
11
  try {
12
12
  process.loadEnvFile?.();
13
- } catch {}
13
+ } catch { }
14
14
 
15
15
  /**
16
16
  * Initializes a new Bluebird project with interactive configuration.
@@ -280,7 +280,7 @@ ${dbEnvBlock}
280
280
  if (fs.existsSync(pkgPath)) {
281
281
  try {
282
282
  pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
283
- } catch {}
283
+ } catch { }
284
284
  } else {
285
285
  const appName =
286
286
  path.basename(this.appDir).toLowerCase().replace(/[^a-z0-9_-]/g, "-") ||
@@ -290,7 +290,7 @@ ${dbEnvBlock}
290
290
  version: "1.0.0",
291
291
  type: "module",
292
292
  dependencies: {
293
- "@seip/bluebird-js": "^0.1.5",
293
+ "@seip/bluebird-js": "^0.1.6",
294
294
  },
295
295
  };
296
296
  }
@@ -326,7 +326,7 @@ ${dbEnvBlock}
326
326
  pkg.dependencies = {};
327
327
  }
328
328
  if (!pkg.dependencies["@seip/bluebird-js"]) {
329
- pkg.dependencies["@seip/bluebird-js"] = "^0.1.5";
329
+ pkg.dependencies["@seip/bluebird-js"] = "^0.1.6";
330
330
  }
331
331
 
332
332
  fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
@@ -639,7 +639,7 @@ export default function LiveCounter({ initial = 0, label = "Interactive Counter"
639
639
  pkg.scripts["dev:client"] = "vite";
640
640
  fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
641
641
  console.log(colors.green("[OK] Added 'build:client' and 'dev:client' scripts to package.json"));
642
- } catch {}
642
+ } catch { }
643
643
  }
644
644
 
645
645
  console.log(colors.bold.green("\nReact Islands setup complete!"));
@@ -876,7 +876,9 @@ function showHelp() {
876
876
  console.log(` ${colors.green("npx bluebird-js migrate:status")} Displays applied migration batches`);
877
877
  console.log(` ${colors.green("npx bluebird-js migrate:rollback")} Reverts the latest migration batch`);
878
878
  console.log(` ${colors.green("npx bluebird-js make:seed <name>")} Creates a new database seed file`);
879
- console.log(` ${colors.green("npx bluebird-js seed")} Executes all seed files`);
879
+ console.log(` ${colors.green("npx bluebird-js seed")} Executes all seed files`);
880
+ console.log(` ${colors.green("npx bluebird-js db:backup [--keep 10] [--out dir]")} Creates compressed WAL-safe database backup`);
881
+ console.log(` ${colors.green("npx bluebird-js db:restore <file> [--yes]")} Safely restores database with pre-backup & verification`);
880
882
  console.log(` ${colors.green("npx bluebird-js nginx:conf [domain] [port] [root]")} Generates Host Nginx reverse proxy`);
881
883
  console.log(` ${colors.green("npx bluebird-js make:ecosystem [--start]")} Generates PM2 cluster config`);
882
884
  console.log(` ${colors.green("npx bluebird-js add <module>")} Installs optional on-demand package
@@ -915,6 +917,13 @@ if (command === "make:ecosystem" || command === "ecosystem") {
915
917
  command === "make:seed"
916
918
  ) {
917
919
  import("./migrate.js");
920
+ } else if (
921
+ command === "db:backup" ||
922
+ command === "backup" ||
923
+ command === "db:restore" ||
924
+ command === "restore"
925
+ ) {
926
+ import("./backup.js");
918
927
  } else if (command === "swagger-install") {
919
928
  addCommand("swagger");
920
929
  } else if (command === "add") {