Skip to content

Commit 68a026b

Browse files
Feat: Consolidate device caching by merging live and app live data fetching into a single function
1 parent 21739ef commit 68a026b

File tree

6 files changed

+65
-69
lines changed

6 files changed

+65
-69
lines changed

src/lib/device-cache.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import fs from "fs";
2+
import os from "os";
3+
import path from "path";
4+
5+
const CACHE_DIR = path.join(os.homedir(), ".browserstack", "combined_cache");
6+
const CACHE_FILE = path.join(CACHE_DIR, "data.json");
7+
const TTL_MS = 24 * 60 * 60 * 1000; // 1 day
8+
9+
const URLS = {
10+
live: "https://www.browserstack.com/list-of-browsers-and-platforms/live.json",
11+
app_live:
12+
"https://www.browserstack.com/list-of-browsers-and-platforms/app_live.json",
13+
};
14+
15+
/**
16+
* Fetches and caches both BrowserStack datasets (live + app_live) with a shared TTL.
17+
*/
18+
export async function getBrowserStackData(
19+
type: "live" | "app_live",
20+
): Promise<any> {
21+
if (!fs.existsSync(CACHE_DIR)) {
22+
fs.mkdirSync(CACHE_DIR, { recursive: true });
23+
}
24+
25+
let cache: any = {};
26+
let shouldRefresh = true;
27+
28+
if (fs.existsSync(CACHE_FILE)) {
29+
const stats = fs.statSync(CACHE_FILE);
30+
if (Date.now() - stats.mtimeMs < TTL_MS) {
31+
cache = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8"));
32+
shouldRefresh = false;
33+
}
34+
}
35+
36+
if (shouldRefresh) {
37+
const [liveRes, appLiveRes] = await Promise.all([
38+
fetch(URLS.live),
39+
fetch(URLS.app_live),
40+
]);
41+
42+
if (!liveRes.ok || !appLiveRes.ok) {
43+
throw new Error(
44+
`Failed to fetch data: live=${liveRes.statusText}, app_live=${appLiveRes.statusText}`,
45+
);
46+
}
47+
48+
const [liveData, appLiveData] = await Promise.all([
49+
liveRes.json(),
50+
appLiveRes.json(),
51+
]);
52+
53+
cache = { live: liveData, app_live: appLiveData };
54+
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache), "utf8");
55+
}
56+
57+
return cache[type];
58+
}

src/tools/applive-utils/device-cache.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/tools/applive-utils/start-session.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import childProcess from "child_process";
22
import logger from "../../logger";
3-
import { getAppLiveData } from "./device-cache";
3+
import { getBrowserStackData } from "../../lib/device-cache";
44
import { fuzzySearchDevices } from "./fuzzy-search";
55
import { sanitizeUrlParam } from "../../lib/utils";
66
import { uploadApp } from "./upload-app";
@@ -30,7 +30,8 @@ export async function startSession(args: StartSessionArgs): Promise<string> {
3030
const { appPath, desiredPlatform, desiredPhone } = args;
3131
let { desiredPlatformVersion } = args;
3232

33-
const data = await getAppLiveData();
33+
const data = await getBrowserStackData("app_live");
34+
3435
const allDevices: DeviceEntry[] = data.mobile.flatMap((group: any) =>
3536
group.devices.map((dev: any) => ({ ...dev, os: group.os })),
3637
);

src/tools/live-utils/desktop-filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { getLiveData } from "./device-cache";
1+
import { getBrowserStackData } from "../../lib/device-cache";
22
import { resolveVersion } from "./version-resolver";
33
import { customFuzzySearch } from "../../lib/fuzzy";
44
import { DesktopArgs, DesktopEntry } from "./types";
55

66
export async function filterDesktop(args: DesktopArgs): Promise<DesktopEntry> {
7-
const data = await getLiveData();
7+
const data = await getBrowserStackData("live");
88
const allEntries = getAllDesktopEntries(data);
99

1010
// Filter OS

src/tools/live-utils/device-cache.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/tools/live-utils/mobile-filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getLiveData } from "./device-cache";
1+
import { getBrowserStackData } from "../../lib/device-cache";
22
import { resolveVersion } from "./version-resolver";
33
import { customFuzzySearch } from "../../lib/fuzzy";
44
import { MobileArgs, MobileEntry } from "./types";
@@ -80,7 +80,7 @@ function createVersionNote(
8080
}
8181

8282
export async function filterMobile(args: MobileArgs): Promise<MobileEntry> {
83-
const data = await getLiveData();
83+
const data = await getBrowserStackData("live");
8484
const allEntries = getAllMobileEntries(data);
8585

8686
const osCandidates = filterByOS(allEntries, args.os);

0 commit comments

Comments
 (0)