@drakkar.software/starfish-client 3.0.0-alpha.4 → 3.0.0-alpha.40

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 (46) hide show
  1. package/README.md +59 -0
  2. package/dist/append-log.d.ts +228 -0
  3. package/dist/append-log.js +267 -0
  4. package/dist/background-sync.js +29 -0
  5. package/dist/bindings/legend.d.ts +23 -0
  6. package/dist/bindings/legend.js +32 -0
  7. package/dist/bindings/legend.js.map +2 -2
  8. package/dist/bindings/suspense.js +49 -0
  9. package/dist/bindings/zustand.d.ts +167 -2
  10. package/dist/bindings/zustand.js +963 -87
  11. package/dist/bindings/zustand.js.map +4 -4
  12. package/dist/client.d.ts +283 -5
  13. package/dist/client.js +391 -0
  14. package/dist/config.d.ts +9 -0
  15. package/dist/config.js +18 -0
  16. package/dist/debounced-sync.js +120 -0
  17. package/dist/dedup.js +35 -0
  18. package/dist/events.d.ts +150 -0
  19. package/dist/events.js +116 -0
  20. package/dist/events.js.map +7 -0
  21. package/dist/export.js +115 -0
  22. package/dist/fetch.d.ts +40 -0
  23. package/dist/fetch.js +51 -14
  24. package/dist/fetch.js.map +2 -2
  25. package/dist/history.js +61 -0
  26. package/dist/index.d.ts +14 -7
  27. package/dist/index.js +1025 -99
  28. package/dist/index.js.map +4 -4
  29. package/dist/kv-cache.d.ts +63 -0
  30. package/dist/logger.d.ts +3 -0
  31. package/dist/logger.js +80 -0
  32. package/dist/migrate.js +38 -0
  33. package/dist/mobile-lifecycle.d.ts +28 -1
  34. package/dist/mobile-lifecycle.js +94 -0
  35. package/dist/multi-store.js +92 -0
  36. package/dist/mutate.d.ts +39 -0
  37. package/dist/polling.js +52 -0
  38. package/dist/resolvers.js +223 -0
  39. package/dist/service-worker.js +55 -0
  40. package/dist/storage/indexeddb.js +59 -0
  41. package/dist/sync.d.ts +83 -0
  42. package/dist/sync.js +181 -0
  43. package/dist/types.d.ts +115 -9
  44. package/dist/types.js +18 -0
  45. package/dist/validate.js +28 -0
  46. package/package.json +12 -3
package/dist/client.d.ts CHANGED
@@ -1,5 +1,17 @@
1
1
  import type { PullResult, PushSuccess } from "@drakkar.software/starfish-protocol";
2
+ import { type AppendAuthor } from "@drakkar.software/starfish-protocol";
2
3
  import type { StarfishClientOptions } from "./types.js";
4
+ /**
5
+ * Whether a {@link PullResult} was served from the offline read-through cache
6
+ * (the transport was unreachable) rather than a live server response. Used by
7
+ * {@link SyncManager} to surface a `stale` flag to the UI without treating a
8
+ * cache hit as proof the server is reachable.
9
+ */
10
+ export declare function pullWasFromCache(result: PullResult): boolean;
11
+ /** The storage `documentKey` for a push `path`: the path with the `/push/`
12
+ * action prefix stripped (the namespace lives only in the URL). The author
13
+ * signature binds to this key. */
14
+ export declare function stripPushPrefix(path: string): string;
3
15
  /** Result of pulling a binary blob from the server. */
4
16
  export interface BlobPullResult {
5
17
  data: ArrayBuffer;
@@ -19,6 +31,13 @@ export interface AppendPullOptions {
19
31
  since?: number;
20
32
  /** Return only the last K items (applied after `since` filter). Sent as `?last=`. */
21
33
  last?: number;
34
+ /** Return only the last K items. Alias of `last`; sent as `?limit=`. When both
35
+ * are given, `limit` wins. */
36
+ limit?: number;
37
+ /** Explicitly fetch the whole collection (sent as `?full=true`). Mutually
38
+ * exclusive with `since`/`limit`/`last` — the server requires a pull to declare
39
+ * exactly one of {checkpoint, limit/last, full}. */
40
+ full?: boolean;
22
41
  }
23
42
  /**
24
43
  * Options for a structured (non-append) pull.
@@ -35,6 +54,66 @@ export interface PullOptions {
35
54
  checkpoint?: number;
36
55
  /** Include the sibling `_keyring` document in the response. Defaults to false. */
37
56
  withKeyring?: boolean;
57
+ /**
58
+ * Serve the last-synced cached snapshot immediately (tagged via
59
+ * {@link pullWasFromCache}) and revalidate in the background. Requires a
60
+ * {@link StarfishClientOptions.cache} to be configured; without one the option
61
+ * is a no-op and the pull goes to the network as usual.
62
+ *
63
+ * On a cache hit: returns the stale snapshot at once, kicks a background fetch,
64
+ * and on success writes the fresh snapshot to cache and fires
65
+ * {@link StarfishClientOptions.onRevalidated}. Uses the same dedup set as the
66
+ * {@link StarfishClientOptions.cacheFallbackStatuses} revalidation path — a
67
+ * concurrent error-triggered loop for the same document is not duplicated.
68
+ *
69
+ * On a cache miss: falls through to the normal network-first pull unchanged.
70
+ */
71
+ staleWhileRevalidate?: boolean;
72
+ }
73
+ /** Per-collection result in a {@link BatchPullResult}: either the pulled
74
+ * document (`data`/`hash`/`timestamp`) or a per-collection `error` string. */
75
+ export interface BatchPullEntry {
76
+ data?: unknown;
77
+ hash?: string;
78
+ timestamp?: number;
79
+ error?: string;
80
+ }
81
+ /** Response of {@link StarfishClient.batchPull}: a map of requested collection
82
+ * name → an ARRAY of {@link BatchPullEntry}, one per requested param-set, in
83
+ * request order. A collection read with no params yields a one-element array. */
84
+ export interface BatchPullResult {
85
+ collections: Record<string, BatchPullEntry[]>;
86
+ }
87
+ /** Options for {@link StarfishClient.batchPull}. */
88
+ export interface BatchPullOptions {
89
+ /** Per-collection path params: collection name → an ARRAY of param-sets, one
90
+ * per document to read from that collection, e.g.
91
+ * `{ profile: [{ identity: "a" }, { identity: "b" }] }` reads two profiles in
92
+ * one round-trip. Serialized to a URL-encoded JSON `params` query. The
93
+ * `{identity}` param is auto-filled by the server from the authenticated
94
+ * caller when a set omits it, so a single self-doc read can pass `[{}]` — or
95
+ * omit the collection from `params` entirely (an unlisted collection reads one
96
+ * auto-filled doc). Results come back under the same name in request order. */
97
+ params?: Record<string, Record<string, string>[]>;
98
+ /**
99
+ * Per-collection append options, index-aligned to `params`. Makes the batch
100
+ * request **append/checkpoint-aware**: each entry returns the bounded tail of
101
+ * that collection's append-only log rather than the full document.
102
+ *
103
+ * Serialized as URL-encoded JSON alongside `params`. Server ignores it for
104
+ * collections that are not append-only (returns `{ error: "append_params_not_supported" }`
105
+ * for those entries). `full` is disallowed in batch (`full_not_allowed` per entry).
106
+ *
107
+ * Example — read the last 5 events for two rooms and the newest item for a third:
108
+ * ```ts
109
+ * await client.batchPull(["events"], {
110
+ * params: { events: [{ room: "a" }, { room: "b" }, { room: "c" }] },
111
+ * appendParams: { events: [{ last: 5 }, { last: 5 }, { last: 1 }] },
112
+ * })
113
+ * ```
114
+ * Each `data[appendField]` in the result is the filtered array for that entry.
115
+ */
116
+ appendParams?: Record<string, AppendPullOptions[]>;
38
117
  }
39
118
  /**
40
119
  * Low-level HTTP client for the Starfish sync protocol.
@@ -42,14 +121,32 @@ export interface PullOptions {
42
121
  */
43
122
  export declare class StarfishClient {
44
123
  private readonly baseUrl;
124
+ private readonly namespace?;
45
125
  private readonly capProvider?;
46
126
  private readonly fetch;
127
+ private readonly cache?;
128
+ private readonly cacheMaxAgeMs?;
129
+ private readonly cacheFallbackStatuses?;
130
+ private readonly onRevalidated?;
131
+ private readonly revalidating;
132
+ /**
133
+ * In-memory mirror of the latest document timestamp written to each cache
134
+ * key via {@link writeCache}. Updated synchronously so {@link revalidateLoop}
135
+ * can guard against stale overwrites without an extra async cache read.
136
+ */
137
+ private readonly latestCacheTimestamp;
47
138
  /**
48
139
  * Installed client-side plugins. Currently stored as inert data; no
49
140
  * hooks fire yet. Extensions can inspect this list if needed.
50
141
  */
51
142
  readonly plugins: ReadonlyArray<import("./types.js").ClientPlugin>;
52
143
  constructor(options: StarfishClientOptions);
144
+ /**
145
+ * Mark a `PullResult` as having been served from the offline read-through
146
+ * cache (transport was unreachable). Non-enumerable so it doesn't leak into
147
+ * JSON / equality / re-caching; read via {@link pullWasFromCache}.
148
+ */
149
+ private tagFromCache;
53
150
  /**
54
151
  * Resolve the host portion of the URL the client will send to. The host
55
152
  * is folded into the signed canonical input as the `h` field so the
@@ -63,6 +160,18 @@ export declare class StarfishClient {
63
160
  * empty-host case still verifies symmetrically when both sides agree.
64
161
  */
65
162
  private signingHost;
163
+ /**
164
+ * Rewrite a request path for the configured namespace. A no-op when no
165
+ * namespace is set; otherwise `/{action}/…` becomes `/v1/{namespace}/{action}/…`
166
+ * (the `/v1` protocol-version segment is part of the namespaced route, matching
167
+ * the Python client and the server's namespace mount).
168
+ *
169
+ * Applied to the path used for BOTH the signature and the URL so the canonical
170
+ * path the client signs equals the path the server reconstructs from the URL.
171
+ * Covers SDK-helper-built paths too — that's the point: a namespace-unaware
172
+ * helper passing `/push/spaces/x/_keyring` reaches `/v1/{ns}/push/spaces/x/_keyring`.
173
+ */
174
+ private applyNamespace;
66
175
  /**
67
176
  * Build auth headers for a request. When a `capProvider` is set, signs the
68
177
  * request with the device's Ed25519 private key and returns the v3 header
@@ -74,23 +183,131 @@ export declare class StarfishClient {
74
183
  * The host bound into the signature is derived from `baseUrl` once per call.
75
184
  */
76
185
  private buildAuthHeaders;
186
+ /**
187
+ * Build the request-signing headers from an ALREADY-fetched cap context. Split
188
+ * out of {@link buildAuthHeaders} so {@link append} can fetch the cap once and
189
+ * reuse it for BOTH the author signature (over the element data) and the
190
+ * request signature (over the body), without redeeming the cap twice — a
191
+ * second `getCap()` could rotate keys and break the `authorPubkey ===
192
+ * presenter` bind the server checks.
193
+ */
194
+ private capRequestHeaders;
195
+ /**
196
+ * Resolve the author public key to attach to a signed append: the redeemer's
197
+ * `pubHex` for an audience cap, else the cert subject `cap.sub` for a
198
+ * device/member cap. This is the SAME key that signs the request, so a server
199
+ * enforcing author proof can bind the stored element to its writer. Returns
200
+ * undefined only for a (malformed) cap with neither — the append then goes
201
+ * unsigned and a server requiring signatures rejects it.
202
+ */
203
+ private appendAuthorKey;
77
204
  /** Pull synced data from the server. Returns the raw `PullResult`. */
78
205
  pull(path: string, checkpoint?: number): Promise<PullResult>;
79
206
  /** Pull synced data with structured options (e.g. `{withKeyring: true}`). */
80
207
  pull(path: string, options: PullOptions): Promise<PullResult>;
81
208
  /** Pull an append-only collection. Extracts and returns `data[appendField]` as `T[]`. */
82
209
  pull<T = unknown>(path: string, options: AppendPullOptions): Promise<T[]>;
210
+ /**
211
+ * Write a pull snapshot to the cache. Fire-and-forget; errors are swallowed
212
+ * so a failing cache never blocks the caller. No-op when no cache is configured.
213
+ */
214
+ private writeCache;
215
+ /** Build the URL + auth headers for one revalidation GET. Shared between
216
+ * {@link pull} and {@link revalidateLoop} to avoid duplicated fetch setup. */
217
+ private revalidateFetch;
218
+ /**
219
+ * Deduplicated fire-and-forget: starts one revalidation loop per cacheKey.
220
+ * Used by both the {@link cacheFallbackStatuses} error path (delayed first
221
+ * attempt, honoring `Retry-After`) and the {@link PullOptions.staleWhileRevalidate}
222
+ * read path (`immediate: true` — no initial delay on the first attempt). The
223
+ * `revalidating` set deduplicates across both triggers so a concurrent
224
+ * error-triggered loop and an SWR-on-read loop for the same key collapse to one.
225
+ */
226
+ private scheduleRevalidate;
227
+ /**
228
+ * Background revalidation loop shared by both {@link cacheFallbackStatuses}
229
+ * hits and {@link PullOptions.staleWhileRevalidate} reads.
230
+ *
231
+ * Retries (honoring `Retry-After`) up to {@link MAX_REVALIDATE_ATTEMPTS} times.
232
+ * When `immediate` is true the first attempt fires without any initial delay
233
+ * (SWR-on-read path). On a live 2xx the fresh snapshot is written to cache and
234
+ * {@link onRevalidated} fires. Stops early on a non-fallback status (403/404).
235
+ */
236
+ private revalidateLoop;
237
+ /**
238
+ * Read the cached snapshot for a document `path` WITHOUT hitting the network —
239
+ * the basis for cache-first paint (seed the UI from the last-synced snapshot,
240
+ * then revalidate with a live {@link pull}). Returns the tagged `PullResult`,
241
+ * or null when no cache is configured / there's no entry. Namespacing matches
242
+ * {@link pull}, so the key lines up with whatever `pull` wrote.
243
+ */
244
+ peekCache(path: string): Promise<PullResult | null>;
245
+ /** Read + parse a cached pull snapshot, tagged {@link tagFromCache}. Returns
246
+ * null on a miss or an unparseable blob (never throws — a corrupt cache entry
247
+ * must not break a pull, just miss). */
248
+ private readCache;
249
+ /**
250
+ * Pull several documents in one round-trip via `/batch/pull`. `collections` is
251
+ * the list of distinct collection names; `opts.params` supplies, per collection,
252
+ * an ARRAY of path-param sets — one per document to read — so the SAME collection
253
+ * can fan in many documents (e.g. many users' `profile`) in a single request.
254
+ * The server auto-fills the `{identity}` param from the authenticated caller for
255
+ * any set that omits it, so a self-doc collection needs no params. Returns a map
256
+ * of collection name → an ARRAY of pulled documents (or per-document `{ error }`),
257
+ * in request order. Honors the configured namespace.
258
+ *
259
+ * For the common "many docs of one collection" case prefer {@link batchPullMany}.
260
+ *
261
+ * Pass `appendParams` per entry for append-only bounded-tail reads (see {@link batchPullManyAppend}).
262
+ */
263
+ batchPull(collections: string[], opts?: BatchPullOptions): Promise<BatchPullResult>;
264
+ /**
265
+ * Convenience over {@link batchPull} for reading MANY documents of ONE
266
+ * collection in a single round-trip: pass the per-document param-sets and get
267
+ * back the {@link BatchPullEntry} array aligned to `paramsList` by index (each
268
+ * entry is `{ data, hash, timestamp }` or `{ error }`). An empty `paramsList`
269
+ * issues no request and returns `[]`.
270
+ */
271
+ batchPullMany(collection: string, paramsList: Record<string, string>[]): Promise<BatchPullEntry[]>;
272
+ /**
273
+ * Convenience over {@link batchPull} for reading append-only bounded tails from
274
+ * MANY entries of ONE collection in a single round-trip.
275
+ *
276
+ * Each request in `requests` carries optional `params` (path params) and
277
+ * `options` (append bounds: `since`/`last`/`limit`/`appendField`). An empty
278
+ * `requests` issues no request and returns `[]`.
279
+ *
280
+ * Returns an array aligned to `requests` by index. Each element is either:
281
+ * - the filtered array `T[]` extracted from `entry.data[appendField]`, or
282
+ * - `{ error: string }` if the server returned a per-entry error.
283
+ *
284
+ * The `appendField` used for extraction defaults to `"items"` and can be
285
+ * overridden per request via `options.appendField`.
286
+ *
287
+ * The `appendField` option is client-side only (used for result extraction, not sent to the server).
288
+ * It must match the collection's server-configured append field and defaults to `"items"`.
289
+ *
290
+ * Note: `full: true` is not supported in batch and is rejected client-side
291
+ * before the request is sent.
292
+ */
293
+ batchPullManyAppend<T = unknown>(collection: string, requests: {
294
+ params?: Record<string, string>;
295
+ options: AppendPullOptions;
296
+ }[]): Promise<(T[] | {
297
+ error: string;
298
+ })[]>;
83
299
  /**
84
300
  * Push synced data to the server.
85
301
  * @param path - The push endpoint path (e.g. "/push/users/abc/settings")
86
302
  * @param data - The full document data to push
87
303
  * @param baseHash - Hash of the document this push is based on (null for first push)
88
304
  *
89
- * v3 author fields (`authorPubkey` + `authorSignature`) live inside `data`
90
- * and are produced by `SyncManager` when a `signer` is configured.
305
+ * v3 author proof (`authorPubkey` + `authorSignature`) is passed via `author`
306
+ * (produced by `SyncManager` when a `signer` is configured) and sent as
307
+ * top-level body siblings of `data`, where the server verifies it.
91
308
  * @throws {ConflictError} if the server detects a hash mismatch (409)
92
309
  */
93
- push(path: string, data: Record<string, unknown>, baseHash: string | null): Promise<PushSuccess>;
310
+ push(path: string, data: Record<string, unknown>, baseHash: string | null, author?: AppendAuthor): Promise<PushSuccess>;
94
311
  /**
95
312
  * Append an element to an appendOnly (`by_timestamp`) collection.
96
313
  *
@@ -105,12 +322,44 @@ export declare class StarfishClient {
105
322
  * @param opts.ts - optional client-supplied element timestamp (ms). Must be a
106
323
  * non-negative integer strictly greater than the latest stored element's ts
107
324
  * (else the server responds 409). Omit to let the server assign one.
108
- * @throws {StarfishHttpError} on a non-2xx response (e.g. 409 for a
109
- * non-monotonic timestamp).
325
+ * @throws {StarfishHttpError} on a non-2xx response e.g. 409
326
+ * `{ error: "non_monotonic_timestamp" }` for a non-monotonic timestamp, or
327
+ * `{ error: "append_limit_exceeded", limit }` if the collection's `maxItems`
328
+ * cap is reached (partition by a path parameter for higher volume).
110
329
  */
111
330
  append(path: string, data: Record<string, unknown>, opts?: {
112
331
  ts?: number;
113
332
  }): Promise<PushSuccess>;
333
+ /**
334
+ * Append one element to a **public-write** append-only collection with an
335
+ * Ed25519 author proof but **no cap `Authorization` header**.
336
+ *
337
+ * Unlike {@link append}, which always attaches a cap-signed `Authorization`
338
+ * header from the configured `capProvider`, this method signs only the
339
+ * append-author proof (binding the element to the writer's Ed25519 key) and
340
+ * sends the request without authentication headers. This is required for
341
+ * collections with `writeRoles: ["public"]` — the server's cap-scope check
342
+ * would reject a request carrying a cap whose scope does not cover the path.
343
+ *
344
+ * Typical use-case: writing a sealed invitation to another user's
345
+ * public-write inbox collection without needing a cap scoped to the
346
+ * recipient's namespace. The author proof is optional on the server side
347
+ * (`requireAuthorSignature: false` for a public inbox), but signing anyway
348
+ * binds the stored element to the sender's Ed25519 key for verification in
349
+ * the receive path.
350
+ *
351
+ * The element is sent as `{ data, authorPubkey, authorSignature }`.
352
+ *
353
+ * @param path The push path, e.g. `/push/inbox/{userId}/{shard}`.
354
+ * @param element The JSON element to append.
355
+ * @param signer The sender's Ed25519 keypair (signs the author proof).
356
+ *
357
+ * @throws {AppendHttpError} on a non-2xx response.
358
+ */
359
+ appendAnonymous(path: string, element: Record<string, unknown>, signer: {
360
+ edPubHex: string;
361
+ edPrivHex: string;
362
+ }): Promise<void>;
114
363
  /**
115
364
  * Pull binary data from a blob collection.
116
365
  * Returns raw bytes with the content hash from the ETag header.
@@ -121,4 +370,33 @@ export declare class StarfishClient {
121
370
  * Binary collections use last-write-wins (no conflict detection).
122
371
  */
123
372
  pushBlob(path: string, data: ArrayBuffer | Uint8Array | Blob, contentType: string): Promise<BlobPushResult>;
373
+ /**
374
+ * Push an Apache Parquet file to a Parquet collection.
375
+ *
376
+ * Thin wrapper over {@link pushBlob} that fixes `Content-Type` to
377
+ * `application/vnd.apache.parquet` so the S3 object is tagged correctly
378
+ * for DuckDB and CDN consumption.
379
+ *
380
+ * @example
381
+ * ```ts
382
+ * const parquetBytes = await generateParquet(rows)
383
+ * const result = await client.pushParquet("/push/analytics/alice/q1.parquet", parquetBytes)
384
+ * console.log("stored hash:", result.hash)
385
+ * ```
386
+ */
387
+ pushParquet(path: string, data: ArrayBuffer | Uint8Array | Blob): Promise<BlobPushResult>;
388
+ /**
389
+ * Pull an Apache Parquet file from a Parquet collection.
390
+ *
391
+ * Thin wrapper over {@link pullBlob} for API symmetry with
392
+ * {@link pushParquet}.
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * const result = await client.pullParquet("/pull/analytics/alice/q1.parquet")
397
+ * // result.data → ArrayBuffer
398
+ * // result.contentType → "application/vnd.apache.parquet"
399
+ * ```
400
+ */
401
+ pullParquet(path: string): Promise<BlobPullResult>;
124
402
  }