@drakkar.software/starfish-client 2.0.0 → 2.2.0
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/bindings/zustand.d.ts +2 -0
- package/dist/bindings/zustand.js +37 -4
- package/dist/bindings/zustand.js.map +2 -2
- package/dist/hash.d.ts +10 -0
- package/dist/hash.js +34 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +29 -0
- package/dist/index.js.map +2 -2
- package/dist/mobile-lifecycle.js +2 -2
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +62 -0
- package/dist/polling.js +2 -2
- package/dist/sync.d.ts +8 -0
- package/package.json +2 -2
- package/dist/append.d.ts +0 -50
- package/dist/bindings/broadcast.d.ts +0 -19
- package/dist/bindings/broadcast.js +0 -65
- package/dist/bindings/react.d.ts +0 -12
- package/dist/bindings/react.js +0 -25
package/dist/mobile-lifecycle.js
CHANGED
|
@@ -31,13 +31,13 @@ export function createMobileLifecycle(store, deps, options = {}) {
|
|
|
31
31
|
const appSub = deps.appState.addEventListener("change", (appState) => {
|
|
32
32
|
if (appState === "background" && flushOnBackground) {
|
|
33
33
|
if (store.getState().dirty) {
|
|
34
|
-
store.getState().flush().catch(() => { });
|
|
34
|
+
store.getState().flush().catch((err) => { console.error("[Starfish] background flush failed:", err); });
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
else if (appState === "active" && pullOnForeground) {
|
|
38
38
|
const { online, syncing } = store.getState();
|
|
39
39
|
if (online && !syncing) {
|
|
40
|
-
store.getState().pull().catch(() => { });
|
|
40
|
+
store.getState().pull().catch((err) => { console.error("[Starfish] foreground pull failed:", err); });
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
// "inactive" (iOS transition) and other states are intentionally ignored
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform abstraction for crypto and base64 operations.
|
|
3
|
+
*
|
|
4
|
+
* Browser and Node.js >= 15 work with zero configuration (globalThis.crypto).
|
|
5
|
+
* React Native users must call configurePlatform() before using the SDK.
|
|
6
|
+
*/
|
|
7
|
+
/** Minimal crypto interface required by the SDK (subset of Web Crypto API). */
|
|
8
|
+
export interface CryptoProvider {
|
|
9
|
+
subtle: {
|
|
10
|
+
digest(algorithm: string, data: BufferSource): Promise<ArrayBuffer>;
|
|
11
|
+
importKey(format: "raw", keyData: BufferSource, algorithm: string | Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
12
|
+
deriveKey(algorithm: HkdfParams, baseKey: CryptoKey, derivedKeyType: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
13
|
+
encrypt(algorithm: AesGcmParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
|
|
14
|
+
decrypt(algorithm: AesGcmParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
|
|
15
|
+
};
|
|
16
|
+
getRandomValues<T extends ArrayBufferView>(array: T): T;
|
|
17
|
+
}
|
|
18
|
+
/** Base64 encode/decode for Uint8Array <-> string. */
|
|
19
|
+
export interface Base64Provider {
|
|
20
|
+
encode(data: Uint8Array): string;
|
|
21
|
+
decode(encoded: string): Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
export interface PlatformConfig {
|
|
24
|
+
crypto?: CryptoProvider;
|
|
25
|
+
base64?: Base64Provider;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Configure platform-specific providers for environments
|
|
29
|
+
* that lack the Web Crypto API (e.g., React Native).
|
|
30
|
+
*
|
|
31
|
+
* Call once at app startup, before using any SDK functions.
|
|
32
|
+
* Not needed for browser or Node.js >= 15.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { configurePlatform } from "@satellite/client"
|
|
37
|
+
* import QuickCrypto from "react-native-quick-crypto"
|
|
38
|
+
*
|
|
39
|
+
* configurePlatform({
|
|
40
|
+
* crypto: QuickCrypto,
|
|
41
|
+
* base64: {
|
|
42
|
+
* encode: (data) => Buffer.from(data).toString("base64"),
|
|
43
|
+
* decode: (str) => new Uint8Array(Buffer.from(str, "base64")),
|
|
44
|
+
* },
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function configurePlatform(config: PlatformConfig): void;
|
|
49
|
+
/** Resolve the active crypto provider. */
|
|
50
|
+
export declare function getCrypto(): CryptoProvider;
|
|
51
|
+
/** Resolve the active base64 provider. */
|
|
52
|
+
export declare function getBase64(): Base64Provider;
|
package/dist/platform.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform abstraction for crypto and base64 operations.
|
|
3
|
+
*
|
|
4
|
+
* Browser and Node.js >= 15 work with zero configuration (globalThis.crypto).
|
|
5
|
+
* React Native users must call configurePlatform() before using the SDK.
|
|
6
|
+
*/
|
|
7
|
+
let _crypto;
|
|
8
|
+
let _base64;
|
|
9
|
+
/**
|
|
10
|
+
* Configure platform-specific providers for environments
|
|
11
|
+
* that lack the Web Crypto API (e.g., React Native).
|
|
12
|
+
*
|
|
13
|
+
* Call once at app startup, before using any SDK functions.
|
|
14
|
+
* Not needed for browser or Node.js >= 15.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { configurePlatform } from "@satellite/client"
|
|
19
|
+
* import QuickCrypto from "react-native-quick-crypto"
|
|
20
|
+
*
|
|
21
|
+
* configurePlatform({
|
|
22
|
+
* crypto: QuickCrypto,
|
|
23
|
+
* base64: {
|
|
24
|
+
* encode: (data) => Buffer.from(data).toString("base64"),
|
|
25
|
+
* decode: (str) => new Uint8Array(Buffer.from(str, "base64")),
|
|
26
|
+
* },
|
|
27
|
+
* })
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function configurePlatform(config) {
|
|
31
|
+
if (config.crypto)
|
|
32
|
+
_crypto = config.crypto;
|
|
33
|
+
if (config.base64)
|
|
34
|
+
_base64 = config.base64;
|
|
35
|
+
}
|
|
36
|
+
/** Resolve the active crypto provider. */
|
|
37
|
+
export function getCrypto() {
|
|
38
|
+
if (_crypto)
|
|
39
|
+
return _crypto;
|
|
40
|
+
if (typeof globalThis !== "undefined" && globalThis.crypto?.subtle) {
|
|
41
|
+
return globalThis.crypto;
|
|
42
|
+
}
|
|
43
|
+
throw new Error("@satellite/client: No crypto provider available. " +
|
|
44
|
+
"In React Native, call configurePlatform({ crypto: ... }) before using the SDK.");
|
|
45
|
+
}
|
|
46
|
+
/** Resolve the active base64 provider. */
|
|
47
|
+
export function getBase64() {
|
|
48
|
+
if (_base64)
|
|
49
|
+
return _base64;
|
|
50
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
|
|
51
|
+
return {
|
|
52
|
+
encode(data) {
|
|
53
|
+
return btoa(String.fromCharCode(...data));
|
|
54
|
+
},
|
|
55
|
+
decode(encoded) {
|
|
56
|
+
return Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0));
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
throw new Error("@satellite/client: No base64 provider available. " +
|
|
61
|
+
"In React Native, call configurePlatform({ base64: ... }) before using the SDK.");
|
|
62
|
+
}
|
package/dist/polling.js
CHANGED
|
@@ -14,7 +14,7 @@ export function startPolling(pullFn, getState, intervalMs = 30_000) {
|
|
|
14
14
|
const timer = setInterval(() => {
|
|
15
15
|
const { online, syncing } = getState();
|
|
16
16
|
if (online && !syncing)
|
|
17
|
-
pullFn().catch(() => { });
|
|
17
|
+
pullFn().catch((err) => { console.error("[Starfish] poll failed:", err); });
|
|
18
18
|
}, intervalMs);
|
|
19
19
|
return () => clearInterval(timer);
|
|
20
20
|
}
|
|
@@ -42,7 +42,7 @@ export function startAdaptivePolling(pullFn, getState, options) {
|
|
|
42
42
|
return;
|
|
43
43
|
const { online, syncing } = getState();
|
|
44
44
|
if (online && !syncing)
|
|
45
|
-
pullFn().catch(() => { });
|
|
45
|
+
pullFn().catch((err) => { console.error("[Starfish] adaptive poll failed:", err); });
|
|
46
46
|
}, intervalMs);
|
|
47
47
|
return {
|
|
48
48
|
pause: () => { paused = true; },
|
package/dist/sync.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ import { StarfishClient } from "./client.js";
|
|
|
4
4
|
import type { Encryptor } from "./crypto.js";
|
|
5
5
|
import type { SyncLogger } from "./logger.js";
|
|
6
6
|
import type { Validator } from "./validate.js";
|
|
7
|
+
export declare class AbortError extends Error {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
7
10
|
export interface SyncManagerOptions {
|
|
8
11
|
client: StarfishClient;
|
|
9
12
|
pullPath: string;
|
|
@@ -42,9 +45,14 @@ export declare class SyncManager {
|
|
|
42
45
|
private lastHash;
|
|
43
46
|
private lastCheckpoint;
|
|
44
47
|
private localData;
|
|
48
|
+
private aborted;
|
|
45
49
|
constructor(options: SyncManagerOptions);
|
|
50
|
+
abort(): void;
|
|
51
|
+
get isAborted(): boolean;
|
|
46
52
|
getData(): Record<string, unknown>;
|
|
47
53
|
getHash(): string | null;
|
|
54
|
+
/** Set the last-known server hash. Used by persistence layers to restore state across restarts. */
|
|
55
|
+
setHash(hash: string | null): void;
|
|
48
56
|
getCheckpoint(): number;
|
|
49
57
|
pull(): Promise<PullResult>;
|
|
50
58
|
push(data: Record<string, unknown>): Promise<{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drakkar.software/starfish-client",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/Drakkar-Software/starfish.git",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@noble/curves": "^2.2.0",
|
|
72
|
-
"@drakkar.software/starfish-protocol": "2.
|
|
72
|
+
"@drakkar.software/starfish-protocol": "2.2.0"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"@legendapp/state": "^2.0.0",
|
package/dist/append.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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[]>;
|
|
@@ -1,19 +0,0 @@
|
|
|
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;
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/bindings/react.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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;
|
package/dist/bindings/react.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|