@trylight/app 0.3.5 → 0.3.6

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 (2) hide show
  1. package/dist/auth/mode.js +29 -23
  2. package/package.json +1 -1
package/dist/auth/mode.js CHANGED
@@ -14,34 +14,40 @@ const TTL_MS = 30_000;
14
14
  let cached = null;
15
15
  let inflight = null;
16
16
  export async function getAuthMode() {
17
- if (cached === null || Date.now() - cached.fetchedAt >= TTL_MS) {
18
- if (inflight === null) {
19
- inflight = fetchMode()
20
- .then((mode) => {
21
- cached = { mode, fetchedAt: Date.now() };
22
- })
23
- .catch((error) => {
24
- // Keep serving the last known answer through a blip. Only a cold start
25
- // with the platform unreachable has nothing to fall back to, and there the door
26
- // stays shut rather than swinging open.
27
- if (cached === null)
28
- throw error;
29
- cached = { ...cached, fetchedAt: Date.now() };
30
- })
31
- .finally(() => {
32
- inflight = null;
33
- });
34
- }
35
- await inflight;
17
+ if (cached !== null && Date.now() - cached.fetchedAt < TTL_MS)
18
+ return cached.mode;
19
+ if (inflight === null) {
20
+ inflight = refresh().finally(() => {
21
+ inflight = null;
22
+ });
36
23
  }
37
- if (cached === null) {
38
- throw new Error("Could not reach the platform to check who may open this app.");
24
+ return inflight;
25
+ }
26
+ async function refresh() {
27
+ try {
28
+ const mode = await fetchMode();
29
+ cached = { mode, fetchedAt: Date.now() };
30
+ return mode;
31
+ }
32
+ catch (error) {
33
+ // Keep serving the last known answer through a blip. Only a cold start
34
+ // with the platform unreachable has nothing to fall back to, and there the door
35
+ // stays shut rather than swinging open.
36
+ if (cached === null)
37
+ throw error;
38
+ cached = { ...cached, fetchedAt: Date.now() };
39
+ return cached.mode;
39
40
  }
40
- return cached.mode;
41
41
  }
42
42
  async function fetchMode() {
43
43
  const { plazaUrl, appId } = miiAuthConfig();
44
- const response = await fetch(`${plazaUrl}${PLATFORM_AUTH_PATHS.config}?app_id=${encodeURIComponent(appId)}`, { cache: "no-store" });
44
+ let response;
45
+ try {
46
+ response = await fetch(`${plazaUrl}${PLATFORM_AUTH_PATHS.config}?app_id=${encodeURIComponent(appId)}`, { cache: "no-store" });
47
+ }
48
+ catch (cause) {
49
+ throw new Error(`Could not reach the platform at ${plazaUrl} to check who may open this app.`, { cause });
50
+ }
45
51
  if (!response.ok) {
46
52
  throw new Error(`The platform's app-auth config answered ${response.status}.`);
47
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trylight/app",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "The Light SDK every v2 org app depends on: entities (Drizzle tables with a typed client), sign-in, contracts and in-app chat.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",