autosolutions-os 0.4.2 → 0.4.3

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,29 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.3 - 2026-11-06
4
+
5
+ The company switcher works in Ops Hub and Asset Portal with no Hub tab open
6
+ (Didac asked on 6 November 2026: "this should be working across everywhere").
7
+
8
+ Those two apps keep their own Supabase projects and borrow the Hub's session
9
+ from the shared cookie for their menu and company switcher. Only the Hub and
10
+ the apps on the core may refresh it, so an hour with no Hub tab open left the
11
+ borrowed token useless: a new page showed the app's fixed plate with no
12
+ switcher, and a switch from a page already open failed with "Couldn't switch".
13
+
14
+ - `autosolutions-os/core-view`: a call that finds the token run out asks the Hub
15
+ to renew it (`renewHubSession`), in a hidden frame on the Hub's blank page
16
+ `/renew-session`. The Hub's own client refreshes by the rules in session.ts
17
+ and writes the shared cookie; the call waits for that, up to 15 seconds, then
18
+ goes on. The reader still never touches a token itself.
19
+ - The frame shows "checking" while that runs, then the Hub's frame.
20
+ - One renewal at a time on a page, and five minutes before the next try after
21
+ one that did not work. A session that has ended or was signed out is not
22
+ brought back: the frame stays the app's fixed one.
23
+ - The page must be live on the Hub first (autosolutions-os PR #67). Before that,
24
+ a renewal gives up after 15 seconds and the frame is the fixed one, as in
25
+ 0.4.2.
26
+
3
27
  ## 0.4.2 - 2026-11-05
4
28
 
5
29
  The ? becomes a Feedback button that people see, and pasting a picture into
package/README.md CHANGED
@@ -427,9 +427,15 @@ sees, without the token) is read-only:
427
427
  only while that token is good (more than 30 seconds left). It never signs in,
428
428
  never refreshes the token, never writes a cookie or storage, and never
429
429
  publishes a session: a refresh would rotate the Hub's refresh token behind
430
- the Hub's back. A token that has expired since the frame was read keeps the
431
- frame; on a fresh page it is the fallback until the Hub (or any app on the
432
- core) refreshes it, and the frame follows when the window comes back;
430
+ the Hub's back;
431
+ - when the token has run out, as it does after an hour with no Hub tab open, it
432
+ asks the Hub to renew it (0.4.3): the Hub's blank page `/renew-session` in a
433
+ hidden frame, whose own client refreshes and writes the shared cookie. The
434
+ call waits for that, up to 15 seconds, with the frame on "checking", then goes
435
+ on. A token that has expired since the frame was read keeps the frame, and
436
+ the next call renews it. If the Hub cannot renew it (the session has ended, or
437
+ the Hub does not answer), the frame is the fallback and the next try is five
438
+ minutes later;
433
439
  - it sends the token to the core's REST endpoint and nowhere else, for
434
440
  `os_context`, `my_admin_scope` and `switch_company_for_app` only
435
441
  (`CORE_VIEW_FUNCTIONS`); any other name is refused before anything is sent;
@@ -23,6 +23,23 @@ export type HubSession = {
23
23
  * shared session, signed out at the Hub, someone else, or cookies blocked.
24
24
  */
25
25
  export declare function hubSession(email: string | null | undefined): HubSession | null;
26
+ /** The Hub's blank page that renews its session (the Hub's src/routes/renew-session.tsx). */
27
+ export declare const HUB_RENEW_PATH = "/renew-session";
28
+ /** Test seam: forget this page's renewal. */
29
+ export declare function resetHubRenewal(): void;
30
+ /**
31
+ * Ask the Hub to renew the session this person's token came from, and wait
32
+ * for a good token in the shared cookie. True when one is there. One at a time
33
+ * on a page, and none for five minutes after one that did not work.
34
+ *
35
+ * Only the Hub's own client touches the session. A session that has ended, or
36
+ * a signed-out one, is not brought back: the Hub's rules decide, and this then
37
+ * answers false, and the frame stays the app's fixed one.
38
+ */
39
+ export declare function renewHubSession(email: () => string | null | undefined, timing?: {
40
+ look?: number;
41
+ wait?: number;
42
+ }): Promise<boolean>;
26
43
  /** What a call answers: the shape supabase-js's `rpc` resolves to, as the package's frame code reads it. */
27
44
  export type CoreViewResult = {
28
45
  data: unknown;
@@ -39,13 +56,16 @@ export type CoreViewClientOptions = {
39
56
  email: () => string | null | undefined;
40
57
  /** For tests: the fetch to use. Default the global one. */
41
58
  fetch?: typeof fetch;
59
+ /** For tests: how the Hub is asked to renew its session. Default `renewHubSession`. */
60
+ renew?: (email: () => string | null | undefined) => Promise<boolean>;
42
61
  };
43
62
  /**
44
63
  * A client for the core that borrows the Hub's session from the shared
45
64
  * cookie, read at every call: `rpc(fn, args)` for os_context,
46
65
  * my_admin_scope and switch_company_for_app, posted to the core's REST
47
- * endpoint with the session's access token, as supabase-js would. With no
48
- * usable Hub session for the app's person, or for any other function, it
49
- * answers with an error and sends nothing.
66
+ * endpoint with the session's access token, as supabase-js would. A token
67
+ * that has run out waits for the Hub to renew it first (`renewHubSession`).
68
+ * With no usable Hub session for the app's person, or for any other function,
69
+ * it answers with an error and sends nothing.
50
70
  */
51
71
  export declare function createCoreViewClient(options: CoreViewClientOptions): CoreViewClient;
@@ -16,6 +16,10 @@
16
16
  * cookie on every call and uses the access token in it while that token is
17
17
  * good. A refresh would rotate the Hub's refresh token behind the Hub's
18
18
  * back; only the Hub and the apps on the core refresh it.
19
+ * - When that token has run out, which an hour with no Hub tab open does, it
20
+ * asks the Hub to renew it (0.4.3): the Hub's own blank page in a hidden
21
+ * frame, whose client refreshes by the rules in session.ts and writes the
22
+ * shared cookie. The reader waits for the cookie, then goes on.
19
23
  * - The token goes to the core's REST endpoint and nowhere else, for three
20
24
  * functions only (CORE_VIEW_FUNCTIONS). Any other name is refused before
21
25
  * anything is sent. The token is never handed to the app.
@@ -26,7 +30,7 @@
26
30
  * Hub, or for someone else, there is no Hub session and the reader answers
27
31
  * every call with an error, sending nothing. No React, no supabase-js.
28
32
  */
29
- import { coreProjectRef, corePublishableKey, coreUrl, PARENT_DOMAIN } from "./config.js";
33
+ import { coreProjectRef, corePublishableKey, coreUrl, HUB_URL, PARENT_DOMAIN } from "./config.js";
30
34
  import { joinChunks, parseCopy, parseSignedOutNote, sharedSessionCookieName, signedOutCookieName, signedOutCopy, } from "./session.js";
31
35
  /** The only core functions the reader calls: the frame's two questions, and the company switch. */
32
36
  export const CORE_VIEW_FUNCTIONS = ["os_context", "my_admin_scope", "switch_company_for_app"];
@@ -94,14 +98,71 @@ function readShared(email) {
94
98
  export function hubSession(email) {
95
99
  return readShared(email)?.session ?? null;
96
100
  }
101
+ /** The Hub's blank page that renews its session (the Hub's src/routes/renew-session.tsx). */
102
+ export const HUB_RENEW_PATH = "/renew-session";
103
+ /** How often the cookie is read while the Hub renews, and how long it gets. */
104
+ const RENEW_LOOK_MS = 250;
105
+ const RENEW_WAIT_MS = 15_000;
106
+ /** After a renewal that did not work, the wait before the next try: the Hub's page is not small. */
107
+ const RENEW_GAP_MS = 5 * 60_000;
108
+ let renewal = null;
109
+ let renewFailedAt = null;
110
+ /** Test seam: forget this page's renewal. */
111
+ export function resetHubRenewal() {
112
+ renewal = null;
113
+ renewFailedAt = null;
114
+ }
115
+ /**
116
+ * Ask the Hub to renew the session this person's token came from, and wait
117
+ * for a good token in the shared cookie. True when one is there. One at a time
118
+ * on a page, and none for five minutes after one that did not work.
119
+ *
120
+ * Only the Hub's own client touches the session. A session that has ended, or
121
+ * a signed-out one, is not brought back: the Hub's rules decide, and this then
122
+ * answers false, and the frame stays the app's fixed one.
123
+ */
124
+ export function renewHubSession(email, timing = {}) {
125
+ if (renewal)
126
+ return renewal;
127
+ if (typeof document === "undefined" || typeof document.createElement !== "function" || !document.body) {
128
+ return Promise.resolve(false);
129
+ }
130
+ if (renewFailedAt !== null && Date.now() - renewFailedAt < RENEW_GAP_MS)
131
+ return Promise.resolve(false);
132
+ const frame = document.createElement("iframe");
133
+ frame.src = `${HUB_URL}${HUB_RENEW_PATH}`;
134
+ frame.title = "AutoSolutions sign-in";
135
+ frame.hidden = true;
136
+ frame.tabIndex = -1;
137
+ frame.setAttribute("aria-hidden", "true");
138
+ document.body.appendChild(frame);
139
+ const started = Date.now();
140
+ const wait = timing.wait ?? RENEW_WAIT_MS;
141
+ renewal = new Promise((resolve) => {
142
+ const timer = setInterval(() => {
143
+ const session = hubSession(email());
144
+ if (session !== null && !session.usable && Date.now() - started < wait)
145
+ return;
146
+ clearInterval(timer);
147
+ frame.remove();
148
+ renewal = null;
149
+ const renewed = session?.usable === true;
150
+ /* No session any more (signed out, or someone else) is no failure: nothing to try again. */
151
+ renewFailedAt = session !== null && !session.usable ? Date.now() : null;
152
+ resolve(renewed);
153
+ }, timing.look ?? RENEW_LOOK_MS);
154
+ });
155
+ return renewal;
156
+ }
97
157
  const refused = (message) => ({ data: null, error: { message } });
98
158
  /**
99
159
  * A client for the core that borrows the Hub's session from the shared
100
160
  * cookie, read at every call: `rpc(fn, args)` for os_context,
101
161
  * my_admin_scope and switch_company_for_app, posted to the core's REST
102
- * endpoint with the session's access token, as supabase-js would. With no
103
- * usable Hub session for the app's person, or for any other function, it
104
- * answers with an error and sends nothing.
162
+ * endpoint with the session's access token, as supabase-js would. A token
163
+ * that has run out waits for the Hub to renew it first (`renewHubSession`).
164
+ * With no usable Hub session for the app's person, or for any other function,
165
+ * it answers with an error and sends nothing.
105
166
  */
106
167
  export function createCoreViewClient(options) {
107
168
  const allowed = new Set(CORE_VIEW_FUNCTIONS);
@@ -109,11 +170,17 @@ export function createCoreViewClient(options) {
109
170
  async rpc(fn, args = {}) {
110
171
  if (!allowed.has(fn))
111
172
  return refused(`${fn}: not one of the core functions a frame reads`);
112
- const shared = readShared(options.email());
173
+ let shared = readShared(options.email());
113
174
  if (!shared)
114
175
  return refused("No Hub session for this person");
115
- if (!shared.session.usable)
116
- return refused("The Hub session needs a refresh at the Hub");
176
+ if (!shared.session.usable) {
177
+ await (options.renew ?? renewHubSession)(options.email);
178
+ shared = readShared(options.email());
179
+ if (!shared)
180
+ return refused("No Hub session for this person");
181
+ if (!shared.session.usable)
182
+ return refused("The Hub session needs a refresh at the Hub");
183
+ }
117
184
  const send = options.fetch ?? (typeof fetch === "function" ? fetch : undefined);
118
185
  if (!send)
119
186
  return refused("No fetch");
@@ -63,9 +63,9 @@ export type CoreFrameOptions = {
63
63
  /**
64
64
  * Where the frame comes from:
65
65
  * - "core": the core's view of the person, through the Hub's session;
66
- * - "checking": the Hub's session is there and the core is being asked, so
67
- * other apps' rows read "Checking your access…" and the plate keeps its
68
- * place, as in the Hub while it loads;
66
+ * - "checking": the Hub's session is there and the core is being asked, or
67
+ * the Hub is renewing it, so other apps' rows read "Checking your access…"
68
+ * and the plate keeps its place, as in the Hub while it loads;
69
69
  * - "static": no Hub session for this person, or the core could not be
70
70
  * read: the fixed addresses and the app's own company.
71
71
  */
package/dist/coreView.js CHANGED
@@ -92,6 +92,14 @@ export function useCoreFrame({ appKey, email, fallback, companySwitch = "os", fe
92
92
  void retryOsContext(client);
93
93
  }, [usable, client]);
94
94
  const { context, status, current, plate } = useOsPlate(client, appKey, companySwitch);
95
+ /* The read may have waited for the Hub to renew its session (0.4.3): take
96
+ the new token now, for the Admin row, not on the next look. */
97
+ useEffect(() => {
98
+ if (status !== "ready")
99
+ return;
100
+ const next = hubSession(emailNow.current);
101
+ setHub((was) => (sameHub(was, next) ? was : next));
102
+ }, [status]);
95
103
  /* The Admin row, as OsWorkspaceFrame decides it: the core's my_admin_scope().
96
104
  Asked for each person whose token can be used; a failed answer keeps the
97
105
  last one for that person, and nobody's carries over to someone else. */
@@ -115,7 +123,7 @@ export function useCoreFrame({ appKey, email, fallback, companySwitch = "os", fe
115
123
  const manages = admin !== null && admin.person === person && admin.manages;
116
124
  const source = hub !== null && status === "ready" && context !== null
117
125
  ? "core"
118
- : usable && status === "loading" ? "checking" : "static";
126
+ : hub !== null && status === "loading" ? "checking" : "static";
119
127
  const apps = context?.apps;
120
128
  const slug = current?.slug ?? null;
121
129
  const frameOptions = useMemo(() => {
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** This package's version, sent with each feedback message. A test keeps it equal to package.json. */
2
- export declare const OS_VERSION = "0.4.2";
2
+ export declare const OS_VERSION = "0.4.3";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  /** This package's version, sent with each feedback message. A test keeps it equal to package.json. */
2
- export const OS_VERSION = "0.4.2";
2
+ export const OS_VERSION = "0.4.3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosolutions-os",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "One sign-in, one company switcher, one app grid. What every AutoSolutions app imports instead of copying.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",