Skip to content

Commit

Permalink
fix: use already downloaded bundle (#955)
Browse files Browse the repository at this point in the history
* fix:  download,write then run instead of download, write, read, run

use the bundle we have instead of read it from file

* chore: change set

* chore: update changes set

* fic: handle exception
  • Loading branch information
hosseinmd authored Feb 9, 2025
1 parent 1bf1b1c commit 49b8ddf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-seas-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

fix: download, write, run instead of download, write, read, run
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RemoteScriptLoader(val reactContext: ReactContext, private val nativeLoade
return clientPerRequestBuilder.build()
}

private fun downloadAndCache(config: ScriptConfig, onSuccess: () -> Unit, onError: (code: String, message: String) -> Unit) {
private fun downloadAndCache(config: ScriptConfig, onSuccess: (bundle: ByteArray) -> Unit, onError: (code: String, message: String) -> Unit) {
val path = getScriptFilePath(config.uniqueId)
val file = File(reactContext.filesDir, path)

Expand Down Expand Up @@ -59,13 +59,20 @@ class RemoteScriptLoader(val reactContext: ReactContext, private val nativeLoade
CodeSigningUtils.verifyBundle(reactContext, token, bundle)
}

if (bundle == null) {
throw Exception("Request should have returned with a valid bundle")
} else if (bundle.isEmpty()) {
throw Exception("Request returned an empty bundle")
}


file.createNewFile()

val outputStream = file.outputStream()
val writer = BufferedOutputStream(outputStream)
writer.write(bundle)
writer.close()
onSuccess()
onSuccess(bundle)
} catch (error: Exception) {
onError(
ScriptLoadingError.ScriptCachingFailure.code,
Expand Down Expand Up @@ -122,8 +129,15 @@ class RemoteScriptLoader(val reactContext: ReactContext, private val nativeLoade
}

fun load(config: ScriptConfig, promise: Promise) {
downloadAndCache(config, {
execute(config, promise)
downloadAndCache(config, { bundle: ByteArray ->
try {
nativeLoader.evaluate(bundle, config.sourceUrl, promise)
} catch (error: Exception) {
promise.reject(
ScriptLoadingError.ScriptEvalFailure.code,
error.message ?: error.toString()
)
}
}, { code, message -> promise.reject(code, message) })
}

Expand Down

0 comments on commit 49b8ddf

Please sign in to comment.