@interncom/diplomatic 0.0.75 → 0.1.0

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.
Files changed (82) hide show
  1. package/README.md +12 -6
  2. package/dist/client.d.ts +43 -56
  3. package/dist/entdb/entdb.d.ts +36 -0
  4. package/dist/entdb/idb.d.ts +27 -0
  5. package/dist/entdb/memory.d.ts +30 -0
  6. package/dist/events.d.ts +18 -0
  7. package/dist/exim.d.ts +0 -0
  8. package/dist/index.d.ts +22 -13
  9. package/dist/index.mjs +6408 -6979
  10. package/dist/lib.d.ts +2 -0
  11. package/dist/react/useClient.d.ts +5 -4
  12. package/dist/react/useEntities.d.ts +0 -0
  13. package/dist/react/useStateWatcher.d.ts +3 -3
  14. package/dist/shared/api/peek.d.ts +3 -0
  15. package/dist/shared/api/pull.d.ts +3 -0
  16. package/dist/shared/api/push.d.ts +4 -0
  17. package/dist/shared/api/user.d.ts +2 -0
  18. package/dist/shared/auth.d.ts +8 -0
  19. package/dist/shared/bag.d.ts +16 -0
  20. package/dist/shared/binary.d.ts +8 -0
  21. package/dist/shared/client.d.ts +20 -9
  22. package/dist/shared/clock.d.ts +13 -0
  23. package/dist/shared/codec.d.ts +43 -0
  24. package/dist/shared/codecs/authTimestamp.d.ts +8 -0
  25. package/dist/shared/codecs/bag.d.ts +3 -0
  26. package/dist/shared/codecs/eid.d.ts +10 -0
  27. package/dist/shared/codecs/file.d.ts +8 -0
  28. package/dist/shared/codecs/fileHead.d.ts +10 -0
  29. package/dist/shared/codecs/fileIndexItem.d.ts +8 -0
  30. package/dist/shared/codecs/messageHead.d.ts +16 -0
  31. package/dist/shared/codecs/notifItem.d.ts +7 -0
  32. package/dist/shared/codecs/peekItem.d.ts +6 -0
  33. package/dist/shared/codecs/peekItemHead.d.ts +7 -0
  34. package/dist/shared/codecs/pullItem.d.ts +6 -0
  35. package/dist/shared/codecs/pushItem.d.ts +11 -0
  36. package/dist/shared/codecs/respHead.d.ts +10 -0
  37. package/dist/shared/codecs/usageQuota.d.ts +6 -0
  38. package/dist/shared/consts.d.ts +50 -0
  39. package/dist/shared/crypto/libsodium.d.ts +5 -5
  40. package/dist/shared/enclave.d.ts +8 -0
  41. package/dist/shared/endpoint.d.ts +23 -0
  42. package/dist/shared/exim.d.ts +12 -0
  43. package/dist/shared/http/listener.d.ts +11 -0
  44. package/dist/shared/http/server.d.ts +11 -0
  45. package/dist/shared/http.d.ts +81 -0
  46. package/dist/shared/lpc/listener.d.ts +16 -0
  47. package/dist/shared/lpc/pusher.d.ts +9 -0
  48. package/dist/shared/lpc/server.d.ts +19 -0
  49. package/dist/shared/message.d.ts +30 -0
  50. package/dist/shared/storage/memory.d.ts +10 -0
  51. package/dist/shared/types.d.ts +127 -49
  52. package/dist/shared/valstat.d.ts +6 -0
  53. package/dist/state.d.ts +12 -17
  54. package/dist/stores/idb/dnlds.d.ts +10 -0
  55. package/dist/stores/idb/hosts.d.ts +15 -0
  56. package/dist/stores/idb/msgs.d.ts +19 -0
  57. package/dist/stores/idb/seed.d.ts +11 -0
  58. package/dist/stores/idb/store.d.ts +23 -0
  59. package/dist/stores/idb/uplds.d.ts +11 -0
  60. package/dist/stores/memory/dnlds.d.ts +9 -0
  61. package/dist/stores/memory/hosts.d.ts +13 -0
  62. package/dist/stores/memory/msgs.d.ts +19 -0
  63. package/dist/stores/memory/seed.d.ts +9 -0
  64. package/dist/stores/memory/store.d.ts +16 -0
  65. package/dist/stores/memory/uplds.d.ts +10 -0
  66. package/dist/sync.d.ts +32 -2
  67. package/dist/types.d.ts +113 -29
  68. package/package.json +9 -6
  69. package/dist/api.d.ts +0 -3
  70. package/dist/entityDB.d.ts +0 -34
  71. package/dist/eventEmitter.d.ts +0 -8
  72. package/dist/idbStore.d.ts +0 -38
  73. package/dist/localStorageStore.d.ts +0 -36
  74. package/dist/memoryStore.d.ts +0 -40
  75. package/dist/queue.d.ts +0 -10
  76. package/dist/react/initSeedView.d.ts +0 -7
  77. package/dist/react/statusBar.d.ts +0 -7
  78. package/dist/shared/lib.d.ts +0 -2
  79. package/dist/shared/ops.d.ts +0 -4
  80. package/dist/shared/server.d.ts +0 -12
  81. package/dist/typedEventEmitter.d.ts +0 -8
  82. package/dist/useClient.d.ts +0 -4
package/README.md CHANGED
@@ -5,14 +5,19 @@ Secure sync layer for web apps.
5
5
  ## Usage
6
6
 
7
7
  ```tsx
8
- import { StateManager, useStateWatcher, localStorageStore, DiplomaticClient } from '@interncom/diplomatic'
8
+ import {
9
+ DiplomaticClient,
10
+ localStorageStore,
11
+ StateManager,
12
+ useStateWatcher,
13
+ } from "@interncom/diplomatic";
9
14
 
10
15
  const appState = { count: 0 };
11
16
  const stateMgr = new StateManager(async (op) => {
12
17
  if (op.type === "count" && typeof op.body === "number") {
13
18
  appState.count = op.body;
14
19
  }
15
- })
20
+ });
16
21
 
17
22
  const client = new DiplomaticClient({
18
23
  seed: "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF",
@@ -22,8 +27,8 @@ const client = new DiplomaticClient({
22
27
  });
23
28
 
24
29
  export default function App() {
25
- const count = useStateWatcher(stateMgr, "count", () => appState.count)
26
- const inc = () => client.upsert("count", count + 1)
30
+ const count = useStateWatcher(stateMgr, "count", () => appState.count);
31
+ const inc = () => client.upsert("count", count + 1);
27
32
 
28
33
  return (
29
34
  <div style={{ width: "100vw", textAlign: "center" }}>
@@ -31,10 +36,11 @@ export default function App() {
31
36
  <h2>{count}</h2>
32
37
  <button onClick={inc}>+1</button>
33
38
  </div>
34
- )
39
+ );
35
40
  }
36
41
  ```
37
42
 
38
43
  ## Development
39
44
 
40
- To deploy npm package, from the `web` dir, run: `npm run build && npm run tsc && npm publish --public`.
45
+ To deploy npm package, from the `web` dir, run:
46
+ `npm run build && npm run tsc && npm publish --public`.
package/dist/client.d.ts CHANGED
@@ -1,57 +1,44 @@
1
- import type { EntityID, GroupID, IOp, KeyPair } from "./shared/types";
2
- import type { IClientStateStore, IDiplomaticClientState, IDiplomaticClientXferState } from "./types";
3
- import type { StateManager } from "./state";
4
- import TypedEventEmitter from "./typedEventEmitter";
5
- export interface IDiplomaticClientParams {
6
- store: IClientStateStore;
7
- stateManager: StateManager;
8
- seed?: string | Uint8Array;
9
- hostURL?: string;
10
- hostID?: string;
11
- }
12
- export default class DiplomaticClient {
13
- store: IClientStateStore;
14
- stateManager: StateManager;
15
- stateEmitter: TypedEventEmitter<IDiplomaticClientState>;
16
- xferStateEmitter: TypedEventEmitter<IDiplomaticClientXferState>;
17
- seed?: Uint8Array;
18
- encKey?: Uint8Array;
19
- hostURL?: URL;
20
- hostKeyPair?: KeyPair;
21
- constructor(params: IDiplomaticClientParams);
22
- addEventListener(func: (state: IDiplomaticClientState) => void): () => void;
23
- addXferEventListener(func: (state: IDiplomaticClientXferState) => void): () => void;
24
- wipe: () => Promise<void>;
25
- websocket?: WebSocket;
26
- connect: (hostURL: URL) => Promise<void>;
27
- init(params: IDiplomaticClientParams): Promise<void>;
28
- loadSeed(): Promise<void>;
29
- setSeed(seed: Uint8Array): Promise<void>;
30
- loadHost(): Promise<void>;
31
- register(hostURL: string): Promise<void>;
32
- disconnect: () => void;
33
- registerAndConnect(hostURL: string): Promise<void>;
34
- requeueAllOpsForUpload(): Promise<void>;
35
- emitUpdate(): Promise<void>;
36
- emitXferUpdate(): Promise<void>;
37
- getState(): Promise<IDiplomaticClientState>;
38
- getXferState(): Promise<IDiplomaticClientXferState>;
39
- processOps(): Promise<void>;
40
- pullOpPaths(): Promise<never[] | undefined>;
41
- fetchAndExecQueuedOps(): Promise<never[] | undefined>;
42
- private pushQueuedOp;
43
- pushQueuedOps(): Promise<void>;
44
- sync(): Promise<void>;
45
- apply(op: IOp): Promise<void>;
46
- export(filename: string, extension?: string): Promise<void>;
47
- import: (file: File) => Promise<void>;
48
- upsert<T>({ type, body, eid, gid, pid, version }: {
49
- type: string;
50
- body: T;
51
- eid?: EntityID;
52
- gid?: GroupID;
53
- pid?: EntityID;
54
- version?: number;
55
- }): Promise<void>;
56
- delete<T>(type: string, eid: Uint8Array, version?: number): Promise<void>;
1
+ import DiplomaticClientAPI from "./shared/client";
2
+ import { IClock } from "./shared/clock";
3
+ import { Status } from "./shared/consts";
4
+ import { EncodedMessage } from "./shared/message";
5
+ import { EntityID, HostHandle, ICrypto, IHostConnectionInfo, IInsertParams, IMessageHead, ITransport, IUpsertParams, MasterSeed } from "./shared/types";
6
+ import { ValStat } from "./shared/valstat";
7
+ import { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStateManager, IStore } from "./types";
8
+ export declare class SyncClient<Handle extends HostHandle> implements IClient<Handle> {
9
+ private clock;
10
+ private state;
11
+ private store;
12
+ private transport;
13
+ private crypto;
14
+ private forceSkewHandlingByDefault;
15
+ connections: Map<string, DiplomaticClientAPI<Handle>>;
16
+ private currentSync;
17
+ clientState: IStateEmitter<IDiplomaticClientState>;
18
+ xferState: IStateEmitter<IDiplomaticClientXferState>;
19
+ constructor(clock: IClock, state: IStateManager, store: IStore<Handle>, transport: (host: IHostRow<Handle>) => ITransport, crypto?: ICrypto, forceSkewHandlingByDefault?: boolean);
20
+ private readonly SYNC_DEBOUNCE_DELAY_MS;
21
+ private syncTimeout;
22
+ setSeed(seed: MasterSeed): Promise<void>;
23
+ private getClientState;
24
+ private getXferState;
25
+ private apply;
26
+ insertRaw(bod: EncodedMessage): Promise<ValStat<IMessageHead>>;
27
+ upsertRaw(eid: EntityID, bod: EncodedMessage | undefined, force?: boolean): Promise<ValStat<IMessageHead>>;
28
+ insert<T = unknown>(op: IInsertParams<T>): Promise<ValStat<IMessageHead>>;
29
+ upsert<T = unknown>(op: IUpsertParams<T>, force?: boolean): Promise<ValStat<IMessageHead>>;
30
+ delete(eid: EntityID): Promise<ValStat<IMessageHead>>;
31
+ genEID(id?: Uint8Array): Promise<ValStat<EntityID>>;
32
+ sync(): Promise<Status>;
33
+ private doSync;
34
+ wipe(): Promise<void>;
35
+ import: (file: File, options?: {
36
+ onProgress?: (index: number, total: number, status: Status) => void;
37
+ }) => Promise<Status>;
38
+ private scheduleSync;
39
+ export(filename: string): Promise<Status.Success | import("./shared/valstat").StatusNOK>;
40
+ link(host: IHostConnectionInfo<Handle>): Promise<void>;
41
+ unlink(label: string): Promise<void>;
42
+ connect(listen?: boolean): Promise<void>;
43
+ disconnect: () => Promise<void>;
57
44
  }
@@ -0,0 +1,36 @@
1
+ import { Status } from "../shared/consts";
2
+ import { ValStat } from "../shared/valstat";
3
+ import { EntityID, GroupID, IMsgEntBody, IOp } from "../shared/types";
4
+ import { StateManager } from "../state.ts";
5
+ export interface IEntity<T = unknown> extends Omit<IMsgEntBody<T>, "body"> {
6
+ eid: EntityID;
7
+ updatedAt: Date;
8
+ createdAt: Date;
9
+ ctr: number;
10
+ body: T;
11
+ }
12
+ export interface IDateRange {
13
+ start: Date;
14
+ end: Date;
15
+ }
16
+ export type EntitiesQuery = {
17
+ type: string;
18
+ gid?: GroupID;
19
+ pid?: EntityID;
20
+ updatedBetween?: IDateRange;
21
+ };
22
+ export interface IEntDB {
23
+ apply: (ops: IOp[]) => Promise<{
24
+ stats: Status[];
25
+ types: Set<string>;
26
+ }>;
27
+ clear: () => Promise<Status>;
28
+ getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
29
+ getEntities<T>({ type, gid, pid, updatedBetween }: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
30
+ countEntities({ type }: {
31
+ type: string;
32
+ }): Promise<ValStat<number>>;
33
+ }
34
+ export declare function entStateManager(edb: IEntDB): StateManager;
35
+ export declare function applyOp(curr: IEntity | undefined, op: IOp): ValStat<IEntity | undefined>;
36
+ export declare const nullEntDB: IEntDB;
@@ -0,0 +1,27 @@
1
+ import { Status } from "../shared/consts";
2
+ import { EntityID, GroupID, IOp } from "../shared/types";
3
+ import { ValStat } from "../shared/valstat.ts";
4
+ import { EntitiesQuery, IEntDB, IEntity } from "./entdb";
5
+ export declare const entityTableName = "entities";
6
+ export declare const typeIndexName = "entity_type_created_at";
7
+ export declare const typeUpdatedAtIndexName = "entity_type_updated_at";
8
+ export declare const typeGroupIndexName = "entity_type_group_id";
9
+ export declare const typeParentIndexName = "entity_type_parent_id";
10
+ export declare class EntIDB implements IEntDB {
11
+ db: IDBDatabase | undefined;
12
+ init(): Promise<void>;
13
+ apply: (ops: IOp[]) => Promise<{
14
+ stats: Status[];
15
+ types: Set<string>;
16
+ }>;
17
+ clear(): Promise<Status>;
18
+ getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
19
+ getAllOfTypeUpdatedBetween<T>(opType: string, start: Date, end: Date): Promise<ValStat<IEntity<T>[]>>;
20
+ getGroupMembers<T>(opType: string, gid: GroupID): Promise<ValStat<IEntity<T>[]>>;
21
+ getAllOfType<T>(opType: string): Promise<ValStat<IEntity<T>[]>>;
22
+ private getAllEntities;
23
+ getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
24
+ countEntities({ type }: {
25
+ type: string;
26
+ }): Promise<ValStat<number>>;
27
+ }
@@ -0,0 +1,30 @@
1
+ import { IEntDB, IEntity } from "./entdb";
2
+ import { Status } from "../shared/consts";
3
+ import { EntityID, GroupID, IOp } from "../shared/types";
4
+ import { ValStat } from "../shared/valstat.ts";
5
+ interface IDateRange {
6
+ start: Date;
7
+ end: Date;
8
+ }
9
+ type EntitiesQuery = {
10
+ type: string;
11
+ gid?: GroupID;
12
+ pid?: EntityID;
13
+ updatedBetween?: IDateRange;
14
+ };
15
+ export declare class EntDBMemory implements IEntDB {
16
+ ents: Map<string, IEntity>;
17
+ constructor(initEnts?: IEntity[]);
18
+ apply(ops: IOp[]): Promise<{
19
+ stats: Status[];
20
+ types: Set<string>;
21
+ }>;
22
+ clear(): Promise<Status>;
23
+ getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
24
+ private getAllEntities;
25
+ getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
26
+ countEntities({ type }: {
27
+ type: string;
28
+ }): Promise<ValStat<number>>;
29
+ }
30
+ export {};
@@ -0,0 +1,18 @@
1
+ import { IStateEmitter } from "./types";
2
+ type TypedListener<T> = (data: T) => void;
3
+ export declare class TypedEventEmitter<T> {
4
+ private listeners;
5
+ addEventListener(eventType: string, listener: TypedListener<T>): () => void;
6
+ removeEventListener(eventType: string, listener: TypedListener<T>): void;
7
+ emit(eventType: string, data: T): void;
8
+ }
9
+ export declare class StateEmitter<T> implements IStateEmitter<T> {
10
+ private getter;
11
+ static eventName: string;
12
+ emitter: TypedEventEmitter<T>;
13
+ constructor(getter: () => Promise<T>);
14
+ get: () => Promise<T>;
15
+ emit(): Promise<void>;
16
+ listen(func: (state: T) => void): () => void;
17
+ }
18
+ export {};
package/dist/exim.d.ts ADDED
File without changes
package/dist/index.d.ts CHANGED
@@ -1,15 +1,24 @@
1
- import { useClientState, useClientXferState, useSyncOnResume } from "./react/useClient";
2
- import { opMapApplier, StateManager } from "./state";
3
- import { localStorageStore } from "./localStorageStore";
4
- import { idbStore } from "./idbStore";
5
- import type { IDiplomaticClientState } from "./types";
6
- import DiplomaticClient from "./client";
1
+ import { SyncClient } from "./client";
7
2
  import libsodiumCrypto from "./crypto";
8
- import { btoh, htob } from "./shared/lib";
9
- import { type IOp, type IUpsertOp, Verb } from "./shared/types";
3
+ import { EntitiesQuery, entStateManager, IEntDB, IEntity, nullEntDB } from "./entdb/entdb";
4
+ import { EntIDB } from "./entdb/idb";
5
+ import { EntDBMemory } from "./entdb/memory";
6
+ import { useClientState, useClientXferState, useSyncOnResume } from "./react/useClient";
10
7
  import useStateWatcher, { useStateWatcherSuspense } from "./react/useStateWatcher";
11
- import InitSeedView from "./react/initSeedView";
12
- import ClientStatusBar from "./react/statusBar";
13
- import * as EntityDB from "./entityDB";
14
- export { btoh, ClientStatusBar, DiplomaticClient, EntityDB, htob, idbStore, InitSeedView, libsodiumCrypto, localStorageStore, opMapApplier, StateManager, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, Verb, };
15
- export type { IDiplomaticClientState, IOp, IUpsertOp };
8
+ import { btoh, htob } from "./shared/binary";
9
+ import { Clock } from "./shared/clock";
10
+ import { genSingletonEID } from "./shared/codecs/eid";
11
+ import { Status } from "./shared/consts";
12
+ import { HTTPTransport } from "./shared/http";
13
+ import { EntityID, GroupID, type IOp, MasterSeed } from "./shared/types";
14
+ import { nullStateManager, StateManager } from "./state";
15
+ import { IDBStore, openIDBStore } from "./stores/idb/store";
16
+ import { MemoryStore } from "./stores/memory/store";
17
+ import type { IDiplomaticClientState, IHostRow, IStateManager, IStore } from "./types";
18
+ export declare const hostHTTPTransport: (host: IHostRow<URL>) => HTTPTransport;
19
+ export declare function genWebClient(stateMgr: StateManager, url: URL): Promise<{
20
+ client: SyncClient<URL>;
21
+ setSeed: (seedHex: string) => Promise<void>;
22
+ }>;
23
+ export { btoh, Clock, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, htob, HTTPTransport, IDBStore, IEntDB, IEntity, IStateManager, IStore, libsodiumCrypto, MasterSeed, MemoryStore, nullEntDB, nullStateManager, openIDBStore, StateManager, Status, SyncClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, };
24
+ export type { IDiplomaticClientState, IOp };