@liorandb/core 1.0.0 → 1.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liorandb/core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "LioranDB is a lightweight, local-first, peer-to-peer, file-based database with a MongoDB-style Node.js API and a simple CLI for seamless distributed development.",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -1,18 +1,61 @@
1
- import fs from "fs";
2
- import path from "path";
3
1
  import crypto from "crypto";
4
- import { getBaseDBFolder } from "./rootpath.js";
2
+ import os from "os";
5
3
 
6
- const KEY_FILE = path.join(getBaseDBFolder(), ".secureKey");
4
+ const VAR_NAME = "LIORANDB_MASTER_KEY";
7
5
 
8
6
  export function getMasterKey() {
9
- if (!fs.existsSync(KEY_FILE)) {
10
- // generate 32-byte random key (256-bit)
11
- const key = crypto.randomBytes(32).toString("hex");
12
- fs.writeFileSync(KEY_FILE, key, { encoding: "utf8", mode: 0o600 });
13
- return key;
7
+ let key = process.env[VAR_NAME];
8
+
9
+ if (!key) {
10
+ console.error("\nāŒ ERROR: Master encryption key not found in system environment variables.");
11
+ console.error(`šŸ”‘ Variable required: ${VAR_NAME}\n`);
12
+
13
+ const generatedKey = crypto.randomBytes(32).toString("hex");
14
+ console.error("šŸ”§ Auto-generated a secure 256-bit key for you:");
15
+ console.error(generatedKey, "\n");
16
+
17
+ console.error("šŸ‘‰ Copy and paste the correct command below to set it permanently:");
18
+
19
+ printSetupCommand(generatedKey);
20
+
21
+ console.error("\nā— After setting the variable, restart your terminal.");
22
+ process.exit(1);
23
+ }
24
+
25
+ key = key.trim();
26
+
27
+ if (key.length !== 64) {
28
+ console.error("\nāŒ ERROR: Invalid master key length.");
29
+ console.error("Required: 64 hex characters (256-bit)\n");
30
+ process.exit(1);
14
31
  }
15
32
 
16
- const key = fs.readFileSync(KEY_FILE, "utf8").trim();
17
33
  return key;
18
34
  }
35
+
36
+ function printSetupCommand(key) {
37
+ const platform = os.platform();
38
+
39
+ console.error("========================================");
40
+
41
+ if (platform === "win32") {
42
+ console.error("šŸ“Œ Windows (System Environment Variable):");
43
+ console.error(
44
+ `setx ${VAR_NAME} "${key}" /M`
45
+ );
46
+ } else if (platform === "linux") {
47
+ console.error("šŸ“Œ Linux (System-wide, requires sudo):");
48
+ console.error(
49
+ `echo "export ${VAR_NAME}=${key}" | sudo tee -a /etc/environment`
50
+ );
51
+ } else if (platform === "darwin") {
52
+ console.error("šŸ“Œ macOS (system/global, requires sudo):");
53
+ console.error(
54
+ `echo "export ${VAR_NAME}=${key}" | sudo tee -a /etc/zshenv`
55
+ );
56
+ } else {
57
+ console.error(`⚠ Unsupported OS: ${platform}`);
58
+ }
59
+
60
+ console.error("========================================");
61
+ }
package/test.js ADDED
File without changes