Download mobile/modules/engine/src/utils/devMiniappSnapshot.ts from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 5.36 kB
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/utils/devMiniappSnapshot.ts
- Command line
-
hf download hf://Anirudh1950/MentraOS/mobile/modules/engine/src/utils/devMiniappSnapshot.ts
-
curl -L -o devMiniappSnapshot.ts https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/utils/devMiniappSnapshot.ts
5.36 kB
| /** | |
| * Dev miniapp snapshot — keep a local copy of the last live-dev bundle so | |
| * walking away from the laptop still opens the miniapp while Mentra App is | |
| * running (and after, as long as the on-disk `dev-*` snapshot remains). | |
| * | |
| * Live launches load HTTP off `mentra-miniapp dev` for hot reload. After a | |
| * successful reachability probe we fire-and-forget `${devUrl}/bundle.zip` | |
| * into `lmas/<pkg>/dev-<ms>/`. When the laptop is unreachable, launch | |
| * routes and MiniappLauncher fall back to that snapshot instead of the | |
| * "Dev server offline" dead-end. | |
| */ | |
| import appRegistry from "../services/AppRegistry" | |
| import {createDevSnapshotRequest} from "./devSnapshotRequests" | |
| import {storage} from "./storage/storage" | |
| import { | |
| decideDevLaunchRoute, | |
| type DecideDevLaunchOptions, | |
| type DevLaunchResult, | |
| type DevManifest, | |
| } from "./devMiniappLaunch" | |
| export type DevOpenDecision = | |
| | {decision: "live"; manifest: DevManifest; resolvedUrl: string} | |
| | {decision: "cached"} | |
| | {decision: "offline"} | |
| export type DevBundleSource = | |
| | {kind: "live"; resolvedUrl: string; manifest: DevManifest} | |
| | {kind: "snapshot"; version: string} | |
| | {kind: "none"} | |
| type SnapshotRequest = ReturnType<typeof createDevSnapshotRequest> | |
| const snapshotInFlight = new Map<string, {promise: Promise<void>; isCurrent: () => boolean}>() | |
| /** Candidate zip URLs: same-origin `/bundle.zip`, then the sidecar path. */ | |
| export function snapshotCandidateUrls(baseUrl: string, sidecarPort?: number | null): string[] { | |
| const trimmed = baseUrl.replace(/\/$/, "") | |
| const urls = [`${trimmed}/bundle.zip`] | |
| try { | |
| const url = new URL(trimmed) | |
| const userPort = Number(url.port) || (url.protocol === "https:" ? 443 : 80) | |
| const sidecar = sidecarPort && sidecarPort > 0 ? sidecarPort : userPort + 1 | |
| url.port = String(sidecar) | |
| const sidecarZip = `${url.origin}/__mentra_dev/bundle.zip` | |
| if (!urls.includes(sidecarZip)) urls.push(sidecarZip) | |
| } catch { | |
| /* ignore malformed base */ | |
| } | |
| return urls | |
| } | |
| function storedSidecarPort(packageName: string): number | undefined { | |
| const stored = storage.load<number>(`${packageName}_dev_port`) | |
| return stored.is_ok() && Number.isFinite(stored.value) ? stored.value : undefined | |
| } | |
| /** | |
| * Download the current live-dev zip into `lmas/<pkg>/dev-<ms>/` and keep | |
| * only the newest snapshot. Coalesces concurrent calls per package. | |
| */ | |
| export function queueDevSnapshot( | |
| packageName: string, | |
| baseUrl: string, | |
| sidecarPort?: number | null, | |
| request: SnapshotRequest = createDevSnapshotRequest(packageName), | |
| ): void { | |
| if (!packageName || !baseUrl || !request.isCurrent()) return | |
| if (snapshotInFlight.get(packageName)?.isCurrent()) return | |
| const promise = installDevSnapshot( | |
| packageName, | |
| baseUrl, | |
| request, | |
| sidecarPort ?? storedSidecarPort(packageName), | |
| ).finally(() => { | |
| if (snapshotInFlight.get(packageName)?.promise === promise) snapshotInFlight.delete(packageName) | |
| }) | |
| snapshotInFlight.set(packageName, {promise, isCurrent: request.isCurrent}) | |
| } | |
| async function installDevSnapshot( | |
| packageName: string, | |
| baseUrl: string, | |
| request: SnapshotRequest, | |
| sidecarPort?: number | null, | |
| ): Promise<void> { | |
| let lastError: unknown | |
| for (const url of snapshotCandidateUrls(baseUrl, sidecarPort)) { | |
| if (!request.isCurrent()) return | |
| const res = await appRegistry.installFromUrl(url, { | |
| expectedPackageName: packageName, | |
| beforeActivate: request.beforeActivate, | |
| versionOverride: `dev-${Date.now()}`, | |
| releaseIdentity: {source: "dev_snapshot"}, | |
| }) | |
| if (!request.isCurrent()) return | |
| if (res.is_ok()) { | |
| appRegistry.gcDevVersions(packageName, 1) | |
| return | |
| } | |
| lastError = res.error | |
| } | |
| console.warn(`Dev snapshot failed for ${packageName}:`, lastError) | |
| } | |
| /** Reachability first; if the laptop is down, open the last local snapshot. */ | |
| export async function decideDevOpenRoute( | |
| packageName: string, | |
| devUrl: string, | |
| options?: DecideDevLaunchOptions, | |
| ): Promise<DevOpenDecision> { | |
| const request = createDevSnapshotRequest(packageName) | |
| const route = await decideDevLaunchRoute(packageName, devUrl, options) | |
| if (route.decision === "live") { | |
| queueDevSnapshot(packageName, route.resolvedUrl, undefined, request) | |
| return route | |
| } | |
| if (packageName && appRegistry.hasDevSnapshot(packageName)) { | |
| return {decision: "cached"} | |
| } | |
| return {decision: "offline"} | |
| } | |
| /** | |
| * Same live-vs-snapshot choice MiniappLauncher uses to resolve a bundle. | |
| * Live also kicks a background snapshot so the next offline open has a copy. | |
| */ | |
| export async function resolveDevBundleSource( | |
| packageName: string, | |
| devUrl: string, | |
| options?: DecideDevLaunchOptions, | |
| ): Promise<DevBundleSource> { | |
| const request = createDevSnapshotRequest(packageName) | |
| const route: DevLaunchResult = await decideDevLaunchRoute(packageName, devUrl, options) | |
| if (route.decision === "live" && route.manifest) { | |
| queueDevSnapshot(packageName, route.resolvedUrl, undefined, request) | |
| return {kind: "live", resolvedUrl: route.resolvedUrl, manifest: route.manifest} | |
| } | |
| const version = appRegistry.getLatestDevSnapshotVersion(packageName) | |
| if (version) return {kind: "snapshot", version} | |
| return {kind: "none"} | |
| } | |