1
1
'use strict' ;
2
2
3
- var requestFetchAdapter = require ( "./request-fetch-adapter.js" ) ;
4
- var Sdk = require ( "code-push/script/acquisition-sdk" ) . AcquisitionManager ;
3
+ var { Alert } = require ( "./AlertAdapter" ) ;
5
4
var NativeCodePush = require ( "react-native" ) . NativeModules . CodePush ;
6
5
var 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" ) ;
8
9
9
10
function checkForUpdate ( deploymentKey = null ) {
10
11
var config , sdk ;
11
12
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
+ */
12
22
return getConfiguration ( )
13
23
. 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
+ */
17
31
if ( deploymentKey ) {
18
32
config = Object . assign ( { } , configResult , { deploymentKey } ) ;
19
33
} else {
20
34
config = configResult ;
21
35
}
22
36
23
37
sdk = getSDK ( config ) ;
38
+
24
39
// Allow dynamic overwrite of function. This is only to be used for tests.
25
40
return module . exports . getCurrentPackage ( ) ;
26
41
} )
27
42
. then ( ( localPackage ) => {
28
43
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 ) {
30
54
queryPackage = localPackage ;
31
55
}
56
+
32
57
return new Promise ( ( resolve , reject ) => {
33
58
sdk . queryUpdateWithCurrentPackage ( queryPackage , ( err , update ) => {
34
59
if ( err ) {
35
60
return reject ( err ) ;
36
61
}
37
62
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 ) ) {
41
77
return resolve ( null ) ;
42
- }
78
+ }
43
79
44
80
update = Object . assign ( update , PackageMixins . remote ) ;
45
81
@@ -297,4 +333,4 @@ var CodePush = {
297
333
}
298
334
} ;
299
335
300
- module . exports = CodePush ;
336
+ module . exports = CodePush ;
0 commit comments