@k8ordo/state 1.4.0 → 1.4.2

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.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/dist/base.d.mts +1 -3
  3. package/dist/base.mjs +1 -2
  4. package/dist/cookie-state.d.mts +1 -3
  5. package/dist/cookie-state.mjs +1 -2
  6. package/dist/entry/codec.d.mts +1 -3
  7. package/dist/entry/codec.mjs +1 -2
  8. package/dist/memory-state.d.mts +1 -3
  9. package/dist/memory-state.mjs +1 -2
  10. package/dist/page-state.d.mts +0 -2
  11. package/dist/page-state.mjs +1 -2
  12. package/dist/register.d.mts +0 -2
  13. package/dist/row/codec.d.mts +1 -3
  14. package/dist/row/codec.mjs +1 -2
  15. package/dist/schema/object.d.mts +1 -3
  16. package/dist/schema/object.mjs +1 -2
  17. package/dist/storage-state.d.mts +0 -2
  18. package/dist/storage-state.mjs +1 -2
  19. package/dist/store/cookie-store.d.mts +1 -3
  20. package/dist/store/cookie-store.mjs +1 -2
  21. package/dist/store/core.d.mts +1 -3
  22. package/dist/store/core.mjs +1 -2
  23. package/dist/store/kind.d.mts +0 -2
  24. package/dist/store/kind.mjs +1 -2
  25. package/dist/store/memory-store.d.mts +1 -3
  26. package/dist/store/memory-store.mjs +1 -2
  27. package/dist/store/page-store.d.mts +1 -3
  28. package/dist/store/page-store.mjs +3 -2
  29. package/dist/store/registry.d.mts +1 -3
  30. package/dist/store/registry.mjs +0 -0
  31. package/dist/store/storage-store.d.mts +0 -2
  32. package/dist/store/storage-store.mjs +30 -8
  33. package/dist/tree-pathname.d.mts +1 -3
  34. package/dist/tree-pathname.mjs +1 -2
  35. package/dist/url/codec.d.mts +1 -3
  36. package/dist/url/codec.mjs +1 -2
  37. package/dist/use-app-state.d.mts +0 -2
  38. package/dist/use-app-state.mjs +1 -2
  39. package/docs/GUIDE.md +11 -3
  40. package/docs/llms.txt +4 -2
  41. package/package.json +5 -5
package/README.md CHANGED
@@ -32,7 +32,7 @@ pnpm add @k8ordo/state zod
32
32
  | ---------------- | ------- | -------- | ------------------------------------------------------- |
33
33
  | `react` | ≥19.3.0 | yes | `useAppState` |
34
34
  | `zod` | ^4.6.6 | yes | the schemas (`zod/mini` works, and is the lighter pick) |
35
- | `@k8ordo/router` | ^1.4.0 | optional | typed `href` paths from the app's route table |
35
+ | `@k8ordo/router` | ^1.4.2 | optional | typed `href` paths from the app's route table |
36
36
  | `typescript` | ≥7.0.0 | optional | the shipped type declarations |
37
37
  | `@types/react` | ≥19.3.0 | optional | the shipped type declarations |
38
38
 
package/dist/base.d.mts CHANGED
@@ -1,9 +1,7 @@
1
- //#region src/base.d.ts
2
1
  /**
3
2
  * A path as the URL spells it: Vite's `base` in front, so `/products` is
4
3
  * `/docs/products` under `base: '/docs/'` and `/` is `/docs/`. The same rule
5
4
  * as `@k8ordo/router`'s `withBase` — not borrowed, because the router is an
6
5
  * optional peer this package only reads types from.
7
6
  */
8
- export declare const withBase: (path: string) => string;
9
- //#endregion
7
+ export declare const withBase: (path: string) => string;
package/dist/base.mjs CHANGED
@@ -1,4 +1,4 @@
1
- //#region src/base.ts
1
+ // src/base.ts
2
2
  /**
3
3
  * A path as the URL spells it: Vite's `base` in front, so `/products` is
4
4
  * `/docs/products` under `base: '/docs/'` and `/` is `/docs/`. The same rule
@@ -11,5 +11,4 @@ const withBase = (path) => {
11
11
  const prefix = base.endsWith("/") ? base.slice(0, -1) : base;
12
12
  return path === "/" ? `${prefix}/` : `${prefix}${path}`;
13
13
  };
14
- //#endregion
15
14
  export { withBase };
@@ -1,7 +1,6 @@
1
1
  import { StateSchema } from "./schema/object.mjs";
2
2
  import { Versioning } from "./row/codec.mjs";
3
3
  import { output } from "zod/v4/core";
4
- //#region src/cookie-state.d.ts
5
4
  /**
6
5
  * App-scope state that lives in a cookie: device-persistent and shared across
7
6
  * tabs like local state, and — unlike it — sent with every request, so a
@@ -32,5 +31,4 @@ export type CookieState<Schema extends StateSchema = StateSchema> = {
32
31
  */
33
32
  cookieValue: (values?: Readonly<Partial<output<Schema>>>) => string;
34
33
  };
35
- export declare const defineCookieState: <Schema extends StateSchema>(key: string, schema: Schema, versioning?: Versioning<Schema>) => CookieState<Schema>;
36
- //#endregion
34
+ export declare const defineCookieState: <Schema extends StateSchema>(key: string, schema: Schema, versioning?: Versioning<Schema>) => CookieState<Schema>;
@@ -1,7 +1,7 @@
1
1
  import { createRowCodec } from "./row/codec.mjs";
2
2
  import { cookieInitialSnapshot, cookieStoreOf, parseCookieText } from "./store/cookie-store.mjs";
3
3
  import { setKind } from "./store/kind.mjs";
4
- //#region src/cookie-state.ts
4
+ // src/cookie-state.ts
5
5
  const COOKIE_NAME_PREFIX = "k8ordo-state.";
6
6
  const TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u;
7
7
  const defineCookieState = (key, schema, versioning) => {
@@ -22,5 +22,4 @@ const defineCookieState = (key, schema, versioning) => {
22
22
  });
23
23
  return def;
24
24
  };
25
- //#endregion
26
25
  export { defineCookieState };
@@ -1,5 +1,4 @@
1
1
  import { StateSchema, StateValues } from "../schema/object.mjs";
2
- //#region src/entry/codec.d.ts
3
2
  export type StoredCodec = {
4
3
  keys: readonly string[];
5
4
  defaults: Readonly<StateValues>;
@@ -13,5 +12,4 @@ export type StoredCodec = {
13
12
  * that what comes back may have been written by an older schema, or by
14
13
  * nobody.
15
14
  */
16
- export declare const createStoredCodec: (schema: StateSchema, slot: string) => StoredCodec;
17
- //#endregion
15
+ export declare const createStoredCodec: (schema: StateSchema, slot: string) => StoredCodec;
@@ -1,5 +1,5 @@
1
1
  import { analyzeSchema, isRecord, parseWithSalvage } from "../schema/object.mjs";
2
- //#region src/entry/codec.ts
2
+ // src/entry/codec.ts
3
3
  /**
4
4
  * The read side shared by every slot that stores typed values (history entry
5
5
  * state, localStorage JSON): no string serialization to derive — only care
@@ -27,5 +27,4 @@ const createStoredCodec = (schema, slot) => {
27
27
  salvage
28
28
  };
29
29
  };
30
- //#endregion
31
30
  export { createStoredCodec };
@@ -1,5 +1,4 @@
1
1
  import { StateValues } from "./schema/object.mjs";
2
- //#region src/memory-state.d.ts
3
2
  /**
4
3
  * A typed shared box that lives for the JS runtime and resets on reload.
5
4
  * The one kind with no schema: its values never leave the runtime, so there
@@ -12,5 +11,4 @@ export type MemoryState<Values extends StateValues = StateValues> = {
12
11
  /** The initial values; also what SSR renders. */
13
12
  initial: Readonly<Values>;
14
13
  };
15
- export declare const defineMemoryState: <Values extends StateValues>(key: string, initial: Values) => MemoryState<Values>;
16
- //#endregion
14
+ export declare const defineMemoryState: <Values extends StateValues>(key: string, initial: Values) => MemoryState<Values>;
@@ -1,6 +1,6 @@
1
1
  import { setKind } from "./store/kind.mjs";
2
2
  import { memoryInitialSnapshot, memoryStoreOf } from "./store/memory-store.mjs";
3
- //#region src/memory-state.ts
3
+ // src/memory-state.ts
4
4
  const defineMemoryState = (key, initial) => {
5
5
  const def = {
6
6
  kind: "memory",
@@ -13,5 +13,4 @@ const defineMemoryState = (key, initial) => {
13
13
  });
14
14
  return def;
15
15
  };
16
- //#endregion
17
16
  export { defineMemoryState };
@@ -3,7 +3,6 @@ import { StoredCodec } from "./entry/codec.mjs";
3
3
  import { RegisteredPath } from "./register.mjs";
4
4
  import { UrlCodec, UrlInput } from "./url/codec.mjs";
5
5
  import { output } from "zod/v4/core";
6
- //#region src/page-state.d.ts
7
6
  export type OutputOf<Schema> = Schema extends StateSchema ? output<Schema> : Record<never, never>;
8
7
  export type PageState<Url extends StateSchema | undefined = StateSchema | undefined, Entry extends StateSchema | undefined = StateSchema | undefined> = {
9
8
  kind: 'page';
@@ -58,5 +57,4 @@ export declare function definePageState<Url extends StateSchema, Entry extends S
58
57
  * codec is built once, when this is called.
59
58
  */
60
59
  export declare const urlReader: <Url extends StateSchema>(schema: Url) => ((input: UrlInput) => output<Url>);
61
- //#endregion
62
60
  export {};
@@ -3,7 +3,7 @@ import { createStoredCodec } from "./entry/codec.mjs";
3
3
  import { setKind } from "./store/kind.mjs";
4
4
  import { pageInitialSnapshot, pageStoreOf } from "./store/page-store.mjs";
5
5
  import { createUrlCodec } from "./url/codec.mjs";
6
- //#region src/page-state.ts
6
+ // src/page-state.ts
7
7
  function definePageState(key, config) {
8
8
  const url = config.url === void 0 ? null : createUrlCodec(config.url);
9
9
  const entry = config.entry === void 0 ? null : createStoredCodec(config.entry, "entry");
@@ -47,5 +47,4 @@ const urlReader = (schema) => {
47
47
  const codec = createUrlCodec(schema);
48
48
  return (input) => codec.parse(input);
49
49
  };
50
- //#endregion
51
50
  export { definePageState, urlReader };
@@ -1,5 +1,4 @@
1
1
  import { NavigablePath, NavigablePatternOf, Routes, RoutesRecord } from "@k8ordo/router";
2
- //#region src/register.d.ts
3
2
  /**
4
3
  * The app-side hook for typed route paths. An application augments this once,
5
4
  * with the same line its `@k8ordo/router` augmentation uses:
@@ -38,5 +37,4 @@ export type AcceptedPath<R, Path extends string> = R extends {
38
37
  } ? [Path] extends [P] ? Path : P : [Path] extends [`/${string}`] ? Path : `/${string}`;
39
38
  /** `href()`'s path check: `Path` when the registered router accepts it. */
40
39
  export type RegisteredPath<Path extends string> = AcceptedPath<Register, Path>;
41
- //#endregion
42
40
  export {};
@@ -1,7 +1,6 @@
1
1
  import { StateSchema, StateValues } from "../schema/object.mjs";
2
2
  import { StoredCodec } from "../entry/codec.mjs";
3
3
  import { input } from "zod/v4/core";
4
- //#region src/row/codec.d.ts
5
4
  /**
6
5
  * How a local or cookie state reads rows an older version of its schema
7
6
  * wrote. Without it, such a row salvages field by field and every field the
@@ -44,5 +43,4 @@ export type RowCodec = Omit<StoredCodec, 'parse'> & {
44
43
  /** The version rows are written with, `undefined` when unversioned. */
45
44
  version: number | undefined;
46
45
  };
47
- export declare const createRowCodec: (schema: StateSchema, slot: string, key: string, versioning: Versioning | undefined) => RowCodec;
48
- //#endregion
46
+ export declare const createRowCodec: (schema: StateSchema, slot: string, key: string, versioning: Versioning | undefined) => RowCodec;
@@ -1,6 +1,6 @@
1
1
  import { isRecord } from "../schema/object.mjs";
2
2
  import { createStoredCodec } from "../entry/codec.mjs";
3
- //#region src/row/codec.ts
3
+ // src/row/codec.ts
4
4
  const envelopeOf = (row) => {
5
5
  if (isRecord(row)) return {
6
6
  fromVersion: 0,
@@ -56,5 +56,4 @@ const createRowCodec = (schema, slot, key, versioning) => {
56
56
  version
57
57
  };
58
58
  };
59
- //#endregion
60
59
  export { createRowCodec };
@@ -1,5 +1,4 @@
1
1
  import { $ZodObject, $ZodShape, $ZodType } from "zod/v4/core";
2
- //#region src/schema/object.d.ts
3
2
  /**
4
3
  * The common ground between `zod` and `zod/mini`: both build on the shared
5
4
  * core object and both expose `shape`. Accepting this keeps the smaller entry
@@ -30,5 +29,4 @@ export declare const analyzeSchema: (schema: StateSchema, slot: string) => Schem
30
29
  * input, so one broken field falls back to its own default instead of taking
31
30
  * the rest down with it.
32
31
  */
33
- export declare const parseWithSalvage: (info: SchemaInfo, raw: StateValues) => StateValues;
34
- //#endregion
32
+ export declare const parseWithSalvage: (info: SchemaInfo, raw: StateValues) => StateValues;
@@ -1,5 +1,5 @@
1
1
  import { safeParse } from "zod/v4/core";
2
- //#region src/schema/object.ts
2
+ // src/schema/object.ts
3
3
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
4
  const isPlainRecord = (value) => {
5
5
  if (typeof value !== "object" || value === null) return false;
@@ -58,5 +58,4 @@ const parseWithSalvage = (info, raw) => {
58
58
  const salvaged = safeParse(info.schema, kept);
59
59
  return salvaged.success ? pickKeys(info, salvaged.data) : { ...info.defaults };
60
60
  };
61
- //#endregion
62
61
  export { analyzeSchema, isRecord, parseWithSalvage, sameValue };
@@ -1,6 +1,5 @@
1
1
  import { StateSchema } from "./schema/object.mjs";
2
2
  import { Versioning } from "./row/codec.mjs";
3
- //#region src/storage-state.d.ts
4
3
  type StorageState<Kind extends 'local' | 'session', Schema extends StateSchema> = {
5
4
  kind: Kind;
6
5
  /** Identity of this state: the storage key and the store registry slot. */
@@ -33,5 +32,4 @@ export type LocalState<Schema extends StateSchema = StateSchema> = StorageState<
33
32
  export type SessionState<Schema extends StateSchema = StateSchema> = StorageState<'session', Schema>;
34
33
  export declare const defineLocalState: <Schema extends StateSchema>(key: string, schema: Schema, versioning?: Versioning<Schema>) => LocalState<Schema>;
35
34
  export declare const defineSessionState: <Schema extends StateSchema>(key: string, schema: Schema) => SessionState<Schema>;
36
- //#endregion
37
35
  export {};
@@ -1,7 +1,7 @@
1
1
  import { createRowCodec } from "./row/codec.mjs";
2
2
  import { setKind } from "./store/kind.mjs";
3
3
  import { storageInitialSnapshot, storageStoreOf } from "./store/storage-store.mjs";
4
- //#region src/storage-state.ts
4
+ // src/storage-state.ts
5
5
  const STORAGE_KEY_PREFIX = "k8ordo-state:";
6
6
  /**
7
7
  * The key as a JavaScript string literal. `JSON.stringify` escapes it for
@@ -32,5 +32,4 @@ const defineStorageState = (kind, area, key, schema, versioning) => {
32
32
  };
33
33
  const defineLocalState = (key, schema, versioning) => defineStorageState("local", "localStorage", key, schema, versioning);
34
34
  const defineSessionState = (key, schema) => defineStorageState("session", "sessionStorage", key, schema, void 0);
35
- //#endregion
36
35
  export { defineLocalState, defineSessionState };
@@ -2,7 +2,6 @@ import { StateValues } from "../schema/object.mjs";
2
2
  import { RowCodec } from "../row/codec.mjs";
3
3
  import { CookieState } from "../cookie-state.mjs";
4
4
  import { Store } from "./core.mjs";
5
- //#region src/store/cookie-store.d.ts
6
5
  export declare const parseCookieText: (text: string | undefined) => unknown;
7
6
  export declare const cookieStoreOf: (def: CookieState, codec: RowCodec) => Store;
8
7
  /**
@@ -10,5 +9,4 @@ export declare const cookieStoreOf: (def: CookieState, codec: RowCodec) => Store
10
9
  * request's cookies when the caller passed them down; the defaults otherwise.
11
10
  * Pure on purpose: the server must never touch `document` or create a store.
12
11
  */
13
- export declare const cookieInitialSnapshot: (codec: RowCodec, initialCookie: Readonly<StateValues> | undefined) => StateValues;
14
- //#endregion
12
+ export declare const cookieInitialSnapshot: (codec: RowCodec, initialCookie: Readonly<StateValues> | undefined) => StateValues;
@@ -1,6 +1,6 @@
1
1
  import { createHandle, createStoreCore, resolvePatch } from "./core.mjs";
2
2
  import { getOrCreateStore } from "./registry.mjs";
3
- //#region src/store/cookie-store.ts
3
+ // src/store/cookie-store.ts
4
4
  const MAX_AGE = 3456e4;
5
5
  const rawCookie = (name) => {
6
6
  for (const part of document.cookie.split(";")) {
@@ -130,5 +130,4 @@ const cookieInitialSnapshot = (codec, initialCookie) => {
130
130
  }
131
131
  return base;
132
132
  };
133
- //#endregion
134
133
  export { cookieInitialSnapshot, cookieStoreOf, parseCookieText };
@@ -1,5 +1,4 @@
1
1
  import { StateValues } from "../schema/object.mjs";
2
- //#region src/store/core.d.ts
3
2
  /**
4
3
  * Mirrors `navigation.navigate()`'s own return shape: an object holding the
5
4
  * promises rather than a promise, so fire-and-forget callers ignore it
@@ -63,5 +62,4 @@ export declare const createStoreCore: (keys: readonly string[], initial: StateVa
63
62
  * The unknown-field guard every store shares: the typed API already prevents
64
63
  * this, so reaching it means an untyped caller — fail loudly, not silently.
65
64
  */
66
- export declare const resolvePatch: (patch: Patch, current: StateValues, keys: readonly string[], label: string) => StateValues;
67
- //#endregion
65
+ export declare const resolvePatch: (patch: Patch, current: StateValues, keys: readonly string[], label: string) => StateValues;
@@ -1,5 +1,5 @@
1
1
  import { sameValue } from "../schema/object.mjs";
2
- //#region src/store/core.ts
2
+ // src/store/core.ts
3
3
  const createHandle = () => {
4
4
  let resolveCommitted;
5
5
  let rejectCommitted;
@@ -113,5 +113,4 @@ const resolvePatch = (patch, current, keys, label) => {
113
113
  for (const key of Object.keys(resolved)) if (!keys.includes(key)) throw new TypeError(`"${label}" has no field "${key}"`);
114
114
  return resolved;
115
115
  };
116
- //#endregion
117
116
  export { createHandle, createStoreCore, resolvePatch };
@@ -1,6 +1,5 @@
1
1
  import { StateValues } from "../schema/object.mjs";
2
2
  import { Store } from "./core.mjs";
3
- //#region src/store/kind.d.ts
4
3
  /** What `useAppState` needs of a definition beyond its public shape. */
5
4
  export type Kind = {
6
5
  /** The definition's store in the browser, made the first time it is read. */
@@ -23,5 +22,4 @@ export declare const kindOf: (def: {
23
22
  readonly kind: keyof typeof DEFINERS;
24
23
  readonly key: string;
25
24
  }) => Kind;
26
- //#endregion
27
25
  export {};
@@ -1,4 +1,4 @@
1
- //#region src/store/kind.ts
1
+ // src/store/kind.ts
2
2
  const DEFINERS = {
3
3
  page: "definePageState",
4
4
  local: "defineLocalState",
@@ -15,5 +15,4 @@ const kindOf = (def) => {
15
15
  if (found === void 0) throw new TypeError(`"${def.key}" was not created by ${DEFINERS[def.kind]} of this module instance`);
16
16
  return found;
17
17
  };
18
- //#endregion
19
18
  export { kindOf, setKind };
@@ -1,8 +1,6 @@
1
1
  import { StateValues } from "../schema/object.mjs";
2
2
  import { MemoryState } from "../memory-state.mjs";
3
3
  import { Store } from "./core.mjs";
4
- //#region src/store/memory-store.d.ts
5
4
  export declare const memoryStoreOf: (def: MemoryState) => Store;
6
5
  /** SSR renders the initial values — static, so no cross-request concern. */
7
- export declare const memoryInitialSnapshot: (def: MemoryState) => StateValues;
8
- //#endregion
6
+ export declare const memoryInitialSnapshot: (def: MemoryState) => StateValues;
@@ -1,6 +1,6 @@
1
1
  import { createHandle, createStoreCore, resolvePatch } from "./core.mjs";
2
2
  import { getOrCreateStore } from "./registry.mjs";
3
- //#region src/store/memory-store.ts
3
+ // src/store/memory-store.ts
4
4
  const createMemoryStore = (def) => {
5
5
  const keys = Object.keys(def.initial);
6
6
  const core = createStoreCore(keys, { ...def.initial });
@@ -24,5 +24,4 @@ const createMemoryStore = (def) => {
24
24
  const memoryStoreOf = (def) => getOrCreateStore("memory", def.key, () => createMemoryStore(def));
25
25
  /** SSR renders the initial values — static, so no cross-request concern. */
26
26
  const memoryInitialSnapshot = (def) => ({ ...def.initial });
27
- //#endregion
28
27
  export { memoryInitialSnapshot, memoryStoreOf };
@@ -1,7 +1,6 @@
1
1
  import { StateValues } from "../schema/object.mjs";
2
2
  import { PageInternals, PageState } from "../page-state.mjs";
3
3
  import { Store } from "./core.mjs";
4
- //#region src/store/page-store.d.ts
5
4
  export type UpdateOptions = {
6
5
  /**
7
6
  * `replace` by default: an update is a refinement of the current entry.
@@ -18,5 +17,4 @@ export declare const pageStoreOf: (def: PageState, page: PageInternals) => Store
18
17
  * reaches the server. Pure on purpose: the server must never touch
19
18
  * `navigation` or create a store.
20
19
  */
21
- export declare const pageInitialSnapshot: (page: PageInternals, initialUrl: Readonly<StateValues> | undefined) => StateValues;
22
- //#endregion
20
+ export declare const pageInitialSnapshot: (page: PageInternals, initialUrl: Readonly<StateValues> | undefined) => StateValues;
@@ -1,7 +1,7 @@
1
1
  import { isRecord, sameValue } from "../schema/object.mjs";
2
2
  import { createHandle, createStoreCore, resolvePatch } from "./core.mjs";
3
3
  import { getOrCreateStore } from "./registry.mjs";
4
- //#region src/store/page-store.ts
4
+ // src/store/page-store.ts
5
5
  const storedEntryState = () => navigation.currentEntry?.getState();
6
6
  const readLive = (def, page) => {
7
7
  const live = {};
@@ -48,6 +48,7 @@ const createPageStore = (def, page) => {
48
48
  if (returned) core.touch();
49
49
  };
50
50
  navigation.addEventListener("currententrychange", sync);
51
+ window.addEventListener("popstate", sync);
51
52
  /**
52
53
  * The whole navigation state travels with every write we make: the entry
53
54
  * state object is shared ground (the router's own state, other page
@@ -159,6 +160,7 @@ const createPageStore = (def, page) => {
159
160
  update,
160
161
  dispose: () => {
161
162
  navigation.removeEventListener("currententrychange", sync);
163
+ window.removeEventListener("popstate", sync);
162
164
  left.clear();
163
165
  bound.clear();
164
166
  core.clear();
@@ -180,5 +182,4 @@ const pageInitialSnapshot = (page, initialUrl) => {
180
182
  }
181
183
  return base;
182
184
  };
183
- //#endregion
184
185
  export { pageInitialSnapshot, pageStoreOf };
@@ -1,4 +1,3 @@
1
- //#region src/store/registry.d.ts
2
1
  export declare const getOrCreateStore: <Store extends {
3
2
  dispose?: () => void;
4
3
  }>(scope: string, key: string, create: () => Store) => Store;
@@ -7,5 +6,4 @@ export declare const getOrCreateStore: <Store extends {
7
6
  * would carry state from one test into the next. Unmount components first —
8
7
  * mounted hooks keep their old store through closures.
9
8
  */
10
- export declare const resetStateRegistry: () => void;
11
- //#endregion
9
+ export declare const resetStateRegistry: () => void;
Binary file
@@ -2,11 +2,9 @@ import { StateValues } from "../schema/object.mjs";
2
2
  import { RowCodec } from "../row/codec.mjs";
3
3
  import { LocalState, SessionState } from "../storage-state.mjs";
4
4
  import { Store } from "./core.mjs";
5
- //#region src/store/storage-store.d.ts
6
5
  /** What a store reads off a local or a session definition. */
7
6
  type Stored = Pick<LocalState | SessionState, 'kind' | 'key' | 'storageKey'>;
8
7
  export declare const storageStoreOf: (def: Stored, codec: RowCodec) => Store;
9
8
  /** SSR and hydration see the defaults — the server has no Web Storage. */
10
9
  export declare const storageInitialSnapshot: (codec: RowCodec) => StateValues;
11
- //#endregion
12
10
  export {};
@@ -1,6 +1,19 @@
1
1
  import { createHandle, createStoreCore, resolvePatch } from "./core.mjs";
2
2
  import { getOrCreateStore } from "./registry.mjs";
3
- //#region src/store/storage-store.ts
3
+ // src/store/storage-store.ts
4
+ const areaOf = (kind) => {
5
+ try {
6
+ return kind === "local" ? localStorage : sessionStorage;
7
+ } catch (error) {
8
+ const refuse = () => {
9
+ throw error;
10
+ };
11
+ return {
12
+ getItem: refuse,
13
+ setItem: refuse
14
+ };
15
+ }
16
+ };
4
17
  const createStorageStore = (def, codec, storage) => {
5
18
  const { storageKey } = def;
6
19
  const write = (values) => {
@@ -19,14 +32,18 @@ const createStorageStore = (def, codec, storage) => {
19
32
  const read = () => readRow().values;
20
33
  const upgrade = () => {
21
34
  queueMicrotask(() => {
22
- const { values, stale } = readRow();
23
- if (!stale) return;
24
35
  try {
25
- write(values);
36
+ const { values, stale } = readRow();
37
+ if (stale) write(values);
26
38
  } catch {}
27
39
  });
28
40
  };
29
- const first = readRow();
41
+ let first;
42
+ try {
43
+ first = readRow();
44
+ } catch {
45
+ first = codec.read(void 0);
46
+ }
30
47
  const core = createStoreCore(codec.keys, first.values);
31
48
  if (first.stale) upgrade();
32
49
  let pending = null;
@@ -34,7 +51,13 @@ const createStorageStore = (def, codec, storage) => {
34
51
  const onStorage = (event) => {
35
52
  if (event.storageArea !== storage) return;
36
53
  if (event.key !== storageKey && event.key !== null) return;
37
- const { values: fresh, stale } = readRow();
54
+ let row;
55
+ try {
56
+ row = readRow();
57
+ } catch {
58
+ return;
59
+ }
60
+ const { values: fresh, stale } = row;
38
61
  if (stale) upgrade();
39
62
  core.applyNext(pending === null ? fresh : {
40
63
  ...fresh,
@@ -87,8 +110,7 @@ const createStorageStore = (def, codec, storage) => {
87
110
  }
88
111
  };
89
112
  };
90
- const storageStoreOf = (def, codec) => getOrCreateStore(def.kind, def.key, () => createStorageStore(def, codec, def.kind === "local" ? localStorage : sessionStorage));
113
+ const storageStoreOf = (def, codec) => getOrCreateStore(def.kind, def.key, () => createStorageStore(def, codec, areaOf(def.kind)));
91
114
  /** SSR and hydration see the defaults — the server has no Web Storage. */
92
115
  const storageInitialSnapshot = (codec) => ({ ...codec.defaults });
93
- //#endregion
94
116
  export { storageInitialSnapshot, storageStoreOf };
@@ -1,4 +1,2 @@
1
1
  import { Context } from "react";
2
- //#region src/tree-pathname.d.ts
3
- export declare const TreePathname: Context<string | null>;
4
- //#endregion
2
+ export declare const TreePathname: Context<string | null>;
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { createContext } from "react";
3
- //#region src/tree-pathname.ts
3
+ // src/tree-pathname.ts
4
4
  /**
5
5
  * The pathname the tree a component renders in was applied for, in the URL's
6
6
  * terms, or `null` where no router says. Under interception the URL commits
@@ -10,5 +10,4 @@ import { createContext } from "react";
10
10
  */
11
11
  const KEY = Symbol.for("@k8ordo/router/tree-pathname");
12
12
  const TreePathname = globalThis[KEY] ??= createContext(null);
13
- //#endregion
14
13
  export { TreePathname };
@@ -1,5 +1,4 @@
1
1
  import { StateSchema, StateValues } from "../schema/object.mjs";
2
- //#region src/url/codec.d.ts
3
2
  /** `URLSearchParams`, or the object shape frameworks hand to a page. */
4
3
  export type UrlInput = URLSearchParams | Readonly<Record<string, string | readonly string[] | undefined>>;
5
4
  export type UrlCodec = {
@@ -10,5 +9,4 @@ export type UrlCodec = {
10
9
  /** Runs already-typed values through the schema, salvage included. */
11
10
  salvage: (values: Readonly<StateValues>) => StateValues;
12
11
  };
13
- export declare const createUrlCodec: (schema: StateSchema) => UrlCodec;
14
- //#endregion
12
+ export declare const createUrlCodec: (schema: StateSchema) => UrlCodec;
@@ -1,6 +1,6 @@
1
1
  import { analyzeSchema, parseWithSalvage, sameValue } from "../schema/object.mjs";
2
2
  import { safeEncode } from "zod/v4/core";
3
- //#region src/url/codec.ts
3
+ // src/url/codec.ts
4
4
  const defOf = (schema) => schema._zod.def;
5
5
  /**
6
6
  * Peels wrappers (`default`, `optional`, `catch`, …) and pipes down to the
@@ -91,5 +91,4 @@ const createUrlCodec = (schema) => {
91
91
  salvage
92
92
  };
93
93
  };
94
- //#endregion
95
94
  export { createUrlCodec };
@@ -6,7 +6,6 @@ import { MemoryState } from "./memory-state.mjs";
6
6
  import { UpdateHandle } from "./store/core.mjs";
7
7
  import { UpdateOptions } from "./store/page-store.mjs";
8
8
  import { output } from "zod/v4/core";
9
- //#region src/use-app-state.d.ts
10
9
  export type AnyState = PageState | LocalState | SessionState | CookieState | MemoryState;
11
10
  type StateOf<Def> = Def extends PageState<infer Url, infer Entry> ? OutputOf<Url> & OutputOf<Entry> : Def extends LocalState<infer Schema> ? output<Schema> : Def extends SessionState<infer Schema> ? output<Schema> : Def extends CookieState<infer Schema> ? output<Schema> : Def extends MemoryState<infer Values> ? Values : never;
12
11
  type UrlStateOf<Def> = Def extends PageState<infer Url> ? OutputOf<Url> : never;
@@ -33,5 +32,4 @@ type OptionsFor<Def> = Def extends {
33
32
  } : never;
34
33
  export declare function useAppState<Def extends AnyState>(def: Def, options?: OptionsFor<Def>): [StateOf<Def>, UpdateFn<Def>];
35
34
  export declare function useAppState<Def extends AnyState, const Key extends Extract<keyof StateOf<Def>, string>>(def: Def, keys: readonly Key[], options?: OptionsFor<Def>): [Pick<StateOf<Def>, Key>, UpdateFn<Def>];
36
- //#endregion
37
35
  export {};
@@ -2,7 +2,7 @@
2
2
  import { kindOf } from "./store/kind.mjs";
3
3
  import { TreePathname } from "./tree-pathname.mjs";
4
4
  import { use, useCallback, useMemo, useSyncExternalStore } from "react";
5
- //#region src/use-app-state.ts
5
+ // src/use-app-state.ts
6
6
  const storeOf = (def) => kindOf(def).store();
7
7
  const initialOf = (def, options) => kindOf(def).initial(options ?? {});
8
8
  const FULL = "*";
@@ -43,5 +43,4 @@ function useAppState(def, keysOrOptions, maybeOptions) {
43
43
  const getServerSnapshot = useCallback(() => serverSnapshot, [serverSnapshot]);
44
44
  return [useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot), useCallback((patch, updateOptions) => storeOf(def).update(patch, updateOptions, tree), [def, tree])];
45
45
  }
46
- //#endregion
47
46
  export { useAppState };
package/docs/GUIDE.md CHANGED
@@ -478,9 +478,9 @@ never surface it. Updates with no navigation behind them return the same
478
478
  shape: entry-only, local, session and no-change page handles settle once the
479
479
  batch is flushed, cookie handles once the Cookie Store API has written it,
480
480
  memory handles on the spot. A local, session or cookie write that fails —
481
- the browser refuses it (a full quota, a cookie over 4 KB), or the schema
482
- throws on the values it was about to write — rejects the handle with that
483
- error while the rendered value stays.
481
+ the browser refuses it (a full quota, blocked site data, a cookie over 4 KB),
482
+ or the schema throws on the values it was about to write — rejects the handle
483
+ with that error while the rendered value stays.
484
484
 
485
485
  - **Patches are validated on the spot.** The merged state goes through the
486
486
  schema inside `update()` itself, url fields by the road a URL arrival
@@ -693,6 +693,14 @@ with no library bookkeeping.
693
693
  the same keyed subscription granularity applies. sessionStorage belongs to one
694
694
  tab, so its `storage` event reaches only other frames of that tab.
695
695
 
696
+ **Blocked storage still renders.** A browser that blocks site data throws a
697
+ `SecurityError` on any read of `localStorage` or `sessionStorage`. Local and
698
+ session state render their defaults there, `update()` keeps a value in memory
699
+ until the next load, and each handle rejects with that error, since nothing
700
+ was stored. A `getItem` that throws on its own also renders the defaults;
701
+ after that, a `storage` event it cannot read leaves the values on screen, and
702
+ an `update()` rejects without writing, so the fields it could not read survive.
703
+
696
704
  **SSR sees real url values when your router hands you the search.** Pass the
697
705
  RSC-parsed url as `initialUrl` and the server render and the hydration render
698
706
  show the actual URL state. In `@k8ordo/framework`'s server mode a page that
package/docs/llms.txt CHANGED
@@ -57,7 +57,9 @@ When installed via npm, these docs live under `node_modules/@k8ordo/state/docs/`
57
57
  the schema's field-by-field salvage and is written back in the current
58
58
  version by the browser store (`parseCookies` migrates but never writes), a
59
59
  newer row is salvaged and left alone, and a throwing `migrate` reads as
60
- nothing stored without touching the row; a local or session definition also exposes
60
+ nothing stored without touching the row; where the browser blocks site
61
+ data, local and session state render their defaults and keep updates in
62
+ memory until the next load; a local or session definition also exposes
61
63
  `storageKey` (the key in its storage area, `k8ordo-state:<key>`) and
62
64
  `inlineRead()`, a JavaScript expression for an inline `<script>` that
63
65
  evaluates to the object stored in that area or `null` before hydration
@@ -81,7 +83,7 @@ When installed via npm, these docs live under `node_modules/@k8ordo/state/docs/`
81
83
  `cookieStore.set()`; memory applies per call), and returns
82
84
  `{ committed, finished }` promise handles (for non-navigating kinds they
83
85
  settle once the write lands, and a write that fails — the browser refuses
84
- it (a full Web Storage area, a cookie over 4 KB) or the schema throws on
86
+ it (a full or blocked Web Storage area, a cookie over 4 KB) or the schema throws on
85
87
  it — rejects them with its error;
86
88
  `{ history: 'push' }` exists only on page state and takes effect only when
87
89
  a url value changes).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k8ordo/state",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Declare state by where it lives — URL search params, history-entry state, localStorage, sessionStorage, a cookie, or memory. One zod schema per boundary place and a typed box for memory, over the Navigation API.",
5
5
  "keywords": [
6
6
  "cookie",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@arethetypeswrong/cli": "0.18.5",
46
- "@k8ordo/router": "^1.4.0",
46
+ "@k8ordo/router": "^1.4.2",
47
47
  "@types/react": "19.3.0",
48
48
  "@types/react-dom": "19.3.0",
49
49
  "@vitejs/plugin-basic-ssl": "2.3.0",
@@ -54,14 +54,14 @@
54
54
  "react": "19.3.1",
55
55
  "react-dom": "19.3.1",
56
56
  "typescript": "7.0.2",
57
- "vite": "npm:@voidzero-dev/vite-plus-core@1.3.0",
58
- "vite-plus": "1.3.0",
57
+ "vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
58
+ "vite-plus": "1.4.0",
59
59
  "vitest": "5.0.5",
60
60
  "vitest-browser-react": "2.3.0",
61
61
  "zod": "4.6.7"
62
62
  },
63
63
  "peerDependencies": {
64
- "@k8ordo/router": "^1.4.0",
64
+ "@k8ordo/router": "^1.4.2",
65
65
  "@types/react": ">=19.3.0",
66
66
  "react": ">=19.3.0",
67
67
  "typescript": ">=7.0.0",