From df7b02cd22ed1cada9a8f11fe9050fe556d38483 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 4 Apr 2024 18:51:06 -0400 Subject: [PATCH] Added findBestAndTargetInLogs --- android/build.gradle | 2 +- .../main/java/land/fx/fula/FulaModule.java | 16 ++++++++++ ios/Fula.mm | 5 ++++ ios/Fula.swift | 29 +++++++++++++++---- package.json | 2 +- src/interfaces/fulaNativeModule.ts | 4 +++ src/protocols/fxblox.ts | 28 ++++++++++++++++++ src/types/fxblox.ts | 6 ++++ 8 files changed, 85 insertions(+), 7 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index b5af74e..10f0933 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -99,7 +99,7 @@ dependencies { // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" - implementation 'com.github.functionland:fula-build-aar:v1.54.8' // From jitpack.io + implementation 'com.github.functionland:fula-build-aar:v1.54.9' // From jitpack.io implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io implementation 'commons-io:commons-io:20030203.000550' implementation 'commons-codec:commons-codec:1.15' diff --git a/android/src/main/java/land/fx/fula/FulaModule.java b/android/src/main/java/land/fx/fula/FulaModule.java index c8b06d9..c4f4a24 100755 --- a/android/src/main/java/land/fx/fula/FulaModule.java +++ b/android/src/main/java/land/fx/fula/FulaModule.java @@ -1599,6 +1599,22 @@ public void fetchContainerLogs(String containerName, String tailCount, Promise p }); } + @ReactMethod + public void findBestAndTargetInLogs(String containerName, String tailCount, Promise promise) { + ThreadUtils.runOnExecutor(() -> { + Log.d("ReactNative", "findBestAndTargetInLogs"); + try { + byte[] result = this.fula.findBestAndTargetInLogs(containerName, tailCount); + String resultString = toString(result); + Log.d("ReactNative", "result string="+resultString); + promise.resolve(resultString); + } catch (Exception e) { + Log.d("ReactNative", "an error happened="+e.getMessage()); + promise.reject(e); + } + }); + } + @ReactMethod public void getFolderSize(String folderPath, Promise promise) { ThreadUtils.runOnExecutor(() -> { diff --git a/ios/Fula.mm b/ios/Fula.mm index 1c90019..eb963a0 100644 --- a/ios/Fula.mm +++ b/ios/Fula.mm @@ -186,6 +186,11 @@ @interface RCT_EXTERN_MODULE(FulaModule, NSObject) withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(findBestAndTargetInLogs:(NSString *)containerName + withTailCount:(NSString *)tailCount + withResolver:(RCTPromiseResolveBlock)resolve + withRejecter:(RCTPromiseRejectBlock)reject) + RCT_EXTERN_METHOD(getFolderSize:(NSString *)folderPath withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) diff --git a/ios/Fula.swift b/ios/Fula.swift index ee62082..37946e2 100644 --- a/ios/Fula.swift +++ b/ios/Fula.swift @@ -398,12 +398,12 @@ class FulaModule: NSObject { func createPeerIdentity(privateKey: Data) throws -> Data { let secretKey = try Cryptography.generateKey(privateKey) - + var encryptedKey: String? = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID) NSLog("ReactNative createPeerIdentity encryptedKey=\(encryptedKey ?? "nil")") if encryptedKey == nil { let privateKeyString = String(data: privateKey, encoding: .utf8) ?? "" - + guard !privateKeyString.isEmpty else { throw NSError(domain: "KeyGenerationError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Private key string conversion failed"]) } @@ -415,7 +415,7 @@ class FulaModule: NSObject { NSLog("ReactNative createPeerIdentity encryptedKey2=\(encryptedKey)") userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey ?? "") } - + // Assuming decryptMsg returns Data or throws an error if decryption fails guard let encryptedKeyData = encryptedKey, !encryptedKeyData.isEmpty else { throw NSError(domain: "DecryptionError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Encrypted key is empty"]) @@ -429,12 +429,12 @@ class FulaModule: NSObject { func decryptLibp2pIdentity(_ encryptedLibp2pId: String, with encryptionSecretKey: Data) throws -> String { // Convert Data to [UInt8] let secretKeyBytes = [UInt8](encryptionSecretKey) - + // Attempt decryption guard let decryptedBytes = try? Cryptography.decryptMsg(encryptedLibp2pId, secretKeyBytes) else { throw NSError(domain: "DecryptionError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to decrypt Libp2p ID"]) } - + // Assuming decryptedBytes is an array of UInt8 return String(decoding: decryptedBytes, as: UTF8.self) } @@ -1519,6 +1519,25 @@ class FulaModule: NSObject { reject("ERR_FULA", "fetchContainerLogs failed", error) } } + @objc(findBestAndTargetInLogs:withTailCount:withResolver:withRejecter:) + func findBestAndTargetInLogs(containerName: String, tailCount: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { + do { + // Since fetchContainerLogs expects a String for tailCount, pass it directly + let result = try self.fula!.findBestAndTargetInLogs(containerName, tailCount: tailCount) + guard let resultString = result.toUTF8String() else { + // Handle the case where result.toUTF8String() returns nil + let error = NSError(domain: "FULAErrorDomain", + code: 1007, // Choose a suitable error code + userInfo: [NSLocalizedDescriptionKey: "Failed to convert log data to string."]) + reject("ERR_FULA", "Log Conversion Error", error) + return + } + resolve(resultString) + } catch let error as NSError { + print("fetchContainerLogs", error.localizedDescription) + reject("ERR_FULA", "fetchContainerLogs failed", error) + } + } diff --git a/package.json b/package.json index f1665f1..5022e07 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@functionland/react-native-fula", - "version": "1.54.15", + "version": "1.54.16", "description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs", "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/src/interfaces/fulaNativeModule.ts b/src/interfaces/fulaNativeModule.ts index 7fcc86b..39b99ab 100644 --- a/src/interfaces/fulaNativeModule.ts +++ b/src/interfaces/fulaNativeModule.ts @@ -125,6 +125,10 @@ interface FulaNativeModule { containerName: string, tailCount: string ) => Promise; + findBestAndTargetInLogs: ( + containerName: string, + tailCount: string + ) => Promise; getFolderSize: (folderPath: string) => Promise; getDatastoreSize: () => Promise; bloxFreeSpace: () => Promise; diff --git a/src/protocols/fxblox.ts b/src/protocols/fxblox.ts index 21b6e73..b81f731 100644 --- a/src/protocols/fxblox.ts +++ b/src/protocols/fxblox.ts @@ -118,6 +118,34 @@ export const fetchContainerLogs = ( return res; }; +export const findBestAndTargetInLogs = ( + containerName: string, + tailCount: string +): Promise => { + console.log('findBestAndTargetInLogs in react-native started'); + let res = Fula.findBestAndTargetInLogs(containerName, tailCount) + .then((res1) => { + try { + console.log('res1 received'); + console.log(res1); + let jsonRes: BType.FindBestAndTargetInLogsResponse = JSON.parse(res1); + return jsonRes; + } catch (e) { + try { + return JSON.parse(res1); + } catch (e1) { + console.error('Error parsing res in findBestAndTargetInLogs:', e1); + throw e1; // Rethrow the error to maintain the rejection state + } + } + }) + .catch((err) => { + console.error('Error findBestAndTargetInLogs:', err); + throw err; // Rethrow the error to maintain the rejection state + }); + return res; +}; + export const getFolderSize = ( folderPath: string ): Promise => { diff --git a/src/types/fxblox.ts b/src/types/fxblox.ts index c99e766..d40a9a8 100644 --- a/src/types/fxblox.ts +++ b/src/types/fxblox.ts @@ -21,6 +21,12 @@ export interface FetchContainerLogsResponse { msg: string; } +export interface FindBestAndTargetInLogsResponse { + best: string; + target: string; + err: string; +} + export interface GetFolderPathResponse { folder_path: string; size: string;