@interncom/diplomatic 0.0.12 → 0.0.14

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.
@@ -3,6 +3,7 @@ declare class IDBStore implements IClientStateStore {
3
3
  seed?: Uint8Array;
4
4
  hostURL?: string;
5
5
  hostID?: string;
6
+ wipe(): Promise<void>;
6
7
  getSeed(): Promise<Uint8Array | undefined>;
7
8
  setSeed(seed: Uint8Array): Promise<void>;
8
9
  getHostURL(): Promise<string | undefined>;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { useClientState } from "./useClient";
1
+ import { useClientState, useSyncOnResume } from "./useClient";
2
2
  import { StateManager, useStateWatcher } from './state';
3
3
  import { localStorageStore } from "./localStorageStore";
4
4
  import { idbStore } from "./idbStore";
@@ -6,5 +6,5 @@ import DiplomaticClient from "./client";
6
6
  import libsodiumCrypto from "./crypto";
7
7
  import { btoh, htob } from "./shared/lib";
8
8
  import { type IOp, Verb } from "./shared/types";
9
- export { useClientState, StateManager, useStateWatcher, localStorageStore, idbStore, DiplomaticClient, libsodiumCrypto, btoh, htob, Verb, };
9
+ export { useClientState, useSyncOnResume, StateManager, useStateWatcher, localStorageStore, idbStore, DiplomaticClient, libsodiumCrypto, btoh, htob, Verb, };
10
10
  export type { IOp, };
package/dist/index.mjs CHANGED
@@ -21,6 +21,17 @@ function useClientState(client) {
21
21
  }, [client]);
22
22
  return state;
23
23
  }
24
+ function useSyncOnResume(client) {
25
+ useEffect(() => {
26
+ function handleOnline() {
27
+ client.sync();
28
+ }
29
+ window.addEventListener("online", handleOnline);
30
+ return () => {
31
+ window.removeEventListener("online", handleOnline);
32
+ };
33
+ }, [client]);
34
+ }
24
35
 
25
36
  // src/state.ts
26
37
  import { useEffect as useEffect2, useState as useState2 } from "react";
@@ -101,6 +112,9 @@ var seedKey = "seedHex";
101
112
  var hostURLKey = "hostURL";
102
113
  var hostIDKey = "hostID";
103
114
  var LocalStorageStore = class {
115
+ async wipe() {
116
+ localStorage.clear();
117
+ }
104
118
  async getSeed() {
105
119
  const storedSeed = localStorage.getItem(seedKey);
106
120
  if (storedSeed) {
@@ -300,6 +314,17 @@ function openDB(name, version, { blocked, upgrade, blocking, terminated } = {})
300
314
  });
301
315
  return openPromise;
302
316
  }
317
+ function deleteDB(name, { blocked } = {}) {
318
+ const request = indexedDB.deleteDatabase(name);
319
+ if (blocked) {
320
+ request.addEventListener("blocked", (event) => blocked(
321
+ // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
322
+ event.oldVersion,
323
+ event
324
+ ));
325
+ }
326
+ return wrap(request).then(() => void 0);
327
+ }
303
328
  var readMethods = ["get", "getKey", "getAll", "getAllKeys", "count"];
304
329
  var writeMethods = ["put", "add", "delete", "clear"];
305
330
  var cachedMethods = /* @__PURE__ */ new Map();
@@ -389,14 +414,24 @@ replaceTraps((oldTraps) => ({
389
414
  var dbPromise = openDB("client-store-db", 1, {
390
415
  upgrade(db) {
391
416
  db.createObjectStore("metaKV");
417
+ db.createObjectStore("uploadQueue", {
418
+ keyPath: "sha256"
419
+ });
420
+ db.createObjectStore("downloadQueue", {
421
+ keyPath: "path"
422
+ });
392
423
  }
393
424
  });
394
425
  var IDBStore = class {
395
426
  seed;
396
427
  hostURL;
397
428
  hostID;
429
+ async wipe() {
430
+ await deleteDB("client-store-db");
431
+ }
398
432
  async getSeed() {
399
- const hex = await (await dbPromise).get("metaKV", "seed");
433
+ const db = await dbPromise;
434
+ const hex = await db.get("metaKV", "seed");
400
435
  if (!hex) {
401
436
  return;
402
437
  }
@@ -405,45 +440,60 @@ var IDBStore = class {
405
440
  }
406
441
  async setSeed(seed) {
407
442
  const hex = btoh(seed);
408
- (await dbPromise).put("metaKV", hex, "seed");
443
+ const db = await dbPromise;
444
+ await db.put("metaKV", hex, "seed");
409
445
  this.seed = seed;
410
446
  }
411
447
  async getHostURL() {
412
- const url = await (await dbPromise).get("metaKV", "hostURL");
448
+ const db = await dbPromise;
449
+ const url = await db.get("metaKV", "hostURL");
413
450
  return url;
414
451
  }
415
452
  async setHostURL(url) {
416
- (await dbPromise).put("metaKV", url, "hostURL");
453
+ const db = await dbPromise;
454
+ db.put("metaKV", url, "hostURL");
417
455
  }
418
456
  async getHostID() {
419
- const id = await (await dbPromise).get("metaKV", "hostID");
457
+ const db = await dbPromise;
458
+ const id = await db.get("metaKV", "hostID");
420
459
  return id;
421
460
  }
422
461
  async setHostID(id) {
423
- (await dbPromise).put("metaKV", id, "hostID");
462
+ const db = await dbPromise;
463
+ await db.put("metaKV", id, "hostID");
424
464
  }
425
465
  uploadQueue = /* @__PURE__ */ new Map();
426
466
  enqueueUpload = async (sha256, cipherOp) => {
427
- this.uploadQueue.set(sha256, cipherOp);
467
+ const db = await dbPromise;
468
+ await db.put("uploadQueue", { sha256, cipherOp });
428
469
  };
429
470
  dequeueUpload = async (sha256) => {
430
- this.uploadQueue.delete(sha256);
471
+ const db = await dbPromise;
472
+ await db.delete("uploadQueue", sha256);
431
473
  };
432
474
  peekUpload = async (sha256) => {
433
- return this.uploadQueue.get(sha256);
475
+ const db = await dbPromise;
476
+ const row = await db.get("uploadQueue", sha256);
477
+ return row?.cipherOp;
434
478
  };
435
479
  listUploads = async () => {
436
- return Array.from(this.uploadQueue.keys());
480
+ const db = await dbPromise;
481
+ const sha256s = await db.getAllKeys("uploadQueue");
482
+ return sha256s;
437
483
  };
438
484
  downloadQueue = /* @__PURE__ */ new Set();
439
485
  enqueueDownload = async (path) => {
440
- this.downloadQueue.add(path);
486
+ const db = await dbPromise;
487
+ await db.put("downloadQueue", { path });
441
488
  };
442
489
  dequeueDownload = async (path) => {
443
- this.downloadQueue.delete(path);
490
+ const db = await dbPromise;
491
+ await db.delete("downloadQueue", path);
444
492
  };
445
493
  listDownloads = async () => {
446
- return Array.from(this.downloadQueue.keys());
494
+ const db = await dbPromise;
495
+ const paths = await db.getAllKeys("downloadQueue");
496
+ return paths;
447
497
  };
448
498
  };
449
499
  var idbStore = new IDBStore();
@@ -8770,7 +8820,6 @@ var DiplomaticClient = class {
8770
8820
  };
8771
8821
  };
8772
8822
  async init(params) {
8773
- await this.store.init?.();
8774
8823
  if (params.seed) {
8775
8824
  const bytes = typeof params.seed === "string" ? htob(params.seed) : params.seed;
8776
8825
  await this.store.setSeed(bytes);
@@ -8926,5 +8975,6 @@ export {
8926
8975
  crypto_default as libsodiumCrypto,
8927
8976
  localStorageStore,
8928
8977
  useClientState,
8929
- useStateWatcher
8978
+ useStateWatcher,
8979
+ useSyncOnResume
8930
8980
  };
@@ -1,5 +1,6 @@
1
1
  import type { IClientStateStore } from "./types";
2
2
  declare class LocalStorageStore implements IClientStateStore {
3
+ wipe(): Promise<void>;
3
4
  getSeed(): Promise<Uint8Array | undefined>;
4
5
  setSeed(seed: Uint8Array): Promise<void>;
5
6
  getHostURL(): Promise<string | undefined>;
@@ -3,6 +3,7 @@ declare class MemoryStore implements IClientStateStore {
3
3
  seed?: Uint8Array;
4
4
  hostURL?: string;
5
5
  hostID?: string;
6
+ wipe(): Promise<void>;
6
7
  getSeed(): Promise<Uint8Array | undefined>;
7
8
  setSeed(seed: Uint8Array): Promise<void>;
8
9
  getHostURL(): Promise<string | undefined>;
package/dist/types.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import type { IOp } from "./shared/types";
2
2
  export interface IClientStateStore {
3
- init?: () => Promise<void>;
4
3
  getSeed: () => Promise<Uint8Array | undefined>;
5
4
  setSeed: (seed: Uint8Array) => Promise<void>;
6
5
  getHostURL: () => Promise<string | undefined>;
7
6
  setHostURL: (url: string) => Promise<void>;
8
7
  getHostID: () => Promise<string | undefined>;
9
8
  setHostID: (id: string) => Promise<void>;
9
+ wipe: () => Promise<void>;
10
10
  enqueueUpload: (sha256: string, cipherOp: Uint8Array) => Promise<void>;
11
11
  dequeueUpload: (sha256: string) => Promise<void>;
12
12
  peekUpload: (sha256: string) => Promise<Uint8Array | undefined>;
@@ -1,3 +1,4 @@
1
1
  import type { DiplomaticClientState } from "./types";
2
2
  import type DiplomaticClient from "./client";
3
3
  export declare function useClientState(client: DiplomaticClient): DiplomaticClientState;
4
+ export declare function useSyncOnResume(client: DiplomaticClient): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interncom/diplomatic",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",