@drakkar.software/starfish-client 3.0.0-alpha.29 → 3.0.0-alpha.30

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.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * KV-backed {@link PullCache} factory.
3
+ *
4
+ * {@link createKvPullCache} adapts any {@link AsyncStateStorage} (or any
5
+ * object with `getItem`/`setItem`) into a {@link PullCache} that the
6
+ * `StarfishClient` can use as its offline read-through cache.
7
+ *
8
+ * Why this matters: the client's `cache` option is ciphertext-at-rest by
9
+ * construction — it stores the raw sealed server response and only decrypts
10
+ * in memory on read. Backing the cache with the platform's own KV (AsyncStorage
11
+ * on React Native, IndexedDB via `createIndexedDBStorage`, a custom adapter)
12
+ * gives offline-first reads without exposing plaintext to the OS storage layer.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import AsyncStorage from "@react-native-async-storage/async-storage"
17
+ * import { StarfishClient } from "@drakkar.software/starfish-client"
18
+ * import { createKvPullCache } from "@drakkar.software/starfish-client"
19
+ *
20
+ * const client = new StarfishClient({
21
+ * baseUrl: "https://api.example.com",
22
+ * cache: createKvPullCache(AsyncStorage, { prefix: "sf:", maxAgeMs: 30 * 24 * 60 * 60 * 1000 }),
23
+ * })
24
+ * ```
25
+ */
26
+ import type { PullCache } from "./types.js";
27
+ /** A storage backend that `createKvPullCache` can wrap. */
28
+ export interface KvStore {
29
+ getItem(key: string): Promise<string | null> | string | null;
30
+ setItem(key: string, value: string): Promise<unknown> | unknown;
31
+ removeItem?: (key: string) => Promise<unknown> | unknown;
32
+ }
33
+ /** Options for {@link createKvPullCache}. */
34
+ export interface KvPullCacheOptions {
35
+ /**
36
+ * Key prefix for cache entries (default `"starfish.pullcache."`). Change
37
+ * when sharing a KV store with other data to avoid key collisions.
38
+ */
39
+ prefix?: string;
40
+ /**
41
+ * Maximum age in milliseconds for a cached snapshot. When set, an entry
42
+ * older than `maxAgeMs` is returned as `null` (a cache MISS) so the next
43
+ * pull goes to the network rather than serving arbitrarily stale data.
44
+ *
45
+ * Each entry stores a `_cachedAt` wall-clock timestamp; expiry is checked on
46
+ * every `get`. Omit (default) for entries that never expire — recommended
47
+ * for offline-first apps where any last-synced data beats none.
48
+ */
49
+ maxAgeMs?: number;
50
+ }
51
+ /**
52
+ * Adapt a KV store into a {@link PullCache} for `StarfishClient`.
53
+ *
54
+ * The adapter serialises the cached pull payload (itself a JSON string) into
55
+ * an outer JSON envelope that tracks the wall-clock write time for optional
56
+ * max-age expiry. Reading a legacy entry without `_cachedAt` treats it as
57
+ * fresh (backward-compatible with plain-string caches).
58
+ *
59
+ * All `get`/`set` errors are swallowed (the {@link PullCache} contract
60
+ * requires implementations not to throw) — a failing KV store simply
61
+ * degrades to "no cache" without crashing the app.
62
+ */
63
+ export declare function createKvPullCache(kv: KvStore, opts?: KvPullCacheOptions): PullCache;
package/dist/types.d.ts CHANGED
@@ -9,6 +9,20 @@ export declare class StarfishHttpError extends Error {
9
9
  readonly body: string;
10
10
  constructor(status: number, body: string);
11
11
  }
12
+ /**
13
+ * Non-2xx HTTP error from an anonymous (cap-less) append call.
14
+ *
15
+ * Distinct from {@link StarfishHttpError} so callers can distinguish "the
16
+ * anonymous write was rejected" (e.g. auth required, payload too large) from
17
+ * other client errors without pattern-matching on the error message.
18
+ */
19
+ export declare class AppendHttpError extends Error {
20
+ /** HTTP status returned by the server. */
21
+ readonly status: number;
22
+ constructor(
23
+ /** HTTP status returned by the server. */
24
+ status: number, message: string);
25
+ }
12
26
  /**
13
27
  * v3.0 cap-cert provider for `StarfishClient`. Returns the device's cap-cert and
14
28
  * the matching Ed25519 private key (hex). The client calls `getCap()` once per
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drakkar.software/starfish-client",
3
- "version": "3.0.0-alpha.29",
3
+ "version": "3.0.0-alpha.30",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/Drakkar-Software/starfish.git",
@@ -26,6 +26,10 @@
26
26
  "types": "./dist/bindings/legend.d.ts",
27
27
  "import": "./dist/bindings/legend.js"
28
28
  },
29
+ "./events": {
30
+ "types": "./dist/events.d.ts",
31
+ "import": "./dist/events.js"
32
+ },
29
33
  "./fetch": {
30
34
  "types": "./dist/fetch.d.ts",
31
35
  "import": "./dist/fetch.js"
@@ -60,7 +64,7 @@
60
64
  }
61
65
  },
62
66
  "dependencies": {
63
- "@drakkar.software/starfish-protocol": "3.0.0-alpha.29"
67
+ "@drakkar.software/starfish-protocol": "3.0.0-alpha.30"
64
68
  },
65
69
  "devDependencies": {
66
70
  "@legendapp/state": "^2.0.0",