@interncom/diplomatic 0.0.6 → 0.0.7

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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import useClient from "./useClient";
1
+ import { useClientState } from "./useClient";
2
2
  import { StateManager, useStateWatcher } from './state';
3
3
  import { localStorageStore } from "./localStorageStore";
4
4
  import DiplomaticClient from "./client";
5
- export { useClient, StateManager, useStateWatcher, localStorageStore, DiplomaticClient, };
5
+ import libsodiumCrypto from "./crypto";
6
+ import { btoh, htob } from "./shared/lib";
7
+ export { useClientState, StateManager, useStateWatcher, localStorageStore, DiplomaticClient, libsodiumCrypto, btoh, htob, };
package/dist/index.mjs CHANGED
@@ -11,7 +11,144 @@ var __export = (target, all) => {
11
11
  };
12
12
 
13
13
  // src/useClient.ts
14
- import { useState, useEffect as useEffect2, useCallback, useMemo } from "react";
14
+ import { useState, useEffect } from "react";
15
+ var initialState = "loading";
16
+ function useClientState(client) {
17
+ const [state, setState] = useState(initialState);
18
+ useEffect(() => {
19
+ setState(client.state);
20
+ client.listener = setState;
21
+ }, [client]);
22
+ return state;
23
+ }
24
+
25
+ // src/state.ts
26
+ import { useEffect as useEffect2, useState as useState2 } from "react";
27
+
28
+ // src/eventEmitter.ts
29
+ var EventEmitter = class {
30
+ events = {};
31
+ on(event, listener) {
32
+ if (!this.events[event]) {
33
+ this.events[event] = [];
34
+ }
35
+ this.events[event].push(listener);
36
+ }
37
+ off(event, listener) {
38
+ if (!this.events[event]) return;
39
+ this.events[event] = this.events[event].filter((l) => l !== listener);
40
+ }
41
+ // Method to emit an event
42
+ emit(event) {
43
+ if (!this.events[event]) return;
44
+ for (const listener of this.events[event]) {
45
+ listener();
46
+ }
47
+ }
48
+ };
49
+
50
+ // src/state.ts
51
+ var StateManager = class {
52
+ emitter = new EventEmitter();
53
+ applier;
54
+ constructor(applier) {
55
+ this.applier = applier;
56
+ }
57
+ apply = async (op) => {
58
+ await this.applier(op);
59
+ this.emitter.emit(op.type);
60
+ };
61
+ on = (opType, listener) => {
62
+ this.emitter.on(opType, listener);
63
+ };
64
+ off = (opType, listener) => {
65
+ this.emitter.off(opType, listener);
66
+ };
67
+ };
68
+ function useStateWatcher(mgr, opType, callback) {
69
+ const [val, setVal] = useState2(callback());
70
+ useEffect2(() => {
71
+ function update() {
72
+ const newVal = callback();
73
+ setVal(newVal);
74
+ }
75
+ mgr.on(opType, update);
76
+ return () => {
77
+ mgr.off(opType, update);
78
+ };
79
+ }, [mgr, opType, callback]);
80
+ return val;
81
+ }
82
+
83
+ // ../shared/lib.ts
84
+ function btoh(bytes) {
85
+ const hex = Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
86
+ return hex;
87
+ }
88
+ function htob(hex) {
89
+ if (hex.length % 2 !== 0) {
90
+ throw new Error("Invalid hex string");
91
+ }
92
+ const bytes = new Uint8Array(hex.length / 2);
93
+ for (let i = 0; i < hex.length; i += 2) {
94
+ bytes[i / 2] = Number.parseInt(hex.substr(i, 2), 16);
95
+ }
96
+ return bytes;
97
+ }
98
+
99
+ // src/localStorageStore.ts
100
+ var seedKey = "seedHex";
101
+ var hostURLKey = "hostURL";
102
+ var hostIDKey = "hostID";
103
+ var LocalStorageStore = class {
104
+ async getSeed() {
105
+ const storedSeed = localStorage.getItem(seedKey);
106
+ if (storedSeed) {
107
+ const seed = htob(storedSeed);
108
+ return seed;
109
+ }
110
+ }
111
+ async setSeed(seed) {
112
+ const seedString = btoh(seed);
113
+ localStorage.setItem(seedKey, seedString);
114
+ }
115
+ async getHostURL() {
116
+ return localStorage.getItem(hostURLKey) ?? void 0;
117
+ }
118
+ async setHostURL(url) {
119
+ return localStorage.setItem(hostURLKey, url);
120
+ }
121
+ async getHostID() {
122
+ return localStorage.getItem(hostIDKey) ?? void 0;
123
+ }
124
+ async setHostID(id) {
125
+ return localStorage.setItem(hostIDKey, id);
126
+ }
127
+ uploadQueue = /* @__PURE__ */ new Map();
128
+ enqueueUpload = async (sha256, cipherOp) => {
129
+ const hex = btoh(sha256);
130
+ this.uploadQueue.set(hex, cipherOp);
131
+ };
132
+ dequeueUpload = async (sha256) => {
133
+ const hex = btoh(sha256);
134
+ this.uploadQueue.delete(hex);
135
+ };
136
+ peekUpload = async (sha256) => {
137
+ const hex = btoh(sha256);
138
+ return this.uploadQueue.get(hex);
139
+ };
140
+ async listUploadQueue() {
141
+ return Array.from(this.uploadQueue.keys());
142
+ }
143
+ downloadQueue = /* @__PURE__ */ new Set();
144
+ enqueueDownload = async (path) => {
145
+ this.downloadQueue.add(path);
146
+ };
147
+ dequeueDownload = async (path) => {
148
+ this.downloadQueue.delete(path);
149
+ };
150
+ };
151
+ var localStorageStore = new LocalStorageStore();
15
152
 
16
153
  // node_modules/@msgpack/msgpack/dist.es5+esm/utils/utf8.mjs
17
154
  function utf8Count(str) {
@@ -1874,22 +2011,6 @@ function decodeAsync(streamLike, options) {
1874
2011
  });
1875
2012
  }
1876
2013
 
1877
- // ../shared/lib.ts
1878
- function btoh(bytes) {
1879
- const hex = Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
1880
- return hex;
1881
- }
1882
- function htob(hex) {
1883
- if (hex.length % 2 !== 0) {
1884
- throw new Error("Invalid hex string");
1885
- }
1886
- const bytes = new Uint8Array(hex.length / 2);
1887
- for (let i = 0; i < hex.length; i += 2) {
1888
- bytes[i / 2] = Number.parseInt(hex.substr(i, 2), 16);
1889
- }
1890
- return bytes;
1891
- }
1892
-
1893
2014
  // ../deno/vendor/raw.githubusercontent.com/interncom/libsodium.js/esm/dist/modules/libsodium-esm-wrappers.js
1894
2015
  var libsodium_esm_wrappers_exports = {};
1895
2016
  __export(libsodium_esm_wrappers_exports, {
@@ -8312,6 +8433,7 @@ var DiplomaticClient = class {
8312
8433
  constructor(params) {
8313
8434
  this.store = params.store;
8314
8435
  this.applier = params.stateManager.apply;
8436
+ console.log("constructing");
8315
8437
  this.init(params);
8316
8438
  }
8317
8439
  websocket;
@@ -8478,163 +8600,13 @@ var DiplomaticClient = class {
8478
8600
  return this.apply(op);
8479
8601
  }
8480
8602
  };
8481
-
8482
- // src/sync.ts
8483
- import { useEffect } from "react";
8484
- function usePollingSync(client, intervalMillis) {
8485
- useEffect(() => {
8486
- if (intervalMillis <= 0) {
8487
- return;
8488
- }
8489
- async function poll() {
8490
- console.log("Polling");
8491
- await client.processDeltas();
8492
- await client.pushQueuedOps();
8493
- }
8494
- poll();
8495
- const handle = setInterval(poll, intervalMillis);
8496
- return () => {
8497
- clearInterval(handle);
8498
- };
8499
- }, [client, intervalMillis]);
8500
- }
8501
-
8502
- // src/localStorageStore.ts
8503
- var seedKey = "seedHex";
8504
- var hostURLKey = "hostURL";
8505
- var hostIDKey = "hostID";
8506
- var LocalStorageStore = class {
8507
- async getSeed() {
8508
- const storedSeed = localStorage.getItem(seedKey);
8509
- if (storedSeed) {
8510
- const seed = htob(storedSeed);
8511
- return seed;
8512
- }
8513
- }
8514
- async setSeed(seed) {
8515
- const seedString = btoh(seed);
8516
- localStorage.setItem(seedKey, seedString);
8517
- }
8518
- async getHostURL() {
8519
- return localStorage.getItem(hostURLKey) ?? void 0;
8520
- }
8521
- async setHostURL(url) {
8522
- return localStorage.setItem(hostURLKey, url);
8523
- }
8524
- async getHostID() {
8525
- return localStorage.getItem(hostIDKey) ?? void 0;
8526
- }
8527
- async setHostID(id) {
8528
- return localStorage.setItem(hostIDKey, id);
8529
- }
8530
- uploadQueue = /* @__PURE__ */ new Map();
8531
- enqueueUpload = async (sha256, cipherOp) => {
8532
- const hex = btoh(sha256);
8533
- this.uploadQueue.set(hex, cipherOp);
8534
- };
8535
- dequeueUpload = async (sha256) => {
8536
- const hex = btoh(sha256);
8537
- this.uploadQueue.delete(hex);
8538
- };
8539
- peekUpload = async (sha256) => {
8540
- const hex = btoh(sha256);
8541
- return this.uploadQueue.get(hex);
8542
- };
8543
- async listUploadQueue() {
8544
- return Array.from(this.uploadQueue.keys());
8545
- }
8546
- downloadQueue = /* @__PURE__ */ new Set();
8547
- enqueueDownload = async (path) => {
8548
- this.downloadQueue.add(path);
8549
- };
8550
- dequeueDownload = async (path) => {
8551
- this.downloadQueue.delete(path);
8552
- };
8553
- };
8554
- var localStorageStore = new LocalStorageStore();
8555
-
8556
- // src/useClient.ts
8557
- var initialState = "loading";
8558
- function useClient(params) {
8559
- const [state, setState] = useState(initialState);
8560
- console.log("rendering useClient");
8561
- const fullParams = useMemo(
8562
- () => ({ store: localStorageStore, ...params }),
8563
- [params]
8564
- );
8565
- const [client, setClient] = useState(new DiplomaticClient(fullParams));
8566
- useEffect2(() => {
8567
- client.listener = setState;
8568
- }, [client]);
8569
- const reset = useCallback(() => {
8570
- setClient(new DiplomaticClient(fullParams));
8571
- setState(initialState);
8572
- }, [fullParams]);
8573
- usePollingSync(client, params.refreshInterval ?? 1e3);
8574
- return [client, state, reset];
8575
- }
8576
-
8577
- // src/state.ts
8578
- import { useEffect as useEffect3, useState as useState2 } from "react";
8579
-
8580
- // src/eventEmitter.ts
8581
- var EventEmitter = class {
8582
- events = {};
8583
- on(event, listener) {
8584
- if (!this.events[event]) {
8585
- this.events[event] = [];
8586
- }
8587
- this.events[event].push(listener);
8588
- }
8589
- off(event, listener) {
8590
- if (!this.events[event]) return;
8591
- this.events[event] = this.events[event].filter((l) => l !== listener);
8592
- }
8593
- // Method to emit an event
8594
- emit(event) {
8595
- if (!this.events[event]) return;
8596
- for (const listener of this.events[event]) {
8597
- listener();
8598
- }
8599
- }
8600
- };
8601
-
8602
- // src/state.ts
8603
- var StateManager = class {
8604
- emitter = new EventEmitter();
8605
- applier;
8606
- constructor(applier) {
8607
- this.applier = applier;
8608
- }
8609
- apply = async (op) => {
8610
- await this.applier(op);
8611
- this.emitter.emit(op.type);
8612
- };
8613
- on = (opType, listener) => {
8614
- this.emitter.on(opType, listener);
8615
- };
8616
- off = (opType, listener) => {
8617
- this.emitter.off(opType, listener);
8618
- };
8619
- };
8620
- function useStateWatcher(mgr, opType, callback) {
8621
- const [val, setVal] = useState2(callback());
8622
- useEffect3(() => {
8623
- function update() {
8624
- const newVal = callback();
8625
- setVal(newVal);
8626
- }
8627
- mgr.on(opType, update);
8628
- return () => {
8629
- mgr.off(opType, update);
8630
- };
8631
- }, [mgr, opType, callback]);
8632
- return val;
8633
- }
8634
8603
  export {
8635
8604
  DiplomaticClient,
8636
8605
  StateManager,
8606
+ btoh,
8607
+ htob,
8608
+ crypto_default as libsodiumCrypto,
8637
8609
  localStorageStore,
8638
- useClient,
8610
+ useClientState,
8639
8611
  useStateWatcher
8640
8612
  };
@@ -1,8 +1,3 @@
1
- import DiplomaticClient, { type IDiplomaticClientParams } from "./client";
2
- import type { DiplomaticClientState, IClientStateStore } from "./types";
3
- interface IClientHookParams extends Omit<IDiplomaticClientParams, "store"> {
4
- refreshInterval?: number;
5
- store?: IClientStateStore;
6
- }
7
- export default function useClient(params: IClientHookParams): [DiplomaticClient, DiplomaticClientState, () => void];
8
- export {};
1
+ import DiplomaticClient from "./client";
2
+ import type { DiplomaticClientState } from "./types";
3
+ export declare function useClientState(client: DiplomaticClient): DiplomaticClientState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interncom/diplomatic",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",