@interncom/diplomatic 0.7.1 → 0.8.1

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>>;
@@ -1,7 +1,7 @@
1
1
  import { Status } from "../../shared/consts";
2
2
  import { ICrypto } from "../../shared/types";
3
3
  import { EntityID, Hash } from "../../shared/types";
4
- import { ApldState, IMessageStore, IStorableMessage, IStoredMessage } from "../../types";
4
+ import { ApldState, IMessageStore, IStorableMessage, IStoredMessage, ListMsgsOpts } from "../../types";
5
5
  export declare class IDBMessageStore implements IMessageStore {
6
6
  private crypto;
7
7
  db: IDBDatabase;
@@ -10,7 +10,9 @@ export declare class IDBMessageStore implements IMessageStore {
10
10
  del(keys: Iterable<Hash>): Promise<void>;
11
11
  get(key: Hash): Promise<IStoredMessage | undefined>;
12
12
  has(key: Hash): Promise<boolean>;
13
- list(apld?: ApldState): Promise<IStoredMessage[]>;
13
+ list(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
14
+ count(apld?: ApldState): Promise<number>;
15
+ listKeys(): Promise<Hash[]>;
14
16
  last(eid: EntityID): Promise<IStoredMessage | undefined>;
15
17
  markApplied(keys: Iterable<Hash>): Promise<void>;
16
18
  markFailed(entries: Iterable<{
@@ -1,6 +1,6 @@
1
1
  import { ICrypto } from "../../shared/types";
2
2
  import { EntityID, Hash } from "../../shared/types";
3
- import { ApldState, IMessageStore, IStorableMessage, IStoredMessage, IStoredMessageData } from "../../types";
3
+ import { ApldState, IMessageStore, IStorableMessage, IStoredMessage, IStoredMessageData, ListMsgsOpts } from "../../types";
4
4
  import { Status } from "../../shared/consts";
5
5
  export declare class MemoryMessageStore implements IMessageStore {
6
6
  private crypto;
@@ -10,7 +10,9 @@ export declare class MemoryMessageStore implements IMessageStore {
10
10
  del(keys: Iterable<Hash>): Promise<void>;
11
11
  get(key: Hash): Promise<IStoredMessage | undefined>;
12
12
  has(key: Hash): Promise<boolean>;
13
- list(apld?: ApldState): Promise<IStoredMessage[]>;
13
+ list(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
14
+ count(apld?: ApldState): Promise<number>;
15
+ listKeys(): Promise<Hash[]>;
14
16
  last(eid: EntityID): Promise<IStoredMessage | undefined>;
15
17
  markApplied(keys: Iterable<Hash>): Promise<void>;
16
18
  markFailed(entries: Iterable<{
@@ -147,18 +147,37 @@ export declare function isTerminalApplyFailure(st: Status): boolean;
147
147
  * Clears `err` unless setting {@link APLD_ERROR}.
148
148
  */
149
149
  export declare function setApld(data: IStoredMessageData, apld: ApldState, err?: Status): void;
150
- export declare function toStoredMessage(hash: Hash, data: IStoredMessageData, crypto: ICrypto): Promise<IStoredMessage>;
150
+ /**
151
+ * Options for archive list / client diagnostics.
152
+ * `body` defaults to **true**. Pass `body: false` for heads + apld + err only.
153
+ */
154
+ export type ListMsgsOpts = {
155
+ /** Filter by apply lifecycle; omit for all archive rows. */
156
+ apld?: ApldState;
157
+ /**
158
+ * Include msg bodies and derive `head.hsh` from them.
159
+ * Default `true`. Pass `false` to omit body and `hsh` (cheaper diagnostics);
160
+ * `head.len` still reflects stored size.
161
+ */
162
+ body?: boolean;
163
+ };
164
+ export declare function toStoredMessage(hash: Hash, data: IStoredMessageData, crypto: ICrypto, opts?: {
165
+ body?: boolean;
166
+ }): Promise<IStoredMessage>;
151
167
  export interface IMessageStore {
152
168
  add: (messages: IStorableMessage[]) => Promise<Status[]>;
153
169
  get: (key: Hash) => Promise<IStoredMessage | undefined>;
154
170
  has: (key: Hash) => Promise<boolean>;
155
171
  del: (keys: Iterable<Hash>) => Promise<void>;
156
172
  /**
157
- * List archive rows. Pass {@link ApldState} to filter by apply lifecycle
158
- * (e.g. {@link APLD_PENDING} for the apply queue, {@link APLD_ERROR} for
159
- * diagnostics). Omit for all messages.
173
+ * List archive rows. Filter with `apld` (e.g. {@link APLD_ERROR}).
174
+ * Bodies included by default; pass `body: false` to omit.
160
175
  */
161
- list: (apld?: ApldState) => Promise<IStoredMessage[]>;
176
+ list: (opts?: ListMsgsOpts) => Promise<IStoredMessage[]>;
177
+ /** Count archive rows; optional apld filter (IDB index when available). */
178
+ count: (apld?: ApldState) => Promise<number>;
179
+ /** Archive keys only (head hashes). Prefer over {@link list} for checksums. */
180
+ listKeys: () => Promise<Hash[]>;
162
181
  last: (eid: EntityID) => Promise<IStoredMessage | undefined>;
163
182
  /** Mark archive rows as applied ({@link APLD_APPLIED}). */
164
183
  markApplied: (keys: Iterable<Hash>) => Promise<void>;
@@ -202,6 +221,31 @@ export interface IClient<Handle extends HostHandle> {
202
221
  /** Allocate an entity id (optional 8-byte id material; else random). */
203
222
  genEID(id?: Uint8Array): Promise<ValStat<EntityID>>;
204
223
  sync(): Promise<Status>;
224
+ /**
225
+ * Wipe application state (e.g. EntDB) and rebuild it by replaying the local
226
+ * message archive. By default, first peeks the full host header list (seq 0)
227
+ * and pulls any missing bags so the archive is complete before replay.
228
+ * Pass `{ checkHost: false }` to skip the host inventory (local-only).
229
+ */
230
+ rebuild(options?: {
231
+ checkHost?: boolean;
232
+ }): Promise<Status>;
233
+ /**
234
+ * Checksum of the local msg archive as a set of head hashes.
235
+ * Decodes store keys to raw hashes, sorts lexicographically, blake3(concat).
236
+ * Key encoding (e.g. b64 in IDB) is independent of this definition.
237
+ */
238
+ msgcheck(): Promise<Hash>;
239
+ /**
240
+ * List local archive rows for diagnostics (failed apply, pending drain, dump).
241
+ * Bodies included by default; pass `body: false` for lighter listings.
242
+ */
243
+ listMsgs(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
244
+ /**
245
+ * Count archive rows; optional apld filter.
246
+ * Prefer over list+length for badges.
247
+ */
248
+ countMsgs(apld?: ApldState): Promise<number>;
205
249
  wipe(): Promise<void>;
206
250
  import(file: File): Promise<Status>;
207
251
  export(filename: string, extension?: string): Promise<Status>;
@@ -1,8 +1,8 @@
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
- import type { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStore } from "../types";
5
+ import type { ApldState, IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStore, IStoredMessage, ListMsgsOpts } from "../types";
6
6
  /**
7
7
  * Options for attaching to an app-owned sync Worker.
8
8
  *
@@ -113,6 +113,19 @@ 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
+ /** Shared message IDB on main — no worker RPC. */
120
+ listMsgs(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
121
+ countMsgs(apld?: ApldState): Promise<number>;
122
+ /** Archive checksum via worker (listKeys + sort + blake3 off main). */
123
+ msgcheck(): Promise<Hash>;
124
+ /**
125
+ * EntDB frontier checksum off main (eid+upd+ctr). Not on IClient —
126
+ * SyncClient has no EntDB; use entDB.checksum(crypto) on the main path.
127
+ */
128
+ entcheck(): Promise<Hash>;
116
129
  wipe(): Promise<void>;
117
130
  import(file: File): Promise<Status>;
118
131
  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";
@@ -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;