@plitzi/sdk-shared 0.39.0 → 0.39.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @plitzi/sdk-shared
2
2
 
3
+ ## 0.39.1
4
+
5
+ ### Patch Changes
6
+
7
+ - f174a61: Released with every `@plitzi` package at this version. What changed in each, and what to do when you upgrade, is in
8
+ `@plitzi/cli`'s `CHANGELOG.md`: one copy of the notes, not one per package.
9
+
3
10
  ## 0.39.0
4
11
 
5
12
  ### Minor Changes
@@ -4,7 +4,7 @@ import { Schema } from '../types';
4
4
  * builder writes (`SCHEMA_UPDATE_SETTINGS`) and the reader of a space keeps. A key outside it is one nothing reads any
5
5
  * more — `head`, from before a space declared its fonts.
6
6
  */
7
- export declare const SCHEMA_SETTINGS: readonly ["keepState", "stateStorage", "transientState", "keptState", "paintedState", "robotsTxt", "blockAiCrawlers", "viewTransitions", "siteName", "language", "favicon", "shareImage", "themeColor", "customCss", "withheldPage", "visitorRoles", "userProvider", "tokenStorage", "loginUrl", "userUrl", "refreshUrl", "logoutUrl", "revokeUrl", "detailsPath", "tokenPath", "refreshTokenPath", "expirationTimePath", "refreshExpirationTimePath", "sessionHintCookie", "sessionExchangeUrl", "mfaUrl", "sessionGate", "sessionRevalidateSeconds", "debugMode", "computed", "channels"];
7
+ export declare const SCHEMA_SETTINGS: readonly ["keepState", "stateStorage", "transientState", "keptState", "paintedState", "browserState", "robotsTxt", "blockAiCrawlers", "viewTransitions", "siteName", "language", "favicon", "shareImage", "themeColor", "customCss", "withheldPage", "visitorRoles", "userProvider", "tokenStorage", "loginUrl", "userUrl", "refreshUrl", "logoutUrl", "revokeUrl", "detailsPath", "tokenPath", "refreshTokenPath", "expirationTimePath", "refreshExpirationTimePath", "sessionHintCookie", "sessionExchangeUrl", "mfaUrl", "sessionGate", "sessionRevalidateSeconds", "debugMode", "computed", "channels"];
8
8
  /** Whether a key is a setting a space may carry: one the builder may write, though the space has no value for it yet. */
9
9
  export declare const isSchemaSetting: (key: string) => key is (typeof SCHEMA_SETTINGS)[number];
10
10
  /**
@@ -1,4 +1,4 @@
1
1
  //#region src/schema/schemaSettings.ts
2
- var e = /* @__PURE__ */ "keepState.stateStorage.transientState.keptState.paintedState.robotsTxt.blockAiCrawlers.viewTransitions.siteName.language.favicon.shareImage.themeColor.customCss.withheldPage.visitorRoles.userProvider.tokenStorage.loginUrl.userUrl.refreshUrl.logoutUrl.revokeUrl.detailsPath.tokenPath.refreshTokenPath.expirationTimePath.refreshExpirationTimePath.sessionHintCookie.sessionExchangeUrl.mfaUrl.sessionGate.sessionRevalidateSeconds.debugMode.computed.channels".split("."), t = new Set(e), n = (e) => t.has(e);
2
+ var e = /* @__PURE__ */ "keepState.stateStorage.transientState.keptState.paintedState.browserState.robotsTxt.blockAiCrawlers.viewTransitions.siteName.language.favicon.shareImage.themeColor.customCss.withheldPage.visitorRoles.userProvider.tokenStorage.loginUrl.userUrl.refreshUrl.logoutUrl.revokeUrl.detailsPath.tokenPath.refreshTokenPath.expirationTimePath.refreshExpirationTimePath.sessionHintCookie.sessionExchangeUrl.mfaUrl.sessionGate.sessionRevalidateSeconds.debugMode.computed.channels".split("."), t = new Set(e), n = (e) => t.has(e);
3
3
  //#endregion
4
4
  export { e as SCHEMA_SETTINGS, n as isSchemaSetting };
@@ -1,3 +1,3 @@
1
- import { PAINTED_STATE_BUDGET as e, clearPaintedEntry as t, paintedEntryFromCookies as n, paintedKeys as r, paintedStateCookieName as i, paintedStateFor as a, pickPainted as o, readPaintedEntry as s, writePaintedEntry as c } from "./paintedState.mjs";
2
- import { runtimeStatePersist as l, stateOwner as u } from "./runtimeStatePersist.mjs";
3
- export { e as PAINTED_STATE_BUDGET, t as clearPaintedEntry, n as paintedEntryFromCookies, r as paintedKeys, i as paintedStateCookieName, a as paintedStateFor, o as pickPainted, s as readPaintedEntry, l as runtimeStatePersist, u as stateOwner, c as writePaintedEntry };
1
+ import { PAINTED_STATE_BUDGET as e, accountOwner as t, browserKeys as n, clearPaintedEntry as r, paintedEntryFromCookies as i, paintedKeys as a, paintedStateCookieName as o, paintedStateFor as s, pickPainted as c, readPaintedEntry as l, writePaintedEntry as u } from "./paintedState.mjs";
2
+ import { runtimeStatePersist as d, stateOwner as f } from "./runtimeStatePersist.mjs";
3
+ export { e as PAINTED_STATE_BUDGET, t as accountOwner, n as browserKeys, r as clearPaintedEntry, i as paintedEntryFromCookies, a as paintedKeys, o as paintedStateCookieName, s as paintedStateFor, c as pickPainted, l as readPaintedEntry, d as runtimeStatePersist, f as stateOwner, u as writePaintedEntry };
@@ -12,8 +12,11 @@
12
12
  * every request, so it holds only what the first paint needs, and never more than {@link PAINTED_STATE_BUDGET}.
13
13
  *
14
14
  * The entry carries who wrote it, as the kept state in web storage does, and a page that finds it written by somebody
15
- * else discards what it rendered with. The server cannot tell — who the visitor is, for a space with its own sign-in,
16
- * is only settled in the browser.
15
+ * else discards what it rendered with — all but the browser's keys (`browserState`), which are nobody's. The server
16
+ * can tell only half of that — who the visitor is, for a space with its own sign-in, is only settled in the browser — so
17
+ * it draws an entry whole unless the request proved an account and the entry names another: the page's providers read
18
+ * the painted values before its auth settles, and a dashboard drawn from the previous account's cookie asked for that
19
+ * account's workspace before the page could discard it.
17
20
  */
18
21
  /** Bytes of the encoded value: well inside the 4096 a browser allows a cookie, name and attributes included. */
19
22
  export declare const PAINTED_STATE_BUDGET = 3072;
@@ -21,6 +24,8 @@ export type PaintedEntry = {
21
24
  owner: string;
22
25
  values: Record<string, unknown>;
23
26
  };
27
+ /** How an account is written as the owner of an entry — here and on the kept state in web storage alike. */
28
+ export declare const accountOwner: (id: number | string) => string;
24
29
  /**
25
30
  * Per space, as the web storage key is (`plitzi_<webId>_state`) — and per port, because a cookie's scope is the host
26
31
  * without it: two spaces served from `localhost` on two ports would otherwise read each other's. See `debugCookieName`.
@@ -30,6 +35,13 @@ export declare const paintedStateCookieName: (webId: number, host: string | unde
30
35
  export declare const paintedKeys: (settings: {
31
36
  paintedState?: unknown;
32
37
  } | undefined) => Set<string>;
38
+ /**
39
+ * The kept keys a space says are the browser's rather than whoever is signed in (`settings.browserState`): a choice
40
+ * made of the browser itself — the cookie consent — that signing in, out, or as somebody else does not take away.
41
+ */
42
+ export declare const browserKeys: (settings: {
43
+ browserState?: unknown;
44
+ } | undefined) => Set<string>;
33
45
  /** The declared keys' values out of `runtime.state` — a key with no value is left out, not written as `undefined`. */
34
46
  export declare const pickPainted: (state: unknown, keys: ReadonlySet<string>) => Record<string, unknown>;
35
47
  /**
@@ -39,9 +51,13 @@ export declare const pickPainted: (state: unknown, keys: ReadonlySet<string>) =>
39
51
  export declare const paintedEntryFromCookies: (cookies: string | undefined, name: string) => PaintedEntry | undefined;
40
52
  /**
41
53
  * What the server renders with: the entry's values for the keys the space declares — a key it no longer declares, or
42
- * one the visitor added, is not the server's to render.
54
+ * one the visitor added, is not the server's to render — and, when `account`, the one the request proved, is not the
55
+ * one that wrote it, only the browser's keys.
43
56
  */
44
- export declare const paintedStateFor: (cookies: string | undefined, name: string, keys: ReadonlySet<string>) => Record<string, unknown> | undefined;
57
+ export declare const paintedStateFor: (cookies: string | undefined, name: string, settings: {
58
+ paintedState?: unknown;
59
+ browserState?: unknown;
60
+ } | undefined, account?: number | string) => Record<string, unknown> | undefined;
45
61
  /** The entry in this document's cookies. */
46
62
  export declare const readPaintedEntry: (name: string) => PaintedEntry | undefined;
47
63
  /**
@@ -1,10 +1,10 @@
1
1
  import { isRecord as e } from "../helpers/isRecord.mjs";
2
2
  import { cookieFromHeader as t, documentCookies as n } from "../helpers/cookies.mjs";
3
3
  //#region src/state/paintedState.ts
4
- var r = 3072, i = 31536e3, a = (e, t) => {
4
+ var r = 3072, i = 31536e3, a = (e) => `user:${String(e)}`, o = (e, t) => {
5
5
  let n = t?.split(":")[1];
6
6
  return n ? `plitzi_${e}_painted_${n}` : `plitzi_${e}_painted`;
7
- }, o = (e) => new Set(Array.isArray(e?.paintedState) ? e.paintedState.filter((e) => typeof e == "string" && e !== "") : []), s = (t, n) => e(t) ? Object.fromEntries(Object.entries(t).filter(([e, t]) => n.has(e) && t !== void 0)) : {}, c = (n, r) => {
7
+ }, s = (e) => new Set(Array.isArray(e) ? e.filter((e) => typeof e == "string" && e !== "") : []), c = (e) => s(e?.paintedState), l = (e) => s(e?.browserState), u = (t, n) => e(t) ? Object.fromEntries(Object.entries(t).filter(([e, t]) => n.has(e) && t !== void 0)) : {}, d = (n, r) => {
8
8
  let i = t(n, r);
9
9
  if (i !== void 0) try {
10
10
  let t = JSON.parse(i);
@@ -15,24 +15,25 @@ var r = 3072, i = 31536e3, a = (e, t) => {
15
15
  } catch {
16
16
  return;
17
17
  }
18
- }, l = (e, t, n) => {
19
- if (n.size === 0) return;
20
- let r = c(e, t);
21
- if (!r) return;
22
- let i = s(r.values, n);
23
- return Object.keys(i).length > 0 ? i : void 0;
24
- }, u = (e) => c(n(), e), d = (e, t) => {
18
+ }, f = (e, t, n, r) => {
19
+ let i = c(n);
20
+ if (i.size === 0) return;
21
+ let o = d(e, t);
22
+ if (!o) return;
23
+ let s = r !== void 0 && o.owner.startsWith("user:") && o.owner !== a(r), f = l(n), p = u(o.values, s ? new Set([...i].filter((e) => f.has(e))) : i);
24
+ return Object.keys(p).length > 0 ? p : void 0;
25
+ }, p = (e) => d(n(), e), m = (e, t) => {
25
26
  let n = encodeURIComponent(JSON.stringify(t));
26
- if (n.length > 3072) return f(e), "too-large";
27
+ if (n.length > 3072) return h(e), "too-large";
27
28
  try {
28
29
  return document.cookie = `${e}=${n};path=/;max-age=${i};SameSite=Lax`, "written";
29
30
  } catch {
30
31
  return "unavailable";
31
32
  }
32
- }, f = (e) => {
33
+ }, h = (e) => {
33
34
  try {
34
35
  document.cookie = `${e}=;path=/;max-age=0;SameSite=Lax`;
35
36
  } catch {}
36
37
  };
37
38
  //#endregion
38
- export { r as PAINTED_STATE_BUDGET, f as clearPaintedEntry, c as paintedEntryFromCookies, o as paintedKeys, a as paintedStateCookieName, l as paintedStateFor, s as pickPainted, u as readPaintedEntry, d as writePaintedEntry };
39
+ export { r as PAINTED_STATE_BUDGET, a as accountOwner, l as browserKeys, h as clearPaintedEntry, d as paintedEntryFromCookies, c as paintedKeys, o as paintedStateCookieName, f as paintedStateFor, u as pickPainted, p as readPaintedEntry, m as writePaintedEntry };
@@ -1,28 +1,28 @@
1
1
  import { isRecord as e } from "../helpers/isRecord.mjs";
2
2
  import { pConsole as t } from "../devTools/utils/PlitziConsole.mjs";
3
- import { clearPaintedEntry as n, paintedKeys as r, paintedStateCookieName as i, pickPainted as a, readPaintedEntry as o, writePaintedEntry as s } from "./paintedState.mjs";
4
- import { persistMiddleware as c } from "@plitzi/nexus";
3
+ import { accountOwner as n, browserKeys as r, clearPaintedEntry as i, paintedKeys as a, paintedStateCookieName as o, pickPainted as s, readPaintedEntry as c, writePaintedEntry as l } from "./paintedState.mjs";
4
+ import { persistMiddleware as u } from "@plitzi/nexus";
5
5
  //#region src/state/runtimeStatePersist.ts
6
- var l = /* @__PURE__ */ new Set([
6
+ var d = /* @__PURE__ */ new Set([
7
7
  "init",
8
8
  "initLoading",
9
9
  "authenticating"
10
- ]), u = (e) => {
10
+ ]), f = (e) => {
11
11
  let t = e.runtime?.sources.auth;
12
12
  if (t) {
13
13
  if (t.status === void 0) return { kind: "browser" };
14
- if (!l.has(t.status)) return t.isAuthenticated && t.details?.id !== void 0 ? {
14
+ if (!d.has(t.status)) return t.isAuthenticated && t.details?.id !== void 0 ? {
15
15
  kind: "account",
16
16
  id: String(t.details.id)
17
17
  } : { kind: "guest" };
18
18
  }
19
- }, d = (e) => {
19
+ }, p = (e) => {
20
20
  switch (e.kind) {
21
- case "account": return `user:${e.id}`;
21
+ case "account": return n(e.id);
22
22
  case "guest": return "guest";
23
23
  case "browser": return "";
24
24
  }
25
- }, f = (e, t) => ({
25
+ }, m = (e, t) => ({
26
26
  getItem: (n) => {
27
27
  let r = e.getItem(n);
28
28
  if (r === null) return null;
@@ -37,83 +37,136 @@ var l = /* @__PURE__ */ new Set([
37
37
  payload: r
38
38
  })),
39
39
  removeItem: (t) => e.removeItem(t)
40
- }), p = "runtime.state", m = (e, t) => Object.fromEntries(Object.entries(e).filter(([e]) => !t.has(e))), h = (e) => e.schema?.settings, g = (e) => new Set(Array.isArray(e) ? e.filter((e) => typeof e == "string" && e !== "") : []), _ = (e) => {
41
- let t = g(e?.keptState);
40
+ }), h = "runtime.state", g = (e, t) => Object.fromEntries(Object.entries(e).filter(([e]) => !t.has(e))), _ = (e) => e.schema?.settings, v = (e) => new Set(Array.isArray(e) ? e.filter((e) => typeof e == "string" && e !== "") : []), y = (e) => {
41
+ let t = v(e?.keptState);
42
42
  if (t.size > 0) return (e) => t.has(e);
43
- let n = g(e?.transientState);
43
+ let n = v(e?.transientState);
44
44
  return (e) => !n.has(e);
45
- }, v = (t, n) => e(t) ? Object.fromEntries(Object.entries(t).filter(([e]) => n(e))) : {}, y = (e) => {
45
+ }, b = (t, n) => e(t) ? Object.fromEntries(Object.entries(t).filter(([e]) => n(e))) : {}, x = (e) => {
46
+ let t = y(e), n = r(e);
47
+ return (e) => t(e) && !n.has(e);
48
+ }, S = (e) => {
46
49
  try {
47
50
  return e === "session" ? globalThis.sessionStorage : globalThis.localStorage;
48
51
  } catch {
49
52
  return;
50
53
  }
51
- }, b = (c, l) => {
52
- let f = i(l, typeof window > "u" ? void 0 : window.location.host), p = !1, g;
54
+ }, C = (n, u) => {
55
+ let d = o(u, typeof window > "u" ? void 0 : window.location.host), m = !1, h;
53
56
  return () => {
54
- let i = c.getState();
55
- if (typeof document > "u" || i.render?.isHydrating && !i.render.hydrated) return;
56
- let l = h(i), _ = r(l), v = u(i);
57
- if (!l?.keepState || _.size === 0 || v === void 0) return;
58
- let y = d(v), b = i.runtime?.state;
59
- if (!p) {
60
- p = !0;
61
- let t = o(f);
62
- if (t && t.owner !== y && (n(f), e(b) && [..._].some((e) => e in b))) {
63
- c.setState("runtime.state", m(b, _));
64
- return;
57
+ let o = n.getState();
58
+ if (typeof document > "u" || o.render?.isHydrating && !o.render.hydrated) return;
59
+ let u = _(o), v = a(u), y = f(o);
60
+ if (!u?.keepState || v.size === 0 || y === void 0) return;
61
+ let b = p(y), x = o.runtime?.state;
62
+ if (!m) {
63
+ m = !0;
64
+ let t = c(d);
65
+ if (t && t.owner !== b) {
66
+ let t = r(u), i = new Set([...v].filter((e) => !t.has(e)));
67
+ if (e(x) && [...i].some((e) => e in x)) {
68
+ n.setState("runtime.state", g(x, i));
69
+ return;
70
+ }
65
71
  }
66
- g = t?.owner === y ? JSON.stringify(a(t.values, _)) : void 0;
72
+ h = t?.owner === b ? JSON.stringify(s(t.values, v)) : void 0;
67
73
  }
68
- let x = a(b, _), S = JSON.stringify(x);
69
- S !== g && (g = S, Object.keys(x).length === 0 ? n(f) : s(f, {
70
- owner: y,
71
- values: x
72
- }) === "too-large" && t.warning("store", `settings.paintedState holds more than a cookie can carry — the first paint uses the space's defaults for ${[..._].join(", ")}. Declare only what the first paint shows.`, {
74
+ let S = s(x, v), C = JSON.stringify(S);
75
+ C !== h && (h = C, Object.keys(S).length === 0 ? i(d) : l(d, {
76
+ owner: b,
77
+ values: S
78
+ }) === "too-large" && t.warning("store", `settings.paintedState holds more than a cookie can carry — the first paint uses the space's defaults for ${[...v].join(", ")}. Declare only what the first paint shows.`, {
73
79
  storeName: "runtime",
74
80
  path: "runtime.state",
75
81
  prev: void 0,
76
- next: x
82
+ next: S
77
83
  }));
78
84
  };
79
- }, x = (t) => {
80
- let n = c({
85
+ }, w = (e) => `plitzi_${String(e)}_state_browser`, T = (e, t) => {
86
+ try {
87
+ let n = e.getItem(t);
88
+ return n === null ? void 0 : JSON.parse(n);
89
+ } catch {
90
+ return;
91
+ }
92
+ }, E = (n, i) => {
93
+ let a = w(i), o = !1, s;
94
+ return () => {
95
+ let i = n.getState();
96
+ if (i.render?.isHydrating && !i.render.hydrated) return;
97
+ let c = _(i), l = r(c), u = S(c?.stateStorage === "sessionStorage" ? "session" : "local");
98
+ if (!c?.keepState || l.size === 0 || !u) return;
99
+ let d = i.runtime?.state, f = (e) => b(e, (e) => l.has(e));
100
+ if (!o) {
101
+ o = !0;
102
+ let t = f(T(u, a));
103
+ if (s = JSON.stringify(t), Object.keys(t).length > 0 && JSON.stringify({
104
+ ...f(d),
105
+ ...t
106
+ }) !== JSON.stringify(f(d))) {
107
+ n.setState("runtime.state", {
108
+ ...e(d) ? d : {},
109
+ ...t
110
+ });
111
+ return;
112
+ }
113
+ }
114
+ let p = f(d), m = JSON.stringify(p);
115
+ if (m !== s) {
116
+ s = m;
117
+ try {
118
+ Object.keys(p).length === 0 ? u.removeItem(a) : u.setItem(a, m);
119
+ } catch {
120
+ t.warning("store", `settings.browserState could not be kept — the browser refused to store ${[...l].join(", ")}, so the next visit starts without them.`, {
121
+ storeName: "runtime",
122
+ path: "runtime.state",
123
+ prev: void 0,
124
+ next: p
125
+ });
126
+ }
127
+ }
128
+ };
129
+ }, D = (t) => {
130
+ let n = u({
81
131
  key: `plitzi_${t}_state`,
82
- paths: [p],
132
+ paths: [h],
83
133
  partializePath: (t, n, r) => {
84
- let i = _(h(r));
85
- return e(n) ? v(n, i) : n;
134
+ let i = x(_(r));
135
+ return e(n) ? b(n, i) : n;
86
136
  },
87
137
  mergePath: (t, n, r, i) => {
88
- let a = _(h(i));
138
+ let a = x(_(i));
89
139
  return e(n) ? {
90
- ...v(n, a),
91
- ...v(r, (e) => !a(e))
140
+ ...b(r, () => !0),
141
+ ...b(n, a)
92
142
  } : n;
93
143
  },
94
144
  storage: (e) => {
95
145
  let { render: t } = e;
96
146
  if (t?.isHydrating && !t.hydrated) return !1;
97
- let n = h(e), r = u(e);
147
+ let n = _(e), r = f(e);
98
148
  if (!n?.keepState || r === void 0) return !1;
99
- let i = y(n.stateStorage === "sessionStorage" ? "session" : "local");
100
- return i ? f(i, d(r)) : !1;
149
+ let i = S(n.stateStorage === "sessionStorage" ? "session" : "local");
150
+ return i ? m(i, p(r)) : !1;
101
151
  }
102
152
  });
103
153
  return (i) => {
104
- let a = n(i), o = i.getState().runtime?.state, s = u(i.getState()), c = b(i, t);
154
+ let o = n(i), s = i.getState().runtime?.state, c = f(i.getState()), l = C(i, t), u = E(i, t);
105
155
  return {
106
- ...a,
156
+ ...o,
107
157
  onChange: (t) => {
108
- let n = s, l = u(i.getState());
109
- if (l !== void 0 && (s = l), n?.kind === "account" && l !== void 0 && d(l) !== d(n)) {
110
- let t = e(o) ? m(o, r(h(i.getState()))) : o;
111
- i.setState("runtime.state", t);
158
+ let n = c, d = f(i.getState());
159
+ if (d !== void 0 && (c = d), n?.kind === "account" && d !== void 0 && p(d) !== p(n)) {
160
+ let t = _(i.getState()), n = b(i.getState().runtime?.state, (e) => r(t).has(e)), o = e(s) ? g(s, a(t)) : s, c = Object.keys(n).length > 0 ? {
161
+ ...e(o) ? o : {},
162
+ ...n
163
+ } : o;
164
+ i.setState("runtime.state", c);
112
165
  }
113
- a?.onChange?.(t), c();
166
+ o?.onChange?.(t), l(), u();
114
167
  }
115
168
  };
116
169
  };
117
170
  };
118
171
  //#endregion
119
- export { x as runtimeStatePersist, u as stateOwner };
172
+ export { D as runtimeStatePersist, f as stateOwner };