@nexushub/client 0.4.2 → 0.4.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.
@@ -1,86 +1,216 @@
1
- "use client";
2
-
3
1
  // src/content/local-cache-server.ts
4
2
  import fs from "fs";
5
3
  import path from "path";
4
+
5
+ // src/config.ts
6
+ var DEFAULT_API_URL = "https://endpoints.gnexus.co.tz";
7
+ var DEFAULT_ANALYTICS_URL = "https://sentry.gnexus.co.tz";
8
+ var LOCAL_NEST_URL = "https://endpoints.gnexus.co.tz";
9
+ var LOCAL_RUST_URL = "https://sentry.gnexus.co.tz";
10
+ var getEnvConfig = () => {
11
+ const NEXT_PUBLIC_ID = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_NEXUS_ID : void 0;
12
+ const NEXT_PUBLIC_KEY = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_NEXUS_KEY : void 0;
13
+ const NEXT_PUBLIC_URL = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_NEXUS_API_URL : void 0;
14
+ const NODE_ID = typeof process !== "undefined" ? process.env.NEXUS_PROJECT_ID : void 0;
15
+ const NODE_KEY = typeof process !== "undefined" ? process.env.NEXUS_API_KEY : void 0;
16
+ const NODE_URL = typeof process !== "undefined" ? process.env.NEXUS_API_URL : void 0;
17
+ const NODE_ENV = typeof process !== "undefined" ? process.env.NODE_ENV : "production";
18
+ const isDev = NODE_ENV === "development" || typeof window !== "undefined" && window.location.hostname === "localhost";
19
+ const projectId = NEXT_PUBLIC_ID ?? NODE_ID;
20
+ const apiKey = NEXT_PUBLIC_KEY ?? NODE_KEY;
21
+ const apiUrl = NEXT_PUBLIC_URL ?? NODE_URL ?? (isDev ? LOCAL_NEST_URL : DEFAULT_API_URL);
22
+ const analyticsUrl = apiUrl.includes("localhost") ? LOCAL_RUST_URL : DEFAULT_ANALYTICS_URL;
23
+ return {
24
+ projectId,
25
+ apiKey,
26
+ apiUrl,
27
+ analyticsUrl
28
+ };
29
+ };
30
+
31
+ // src/content/binary-compiler.ts
32
+ import * as crypto from "crypto";
33
+ var BinaryCompiler = class {
34
+ /**
35
+ * Generates a secure, deterministic cryptographic key from the project's API key.
36
+ */
37
+ static deriveKeys(apiKey) {
38
+ const hash = crypto.createHash("sha256").update(apiKey).digest();
39
+ const encryptionKey = hash;
40
+ const hmacKey = crypto.createHmac("sha256", apiKey).update("nexus-integrity-key").digest();
41
+ return { encryptionKey, hmacKey };
42
+ }
43
+ /**
44
+ * Compiles JSON content data into a secure, signed, and encrypted .nx binary buffer.
45
+ */
46
+ static compile(data, apiKey, projectId, metadataOverrides = {}) {
47
+ const { encryptionKey, hmacKey } = this.deriveKeys(apiKey);
48
+ const metadata = {
49
+ version: "2.0.0",
50
+ compiledAt: (/* @__PURE__ */ new Date()).toISOString(),
51
+ projectId,
52
+ ...metadataOverrides
53
+ };
54
+ const metadataStr = JSON.stringify(metadata);
55
+ const metadataBuffer = Buffer.from(metadataStr, "utf-8");
56
+ const iv = crypto.randomBytes(16);
57
+ const cipher = crypto.createCipheriv(this.ALGORITHM, encryptionKey, iv);
58
+ const plainTextPayload = JSON.stringify(data);
59
+ let encryptedPayload = cipher.update(plainTextPayload, "utf8");
60
+ encryptedPayload = Buffer.concat([encryptedPayload, cipher.final()]);
61
+ const payloadBuffer = Buffer.concat([iv, encryptedPayload]);
62
+ const headerBuffer = Buffer.from(this.MAGIC_HEADER, "ascii");
63
+ const metaLengthBuffer = Buffer.alloc(4);
64
+ metaLengthBuffer.writeUInt32BE(metadataBuffer.length, 0);
65
+ const payloadLengthBuffer = Buffer.alloc(4);
66
+ payloadLengthBuffer.writeUInt32BE(payloadBuffer.length, 0);
67
+ const prefixBlock = Buffer.concat([
68
+ headerBuffer,
69
+ metaLengthBuffer,
70
+ metadataBuffer,
71
+ payloadLengthBuffer,
72
+ payloadBuffer
73
+ ]);
74
+ const hmac = crypto.createHmac("sha256", hmacKey);
75
+ hmac.update(prefixBlock);
76
+ const signature = hmac.digest();
77
+ return Buffer.concat([prefixBlock, signature]);
78
+ }
79
+ /**
80
+ * Verifies, decrypts, and decompiles a .nx binary buffer back into clean, type-safe JSON.
81
+ * Throws structured errors on integrity verification failures or file corruption.
82
+ */
83
+ static decompile(buffer, apiKey) {
84
+ if (buffer.length < 44) {
85
+ throw new Error(
86
+ "Corrupted File: Binary payload is too short to be a valid Nexus node."
87
+ );
88
+ }
89
+ const { encryptionKey, hmacKey } = this.deriveKeys(apiKey);
90
+ const prefixBlockLength = buffer.length - 32;
91
+ const prefixBlock = buffer.subarray(0, prefixBlockLength);
92
+ const expectedSignature = buffer.subarray(prefixBlockLength);
93
+ const hmac = crypto.createHmac("sha256", hmacKey);
94
+ hmac.update(prefixBlock);
95
+ const actualSignature = hmac.digest();
96
+ if (!crypto.timingSafeEqual(expectedSignature, actualSignature)) {
97
+ throw new Error(
98
+ "DATA_INTEGRITY_VIOLATION: Cryptographic signature mismatch. This local node has been modified externally or tampered with."
99
+ );
100
+ }
101
+ const magicHeader = prefixBlock.subarray(0, 4).toString("ascii");
102
+ if (magicHeader !== this.MAGIC_HEADER) {
103
+ throw new Error(
104
+ "Invalid Format: File lacks the correct Nexus binary magic header."
105
+ );
106
+ }
107
+ const metaLength = prefixBlock.readUInt32BE(4);
108
+ const metaStart = 8;
109
+ const metaEnd = metaStart + metaLength;
110
+ const metadataStr = prefixBlock.subarray(metaStart, metaEnd).toString("utf-8");
111
+ const metadata = JSON.parse(metadataStr);
112
+ const payloadLength = prefixBlock.readUInt32BE(metaEnd);
113
+ const payloadStart = metaEnd + 4;
114
+ const payloadEnd = payloadStart + payloadLength;
115
+ const payloadBuffer = prefixBlock.subarray(payloadStart, payloadEnd);
116
+ const iv = payloadBuffer.subarray(0, 16);
117
+ const cipherText = payloadBuffer.subarray(16);
118
+ const decipher = crypto.createDecipheriv(this.ALGORITHM, encryptionKey, iv);
119
+ let decrypted = decipher.update(cipherText);
120
+ decrypted = Buffer.concat([decrypted, decipher.final()]);
121
+ const data = JSON.parse(decrypted.toString("utf8"));
122
+ return { data, metadata };
123
+ }
124
+ };
125
+ BinaryCompiler.MAGIC_HEADER = "NEXS";
126
+ // 4-byte ASCII magic identifier
127
+ BinaryCompiler.ALGORITHM = "aes-256-cbc";
128
+
129
+ // src/content/local-cache-server.ts
6
130
  var LocalCache = class {
7
131
  constructor(customPath) {
132
+ this.apiKey = "";
8
133
  this.baseDir = customPath || ".nexus/local";
134
+ const config = getEnvConfig();
135
+ this.apiKey = config.apiKey || "";
9
136
  }
10
137
  isLoaded() {
11
138
  return fs.existsSync(path.resolve(process.cwd(), this.baseDir));
12
139
  }
13
140
  /**
14
- * Retrieve a specific page directly from its file.
141
+ * Helper to decrypt and decompile `.nx` files on-the-fly.
15
142
  */
16
- async getPage(slug) {
17
- try {
18
- const filePath = path.resolve(
19
- process.cwd(),
20
- this.baseDir,
21
- "pages",
22
- `${slug}.json`
23
- );
24
- if (fs.existsSync(filePath)) {
25
- const fileContent = fs.readFileSync(filePath, "utf-8");
26
- const pageData = JSON.parse(fileContent);
27
- return pageData.data !== void 0 ? pageData.data : pageData;
143
+ decompileFile(filePath) {
144
+ if (!this.apiKey) {
145
+ if (process.env.NODE_ENV === "development") {
146
+ console.warn(
147
+ "\u26A0\uFE0F NexusHub: Missing NEXUS_API_KEY. Local decryption bypassed."
148
+ );
28
149
  }
150
+ return null;
151
+ }
152
+ try {
153
+ const encryptedBuffer = fs.readFileSync(filePath);
154
+ const decompiled = BinaryCompiler.decompile(encryptedBuffer, this.apiKey);
155
+ return decompiled.data;
29
156
  } catch (error) {
30
157
  if (process.env.NODE_ENV === "development") {
31
- console.warn(
32
- `\u26A0\uFE0F NexusHub: Failed to load local page '${slug}':`,
33
- error
158
+ console.error(
159
+ `
160
+ \u{1F6A8} NexusHub Security Alert: Data Integrity Violation in file:
161
+ ${filePath}
162
+ Error: ${error.message}
163
+ This file has been disabled until regenerated via npx nexus pull.
164
+ `
34
165
  );
35
166
  }
167
+ return null;
168
+ }
169
+ }
170
+ /**
171
+ * Retrieve a specific page directly from its .nx file.
172
+ */
173
+ async getPage(slug) {
174
+ const filePath = path.resolve(
175
+ process.cwd(),
176
+ this.baseDir,
177
+ "pages",
178
+ `${slug}.nx`
179
+ );
180
+ if (fs.existsSync(filePath)) {
181
+ const pageData = this.decompileFile(filePath);
182
+ if (!pageData) return null;
183
+ return pageData.data !== void 0 ? pageData.data : pageData;
36
184
  }
37
185
  return null;
38
186
  }
39
187
  /**
40
- * Retrieve an entire collection from its specific collection file.
188
+ * Retrieve an entire collection from its specific .nx file.
41
189
  */
42
190
  async getCollection(collectionId) {
43
- try {
44
- const filePath = path.resolve(
45
- process.cwd(),
46
- this.baseDir,
47
- "collections",
48
- `${collectionId}.json`
49
- );
50
- if (fs.existsSync(filePath)) {
51
- const fileContent = fs.readFileSync(filePath, "utf-8");
52
- return JSON.parse(fileContent);
53
- }
54
- } catch (error) {
55
- if (process.env.NODE_ENV === "development") {
56
- console.warn(
57
- `\u26A0\uFE0F NexusHub: Failed to load local collection '${collectionId}':`,
58
- error
59
- );
60
- }
191
+ const filePath = path.resolve(
192
+ process.cwd(),
193
+ this.baseDir,
194
+ "collections",
195
+ `${collectionId}.nx`
196
+ );
197
+ if (fs.existsSync(filePath)) {
198
+ return this.decompileFile(filePath);
61
199
  }
62
200
  return null;
63
201
  }
64
202
  /**
65
- * Retrieve global settings from its file.
203
+ * Retrieve global settings from its .nx file.
66
204
  */
67
205
  async getGlobals() {
68
- try {
69
- const filePath = path.resolve(
70
- process.cwd(),
71
- this.baseDir,
72
- "globals.json"
73
- );
74
- if (fs.existsSync(filePath)) {
75
- const fileContent = fs.readFileSync(filePath, "utf-8");
76
- return JSON.parse(fileContent);
77
- }
78
- } catch {
206
+ const filePath = path.resolve(process.cwd(), this.baseDir, "globals.nx");
207
+ if (fs.existsSync(filePath)) {
208
+ return this.decompileFile(filePath);
79
209
  }
80
210
  return null;
81
211
  }
82
212
  /**
83
- * Returns a merged JSON of the entire workspace structure (for dev analytics/inspectors)
213
+ * Returns a merged JSON of the entire workspace structure.
84
214
  */
85
215
  async getAllData() {
86
216
  const pages = {};
@@ -90,8 +220,8 @@ var LocalCache = class {
90
220
  const pagesDir = path.resolve(process.cwd(), this.baseDir, "pages");
91
221
  if (fs.existsSync(pagesDir)) {
92
222
  for (const file of fs.readdirSync(pagesDir)) {
93
- if (file.endsWith(".json")) {
94
- const slug = path.basename(file, ".json");
223
+ if (file.endsWith(".nx")) {
224
+ const slug = path.basename(file, ".nx");
95
225
  pages[slug] = await this.getPage(slug);
96
226
  }
97
227
  }
@@ -99,8 +229,8 @@ var LocalCache = class {
99
229
  const colsDir = path.resolve(process.cwd(), this.baseDir, "collections");
100
230
  if (fs.existsSync(colsDir)) {
101
231
  for (const file of fs.readdirSync(colsDir)) {
102
- if (file.endsWith(".json")) {
103
- const id = path.basename(file, ".json");
232
+ if (file.endsWith(".nx")) {
233
+ const id = path.basename(file, ".nx");
104
234
  collections[id] = await this.getCollection(id) || [];
105
235
  }
106
236
  }