Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 525e160

Browse files
committed
feedback
1 parent cda2db1 commit 525e160

File tree

7 files changed

+161
-113
lines changed

7 files changed

+161
-113
lines changed

CodePush.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ async function checkForUpdate(deploymentKey = null) {
4040
* to send the app version to the server, since we are interested
4141
* in any updates for current app store version, regardless of hash.
4242
*/
43-
const queryPackage = localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0
44-
? localPackage
45-
: { appVersion: config.appVersion };
43+
const queryPackage;
44+
if (localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0) {
45+
queryPackage = localPackage;
46+
} else {
47+
queryPackage = { appVersion: config.appVersion };
48+
if (Platform.OS === "ios" && config.packageHash) {
49+
queryPackage.packageHash = config.packageHash;
50+
}
51+
}
52+
4653
const update = await sdk.queryUpdateWithCurrentPackage(queryPackage);
4754

4855
/*
@@ -57,26 +64,19 @@ async function checkForUpdate(deploymentKey = null) {
5764
* the currently running update. This should _never_ happen, unless there is a
5865
* bug in the server, but we're adding this check just to double-check that the
5966
* client app is resilient to a potential issue with the update check.
60-
* 4) On Android: the server said there is an update, but the update's hash is the
61-
* same as that of the binary's currently running version. We did not attach the
62-
* binary's hash to the updateCheck request because we want to avoid having to
63-
* install diff updates against the binary's version, which we can't do yet on
64-
* Android.
67+
* 4) The server said there is an update, but the update's hash is the same as that
68+
* of the binary's currently running version. This should only happen in Android -
69+
* unlike iOS, we don't attach the binary's hash to the updateCheck request
70+
* because we want to avoid having to install diff updates against the binary's
71+
* version, which we can't do yet on Android.
6572
*/
66-
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash)) {
73+
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash) || config.packageHash === localPackage.packageHash) {
6774
if (update && update.updateAppVersion) {
6875
log("An update is available but it is targeting a newer binary version than you are currently running.");
6976
}
7077

7178
return null;
72-
} else {
73-
if (Platform.OS === "android" && !localPackage) {
74-
const binaryHash = await NativeCodePush.getBinaryHash();
75-
if (update.packageHash === binaryHash) {
76-
return null;
77-
}
78-
}
79-
79+
} else {
8080
const remotePackage = { ...update, ...PackageMixins.remote(sdk.reportStatusDownload) };
8181
remotePackage.failedInstall = await NativeCodePush.isFailedUpdate(remotePackage.packageHash);
8282
remotePackage.deploymentKey = deploymentKey || nativeConfig.deploymentKey;

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public class CodePush {
5050

5151
private final String ASSETS_BUNDLE_PREFIX = "assets://";
5252
private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
53-
private final String CODE_PUSH_HASH_FILE_NAME = "CodePushHash.json";
5453
private final String CODE_PUSH_PREFERENCES = "CodePush";
5554
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress";
5655
private final String FAILED_UPDATES_KEY = "CODE_PUSH_FAILED_UPDATES";
@@ -70,9 +69,9 @@ public class CodePush {
7069
private CodePushTelemetryManager codePushTelemetryManager;
7170

7271
// Config properties.
73-
private String deploymentKey;
7472
private String appVersion;
7573
private int buildVersion;
74+
private String deploymentKey;
7675
private final String serverUrl = "https://codepush.azurewebsites.net/";
7776

7877
private Activity mainActivity;
@@ -393,20 +392,6 @@ public void call(DownloadProgress downloadProgress) {
393392
asyncTask.execute();
394393
}
395394

396-
@ReactMethod
397-
public void getBinaryHash(Promise promise) {
398-
try {
399-
promise.resolve(CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CODE_PUSH_HASH_FILE_NAME)));
400-
} catch (IOException e) {
401-
if (!isDebugMode) {
402-
// Only print this message in "Release" mode. In "Debug", we may not have the
403-
// hash if the build skips bundling the files.
404-
CodePushUtils.log("Unable to get the hash of the binary's bundled resources - \"codepush.gradle\" may have not been added to the build definition.");
405-
}
406-
promise.resolve("");
407-
}
408-
}
409-
410395
@ReactMethod
411396
public void getConfiguration(Promise promise) {
412397
WritableNativeMap configMap = new WritableNativeMap();
@@ -417,6 +402,11 @@ public void getConfiguration(Promise promise) {
417402
configMap.putString("clientUniqueId",
418403
Settings.Secure.getString(mainActivity.getContentResolver(),
419404
android.provider.Settings.Secure.ANDROID_ID));
405+
String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(mainActivity, isDebugMode);
406+
if (binaryHash != null) {
407+
configMap.putString(PACKAGE_HASH_KEY, binaryHash);
408+
}
409+
420410
promise.resolve(configMap);
421411
}
422412

android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.codepush.react;
22

3+
import android.app.Activity;
34
import android.util.Base64;
45

56
import com.facebook.react.bridge.ReadableArray;
@@ -21,6 +22,12 @@
2122

2223
public class CodePushUpdateUtils {
2324

25+
private static final String CODE_PUSH_HASH_FILE_NAME = "CodePushHash.json";
26+
27+
// These variables are used to cache the hash of the binary contents in memory.
28+
private static String binaryHash = null;
29+
private static boolean didLoadBinaryHash = false;
30+
2431
private static void addContentsOfFolderToManifest(String folderPath, String pathPrefix, ArrayList<String> manifest) {
2532
File folder = new File(folderPath);
2633
File[] folderFiles = folder.listFiles();
@@ -102,6 +109,25 @@ public static String findJSBundleInUpdateContents(String folderPath) {
102109
return null;
103110
}
104111

112+
public static String getHashForBinaryContents(Activity mainActivity, boolean isDebugMode) {
113+
if (!didLoadBinaryHash) {
114+
didLoadBinaryHash = true;
115+
try {
116+
binaryHash = CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CODE_PUSH_HASH_FILE_NAME));
117+
} catch (IOException e) {
118+
if (!isDebugMode) {
119+
// Only print this message in "Release" mode. In "Debug", we may not have the
120+
// hash if the build skips bundling the files.
121+
CodePushUtils.log("Unable to get the hash of the binary's bundled resources - \"codepush.gradle\" may have not been added to the build definition.");
122+
}
123+
124+
return null;
125+
}
126+
}
127+
128+
return binaryHash;
129+
}
130+
105131
public static void verifyHashForDiffUpdate(String folderPath, String expectedHash) {
106132
ArrayList<String> updateContentsManifest = new ArrayList<String>();
107133
addContentsOfFolderToManifest(folderPath, "", updateContentsManifest);

ios/CodePush/CodePush.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ failCallback:(void (^)(NSError *err))failCallback;
115115
+ (NSString *)findMainBundleInFolder:(NSString *)folderPath
116116
error:(NSError **)error;
117117

118-
+ (NSString *)getDefaultAssetsFolderName;
118+
+ (NSString *)getAssetsFolderName;
119119
+ (NSString *)getDefaultJsBundleName;
120120

121121
+ (NSString *)getHashForBinaryContents:(NSURL *)binaryBundleUrl
122122
error:(NSError **)error;
123123

124124
+ (NSString *)getManifestFolderPrefix;
125+
+ (NSString *)modifiedDateStringOfFileAtURL:(NSURL *)fileURL;
125126

126127
+ (BOOL)verifyHashForDiffUpdate:(NSString *)finalUpdateFolder
127128
expectedHash:(NSString *)expectedHash

ios/CodePush/CodePush.m

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
9393
NSString *packageDate = [currentPackageMetadata objectForKey:BinaryBundleDateKey];
9494
NSString *packageAppVersion = [currentPackageMetadata objectForKey:AppVersionKey];
9595

96-
if ([[self modifiedDateStringOfFileAtURL:binaryBundleURL] isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) {
96+
if ([[CodePushUpdateUtils modifiedDateStringOfFileAtURL:binaryBundleURL] isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) {
9797
// Return package file because it is newer than the app store binary's JS bundle
9898
NSURL *packageUrl = [[NSURL alloc] initFileURLWithPath:packageFile];
9999
NSLog(logMessageFormat, packageUrl);
@@ -288,20 +288,6 @@ - (void)loadBundle
288288
});
289289
}
290290

291-
/*
292-
* This returns the modified date as a string for a given file URL.
293-
*/
294-
+ (NSString *)modifiedDateStringOfFileAtURL:(NSURL *)fileURL
295-
{
296-
if (fileURL != nil) {
297-
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
298-
NSDate *modifiedDate = [fileAttributes objectForKey:NSFileModificationDate];
299-
return [NSString stringWithFormat:@"%f", [modifiedDate timeIntervalSince1970]];
300-
} else {
301-
return nil;
302-
}
303-
}
304-
305291
/*
306292
* This method is used when an update has failed installation
307293
* and the app needs to be rolled back to the previous bundle.
@@ -398,7 +384,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
398384
NSDictionary *mutableUpdatePackage = [updatePackage mutableCopy];
399385
NSURL *binaryBundleURL = [CodePush binaryBundleURL];
400386
if (binaryBundleURL != nil) {
401-
[mutableUpdatePackage setValue:[CodePush modifiedDateStringOfFileAtURL:binaryBundleURL]
387+
[mutableUpdatePackage setValue:[CodePushUpdateUtils modifiedDateStringOfFileAtURL:binaryBundleURL]
402388
forKey:BinaryBundleDateKey];
403389
}
404390

@@ -449,35 +435,38 @@ - (void)savePendingUpdate:(NSString *)packageHash
449435
RCT_EXPORT_METHOD(getConfiguration:(RCTPromiseResolveBlock)resolve
450436
rejecter:(RCTPromiseRejectBlock)reject)
451437
{
452-
resolve([[CodePushConfig current] configuration]);
453-
}
454-
455-
/*
456-
* This method is the native side of the CodePush.getCurrentPackage method.
457-
*/
458-
RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
459-
rejecter:(RCTPromiseRejectBlock)reject)
460-
{
438+
NSDictionary *configuration = [[CodePushConfig current] configuration];
461439
NSError *error;
462440
if (isRunningBinaryVersion) {
463441
// isRunningBinaryVersion will not get set to "YES" if running against the packager.
464442
NSString *binaryHash = [CodePushUpdateUtils getHashForBinaryContents:[CodePush binaryBundleURL] error:&error];
465443
if (error) {
466444
NSLog(@"Error obtaining hash for binary contents: %@", error);
467-
resolve(nil);
445+
resolve(configuration);
468446
return;
469-
} else if (binaryHash == nil) {
470-
resolve(nil);
447+
}
448+
449+
if (binaryHash == nil) {
450+
resolve(configuration);
471451
return;
472452
}
473453

474-
resolve(@{
475-
PackageHashKey:binaryHash,
476-
AppVersionKey:[[CodePushConfig current] appVersion]
477-
});
454+
NSMutableDictionary *mutableConfiguration = [configuration mutableCopy];
455+
[mutableConfiguration setObject:binaryHash forKey:PackageHashKey];
456+
resolve(mutableConfiguration);
478457
return;
479458
}
480459

460+
resolve(configuration);
461+
}
462+
463+
/*
464+
* This method is the native side of the CodePush.getCurrentPackage method.
465+
*/
466+
RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
467+
rejecter:(RCTPromiseRejectBlock)reject)
468+
{
469+
NSError *error;
481470
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
482471

483472
if (error) {

ios/CodePush/CodePushPackage.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ @implementation CodePushPackage
1515

1616
+ (NSString *)getBinaryAssetsPath
1717
{
18-
return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[CodePushUpdateUtils getDefaultAssetsFolderName]];
18+
return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[CodePushUpdateUtils getAssetsFolderName]];
1919
}
2020

2121
+ (NSString *)getCodePushPath
@@ -286,7 +286,7 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
286286
}
287287

288288
[[NSFileManager defaultManager] copyItemAtPath:[self getBinaryAssetsPath]
289-
toPath:[newUpdateCodePushPath stringByAppendingPathComponent:[CodePushUpdateUtils getDefaultAssetsFolderName]]
289+
toPath:[newUpdateCodePushPath stringByAppendingPathComponent:[CodePushUpdateUtils getAssetsFolderName]]
290290
error:&error];
291291
if (error) {
292292
failCallback(error);

0 commit comments

Comments
 (0)