diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java index 6d9fa74..3582dac 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java @@ -90,50 +90,50 @@ public class UpdateContext { void onDownloadFailed(Throwable error); } - public void downloadFile(String url, String hashName, DownloadFileListener listener) { + public void downloadFile(String url, String hash, DownloadFileListener listener) { DownloadTaskParams params = new DownloadTaskParams(); params.type = DownloadTaskParams.TASK_TYPE_FULL_DOWNLOAD; params.url = url; - params.hash = hashName; + params.hash = hash; params.listener = listener; - params.zipFilePath = new File(rootDir, hashName + ".ppk"); - params.unzipDirectory = new File(rootDir, hashName); + params.zipFilePath = new File(rootDir, hash + ".ppk"); + params.unzipDirectory = new File(rootDir, hash); new DownloadTask(context).executeOnExecutor(this.executor, params); } - public void downloadPatchFromApk(String url, String hashName, DownloadFileListener listener) { + public void downloadPatchFromApk(String url, String hash, DownloadFileListener listener) { DownloadTaskParams params = new DownloadTaskParams(); params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK; params.url = url; - params.hash = hashName; + params.hash = hash; params.listener = listener; - params.zipFilePath = new File(rootDir, hashName + ".apk.patch"); - params.unzipDirectory = new File(rootDir, hashName); + params.zipFilePath = new File(rootDir, hash + ".apk.patch"); + params.unzipDirectory = new File(rootDir, hash); new DownloadTask(context).executeOnExecutor(this.executor, params); } - public void downloadPatchFromPpk(String url, String hashName, String originHashName, DownloadFileListener listener) { + public void downloadPatchFromPpk(String url, String hash, String originHash, DownloadFileListener listener) { DownloadTaskParams params = new DownloadTaskParams(); params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK; params.url = url; - params.hash = hashName; - params.originHash = originHashName; + params.hash = hash; + params.originHash = originHash; params.listener = listener; - params.zipFilePath = new File(rootDir, originHashName + "-" + hashName + ".ppk.patch"); - params.unzipDirectory = new File(rootDir, hashName); - params.originDirectory = new File(rootDir, originHashName); + params.zipFilePath = new File(rootDir, originHash + "-" + hash + ".ppk.patch"); + params.unzipDirectory = new File(rootDir, hash); + params.originDirectory = new File(rootDir, originHash); new DownloadTask(context).executeOnExecutor(this.executor, params); } private SharedPreferences sp; - public void switchVersion(String hashName) { - if (!new File(rootDir, hashName+"/index.bundlejs").exists()) { - throw new Error("Bundle version " + hashName + " not found."); + public void switchVersion(String hash) { + if (!new File(rootDir, hash+"/index.bundlejs").exists()) { + throw new Error("Bundle version " + hash + " not found."); } String lastVersion = getCurrentVersion(); SharedPreferences.Editor editor = sp.edit(); - editor.putString("currentVersion", hashName); + editor.putString("currentVersion", hash); if (lastVersion != null) { editor.putString("lastVersion", lastVersion); } diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java index 911943a..8a6051b 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java @@ -70,7 +70,7 @@ public class UpdateModule extends ReactContextBaseJavaModule{ @ReactMethod public void downloadUpdate(ReadableMap options, final Promise promise){ String url = options.getString("updateUrl"); - String hash = options.getString("hashName"); + String hash = options.getString("hash"); updateContext.downloadFile(url, hash, new UpdateContext.DownloadFileListener() { @Override public void onDownloadCompleted() { @@ -87,7 +87,7 @@ public class UpdateModule extends ReactContextBaseJavaModule{ @ReactMethod public void downloadPatchFromPackage(ReadableMap options, final Promise promise){ String url = options.getString("updateUrl"); - String hash = options.getString("hashName"); + String hash = options.getString("hash"); updateContext.downloadPatchFromApk(url, hash, new UpdateContext.DownloadFileListener() { @Override public void onDownloadCompleted() { @@ -104,8 +104,8 @@ public class UpdateModule extends ReactContextBaseJavaModule{ @ReactMethod public void downloadPatchFromPpk(ReadableMap options, final Promise promise){ String url = options.getString("updateUrl"); - String hash = options.getString("hashName"); - String originHash = options.getString("originHashName"); + String hash = options.getString("hash"); + String originHash = options.getString("originHash"); updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() { @Override public void onDownloadCompleted() { @@ -121,7 +121,7 @@ public class UpdateModule extends ReactContextBaseJavaModule{ @ReactMethod public void reloadUpdate(ReadableMap options) { - final String hash = options.getString("hashName"); + final String hash = options.getString("hash"); UiThreadUtil.runOnUiThread(new Runnable() { @Override @@ -162,7 +162,7 @@ public class UpdateModule extends ReactContextBaseJavaModule{ @ReactMethod public void setNeedUpdate(ReadableMap options) { - final String hash = options.getString("hashName"); + final String hash = options.getString("hash"); UiThreadUtil.runOnUiThread(new Runnable() { @Override diff --git a/ios/RCTPushy/RCTPushy.m b/ios/RCTPushy/RCTPushy.m index 6fd95d9..a44ed25 100644 --- a/ios/RCTPushy/RCTPushy.m +++ b/ios/RCTPushy/RCTPushy.m @@ -44,7 +44,7 @@ static NSString * const ERROR_FILE_OPERATION = @"file operation error"; // event def static NSString * const EVENT_PROGRESS_DOWNLOAD = @"RCTPushyDownloadProgress"; // static NSString * const EVENT_PROGRESS_UNZIP = @"RCTPushyUnzipProgress"; -static NSString * const PARAM_PROGRESS_HASHNAME = @"hash"; +static NSString * const PARAM_PROGRESS_HASH = @"hash"; static NSString * const PARAM_PROGRESS_RECEIVED = @"received"; static NSString * const PARAM_PROGRESS_TOTAL = @"total"; @@ -248,8 +248,8 @@ RCT_EXPORT_METHOD(downloadPatchFromPpk:(NSDictionary *)options RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options) { - NSString *hashName = options[@"hashName"]; - if (hashName.length) { + NSString *hash = options[@"hash"]; + if (hash.length) { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *lastVersion = nil; if ([defaults objectForKey:keyPushyInfo]) { @@ -258,7 +258,7 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options) } NSMutableDictionary *newInfo = [[NSMutableDictionary alloc] init]; - newInfo[paramCurrentVersion] = hashName; + newInfo[paramCurrentVersion] = hash; newInfo[paramLastVersion] = lastVersion; newInfo[paramIsFirstTime] = @(YES); newInfo[paramIsFirstLoadOk] = @(NO); @@ -271,8 +271,8 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options) RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options) { - NSString *hashName = options[@"hashName"]; - if (hashName.length) { + NSString *hash = options[@"hash"]; + if (hash.length) { [self setNeedUpdate:options]; // reload 0.62+ @@ -327,13 +327,13 @@ RCT_EXPORT_METHOD(markSuccess) - (void)doPushy:(PushyType)type options:(NSDictionary *)options callback:(void (^)(NSError *error))callback { NSString *updateUrl = [RCTConvert NSString:options[@"updateUrl"]]; - NSString *hashName = [RCTConvert NSString:options[@"hashName"]]; - if (updateUrl.length<=0 || hashName.length<=0) { + NSString *hash = [RCTConvert NSString:options[@"hash"]]; + if (updateUrl.length <= 0 || hash.length <= 0) { callback([self errorWithMessage:ERROR_OPTIONS]); return; } - NSString *originHashName = [RCTConvert NSString:options[@"originHashName"]]; - if (type == PushyTypePatchFromPpk && originHashName<=0) { + NSString *originHash = [RCTConvert NSString:options[@"originHash"]]; + if (type == PushyTypePatchFromPpk && originHash <= 0) { callback([self errorWithMessage:ERROR_OPTIONS]); return; } @@ -345,14 +345,14 @@ RCT_EXPORT_METHOD(markSuccess) return; } - NSString *zipFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",hashName, [self zipExtension:type]]]; -// NSString *unzipDir = [dir stringByAppendingPathComponent:hashName]; + NSString *zipFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",hash, [self zipExtension:type]]]; +// NSString *unzipDir = [dir stringByAppendingPathComponent:hash]; RCTLogInfo(@"RCTPushy -- download file %@", updateUrl); [RCTPushyDownloader download:updateUrl savePath:zipFilePath progressHandler:^(long long receivedBytes, long long totalBytes) { if (self->hasListeners) { [self sendEventWithName:EVENT_PROGRESS_DOWNLOAD body:@{ - PARAM_PROGRESS_HASHNAME:hashName, + PARAM_PROGRESS_HASH:hash, PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLongLong:receivedBytes], PARAM_PROGRESS_TOTAL:[NSNumber numberWithLongLong:totalBytes] }]; @@ -363,12 +363,12 @@ RCT_EXPORT_METHOD(markSuccess) } else { RCTLogInfo(@"RCTPushy -- unzip file %@", zipFilePath); - NSString *unzipFilePath = [dir stringByAppendingPathComponent:hashName]; + NSString *unzipFilePath = [dir stringByAppendingPathComponent:hash]; [self->_fileManager unzipFileAtPath:zipFilePath toDestination:unzipFilePath progressHandler:^(NSString *entry,long entryNumber, long total) { // if (self->hasListeners) { // [self sendEventWithName:EVENT_PROGRESS_UNZIP // body:@{ - // PARAM_PROGRESS_HASHNAME:hashName, + // PARAM_PROGRESS_HASH:hash, // PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber], // PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total] // }]; @@ -385,16 +385,16 @@ RCT_EXPORT_METHOD(markSuccess) { NSString *sourceOrigin = [[NSBundle mainBundle] resourcePath]; NSString *bundleOrigin = [[RCTPushy binaryBundleURL] path]; - [self patch:hashName fromBundle:bundleOrigin source:sourceOrigin callback:callback]; + [self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback]; } break; case PushyTypePatchFromPpk: { - NSString *lastVersionDir = [dir stringByAppendingPathComponent:originHashName]; + NSString *lastVersionDir = [dir stringByAppendingPathComponent:originHash]; NSString *sourceOrigin = lastVersionDir; NSString *bundleOrigin = [lastVersionDir stringByAppendingPathComponent:BUNDLE_FILE_NAME]; - [self patch:hashName fromBundle:bundleOrigin source:sourceOrigin callback:callback]; + [self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback]; } break; default: @@ -408,9 +408,9 @@ RCT_EXPORT_METHOD(markSuccess) }]; } -- (void)patch:(NSString *)hashName fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback +- (void)patch:(NSString *)hash fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback { - NSString *unzipDir = [[RCTPushy downloadDir] stringByAppendingPathComponent:hashName]; + NSString *unzipDir = [[RCTPushy downloadDir] stringByAppendingPathComponent:hash]; NSString *sourcePatch = [unzipDir stringByAppendingPathComponent:SOURCE_PATCH_NAME]; NSString *bundlePatch = [unzipDir stringByAppendingPathComponent:BUNDLE_PATCH_NAME]; diff --git a/lib/index.d.ts b/lib/index.d.ts index a1fbe01..3f5958f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -60,7 +60,7 @@ export function setCustomEndpoints({ }): void; interface ProgressData { - hashname: string; + hash: string; received: number; total: number; } diff --git a/lib/index.js b/lib/index.js index dd18d05..59d3f39 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,7 +9,6 @@ const { version: v, } = require('react-native/Libraries/Core/ReactNativeVersion'); const RNVersion = `${v.major}.${v.minor}.${v.patch}`; -const uuidv4 = require('uuid/v4'); let Pushy = NativeModules.Pushy; @@ -35,7 +34,7 @@ if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) { const eventEmitter = new NativeEventEmitter(Pushy); if (!uuid) { - uuid = uuidv4(); + uuid = require('uuid/v4')(); Pushy.setUuid(uuid); } @@ -158,14 +157,14 @@ export async function downloadUpdate(options, eventListeners) { logger('downloading diff'); await Pushy.downloadPatchFromPpk({ updateUrl: options.diffUrl, - hashName: options.hash, - originHashName: currentVersion, + hash: options.hash, + originHash: currentVersion, }); } else if (options.pdiffUrl) { logger('downloading pdiff'); await Pushy.downloadPatchFromPackage({ updateUrl: options.pdiffUrl, - hashName: options.hash, + hash: options.hash, }); } eventEmitter.removeAllListeners('RCTPushyDownloadProgress'); @@ -175,13 +174,13 @@ export async function downloadUpdate(options, eventListeners) { export function switchVersion(hash) { assertRelease(); logger('switchVersion'); - Pushy.reloadUpdate({ hashName: hash }); + Pushy.reloadUpdate({ hash }); } export function switchVersionLater(hash) { assertRelease(); logger('switchVersionLater'); - Pushy.setNeedUpdate({ hashName: hash }); + Pushy.setNeedUpdate({ hash }); } export function markSuccess() {