Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions CodePush.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AcquisitionManager as Sdk, DownloadStatus } from "./lib/commonjs/acquisition-sdk/acquisition-sdk";
import { buildQueryPackage } from "./lib/commonjs/build-query-package";
import { onceUntilRejected } from "./lib/commonjs/once-until-rejected";
import { Alert } from "./AlertAdapter";
import requestFetchAdapter from "./request-fetch-adapter";
import { AppState, NativeEventEmitter, Platform } from "react-native";
import { AppState, NativeEventEmitter } from "react-native";
import log, { logFields, packageLogFields, redactUrl } from "./logging";
import hoistStatics from 'hoist-non-react-statics';

Expand Down Expand Up @@ -111,15 +112,7 @@ async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchC
* to send the app version to the server, since we are interested
* in any updates for current binary version, regardless of hash.
*/
let queryPackage;
if (localPackage) {
queryPackage = localPackage;
} else {
queryPackage = { appVersion: config.appVersion };
if (Platform.OS === "ios" && config.packageHash) {
queryPackage.packageHash = config.packageHash;
}
}
const queryPackage = buildQueryPackage(localPackage, config);

log.info(`Checking for update. ${logFields({ appVersion: queryPackage.appVersion, ...packageLogFields(queryPackage), deploymentKey: config.deploymentKey })}`);
const update = await sdk.queryUpdateWithCurrentPackage(queryPackage);
Expand All @@ -137,10 +130,8 @@ async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchC
* bug in the server, but we're adding this check just to double-check that the
* client app is resilient to a potential issue with the update check.
* 4) The server said there is an update, but the update's hash is the same as that
* of the binary's currently running version. This should only happen in Android -
* unlike iOS, we don't attach the binary's hash to the updateCheck request
* because we want to avoid having to install diff updates against the binary's
* version, which we can't do yet on Android.
* of the binary's currently running version. The binary's hash is sent in the
* updateCheck request, so the server can answer with the binary itself.
*/
if (!update) {
log.info("No update available.");
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/build-query-package.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as assert from 'assert';

import { buildQueryPackage } from '../build-query-package';

describe('buildQueryPackage', () => {
it('sends the binary hash when no update is installed', () => {
assert.deepStrictEqual(buildQueryPackage(null, { appVersion: '1.0.0', packageHash: 'abc' }), {
appVersion: '1.0.0',
packageHash: 'abc',
});
});

it('omits the hash when the binary has none', () => {
assert.deepStrictEqual(buildQueryPackage(undefined, { appVersion: '1.0.0', packageHash: null }), {
appVersion: '1.0.0',
});
assert.deepStrictEqual(buildQueryPackage(undefined, { appVersion: '1.0.0' }), { appVersion: '1.0.0' });
});

it('uses the installed package as is', () => {
const localPackage = { appVersion: '1.0.0', packageHash: 'installed' };
assert.strictEqual(buildQueryPackage(localPackage, { appVersion: '1.0.0', packageHash: 'abc' }), localPackage);
});
});
20 changes: 20 additions & 0 deletions src/build-query-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface QueryPackage {
appVersion?: string;
packageHash?: string;
[key: string]: unknown;
}

export function buildQueryPackage(
localPackage: QueryPackage | null | undefined,
config: { appVersion: string; packageHash?: string | null },
): QueryPackage {
if (localPackage) {
return localPackage;
}
const queryPackage: QueryPackage = { appVersion: config.appVersion };
// Null in Android debug builds, which have no binary hash to report.
if (config.packageHash) {
queryPackage.packageHash = config.packageHash;
}
return queryPackage;
}
Loading