@drakkar.software/starfish-client 3.0.0-alpha.1 → 3.0.0-alpha.11

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.
package/dist/client.d.ts CHANGED
@@ -1,5 +1,10 @@
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
+ /** The storage `documentKey` for a push `path`: the path with the `/push/`
5
+ * action prefix stripped (the namespace lives only in the URL). The author
6
+ * signature binds to this key. */
7
+ export declare function stripPushPrefix(path: string): string;
3
8
  /** Result of pulling a binary blob from the server. */
4
9
  export interface BlobPullResult {
5
10
  data: ArrayBuffer;
@@ -36,12 +41,39 @@ export interface PullOptions {
36
41
  /** Include the sibling `_keyring` document in the response. Defaults to false. */
37
42
  withKeyring?: boolean;
38
43
  }
44
+ /** Per-collection result in a {@link BatchPullResult}: either the pulled
45
+ * document (`data`/`hash`/`timestamp`) or a per-collection `error` string. */
46
+ export interface BatchPullEntry {
47
+ data?: unknown;
48
+ hash?: string;
49
+ timestamp?: number;
50
+ error?: string;
51
+ }
52
+ /** Response of {@link StarfishClient.batchPull}: a map of requested collection
53
+ * name → an ARRAY of {@link BatchPullEntry}, one per requested param-set, in
54
+ * request order. A collection read with no params yields a one-element array. */
55
+ export interface BatchPullResult {
56
+ collections: Record<string, BatchPullEntry[]>;
57
+ }
58
+ /** Options for {@link StarfishClient.batchPull}. */
59
+ export interface BatchPullOptions {
60
+ /** Per-collection path params: collection name → an ARRAY of param-sets, one
61
+ * per document to read from that collection, e.g.
62
+ * `{ profile: [{ identity: "a" }, { identity: "b" }] }` reads two profiles in
63
+ * one round-trip. Serialized to a URL-encoded JSON `params` query. The
64
+ * `{identity}` param is auto-filled by the server from the authenticated
65
+ * caller when a set omits it, so a single self-doc read can pass `[{}]` — or
66
+ * omit the collection from `params` entirely (an unlisted collection reads one
67
+ * auto-filled doc). Results come back under the same name in request order. */
68
+ params?: Record<string, Record<string, string>[]>;
69
+ }
39
70
  /**
40
71
  * Low-level HTTP client for the Starfish sync protocol.
41
72
  * Handles auth headers and response parsing.
42
73
  */
43
74
  export declare class StarfishClient {
44
75
  private readonly baseUrl;
76
+ private readonly namespace?;
45
77
  private readonly capProvider?;
46
78
  private readonly fetch;
47
79
  /**
@@ -63,6 +95,18 @@ export declare class StarfishClient {
63
95
  * empty-host case still verifies symmetrically when both sides agree.
64
96
  */
65
97
  private signingHost;
98
+ /**
99
+ * Rewrite a request path for the configured namespace. A no-op when no
100
+ * namespace is set; otherwise `/{action}/…` becomes `/v1/{namespace}/{action}/…`
101
+ * (the `/v1` protocol-version segment is part of the namespaced route, matching
102
+ * the Python client and the server's namespace mount).
103
+ *
104
+ * Applied to the path used for BOTH the signature and the URL so the canonical
105
+ * path the client signs equals the path the server reconstructs from the URL.
106
+ * Covers SDK-helper-built paths too — that's the point: a namespace-unaware
107
+ * helper passing `/push/spaces/x/_keyring` reaches `/v1/{ns}/push/spaces/x/_keyring`.
108
+ */
109
+ private applyNamespace;
66
110
  /**
67
111
  * Build auth headers for a request. When a `capProvider` is set, signs the
68
112
  * request with the device's Ed25519 private key and returns the v3 header
@@ -74,23 +118,88 @@ export declare class StarfishClient {
74
118
  * The host bound into the signature is derived from `baseUrl` once per call.
75
119
  */
76
120
  private buildAuthHeaders;
121
+ /**
122
+ * Build the request-signing headers from an ALREADY-fetched cap context. Split
123
+ * out of {@link buildAuthHeaders} so {@link append} can fetch the cap once and
124
+ * reuse it for BOTH the author signature (over the element data) and the
125
+ * request signature (over the body), without redeeming the cap twice — a
126
+ * second `getCap()` could rotate keys and break the `authorPubkey ===
127
+ * presenter` bind the server checks.
128
+ */
129
+ private capRequestHeaders;
130
+ /**
131
+ * Resolve the author public key to attach to a signed append: the redeemer's
132
+ * `pubHex` for an audience cap, else the cert subject `cap.sub` for a
133
+ * device/member cap. This is the SAME key that signs the request, so a server
134
+ * enforcing author proof can bind the stored element to its writer. Returns
135
+ * undefined only for a (malformed) cap with neither — the append then goes
136
+ * unsigned and a server requiring signatures rejects it.
137
+ */
138
+ private appendAuthorKey;
77
139
  /** Pull synced data from the server. Returns the raw `PullResult`. */
78
140
  pull(path: string, checkpoint?: number): Promise<PullResult>;
79
141
  /** Pull synced data with structured options (e.g. `{withKeyring: true}`). */
80
142
  pull(path: string, options: PullOptions): Promise<PullResult>;
81
143
  /** Pull an append-only collection. Extracts and returns `data[appendField]` as `T[]`. */
82
144
  pull<T = unknown>(path: string, options: AppendPullOptions): Promise<T[]>;
145
+ /**
146
+ * Pull several documents in one round-trip via `/batch/pull`. `collections` is
147
+ * the list of distinct collection names; `opts.params` supplies, per collection,
148
+ * an ARRAY of path-param sets — one per document to read — so the SAME collection
149
+ * can fan in many documents (e.g. many users' `profile`) in a single request.
150
+ * The server auto-fills the `{identity}` param from the authenticated caller for
151
+ * any set that omits it, so a self-doc collection needs no params. Returns a map
152
+ * of collection name → an ARRAY of pulled documents (or per-document `{ error }`),
153
+ * in request order. Honors the configured namespace.
154
+ *
155
+ * For the common "many docs of one collection" case prefer {@link batchPullMany}.
156
+ *
157
+ * Note: not append/checkpoint-aware — for incremental append-only reads use
158
+ * `pull(path, { since })` (or `AppendLogCursor`) per collection.
159
+ */
160
+ batchPull(collections: string[], opts?: BatchPullOptions): Promise<BatchPullResult>;
161
+ /**
162
+ * Convenience over {@link batchPull} for reading MANY documents of ONE
163
+ * collection in a single round-trip: pass the per-document param-sets and get
164
+ * back the {@link BatchPullEntry} array aligned to `paramsList` by index (each
165
+ * entry is `{ data, hash, timestamp }` or `{ error }`). An empty `paramsList`
166
+ * issues no request and returns `[]`.
167
+ */
168
+ batchPullMany(collection: string, paramsList: Record<string, string>[]): Promise<BatchPullEntry[]>;
83
169
  /**
84
170
  * Push synced data to the server.
85
171
  * @param path - The push endpoint path (e.g. "/push/users/abc/settings")
86
172
  * @param data - The full document data to push
87
173
  * @param baseHash - Hash of the document this push is based on (null for first push)
88
174
  *
89
- * v3 author fields (`authorPubkey` + `authorSignature`) live inside `data`
90
- * and are produced by `SyncManager` when a `signer` is configured.
175
+ * v3 author proof (`authorPubkey` + `authorSignature`) is passed via `author`
176
+ * (produced by `SyncManager` when a `signer` is configured) and sent as
177
+ * top-level body siblings of `data`, where the server verifies it.
91
178
  * @throws {ConflictError} if the server detects a hash mismatch (409)
92
179
  */
93
- push(path: string, data: Record<string, unknown>, baseHash: string | null): Promise<PushSuccess>;
180
+ push(path: string, data: Record<string, unknown>, baseHash: string | null, author?: AppendAuthor): Promise<PushSuccess>;
181
+ /**
182
+ * Append an element to an appendOnly (`by_timestamp`) collection.
183
+ *
184
+ * Unlike {@link push}, appendOnly writes carry no hash/conflict check — an
185
+ * authorized append is always accepted. Each element is stored server-side as
186
+ * `{ts, data}` and pulls can filter by `ts` via `since`/`checkpoint`.
187
+ *
188
+ * @param path - the push endpoint (e.g. "/push/events")
189
+ * @param data - the element payload. For a `delegated` collection, encrypt it
190
+ * first (e.g. `createKeyringEncryptor(keyring, kem).encrypt(data)`); the
191
+ * server stores it opaquely and never reads it.
192
+ * @param opts.ts - optional client-supplied element timestamp (ms). Must be a
193
+ * non-negative integer strictly greater than the latest stored element's ts
194
+ * (else the server responds 409). Omit to let the server assign one.
195
+ * @throws {StarfishHttpError} on a non-2xx response — e.g. 409
196
+ * `{ error: "non_monotonic_timestamp" }` for a non-monotonic timestamp, or
197
+ * `{ error: "append_limit_exceeded", limit }` if the collection's `maxItems`
198
+ * cap is reached (partition by a path parameter for higher volume).
199
+ */
200
+ append(path: string, data: Record<string, unknown>, opts?: {
201
+ ts?: number;
202
+ }): Promise<PushSuccess>;
94
203
  /**
95
204
  * Pull binary data from a blob collection.
96
205
  * Returns raw bytes with the content hash from the ETag header.
package/dist/config.d.ts CHANGED
@@ -2,12 +2,12 @@
2
2
  export type EncryptionMode = "none" | "delegated";
3
3
  /** Append-only configuration exposed via GET /config. */
4
4
  export interface AppendOnlyClientInfo {
5
+ /** Append-only strategy. Only `"by_timestamp"` is currently supported. */
6
+ type: "by_timestamp";
5
7
  /** Array field name in the stored document. Defaults to "items". */
6
8
  field?: string;
7
9
  /** false = no storage write (replaces queueOnly). true/absent = append to array. */
8
10
  persist?: boolean;
9
- /** When true, server validates client's baseHash against hash(lastItem). */
10
- checkLastItem?: boolean;
11
11
  }
12
12
  /** Per-collection metadata returned by GET /config. */
13
13
  export interface CollectionClientInfo {
package/dist/index.d.ts CHANGED
@@ -5,9 +5,11 @@ export { buildRevocationList, revocationListCanonicalSigningInput } from "@drakk
5
5
  export type { RevocationList, RevocationEntry, RevokedSubject, BuildRevocationListOpts, } from "@drakkar.software/starfish-protocol";
6
6
  export type { PullResult, PushSuccess, PullKeyringProjection } from "@drakkar.software/starfish-protocol";
7
7
  export { StarfishClient } from "./client.js";
8
- export type { BlobPullResult, BlobPushResult, AppendPullOptions, PullOptions } from "./client.js";
8
+ export type { BlobPullResult, BlobPushResult, AppendPullOptions, PullOptions, BatchPullOptions, BatchPullResult, BatchPullEntry, } from "./client.js";
9
9
  export { SyncManager, AbortError } from "./sync.js";
10
10
  export type { SyncManagerOptions, SyncSigner } from "./sync.js";
11
+ export { AppendLogCursor, AppendAuthorError, checkpointOf } from "./append-log.js";
12
+ export type { AppendLogCursorOptions, AppendElement, AuthorVerifier, ElementErrorPolicy } from "./append-log.js";
11
13
  export { ENCRYPTED_KEY } from "@drakkar.software/starfish-protocol";
12
14
  export type { Encryptor } from "@drakkar.software/starfish-protocol";
13
15
  export { ConflictError, StarfishHttpError, } from "./types.js";
@@ -40,8 +42,8 @@ export type { ServiceWorkerOptions } from "./service-worker.js";
40
42
  export { createSuspenseResource } from "./bindings/suspense.js";
41
43
  export { createDebouncedSync, createDebouncedPush } from "./debounced-sync.js";
42
44
  export type { DebouncedSyncOptions, DebouncedSync, DebouncedPushOptions, DebouncedPush } from "./debounced-sync.js";
43
- export { createMobileLifecycle } from "./mobile-lifecycle.js";
44
- export type { AppStateModule, NetInfoModule, MobileLifecycleDeps, MobileLifecycleOptions } from "./mobile-lifecycle.js";
45
+ export { createMobileLifecycle, createAppendLogMobileLifecycle } from "./mobile-lifecycle.js";
46
+ export type { AppStateModule, NetInfoModule, MobileLifecycleDeps, MobileLifecycleOptions, AppendLogLifecycleOptions } from "./mobile-lifecycle.js";
45
47
  export { createMultiStoreSync } from "./multi-store.js";
46
48
  export type { StoreSlice, BackupDocument, MultiStoreMigrationFn, MultiStoreSyncOptions, MultiStoreSync, } from "./multi-store.js";
47
49
  export type { AppendOnlyClientInfo } from "./config.js";