11'use strict' ;
22
3- var requestFetchAdapter = require ( "./request-fetch-adapter.js" ) ;
4- var Sdk = require ( "code-push/script/acquisition-sdk" ) . AcquisitionManager ;
3+ var { Alert } = require ( "./AlertAdapter" ) ;
54var NativeCodePush = require ( "react-native" ) . NativeModules . CodePush ;
65var PackageMixins = require ( "./package-mixins" ) ( NativeCodePush ) ;
7- var { Alert } = require ( "./AlertAdapter" ) ;
6+ var requestFetchAdapter = require ( "./request-fetch-adapter.js" ) ;
7+ var Sdk = require ( "code-push/script/acquisition-sdk" ) . AcquisitionManager ;
8+ var semver = require ( "semver" ) ;
89
910function checkForUpdate ( deploymentKey = null ) {
1011 var config , sdk ;
1112
13+ /*
14+ * Before we ask the server if an update exists, we
15+ * need to retrieve three pieces of information from the
16+ * native side: deployment key, app version (e.g. 1.0.1)
17+ * and the hash of the currently running update (if there is one).
18+ * This allows the client to only receive updates which are targetted
19+ * for their specific deployment and version and which are actually
20+ * different from the CodePush update they have already installed.
21+ */
1222 return getConfiguration ( )
1323 . then ( ( configResult ) => {
14- // If a deployment key was explicitly provided,
15- // then let's override the one we retrieved
16- // from the native-side of the app.
24+ /*
25+ * If a deployment key was explicitly provided,
26+ * then let's override the one we retrieved
27+ * from the native-side of the app. This allows
28+ * dynamically "redirecting" end-users at different
29+ * deployments (e.g. an early access deployment for insiders).
30+ */
1731 if ( deploymentKey ) {
1832 config = Object . assign ( { } , configResult , { deploymentKey } ) ;
1933 } else {
2034 config = configResult ;
2135 }
2236
2337 sdk = getSDK ( config ) ;
38+
2439 // Allow dynamic overwrite of function. This is only to be used for tests.
2540 return module . exports . getCurrentPackage ( ) ;
2641 } )
2742 . then ( ( localPackage ) => {
2843 var queryPackage = { appVersion : config . appVersion } ;
29- if ( localPackage && localPackage . appVersion === config . appVersion ) {
44+
45+ /*
46+ * If the app has a previously installed update, and that update
47+ * was targetted at the same app version that is currently running,
48+ * then we want to use its package hash to determine whether a new
49+ * release has been made on the server. Otherwise, we only need
50+ * to send the app version to the server, since we are interested
51+ * in any updates for current app store version, regardless of hash.
52+ */
53+ if ( localPackage && semver . compare ( localPackage . appVersion , config . appVersion ) === 0 ) {
3054 queryPackage = localPackage ;
3155 }
56+
3257 return new Promise ( ( resolve , reject ) => {
3358 sdk . queryUpdateWithCurrentPackage ( queryPackage , ( err , update ) => {
3459 if ( err ) {
3560 return reject ( err ) ;
3661 }
3762
38- // Ignore updates that require a newer app version,
39- // since the end-user couldn't reliably install it
40- if ( ! update || update . updateAppVersion ) {
63+ /*
64+ * There are three cases where checkForUpdate will resolve to null:
65+ * ----------------------------------------------------------------
66+ * 1) The server said there isn't an update. This is the most common case.
67+ * 2) The server said there is an update but it requires a newer binary version.
68+ * This would occur when end-users are running an older app store version than
69+ * is available, and CodePush is making sure they don't get an update that
70+ * potentially wouldn't be compatible with what they are running.
71+ * 3) The server said there is an update, but the update's hash is the same as
72+ * the currently running update. This should _never_ happen, unless there is a
73+ * bug in the server, but we're adding this check just to double-check that the
74+ * client app is resilient to a potential issue with the update check.
75+ */
76+ if ( ! update || update . updateAppVersion || ( update . packageHash === localPackage . packageHash ) ) {
4177 return resolve ( null ) ;
42- }
78+ }
4379
4480 update = Object . assign ( update , PackageMixins . remote ) ;
4581
@@ -297,4 +333,4 @@ var CodePush = {
297333 }
298334} ;
299335
300- module . exports = CodePush ;
336+ module . exports = CodePush ;
0 commit comments