@novacraft-engineering/pulsar-code-push 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CodePush.js CHANGED
@@ -8,7 +8,7 @@ import hoistStatics from 'hoist-non-react-statics';
8
8
  let NativeCodePush = require("react-native").NativeModules.CodePush;
9
9
  const PackageMixins = require("./package-mixins")(NativeCodePush);
10
10
 
11
- async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchCallback = null) {
11
+ async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchCallback = null, serverUrl = null) {
12
12
  /*
13
13
  * Before we ask the server if an update exists, we
14
14
  * need to retrieve three pieces of information from the
@@ -26,7 +26,13 @@ async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchC
26
26
  * dynamically "redirecting" end-users at different
27
27
  * deployments (e.g. an early access deployment for insiders).
28
28
  */
29
- const config = deploymentKey ? { ...nativeConfig, ...{ deploymentKey } } : nativeConfig;
29
+ // Allow the deployment key AND server URL to be overridden per-call, so one client can
30
+ // check multiple backends (e.g. RevoPush and Pulsar) in a single update cycle.
31
+ const config = {
32
+ ...nativeConfig,
33
+ ...(deploymentKey ? { deploymentKey } : {}),
34
+ ...(serverUrl ? { serverUrl } : {}),
35
+ };
30
36
  const sdk = getPromisifiedSdk(requestFetchAdapter, config);
31
37
 
32
38
  // Use dynamically overridden getCurrentPackage() during tests.
@@ -364,6 +370,7 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
364
370
  let resolvedInstallMode;
365
371
  const syncOptions = {
366
372
  deploymentKey: null,
373
+ serverUrl: null,
367
374
  ignoreFailedUpdates: true,
368
375
  rollbackRetryOptions: null,
369
376
  installMode: CodePush.InstallMode.ON_NEXT_RESTART,
@@ -416,7 +423,7 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
416
423
  await CodePush.notifyApplicationReady();
417
424
 
418
425
  syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
419
- const remotePackage = await checkForUpdate(syncOptions.deploymentKey, handleBinaryVersionMismatchCallback);
426
+ const remotePackage = await checkForUpdate(syncOptions.deploymentKey, handleBinaryVersionMismatchCallback, syncOptions.serverUrl);
420
427
 
421
428
  const doDownloadAndInstall = async () => {
422
429
  syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
@@ -69,9 +69,9 @@ gradle.projectsEvaluated {
69
69
 
70
70
  def nodeModulesPath;
71
71
  if (project.hasProperty('nodeModulesPath')) {
72
- nodeModulesPath = "${project.nodeModulesPath}/@revopush/react-native-code-push"
72
+ nodeModulesPath = "${project.nodeModulesPath}/@novacraft-engineering/pulsar-code-push"
73
73
  } else {
74
- nodeModulesPath = findNodeModulePath(projectDir, "@revopush/react-native-code-push")
74
+ nodeModulesPath = findNodeModulePath(projectDir, "@novacraft-engineering/pulsar-code-push")
75
75
  }
76
76
 
77
77
  def targetName = variant.name.capitalize()
package/package.json CHANGED
@@ -1,18 +1,28 @@
1
1
  {
2
2
  "name": "@novacraft-engineering/pulsar-code-push",
3
- "version": "1.5.0",
4
- "description": "Pulsar OTA client for React Native — over-the-air JavaScript updates. A CodePush-compatible client (RevoPush lineage) pointed at Pulsar.",
3
+ "version": "1.5.2",
4
+ "description": "Pulsar OTA client for React Native \u2014 over-the-air JavaScript updates. A CodePush-compatible client (RevoPush lineage) pointed at Pulsar.",
5
5
  "main": "CodePush.js",
6
6
  "typings": "typings/react-native-code-push.d.ts",
7
7
  "homepage": "https://pulsar.novacraft.africa",
8
- "keywords": ["react-native", "code-push", "codepush", "pulsar", "ota", "over-the-air", "hot-update"],
8
+ "keywords": [
9
+ "react-native",
10
+ "code-push",
11
+ "codepush",
12
+ "pulsar",
13
+ "ota",
14
+ "over-the-air",
15
+ "hot-update"
16
+ ],
9
17
  "author": "Novacraft Engineering",
10
18
  "license": "MIT",
11
19
  "repository": {
12
20
  "type": "git",
13
21
  "url": "git+https://github.com/Novacraft-Africa/pulsar-code-push.git"
14
22
  },
15
- "publishConfig": {"access": "public"},
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
16
26
  "files": [
17
27
  "CodePush.js",
18
28
  "AlertAdapter.js",
@@ -23,6 +33,7 @@
23
33
  "CodePush.podspec",
24
34
  "android",
25
35
  "ios",
36
+ "scripts",
26
37
  "typings",
27
38
  "README.md",
28
39
  "LICENSE"
@@ -45,7 +56,9 @@
45
56
  "packageInstance": "new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
46
57
  },
47
58
  "ios": {
48
- "sharedLibraries": ["libz"]
59
+ "sharedLibraries": [
60
+ "libz"
61
+ ]
49
62
  }
50
63
  }
51
- }
64
+ }
@@ -0,0 +1,165 @@
1
+ /*
2
+ * This script generates a hash of all the React Native bundled assets and writes it into
3
+ * into the APK. The hash in "updateCheck" requests to prevent downloading an identical
4
+ * update to the one already present in the binary.
5
+ *
6
+ * It first creates a snapshot of the contents in the resource directory by creating
7
+ * a map with the modified time of all the files in the directory. It then compares this
8
+ * snapshot with the one saved earlier in "recordFilesBeforeBundleCommand.js" to figure
9
+ * out which files were generated by the "react-native bundle" command. It then computes
10
+ * the hash for each file to generate a manifest, and then computes a hash over the entire
11
+ * manifest to generate the final hash, which is saved to the APK's assets directory.
12
+ */
13
+
14
+ var crypto = require("crypto");
15
+ var fs = require("fs");
16
+ var path = require("path");
17
+
18
+ var getFilesInFolder = require("./getFilesInFolder");
19
+
20
+ const CODE_PUSH_FOLDER_PREFIX = "CodePush";
21
+ const CODE_PUSH_HASH_FILE_NAME = "CodePushHash";
22
+ const CODE_PUSH_METADATA_FILE_NAME = "CodePushMetadata";
23
+ const CODE_PUSH_PACKAGE_HASH = "packageHash"
24
+ const CODE_PUSH_BUNDLE_HASH = "bundleHash"
25
+ const CODE_PUSH_ASSET_HASH = "assetHash"
26
+ const MANIFEST = "manifest"
27
+ const CODE_PUSH_HASH_OLD_FILE_NAME = "CodePushHash.json";
28
+ const HASH_ALGORITHM = "sha256";
29
+
30
+ var resourcesDir = process.argv[2];
31
+ var jsBundleFilePath = process.argv[3];
32
+ var assetsDir = process.argv[4];
33
+ var tempFileName = process.argv[5];
34
+
35
+ var oldFileToModifiedTimeMap = {};
36
+ var tempFileLocalPath = null;
37
+ if (tempFileName) {
38
+ tempFileLocalPath = path.join(require("os").tmpdir(), tempFileName);
39
+ oldFileToModifiedTimeMap = require(tempFileLocalPath);
40
+ }
41
+ var resourceFiles = [];
42
+
43
+ getFilesInFolder(resourcesDir, resourceFiles);
44
+
45
+ function getBundleResourceEntries(resourcesDir, assetsDir) {
46
+ const resourceFiles = [];
47
+ const assetFiles = [];
48
+ getFilesInFolder(resourcesDir, resourceFiles);
49
+ getFilesInFolder(assetsDir, assetFiles);
50
+ let resourceFilesRelative = resourceFiles.map((it) => path.relative(resourcesDir, it.path));
51
+ let assetsFilesRelative = assetFiles.map((it) => path.relative(assetsDir, it.path));
52
+
53
+ const normalizePath = (it) => path.join(CODE_PUSH_FOLDER_PREFIX, it).replace(/\\/g, "/")
54
+ return {
55
+ resources: Object.fromEntries(resourceFilesRelative.map((it) => [normalizePath(it), path.parse(it).name])),
56
+ assets: Object.fromEntries(assetsFilesRelative.map((it) => [it.replace(/\\/g, "/"), normalizePath(it)]))
57
+ }
58
+ }
59
+
60
+ var newFileToModifiedTimeMap = {};
61
+
62
+ resourceFiles.forEach(function (resourceFile) {
63
+ newFileToModifiedTimeMap[resourceFile.path.substring(resourcesDir.length)] = resourceFile.mtime;
64
+ });
65
+
66
+ var bundleGeneratedAssetFiles = [];
67
+
68
+ for (var newFilePath in newFileToModifiedTimeMap) {
69
+ if (!oldFileToModifiedTimeMap[newFilePath] || oldFileToModifiedTimeMap[newFilePath] < newFileToModifiedTimeMap[newFilePath].getTime()) {
70
+ bundleGeneratedAssetFiles.push(newFilePath);
71
+ }
72
+ }
73
+
74
+ var manifest = [];
75
+
76
+ if (bundleGeneratedAssetFiles.length) {
77
+ bundleGeneratedAssetFiles.forEach(function (assetFile) {
78
+ // Generate hash for each asset file
79
+ addFileToManifest(resourcesDir, assetFile, manifest, function () {
80
+ if (manifest.length === bundleGeneratedAssetFiles.length) {
81
+ addJsBundleAndMetaToManifest();
82
+ }
83
+ });
84
+ });
85
+ } else {
86
+ addJsBundleAndMetaToManifest();
87
+ }
88
+
89
+ function addJsBundleAndMetaToManifest() {
90
+ addFileToManifest(path.dirname(jsBundleFilePath), path.basename(jsBundleFilePath), manifest, function (jsBundleFileHash) {
91
+ var jsBundleMetaFilePath = jsBundleFilePath + ".meta";
92
+ addFileToManifest(path.dirname(jsBundleMetaFilePath), path.basename(jsBundleMetaFilePath), manifest, function () {
93
+ manifest = manifest.sort();
94
+ const finalHash = crypto.createHash(HASH_ALGORITHM)
95
+ .update(JSON.stringify(manifest))
96
+ .digest("hex");
97
+
98
+ const bundleHashDada = path.join(CODE_PUSH_FOLDER_PREFIX, path.basename(jsBundleFilePath)).replace(/\\/g, "/") + ":" + jsBundleFileHash;
99
+ const bundleHash = crypto.createHash(HASH_ALGORITHM)
100
+ .update(JSON.stringify([bundleHashDada]))
101
+ .digest("hex");
102
+
103
+ const assetHash = crypto.createHash(HASH_ALGORITHM)
104
+ .update(JSON.stringify([...manifest].filter(it => it !== bundleHashDada).sort()))
105
+ .digest("hex");
106
+
107
+ const manifestMetadata = {
108
+ [CODE_PUSH_PACKAGE_HASH]: finalHash,
109
+ [CODE_PUSH_BUNDLE_HASH]: bundleHash,
110
+ [CODE_PUSH_ASSET_HASH]: assetHash,
111
+ ...(getBundleResourceEntries(resourcesDir, assetsDir)),
112
+ [MANIFEST]: manifest
113
+ }
114
+
115
+ console.log(finalHash);
116
+
117
+ fs.writeFileSync(path.join(assetsDir, CODE_PUSH_HASH_FILE_NAME), finalHash);
118
+ fs.writeFileSync(path.join(assetsDir, CODE_PUSH_METADATA_FILE_NAME), JSON.stringify(manifestMetadata));
119
+
120
+ // "CodePushHash.json" file name breaks flow type checking.
121
+ // To fix the issue we need to delete "CodePushHash.json" file and
122
+ // use "CodePushHash" file name instead to store the hash value.
123
+ // Relates to https://github.com/microsoft/react-native-code-push/issues/577
124
+ var oldSavedResourcesManifestPath = assetsDir + "/" + CODE_PUSH_HASH_OLD_FILE_NAME;
125
+ if (fs.existsSync(oldSavedResourcesManifestPath)) {
126
+ fs.unlinkSync(oldSavedResourcesManifestPath);
127
+ }
128
+ });
129
+ });
130
+ }
131
+
132
+ function addFileToManifest(folder, assetFile, manifest, done) {
133
+ var fullFilePath = path.join(folder, assetFile);
134
+ if (!fileExists(fullFilePath)) {
135
+ done(undefined);
136
+ return;
137
+ }
138
+
139
+ var readStream = fs.createReadStream(path.join(folder, assetFile));
140
+ var hashStream = crypto.createHash(HASH_ALGORITHM);
141
+
142
+ readStream.pipe(hashStream)
143
+ .on("error", function (error) {
144
+ throw error;
145
+ })
146
+ .on("finish", function () {
147
+ hashStream.end();
148
+ var buffer = hashStream.read();
149
+ var fileHash = buffer.toString("hex");
150
+ manifest.push(path.join(CODE_PUSH_FOLDER_PREFIX, assetFile).replace(/\\/g, "/") + ":" + fileHash);
151
+ done(fileHash);
152
+ });
153
+ }
154
+
155
+ function fileExists(file) {
156
+ try {
157
+ return fs.statSync(file).isFile();
158
+ } catch (e) {
159
+ return false;
160
+ }
161
+ }
162
+
163
+ if (tempFileLocalPath) {
164
+ fs.unlinkSync(tempFileLocalPath);
165
+ }
@@ -0,0 +1,19 @@
1
+ var fs = require("fs");
2
+ var path = require("path");
3
+
4
+ // Utility function that collects the stats of every file in a directory
5
+ // as well as in its subdirectories.
6
+ function getFilesInFolder(folderName, fileList) {
7
+ var folderFiles = fs.readdirSync(folderName);
8
+ folderFiles.forEach(function(file) {
9
+ var fileStats = fs.statSync(path.join(folderName, file));
10
+ if (fileStats.isDirectory()) {
11
+ getFilesInFolder(path.join(folderName, file), fileList);
12
+ } else {
13
+ fileStats.path = path.join(folderName, file);
14
+ fileList.push(fileStats);
15
+ }
16
+ });
17
+ }
18
+
19
+ module.exports = getFilesInFolder;
@@ -0,0 +1,41 @@
1
+ /*
2
+ * This script creates a snapshot of the contents in the resource directory
3
+ * by creating a map with the modified time of all the files in the directory
4
+ * and saving it to a temp file. This snapshot is later referenced in
5
+ * "generatePackageHash.js" to figure out which files have changed or were
6
+ * newly generated by the "react-native bundle" command.
7
+ */
8
+
9
+ var fs = require("fs");
10
+ var path = require("path");
11
+
12
+ var getFilesInFolder = require("./getFilesInFolder");
13
+
14
+
15
+ var resourcesDir = process.argv[2];
16
+ var tempFileName = process.argv[3];
17
+
18
+ var tempFileLocalPath = path.join(require("os").tmpdir(), tempFileName);
19
+ var resourceFiles = [];
20
+
21
+ try {
22
+ getFilesInFolder(resourcesDir, resourceFiles);
23
+ } catch(error) {
24
+ var targetPathNotFoundExceptionMessage = "\nResources directory path does not exist.\n";
25
+ targetPathNotFoundExceptionMessage += "Unable to find '" + resourcesDir;
26
+ targetPathNotFoundExceptionMessage += "' directory. Please check version of Android Plugin for Gradle.";
27
+ error.message += targetPathNotFoundExceptionMessage;
28
+ throw error;
29
+ }
30
+
31
+ var fileToModifiedTimeMap = {};
32
+
33
+ resourceFiles.forEach(function(resourceFile) {
34
+ fileToModifiedTimeMap[resourceFile.path.substring(resourcesDir.length)] = resourceFile.mtime.getTime();
35
+ });
36
+
37
+ fs.writeFile(tempFileLocalPath, JSON.stringify(fileToModifiedTimeMap), function(err) {
38
+ if (err) {
39
+ throw err;
40
+ }
41
+ });