From d4e2e31208d9cfeed2dc73be1c3b289d044e4c03 Mon Sep 17 00:00:00 2001 From: Pedro Tacla Yamada Date: Thu, 8 Feb 2024 08:45:13 +0000 Subject: [PATCH] Delete node_addon directory --- CMakeLists.txt | 2 - src/node_addon/CMakeLists.txt | 44 ----- src/node_addon/main.cpp | 291 ---------------------------------- 3 files changed, 337 deletions(-) delete mode 100644 src/node_addon/CMakeLists.txt delete mode 100644 src/node_addon/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dc44880..56a9f49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,5 +27,3 @@ add_compile_definitions(GIT_SHORT_HASH="${GIT_SHORT_HASH}") add_subdirectory (src) add_subdirectory (tests) - -add_subdirectory(./src/node_addon) diff --git a/src/node_addon/CMakeLists.txt b/src/node_addon/CMakeLists.txt deleted file mode 100644 index a9756d8..0000000 --- a/src/node_addon/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -function (add_node_addon) - set( - NODE_SOURCE_FILES - main.cpp - ) - add_library(node_filesaver SHARED ${NODE_SOURCE_FILES} ${CMAKE_JS_SRC}) - target_include_directories( - node_filesaver - PRIVATE - SYSTEM - ${CMAKE_JS_INC} - ${CMAKE_CURRENT_LIST_DIR}/../ - ) - set_target_properties(node_filesaver PROPERTIES PREFIX "" SUFFIX ".node") - target_link_libraries( - node_filesaver - lfilesaver - ${CMAKE_JS_LIB} - ) -endfunction() - -if (CMAKE_JS_VERSION) - message("-- CMAKE_JS_VERSION: ${CMAKE_JS_VERSION}") - message("-- CMAKE_JS_INC: ${CMAKE_JS_INC}") - message("-- CMAKE_JS_LIB: ${CMAKE_JS_LIB}") - message("-- CMAKE_JS_SRC: ${CMAKE_JS_SRC}") - add_node_addon() -elseif(MOCK_CMAKE_JS) - set( - CMAKE_JS_INC - ~/.cmake-js/node-x64/v14.0.0/include/node/ - ${CMAKE_CURRENT_LIST_DIR}/../../node_modules/nan - ) - message("-- Fallback include to ${CMAKE_JS_INC}") - set(NODE_RUNTIME "node") - set(NODE_RUNTIMEVERSION "14.0.0") - set(NODE_ARCH "x64") - set(WARNINGS_AS_ERRORS "OFF") - set(CMAKE_CXX_FLAGS "-D_DARWIN_USE_64_BIT_INODE=1 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DBUILDING_NODE_EXTENSION") - set(CMAKE_SHARED_LINKER_FLAGS "-undefined dynamic_lookup") - add_node_addon() -else() - message("-- Skipping node addon build") -endif() diff --git a/src/node_addon/main.cpp b/src/node_addon/main.cpp deleted file mode 100644 index b65c3fb..0000000 --- a/src/node_addon/main.cpp +++ /dev/null @@ -1,291 +0,0 @@ -// -// Created by Pedro Tacla Yamada on 18/05/20. -// - -#include -#include -#include -#include - -#include - -/// Defines Node.js bindings to FileSaver -/** - * To build this, use `cmake-js compile`. - * - * It should be able to be `npm install`ed seamlessly as well. - */ -namespace filesaver::node_addon -{ - -using filesaver::FileSaver; -using v8::Context; -using v8::Exception; -using v8::Function; -using v8::FunctionCallbackInfo; -using v8::FunctionTemplate; -using v8::Isolate; -using v8::Local; -using v8::NewStringType; -using v8::Object; -using v8::ObjectTemplate; -using v8::String; -using v8::Value; - -static FileSaver filesaver; - -/** - * The main export from this module. Wraps the `FileSaver` class and does - * type conversions. - */ -class NodeFileSaver : public node::ObjectWrap -{ -public: - static void initialize (Local exports) - { - auto* isolate = exports->GetIsolate (); - auto context = isolate->GetCurrentContext (); - - auto addonDataTpl = ObjectTemplate::New (isolate); - addonDataTpl->SetInternalFieldCount (1); - - auto addonData = addonDataTpl->NewInstance (context).ToLocalChecked (); - auto name = String::NewFromUtf8 (isolate, "FileSaver", NewStringType::kNormal).ToLocalChecked (); - auto tpl = FunctionTemplate::New (isolate, New, addonData); - tpl->SetClassName (name); - - std::vector> methods{ - {"start", Start}, - {"stop", Stop}, - {"scan", Scan}, - - {"getCurrentSizeAt", GetCurrentSizeAt}, - {"getTargets", GetTargets}, - {"getTotalFiles", GetTotalFiles}, - {"getTotalKnownFiles", GetTotalKnownFiles}, - {"getFilesPerSecond", GetFilesPerSecond}, - {"getNumWorkers", GetNumWorkers}, - {"getElapsed", GetElapsed}, - - {"isPathFinished", IsPathFinished}, - {"areAllTargetsFinished", AreAllTargetsFinished}, - - {"setNumWorkers", SetNumWorkers}, - }; - - tpl->InstanceTemplate ()->SetInternalFieldCount (static_cast (methods.size ())); - for (auto& pair : methods) - { - NODE_SET_PROTOTYPE_METHOD (tpl, pair.first.c_str (), pair.second); - } - - auto constructor = tpl->GetFunction (context).ToLocalChecked (); - addonData->SetInternalField (0, constructor); - exports->Set (context, name, constructor).FromJust (); - } - -private: - NodeFileSaver () = default; - ~NodeFileSaver () override = default; - - static void New (const v8::FunctionCallbackInfo& args) - { - Isolate* isolate = args.GetIsolate (); - Local context = isolate->GetCurrentContext (); - - if (args.IsConstructCall ()) - { - auto* obj = new NodeFileSaver (); - obj->Wrap (args.This ()); - args.GetReturnValue ().Set (args.This ()); - } - else - { - auto cons = args.Data ().As ()->GetInternalField (0).As (); - auto result = cons->NewInstance (context, 0, nullptr).ToLocalChecked (); - args.GetReturnValue ().Set (result); - } - } - - static void Start (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - nodeFileSaver->fileSaver.start (); - } - - static void Stop (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - nodeFileSaver->fileSaver.stop (); - } - - static void Scan (const v8::FunctionCallbackInfo& args) - { - auto* isolate = args.GetIsolate (); - auto context = isolate->GetCurrentContext (); - auto target = args[0]->ToString (context); - if (target.IsEmpty ()) - { - isolate->ThrowException (Exception::TypeError ( - String::NewFromUtf8 (isolate, "Missing required target argument", NewStringType::kNormal) - .ToLocalChecked ())); - return; - } - - std::string output; - output.resize (static_cast (target.ToLocalChecked ()->Length ())); - target.ToLocalChecked ()->WriteUtf8 (isolate, output.data ()); - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - nodeFileSaver->fileSaver.scan (output); - } - - static void GetCurrentSizeAt (const v8::FunctionCallbackInfo& args) - { - auto* isolate = args.GetIsolate (); - auto context = isolate->GetCurrentContext (); - auto target = args[0]->ToString (context); - if (target.IsEmpty ()) - { - isolate->ThrowException (Exception::TypeError ( - String::NewFromUtf8 (isolate, "Missing required target argument", NewStringType::kNormal) - .ToLocalChecked ())); - return; - } - - std::string output; - output.resize (static_cast (target.ToLocalChecked ()->Length ())); - target.ToLocalChecked ()->WriteUtf8 (isolate, output.data ()); - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getCurrentSizeAt (output); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void GetTargets (const v8::FunctionCallbackInfo& args) - { - auto* isolate = args.GetIsolate (); - auto context = isolate->GetCurrentContext (); - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getTargets (); - - auto resultArray = v8::Array::New (isolate, static_cast (result.size ())); - unsigned int i = 0; - for (const auto& pth : result) - { - auto value = - String::NewFromUtf8 (isolate, pth.string ().c_str (), NewStringType::kNormal).ToLocalChecked (); - auto r = resultArray->Set (context, i, value); - - if (!r.FromMaybe (false)) - { - isolate->ThrowException (Exception::Error ( - String::NewFromUtf8 (isolate, "Failed to create result array", NewStringType::kNormal) - .ToLocalChecked ())); - } - - i += 1; - } - - args.GetReturnValue ().Set (resultArray); - } - - static void GetTotalFiles (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getTotalFiles (); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void GetTotalKnownFiles (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getTotalKnownFiles (); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void GetFilesPerSecond (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getFilesPerSecond (); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void GetNumWorkers (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getNumWorkers (); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void GetElapsed (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.getElapsed (); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void IsPathFinished (const v8::FunctionCallbackInfo& args) - { - auto* isolate = args.GetIsolate (); - auto context = isolate->GetCurrentContext (); - auto target = args[0]->ToString (context); - if (target.IsEmpty ()) - { - isolate->ThrowException (Exception::TypeError ( - String::NewFromUtf8 (isolate, "Missing required target argument", NewStringType::kNormal) - .ToLocalChecked ())); - return; - } - - std::string output; - output.resize (static_cast (target.ToLocalChecked ()->Length ())); - target.ToLocalChecked ()->WriteUtf8 (isolate, output.data ()); - boost::filesystem::path outputPath{output}; - - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.isPathFinished (outputPath); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void AreAllTargetsFinished (const v8::FunctionCallbackInfo& args) - { - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - auto result = nodeFileSaver->fileSaver.areAllTargetsFinished (); - - args.GetReturnValue ().Set (static_cast (result)); - } - - static void SetNumWorkers (const v8::FunctionCallbackInfo& args) - { - auto* isolate = args.GetIsolate (); - auto context = isolate->GetCurrentContext (); - auto numWorkers = args[0]->ToUint32 (context); - if (numWorkers.IsEmpty ()) - { - isolate->ThrowException (Exception::TypeError ( - String::NewFromUtf8 (isolate, "Missing required numWorkers argument", NewStringType::kNormal) - .ToLocalChecked ())); - return; - } - - auto* nodeFileSaver = ObjectWrap::Unwrap (args.Holder ()); - nodeFileSaver->fileSaver.setNumWorkers (numWorkers.ToLocalChecked ()->Value ()); - } - - FileSaver fileSaver; -}; - -void initialize (Local exports) -{ - NodeFileSaver::initialize (exports); -} - -NODE_MODULE (NODE_GYP_MODULE_NAME, initialize) - -} // namespace filesaver::node_addon