universe-code 0.0.91 → 0.0.93

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,7 +1,9 @@
1
- class EncryptionService {
2
- constructor() {
3
- this.PASS =
4
- "9f2d3b7c4a6e8f0b-1c2d3e4f5a6b7c8d-9e0f1a2b3c4d5e6f7-a8b9c0d1e2f3a4";
1
+ export class EncryptionService {
2
+ constructor(pass) {
3
+ if (!pass) {
4
+ throw new Error("Encryption PASS is required");
5
+ }
6
+ this.PASS = pass;
5
7
  }
6
8
 
7
9
  // --- Encoding helpers ---
@@ -142,7 +144,4 @@ class EncryptionService {
142
144
  const [iv, payload] = data.split("###");
143
145
  return this.decryption1({ iv, payload });
144
146
  }
145
- }
146
-
147
- // Export / usage
148
- export const encryptionService = new EncryptionService();
147
+ }
@@ -1,3 +1,3 @@
1
1
  export { IdbService } from './indexdb/services/idbService.js';
2
2
  export { ColorSparkService, initSpark, singleSpark } from './spark/index.js';
3
- export { encryptionService } from './encryption/index.js';
3
+ export { EncryptionService } from './encryption/index.js';
@@ -1,19 +1,31 @@
1
- /* ========= UNIVERSAL CRYPTO SERVICE ========= */
2
- /* Works in Angular, React, Next.js (SSR safe), Browser */
3
-
4
- const SECRET_PASS =
5
- "9f2d3b7c4a6e8f0b-1c2d3e4f5a6b7c8d-9e0f1a2b3c4d5e6f7-a8b9c0d1e2f3a4";
1
+ /* ---------- INTERNAL STATE ---------- */
2
+ let SECRET_PASS = null;
3
+ let PASS = null;
4
+
5
+ /* ---------- INIT FUNCTION ---------- */
6
+ export function initEncryptionService(secret) {
7
+ if (!secret || typeof secret !== "string") {
8
+ throw new Error("SECRET_PASS is required to initialize encryption service");
9
+ }
10
+ SECRET_PASS = secret;
11
+ PASS = secret;
12
+ }
6
13
 
7
- const PASS = SECRET_PASS;
14
+ /* ---------- SAFETY CHECK ---------- */
15
+ function ensureInit() {
16
+ if (!SECRET_PASS) {
17
+ throw new Error("Crypto service not initialized. Call initEncryptionService()");
18
+ }
19
+ }
8
20
 
9
- /* ---------- Helpers ---------- */
21
+ /* ---------- HELPERS ---------- */
10
22
 
11
23
  const enc = (str) => new TextEncoder().encode(str);
12
24
  const dec = (buf) => new TextDecoder().decode(buf);
13
25
 
14
26
  const hasBuffer = typeof Buffer !== "undefined";
15
27
 
16
- /* Base64 encode (Node + Browser safe) */
28
+ /* Base64 encode (Node + Browser) */
17
29
  const b64 = (data) => {
18
30
  const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
19
31
 
@@ -28,7 +40,7 @@ const b64 = (data) => {
28
40
  return btoa(binary);
29
41
  };
30
42
 
31
- /* Base64 decode (Node + Browser safe) */
43
+ /* Base64 decode (Node + Browser) */
32
44
  const ub64 = (base64) => {
33
45
  if (hasBuffer) {
34
46
  return new Uint8Array(Buffer.from(base64, "base64"));
@@ -42,31 +54,30 @@ const ub64 = (base64) => {
42
54
  return bytes;
43
55
  };
44
56
 
45
- /* WebCrypto subtle (Browser + Node/Next) */
57
+ /* WebCrypto subtle (Browser + Node + Next SSR) */
46
58
  const subtle = () => {
47
59
  if (typeof window !== "undefined" && window.crypto?.subtle) {
48
60
  return window.crypto.subtle;
49
61
  }
50
-
51
62
  if (globalThis.crypto?.subtle) {
52
63
  return globalThis.crypto.subtle;
53
64
  }
54
-
55
- throw new Error("WebCrypto API not supported in this environment");
65
+ throw new Error("WebCrypto API not supported");
56
66
  };
57
67
 
58
- /* Random values (Browser + Node) */
68
+ /* Random IV (Browser + Node) */
59
69
  const randomIV = () => {
60
70
  if (typeof window !== "undefined" && window.crypto) {
61
71
  return window.crypto.getRandomValues(new Uint8Array(12));
62
72
  }
63
-
64
73
  return globalThis.crypto.getRandomValues(new Uint8Array(12));
65
74
  };
66
75
 
67
- /* ---------- Key Derivation ---------- */
76
+ /* ---------- KEY DERIVATION ---------- */
68
77
 
69
78
  async function getKey() {
79
+ ensureInit();
80
+
70
81
  const keyMaterial = await subtle().importKey(
71
82
  "raw",
72
83
  enc(SECRET_PASS),
@@ -90,6 +101,8 @@ async function getKey() {
90
101
  }
91
102
 
92
103
  async function key1() {
104
+ ensureInit();
105
+
93
106
  const keyMaterial = await subtle().importKey(
94
107
  "raw",
95
108
  enc(PASS),
@@ -112,11 +125,13 @@ async function key1() {
112
125
  );
113
126
  }
114
127
 
115
- /* ---------- Crypto Service ---------- */
128
+ /* ---------- CRYPTO SERVICE ---------- */
116
129
 
117
130
  export const encryptionService = {
118
- /* Encrypt using main key */
131
+ /* Encrypt (main key) */
119
132
  async encryption(obj) {
133
+ ensureInit();
134
+
120
135
  const iv = randomIV();
121
136
  const key = await getKey();
122
137
 
@@ -132,8 +147,10 @@ export const encryptionService = {
132
147
  };
133
148
  },
134
149
 
135
- /* Decrypt using main key */
150
+ /* Decrypt (main key) */
136
151
  async decryption(data) {
152
+ ensureInit();
153
+
137
154
  const key = await getKey();
138
155
 
139
156
  const decrypted = await subtle().decrypt(
@@ -145,8 +162,10 @@ export const encryptionService = {
145
162
  return JSON.parse(dec(decrypted));
146
163
  },
147
164
 
148
- /* Encrypt using secondary key */
165
+ /* Encrypt (secondary key) */
149
166
  async encryption1(obj) {
167
+ ensureInit();
168
+
150
169
  const iv = randomIV();
151
170
  const key = await key1();
152
171
 
@@ -162,8 +181,10 @@ export const encryptionService = {
162
181
  };
163
182
  },
164
183
 
165
- /* Decrypt using secondary key */
184
+ /* Decrypt (secondary key) */
166
185
  async decryption1(data) {
186
+ ensureInit();
187
+
167
188
  const key = await key1();
168
189
 
169
190
  const decrypted = await subtle().decrypt(
@@ -177,6 +198,8 @@ export const encryptionService = {
177
198
 
178
199
  /* API helper */
179
200
  async decryptApiResponse(data) {
201
+ ensureInit();
202
+
180
203
  if (!data || !data.includes("###")) return null;
181
204
  const [iv, payload] = data.split("###");
182
205
  return this.decryption1({ iv, payload });
@@ -2,10 +2,10 @@ import { getIdbStore } from './indexdb/store/idbStore.js';
2
2
  import { useIdbStore } from './indexdb/hooks/idbHook.js';
3
3
  import { configureIdb } from "./indexdb/store/config.js";
4
4
  import { useColorSpark } from "./spark/index.js";
5
- import { encryptionService } from "./encryption/index.js";
5
+ import { encryptionService, initEncryptionService } from "./encryption/index.js";
6
6
 
7
7
  // Export everything as a unified object or individual pieces
8
- export { configureIdb, getIdbStore, useIdbStore, useColorSpark, encryptionService };
8
+ export { configureIdb, getIdbStore, useIdbStore, useColorSpark, encryptionService, initEncryptionService };
9
9
 
10
10
  // Default export for easy usage
11
11
  export default {
@@ -13,5 +13,6 @@ export default {
13
13
  getIdbStore,
14
14
  useIdbStore,
15
15
  useColorSpark,
16
- encryptionService
16
+ encryptionService,
17
+ initEncryptionService
17
18
  };
@@ -1,6 +1,43 @@
1
- // Inject plain CSS for UniverseDataTable
2
- const style = document.createElement("style");
3
- style.textContent = `
1
+ class UniverseDataTable {
2
+ constructor({
3
+ container,
4
+ apiData = [],
5
+ columns = [],
6
+ pagination = {},
7
+ jumpToPage = "",
8
+ shouldEnableJumpToPage = false,
9
+ handleJumpToPage = () => { },
10
+ handleKeyPress = () => { },
11
+ handlePageChange = () => { },
12
+ shouldShowPagination = true,
13
+ }) {
14
+ if (!container) throw new Error("UniverseDataTable: container is required");
15
+ if (typeof document !== "undefined") {
16
+ this.injectStyles();
17
+ }
18
+
19
+ this.container = container;
20
+ this.apiData = apiData;
21
+ this.columns = columns;
22
+ this.pagination = pagination;
23
+
24
+ this.jumpToPage = jumpToPage;
25
+ this.shouldEnableJumpToPage = shouldEnableJumpToPage;
26
+ this.handleJumpToPage = handleJumpToPage;
27
+ this.handleKeyPress = handleKeyPress;
28
+ this.handlePageChange = handlePageChange;
29
+
30
+ this.shouldShowPagination = shouldShowPagination;
31
+
32
+ this.render();
33
+ }
34
+
35
+ injectStyles() {
36
+ if (document.getElementById("udt-style")) return;
37
+
38
+ const style = document.createElement("style");
39
+ style.id = "udt-style";
40
+ style.textContent = `
4
41
  /* Container & Table */
5
42
  .udt-container {
6
43
  background-color: #fff;
@@ -179,39 +216,8 @@ style.textContent = `
179
216
  }
180
217
  }
181
218
  `;
182
- document.head.appendChild(style);
183
-
184
- // UniverseDataTable Class
185
- class UniverseDataTable {
186
- constructor({
187
- container,
188
- apiData = [],
189
- columns = [],
190
- pagination = {},
191
- jumpToPage = "",
192
- shouldEnableJumpToPage = false,
193
- handleJumpToPage = () => { },
194
- handleKeyPress = () => { },
195
- handlePageChange = () => { },
196
- shouldShowPagination = true,
197
- }) {
198
- if (!container) throw new Error("UniverseDataTable: container is required");
199
-
200
- this.container = container;
201
- this.apiData = apiData;
202
- this.columns = columns;
203
- this.pagination = pagination;
204
-
205
- this.jumpToPage = jumpToPage;
206
- this.shouldEnableJumpToPage = shouldEnableJumpToPage;
207
- this.handleJumpToPage = handleJumpToPage;
208
- this.handleKeyPress = handleKeyPress;
209
- this.handlePageChange = handlePageChange;
210
-
211
- this.shouldShowPagination = shouldShowPagination;
212
-
213
- this.render();
214
- }
219
+ document.head.appendChild(style);
220
+ }
215
221
 
216
222
  th(label) {
217
223
  return `<th class="udt-th">${label}</th>`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universe-code",
3
- "version": "0.0.91",
3
+ "version": "0.0.93",
4
4
  "description": "Universal utility functions for all JS frameworks",
5
5
  "license": "ISC",
6
6
  "type": "module",