@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.
- package/dist/auth/mode.js +29 -23
- 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
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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