@unicitylabs/sphere-sdk 0.5.3 → 0.5.5

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.
Files changed (50) hide show
  1. package/README.md +2 -0
  2. package/dist/connect/index.cjs +145 -23
  3. package/dist/connect/index.cjs.map +1 -1
  4. package/dist/connect/index.d.cts +15 -2
  5. package/dist/connect/index.d.ts +15 -2
  6. package/dist/connect/index.js +145 -23
  7. package/dist/connect/index.js.map +1 -1
  8. package/dist/core/index.cjs +670 -473
  9. package/dist/core/index.cjs.map +1 -1
  10. package/dist/core/index.d.cts +123 -2
  11. package/dist/core/index.d.ts +123 -2
  12. package/dist/core/index.js +667 -473
  13. package/dist/core/index.js.map +1 -1
  14. package/dist/impl/browser/connect/index.cjs +119 -1
  15. package/dist/impl/browser/connect/index.cjs.map +1 -1
  16. package/dist/impl/browser/connect/index.d.cts +53 -1
  17. package/dist/impl/browser/connect/index.d.ts +53 -1
  18. package/dist/impl/browser/connect/index.js +119 -1
  19. package/dist/impl/browser/connect/index.js.map +1 -1
  20. package/dist/impl/browser/index.cjs +306 -193
  21. package/dist/impl/browser/index.cjs.map +1 -1
  22. package/dist/impl/browser/index.js +306 -193
  23. package/dist/impl/browser/index.js.map +1 -1
  24. package/dist/impl/browser/ipfs.cjs +134 -19
  25. package/dist/impl/browser/ipfs.cjs.map +1 -1
  26. package/dist/impl/browser/ipfs.js +134 -19
  27. package/dist/impl/browser/ipfs.js.map +1 -1
  28. package/dist/impl/nodejs/connect/index.cjs +101 -6
  29. package/dist/impl/nodejs/connect/index.cjs.map +1 -1
  30. package/dist/impl/nodejs/connect/index.d.cts +2 -0
  31. package/dist/impl/nodejs/connect/index.d.ts +2 -0
  32. package/dist/impl/nodejs/connect/index.js +101 -6
  33. package/dist/impl/nodejs/connect/index.js.map +1 -1
  34. package/dist/impl/nodejs/index.cjs +267 -152
  35. package/dist/impl/nodejs/index.cjs.map +1 -1
  36. package/dist/impl/nodejs/index.d.cts +2 -1
  37. package/dist/impl/nodejs/index.d.ts +2 -1
  38. package/dist/impl/nodejs/index.js +267 -152
  39. package/dist/impl/nodejs/index.js.map +1 -1
  40. package/dist/index.cjs +682 -493
  41. package/dist/index.cjs.map +1 -1
  42. package/dist/index.d.cts +124 -8
  43. package/dist/index.d.ts +124 -8
  44. package/dist/index.js +680 -493
  45. package/dist/index.js.map +1 -1
  46. package/dist/l1/index.cjs +139 -32
  47. package/dist/l1/index.cjs.map +1 -1
  48. package/dist/l1/index.js +139 -32
  49. package/dist/l1/index.js.map +1 -1
  50. package/package.json +1 -16
@@ -499,6 +499,20 @@ var sha256 = /* @__PURE__ */ createHasher(
499
499
  import * as bip39 from "bip39";
500
500
  import CryptoJS from "crypto-js";
501
501
  import elliptic from "elliptic";
502
+
503
+ // core/errors.ts
504
+ var SphereError = class extends Error {
505
+ code;
506
+ cause;
507
+ constructor(message, code, cause) {
508
+ super(message);
509
+ this.name = "SphereError";
510
+ this.code = code;
511
+ this.cause = cause;
512
+ }
513
+ };
514
+
515
+ // core/crypto.ts
502
516
  var ec = new elliptic.ec("secp256k1");
503
517
  var CURVE_ORDER = BigInt(
504
518
  "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"
@@ -732,6 +746,98 @@ var IpfsCache = class {
732
746
  }
733
747
  };
734
748
 
749
+ // core/logger.ts
750
+ var LOGGER_KEY = "__sphere_sdk_logger__";
751
+ function getState() {
752
+ const g = globalThis;
753
+ if (!g[LOGGER_KEY]) {
754
+ g[LOGGER_KEY] = { debug: false, tags: {}, handler: null };
755
+ }
756
+ return g[LOGGER_KEY];
757
+ }
758
+ function isEnabled(tag) {
759
+ const state = getState();
760
+ if (tag in state.tags) return state.tags[tag];
761
+ return state.debug;
762
+ }
763
+ var logger = {
764
+ /**
765
+ * Configure the logger. Can be called multiple times (last write wins).
766
+ * Typically called by createBrowserProviders(), createNodeProviders(), or Sphere.init().
767
+ */
768
+ configure(config) {
769
+ const state = getState();
770
+ if (config.debug !== void 0) state.debug = config.debug;
771
+ if (config.handler !== void 0) state.handler = config.handler;
772
+ },
773
+ /**
774
+ * Enable/disable debug logging for a specific tag.
775
+ * Per-tag setting overrides the global debug flag.
776
+ *
777
+ * @example
778
+ * ```ts
779
+ * logger.setTagDebug('Nostr', true); // enable only Nostr logs
780
+ * logger.setTagDebug('Nostr', false); // disable Nostr logs even if global debug=true
781
+ * ```
782
+ */
783
+ setTagDebug(tag, enabled) {
784
+ getState().tags[tag] = enabled;
785
+ },
786
+ /**
787
+ * Clear per-tag override, falling back to global debug flag.
788
+ */
789
+ clearTagDebug(tag) {
790
+ delete getState().tags[tag];
791
+ },
792
+ /** Returns true if debug mode is enabled for the given tag (or globally). */
793
+ isDebugEnabled(tag) {
794
+ if (tag) return isEnabled(tag);
795
+ return getState().debug;
796
+ },
797
+ /**
798
+ * Debug-level log. Only shown when debug is enabled (globally or for this tag).
799
+ * Use for detailed operational information.
800
+ */
801
+ debug(tag, message, ...args) {
802
+ if (!isEnabled(tag)) return;
803
+ const state = getState();
804
+ if (state.handler) {
805
+ state.handler("debug", tag, message, ...args);
806
+ } else {
807
+ console.log(`[${tag}]`, message, ...args);
808
+ }
809
+ },
810
+ /**
811
+ * Warning-level log. ALWAYS shown regardless of debug flag.
812
+ * Use for important but non-critical issues (timeouts, retries, degraded state).
813
+ */
814
+ warn(tag, message, ...args) {
815
+ const state = getState();
816
+ if (state.handler) {
817
+ state.handler("warn", tag, message, ...args);
818
+ } else {
819
+ console.warn(`[${tag}]`, message, ...args);
820
+ }
821
+ },
822
+ /**
823
+ * Error-level log. ALWAYS shown regardless of debug flag.
824
+ * Use for critical failures that should never be silenced.
825
+ */
826
+ error(tag, message, ...args) {
827
+ const state = getState();
828
+ if (state.handler) {
829
+ state.handler("error", tag, message, ...args);
830
+ } else {
831
+ console.error(`[${tag}]`, message, ...args);
832
+ }
833
+ },
834
+ /** Reset all logger state (debug flag, tags, handler). Primarily for tests. */
835
+ reset() {
836
+ const g = globalThis;
837
+ delete g[LOGGER_KEY];
838
+ }
839
+ };
840
+
735
841
  // impl/shared/ipfs/ipfs-http-client.ts
736
842
  var DEFAULT_CONNECTIVITY_TIMEOUT_MS = 5e3;
737
843
  var DEFAULT_FETCH_TIMEOUT_MS = 15e3;
@@ -888,7 +994,10 @@ var IpfsHttpClient = class {
888
994
  { headers: { Accept: "application/octet-stream" } }
889
995
  );
890
996
  if (!response.ok) {
891
- const body = await response.text().catch(() => "");
997
+ const body = await response.text().catch((err) => {
998
+ logger.debug("IPFS-HTTP", "Failed to read error response body", err);
999
+ return "";
1000
+ });
892
1001
  throw new IpfsError(
893
1002
  `Fetch failed: HTTP ${response.status}`,
894
1003
  classifyHttpStatus(response.status, body),
@@ -934,7 +1043,10 @@ var IpfsHttpClient = class {
934
1043
  { method: "POST" }
935
1044
  );
936
1045
  if (!response.ok) {
937
- const body = await response.text().catch(() => "");
1046
+ const body = await response.text().catch((err) => {
1047
+ logger.debug("IPFS-HTTP", "Failed to read error response body", err);
1048
+ return "";
1049
+ });
938
1050
  const category = classifyHttpStatus(response.status, body);
939
1051
  if (category === "NOT_FOUND") return null;
940
1052
  throw new IpfsError(`Routing API: HTTP ${response.status}`, category, gateway);
@@ -980,7 +1092,8 @@ var IpfsHttpClient = class {
980
1092
  }
981
1093
  }
982
1094
  return { cid: "", content };
983
- } catch {
1095
+ } catch (err) {
1096
+ logger.debug("IPFS-HTTP", "IPNS gateway resolution failed", err);
984
1097
  return null;
985
1098
  }
986
1099
  }
@@ -1045,7 +1158,10 @@ var IpfsHttpClient = class {
1045
1158
  { method: "POST", body: formData }
1046
1159
  );
1047
1160
  if (!response.ok) {
1048
- const errorText = await response.text().catch(() => "");
1161
+ const errorText = await response.text().catch((err) => {
1162
+ logger.debug("IPFS-HTTP", "Failed to read error response body", err);
1163
+ return "";
1164
+ });
1049
1165
  throw new IpfsError(
1050
1166
  `IPNS publish: HTTP ${response.status}: ${errorText.slice(0, 100)}`,
1051
1167
  classifyHttpStatus(response.status, errorText),
@@ -1122,9 +1238,7 @@ var IpfsHttpClient = class {
1122
1238
  }
1123
1239
  }
1124
1240
  log(message) {
1125
- if (this.debug) {
1126
- console.log(`[IPFS-HTTP] ${message}`);
1127
- }
1241
+ logger.debug("IPFS-HTTP", message);
1128
1242
  }
1129
1243
  };
1130
1244
 
@@ -1576,10 +1690,12 @@ var IpnsSubscriptionClient = class {
1576
1690
  startFallbackPolling() {
1577
1691
  if (this.fallbackPollInterval || !this.fallbackPollFn || this.destroyed) return;
1578
1692
  this.log(`Starting fallback polling (${this.fallbackPollIntervalMs / 1e3}s interval)`);
1579
- this.fallbackPollFn().catch(() => {
1693
+ this.fallbackPollFn().catch((err) => {
1694
+ logger.warn("IPNS-WS", "Fallback poll error:", err);
1580
1695
  });
1581
1696
  this.fallbackPollInterval = setInterval(() => {
1582
- this.fallbackPollFn?.().catch(() => {
1697
+ this.fallbackPollFn?.().catch((err) => {
1698
+ logger.warn("IPNS-WS", "Fallback poll error:", err);
1583
1699
  });
1584
1700
  }, this.fallbackPollIntervalMs);
1585
1701
  }
@@ -1593,9 +1709,7 @@ var IpnsSubscriptionClient = class {
1593
1709
  // Internal: Logging
1594
1710
  // ---------------------------------------------------------------------------
1595
1711
  log(message) {
1596
- if (this.debugEnabled) {
1597
- console.log(`[IPNS-WS] ${message}`);
1598
- }
1712
+ logger.debug("IPNS-WS", message);
1599
1713
  }
1600
1714
  };
1601
1715
 
@@ -1867,7 +1981,8 @@ var IpfsStorageProvider = class {
1867
1981
  } else {
1868
1982
  this.log("Warning: no healthy gateways found");
1869
1983
  }
1870
- }).catch(() => {
1984
+ }).catch((err) => {
1985
+ logger.warn("IPFS-Storage", "Gateway health check failed (non-fatal):", err);
1871
1986
  });
1872
1987
  this.isShuttingDown = false;
1873
1988
  this.status = "connected";
@@ -2031,7 +2146,7 @@ var IpfsStorageProvider = class {
2031
2146
  };
2032
2147
  const result = await this._doSave(baseData);
2033
2148
  if (!result.success) {
2034
- throw new Error(result.error ?? "Save failed");
2149
+ throw new SphereError(result.error ?? "Save failed", "STORAGE_ERROR");
2035
2150
  }
2036
2151
  this.log(`Flushed successfully: CID=${result.cid}`);
2037
2152
  } catch (error) {
@@ -2271,10 +2386,12 @@ var IpfsStorageProvider = class {
2271
2386
  if (this.flushTimer) {
2272
2387
  clearTimeout(this.flushTimer);
2273
2388
  this.flushTimer = null;
2274
- await this.flushQueue.enqueue(() => this.executeFlush()).catch(() => {
2389
+ await this.flushQueue.enqueue(() => this.executeFlush()).catch((err) => {
2390
+ logger.warn("IPFS-Storage", "Flush on shutdown failed:", err);
2275
2391
  });
2276
2392
  } else if (!this.pendingBuffer.isEmpty) {
2277
- await this.flushQueue.enqueue(() => this.executeFlush()).catch(() => {
2393
+ await this.flushQueue.enqueue(() => this.executeFlush()).catch((err) => {
2394
+ logger.warn("IPFS-Storage", "Flush on shutdown failed:", err);
2278
2395
  });
2279
2396
  } else {
2280
2397
  await this.flushQueue.enqueue(async () => {
@@ -2328,9 +2445,7 @@ var IpfsStorageProvider = class {
2328
2445
  }
2329
2446
  }
2330
2447
  log(message) {
2331
- if (this.debug) {
2332
- console.log(`[IPFS-Storage] ${message}`);
2333
- }
2448
+ logger.debug("IPFS-Storage", message);
2334
2449
  }
2335
2450
  };
2336
2451