Skip to content

Commit 589d2e6

Browse files
committed
Ensure local APK handling handles non-semver filenames
This shouldn't ever happen (we should only save semver-valid versions), but if it does, we don't want to crash the whole server.
1 parent 924d1e1 commit 589d2e6

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/interceptors/android/fetch-apk.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ async function getLatestRelease(): Promise<{ version: string, url: string } | un
2929
}
3030
}
3131

32-
async function getLocalApk(config: HtkConfig) {
33-
try {
34-
const apks = (await readDir(config.configPath))
35-
.map(filename => filename.match(/^httptoolkit-(.*).apk$/))
36-
.filter((match): match is RegExpMatchArray => !!match)
37-
.map((match) => ({
38-
path: path.join(config.configPath, match[0]),
39-
version: match[1]
40-
}));
41-
42-
apks.sort((apk1, apk2) => {
43-
return -1 * semver.compare(apk1.version, apk2.version);
44-
});
32+
async function getAllLocalApks(config: HtkConfig) {
33+
const apks = (await readDir(config.configPath))
34+
.map(filename => filename.match(/^httptoolkit-(.*).apk$/))
35+
.filter((match): match is RegExpMatchArray => !!match)
36+
.map((match) => ({
37+
path: path.join(config.configPath, match[0]),
38+
version: semver.valid(match[1]) || '0.0.0'
39+
}));
40+
41+
apks.sort((apk1, apk2) => {
42+
return -1 * semver.compare(apk1.version, apk2.version);
43+
});
4544

45+
return apks;
46+
}
47+
48+
async function getLatestLocalApk(config: HtkConfig) {
49+
try {
50+
const apks = await getAllLocalApks(config);
4651
const latestLocalApk = apks[0];
4752
if (!latestLocalApk) return;
4853
else return latestLocalApk;
@@ -90,17 +95,7 @@ async function updateLocalApk(
9095

9196
// Delete all but the most recent APK version in the config directory.
9297
async function cleanupOldApks(config: HtkConfig) {
93-
const apks = (await readDir(config.configPath))
94-
.map(filename => filename.match(/^httptoolkit-(.*).apk$/))
95-
.filter((match): match is RegExpMatchArray => !!match)
96-
.map((match) => ({
97-
path: path.join(config.configPath, match[0]),
98-
version: match[1]
99-
}));
100-
101-
apks.sort((apk1, apk2) => {
102-
return -1 * semver.compare(apk1.version, apk2.version);
103-
});
98+
const apks = await getAllLocalApks(config);
10499

105100
console.log(`Deleting old APKs: ${apks.slice(1).map(apk => apk.path).join(', ')}`);
106101

@@ -112,7 +107,7 @@ async function cleanupOldApks(config: HtkConfig) {
112107
export async function streamLatestApk(config: HtkConfig): Promise<stream.Readable> {
113108
const [latestApkRelease, localApk] = await Promise.all([
114109
await getLatestRelease(),
115-
await getLocalApk(config)
110+
await getLatestLocalApk(config)
116111
]);
117112

118113
if (!localApk) {

0 commit comments

Comments
 (0)