Skip to content

Commit eb51066

Browse files
authored
Binary Hashing (Android) (#401)
binary hashing android
1 parent 660d698 commit eb51066

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed
+50-41
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
* 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
3+
* into the APK. The hash in "updateCheck" requests to prevent downloading an identical
44
* 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
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
88
* snapshot with the one saved earlier in "recordFilesBeforeBundleCommand.js" to figure
99
* out which files were generated by the "react-native bundle" command. It then computes
1010
* the hash for each file to generate a manifest, and then computes a hash over the entire
@@ -49,44 +49,53 @@ var manifest = [];
4949
if (bundleGeneratedAssetFiles.length) {
5050
bundleGeneratedAssetFiles.forEach(function(assetFile) {
5151
// Generate hash for each asset file
52-
var readStream = fs.createReadStream(resourcesDir + assetFile);
53-
var hashStream = crypto.createHash(HASH_ALGORITHM);
54-
55-
readStream.pipe(hashStream)
56-
.on("error", function(error) {
57-
throw error;
58-
})
59-
.on("finish", function() {
60-
hashStream.end();
61-
var buffer = hashStream.read();
62-
var fileHash = buffer.toString("hex");
63-
manifest.push(CODE_PUSH_FOLDER_PREFIX + assetFile.replace(/\\/g, "/") + ":" + fileHash);
64-
65-
if (manifest.length === bundleGeneratedAssetFiles.length) {
66-
// Generate hash for JS bundle
67-
readStream = fs.createReadStream(jsBundleFilePath);
68-
hashStream = crypto.createHash(HASH_ALGORITHM);
69-
readStream.pipe(hashStream)
70-
.on("error", function(error) {
71-
throw error;
72-
})
73-
.on("finish", function() {
74-
hashStream.end();
75-
var buffer = hashStream.read();
76-
var fileHash = buffer.toString("hex");
77-
manifest.push(CODE_PUSH_FOLDER_PREFIX + "/" + path.basename(jsBundleFilePath) + ":" + fileHash);
78-
manifest = manifest.sort();
79-
80-
var finalHash = crypto.createHash(HASH_ALGORITHM)
81-
.update(JSON.stringify(manifest))
82-
.digest("hex");
83-
84-
var savedResourcesManifestPath = assetsDir + "/" + CODE_PUSH_HASH_FILE_NAME;
85-
fs.writeFileSync(savedResourcesManifestPath, finalHash);
86-
});
87-
}
88-
});
52+
addFileToManifest(resourcesDir, assetFile, manifest, function() {
53+
if (manifest.length === bundleGeneratedAssetFiles.length) {
54+
// Generate hash for JS bundle
55+
addFileToManifest(path.dirname(jsBundleFilePath), path.basename(jsBundleFilePath), manifest, function() {
56+
// ...and the JS bundle "meta"
57+
var jsBundleMetaFilePath = jsBundleFilePath + ".meta";
58+
addFileToManifest(path.dirname(jsBundleMetaFilePath), path.basename(jsBundleMetaFilePath), manifest, function() {
59+
manifest = manifest.sort();
60+
var finalHash = crypto.createHash(HASH_ALGORITHM)
61+
.update(JSON.stringify(manifest))
62+
.digest("hex");
63+
64+
var savedResourcesManifestPath = assetsDir + "/" + CODE_PUSH_HASH_FILE_NAME;
65+
fs.writeFileSync(savedResourcesManifestPath, finalHash);
66+
});
67+
});
68+
}
69+
});
8970
});
9071
}
9172

73+
function addFileToManifest(folder, assetFile, manifest, done) {
74+
var fullFilePath = path.join(folder, assetFile);
75+
if (!fileExists(fullFilePath)) {
76+
done();
77+
return;
78+
}
79+
80+
var readStream = fs.createReadStream(path.join(folder, assetFile));
81+
var hashStream = crypto.createHash(HASH_ALGORITHM);
82+
83+
readStream.pipe(hashStream)
84+
.on("error", function(error) {
85+
throw error;
86+
})
87+
.on("finish", function() {
88+
hashStream.end();
89+
var buffer = hashStream.read();
90+
var fileHash = buffer.toString("hex");
91+
manifest.push(path.join(CODE_PUSH_FOLDER_PREFIX, assetFile).replace(/\\/g, "/") + ":" + fileHash);
92+
done();
93+
});
94+
}
95+
96+
function fileExists(file) {
97+
try { return fs.statSync(file).isFile(); }
98+
catch (e) { return false; }
99+
}
100+
92101
fs.unlinkSync(TEMP_FILE_PATH);

0 commit comments

Comments
 (0)