Skip to content

Commit

Permalink
Added findBestAndTargetInLogs
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Apr 4, 2024
1 parent dd22f0a commit df7b02c
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 16 additions & 0 deletions android/src/main/java/land/fx/fula/FulaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(() -> {
Expand Down
5 changes: 5 additions & 0 deletions ios/Fula.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 24 additions & 5 deletions ios/Fula.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
Expand All @@ -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"])
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}



Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/fulaNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ interface FulaNativeModule {
containerName: string,
tailCount: string
) => Promise<string>;
findBestAndTargetInLogs: (
containerName: string,
tailCount: string
) => Promise<string>;
getFolderSize: (folderPath: string) => Promise<string>;
getDatastoreSize: () => Promise<string>;
bloxFreeSpace: () => Promise<string>;
Expand Down
28 changes: 28 additions & 0 deletions src/protocols/fxblox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ export const fetchContainerLogs = (
return res;
};

export const findBestAndTargetInLogs = (
containerName: string,
tailCount: string
): Promise<BType.FindBestAndTargetInLogsResponse> => {
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<BType.GetFolderPathResponse> => {
Expand Down
6 changes: 6 additions & 0 deletions src/types/fxblox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit df7b02c

Please sign in to comment.