@interncom/diplomatic 0.0.5 → 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;
@@ -8320,7 +8442,11 @@ var DiplomaticClient = class {
8320
8442
  return;
8321
8443
  }
8322
8444
  const url = new URL(hostURL);
8323
- url.protocol = "ws";
8445
+ if (window.location.protocol === "https:") {
8446
+ url.protocol = "wss";
8447
+ } else {
8448
+ url.protocol = "ws";
8449
+ }
8324
8450
  const keyHex = btoh(this.hostKeyPair?.publicKey);
8325
8451
  url.searchParams.set("key", keyHex);
8326
8452
  this.websocket = new WebSocket(url);
@@ -8474,163 +8600,13 @@ var DiplomaticClient = class {
8474
8600
  return this.apply(op);
8475
8601
  }
8476
8602
  };
8477
-
8478
- // src/sync.ts
8479
- import { useEffect } from "react";
8480
- function usePollingSync(client, intervalMillis) {
8481
- useEffect(() => {
8482
- if (intervalMillis <= 0) {
8483
- return;
8484
- }
8485
- async function poll() {
8486
- console.log("Polling");
8487
- await client.processDeltas();
8488
- await client.pushQueuedOps();
8489
- }
8490
- poll();
8491
- const handle = setInterval(poll, intervalMillis);
8492
- return () => {
8493
- clearInterval(handle);
8494
- };
8495
- }, [client, intervalMillis]);
8496
- }
8497
-
8498
- // src/localStorageStore.ts
8499
- var seedKey = "seedHex";
8500
- var hostURLKey = "hostURL";
8501
- var hostIDKey = "hostID";
8502
- var LocalStorageStore = class {
8503
- async getSeed() {
8504
- const storedSeed = localStorage.getItem(seedKey);
8505
- if (storedSeed) {
8506
- const seed = htob(storedSeed);
8507
- return seed;
8508
- }
8509
- }
8510
- async setSeed(seed) {
8511
- const seedString = btoh(seed);
8512
- localStorage.setItem(seedKey, seedString);
8513
- }
8514
- async getHostURL() {
8515
- return localStorage.getItem(hostURLKey) ?? void 0;
8516
- }
8517
- async setHostURL(url) {
8518
- return localStorage.setItem(hostURLKey, url);
8519
- }
8520
- async getHostID() {
8521
- return localStorage.getItem(hostIDKey) ?? void 0;
8522
- }
8523
- async setHostID(id) {
8524
- return localStorage.setItem(hostIDKey, id);
8525
- }
8526
- uploadQueue = /* @__PURE__ */ new Map();
8527
- enqueueUpload = async (sha256, cipherOp) => {
8528
- const hex = btoh(sha256);
8529
- this.uploadQueue.set(hex, cipherOp);
8530
- };
8531
- dequeueUpload = async (sha256) => {
8532
- const hex = btoh(sha256);
8533
- this.uploadQueue.delete(hex);
8534
- };
8535
- peekUpload = async (sha256) => {
8536
- const hex = btoh(sha256);
8537
- return this.uploadQueue.get(hex);
8538
- };
8539
- async listUploadQueue() {
8540
- return Array.from(this.uploadQueue.keys());
8541
- }
8542
- downloadQueue = /* @__PURE__ */ new Set();
8543
- enqueueDownload = async (path) => {
8544
- this.downloadQueue.add(path);
8545
- };
8546
- dequeueDownload = async (path) => {
8547
- this.downloadQueue.delete(path);
8548
- };
8549
- };
8550
- var localStorageStore = new LocalStorageStore();
8551
-
8552
- // src/useClient.ts
8553
- var initialState = "loading";
8554
- function useClient(params) {
8555
- const [state, setState] = useState(initialState);
8556
- console.log("rendering useClient");
8557
- const fullParams = useMemo(
8558
- () => ({ store: localStorageStore, ...params }),
8559
- [params]
8560
- );
8561
- const [client, setClient] = useState(new DiplomaticClient(fullParams));
8562
- useEffect2(() => {
8563
- client.listener = setState;
8564
- }, [client]);
8565
- const reset = useCallback(() => {
8566
- setClient(new DiplomaticClient(fullParams));
8567
- setState(initialState);
8568
- }, [fullParams]);
8569
- usePollingSync(client, params.refreshInterval ?? 1e3);
8570
- return [client, state, reset];
8571
- }
8572
-
8573
- // src/state.ts
8574
- import { useEffect as useEffect3, useState as useState2 } from "react";
8575
-
8576
- // src/eventEmitter.ts
8577
- var EventEmitter = class {
8578
- events = {};
8579
- on(event, listener) {
8580
- if (!this.events[event]) {
8581
- this.events[event] = [];
8582
- }
8583
- this.events[event].push(listener);
8584
- }
8585
- off(event, listener) {
8586
- if (!this.events[event]) return;
8587
- this.events[event] = this.events[event].filter((l) => l !== listener);
8588
- }
8589
- // Method to emit an event
8590
- emit(event) {
8591
- if (!this.events[event]) return;
8592
- for (const listener of this.events[event]) {
8593
- listener();
8594
- }
8595
- }
8596
- };
8597
-
8598
- // src/state.ts
8599
- var StateManager = class {
8600
- emitter = new EventEmitter();
8601
- applier;
8602
- constructor(applier) {
8603
- this.applier = applier;
8604
- }
8605
- apply = async (op) => {
8606
- await this.applier(op);
8607
- this.emitter.emit(op.type);
8608
- };
8609
- on = (opType, listener) => {
8610
- this.emitter.on(opType, listener);
8611
- };
8612
- off = (opType, listener) => {
8613
- this.emitter.off(opType, listener);
8614
- };
8615
- };
8616
- function useStateWatcher(mgr, opType, callback) {
8617
- const [val, setVal] = useState2(callback());
8618
- useEffect3(() => {
8619
- function update() {
8620
- const newVal = callback();
8621
- setVal(newVal);
8622
- }
8623
- mgr.on(opType, update);
8624
- return () => {
8625
- mgr.off(opType, update);
8626
- };
8627
- }, [mgr, opType, callback]);
8628
- return val;
8629
- }
8630
8603
  export {
8631
8604
  DiplomaticClient,
8632
8605
  StateManager,
8606
+ btoh,
8607
+ htob,
8608
+ crypto_default as libsodiumCrypto,
8633
8609
  localStorageStore,
8634
- useClient,
8610
+ useClientState,
8635
8611
  useStateWatcher
8636
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.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",