From e3e093fd1fc42c423ceadc619f0c5d61056614b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Trys=C5=82a?= Date: Wed, 19 Jun 2019 21:06:26 +0200 Subject: [PATCH] fix crash with Metro and delta bundle --- .../react/devsupport/BundleDownloader.java | 16 +++++++++++----- .../react/devsupport/DevBundlesContainer.java | 6 +++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java index 8f48b031c807..53d4d472dc9a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java @@ -498,6 +498,7 @@ private void processBundle( File tmpFile = new File(outputFile.getPath() + ".tmp"); boolean bundleWritten; + boolean pushBundleNameToContainer = false; NativeDeltaClient nativeDeltaClient = null; if (BundleDeltaClient.isDeltaUrl(url)) { @@ -507,17 +508,22 @@ private void processBundle( Pair result = deltaClient.processDelta(headers, body, tmpFile); bundleWritten = result.first; nativeDeltaClient = result.second; + // Always push bundle name, regardless if it was written or not. + // If `bundleWritten` is `false` it means that the previously stored + // bundle is still valid, so we can safely add to `DevBundlesContainer`. + pushBundleNameToContainer = true; } else { mBundleDeltaClient = null; bundleWritten = storePlainJSInFile(body, tmpFile); + pushBundleNameToContainer = bundleWritten; } - if (bundleWritten) { - String bundleName = getBundleNameFromURL(url); // fallback + if (pushBundleNameToContainer && mDevBundlesContainer != null) { + String bundleName = getBundleNameFromURL(url); + mDevBundlesContainer.pushBundle(bundleName, url, outputFile.getPath()); + } - if(mDevBundlesContainer != null) { - mDevBundlesContainer.pushBundle(bundleName, url, outputFile.getPath()); - } + if (bundleWritten) { if (!tmpFile.renameTo(outputFile)) { throw new IOException("Couldn't rename " + tmpFile + " to " + outputFile); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevBundlesContainer.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevBundlesContainer.java index 2520e72ef5de..4c11d25aff96 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevBundlesContainer.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevBundlesContainer.java @@ -82,7 +82,7 @@ public String getFileURLByName(String name) { public String getNameBySourceURL(String sourceURL) { for (String key : bundleNameMapping.keySet()) { BundleURLs tmp = bundleNameMapping.get(key); - if(tmp.sourceURL.equals(sourceURL)) { + if(tmp.sourceURL.equals(sourceURL.split("\\?")[0])) { return key; } } @@ -102,7 +102,7 @@ public String getNameByFileURL(String fileURL) { public String getFileURLBySourceURL(String sourceURL) { for (String key : bundleNameMapping.keySet()) { BundleURLs tmp = bundleNameMapping.get(key); - if(tmp.sourceURL.equals(sourceURL)) { + if(tmp.sourceURL.startsWith(sourceURL.split("\\?")[0])) { return tmp.fileURL; } } @@ -136,4 +136,4 @@ public DevBundlesContainer(JSONObject json) { FLog.e(ReactConstants.TAG, "DevBundlesContainer is unable to create from JSON"); } } -} \ No newline at end of file +}