@abraca/dabra 1.2.0 → 1.3.0

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.
@@ -7310,6 +7310,30 @@ var CryptoIdentityKeystore = class {
7310
7310
  db.close();
7311
7311
  }
7312
7312
  }
7313
+ /**
7314
+ * Updates the cached username for a given credential (or the first cached identity).
7315
+ * Call this after the user sets/changes their display name so it persists across devices.
7316
+ */
7317
+ async setUsername(username, credentialIdHint) {
7318
+ const db = await openDb$4();
7319
+ try {
7320
+ if (credentialIdHint) {
7321
+ const stored = await dbGet(db, credentialIdHint);
7322
+ if (stored) await dbPut(db, credentialIdHint, {
7323
+ ...stored,
7324
+ username
7325
+ });
7326
+ } else {
7327
+ const all = await dbGetAll(db);
7328
+ if (all.length > 0) await dbPut(db, all[0].key, {
7329
+ ...all[0].value,
7330
+ username
7331
+ });
7332
+ }
7333
+ } finally {
7334
+ db.close();
7335
+ }
7336
+ }
7313
7337
  /** Returns true if an identity is cached in IndexedDB. */
7314
7338
  async hasIdentity() {
7315
7339
  const db = await openDb$4();
@@ -9932,6 +9956,8 @@ var AbracadabraWebRTC = class AbracadabraWebRTC extends EventEmitter {
9932
9956
  this.yjsChannels = /* @__PURE__ */ new Map();
9933
9957
  this.fileChannels = /* @__PURE__ */ new Map();
9934
9958
  this.e2eeChannels = /* @__PURE__ */ new Map();
9959
+ this._resolvedE2ee = null;
9960
+ this._resolveE2eePromise = null;
9935
9961
  this.peers = /* @__PURE__ */ new Map();
9936
9962
  this.localPeerId = null;
9937
9963
  this.isConnected = false;
@@ -10198,9 +10224,27 @@ var AbracadabraWebRTC = class AbracadabraWebRTC extends EventEmitter {
10198
10224
  this.attachDataHandlers(peerId, pc);
10199
10225
  return pc;
10200
10226
  }
10227
+ /** Resolve the E2EE identity, supporting both pre-resolved objects and lazy factories. */
10228
+ async resolveE2ee() {
10229
+ if (this._resolvedE2ee) return this._resolvedE2ee;
10230
+ if (!this.config.e2ee) return null;
10231
+ if (typeof this.config.e2ee === "function") {
10232
+ if (!this._resolveE2eePromise) this._resolveE2eePromise = this.config.e2ee().then((id) => {
10233
+ this._resolvedE2ee = id;
10234
+ return id;
10235
+ });
10236
+ return this._resolveE2eePromise;
10237
+ }
10238
+ this._resolvedE2ee = this.config.e2ee;
10239
+ return this._resolvedE2ee;
10240
+ }
10201
10241
  attachDataHandlers(peerId, pc) {
10202
- if (this.config.e2ee) {
10203
- const e2ee = new E2EEChannel(this.config.e2ee, this.config.docId);
10242
+ if (this.config.e2ee) this.resolveE2ee().then((identity) => {
10243
+ if (!identity) {
10244
+ this.startDataSync(peerId, pc);
10245
+ return;
10246
+ }
10247
+ const e2ee = new E2EEChannel(identity, this.config.docId);
10204
10248
  this.e2eeChannels.set(peerId, e2ee);
10205
10249
  pc.router.setEncryptor(e2ee);
10206
10250
  pc.router.on("channelMessage", async ({ name, data }) => {
@@ -10227,7 +10271,14 @@ var AbracadabraWebRTC = class AbracadabraWebRTC extends EventEmitter {
10227
10271
  error: err
10228
10272
  });
10229
10273
  });
10230
- } else this.startDataSync(peerId, pc);
10274
+ }).catch((err) => {
10275
+ this.emit("e2eeFailed", {
10276
+ peerId,
10277
+ error: err
10278
+ });
10279
+ this.startDataSync(peerId, pc);
10280
+ });
10281
+ else this.startDataSync(peerId, pc);
10231
10282
  }
10232
10283
  startDataSync(peerId, pc) {
10233
10284
  if (this.config.document && this.config.enableDocSync) {