Improve Fetch Extension Store

This commit is contained in:
Syer10
2026-06-17 13:31:15 -04:00
parent 74ade8a3a3
commit 3cf4cf6cf8

View File

@@ -49,44 +49,41 @@ object ExtensionStoreService {
): ExtensionStore { ): ExtensionStore {
var updatedIndexUrl = indexUrl var updatedIndexUrl = indexUrl
return try { return try {
val response = network.client.newCall(GET(indexUrl)).awaitSuccess() network.client.newCall(GET(indexUrl)).awaitSuccess().body.source().use { source ->
response.body try {
.source() protoBuf.decodeFromByteArray<NetworkExtensionStore>(source.peek().readByteArray())
.use { source -> } catch (e: IllegalArgumentException) {
logger.debug { "Failed to decode as protobuf, trying JSON" }
try { try {
protoBuf.decodeFromByteArray<NetworkExtensionStore>(source.peek().readByteArray()) json.decodeFromBufferedSource<NetworkExtensionStore>(source.peek())
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
logger.debug { "Failed to decode as protobuf, trying JSON" }
if (forceV2) throw e if (forceV2) throw e
try { logger.debug { "Failed to decode as NetworkExtensionStore, trying LegacyExtensionRepo" }
json.decodeFromBufferedSource<NetworkExtensionStore>(source.peek()) val legacyIndex =
} catch (_: IllegalArgumentException) { try {
logger.debug { "Failed to decode as NetworkExtensionStore, trying LegacyExtensionRepo" } json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(source.peek())
val legacyIndex = } catch (e: IllegalArgumentException) {
try { if (!indexUrl.endsWith("/index.min.json")) {
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(source.peek()) throw e
} catch (e: IllegalArgumentException) { }
if (!indexUrl.endsWith("/index.min.json")) { logger.debug { "Checking for new repo.json format" }
throw e updatedIndexUrl = indexUrl.replace("/index.min.json", "/repo.json")
} network.client.newCall(GET(updatedIndexUrl)).awaitSuccess().body.source().use {
logger.debug { "Retrying with /index.min.json" } json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it)
updatedIndexUrl = indexUrl.replace("/index.min.json", "/repo.json")
network.client.newCall(GET(updatedIndexUrl)).awaitSuccess().body.source().use {
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it)
}
} }
if (legacyIndex.indexV2 != null) {
return fetch(legacyIndex.indexV2, forceV2 = true)
} else {
legacyIndex
} }
if (legacyIndex.indexV2 != null) {
return fetch(legacyIndex.indexV2, forceV2 = true)
} else {
legacyIndex
} }
} }
}.toExtensionStore(updatedIndexUrl) }.toExtensionStore(updatedIndexUrl)
}
} catch (e: Exception) { } catch (e: Exception) {
if (e is CancellationException) throw e if (e is CancellationException) throw e
logger.debug(e) { "Failed to fetch extension store '$indexUrl'" } logger.error(e) { "Failed to fetch extension store '$indexUrl'" }
throw e throw e
} }
} }