@interncom/diplomatic 0.7.0 → 0.8.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.
@@ -0,0 +1,25 @@
1
+ import type { Hash, ICrypto, IEntRev } from "./types.ts";
2
+ import { type ValStat } from "./valstat.ts";
3
+ /** Lexicographic compare of raw bytes (unsigned). */
4
+ export declare function cmpBytes(a: Uint8Array, b: Uint8Array): number;
5
+ /**
6
+ * Checksum of a set of byte records (hashes, encoded revs, …).
7
+ *
8
+ * Preimage: blake3( concat( sort_lexicographic(items) ) ).
9
+ * Empty set → blake3(empty). Order of the input iterable is ignored.
10
+ *
11
+ * Equal-length items: concat is trivially unambiguous.
12
+ * Variable-length items: only unambiguous if each item is self-delimiting
13
+ * (e.g. encodeEntRev) so a concat has a unique parse.
14
+ */
15
+ export declare function checksumSet(items: Iterable<Uint8Array>, crypto: ICrypto): Promise<Hash>;
16
+ /**
17
+ * Wire preimage for one EntDB frontier row:
18
+ * varbytes(eid) ‖ date(updatedAt) ‖ varint(ctr). Self-delimiting.
19
+ */
20
+ export declare function encodeEntRev(rev: IEntRev): ValStat<Uint8Array>;
21
+ /**
22
+ * Frontier checksum of live EntDB rows: encode each rev, then
23
+ * {@link checksumSet}.
24
+ */
25
+ export declare function checksumEntRevs(revs: Iterable<IEntRev>, crypto: ICrypto): Promise<ValStat<Hash>>;
@@ -11,6 +11,7 @@ export declare class IDBMessageStore implements IMessageStore {
11
11
  get(key: Hash): Promise<IStoredMessage | undefined>;
12
12
  has(key: Hash): Promise<boolean>;
13
13
  list(apld?: ApldState): Promise<IStoredMessage[]>;
14
+ listKeys(): Promise<Hash[]>;
14
15
  last(eid: EntityID): Promise<IStoredMessage | undefined>;
15
16
  markApplied(keys: Iterable<Hash>): Promise<void>;
16
17
  markFailed(entries: Iterable<{
@@ -28,5 +28,6 @@ export declare class IDBStore implements IStore<URL> {
28
28
  constructor(db: IDBDatabase, crypto: ICrypto);
29
29
  wipe(): Promise<void>;
30
30
  }
31
+ export declare const DIPLOMATIC_STORE_DB_NAME = "diplomatic-store-db";
31
32
  export declare function openIDBStoreDB(): Promise<IDBDatabase>;
32
33
  export declare function openIDBStore(crypto: ICrypto): Promise<IDBStore>;
@@ -11,6 +11,7 @@ export declare class MemoryMessageStore implements IMessageStore {
11
11
  get(key: Hash): Promise<IStoredMessage | undefined>;
12
12
  has(key: Hash): Promise<boolean>;
13
13
  list(apld?: ApldState): Promise<IStoredMessage[]>;
14
+ listKeys(): Promise<Hash[]>;
14
15
  last(eid: EntityID): Promise<IStoredMessage | undefined>;
15
16
  markApplied(keys: Iterable<Hash>): Promise<void>;
16
17
  markFailed(entries: Iterable<{
@@ -159,6 +159,8 @@ export interface IMessageStore {
159
159
  * diagnostics). Omit for all messages.
160
160
  */
161
161
  list: (apld?: ApldState) => Promise<IStoredMessage[]>;
162
+ /** Archive keys only (head hashes). Prefer over {@link list} for checksums. */
163
+ listKeys: () => Promise<Hash[]>;
162
164
  last: (eid: EntityID) => Promise<IStoredMessage | undefined>;
163
165
  /** Mark archive rows as applied ({@link APLD_APPLIED}). */
164
166
  markApplied: (keys: Iterable<Hash>) => Promise<void>;
@@ -202,6 +204,21 @@ export interface IClient<Handle extends HostHandle> {
202
204
  /** Allocate an entity id (optional 8-byte id material; else random). */
203
205
  genEID(id?: Uint8Array): Promise<ValStat<EntityID>>;
204
206
  sync(): Promise<Status>;
207
+ /**
208
+ * Wipe application state (e.g. EntDB) and rebuild it by replaying the local
209
+ * message archive. By default, first peeks the full host header list (seq 0)
210
+ * and pulls any missing bags so the archive is complete before replay.
211
+ * Pass `{ checkHost: false }` to skip the host inventory (local-only).
212
+ */
213
+ rebuild(options?: {
214
+ checkHost?: boolean;
215
+ }): Promise<Status>;
216
+ /**
217
+ * Checksum of the local msg archive as a set of head hashes.
218
+ * Decodes store keys to raw hashes, sorts lexicographically, blake3(concat).
219
+ * Key encoding (e.g. b64 in IDB) is independent of this definition.
220
+ */
221
+ msgcheck(): Promise<Hash>;
205
222
  wipe(): Promise<void>;
206
223
  import(file: File): Promise<Status>;
207
224
  export(filename: string, extension?: string): Promise<Status>;
@@ -1,6 +1,6 @@
1
1
  import { IClock } from "../shared/clock";
2
2
  import { Status } from "../shared/consts";
3
- import type { EntityID, IDeleteParams, IEntRev, IHostConnectionInfo, IInsertParams, IMessageHead, IStateManager, IUpdateParams, MasterSeed, SerializedContent } from "../shared/types";
3
+ import type { EntityID, Hash, IDeleteParams, IEntRev, IHostConnectionInfo, IInsertParams, IMessageHead, IStateManager, IUpdateParams, MasterSeed, SerializedContent } from "../shared/types";
4
4
  import type { ValStat } from "../shared/valstat";
5
5
  import type { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStore } from "../types";
6
6
  /**
@@ -113,6 +113,16 @@ export declare class WorkerClient implements IClient<URL> {
113
113
  delete(op: IDeleteParams): Promise<ValStat<IMessageHead>>;
114
114
  genEID(id?: Uint8Array): Promise<ValStat<EntityID>>;
115
115
  sync(): Promise<Status>;
116
+ rebuild(options?: {
117
+ checkHost?: boolean;
118
+ }): Promise<Status>;
119
+ /** Archive checksum via worker (listKeys + sort + blake3 off main). */
120
+ msgcheck(): Promise<Hash>;
121
+ /**
122
+ * EntDB frontier checksum off main (eid+upd+ctr). Not on IClient —
123
+ * SyncClient has no EntDB; use entDB.checksum(crypto) on the main path.
124
+ */
125
+ entcheck(): Promise<Hash>;
116
126
  wipe(): Promise<void>;
117
127
  import(file: File): Promise<Status>;
118
128
  export(filename: string, _extension?: string): Promise<Status>;
@@ -34,6 +34,16 @@ export type WorkerCmd = {
34
34
  } | {
35
35
  id: number;
36
36
  op: "sync";
37
+ } | {
38
+ id: number;
39
+ op: "rebuild";
40
+ checkHost?: boolean;
41
+ } | {
42
+ id: number;
43
+ op: "msgcheck";
44
+ } | {
45
+ id: number;
46
+ op: "entcheck";
37
47
  } | {
38
48
  id: number;
39
49
  op: "wipe";
@@ -64,6 +74,11 @@ export type WorkerReply = {
64
74
  };
65
75
  export type WorkerEvent = {
66
76
  kind: "ready";
77
+ }
78
+ /** Worker init failed (e.g. IDB open); main should fail handshake, not hang. */
79
+ | {
80
+ kind: "initError";
81
+ message: string;
67
82
  } | {
68
83
  kind: "clientState";
69
84
  state: IDiplomaticClientState;
@@ -3,6 +3,7 @@ import type { WorkerCmd, WorkerEvent } from "./protocol";
3
3
  export type PostFn = (msg: WorkerEvent, transfer?: Transferable[]) => void;
4
4
  export declare class WorkerRuntime {
5
5
  private client;
6
+ private entDB;
6
7
  private post;
7
8
  /** Resolves when init finishes (success or failure). Cmds wait on this. */
8
9
  private whenReady;