@drakkar.software/starfish-client 3.0.0-alpha.21 → 3.0.0-alpha.22

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,4 @@
1
+ export declare function hkdfBytes(ikm: Uint8Array, salt: Uint8Array, info: Uint8Array, lengthBytes?: number): Promise<Uint8Array>;
2
+ export declare function bytesToHex(bytes: Uint8Array): string;
3
+ export declare function hexToBytes(hex: string): Uint8Array;
4
+ export declare function concat(...parts: Uint8Array[]): Uint8Array;
@@ -0,0 +1,267 @@
1
+ import { verifyAppendAuthor, } from "@drakkar.software/starfish-protocol";
2
+ /** The `/pull/` action prefix; mirrors `PUSH_PATH_PREFIX` for the read side. */
3
+ const PULL_PATH_PREFIX = "/pull/";
4
+ /** The storage `documentKey` for a pull `path`: the path with the `/pull/`
5
+ * action prefix stripped (the namespace lives only in the URL). The author
6
+ * signature binds to this key, so a reader re-derives it the same way the
7
+ * writer did from `/push/…`. */
8
+ function stripPullPrefix(path) {
9
+ return path.startsWith(PULL_PATH_PREFIX) ? path.slice(PULL_PATH_PREFIX.length) : path;
10
+ }
11
+ /** Thrown when an append element's author signature fails verification. */
12
+ export class AppendAuthorError extends Error {
13
+ ts;
14
+ constructor(ts) {
15
+ super(`append element author verification failed (ts=${ts})`);
16
+ this.ts = ts;
17
+ this.name = "AppendAuthorError";
18
+ }
19
+ }
20
+ /** Largest `ts` among `items`, or `0` when empty. The checkpoint for an
21
+ * append-only log is exactly this — the server returns elements with
22
+ * `ts > checkpoint`, and element timestamps are strictly increasing. */
23
+ export function checkpointOf(items) {
24
+ let max = 0;
25
+ for (const it of items)
26
+ if (it.ts > max)
27
+ max = it.ts;
28
+ return max;
29
+ }
30
+ /** Copy the optional author fields from `src` onto a fresh element with `data`. */
31
+ function withAuthor(ts, data, src) {
32
+ const out = { ts, data };
33
+ if (src.authorPubkey !== undefined)
34
+ out.authorPubkey = src.authorPubkey;
35
+ if (src.authorSignature !== undefined)
36
+ out.authorSignature = src.authorSignature;
37
+ return out;
38
+ }
39
+ /**
40
+ * A stateful cursor over an append-only collection. It owns the accumulated
41
+ * array of elements and pulls only what is new: each {@link pull} derives the
42
+ * checkpoint from the last element it holds and asks the server for elements
43
+ * with a greater `ts`.
44
+ *
45
+ * This is the incremental, stateful counterpart to the deliberately stateless
46
+ * `client.pull(path, { appendField, since })`, and the sibling of
47
+ * {@link SyncManager} for append-only logs (no merge / push-conflict
48
+ * machinery — a log only grows).
49
+ *
50
+ * The cursor accumulates every pulled element in memory; for an unboundedly
51
+ * large log, pull a bounded window with raw `client.pull(path, { last })` instead.
52
+ *
53
+ * Cold start (nothing persisted) — first `pull()` fetches the whole collection:
54
+ * ```ts
55
+ * const log = new AppendLogCursor({ client, pullPath: "/pull/events" })
56
+ * const all = await log.pull()
57
+ * ```
58
+ * Warm start (resume from persisted data) — first `pull()` fetches only newer
59
+ * elements; persistence is a round-trip of `getItems()`:
60
+ * ```ts
61
+ * const log = new AppendLogCursor({ client, pullPath: "/pull/events",
62
+ * initialItems: await store.load() })
63
+ * const fresh = await log.pull()
64
+ * await store.save(log.getItems())
65
+ * ```
66
+ * Warm start for an **E2EE** log — persist ciphertext, render decrypted:
67
+ * ```ts
68
+ * const log = new AppendLogCursor({ client, pullPath: "/pull/streamchat",
69
+ * encryptor, persistEncrypted: true, onElementError: "skip",
70
+ * initialItems: await store.load() }) // ciphertext from disk
71
+ * const history = await log.getDecryptedItems() // render persisted history
72
+ * const fresh = await log.pull() // decrypted delta
73
+ * await store.save(log.getItems()) // ciphertext back to disk
74
+ * ```
75
+ */
76
+ export class AppendLogCursor {
77
+ client;
78
+ pullPath;
79
+ appendField;
80
+ encryptor;
81
+ verifyAuthor;
82
+ onElementError;
83
+ persistEncrypted;
84
+ documentKey;
85
+ logger;
86
+ loggerName;
87
+ items;
88
+ lastCheckpoint;
89
+ /** Tail of the serialized pull chain. Concurrent `pull()` calls queue behind
90
+ * it so each runs against the checkpoint the previous one advanced — no two
91
+ * overlapping fetches read the same checkpoint and double-append a window. */
92
+ pullChain = Promise.resolve();
93
+ constructor(options) {
94
+ this.client = options.client;
95
+ this.pullPath = options.pullPath;
96
+ this.appendField = options.appendField ?? "items";
97
+ this.encryptor = options.encryptor;
98
+ this.verifyAuthor = options.verifyAuthor;
99
+ this.onElementError = options.onElementError ?? "throw";
100
+ this.persistEncrypted = options.persistEncrypted ?? false;
101
+ this.documentKey = stripPullPrefix(options.pullPath);
102
+ this.logger = options.logger;
103
+ this.loggerName =
104
+ options.loggerName ?? options.pullPath.split("/").filter(Boolean).pop() ?? options.pullPath;
105
+ const seed = options.initialItems ?? [];
106
+ const seedCheckpoint = checkpointOf(seed);
107
+ if (options.since != null) {
108
+ if (options.since < 0)
109
+ throw new Error("since must be non-negative");
110
+ if (options.since < seedCheckpoint) {
111
+ throw new Error("since must be >= the max ts of initialItems");
112
+ }
113
+ this.lastCheckpoint = options.since;
114
+ }
115
+ else {
116
+ this.lastCheckpoint = seedCheckpoint;
117
+ }
118
+ this.items = [...seed];
119
+ }
120
+ /**
121
+ * Fetch elements newer than the current checkpoint, verify + decrypt them,
122
+ * append them to the local log, and return ONLY the newly-fetched batch
123
+ * (decrypted when an `encryptor` is set).
124
+ *
125
+ * Atomic under `onElementError: "throw"` (the default): the batch is fully
126
+ * verified and decrypted into a local before any state mutation, so a
127
+ * verify/decrypt failure throws without advancing the checkpoint past elements
128
+ * that could never be re-fetched. Under `"skip"`, a failing element is dropped
129
+ * from the returned batch but the checkpoint still advances past it.
130
+ *
131
+ * Safe to call concurrently: overlapping calls are serialized internally, so
132
+ * each runs against the checkpoint the previous one advanced (no double-fetch
133
+ * of the same window). The next pull after one completes will pick up anything
134
+ * that arrived in between.
135
+ */
136
+ async pull() {
137
+ // Chain onto the previous pull (whether it resolved or rejected) so calls
138
+ // run one-at-a-time against the latest checkpoint. `pullChain` swallows
139
+ // outcomes to stay alive; the caller still sees this call's real result.
140
+ const run = this.pullChain.then(() => this.doPull(), () => this.doPull());
141
+ this.pullChain = run.then(() => undefined, () => undefined);
142
+ return run;
143
+ }
144
+ async doPull() {
145
+ this.logger?.pullStart(this.loggerName);
146
+ const start = performance.now();
147
+ try {
148
+ const since = this.lastCheckpoint;
149
+ // Omit `since` on cold start so the request carries no `?checkpoint=`.
150
+ const opts = since > 0 ? { appendField: this.appendField, since } : { appendField: this.appendField };
151
+ const raw = await this.client.pull(this.pullPath, opts);
152
+ const batch = []; // decrypted, returned to the caller
153
+ const stored = []; // what we keep in `items` (cipher- or plaintext)
154
+ let maxTs = since;
155
+ let skipped = 0;
156
+ for (const el of raw) {
157
+ // Defensive: guard a misbehaving/mocked server from making us
158
+ // double-append a held element. Gated on `since > 0` to mirror the
159
+ // server (which only filters when checkpoint > 0): on a cold start
160
+ // `since` is 0 and we must NOT drop a legitimate `ts: 0` first element.
161
+ if (since > 0 && el.ts <= since)
162
+ continue;
163
+ // Advance past every windowed element BEFORE verify/decrypt so a skipped
164
+ // element still moves the checkpoint and is never re-fetched.
165
+ if (el.ts > maxTs)
166
+ maxTs = el.ts;
167
+ let decrypted = null;
168
+ try {
169
+ this.verifyOne(el);
170
+ const data = this.encryptor ? await this.encryptor.decrypt(el.data) : el.data;
171
+ decrypted = withAuthor(el.ts, data, el);
172
+ }
173
+ catch (err) {
174
+ // "throw" rethrows here, before any state mutation below — atomic.
175
+ if (this.onElementError !== "skip")
176
+ throw err;
177
+ skipped++;
178
+ }
179
+ if (this.persistEncrypted) {
180
+ // Keep the original ciphertext envelope (even for a skipped element:
181
+ // it is valid data we simply cannot read now — a later key might).
182
+ stored.push(withAuthor(el.ts, el.data, el));
183
+ }
184
+ else if (decrypted) {
185
+ stored.push(decrypted);
186
+ }
187
+ if (decrypted)
188
+ batch.push(decrypted);
189
+ }
190
+ this.items.push(...stored);
191
+ this.lastCheckpoint = maxTs;
192
+ this.logger?.pullSuccess(this.loggerName, Math.round(performance.now() - start), skipped > 0 ? { skippedCount: skipped } : undefined);
193
+ return batch;
194
+ }
195
+ catch (err) {
196
+ this.logger?.pullError(this.loggerName, err instanceof Error ? err.message : String(err));
197
+ throw err;
198
+ }
199
+ }
200
+ /** Verify a single element's author signature over its RAW (pre-decryption)
201
+ * `data`. Throws {@link AppendAuthorError} on any failure. No-op when
202
+ * verification is disabled. */
203
+ verifyOne(el) {
204
+ if (!this.verifyAuthor)
205
+ return;
206
+ const policy = typeof this.verifyAuthor === "object" ? this.verifyAuthor : {};
207
+ const { authorPubkey, authorSignature } = el;
208
+ if (!authorPubkey || !authorSignature)
209
+ throw new AppendAuthorError(el.ts);
210
+ // Public keys are hex, which is case-insensitive — compare normalized so a
211
+ // caller passing a differently-cased `expectedAuthorPubkey` isn't falsely rejected.
212
+ if (policy.expectedAuthorPubkey &&
213
+ authorPubkey.toLowerCase() !== policy.expectedAuthorPubkey.toLowerCase()) {
214
+ throw new AppendAuthorError(el.ts);
215
+ }
216
+ void policy;
217
+ const ok = verifyAppendAuthor(this.documentKey, el.data, authorPubkey, authorSignature);
218
+ if (!ok)
219
+ throw new AppendAuthorError(el.ts);
220
+ }
221
+ /** The full accumulated log (a shallow copy), in `ts` order. Under
222
+ * `persistEncrypted` these carry CIPHERTEXT `data` (persist them as-is, then
223
+ * re-seed via `initialItems`); otherwise they carry decrypted/plaintext data. */
224
+ getItems() {
225
+ return [...this.items];
226
+ }
227
+ /**
228
+ * The full accumulated log, DECRYPTED — for rendering warm-started history in
229
+ * `persistEncrypted` mode (where {@link getItems} holds ciphertext). Honors
230
+ * `onElementError` (a `"skip"` cursor drops elements it cannot read). When the
231
+ * cursor has no `encryptor`, or is not in `persistEncrypted` mode, the held
232
+ * elements are already plaintext/decrypted and are returned as-is.
233
+ */
234
+ async getDecryptedItems() {
235
+ const snapshot = [...this.items];
236
+ if (!this.encryptor || !this.persistEncrypted)
237
+ return snapshot;
238
+ const out = [];
239
+ for (const el of snapshot) {
240
+ try {
241
+ this.verifyOne(el);
242
+ const data = await this.encryptor.decrypt(el.data);
243
+ out.push(withAuthor(el.ts, data, el));
244
+ }
245
+ catch (err) {
246
+ if (this.onElementError !== "skip")
247
+ throw err;
248
+ }
249
+ }
250
+ return out;
251
+ }
252
+ /** The current checkpoint: the max `ts` held (the next pull's `since`). `0`
253
+ * when nothing has been pulled or seeded. */
254
+ getCheckpoint() {
255
+ return this.lastCheckpoint;
256
+ }
257
+ /** Restore the checkpoint without seeding items — for persistence layers that
258
+ * store only the checkpoint. Used to resume incrementally across restarts.
259
+ * Rejects a value below the max `ts` already held: rewinding would make the
260
+ * next pull re-deliver, and duplicate, elements the cursor already has. */
261
+ setCheckpoint(ts) {
262
+ if (ts < checkpointOf(this.items)) {
263
+ throw new Error("checkpoint must be >= the max ts already held");
264
+ }
265
+ this.lastCheckpoint = ts;
266
+ }
267
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Phase-2 transitional shim. `mintDeviceCap` + `scopes.rootAll` now live in
3
+ * `@drakkar.software/starfish-identities`; `mintMemberCap` +
4
+ * `scopes.readOnly`/`scopes.writer`/`scopes.admin` now live in
5
+ * `@drakkar.software/starfish-sharing`. The `scopes` re-export here merges
6
+ * both so existing imports of `import { scopes } from
7
+ * "../src/cap-mint.js"` keep working. Removed in Phase 3.
8
+ */
9
+ import { type ScopePreset as IdentityScopePreset, type MintOpts as IdentityMintOpts } from "@drakkar.software/starfish-identities";
10
+ export { mintDeviceCap } from "@drakkar.software/starfish-identities";
11
+ export { mintMemberCap } from "@drakkar.software/starfish-sharing";
12
+ /** @deprecated Phase-2 transitional facade. Use the per-package `scopes` directly. */
13
+ export declare const scopes: {
14
+ readOnly: (c: string) => import("@drakkar.software/starfish-sharing").ScopePreset;
15
+ writer: (c: string) => import("@drakkar.software/starfish-sharing").ScopePreset;
16
+ admin: (c: string) => import("@drakkar.software/starfish-sharing").ScopePreset;
17
+ rootAll: () => IdentityScopePreset;
18
+ };
19
+ export type ScopePreset = IdentityScopePreset;
20
+ export type MintOpts = IdentityMintOpts;
@@ -0,0 +1,12 @@
1
+ // src/cap-mint.ts
2
+ import { scopes as identityScopes } from "@drakkar.software/starfish-identities";
3
+ import { scopes as sharingScopes } from "@drakkar.software/starfish-sharing";
4
+ import { mintDeviceCap } from "@drakkar.software/starfish-identities";
5
+ import { mintMemberCap } from "@drakkar.software/starfish-sharing";
6
+ var scopes = { ...identityScopes, ...sharingScopes };
7
+ export {
8
+ mintDeviceCap,
9
+ mintMemberCap,
10
+ scopes
11
+ };
12
+ //# sourceMappingURL=cap-mint.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/cap-mint.ts"],
4
+ "sourcesContent": ["/**\n * Phase-2 transitional shim. `mintDeviceCap` + `scopes.rootAll` now live in\n * `@drakkar.software/starfish-identities`; `mintMemberCap` +\n * `scopes.readOnly`/`scopes.writer`/`scopes.admin` now live in\n * `@drakkar.software/starfish-sharing`. The `scopes` re-export here merges\n * both so existing imports of `import { scopes } from\n * \"../src/cap-mint.js\"` keep working. Removed in Phase 3.\n */\nimport { scopes as identityScopes, type ScopePreset as IdentityScopePreset, type MintOpts as IdentityMintOpts } from \"@drakkar.software/starfish-identities\"\nimport { scopes as sharingScopes } from \"@drakkar.software/starfish-sharing\"\n\nexport { mintDeviceCap } from \"@drakkar.software/starfish-identities\"\nexport { mintMemberCap } from \"@drakkar.software/starfish-sharing\"\n\n/** @deprecated Phase-2 transitional facade. Use the per-package `scopes` directly. */\nexport const scopes = { ...identityScopes, ...sharingScopes }\n\nexport type ScopePreset = IdentityScopePreset\nexport type MintOpts = IdentityMintOpts\n"],
5
+ "mappings": ";AAQA,SAAS,UAAU,sBAAkG;AACrH,SAAS,UAAU,qBAAqB;AAExC,SAAS,qBAAqB;AAC9B,SAAS,qBAAqB;AAGvB,IAAM,SAAS,EAAE,GAAG,gBAAgB,GAAG,cAAc;",
6
+ "names": []
7
+ }