diff --git a/binding.gyp b/binding.gyp index 8389e1aa..525f60c9 100644 --- a/binding.gyp +++ b/binding.gyp @@ -27,6 +27,7 @@ "native-src/sync_root_interface/callbacks/FetchData/FileCopierWithProgress.cpp", "native-src/sync_root_interface/callbacks/FetchData/TransferContext.cpp", "native-src/virtual_drive/Wrappers.cpp", + "native-src/virtual_drive/connect_sync_root.cpp", "native-src/virtual_drive/create_file_placeholder.cpp", "native-src/virtual_drive/create_folder_placeholder.cpp", "native-src/virtual_drive/get_file_identity.cpp", diff --git a/dist/addon.node b/dist/addon.node index 2f7da47a..856a5c92 100644 Binary files a/dist/addon.node and b/dist/addon.node differ diff --git a/include/virtual_drive/connect_sync_root.h b/include/virtual_drive/connect_sync_root.h new file mode 100644 index 00000000..6688aa73 --- /dev/null +++ b/include/virtual_drive/connect_sync_root.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +napi_value connect_sync_root_impl(napi_env env, napi_callback_info args); diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index a643b716..e6eedbfd 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -12,6 +12,7 @@ #include "create_folder_placeholder.h" #include "create_file_placeholder.h" #include "get_file_identity.h" +#include "connect_sync_root.h" #include "napi_safe_wrap.h" std::string WStringToUTF8(const std::wstring &wstr) @@ -149,91 +150,7 @@ napi_value GetRegisteredSyncRootsWrapper(napi_env env, napi_callback_info args) napi_value ConnectSyncRootWrapper(napi_env env, napi_callback_info args) { - try - { - - size_t argc = 2; - napi_value argv[2]; - - napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); - - if (argc < 2) - { - napi_throw_error(env, nullptr, "Se requieren más argumentos para ConnectSyncRoot"); - return nullptr; - } - - LPCWSTR syncRootPath; - size_t pathLength; - napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength); - syncRootPath = new WCHAR[pathLength + 1]; - napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(syncRootPath)), pathLength + 1, nullptr); - - // CALLBACKS - InputSyncCallbacks callbacks = {}; - - napi_value fetchDataCallback; - napi_value cancelFetchDataCallback; - - if (napi_get_named_property(env, argv[1], "fetchDataCallback", &fetchDataCallback) == napi_ok) - { - napi_create_reference(env, fetchDataCallback, 1, &callbacks.fetch_data_callback_ref); - } - - napi_valuetype valuetype_fetch_data; - napi_status type_status_fetch_data = napi_typeof(env, fetchDataCallback, &valuetype_fetch_data); - if (type_status_fetch_data != napi_ok || valuetype_fetch_data != napi_function) - { - napi_throw_error(env, nullptr, "fetchDataCallback should be a function."); - return nullptr; - } - - if (napi_get_named_property(env, argv[1], "cancelFetchDataCallback", &cancelFetchDataCallback) == napi_ok) - { - napi_create_reference(env, cancelFetchDataCallback, 1, &callbacks.cancel_fetch_data_callback_ref); - } - - napi_valuetype valuetype_cancel_fetch_data; - napi_status type_status_cancel_fetch_data = napi_typeof(env, cancelFetchDataCallback, &valuetype_cancel_fetch_data); - if (type_status_cancel_fetch_data != napi_ok || valuetype_cancel_fetch_data != napi_function) - { - napi_throw_error(env, nullptr, "cancelFetchDataCallback should be a function."); - return nullptr; - } - - CF_CONNECTION_KEY connectionKey; - HRESULT hr = SyncRoot::ConnectSyncRoot(syncRootPath, callbacks, env, &connectionKey); - - delete[] syncRootPath; - - if (FAILED(hr)) - { - napi_throw_error(env, nullptr, "ConnectSyncRoot failed"); - return nullptr; - } - - napi_value resultObj, hrValue, connectionKeyValue; - - napi_create_object(env, &resultObj); - - napi_create_int32(env, static_cast(hr), &hrValue); - napi_set_named_property(env, resultObj, "hr", hrValue); - - std::wstringstream ss; - ss << connectionKey.Internal; - - std::wstring connectionKeyString = ss.str(); - napi_create_string_utf16(env, reinterpret_cast(connectionKeyString.c_str()), connectionKeyString.length(), &connectionKeyValue); - - napi_set_named_property(env, resultObj, "connectionKey", connectionKeyValue); - - return resultObj; - } - catch (...) - { - napi_throw_error(env, nullptr, "An unknown error occurred in ConnectSyncRootWrapper"); - return nullptr; - } + return napi_safe_wrap(env, args, connect_sync_root_impl); } napi_value CreateEntryWrapper(napi_env env, napi_callback_info args) diff --git a/native-src/virtual_drive/connect_sync_root.cpp b/native-src/virtual_drive/connect_sync_root.cpp new file mode 100644 index 00000000..297777bb --- /dev/null +++ b/native-src/virtual_drive/connect_sync_root.cpp @@ -0,0 +1,82 @@ +#include +#include "SyncRoot.h" + +napi_value connect_sync_root_impl(napi_env env, napi_callback_info args) +{ + size_t argc = 2; + napi_value argv[2]; + + napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); + + if (argc < 2) + { + napi_throw_error(env, nullptr, "Se requieren más argumentos para ConnectSyncRoot"); + return nullptr; + } + + LPCWSTR syncRootPath; + size_t pathLength; + napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength); + syncRootPath = new WCHAR[pathLength + 1]; + napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(syncRootPath)), pathLength + 1, nullptr); + + // CALLBACKS + InputSyncCallbacks callbacks = {}; + + napi_value fetchDataCallback; + napi_value cancelFetchDataCallback; + + if (napi_get_named_property(env, argv[1], "fetchDataCallback", &fetchDataCallback) == napi_ok) + { + napi_create_reference(env, fetchDataCallback, 1, &callbacks.fetch_data_callback_ref); + } + + napi_valuetype valuetype_fetch_data; + napi_status type_status_fetch_data = napi_typeof(env, fetchDataCallback, &valuetype_fetch_data); + if (type_status_fetch_data != napi_ok || valuetype_fetch_data != napi_function) + { + napi_throw_error(env, nullptr, "fetchDataCallback should be a function."); + return nullptr; + } + + if (napi_get_named_property(env, argv[1], "cancelFetchDataCallback", &cancelFetchDataCallback) == napi_ok) + { + napi_create_reference(env, cancelFetchDataCallback, 1, &callbacks.cancel_fetch_data_callback_ref); + } + + napi_valuetype valuetype_cancel_fetch_data; + napi_status type_status_cancel_fetch_data = napi_typeof(env, cancelFetchDataCallback, &valuetype_cancel_fetch_data); + if (type_status_cancel_fetch_data != napi_ok || valuetype_cancel_fetch_data != napi_function) + { + napi_throw_error(env, nullptr, "cancelFetchDataCallback should be a function."); + return nullptr; + } + + CF_CONNECTION_KEY connectionKey; + HRESULT hr = SyncRoot::ConnectSyncRoot(syncRootPath, callbacks, env, &connectionKey); + + delete[] syncRootPath; + + if (FAILED(hr)) + { + napi_throw_error(env, nullptr, "ConnectSyncRoot failed"); + return nullptr; + } + + napi_value resultObj, hrValue, connectionKeyValue; + + napi_create_object(env, &resultObj); + + napi_create_int32(env, static_cast(hr), &hrValue); + napi_set_named_property(env, resultObj, "hr", hrValue); + + std::wstringstream ss; + ss << connectionKey.Internal; + + std::wstring connectionKeyString = ss.str(); + napi_create_string_utf16(env, reinterpret_cast(connectionKeyString.c_str()), connectionKeyString.length(), &connectionKeyValue); + + napi_set_named_property(env, resultObj, "connectionKey", connectionKeyValue); + + return resultObj; +}