diff --git a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h --- a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h @@ -29,6 +29,8 @@ readBufferFromFile(jsi::Runtime &rt, jsi::String path) override; virtual jsi::String base64EncodeBuffer(jsi::Runtime &rt, jsi::Object data) override; + virtual jsi::Object + base64DecodeBuffer(jsi::Runtime &rt, jsi::String base64) override; virtual jsi::String sha256(jsi::Runtime &rt, jsi::Object data) override; public: diff --git a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp @@ -113,6 +113,23 @@ return jsi::String::createFromUtf8(rt, base64); } +jsi::Object +CommUtilsModule::base64DecodeBuffer(jsi::Runtime &rt, jsi::String base64) { + auto bytes = Base64::decode(base64.utf8(rt)); + auto size = bytes.size(); + + auto arrayBuffer = + rt.global() + .getPropertyAsFunction(rt, "ArrayBuffer") + // ArrayBuffer constructor takes one parameter: byte length + .callAsConstructor(rt, {static_cast(size)}) + .asObject(rt) + .getArrayBuffer(rt); + + memcpy(arrayBuffer.data(rt), bytes.data(), size); + return arrayBuffer; +} + jsi::String CommUtilsModule::sha256(jsi::Runtime &rt, jsi::Object data) { auto arrayBuffer = data.getArrayBuffer(rt); auto inputPtr = arrayBuffer.data(rt); diff --git a/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp b/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp --- a/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp +++ b/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp @@ -21,6 +21,9 @@ static jsi::Value __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_base64EncodeBuffer(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->base64EncodeBuffer(rt, args[0].asObject(rt)); } +static jsi::Value __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_base64DecodeBuffer(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->base64DecodeBuffer(rt, args[0].asString(rt)); +} static jsi::Value __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_sha256(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->sha256(rt, args[0].asObject(rt)); } @@ -30,6 +33,7 @@ methodMap_["writeBufferToFile"] = MethodMetadata {2, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_writeBufferToFile}; methodMap_["readBufferFromFile"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_readBufferFromFile}; methodMap_["base64EncodeBuffer"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_base64EncodeBuffer}; + methodMap_["base64DecodeBuffer"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_base64DecodeBuffer}; methodMap_["sha256"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_sha256}; } diff --git a/native/cpp/CommonCpp/_generated/utilsJSI.h b/native/cpp/CommonCpp/_generated/utilsJSI.h --- a/native/cpp/CommonCpp/_generated/utilsJSI.h +++ b/native/cpp/CommonCpp/_generated/utilsJSI.h @@ -23,6 +23,7 @@ virtual jsi::Value writeBufferToFile(jsi::Runtime &rt, jsi::String path, jsi::Object data) = 0; virtual jsi::Value readBufferFromFile(jsi::Runtime &rt, jsi::String path) = 0; virtual jsi::String base64EncodeBuffer(jsi::Runtime &rt, jsi::Object data) = 0; + virtual jsi::Object base64DecodeBuffer(jsi::Runtime &rt, jsi::String base64) = 0; virtual jsi::String sha256(jsi::Runtime &rt, jsi::Object data) = 0; }; @@ -69,6 +70,14 @@ return bridging::callFromJs( rt, &T::base64EncodeBuffer, jsInvoker_, instance_, std::move(data)); } + jsi::Object base64DecodeBuffer(jsi::Runtime &rt, jsi::String base64) override { + static_assert( + bridging::getParameterCount(&T::base64DecodeBuffer) == 2, + "Expected base64DecodeBuffer(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::base64DecodeBuffer, jsInvoker_, instance_, std::move(base64)); + } jsi::String sha256(jsi::Runtime &rt, jsi::Object data) override { static_assert( bridging::getParameterCount(&T::sha256) == 2, diff --git a/native/schema/CommUtilsModuleSchema.js b/native/schema/CommUtilsModuleSchema.js --- a/native/schema/CommUtilsModuleSchema.js +++ b/native/schema/CommUtilsModuleSchema.js @@ -12,6 +12,7 @@ +writeBufferToFile: (path: string, data: JSIArrayBuffer) => Promise; +readBufferFromFile: (path: string) => Promise; +base64EncodeBuffer: (data: JSIArrayBuffer) => string; + +base64DecodeBuffer: (base64: string) => JSIArrayBuffer; // crypto utils +sha256: (data: JSIArrayBuffer) => string; } @@ -21,6 +22,7 @@ +writeBufferToFile: (path: string, data: ArrayBuffer) => Promise; +readBufferFromFile: (path: string) => Promise; +base64EncodeBuffer: (data: ArrayBuffer) => string; + +base64DecodeBuffer: (base64: string) => ArrayBuffer; +sha256: (data: ArrayBuffer) => string; }