@drakkar.software/starfish-client 1.7.0 → 1.11.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/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/package.json +2 -2
- package/dist/hash.d.ts +0 -10
- package/dist/hash.js +0 -34
- package/dist/platform.d.ts +0 -52
- package/dist/platform.js +0 -62
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drakkar.software/starfish-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/Drakkar-Software/starfish.git",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@drakkar.software/starfish-protocol": "1.
|
|
67
|
+
"@drakkar.software/starfish-protocol": "1.11.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@legendapp/state": "^2.0.0",
|
package/dist/hash.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Deterministic JSON serialization with sorted keys (recursive).
|
|
3
|
-
* Must produce identical output to the server's stableStringify.
|
|
4
|
-
*/
|
|
5
|
-
export declare function stableStringify(value: unknown): string;
|
|
6
|
-
/**
|
|
7
|
-
* Compute SHA-256 hex digest of the stable-stringified data.
|
|
8
|
-
* Works in both browser (crypto.subtle) and Node.js environments.
|
|
9
|
-
*/
|
|
10
|
-
export declare function computeHash(data: Record<string, unknown>): Promise<string>;
|
package/dist/hash.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { getCrypto } from "./platform.js";
|
|
2
|
-
/**
|
|
3
|
-
* Deterministic JSON serialization with sorted keys (recursive).
|
|
4
|
-
* Must produce identical output to the server's stableStringify.
|
|
5
|
-
*/
|
|
6
|
-
export function stableStringify(value) {
|
|
7
|
-
if (value === null || value === undefined)
|
|
8
|
-
return "null";
|
|
9
|
-
if (typeof value === "boolean" || typeof value === "number")
|
|
10
|
-
return JSON.stringify(value);
|
|
11
|
-
if (typeof value === "string")
|
|
12
|
-
return JSON.stringify(value);
|
|
13
|
-
if (Array.isArray(value)) {
|
|
14
|
-
return "[" + value.map(v => stableStringify(v)).join(",") + "]";
|
|
15
|
-
}
|
|
16
|
-
if (typeof value === "object") {
|
|
17
|
-
const obj = value;
|
|
18
|
-
const keys = Object.keys(obj).sort();
|
|
19
|
-
const pairs = keys.map(k => JSON.stringify(k) + ":" + stableStringify(obj[k]));
|
|
20
|
-
return "{" + pairs.join(",") + "}";
|
|
21
|
-
}
|
|
22
|
-
return "null";
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Compute SHA-256 hex digest of the stable-stringified data.
|
|
26
|
-
* Works in both browser (crypto.subtle) and Node.js environments.
|
|
27
|
-
*/
|
|
28
|
-
export async function computeHash(data) {
|
|
29
|
-
const encoded = new TextEncoder().encode(stableStringify(data));
|
|
30
|
-
const buf = await getCrypto().subtle.digest("SHA-256", encoded);
|
|
31
|
-
return Array.from(new Uint8Array(buf))
|
|
32
|
-
.map(b => b.toString(16).padStart(2, "0"))
|
|
33
|
-
.join("");
|
|
34
|
-
}
|
package/dist/platform.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
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
|
-
}
|