@interncom/diplomatic 0.1.0 → 0.1.2

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.
@@ -1,6 +1,17 @@
1
- import type { SyncClient } from "../client";
2
- import { HostHandle } from "../shared/types";
1
+ import { SyncClient } from "../client";
2
+ import { HostHandle, IHostConnectionInfo, IStateManager, MasterSeed } from "../shared/types";
3
3
  import type { IDiplomaticClientState, IDiplomaticClientXferState } from "../types";
4
+ import { IEntDB } from "../entdb/entdb";
5
+ import { IClock } from "../shared/clock";
4
6
  export declare function useClientState<Handle extends HostHandle>(client: SyncClient<Handle>): IDiplomaticClientState | undefined;
5
7
  export declare function useClientXferState<Handle extends HostHandle>(client: SyncClient<Handle>): IDiplomaticClientXferState | undefined;
6
8
  export declare function useSyncOnResume<Handle extends HostHandle>(client: SyncClient<Handle>): void;
9
+ export declare function useClient({ clock, seed, host }: {
10
+ clock?: IClock;
11
+ seed?: MasterSeed;
12
+ host?: IHostConnectionInfo<URL>;
13
+ }): {
14
+ client?: SyncClient<URL>;
15
+ entDB?: IEntDB;
16
+ stateMgr: IStateManager;
17
+ };
@@ -1,3 +1,3 @@
1
- import { IStateManager } from "../types";
1
+ import { IStateManager } from "../shared/types";
2
2
  export default function useStateWatcher<T>(mgr: IStateManager, opType: string, callback: () => Promise<T>): T | undefined;
3
3
  export declare function useStateWatcherSuspense<T>(mgr: IStateManager, opType: string, callback: () => Promise<T>): T;
@@ -6,11 +6,10 @@ import type { Hash, HostSpecificKeyPair, IBag, ICrypto, IHostCrypto, IMessage, I
6
6
  export declare function bagSigValid(bag: IBag, pubKey: PublicKey, crypto: IHostCrypto): Promise<boolean>;
7
7
  export declare function kdmFor(msgHeadEnc: Uint8Array, keys: HostSpecificKeyPair, crypto: ICrypto): Promise<Uint8Array>;
8
8
  export declare function sealBag(msg: IMessage, keys: HostSpecificKeyPair, crypto: ICrypto, enclave: Enclave): Promise<ValStat<IBag>>;
9
- interface IOpenBag {
9
+ export interface IOpenBag {
10
10
  msgHead: IMessageHead;
11
11
  bod?: EncodedMessage;
12
12
  headHash: Hash;
13
13
  }
14
14
  export declare function openBagBody(headEnc: Uint8Array, bodyCph: Uint8Array | undefined, key: Uint8Array, crypto: ICrypto): Promise<ValStat<IOpenBag>>;
15
15
  export declare function openBag(bag: IBag, pubKey: PublicKey, crypto: ICrypto, enclave: Enclave): Promise<ValStat<IMessageWithHash>>;
16
- export {};
@@ -19,5 +19,5 @@ export interface IAuthData {
19
19
  keys: HostSpecificKeyPair;
20
20
  authTS: IAuthTimestamp;
21
21
  }
22
- export declare function hostKeys(host: IProtoClient, keyPath: string, idx: number): Promise<HostSpecificKeyPair>;
22
+ export declare function hostKeys(host: IProtoClient, keyPath: string, idx?: number): Promise<HostSpecificKeyPair>;
23
23
  export {};
@@ -0,0 +1,8 @@
1
+ type TypedListener<T> = (data: T) => void;
2
+ export declare class TypedEventEmitter<T> {
3
+ private listeners;
4
+ addEventListener(eventType: string, listener: TypedListener<T>): () => void;
5
+ removeEventListener(eventType: string, listener: TypedListener<T>): void;
6
+ emit(eventType: string, data: T): void;
7
+ }
8
+ export {};
@@ -1,11 +1,14 @@
1
1
  import { IClock } from "../clock.ts";
2
- import { type IHostCrypto, type IProtoHost, type IStorage, type IWebSocketPushNotifier } from "../types.ts";
2
+ import { IAuthTimestamp } from "../codecs/authTimestamp.ts";
3
+ import { type IHostCrypto, type IProtoHost, type IPushNotifier, type IStorage } from "../types.ts";
4
+ import { ValStat } from "../valstat.ts";
3
5
  export declare class DiplomaticHTTPServer implements IProtoHost {
4
6
  storage: IStorage;
5
7
  crypto: IHostCrypto;
6
- notifier: IWebSocketPushNotifier;
8
+ notifier: IPushNotifier;
7
9
  clock: IClock;
8
- constructor(storage: IStorage, crypto: IHostCrypto, notifier: IWebSocketPushNotifier, clock: IClock);
10
+ constructor(storage: IStorage, crypto: IHostCrypto, notifier: IPushNotifier, clock: IClock);
9
11
  corsHandler: (request: Request) => Promise<Response>;
10
12
  handler: (request: Request) => Promise<Response>;
11
13
  }
14
+ export declare function validateWebSocketAuth(request: Request, host: IProtoHost): Promise<ValStat<IAuthTimestamp>>;
@@ -1,6 +1,6 @@
1
1
  import { Decoder, Encoder } from "./codec.ts";
2
2
  import { APICallName } from "./consts.ts";
3
- import { IPushListener, ITransport } from "./types.ts";
3
+ import { IHostConnectionInfo, IPushListener, ITransport } from "./types.ts";
4
4
  import { IRespHead } from "./codecs/respHead.ts";
5
5
  import { ValStat } from "./valstat.ts";
6
6
  export declare const api: {
@@ -75,6 +75,7 @@ export declare class HTTPTransport implements ITransport {
75
75
  constructor(url: URL);
76
76
  call(name: APICallName, enc: Encoder): Promise<ValStat<Decoder>>;
77
77
  }
78
+ export declare const hostHTTPTransport: (host: IHostConnectionInfo<URL>) => HTTPTransport;
78
79
  export declare function errResp(head: IRespHead): Response;
79
80
  export declare function binResp(encoder: Encoder): Response;
80
81
  export declare function cors(resp: Response): Response;
@@ -1,12 +1,5 @@
1
- import { IClock } from "./clock.ts";
2
- import type { ICrypto, IMessage, IMessageHead, SerializedContent } from "./types.ts";
1
+ import type { ICrypto, IDeleteMessage, IMessage, IMessageHead, IUpsertMessage, SerializedContent } from "./types.ts";
3
2
  import { ValStat } from "./valstat.ts";
4
- interface IUpsertMessage extends IMessage {
5
- bod: SerializedContent;
6
- }
7
- interface IDeleteMessage extends Omit<IMessage, "bod"> {
8
- len: 0;
9
- }
10
3
  export type EncodedMessage = Uint8Array;
11
4
  interface IInsertParams {
12
5
  now: Date;
@@ -26,5 +19,4 @@ interface IDeleteParams extends Omit<IMessage, "off" | "len" | "hsh" | "bod"> {
26
19
  export declare function genDeleteHead({ now, eid, ctr, crypto }: IDeleteParams): Promise<ValStat<IMessageHead>>;
27
20
  export declare function genInsert({ now, bod, crypto }: IInsertParams): Promise<ValStat<IUpsertMessage>>;
28
21
  export declare function genDelete(params: IDeleteParams): Promise<ValStat<IDeleteMessage>>;
29
- export declare function genSingletonUpsert(type: string, clk: IClock, content: Uint8Array, ctr?: number): Promise<ValStat<IUpsertMessage>>;
30
22
  export {};
@@ -0,0 +1,15 @@
1
+ import { IClock } from "./clock.ts";
2
+ import { Status } from "./consts.ts";
3
+ import { IMessage, IStateManager, IUpsertMessage, SerializedContent } from "./types.ts";
4
+ import { ValStat } from "./valstat.ts";
5
+ export declare function genSingletonUpsert(type: string, clk: IClock, content: Uint8Array, ctr?: number): Promise<ValStat<IUpsertMessage>>;
6
+ export declare class SingletonStateManager implements IStateManager {
7
+ private singletonType;
8
+ latest: SerializedContent | undefined;
9
+ private latestOff;
10
+ private emitter;
11
+ constructor(singletonType: string);
12
+ apply(messages: IMessage[]): Promise<Status[]>;
13
+ on: (event: string, listener: () => void) => void;
14
+ off: (event: string, listener: () => void) => void;
15
+ }
@@ -0,0 +1,9 @@
1
+ import { IBagPeekItem } from "./codecs/peekItem.ts";
2
+ import { Enclave } from "./enclave.ts";
3
+ import { HostSpecificKeyPair, ICrypto } from "./types.ts";
4
+ import { ValStat } from "./valstat.ts";
5
+ export interface IDecryptedBagPeekItem {
6
+ kdm: Uint8Array;
7
+ headEnc: Uint8Array;
8
+ }
9
+ export declare function decryptPeekItem(item: IBagPeekItem, hostKeys: HostSpecificKeyPair, enclave: Enclave, crypto: ICrypto): Promise<ValStat<IDecryptedBagPeekItem>>;
@@ -115,9 +115,6 @@ export interface IPushNotifier {
115
115
  open(authTS: IAuthTimestamp, recv: PushReceiver, crypto: IHostCrypto, clock: IClock): Promise<IPushOpenResponse>;
116
116
  push(pubKey: PublicKey, data: Uint8Array): void;
117
117
  }
118
- export interface IWebSocketPushNotifier extends IPushNotifier {
119
- handle(host: IProtoHost, req: Request): Promise<Response>;
120
- }
121
118
  export interface IPushListener {
122
119
  connect(authTS: IAuthTimestamp, recv: PushReceiver, onDisconnect: () => void): Promise<Status>;
123
120
  connected(): boolean;
@@ -136,7 +133,7 @@ export type HostHandle = URL | IProtoHost;
136
133
  export interface IHostConnectionInfo<Handle extends HostHandle> {
137
134
  handle: Handle;
138
135
  label: string;
139
- idx: number;
136
+ idx?: number;
140
137
  }
141
138
  export interface ISubscriptionMetadata {
142
139
  term: number;
@@ -153,4 +150,15 @@ export interface ITransport {
153
150
  call: (name: APICallName, enc: Encoder) => Promise<ValStat<Decoder>>;
154
151
  listener: IPushListener;
155
152
  }
153
+ export interface IStateManager {
154
+ apply: (msgs: IMessage[]) => Promise<Status[]>;
155
+ on: (type: string, listener: () => void) => void;
156
+ off: (type: string, listener: () => void) => void;
157
+ }
158
+ export interface IUpsertMessage extends IMessage {
159
+ bod: SerializedContent;
160
+ }
161
+ export interface IDeleteMessage extends Omit<IMessage, "bod"> {
162
+ len: 0;
163
+ }
156
164
  export {};
package/dist/state.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Status } from "./shared/consts";
2
- import { IMessage, IMsgEntBody, IOp } from "./shared/types";
2
+ import { IMessage, IMsgEntBody, IOp, IStateManager } from "./shared/types";
3
3
  import { ValStat } from "./shared/valstat";
4
- import type { Applier, IStateManager } from "./types";
4
+ import type { Applier } from "./types";
5
5
  export declare function isMsgEntBody(bodDec: unknown): bodDec is IMsgEntBody;
6
6
  export declare function msgToOp(msg: IMessage): ValStat<IOp>;
7
7
  export declare class StateManager implements IStateManager {
@@ -20,4 +20,5 @@ export declare class IDBStore implements IStore<URL> {
20
20
  constructor(db: IDBDatabase, crypto: ICrypto);
21
21
  wipe(): Promise<void>;
22
22
  }
23
- export declare function openIDBStore(): Promise<IDBDatabase>;
23
+ export declare function openIDBStoreDB(): Promise<IDBDatabase>;
24
+ export declare function openIDBStore(crypto: ICrypto): Promise<IDBStore>;
package/dist/sync.d.ts CHANGED
@@ -1,16 +1,9 @@
1
1
  import DiplomaticClientAPI from "./shared/client";
2
2
  import { IClock } from "./shared/clock";
3
- import { IBagPeekItem } from "./shared/codecs/peekItem";
4
3
  import { Status } from "./shared/consts";
5
4
  import { Enclave } from "./shared/enclave";
6
- import { HostHandle, HostSpecificKeyPair, ICrypto } from "./shared/types";
7
- import { ValStat } from "./shared/valstat";
5
+ import { HostHandle, ICrypto } from "./shared/types";
8
6
  import { IHostRow, IMsgParts, IStore, IStoredMessageData } from "./types";
9
- export interface IDecryptedBagPeekItem {
10
- kdm: Uint8Array;
11
- headEnc: Uint8Array;
12
- }
13
- export declare function decryptPeekItem(item: IBagPeekItem, hostKeys: HostSpecificKeyPair, enclave: Enclave, crypto: ICrypto): Promise<ValStat<IDecryptedBagPeekItem>>;
14
7
  export interface ISyncParams<Handle extends HostHandle> {
15
8
  conn: DiplomaticClientAPI<Handle>;
16
9
  store: IStore<Handle>;
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Status } from "./shared/consts";
2
2
  import type { Enclave } from "./shared/enclave";
3
3
  import type { EncodedMessage } from "./shared/message";
4
- import type { EntityID, Hash, HostHandle, IHostConnectionInfo, IHostMetadata, IInsertParams, IMessage, IMessageHead, IOp, IUpsertParams, MasterSeed, SerializedContent } from "./shared/types";
4
+ import type { EntityID, Hash, HostHandle, IHostConnectionInfo, IHostMetadata, IInsertParams, IMessageHead, IOp, IUpsertParams, MasterSeed, SerializedContent } from "./shared/types";
5
5
  import { ValStat } from "./shared/valstat";
6
6
  import { ICrypto } from "./shared/types";
7
7
  export interface IMsgParts {
@@ -115,9 +115,4 @@ export interface IClient<Handle extends HostHandle> {
115
115
  clientState: IStateEmitter<IDiplomaticClientState>;
116
116
  xferState: IStateEmitter<IDiplomaticClientXferState>;
117
117
  }
118
- export interface IStateManager {
119
- apply: (msgs: IMessage[]) => Promise<Status[]>;
120
- on: (type: string, listener: () => void) => void;
121
- off: (type: string, listener: () => void) => void;
122
- }
123
118
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interncom/diplomatic",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Secure sync layer",
6
6
  "main": "dist/index.mjs",
@@ -34,7 +34,7 @@
34
34
  "react": "^19.0.0"
35
35
  },
36
36
  "dependencies": {
37
- "@msgpack/msgpack": "^3.0.0-beta2",
37
+ "@msgpack/msgpack": "3.1.3",
38
38
  "@noble/hashes": "1.8.0",
39
39
  "file-saver": "2.0.5",
40
40
  "libsodium.js": "git+ssh://github.com/interncom/libsodium.js#e4e7647"