@interncom/diplomatic 0.0.35 → 0.0.37

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.
@@ -28,6 +28,7 @@ declare class IDBStore implements IClientStateStore {
28
28
  numDownloads: () => Promise<number>;
29
29
  storeOp: (sha256: Uint8Array, cipherOp: Uint8Array) => Promise<void>;
30
30
  clearOp: (sha256: Uint8Array) => Promise<void>;
31
+ hasOp: (sha256: Uint8Array) => Promise<boolean>;
31
32
  }
32
33
  export declare const idbStore: IDBStore;
33
34
  export {};
package/dist/index.mjs CHANGED
@@ -199,6 +199,10 @@ var LocalStorageStore = class {
199
199
  const hex = btoh(sha256);
200
200
  this.ops.delete(hex);
201
201
  };
202
+ hasOp = async (sha256) => {
203
+ const hex = btoh(sha256);
204
+ return this.ops.has(hex);
205
+ };
202
206
  };
203
207
  var localStorageStore = new LocalStorageStore();
204
208
 
@@ -501,18 +505,21 @@ var IDBStore = class {
501
505
  await db.put("metaKV", str, "lastFetchedAt");
502
506
  }
503
507
  enqueueUpload = async (sha256, cipherOp) => {
504
- await db.put("uploadQueue", { sha256, cipherOp });
508
+ const hex = btoh(sha256);
509
+ await db.put("uploadQueue", { sha256: hex, cipherOp });
505
510
  };
506
511
  dequeueUpload = async (sha256) => {
507
- await db.delete("uploadQueue", sha256);
512
+ const hex = btoh(sha256);
513
+ await db.delete("uploadQueue", hex);
508
514
  };
509
515
  peekUpload = async (sha256) => {
510
- const row = await db.get("uploadQueue", sha256);
516
+ const hex = btoh(sha256);
517
+ const row = await db.get("uploadQueue", hex);
511
518
  return row?.cipherOp;
512
519
  };
513
520
  listUploads = async () => {
514
- const sha256s = await db.getAllKeys("uploadQueue");
515
- return sha256s;
521
+ const hexes = await db.getAllKeys("uploadQueue");
522
+ return hexes.map(htob);
516
523
  };
517
524
  numUploads = async () => {
518
525
  const num = await db.count("uploadQueue");
@@ -520,24 +527,33 @@ var IDBStore = class {
520
527
  };
521
528
  downloadQueue = /* @__PURE__ */ new Set();
522
529
  enqueueDownload = async (sha256, recordedAt) => {
523
- await db.put("downloadQueue", { sha256, recordedAt });
530
+ const hex = btoh(sha256);
531
+ await db.put("downloadQueue", { sha256: hex, recordedAt });
524
532
  };
525
533
  dequeueDownload = async (sha256) => {
526
- await db.delete("downloadQueue", sha256);
534
+ const hex = btoh(sha256);
535
+ await db.delete("downloadQueue", hex);
527
536
  };
528
537
  listDownloads = async () => {
529
538
  const list = await db.getAll("downloadQueue");
530
- return list;
539
+ return list.map((item) => ({ sha256: htob(item.sha256), recordedAt: item.recordedAt }));
531
540
  };
532
541
  numDownloads = async () => {
533
542
  const num = await db.count("downloadQueue");
534
543
  return num;
535
544
  };
536
545
  storeOp = async (sha256, cipherOp) => {
537
- await db.put("ops", { sha256, cipherOp });
546
+ const hex = btoh(sha256);
547
+ await db.put("ops", { sha256: hex, cipherOp });
538
548
  };
539
549
  clearOp = async (sha256) => {
540
- await db.delete("ops", sha256);
550
+ const hex = btoh(sha256);
551
+ await db.delete("ops", hex);
552
+ };
553
+ hasOp = async (sha256) => {
554
+ const hex = btoh(sha256);
555
+ const op = await db.get("ops", hex);
556
+ return op !== void 0;
541
557
  };
542
558
  };
543
559
  var idbStore = new IDBStore();
@@ -8983,7 +8999,7 @@ var DiplomaticClient = class {
8983
8999
  return [];
8984
9000
  }
8985
9001
  const lastFetchedAt = await this.store.getLastFetchedAt();
8986
- const begin = lastFetchedAt ?? /* @__PURE__ */ new Date(0);
9002
+ const begin = /* @__PURE__ */ new Date(0);
8987
9003
  const { hostURL, hostKeyPair } = this;
8988
9004
  const resp = await api_default.listDeltas(hostURL, begin, hostKeyPair);
8989
9005
  for (const item of resp.deltas) {
@@ -9000,13 +9016,19 @@ var DiplomaticClient = class {
9000
9016
  const items = await this.store.listDownloads();
9001
9017
  items.sort((i1, i2) => i1.recordedAt.getTime() - i2.recordedAt.getTime());
9002
9018
  for (const item of items) {
9003
- const cipher = await api_default.getDelta(hostURL, item.sha256, hostKeyPair);
9004
- const packed = await crypto_default.decryptXSalsa20Poly1305Combined(cipher, encKey);
9005
- const op = decode(packed);
9019
+ if (await this.store.hasOp(item.sha256)) {
9020
+ console.log("already got it skipping");
9021
+ await this.store.dequeueDownload(item.sha256);
9022
+ this.emitUpdate();
9023
+ continue;
9024
+ }
9006
9025
  try {
9026
+ const cipher = await api_default.getDelta(hostURL, item.sha256, hostKeyPair);
9027
+ const packed = await crypto_default.decryptXSalsa20Poly1305Combined(cipher, encKey);
9028
+ const op = decode(packed);
9007
9029
  const sha256 = await crypto_default.sha256Hash(cipher);
9008
- await this.store.storeOp(sha256, cipher);
9009
9030
  await this.stateManager.apply(op);
9031
+ await this.store.storeOp(sha256, cipher);
9010
9032
  await this.store.dequeueDownload(sha256);
9011
9033
  this.emitUpdate();
9012
9034
  } catch {
@@ -9045,11 +9067,11 @@ var DiplomaticClient = class {
9045
9067
  const packed = encode(op);
9046
9068
  const cipherOp = await crypto_default.encryptXSalsa20Poly1305Combined(packed, this.encKey);
9047
9069
  const sha256 = await crypto_default.sha256Hash(cipherOp);
9048
- await this.store.storeOp(sha256, cipherOp);
9049
9070
  await this.store.enqueueUpload(sha256, cipherOp);
9050
9071
  this.emitUpdate();
9051
9072
  try {
9052
9073
  await this.stateManager.apply(op);
9074
+ await this.store.storeOp(sha256, cipherOp);
9053
9075
  } catch (err) {
9054
9076
  await this.store.dequeueUpload(sha256);
9055
9077
  this.emitUpdate();
@@ -26,6 +26,7 @@ declare class LocalStorageStore implements IClientStateStore {
26
26
  ops: Map<string, Uint8Array>;
27
27
  storeOp: (sha256: Uint8Array, cipherOp: Uint8Array) => Promise<void>;
28
28
  clearOp: (sha256: Uint8Array) => Promise<void>;
29
+ hasOp: (sha256: Uint8Array) => Promise<boolean>;
29
30
  }
30
31
  export declare const localStorageStore: LocalStorageStore;
31
32
  export {};
@@ -30,6 +30,7 @@ declare class MemoryStore implements IClientStateStore {
30
30
  ops: Map<string, Uint8Array>;
31
31
  storeOp: (sha256: Uint8Array, cipherOp: Uint8Array) => Promise<void>;
32
32
  clearOp: (sha256: Uint8Array) => Promise<void>;
33
+ hasOp: (sha256: Uint8Array) => Promise<boolean>;
33
34
  }
34
35
  export declare const memoryStore: MemoryStore;
35
36
  export {};
package/dist/types.d.ts CHANGED
@@ -21,6 +21,7 @@ export interface IClientStateStore {
21
21
  numDownloads: () => Promise<number>;
22
22
  storeOp: (sha256: Uint8Array, cipherOp: Uint8Array) => Promise<void>;
23
23
  clearOp: (sha256: Uint8Array) => Promise<void>;
24
+ hasOp: (sha256: Uint8Array) => Promise<boolean>;
24
25
  }
25
26
  export interface IDiplomaticClientState {
26
27
  hasSeed: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interncom/diplomatic",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",