@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
@@ -1,78 +1,156 @@
1
- export declare enum Verb {
2
- DELETE = 0,
3
- UPSERT = 1
4
- }
5
- type Timestamp = string;
6
- export type GroupID = Uint8Array | string;
7
- export type EntityID = Uint8Array;
8
- export interface IBaseOp {
9
- eid: EntityID;
10
- ts: Timestamp;
11
- type: string;
12
- ver: number;
13
- }
14
- export interface IUpsertOp extends IBaseOp {
1
+ import { IClock } from "./clock.ts";
2
+ import { Decoder, Encoder } from "./codec.ts";
3
+ import { IAuthTimestamp } from "./codecs/authTimestamp.ts";
4
+ import type { IBagPeekItem } from "./codecs/peekItem.ts";
5
+ import { IUsageQuota } from "./codecs/usageQuota.ts";
6
+ import { APICallName, Status } from "./consts.ts";
7
+ import { ValStat } from "./valstat.ts";
8
+ export type GroupID = string;
9
+ declare const entityIDSymbol: unique symbol;
10
+ export type EntityID = Uint8Array & {
11
+ readonly [entityIDSymbol]: true;
12
+ };
13
+ export type SerializedContent = Uint8Array;
14
+ export interface IMsgEntBody<T = unknown> {
15
15
  gid?: GroupID;
16
16
  pid?: EntityID;
17
- verb: Verb.UPSERT;
18
- body: unknown;
17
+ type: string;
18
+ body?: T;
19
19
  }
20
- export interface IDeleteOp extends IBaseOp {
21
- verb: Verb.DELETE;
20
+ export interface IMessageHead {
21
+ eid: EntityID;
22
+ off: number;
23
+ ctr: number;
24
+ len: number;
25
+ hsh?: Uint8Array;
26
+ }
27
+ export interface IMessage extends IMessageHead {
28
+ bod?: SerializedContent;
22
29
  }
23
- export type IOp = IUpsertOp | IDeleteOp;
24
- export type CipherOp = Uint8Array;
25
- export interface ISyncRequest {
26
- ops: CipherOp[];
27
- begin: Timestamp;
30
+ export interface IMessageWithHash extends IMessage {
31
+ headHash: Hash;
28
32
  }
29
- export interface IRegistrationRequest {
30
- token: string;
31
- pubKey: Uint8Array;
33
+ export interface IDeleteOp extends Omit<IMessageHead, "len" | "hsh" | "bod"> {
32
34
  }
33
- export interface IOperationRequest {
34
- cipher: Uint8Array;
35
+ export interface IMutateOp<T = unknown> extends IDeleteOp, Omit<IMsgEntBody<T>, "body"> {
36
+ body: T;
35
37
  }
36
- export interface IDeltaListItem {
37
- sha256: Uint8Array;
38
- recordedAt: Date;
38
+ export type IOp = IDeleteOp | IMutateOp;
39
+ export declare function isDeleteOp(op: IOp): op is IDeleteOp;
40
+ export declare function isMutateOp(op: IOp): op is IMutateOp;
41
+ export interface IInsertParams<T> extends IMsgEntBody<T> {
42
+ id?: Uint8Array;
39
43
  }
40
- export interface IListDeltasResponse {
41
- deltas: IDeltaListItem[];
42
- fetchedAt: string;
44
+ export interface IUpsertParams<T> extends IMsgEntBody<T> {
45
+ eid?: EntityID;
43
46
  }
44
47
  export interface IStorage {
45
- addUser: (pubKeyHex: string) => Promise<void>;
46
- hasUser: (pubKeyHex: string) => Promise<boolean>;
47
- setOp: (pubKeyHex: string, recordedAt: Date, op: Uint8Array) => Promise<void>;
48
- getOp: (pubKeyHex: string, sha256Hex: string) => Promise<Uint8Array | undefined>;
49
- listOps: (pubKeyHex: string, begin: string, end: string) => Promise<IDeltaListItem[]>;
48
+ addUser: (pubKey: PublicKey) => Promise<ValStat<void>>;
49
+ hasUser: (pubKey: PublicKey) => Promise<ValStat<boolean>>;
50
+ subMeta: (pubKey: PublicKey) => Promise<ValStat<ISubscriptionMetadata>>;
51
+ setBag: (pubKey: PublicKey, bag: IBag) => Promise<ValStat<number>>;
52
+ getBody: (pubKey: PublicKey, seq: number) => Promise<ValStat<Uint8Array | undefined>>;
53
+ listHeads: (pubKey: PublicKey, minSeq: number) => Promise<ValStat<IBagPeekItem[]>>;
50
54
  }
51
55
  export interface KeyPair {
52
56
  keyType: "public" | "private" | "secret";
53
- privateKey: Uint8Array;
54
- publicKey: Uint8Array;
57
+ privateKey: PrivateKey;
58
+ publicKey: PublicKey;
55
59
  }
60
+ declare const masterSeedSymbol: unique symbol;
61
+ declare const derivationSeedSymbol: unique symbol;
62
+ declare const publicKeySymbol: unique symbol;
63
+ declare const privateKeySymbol: unique symbol;
64
+ declare const hostSpecificKeyPairSymbol: unique symbol;
65
+ declare const hashSymbol: unique symbol;
66
+ export type MasterSeed = Uint8Array & {
67
+ readonly [masterSeedSymbol]: true;
68
+ };
69
+ export type DerivationSeed = Uint8Array & {
70
+ readonly [derivationSeedSymbol]: true;
71
+ };
72
+ export type PublicKey = Uint8Array & {
73
+ readonly [publicKeySymbol]: true;
74
+ };
75
+ export type PrivateKey = Uint8Array & {
76
+ readonly [privateKeySymbol]: true;
77
+ };
78
+ export type HostSpecificKeyPair = KeyPair & {
79
+ readonly [hostSpecificKeyPairSymbol]: true;
80
+ };
81
+ export type Hash = Uint8Array & {
82
+ readonly [hashSymbol]: true;
83
+ };
56
84
  export interface IHostCrypto {
57
- checkSigEd25519: (sig: Uint8Array, message: Uint8Array | string, pubKey: Uint8Array) => Promise<boolean>;
58
- sha256Hash: (data: Uint8Array) => Promise<Uint8Array>;
85
+ checkSigEd25519: (sig: Uint8Array, message: Uint8Array | string, pubKey: PublicKey) => Promise<boolean>;
59
86
  }
60
87
  export interface ICrypto extends IHostCrypto {
61
- gen128BitRandomID: () => Promise<Uint8Array>;
88
+ genRandomBytes: (bytes: number) => Promise<Uint8Array>;
62
89
  gen256BitSecureRandomSeed: () => Promise<Uint8Array>;
63
90
  deriveXSalsa20Poly1305Key: (seed: Uint8Array, derivationIndex: number) => Promise<Uint8Array>;
64
91
  encryptXSalsa20Poly1305Combined: (plaintext: Uint8Array, key: Uint8Array) => Promise<Uint8Array>;
65
92
  decryptXSalsa20Poly1305Combined: (headerAndCipher: Uint8Array, key: Uint8Array) => Promise<Uint8Array>;
66
- deriveEd25519KeyPair: (seed: Uint8Array, hostID: string, derivationIndex: number) => Promise<KeyPair>;
67
- signEd25519: (message: Uint8Array | string, secKey: Uint8Array) => Promise<Uint8Array>;
93
+ deriveEd25519KeyPair: (derivationSeed: DerivationSeed) => Promise<KeyPair>;
94
+ signEd25519: (message: Uint8Array | string, secKey: PrivateKey) => Promise<Uint8Array>;
95
+ blake3: (data: Uint8Array) => Promise<Hash>;
68
96
  }
69
97
  export interface IMsgpackCodec {
70
98
  encode: (source: unknown) => Uint8Array;
71
99
  decode: (packed: ArrayBuffer | Uint8Array) => unknown;
72
100
  decodeAsync: (stream: ReadableStream<Uint8Array>) => Promise<unknown>;
73
101
  }
74
- export interface IWebsocketNotifier {
75
- handler: (request: Request, hasUser: (pubKeyHex: string) => Promise<boolean>) => Promise<Response>;
76
- notify: (pubKeyHex: string) => Promise<void>;
102
+ export interface IProtoHost {
103
+ storage: IStorage;
104
+ crypto: IHostCrypto;
105
+ notifier: IPushNotifier;
106
+ clock: IClock;
107
+ }
108
+ export type PushReceiver = (data: Uint8Array) => void;
109
+ export interface IPushOpenResponse {
110
+ send: (data: Uint8Array) => Status;
111
+ shut: () => Status;
112
+ status: Status;
113
+ }
114
+ export interface IPushNotifier {
115
+ open(authTS: IAuthTimestamp, recv: PushReceiver, crypto: IHostCrypto, clock: IClock): Promise<IPushOpenResponse>;
116
+ push(pubKey: PublicKey, data: Uint8Array): void;
117
+ }
118
+ export interface IWebSocketPushNotifier extends IPushNotifier {
119
+ handle(host: IProtoHost, req: Request): Promise<Response>;
120
+ }
121
+ export interface IPushListener {
122
+ connect(authTS: IAuthTimestamp, recv: PushReceiver, onDisconnect: () => void): Promise<Status>;
123
+ connected(): boolean;
124
+ disconnect(): void;
125
+ }
126
+ export interface IBagHeader {
127
+ sig: Uint8Array;
128
+ kdm: Uint8Array;
129
+ }
130
+ export interface IBag extends IBagHeader {
131
+ headCph: Uint8Array;
132
+ bodyCph: Uint8Array;
133
+ }
134
+ export type EncodedBag = Uint8Array;
135
+ export type HostHandle = URL | IProtoHost;
136
+ export interface IHostConnectionInfo<Handle extends HostHandle> {
137
+ handle: Handle;
138
+ label: string;
139
+ idx: number;
140
+ }
141
+ export interface ISubscriptionMetadata {
142
+ term: number;
143
+ elapsed: number;
144
+ stat: IUsageQuota;
145
+ dyn: IUsageQuota;
146
+ }
147
+ export declare const nullSubMeta: ISubscriptionMetadata;
148
+ export interface IHostMetadata {
149
+ subscription: ISubscriptionMetadata;
150
+ clockOffset: number;
151
+ }
152
+ export interface ITransport {
153
+ call: (name: APICallName, enc: Encoder) => Promise<ValStat<Decoder>>;
154
+ listener: IPushListener;
77
155
  }
78
156
  export {};
@@ -0,0 +1,6 @@
1
+ import { Status } from "./consts.ts";
2
+ export type StatusOK = Status.Success;
3
+ export type StatusNOK = Exclude<Status, Status.Success>;
4
+ export type ValStat<T> = [T, StatusOK] | [undefined, StatusNOK];
5
+ export declare function ok<T>(val: T): ValStat<T>;
6
+ export declare function err<T>(stat: StatusNOK): ValStat<T>;
package/dist/state.d.ts CHANGED
@@ -1,21 +1,16 @@
1
- import { EventEmitter } from "./eventEmitter";
2
- import type { Applier } from "./types";
3
- import type { IOp } from "./shared/types";
4
- export declare class StateManager {
5
- emitter: EventEmitter;
1
+ import { Status } from "./shared/consts";
2
+ import { IMessage, IMsgEntBody, IOp } from "./shared/types";
3
+ import { ValStat } from "./shared/valstat";
4
+ import type { Applier, IStateManager } from "./types";
5
+ export declare function isMsgEntBody(bodDec: unknown): bodDec is IMsgEntBody;
6
+ export declare function msgToOp(msg: IMessage): ValStat<IOp>;
7
+ export declare class StateManager implements IStateManager {
6
8
  applier: Applier;
7
- clear: () => Promise<void>;
8
- constructor(applier: Applier, clear: () => Promise<void>);
9
- apply: (op: IOp) => Promise<void>;
9
+ clear: () => Promise<Status>;
10
+ private emitter;
11
+ constructor(applier: Applier, clear: () => Promise<Status>);
12
+ apply: (msgs: IMessage[]) => Promise<Status[]>;
10
13
  on: (opType: string, listener: () => void) => void;
11
14
  off: (opType: string, listener: () => void) => void;
12
15
  }
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 {};
16
+ export declare const nullStateManager: IStateManager;
@@ -0,0 +1,10 @@
1
+ import { IDownloadMessage, IDownloadQueue } from "../../types";
2
+ export declare class IDBDownloadQueue implements IDownloadQueue {
3
+ db: IDBDatabase;
4
+ constructor(db: IDBDatabase);
5
+ enq(msgs: Iterable<IDownloadMessage>): Promise<void>;
6
+ deq(host: string, seqs: Iterable<number>): Promise<void>;
7
+ list(): Promise<IDownloadMessage[]>;
8
+ count(): Promise<number>;
9
+ wipe(): Promise<void>;
10
+ }
@@ -0,0 +1,15 @@
1
+ import { Status } from "../../shared/consts";
2
+ import { IHostConnectionInfo, IHostMetadata } from "../../shared/types";
3
+ import type { IHostRow, IHostStore } from "../../types";
4
+ export declare class IDBHostStore implements IHostStore<URL> {
5
+ db: IDBDatabase;
6
+ constructor(db: IDBDatabase);
7
+ add(info: IHostConnectionInfo<URL>): Promise<void>;
8
+ private put;
9
+ touch(label: string, seq: number): Promise<void>;
10
+ get(label: string): Promise<IHostRow<URL> | undefined>;
11
+ set(label: string, meta: IHostMetadata): Promise<Status.Success | Status.NotFound | Status.DatabaseError>;
12
+ del(label: string): Promise<void>;
13
+ list(): Promise<IHostRow<URL>[]>;
14
+ wipe(): Promise<void>;
15
+ }
@@ -0,0 +1,19 @@
1
+ import { Status } from "../../shared/consts";
2
+ import { ICrypto } from "../../shared/types";
3
+ import { EntityID, Hash } from "../../shared/types";
4
+ import { IMessageStore, IStoredMessage, IStoredMessageData } from "../../types";
5
+ export declare class IDBMessageStore implements IMessageStore {
6
+ private crypto;
7
+ db: IDBDatabase;
8
+ constructor(db: IDBDatabase, crypto: ICrypto);
9
+ add(messages: {
10
+ key: Hash;
11
+ data: IStoredMessageData;
12
+ }[]): Promise<Status[]>;
13
+ del(keys: Iterable<Hash>): Promise<void>;
14
+ get(key: Hash): Promise<IStoredMessage | undefined>;
15
+ has(key: Hash): Promise<boolean>;
16
+ list(): Promise<Iterable<IStoredMessage>>;
17
+ last(eid: EntityID): Promise<IStoredMessage | undefined>;
18
+ wipe(): Promise<void>;
19
+ }
@@ -0,0 +1,11 @@
1
+ import { Enclave } from "../../shared/enclave";
2
+ import { MasterSeed } from "../../shared/types";
3
+ import { ISeedStore } from "../../types";
4
+ export declare class IDBSeedStore implements ISeedStore {
5
+ enclave?: Enclave;
6
+ db: IDBDatabase;
7
+ constructor(db: IDBDatabase);
8
+ save(seed: MasterSeed): Promise<Enclave>;
9
+ load(): Promise<Enclave | undefined>;
10
+ wipe(): Promise<void>;
11
+ }
@@ -0,0 +1,23 @@
1
+ import { ICrypto } from "../../shared/types";
2
+ import { IStore } from "../../types";
3
+ import { IDBDownloadQueue } from "./dnlds";
4
+ import { IDBHostStore } from "./hosts";
5
+ import { IDBMessageStore } from "./msgs";
6
+ import { IDBSeedStore } from "./seed";
7
+ import { IDBUploadQueue } from "./uplds";
8
+ export declare const SEED_META_TABLE = "seedMeta";
9
+ export declare const HOSTS_TABLE = "hosts";
10
+ export declare const UPLOAD_QUEUE_TABLE = "uploadQueue";
11
+ export declare const DOWNLOAD_QUEUE_TABLE = "downloadQueue";
12
+ export declare const MESSAGES_TABLE = "messages";
13
+ export declare class IDBStore implements IStore<URL> {
14
+ seed: IDBSeedStore;
15
+ hosts: IDBHostStore;
16
+ uploads: IDBUploadQueue;
17
+ downloads: IDBDownloadQueue;
18
+ messages: IDBMessageStore;
19
+ db: IDBDatabase;
20
+ constructor(db: IDBDatabase, crypto: ICrypto);
21
+ wipe(): Promise<void>;
22
+ }
23
+ export declare function openIDBStore(): Promise<IDBDatabase>;
@@ -0,0 +1,11 @@
1
+ import { Hash } from "../../shared/types";
2
+ import { IUploadQueue } from "../../types";
3
+ export declare class IDBUploadQueue implements IUploadQueue {
4
+ db: IDBDatabase;
5
+ constructor(db: IDBDatabase);
6
+ enq(host: string, hshs: Iterable<Hash>): Promise<void>;
7
+ deq(host: string, hshs: Iterable<Hash>): Promise<void>;
8
+ list(host: string): Promise<Hash[]>;
9
+ count(): Promise<number>;
10
+ wipe(): Promise<void>;
11
+ }
@@ -0,0 +1,9 @@
1
+ import { IDownloadMessage, IDownloadQueue } from "../../types";
2
+ export declare class MemoryDownloadQueue implements IDownloadQueue {
3
+ queue: Map<string, IDownloadMessage>;
4
+ enq(msgs: Iterable<IDownloadMessage>): Promise<void>;
5
+ deq(host: string, seqs: Iterable<number>): Promise<void>;
6
+ list(): Promise<IDownloadMessage[]>;
7
+ count(): Promise<number>;
8
+ wipe(): Promise<void>;
9
+ }
@@ -0,0 +1,13 @@
1
+ import { Status } from "../../shared/consts";
2
+ import { HostHandle, IHostConnectionInfo, IHostMetadata } from "../../shared/types";
3
+ import type { IHostRow, IHostStore } from "../../types";
4
+ export declare class MemoryHostStore<Handle extends HostHandle> implements IHostStore<Handle> {
5
+ hosts: Map<string, IHostRow<Handle>>;
6
+ add(info: IHostConnectionInfo<Handle>): Promise<void>;
7
+ touch(label: string, seq: number): Promise<void>;
8
+ get(label: string): Promise<IHostRow<Handle> | undefined>;
9
+ set(label: string, meta: IHostMetadata): Promise<Status.Success | Status.NotFound>;
10
+ del(label: string): Promise<void>;
11
+ list(): Promise<MapIterator<IHostRow<Handle>>>;
12
+ wipe(): Promise<void>;
13
+ }
@@ -0,0 +1,19 @@
1
+ import { ICrypto } from "../../shared/types";
2
+ import { EntityID, Hash } from "../../shared/types";
3
+ import { IMessageStore, IStoredMessage, IStoredMessageData } from "../../types";
4
+ import { Status } from "../../shared/consts";
5
+ export declare class MemoryMessageStore implements IMessageStore {
6
+ private crypto;
7
+ messages: Map<string, IStoredMessageData>;
8
+ constructor(crypto: ICrypto);
9
+ add(messages: {
10
+ key: Hash;
11
+ data: IStoredMessageData;
12
+ }[]): Promise<Status[]>;
13
+ del(keys: Iterable<Hash>): Promise<void>;
14
+ get(key: Hash): Promise<IStoredMessage | undefined>;
15
+ has(key: Hash): Promise<boolean>;
16
+ list(): Promise<Iterable<IStoredMessage>>;
17
+ last(eid: EntityID): Promise<IStoredMessage | undefined>;
18
+ wipe(): Promise<void>;
19
+ }
@@ -0,0 +1,9 @@
1
+ import { Enclave } from "../../shared/enclave";
2
+ import { MasterSeed } from "../../shared/types";
3
+ import { ISeedStore } from "../../types";
4
+ export declare class MemorySeedStore implements ISeedStore {
5
+ enclave?: Enclave;
6
+ save(seed: MasterSeed): Promise<Enclave>;
7
+ load(): Promise<Enclave | undefined>;
8
+ wipe(): Promise<void>;
9
+ }
@@ -0,0 +1,16 @@
1
+ import { HostHandle, ICrypto } from "../../shared/types";
2
+ import { IStore } from "../../types";
3
+ import { MemoryDownloadQueue } from "./dnlds";
4
+ import { MemoryHostStore } from "./hosts";
5
+ import { MemoryMessageStore } from "./msgs";
6
+ import { MemorySeedStore } from "./seed";
7
+ import { MemoryUploadQueue } from "./uplds";
8
+ export declare class MemoryStore<Handle extends HostHandle> implements IStore<Handle> {
9
+ seed: MemorySeedStore;
10
+ hosts: MemoryHostStore<Handle>;
11
+ uploads: MemoryUploadQueue;
12
+ downloads: MemoryDownloadQueue;
13
+ messages: MemoryMessageStore;
14
+ constructor(crypto: ICrypto);
15
+ wipe(): Promise<void>;
16
+ }
@@ -0,0 +1,10 @@
1
+ import { Hash } from "../../shared/types";
2
+ import { IUploadQueue } from "../../types";
3
+ export declare class MemoryUploadQueue implements IUploadQueue {
4
+ queue: Map<string, Set<string>>;
5
+ enq(host: string, hshs: Iterable<Hash>): Promise<void>;
6
+ deq(host: string, hshs: Iterable<Hash>): Promise<void>;
7
+ list(host: string): Promise<Hash[]>;
8
+ count(): Promise<number>;
9
+ wipe(): Promise<void>;
10
+ }
package/dist/sync.d.ts CHANGED
@@ -1,2 +1,32 @@
1
- import type DiplomaticClient from "./client";
2
- export declare function usePollingSync(client: DiplomaticClient, intervalMillis: number): void;
1
+ import DiplomaticClientAPI from "./shared/client";
2
+ import { IClock } from "./shared/clock";
3
+ import { IBagPeekItem } from "./shared/codecs/peekItem";
4
+ import { Status } from "./shared/consts";
5
+ import { Enclave } from "./shared/enclave";
6
+ import { HostHandle, HostSpecificKeyPair, ICrypto } from "./shared/types";
7
+ import { ValStat } from "./shared/valstat";
8
+ 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
+ export interface ISyncParams<Handle extends HostHandle> {
15
+ conn: DiplomaticClientAPI<Handle>;
16
+ store: IStore<Handle>;
17
+ enclave: Enclave;
18
+ clock: IClock;
19
+ host: IHostRow<Handle>;
20
+ crypto: ICrypto;
21
+ }
22
+ export declare function syncPeek<Handle extends HostHandle>({ conn, store, enclave, host, crypto }: ISyncParams<Handle>): Promise<Status>;
23
+ export declare function syncPush<Handle extends HostHandle>({ conn, store, host }: ISyncParams<Handle>): Promise<Status>;
24
+ export declare function syncPull<Handle extends HostHandle>({ conn, store, enclave, host, crypto }: ISyncParams<Handle>, apply: (parts: IMsgParts[], options?: {
25
+ enqueueUpload: boolean;
26
+ triggerUpload: boolean;
27
+ }) => Promise<Status[]>): Promise<Status>;
28
+ export declare function msg2StoredMsgData({ head, body }: IMsgParts): IStoredMessageData;
29
+ export declare function handleNotif<Handle extends HostHandle>(bytes: Uint8Array, { conn, store, enclave, host, crypto, clock }: ISyncParams<Handle>, apply: (parts: IMsgParts[], options?: {
30
+ enqueueUpload: boolean;
31
+ triggerUpload: boolean;
32
+ }) => Promise<Status[]>, scheduleSync: () => void): Promise<void>;
package/dist/types.d.ts CHANGED
@@ -1,31 +1,12 @@
1
- import type { IDeltaListItem, IOp } from "./shared/types";
2
- export interface IClientStateStore {
3
- init?: () => Promise<void>;
4
- getSeed: () => Promise<Uint8Array | undefined>;
5
- setSeed: (seed: Uint8Array) => Promise<void>;
6
- getHostURL: () => Promise<string | undefined>;
7
- setHostURL: (url: string) => Promise<void>;
8
- getHostID: () => Promise<string | undefined>;
9
- setHostID: (id: string) => Promise<void>;
10
- setLastFetchedAt: (ts: Date) => Promise<void>;
11
- getLastFetchedAt: () => Promise<Date | undefined>;
12
- wipe: () => Promise<void>;
13
- enqueueUpload: (sha256: Uint8Array, cipherOp: Uint8Array) => Promise<void>;
14
- dequeueUpload: (sha256: Uint8Array) => Promise<void>;
15
- peekUpload: (sha256: Uint8Array) => Promise<Uint8Array | undefined>;
16
- listUploads: () => Promise<Uint8Array[]>;
17
- numUploads: () => Promise<number>;
18
- enqueueDownload: (sha256: Uint8Array, recordedAt: Date) => Promise<void>;
19
- dequeueDownload: (sha256: Uint8Array) => Promise<void>;
20
- listDownloads: () => Promise<IDeltaListItem[]>;
21
- numDownloads: () => Promise<number>;
22
- storeOp: (sha256: Uint8Array, cipherOp: Uint8Array) => Promise<void>;
23
- clearOp: (sha256: Uint8Array) => Promise<void>;
24
- listOps: () => Promise<Array<{
25
- cipherOp: Uint8Array;
26
- sha256: string;
27
- }>>;
28
- hasOp: (sha256: Uint8Array) => Promise<boolean>;
1
+ import { Status } from "./shared/consts";
2
+ import type { Enclave } from "./shared/enclave";
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";
5
+ import { ValStat } from "./shared/valstat";
6
+ import { ICrypto } from "./shared/types";
7
+ export interface IMsgParts {
8
+ head: IMessageHead;
9
+ body?: EncodedMessage;
29
10
  }
30
11
  export interface IDiplomaticClientState {
31
12
  hasSeed: boolean;
@@ -36,4 +17,107 @@ export interface IDiplomaticClientXferState {
36
17
  numUploads: number;
37
18
  numDownloads: number;
38
19
  }
39
- export type Applier = (op: IOp) => Promise<void>;
20
+ export type Applier = (ops: IOp[]) => Promise<{
21
+ stats: Status[];
22
+ types: Set<string>;
23
+ }>;
24
+ export interface ISeedStore {
25
+ save: (seed: MasterSeed) => Promise<Enclave>;
26
+ load: () => Promise<Enclave | void>;
27
+ wipe: () => Promise<void>;
28
+ }
29
+ export interface IHostRow<Handle extends HostHandle> extends IHostConnectionInfo<Handle>, Partial<IHostMetadata> {
30
+ lastSeq: number;
31
+ }
32
+ export interface IHostStore<Handle extends HostHandle> {
33
+ add: (host: IHostConnectionInfo<Handle>) => Promise<void>;
34
+ get: (label: string) => Promise<IHostRow<Handle> | undefined>;
35
+ del: (label: string) => Promise<void>;
36
+ set: (label: string, meta: IHostMetadata) => Promise<Status>;
37
+ list: () => Promise<Iterable<IHostRow<Handle>>>;
38
+ wipe: () => Promise<void>;
39
+ touch: (label: string, seq: number) => Promise<void>;
40
+ }
41
+ export interface IUploadQueue {
42
+ enq: (host: string, hshs: Iterable<Hash>) => Promise<void>;
43
+ deq: (host: string, hshs: Iterable<Hash>) => Promise<void>;
44
+ list: (host: string) => Promise<Hash[]>;
45
+ count: () => Promise<number>;
46
+ wipe(): Promise<void>;
47
+ }
48
+ export interface IDownloadMessage {
49
+ kdm: Uint8Array;
50
+ seq: number;
51
+ head: IMessageHead;
52
+ host: string;
53
+ }
54
+ export interface IDownloadQueue {
55
+ enq: (msgs: Iterable<IDownloadMessage>) => Promise<void>;
56
+ deq: (host: string, seqs: Iterable<number>) => Promise<void>;
57
+ list: () => Promise<Iterable<IDownloadMessage>>;
58
+ count: () => Promise<number>;
59
+ wipe(): Promise<void>;
60
+ }
61
+ export interface IStorableMessage {
62
+ key: Hash;
63
+ data: IStoredMessageData;
64
+ }
65
+ export interface IStoredMessage {
66
+ hash: Hash;
67
+ head: IMessageHead;
68
+ body?: EncodedMessage;
69
+ }
70
+ export interface IStoredMessageData {
71
+ eid: EntityID;
72
+ off?: number;
73
+ ctr?: number;
74
+ body?: EncodedMessage;
75
+ }
76
+ export declare function toStoredMessage(hash: Hash, data: IStoredMessageData, crypto: ICrypto): Promise<IStoredMessage>;
77
+ export interface IMessageStore {
78
+ add: (messages: IStorableMessage[]) => Promise<Status[]>;
79
+ get: (key: Hash) => Promise<IStoredMessage | undefined>;
80
+ has: (key: Hash) => Promise<boolean>;
81
+ del: (keys: Iterable<Hash>) => Promise<void>;
82
+ list: () => Promise<Iterable<IStoredMessage>>;
83
+ last: (eid: EntityID) => Promise<IStoredMessage | undefined>;
84
+ wipe(): Promise<void>;
85
+ }
86
+ export interface IStore<Handle extends HostHandle> {
87
+ seed: ISeedStore;
88
+ hosts: IHostStore<Handle>;
89
+ uploads: IUploadQueue;
90
+ downloads: IDownloadQueue;
91
+ messages: IMessageStore;
92
+ wipe(): Promise<void>;
93
+ }
94
+ type UnlistenFunc = () => void;
95
+ export interface IStateEmitter<T> {
96
+ get(): Promise<T>;
97
+ emit(): void;
98
+ listen(listener: (state: T) => void): UnlistenFunc;
99
+ }
100
+ export interface IClient<Handle extends HostHandle> {
101
+ setSeed(seed: MasterSeed): Promise<void>;
102
+ link(host: IHostConnectionInfo<Handle>): Promise<void>;
103
+ unlink(label: string): Promise<void>;
104
+ connect(): Promise<void>;
105
+ disconnect(): Promise<void>;
106
+ insertRaw(content: SerializedContent): Promise<ValStat<IMessageHead>>;
107
+ upsertRaw(eid: EntityID, content: SerializedContent, force?: boolean): Promise<ValStat<IMessageHead>>;
108
+ insert<T = unknown>(op: IInsertParams<T>): Promise<ValStat<IMessageHead>>;
109
+ upsert<T = unknown>(op: IUpsertParams<T>, force?: boolean): Promise<ValStat<IMessageHead>>;
110
+ delete(eid: EntityID): Promise<ValStat<IMessageHead>>;
111
+ sync(): Promise<Status>;
112
+ wipe(): Promise<void>;
113
+ import(file: File): Promise<Status>;
114
+ export(filename: string, extension?: string): Promise<Status>;
115
+ clientState: IStateEmitter<IDiplomaticClientState>;
116
+ xferState: IStateEmitter<IDiplomaticClientXferState>;
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
+ export {};