@drakkar.software/starfish-client 3.0.0-alpha.16 → 3.0.0-alpha.19
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/append.d.ts +50 -0
- package/dist/bindings/broadcast.d.ts +19 -0
- package/dist/bindings/broadcast.js +65 -0
- package/dist/bindings/react.d.ts +12 -0
- package/dist/bindings/react.js +25 -0
- package/dist/bindings/zustand.js +10 -0
- package/dist/bindings/zustand.js.map +2 -2
- package/dist/client.d.ts +7 -0
- package/dist/client.js +37 -316
- package/dist/crypto.js +49 -0
- package/dist/entitlements.js +41 -0
- package/dist/group-crypto.d.ts +111 -0
- package/dist/group-crypto.js +205 -0
- package/dist/group-crypto.js.map +7 -0
- package/dist/identity.d.ts +82 -4
- package/dist/identity.js +354 -2
- package/dist/identity.js.map +4 -4
- package/dist/index.js +11 -1
- package/dist/index.js.map +2 -2
- package/dist/mobile-lifecycle.js +2 -41
- package/dist/polling.js +2 -2
- package/dist/sync.js +14 -68
- package/package.json +2 -2
- package/dist/_crypto_helpers.d.ts +0 -4
- package/dist/append-log.js +0 -267
- package/dist/cap-mint.d.ts +0 -20
- package/dist/cap-mint.js +0 -12
- package/dist/cap-mint.js.map +0 -7
- package/dist/directory.d.ts +0 -9
- package/dist/directory.js +0 -24
- package/dist/directory.js.map +0 -7
- package/dist/keyring.d.ts +0 -6
- package/dist/keyring.js +0 -26
- package/dist/keyring.js.map +0 -7
- package/dist/pairing.d.ts +0 -6
- package/dist/pairing.js +0 -26
- package/dist/pairing.js.map +0 -7
- package/dist/recipients.d.ts +0 -6
- package/dist/recipients.js +0 -16
- package/dist/recipients.js.map +0 -7
package/dist/append.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { StarfishClient } from "./client.js";
|
|
2
|
+
import type { PushSuccess } from "@drakkar.software/starfish-protocol";
|
|
3
|
+
/**
|
|
4
|
+
* Appends `item` to an append-only collection.
|
|
5
|
+
*
|
|
6
|
+
* Sends `{ data: item, baseHash: null }` — the server ignores `baseHash` for
|
|
7
|
+
* append-only collections (conflict detection is disabled or delegated to
|
|
8
|
+
* `checkLastItem` on the server side).
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* await pushAppend(client, "/push/events", { type: "click", ts: Date.now() })
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function pushAppend(client: StarfishClient, path: string, item: Record<string, unknown>): Promise<PushSuccess>;
|
|
15
|
+
export interface PullAppendListOptions {
|
|
16
|
+
/** Array field name. Defaults to `"items"` (server default). */
|
|
17
|
+
field?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Return only items appended after this timestamp (milliseconds since epoch).
|
|
20
|
+
* Sent as `?checkpoint=<since>`. Omit for a full pull.
|
|
21
|
+
*/
|
|
22
|
+
since?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Return only the last K items. Applied after the `since` filter.
|
|
25
|
+
* Useful for "latest N entries" queries without a tracked checkpoint.
|
|
26
|
+
* Sent as `?last=<K>`.
|
|
27
|
+
*/
|
|
28
|
+
last?: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Pulls the stored item array from an append-only collection.
|
|
32
|
+
*
|
|
33
|
+
* Returns `data[field]` filtered to an array; returns `[]` when the document
|
|
34
|
+
* does not exist yet or the field is absent / not an array.
|
|
35
|
+
*
|
|
36
|
+
* Pass `{ since: ts }` for incremental pulls — only items appended after `ts`
|
|
37
|
+
* are returned (requires per-item timestamps on the server, available from 2.0.0).
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* // Full pull
|
|
41
|
+
* const events = await pullAppendList(client, "/pull/events")
|
|
42
|
+
*
|
|
43
|
+
* // Incremental pull
|
|
44
|
+
* const newEvents = await pullAppendList(client, "/pull/events", { since: lastSyncTs })
|
|
45
|
+
*
|
|
46
|
+
* // Custom field name
|
|
47
|
+
* const logs = await pullAppendList(client, "/pull/audit", { field: "logs" })
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function pullAppendList<T = unknown>(client: StarfishClient, path: string, options?: PullAppendListOptions): Promise<T[]>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { StoreApi } from "zustand/vanilla";
|
|
2
|
+
import type { StarfishStore } from "./zustand.js";
|
|
3
|
+
/**
|
|
4
|
+
* Syncs a Zustand Starfish store across browser tabs using BroadcastChannel.
|
|
5
|
+
* Returns a cleanup function that closes the channel.
|
|
6
|
+
*/
|
|
7
|
+
export declare function setupBroadcastSync(store: StoreApi<StarfishStore>, name: string): () => void;
|
|
8
|
+
/**
|
|
9
|
+
* Syncs a Zustand Starfish store across browser tabs using storage events.
|
|
10
|
+
* Fallback for environments without BroadcastChannel.
|
|
11
|
+
* Returns a cleanup function.
|
|
12
|
+
*/
|
|
13
|
+
export declare function setupStorageFallback(store: StoreApi<StarfishStore>, name: string): () => void;
|
|
14
|
+
/**
|
|
15
|
+
* Auto-detects the best cross-tab sync mechanism and sets it up.
|
|
16
|
+
* Uses BroadcastChannel when available, falls back to storage events.
|
|
17
|
+
* Returns a cleanup function.
|
|
18
|
+
*/
|
|
19
|
+
export declare function setupCrossTabSync(store: StoreApi<StarfishStore>, name: string): () => void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syncs a Zustand Starfish store across browser tabs using BroadcastChannel.
|
|
3
|
+
* Returns a cleanup function that closes the channel.
|
|
4
|
+
*/
|
|
5
|
+
export function setupBroadcastSync(store, name) {
|
|
6
|
+
const channel = new BroadcastChannel(`starfish-${name}`);
|
|
7
|
+
let lastReceivedData = null;
|
|
8
|
+
channel.onmessage = (event) => {
|
|
9
|
+
lastReceivedData = event.data.data;
|
|
10
|
+
store.setState({ data: event.data.data, dirty: event.data.dirty });
|
|
11
|
+
};
|
|
12
|
+
const unsub = store.subscribe((state, prev) => {
|
|
13
|
+
if (state.data === lastReceivedData)
|
|
14
|
+
return;
|
|
15
|
+
if (state.data !== prev.data || state.dirty !== prev.dirty) {
|
|
16
|
+
channel.postMessage({ data: state.data, dirty: state.dirty });
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return () => {
|
|
20
|
+
unsub();
|
|
21
|
+
channel.close();
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Syncs a Zustand Starfish store across browser tabs using storage events.
|
|
26
|
+
* Fallback for environments without BroadcastChannel.
|
|
27
|
+
* Returns a cleanup function.
|
|
28
|
+
*/
|
|
29
|
+
export function setupStorageFallback(store, name) {
|
|
30
|
+
const storageKey = `starfish-broadcast-${name}`;
|
|
31
|
+
let lastReceivedData = null;
|
|
32
|
+
const onStorage = (e) => {
|
|
33
|
+
if (e.key !== storageKey || !e.newValue)
|
|
34
|
+
return;
|
|
35
|
+
const payload = JSON.parse(e.newValue);
|
|
36
|
+
lastReceivedData = payload.data;
|
|
37
|
+
store.setState({ data: payload.data, dirty: payload.dirty });
|
|
38
|
+
};
|
|
39
|
+
globalThis.addEventListener("storage", onStorage);
|
|
40
|
+
const unsub = store.subscribe((state, prev) => {
|
|
41
|
+
if (state.data === lastReceivedData)
|
|
42
|
+
return;
|
|
43
|
+
if (state.data !== prev.data || state.dirty !== prev.dirty) {
|
|
44
|
+
localStorage.setItem(storageKey, JSON.stringify({ data: state.data, dirty: state.dirty }));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return () => {
|
|
48
|
+
unsub();
|
|
49
|
+
globalThis.removeEventListener("storage", onStorage);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Auto-detects the best cross-tab sync mechanism and sets it up.
|
|
54
|
+
* Uses BroadcastChannel when available, falls back to storage events.
|
|
55
|
+
* Returns a cleanup function.
|
|
56
|
+
*/
|
|
57
|
+
export function setupCrossTabSync(store, name) {
|
|
58
|
+
if (typeof BroadcastChannel !== "undefined") {
|
|
59
|
+
return setupBroadcastSync(store, name);
|
|
60
|
+
}
|
|
61
|
+
if (typeof globalThis.addEventListener === "function" && typeof localStorage !== "undefined") {
|
|
62
|
+
return setupStorageFallback(store, name);
|
|
63
|
+
}
|
|
64
|
+
return () => { };
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StoreApi } from "zustand/vanilla";
|
|
2
|
+
import type { StarfishStore, StarfishState } from "./zustand.js";
|
|
3
|
+
/** Derived sync status for UI display. */
|
|
4
|
+
export type SyncStatus = "synced" | "syncing" | "pending" | "error" | "offline";
|
|
5
|
+
/** Derive a single sync status from store state. */
|
|
6
|
+
export declare function deriveSyncStatus(state: StarfishState): SyncStatus;
|
|
7
|
+
/** Use the full Starfish store state and actions. */
|
|
8
|
+
export declare function useStarfish(store: StoreApi<StarfishStore>): StarfishStore;
|
|
9
|
+
/** Use only the synced data, with an optional selector for fine-grained subscriptions. */
|
|
10
|
+
export declare function useStarfishData<T = Record<string, unknown>>(store: StoreApi<StarfishStore>, selector?: (data: Record<string, unknown>) => T): T;
|
|
11
|
+
/** Use the derived sync status (synced | syncing | pending | error | offline). */
|
|
12
|
+
export declare function useSyncStatus(store: StoreApi<StarfishStore>): SyncStatus;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useStore } from "zustand";
|
|
2
|
+
/** Derive a single sync status from store state. */
|
|
3
|
+
export function deriveSyncStatus(state) {
|
|
4
|
+
if (!state.online)
|
|
5
|
+
return "offline";
|
|
6
|
+
if (state.error)
|
|
7
|
+
return "error";
|
|
8
|
+
if (state.syncing)
|
|
9
|
+
return "syncing";
|
|
10
|
+
if (state.dirty)
|
|
11
|
+
return "pending";
|
|
12
|
+
return "synced";
|
|
13
|
+
}
|
|
14
|
+
/** Use the full Starfish store state and actions. */
|
|
15
|
+
export function useStarfish(store) {
|
|
16
|
+
return useStore(store);
|
|
17
|
+
}
|
|
18
|
+
/** Use only the synced data, with an optional selector for fine-grained subscriptions. */
|
|
19
|
+
export function useStarfishData(store, selector) {
|
|
20
|
+
return useStore(store, (state) => selector ? selector(state.data) : state.data);
|
|
21
|
+
}
|
|
22
|
+
/** Use the derived sync status (synced | syncing | pending | error | offline). */
|
|
23
|
+
export function useSyncStatus(store) {
|
|
24
|
+
return useStore(store, deriveSyncStatus);
|
|
25
|
+
}
|
package/dist/bindings/zustand.js
CHANGED
|
@@ -421,14 +421,24 @@ var StarfishClient = class {
|
|
|
421
421
|
}
|
|
422
422
|
} else {
|
|
423
423
|
appendField = opts.appendField ?? APPEND_DEFAULT_FIELD;
|
|
424
|
+
if (opts.full && (opts.since != null || opts.limit != null || opts.last != null)) {
|
|
425
|
+
throw new Error("full cannot be combined with since, limit, or last");
|
|
426
|
+
}
|
|
424
427
|
if (opts.since != null) {
|
|
425
428
|
if (opts.since < 0) throw new Error("since must be non-negative");
|
|
426
429
|
params.set("checkpoint", String(opts.since));
|
|
427
430
|
}
|
|
431
|
+
if (opts.limit != null) {
|
|
432
|
+
if (opts.limit < 0) throw new Error("limit must be non-negative");
|
|
433
|
+
params.set("limit", String(opts.limit));
|
|
434
|
+
}
|
|
428
435
|
if (opts.last != null) {
|
|
429
436
|
if (opts.last < 0) throw new Error("last must be non-negative");
|
|
430
437
|
params.set("last", String(opts.last));
|
|
431
438
|
}
|
|
439
|
+
if (opts.full) {
|
|
440
|
+
params.set("full", "true");
|
|
441
|
+
}
|
|
432
442
|
}
|
|
433
443
|
if (params.size > 0) pathAndQuery += `?${params.toString()}`;
|
|
434
444
|
}
|