@unicitylabs/sphere-sdk 0.17.5 → 0.17.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.
@@ -225,6 +225,7 @@ declare class FileStorageProvider implements StorageProvider {
225
225
  private isTxtMode;
226
226
  private network?;
227
227
  private data;
228
+ private loading;
228
229
  private status;
229
230
  private _identity;
230
231
  constructor(config: FileStorageProviderConfig | string);
@@ -232,6 +233,14 @@ declare class FileStorageProvider implements StorageProvider {
232
233
  getIdentity(): FullIdentity | null;
233
234
  connect(): Promise<void>;
234
235
  disconnect(): Promise<void>;
236
+ /**
237
+ * `data` holds the file's contents only after connect() has read them. Used before
238
+ * that, this provider answered reads from an empty map and — worse — save() rewrote
239
+ * the WHOLE file from it, so one set() on a never-connected provider erased the wallet
240
+ * and every payment journal (#811). Every data method now loads the file first; an
241
+ * explicit connect() stays the normal path and this is the safety net under it.
242
+ */
243
+ private ensureLoaded;
235
244
  isConnected(): boolean;
236
245
  getStatus(): ProviderStatus;
237
246
  get(key: string): Promise<string | null>;
@@ -225,6 +225,7 @@ declare class FileStorageProvider implements StorageProvider {
225
225
  private isTxtMode;
226
226
  private network?;
227
227
  private data;
228
+ private loading;
228
229
  private status;
229
230
  private _identity;
230
231
  constructor(config: FileStorageProviderConfig | string);
@@ -232,6 +233,14 @@ declare class FileStorageProvider implements StorageProvider {
232
233
  getIdentity(): FullIdentity | null;
233
234
  connect(): Promise<void>;
234
235
  disconnect(): Promise<void>;
236
+ /**
237
+ * `data` holds the file's contents only after connect() has read them. Used before
238
+ * that, this provider answered reads from an empty map and — worse — save() rewrote
239
+ * the WHOLE file from it, so one set() on a never-connected provider erased the wallet
240
+ * and every payment journal (#811). Every data method now loads the file first; an
241
+ * explicit connect() stays the normal path and this is the safety net under it.
242
+ */
243
+ private ensureLoaded;
235
244
  isConnected(): boolean;
236
245
  getStatus(): ProviderStatus;
237
246
  get(key: string): Promise<string | null>;
@@ -23844,6 +23844,7 @@ var FileStorageProvider = class {
23844
23844
  isTxtMode;
23845
23845
  network;
23846
23846
  data = {};
23847
+ loading = null;
23847
23848
  status = "disconnected";
23848
23849
  _identity = null;
23849
23850
  constructor(config) {
@@ -23908,9 +23909,25 @@ var FileStorageProvider = class {
23908
23909
  this.status = "connected";
23909
23910
  }
23910
23911
  async disconnect() {
23911
- await this.save();
23912
+ if (this.status === "connected") {
23913
+ await this.save();
23914
+ }
23912
23915
  this.status = "disconnected";
23913
23916
  }
23917
+ /**
23918
+ * `data` holds the file's contents only after connect() has read them. Used before
23919
+ * that, this provider answered reads from an empty map and — worse — save() rewrote
23920
+ * the WHOLE file from it, so one set() on a never-connected provider erased the wallet
23921
+ * and every payment journal (#811). Every data method now loads the file first; an
23922
+ * explicit connect() stays the normal path and this is the safety net under it.
23923
+ */
23924
+ async ensureLoaded() {
23925
+ if (this.status === "connected") return;
23926
+ this.loading ??= this.connect().finally(() => {
23927
+ this.loading = null;
23928
+ });
23929
+ await this.loading;
23930
+ }
23914
23931
  isConnected() {
23915
23932
  return this.status === "connected";
23916
23933
  }
@@ -23918,24 +23935,29 @@ var FileStorageProvider = class {
23918
23935
  return this.status;
23919
23936
  }
23920
23937
  async get(key) {
23938
+ await this.ensureLoaded();
23921
23939
  const fullKey = this.getFullKey(key);
23922
23940
  return this.data[fullKey] ?? null;
23923
23941
  }
23924
23942
  async set(key, value) {
23943
+ await this.ensureLoaded();
23925
23944
  const fullKey = this.getFullKey(key);
23926
23945
  this.data[fullKey] = value;
23927
23946
  await this.save();
23928
23947
  }
23929
23948
  async remove(key) {
23949
+ await this.ensureLoaded();
23930
23950
  const fullKey = this.getFullKey(key);
23931
23951
  delete this.data[fullKey];
23932
23952
  await this.save();
23933
23953
  }
23934
23954
  async has(key) {
23955
+ await this.ensureLoaded();
23935
23956
  const fullKey = this.getFullKey(key);
23936
23957
  return fullKey in this.data;
23937
23958
  }
23938
23959
  async keys(prefix) {
23960
+ await this.ensureLoaded();
23939
23961
  const allKeys = Object.keys(this.data);
23940
23962
  if (prefix) {
23941
23963
  return allKeys.filter((k) => k.startsWith(prefix));
@@ -23943,6 +23965,7 @@ var FileStorageProvider = class {
23943
23965
  return allKeys;
23944
23966
  }
23945
23967
  async clear(prefix) {
23968
+ await this.ensureLoaded();
23946
23969
  if (prefix) {
23947
23970
  const keysToDelete = Object.keys(this.data).filter((k) => k.startsWith(prefix));
23948
23971
  for (const key of keysToDelete) {