@ojolowoblue/lamba 1.0.5 → 1.0.7

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.
@@ -5,34 +5,33 @@ import { useState, useEffect } from "react";
5
5
  var STORAGE_PRESETS_KEY = "__lamba_presets__";
6
6
  var STORAGE_ACTIVE_PRESET_KEY = "__lamba_active_preset__";
7
7
  var PresetManager = class {
8
- constructor() {
8
+ constructor(storage) {
9
9
  this.presets = [];
10
10
  this.activePresetId = null;
11
+ this.storage = storage;
11
12
  this.loadFromStorage();
12
13
  }
13
14
  loadFromStorage() {
14
- if (typeof localStorage === "undefined") return;
15
15
  try {
16
- const rawPresets = localStorage.getItem(STORAGE_PRESETS_KEY);
16
+ const rawPresets = this.storage.getItem(STORAGE_PRESETS_KEY);
17
17
  if (rawPresets) {
18
18
  this.presets = JSON.parse(rawPresets);
19
19
  }
20
- this.activePresetId = localStorage.getItem(STORAGE_ACTIVE_PRESET_KEY);
20
+ this.activePresetId = this.storage.getItem(STORAGE_ACTIVE_PRESET_KEY);
21
21
  } catch (e) {
22
- console.warn("[lamba] Failed to parse presets from localStorage", e);
22
+ console.warn("[lamba] Failed to parse presets from storage", e);
23
23
  }
24
24
  }
25
25
  saveToStorage() {
26
- if (typeof localStorage === "undefined") return;
27
26
  try {
28
- localStorage.setItem(STORAGE_PRESETS_KEY, JSON.stringify(this.presets));
27
+ this.storage.setItem(STORAGE_PRESETS_KEY, JSON.stringify(this.presets));
29
28
  if (this.activePresetId) {
30
- localStorage.setItem(STORAGE_ACTIVE_PRESET_KEY, this.activePresetId);
29
+ this.storage.setItem(STORAGE_ACTIVE_PRESET_KEY, this.activePresetId);
31
30
  } else {
32
- localStorage.removeItem(STORAGE_ACTIVE_PRESET_KEY);
31
+ this.storage.removeItem(STORAGE_ACTIVE_PRESET_KEY);
33
32
  }
34
33
  } catch (e) {
35
- console.warn("[lamba] Failed to save presets to localStorage", e);
34
+ console.warn("[lamba] Failed to save presets to storage", e);
36
35
  }
37
36
  }
38
37
  getPresets() {
@@ -184,6 +183,99 @@ async function tryFetchRootEnvFile() {
184
183
  return {};
185
184
  }
186
185
 
186
+ // src/core/storage.ts
187
+ var LocalStorageAdapter = class {
188
+ getItem(key) {
189
+ try {
190
+ return typeof localStorage !== "undefined" ? localStorage.getItem(key) : null;
191
+ } catch {
192
+ return null;
193
+ }
194
+ }
195
+ setItem(key, value) {
196
+ try {
197
+ if (typeof localStorage !== "undefined") localStorage.setItem(key, value);
198
+ } catch {
199
+ }
200
+ }
201
+ removeItem(key) {
202
+ try {
203
+ if (typeof localStorage !== "undefined") localStorage.removeItem(key);
204
+ } catch {
205
+ }
206
+ }
207
+ clear() {
208
+ try {
209
+ if (typeof localStorage !== "undefined") {
210
+ localStorage.removeItem("__lamba_overrides__");
211
+ localStorage.removeItem("__lamba_presets__");
212
+ localStorage.removeItem("__lamba_active_preset__");
213
+ }
214
+ } catch {
215
+ }
216
+ }
217
+ };
218
+ var SessionStorageAdapter = class {
219
+ getItem(key) {
220
+ try {
221
+ return typeof sessionStorage !== "undefined" ? sessionStorage.getItem(key) : null;
222
+ } catch {
223
+ return null;
224
+ }
225
+ }
226
+ setItem(key, value) {
227
+ try {
228
+ if (typeof sessionStorage !== "undefined") sessionStorage.setItem(key, value);
229
+ } catch {
230
+ }
231
+ }
232
+ removeItem(key) {
233
+ try {
234
+ if (typeof sessionStorage !== "undefined") sessionStorage.removeItem(key);
235
+ } catch {
236
+ }
237
+ }
238
+ clear() {
239
+ try {
240
+ if (typeof sessionStorage !== "undefined") {
241
+ sessionStorage.removeItem("__lamba_overrides__");
242
+ sessionStorage.removeItem("__lamba_presets__");
243
+ sessionStorage.removeItem("__lamba_active_preset__");
244
+ }
245
+ } catch {
246
+ }
247
+ }
248
+ };
249
+ var MemoryStorageAdapter = class {
250
+ constructor() {
251
+ this.store = /* @__PURE__ */ new Map();
252
+ }
253
+ getItem(key) {
254
+ return this.store.get(key) ?? null;
255
+ }
256
+ setItem(key, value) {
257
+ this.store.set(key, value);
258
+ }
259
+ removeItem(key) {
260
+ this.store.delete(key);
261
+ }
262
+ clear() {
263
+ this.store.clear();
264
+ }
265
+ };
266
+ function createStorageAdapter(strategy = "local", custom) {
267
+ if (custom) return custom;
268
+ switch (strategy) {
269
+ case "session":
270
+ return new SessionStorageAdapter();
271
+ case "memory":
272
+ return new MemoryStorageAdapter();
273
+ case "local":
274
+ default:
275
+ return new LocalStorageAdapter();
276
+ }
277
+ }
278
+
187
279
  // src/core/store.ts
188
280
  var STORAGE_OVERRIDES_KEY = "__lamba_overrides__";
189
281
  var DEFAULT_SECRET_PATTERN = /(KEY|SECRET|TOKEN|PASSWORD|AUTH|PRIVATE|CREDENTIAL|SIGNATURE)/i;
@@ -195,7 +287,8 @@ var EnvStore = class {
195
287
  this.storeChangeListeners = /* @__PURE__ */ new Set();
196
288
  this.secretPattern = options.secretKeysPattern || DEFAULT_SECRET_PATTERN;
197
289
  this.allowedPrefixes = options.allowedPrefixes;
198
- this.presetManager = new PresetManager();
290
+ this.storage = createStorageAdapter(options.storageStrategy ?? "local", options.storage);
291
+ this.presetManager = new PresetManager(this.storage);
199
292
  this.loadOverridesFromStorage();
200
293
  if (options.env) {
201
294
  this.mergeDefaults(options.env);
@@ -212,22 +305,20 @@ var EnvStore = class {
212
305
  this.patchProcessEnv();
213
306
  }
214
307
  loadOverridesFromStorage() {
215
- if (typeof localStorage === "undefined") return;
216
308
  try {
217
- const stored = localStorage.getItem(STORAGE_OVERRIDES_KEY);
309
+ const stored = this.storage.getItem(STORAGE_OVERRIDES_KEY);
218
310
  if (stored) {
219
311
  this.overrides = JSON.parse(stored);
220
312
  }
221
313
  } catch (e) {
222
- console.warn("[lamba] Failed to parse overrides from localStorage", e);
314
+ console.warn("[lamba] Failed to parse overrides from storage", e);
223
315
  }
224
316
  }
225
317
  saveOverridesToStorage() {
226
- if (typeof localStorage === "undefined") return;
227
318
  try {
228
- localStorage.setItem(STORAGE_OVERRIDES_KEY, JSON.stringify(this.overrides));
319
+ this.storage.setItem(STORAGE_OVERRIDES_KEY, JSON.stringify(this.overrides));
229
320
  } catch (e) {
230
- console.warn("[lamba] Failed to save overrides to localStorage", e);
321
+ console.warn("[lamba] Failed to save overrides to storage", e);
231
322
  }
232
323
  }
233
324
  /**
@@ -1448,7 +1539,10 @@ var LambaManager = class {
1448
1539
  interceptNetworkRequests: true,
1449
1540
  ...options
1450
1541
  };
1451
- if (this.options.enabled === false) return this;
1542
+ if (this.options.enabled === false) {
1543
+ this.purge(options);
1544
+ return this;
1545
+ }
1452
1546
  this.store = new EnvStore(this.options);
1453
1547
  if (this.options.interceptNetworkRequests !== false) {
1454
1548
  this.networkInterceptor = new NetworkInterceptor(this.store);
@@ -1505,6 +1599,18 @@ var LambaManager = class {
1505
1599
  reset() {
1506
1600
  this.store?.resetAllOverrides();
1507
1601
  }
1602
+ /**
1603
+ * Removes ALL lamba data from storage (overrides, presets, active preset).
1604
+ * Safe to call even when lamba has not been initialized.
1605
+ * Useful when disabling lamba to ensure no stale data remains in the browser.
1606
+ *
1607
+ * @param options - Optionally pass storage options to target the correct adapter.
1608
+ * Defaults to 'local' (localStorage) to cover the common case.
1609
+ */
1610
+ purge(options) {
1611
+ const adapter = this.store?.storage ?? createStorageAdapter(options?.storageStrategy ?? "local", options?.storage);
1612
+ adapter.clear();
1613
+ }
1508
1614
  /**
1509
1615
  * Subscribes to environment variable changes.
1510
1616
  */