@interncom/diplomatic 0.0.26 → 0.0.28

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,5 @@
1
- import { useClientState, useSyncOnResume } from "./useClient";
2
- import { StateManager, useStateWatcher } from './state';
1
+ import { useClientState, useSyncOnResume } from "./react/useClient";
2
+ import { StateManager, opMapApplier } from './state';
3
3
  import { localStorageStore } from "./localStorageStore";
4
4
  import { idbStore } from "./idbStore";
5
5
  import type { IDiplomaticClientState } from "./types";
@@ -7,5 +7,8 @@ import DiplomaticClient from "./client";
7
7
  import libsodiumCrypto from "./crypto";
8
8
  import { btoh, htob } from "./shared/lib";
9
9
  import { type IOp, Verb } from "./shared/types";
10
- export { useClientState, useSyncOnResume, StateManager, useStateWatcher, localStorageStore, idbStore, DiplomaticClient, libsodiumCrypto, btoh, htob, Verb, };
10
+ import useStateWatcher from "./react/useStateWatcher";
11
+ import InitSeedView from "./react/initSeedView";
12
+ import ClientStatusBar from "./react/statusBar";
13
+ export { useClientState, useSyncOnResume, StateManager, useStateWatcher, opMapApplier, localStorageStore, idbStore, DiplomaticClient, libsodiumCrypto, btoh, htob, Verb, InitSeedView, ClientStatusBar, };
11
14
  export type { IOp, IDiplomaticClientState, };
package/dist/index.mjs CHANGED
@@ -10,7 +10,7 @@ var __export = (target, all) => {
10
10
  __defProp(target, name, { get: all[name], enumerable: true });
11
11
  };
12
12
 
13
- // src/useClient.ts
13
+ // src/react/useClient.ts
14
14
  import { useState, useEffect } from "react";
15
15
  function useClientState(client) {
16
16
  const [state, setState] = useState();
@@ -42,9 +42,6 @@ function useSyncOnResume(client) {
42
42
  }, [client]);
43
43
  }
44
44
 
45
- // src/state.ts
46
- import { useEffect as useEffect2, useState as useState2 } from "react";
47
-
48
45
  // src/eventEmitter.ts
49
46
  var EventEmitter = class {
50
47
  events = {};
@@ -87,19 +84,13 @@ var StateManager = class {
87
84
  this.emitter.off(opType, listener);
88
85
  };
89
86
  };
90
- function useStateWatcher(mgr, opType, callback) {
91
- const [val, setVal] = useState2(callback());
92
- useEffect2(() => {
93
- function update() {
94
- const newVal = callback();
95
- setVal(newVal);
87
+ function opMapApplier(appliers) {
88
+ return async (op) => {
89
+ const applier = appliers[op.type];
90
+ if (applier?.check(op)) {
91
+ await applier.apply(op);
96
92
  }
97
- mgr.on(opType, update);
98
- return () => {
99
- mgr.off(opType, update);
100
- };
101
- }, [mgr, opType, callback]);
102
- return val;
93
+ };
103
94
  }
104
95
 
105
96
  // ../shared/lib.ts
@@ -9010,8 +9001,98 @@ var DiplomaticClient = class {
9010
9001
  return this.apply(op);
9011
9002
  }
9012
9003
  };
9004
+
9005
+ // src/react/useStateWatcher.ts
9006
+ import { useState as useState2, useEffect as useEffect2 } from "react";
9007
+ function useStateWatcher(mgr, opType, callback) {
9008
+ const [val, setVal] = useState2(callback());
9009
+ useEffect2(() => {
9010
+ function update() {
9011
+ const newVal = callback();
9012
+ setVal(newVal);
9013
+ }
9014
+ mgr.on(opType, update);
9015
+ return () => {
9016
+ mgr.off(opType, update);
9017
+ };
9018
+ }, [mgr, opType, callback]);
9019
+ return val;
9020
+ }
9021
+
9022
+ // src/react/initSeedView.tsx
9023
+ import { useState as useState3, useCallback } from "react";
9024
+ import { jsx, jsxs } from "react/jsx-runtime";
9025
+ function InitSeedView({ client, path }) {
9026
+ const [seedString, setSeedString] = useState3("");
9027
+ const [username, setUsername] = useState3("");
9028
+ const genSeed = useCallback(async () => {
9029
+ const seed = await crypto_default.gen256BitSecureRandomSeed();
9030
+ const seedStr = btoh(seed);
9031
+ setSeedString(seedStr);
9032
+ }, []);
9033
+ const handleInitFormSubmit = useCallback(async (e) => {
9034
+ e.preventDefault();
9035
+ const seed = htob(seedString);
9036
+ await client.setSeed(seed);
9037
+ window.location.replace(path);
9038
+ }, [seedString, client, path]);
9039
+ return /* @__PURE__ */ jsxs("div", { children: [
9040
+ /* @__PURE__ */ jsx("h1", { children: "Initialize" }),
9041
+ /* @__PURE__ */ jsxs("form", { id: "seed", action: "/", method: "get", onSubmit: handleInitFormSubmit, style: { display: "flex", flexDirection: "column" }, children: [
9042
+ /* @__PURE__ */ jsx("button", { type: "button", onClick: genSeed, children: "Generate" }),
9043
+ /* @__PURE__ */ jsx("input", { name: "password", type: "password", autoComplete: "new-password", placeholder: "Push generate to pick a seed", value: seedString, onChange: (e) => setSeedString(e.target.value) }),
9044
+ /* @__PURE__ */ jsx("input", { name: "username", type: "text", autoComplete: "username", placeholder: "Choose an account name (not shared)", onChange: (e) => setUsername(e.target.value), required: true }),
9045
+ /* @__PURE__ */ jsx("button", { type: "submit", disabled: !seedString || !username, children: "INIT" })
9046
+ ] })
9047
+ ] });
9048
+ }
9049
+
9050
+ // src/react/statusBar.tsx
9051
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
9052
+ function GlowingDot({ on }) {
9053
+ const color = on ? "#01FF70" : "#AAAAAA";
9054
+ const size = 6;
9055
+ return /* @__PURE__ */ jsx2("div", { style: { width: size, height: size, borderRadius: "50%", boxShadow: `0 0 4px ${color}`, backgroundColor: color } });
9056
+ }
9057
+ function DotLabel({ on, label }) {
9058
+ return /* @__PURE__ */ jsxs2("div", { style: { display: "flex", flexDirection: "row", marginRight: 16, alignItems: "center", gap: 6 }, children: [
9059
+ /* @__PURE__ */ jsx2(GlowingDot, { on }),
9060
+ label
9061
+ ] });
9062
+ }
9063
+ function ClientStatusBar({ state }) {
9064
+ return /* @__PURE__ */ jsxs2(
9065
+ "div",
9066
+ {
9067
+ style: {
9068
+ marginTop: 16,
9069
+ borderRadius: 8,
9070
+ padding: "8px 16px",
9071
+ border: "1px solid grey",
9072
+ display: "flex",
9073
+ flexDirection: "row",
9074
+ justifyContent: "space-around"
9075
+ },
9076
+ children: [
9077
+ /* @__PURE__ */ jsx2(DotLabel, { on: state.hasSeed, label: "SEED" }),
9078
+ /* @__PURE__ */ jsx2(DotLabel, { on: state.hasHost, label: "HOST" }),
9079
+ /* @__PURE__ */ jsx2(DotLabel, { on: state.connected, label: "LINK" }),
9080
+ /* @__PURE__ */ jsxs2("div", { style: { marginRight: 16 }, children: [
9081
+ "\u21D1 ",
9082
+ state.numUploads
9083
+ ] }),
9084
+ /* @__PURE__ */ jsxs2("div", { children: [
9085
+ "\u21D3 ",
9086
+ state.numDownloads
9087
+ ] })
9088
+ ]
9089
+ }
9090
+ );
9091
+ }
9013
9092
  export {
9093
+ ClientStatusBar,
9014
9094
  DiplomaticClient,
9095
+ InitSeedView,
9015
9096
  StateManager,
9016
9097
  Verb,
9017
9098
  btoh,
@@ -9019,6 +9100,7 @@ export {
9019
9100
  idbStore,
9020
9101
  crypto_default as libsodiumCrypto,
9021
9102
  localStorageStore,
9103
+ opMapApplier,
9022
9104
  useClientState,
9023
9105
  useStateWatcher,
9024
9106
  useSyncOnResume
@@ -0,0 +1,7 @@
1
+ import type DiplomaticClient from "../client";
2
+ interface IProps {
3
+ client: DiplomaticClient;
4
+ path: string;
5
+ }
6
+ export default function InitSeedView({ client, path }: IProps): import("react/jsx-runtime").JSX.Element;
7
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { IDiplomaticClientState } from "../types";
2
+ interface IProps {
3
+ state: IDiplomaticClientState;
4
+ }
5
+ export default function ClientStatusBar({ state }: IProps): import("react/jsx-runtime").JSX.Element;
6
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { IDiplomaticClientState } from "../types";
2
+ import type DiplomaticClient from "../client";
3
+ export declare function useClientState(client: DiplomaticClient): IDiplomaticClientState | undefined;
4
+ export declare function useSyncOnResume(client: DiplomaticClient): void;
@@ -0,0 +1,2 @@
1
+ import type { StateManager } from "../state";
2
+ export default function useStateWatcher<T>(mgr: StateManager, opType: string, callback: () => T): T;
package/dist/state.d.ts CHANGED
@@ -4,10 +4,18 @@ import type { IOp } from "./shared/types";
4
4
  export declare class StateManager {
5
5
  emitter: EventEmitter;
6
6
  applier: Applier;
7
- clear: () => void;
7
+ clear: () => Promise<void>;
8
8
  constructor(applier: Applier, clear: () => Promise<void>);
9
9
  apply: (op: IOp) => Promise<void>;
10
10
  on: (opType: string, listener: () => void) => void;
11
11
  off: (opType: string, listener: () => void) => void;
12
12
  }
13
- export declare function useStateWatcher<T>(mgr: StateManager, opType: string, callback: () => T): T;
13
+ interface IApplier<T extends IOp> {
14
+ check: (op: IOp) => op is T;
15
+ apply: (op: T) => Promise<void>;
16
+ }
17
+ type Appliers<M> = {
18
+ [K in keyof M]: M[K] extends IOp ? IApplier<M[K]> : never;
19
+ };
20
+ export declare function opMapApplier<M>(appliers: Appliers<M>): (op: IOp) => Promise<void>;
21
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interncom/diplomatic",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",