package expo.modules.updates.manifest import android.net.Uri import android.util.Log import expo.modules.jsonutils.getNullable import expo.modules.jsonutils.require import expo.modules.updates.UpdatesConfiguration import expo.modules.updates.UpdatesUtils import expo.modules.updates.db.entity.AssetEntity import expo.modules.updates.db.entity.UpdateEntity import expo.modules.updates.loader.EmbeddedLoader import expo.modules.manifests.core.ExpoUpdatesManifest import expo.modules.updates.db.enums.UpdateStatus import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.io.IOException import java.net.URI import java.text.ParseException import java.util.* /** * Class for manifests using the modern format defined in the Expo Updates specification * (https://docs.expo.dev/technical-specs/expo-updates-1/). This is used by EAS Update. */ class ExpoUpdatesUpdate private constructor( override val manifest: ExpoUpdatesManifest, private val id: UUID, private val scopeKey: String, private val commitTime: Date, private val runtimeVersion: String, private val launchAsset: JSONObject, private val assets: JSONArray?, private val extensions: JSONObject?, private val url: Uri, private val requestHeaders: Map ) : Update { override val updateEntity: UpdateEntity by lazy { UpdateEntity(id, commitTime, runtimeVersion, scopeKey, this@ExpoUpdatesUpdate.manifest.getRawJson(), url, requestHeaders).apply { if (isDevelopmentMode) { status = UpdateStatus.DEVELOPMENT } } } private val assetHeaders: Map by lazy { val assetRequestHeadersJSON = (extensions ?: JSONObject()).getNullable("assetRequestHeaders") assetRequestHeadersJSON?.let { it.keys().asSequence().associateWith { key -> it.require(key) } } ?: mapOf() } override val assetEntityList: List by lazy { val assetList = mutableListOf() try { assetList.add( AssetEntity( launchAsset.getString("key"), // the fileExtension is not necessary for the launch asset and EAS servers will not include it. launchAsset.getNullable("fileExtension") ).apply { url = Uri.parse(launchAsset.getString("url")) extraRequestHeaders = assetHeaders[launchAsset.getString("key")] isLaunchAsset = true embeddedAssetFilename = EmbeddedLoader.BUNDLE_FILENAME expectedHash = launchAsset.getNullable("hash") } ) } catch (e: JSONException) { Log.e(TAG, "Could not read launch asset from manifest", e) } if (assets != null && assets.length() > 0) { for (i in 0 until assets.length()) { try { val assetObject = assets.getJSONObject(i) assetList.add( AssetEntity( assetObject.getString("key"), assetObject.getString("fileExtension") ).apply { url = Uri.parse(assetObject.getString("url")) extraRequestHeaders = assetHeaders[assetObject.getString("key")] embeddedAssetFilename = assetObject.getNullable("embeddedAssetFilename") expectedHash = assetObject.getNullable("hash") } ) } catch (e: JSONException) { Log.e(TAG, "Could not read asset from manifest", e) } } } assetList } override val isDevelopmentMode: Boolean by lazy { manifest.isDevelopmentMode() } companion object { private val TAG = Update::class.java.simpleName @Throws(Exception::class) fun fromExpoUpdatesManifest( manifest: ExpoUpdatesManifest, extensions: JSONObject?, configuration: UpdatesConfiguration ): ExpoUpdatesUpdate { val resolvedManifest = ExpoUpdatesManifest( resolveManifestAssetUrls(manifest.getRawJson(), configuration.updateUrl) ) val update = ExpoUpdatesUpdate( resolvedManifest, id = UUID.fromString(resolvedManifest.getID()), configuration.scopeKey, commitTime = try { UpdatesUtils.parseDateString(resolvedManifest.getCreatedAt()) } catch (e: ParseException) { Log.e(TAG, "Could not parse manifest createdAt string; falling back to current time", e) Date() }, runtimeVersion = resolvedManifest.getRuntimeVersion(), launchAsset = resolvedManifest.getLaunchAsset(), assets = resolvedManifest.getAssets(), extensions = extensions, url = configuration.updateUrl, requestHeaders = configuration.requestHeaders ) update.assetEntityList.forEach { asset -> val filename = UpdatesUtils.createFilenameForAsset(asset) if (!UpdatesUtils.isSafeFilename(filename)) { throw IOException( "Update ${resolvedManifest.getID()} could not be loaded because the asset filename " + "\"$filename\" is not a valid filename. Assets are stored under their key and file " + "extension, so neither may contain a path separator. Check the server that produced " + "this manifest." ) } } return update } private fun resolveManifestAssetUrls(manifestJson: JSONObject, baseUrl: Uri): JSONObject { val resolvedManifestJson = JSONObject(manifestJson.toString()) val launchAsset = resolvedManifestJson.getJSONObject("launchAsset") launchAsset.put("url", resolveUrl(launchAsset.getString("url"), baseUrl)) val assets = resolvedManifestJson.getNullable("assets") if (assets != null) { for (i in 0 until assets.length()) { val asset = assets.getJSONObject(i) asset.put("url", resolveUrl(asset.getString("url"), baseUrl)) } } return resolvedManifestJson } private fun resolveUrl(url: String, baseUrl: Uri): String { return try { URI(baseUrl.toString()).withRootPath().resolve(url).toString() } catch (e: Exception) { url } } /** * Works around Android's [URI.resolve] dropping the separator between the authority and a * path-relative reference when the base path is empty, which splices the reference's first * segment onto the port. Only the base is adjusted, so non-HTTP bases such as `exp://` still * resolve. */ private fun URI.withRootPath(): URI { if (rawAuthority == null || !rawPath.isNullOrEmpty()) { return this } // A base query or fragment is dropped during resolution anyway, so it doesn't need carrying. return URI("$scheme://$rawAuthority/") } } }