-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.js
More file actions
81 lines (75 loc) · 2.36 KB
/
Copy pathstorage.js
File metadata and controls
81 lines (75 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Read the store's own record of which catalog id maps to which
// installed slug + version. Used so we can detect "update available"
// without baking version into description prose.
export function runtimeStorage() {
return (typeof window !== 'undefined' && window.mobius && window.mobius.storage) || null
}
export function normalizeInstalledVersions(data) {
if (data == null) return {}
let parsed = data
if (typeof data === 'string') {
try {
parsed = JSON.parse(data)
} catch {
return {}
}
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
const clean = {}
for (const [id, version] of Object.entries(parsed)) {
if (!id || typeof version !== 'string') continue
clean[id] = version
}
return clean
}
export async function loadInstalledVersions(appId, token) {
const storage = runtimeStorage()
if (storage?.get) {
try {
return normalizeInstalledVersions(await storage.get('installed-versions.json'))
} catch {
// Fall through to the raw storage API; older shells or transient
// IndexedDB failures should not break update detection.
}
}
try {
const r = await fetch(`/api/storage/apps/${appId}/installed-versions.json`, {
headers: { Authorization: `Bearer ${token}` },
})
if (r.status === 404) return {}
if (!r.ok) return {}
return normalizeInstalledVersions(await r.json())
} catch {
return {}
}
}
export async function saveInstalledVersions(appId, token, map) {
const clean = normalizeInstalledVersions(map)
const storage = runtimeStorage()
if (storage?.set) {
try {
await storage.set('installed-versions.json', clean)
return true
} catch {
// Continue to the raw API fallback below.
}
}
try {
const r = await fetch(`/api/storage/apps/${appId}/installed-versions.json`, {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(clean),
})
return r.ok
} catch {
return false
}
}
// Ask the shell to switch to the installed app via the
// `moebius:open-app` postMessage protocol (see Shell.jsx's handler).
// `id` is the numeric DB id from /api/apps/. If we're not embedded in
// the shell (mini-app run standalone), we can't navigate the parent
// shell — surface a hint via the caller's toast.