Skip to content

Commit df7b02c

Browse files
committed
Added findBestAndTargetInLogs
1 parent dd22f0a commit df7b02c

File tree

8 files changed

+85
-7
lines changed

8 files changed

+85
-7
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ dependencies {
9999
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
100100
//noinspection GradleDynamicVersion
101101
implementation "com.facebook.react:react-native:+"
102-
implementation 'com.github.functionland:fula-build-aar:v1.54.8' // From jitpack.io
102+
implementation 'com.github.functionland:fula-build-aar:v1.54.9' // From jitpack.io
103103
implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io
104104
implementation 'commons-io:commons-io:20030203.000550'
105105
implementation 'commons-codec:commons-codec:1.15'

android/src/main/java/land/fx/fula/FulaModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,22 @@ public void fetchContainerLogs(String containerName, String tailCount, Promise p
15991599
});
16001600
}
16011601

1602+
@ReactMethod
1603+
public void findBestAndTargetInLogs(String containerName, String tailCount, Promise promise) {
1604+
ThreadUtils.runOnExecutor(() -> {
1605+
Log.d("ReactNative", "findBestAndTargetInLogs");
1606+
try {
1607+
byte[] result = this.fula.findBestAndTargetInLogs(containerName, tailCount);
1608+
String resultString = toString(result);
1609+
Log.d("ReactNative", "result string="+resultString);
1610+
promise.resolve(resultString);
1611+
} catch (Exception e) {
1612+
Log.d("ReactNative", "an error happened="+e.getMessage());
1613+
promise.reject(e);
1614+
}
1615+
});
1616+
}
1617+
16021618
@ReactMethod
16031619
public void getFolderSize(String folderPath, Promise promise) {
16041620
ThreadUtils.runOnExecutor(() -> {

ios/Fula.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ @interface RCT_EXTERN_MODULE(FulaModule, NSObject)
186186
withResolver:(RCTPromiseResolveBlock)resolve
187187
withRejecter:(RCTPromiseRejectBlock)reject)
188188

189+
RCT_EXTERN_METHOD(findBestAndTargetInLogs:(NSString *)containerName
190+
withTailCount:(NSString *)tailCount
191+
withResolver:(RCTPromiseResolveBlock)resolve
192+
withRejecter:(RCTPromiseRejectBlock)reject)
193+
189194
RCT_EXTERN_METHOD(getFolderSize:(NSString *)folderPath
190195
withResolver:(RCTPromiseResolveBlock)resolve
191196
withRejecter:(RCTPromiseRejectBlock)reject)

ios/Fula.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,12 @@ class FulaModule: NSObject {
398398

399399
func createPeerIdentity(privateKey: Data) throws -> Data {
400400
let secretKey = try Cryptography.generateKey(privateKey)
401-
401+
402402
var encryptedKey: String? = userDataHelper.getValue(FulaModule.PRIVATE_KEY_STORE_ID)
403403
NSLog("ReactNative createPeerIdentity encryptedKey=\(encryptedKey ?? "nil")")
404404
if encryptedKey == nil {
405405
let privateKeyString = String(data: privateKey, encoding: .utf8) ?? ""
406-
406+
407407
guard !privateKeyString.isEmpty else {
408408
throw NSError(domain: "KeyGenerationError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Private key string conversion failed"])
409409
}
@@ -415,7 +415,7 @@ class FulaModule: NSObject {
415415
NSLog("ReactNative createPeerIdentity encryptedKey2=\(encryptedKey)")
416416
userDataHelper.add(FulaModule.PRIVATE_KEY_STORE_ID, encryptedKey ?? "")
417417
}
418-
418+
419419
// Assuming decryptMsg returns Data or throws an error if decryption fails
420420
guard let encryptedKeyData = encryptedKey, !encryptedKeyData.isEmpty else {
421421
throw NSError(domain: "DecryptionError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Encrypted key is empty"])
@@ -429,12 +429,12 @@ class FulaModule: NSObject {
429429
func decryptLibp2pIdentity(_ encryptedLibp2pId: String, with encryptionSecretKey: Data) throws -> String {
430430
// Convert Data to [UInt8]
431431
let secretKeyBytes = [UInt8](encryptionSecretKey)
432-
432+
433433
// Attempt decryption
434434
guard let decryptedBytes = try? Cryptography.decryptMsg(encryptedLibp2pId, secretKeyBytes) else {
435435
throw NSError(domain: "DecryptionError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to decrypt Libp2p ID"])
436436
}
437-
437+
438438
// Assuming decryptedBytes is an array of UInt8
439439
return String(decoding: decryptedBytes, as: UTF8.self)
440440
}
@@ -1519,6 +1519,25 @@ class FulaModule: NSObject {
15191519
reject("ERR_FULA", "fetchContainerLogs failed", error)
15201520
}
15211521
}
1522+
@objc(findBestAndTargetInLogs:withTailCount:withResolver:withRejecter:)
1523+
func findBestAndTargetInLogs(containerName: String, tailCount: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
1524+
do {
1525+
// Since fetchContainerLogs expects a String for tailCount, pass it directly
1526+
let result = try self.fula!.findBestAndTargetInLogs(containerName, tailCount: tailCount)
1527+
guard let resultString = result.toUTF8String() else {
1528+
// Handle the case where result.toUTF8String() returns nil
1529+
let error = NSError(domain: "FULAErrorDomain",
1530+
code: 1007, // Choose a suitable error code
1531+
userInfo: [NSLocalizedDescriptionKey: "Failed to convert log data to string."])
1532+
reject("ERR_FULA", "Log Conversion Error", error)
1533+
return
1534+
}
1535+
resolve(resultString)
1536+
} catch let error as NSError {
1537+
print("fetchContainerLogs", error.localizedDescription)
1538+
reject("ERR_FULA", "fetchContainerLogs failed", error)
1539+
}
1540+
}
15221541

15231542

15241543

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "1.54.15",
3+
"version": "1.54.16",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/interfaces/fulaNativeModule.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ interface FulaNativeModule {
125125
containerName: string,
126126
tailCount: string
127127
) => Promise<string>;
128+
findBestAndTargetInLogs: (
129+
containerName: string,
130+
tailCount: string
131+
) => Promise<string>;
128132
getFolderSize: (folderPath: string) => Promise<string>;
129133
getDatastoreSize: () => Promise<string>;
130134
bloxFreeSpace: () => Promise<string>;

src/protocols/fxblox.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,34 @@ export const fetchContainerLogs = (
118118
return res;
119119
};
120120

121+
export const findBestAndTargetInLogs = (
122+
containerName: string,
123+
tailCount: string
124+
): Promise<BType.FindBestAndTargetInLogsResponse> => {
125+
console.log('findBestAndTargetInLogs in react-native started');
126+
let res = Fula.findBestAndTargetInLogs(containerName, tailCount)
127+
.then((res1) => {
128+
try {
129+
console.log('res1 received');
130+
console.log(res1);
131+
let jsonRes: BType.FindBestAndTargetInLogsResponse = JSON.parse(res1);
132+
return jsonRes;
133+
} catch (e) {
134+
try {
135+
return JSON.parse(res1);
136+
} catch (e1) {
137+
console.error('Error parsing res in findBestAndTargetInLogs:', e1);
138+
throw e1; // Rethrow the error to maintain the rejection state
139+
}
140+
}
141+
})
142+
.catch((err) => {
143+
console.error('Error findBestAndTargetInLogs:', err);
144+
throw err; // Rethrow the error to maintain the rejection state
145+
});
146+
return res;
147+
};
148+
121149
export const getFolderSize = (
122150
folderPath: string
123151
): Promise<BType.GetFolderPathResponse> => {

src/types/fxblox.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export interface FetchContainerLogsResponse {
2121
msg: string;
2222
}
2323

24+
export interface FindBestAndTargetInLogsResponse {
25+
best: string;
26+
target: string;
27+
err: string;
28+
}
29+
2430
export interface GetFolderPathResponse {
2531
folder_path: string;
2632
size: string;

0 commit comments

Comments
 (0)