Skip to content

Commit

Permalink
run migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Aug 29, 2024
1 parent 138e806 commit d922476
Show file tree
Hide file tree
Showing 18 changed files with 986 additions and 921 deletions.
31 changes: 29 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
'targets': [
{
'target_name': 'sslctx',
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'type': 'static_library',
'sources': [
'src/sslctx.c',
Expand All @@ -29,6 +38,15 @@
},
{
'target_name': '<(module_name)',
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'type': 'loadable_module',
'sources': [
'src/node_libcurl.cc',
Expand All @@ -41,7 +59,6 @@
'src/Http2PushFrameHeaders.cc',
],
'include_dirs' : [
"<!(node -e \"require('nan')\")",
],
'dependencies': [
':sslctx',
Expand All @@ -53,7 +70,8 @@
]
}],
['curl_include_dirs!=""', {
'include_dirs': ['<@(curl_include_dirs)']
'include_dirs': [
'<!(node -p "require(\'node-addon-api\').include_dir")','<@(curl_include_dirs)']
}],
['curl_libraries!=""', {
'libraries': ['<@(curl_libraries)']
Expand Down Expand Up @@ -250,6 +268,15 @@
},
{
'target_name': 'action_after_build',
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'type': 'none',
'dependencies': [ '<(module_name)' ],
'copies': [
Expand Down
1 change: 1 addition & 0 deletions examples/19-binary-data-protobuf/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"node-addon-api": "8.1.0",
"koa": "^2.12.1",
"@koa/router": "^9.0.1",
"stoppable": "1.1.0",
Expand Down
1 change: 1 addition & 0 deletions examples/21-websockets-server/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"node-addon-api": "8.1.0",
"ws": "7.3.1"
}
}
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"node-addon-api": "8.1.0",
"node-libcurl": "^2.2.0",
"protobufjs": "6.9.0"
}
Expand Down
94 changes: 47 additions & 47 deletions src/Curl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -712,32 +712,32 @@ const std::vector<CurlConstant> curlOptionBlob = {
#endif
};

static void ExportConstants(v8::Local<v8::Object> obj,
static void ExportConstants(Napi::Object obj,
const std::vector<NodeLibcurl::CurlConstant>& optionGroup,
v8::PropertyAttribute attributes) {
Nan::HandleScope scope;
Napi::HandleScope scope(env);

for (std::vector<NodeLibcurl::CurlConstant>::const_iterator it = optionGroup.begin(),
end = optionGroup.end();
it != end; ++it) {
Nan::DefineOwnProperty(obj, Nan::New<v8::String>(it->name).ToLocalChecked(),
Nan::New<v8::Integer>(static_cast<int32_t>(it->value)), attributes);
Napi::DefineOwnProperty(obj, Napi::String::New(env, it->name),
Napi::Number::New(env, static_cast<int32_t>(it->value)), attributes);
}
}

// Add Curl constructor to the module exports
NAN_MODULE_INIT(Initialize) {
Nan::HandleScope scope;
Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

v8::Local<v8::Object> obj = Nan::New<v8::Object>();
Napi::Object obj = Napi::Object::New(env);

v8::PropertyAttribute attributes =
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
v8::PropertyAttribute attributesDontEnum =
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete | v8::DontEnum);

// export options
v8::Local<v8::Object> optionsObj = Nan::New<v8::Object>();
Napi::Object optionsObj = Napi::Object::New(env);
ExportConstants(optionsObj, curlOptionNotImplemented, attributesDontEnum);
ExportConstants(optionsObj, curlOptionString, attributes);
ExportConstants(optionsObj, curlOptionInteger, attributes);
Expand All @@ -747,7 +747,7 @@ NAN_MODULE_INIT(Initialize) {
ExportConstants(optionsObj, curlOptionBlob, attributes);

// export infos
v8::Local<v8::Object> infosObj = Nan::New<v8::Object>();
Napi::Object infosObj = Napi::Object::New(env);
ExportConstants(infosObj, curlInfoNotImplemented, attributesDontEnum);
ExportConstants(infosObj, curlInfoString, attributes);
ExportConstants(infosObj, curlInfoOffT, attributes);
Expand All @@ -757,34 +757,34 @@ NAN_MODULE_INIT(Initialize) {
ExportConstants(infosObj, curlInfoLinkedList, attributes);

// export Curl codes
v8::Local<v8::Object> multiObj = Nan::New<v8::Object>();
Napi::Object multiObj = Napi::Object::New(env);
ExportConstants(multiObj, curlMultiOptionNotImplemented, attributesDontEnum);
ExportConstants(multiObj, curlMultiOptionInteger, attributes);
ExportConstants(multiObj, curlMultiOptionStringArray, attributes);
ExportConstants(multiObj, curlMultiOptionFunction, attributes);

// static members
Nan::DefineOwnProperty(obj, Nan::New<v8::String>("option").ToLocalChecked(), optionsObj,
Napi::DefineOwnProperty(obj, Napi::String::New(env, "option"), optionsObj,
attributes);
Nan::DefineOwnProperty(obj, Nan::New<v8::String>("info").ToLocalChecked(), infosObj, attributes);
Nan::DefineOwnProperty(obj, Nan::New<v8::String>("multi").ToLocalChecked(), multiObj, attributes);
Napi::DefineOwnProperty(obj, Napi::String::New(env, "info"), infosObj, attributes);
Napi::DefineOwnProperty(obj, Napi::String::New(env, "multi"), multiObj, attributes);

Nan::SetMethod(obj, "globalInit", GlobalInit);
Nan::SetMethod(obj, "globalCleanup", GlobalCleanup);
Nan::SetMethod(obj, "getVersion", GetVersion);
Nan::SetMethod(obj, "getCount", GetCount);
Nan::SetAccessor(obj, Nan::New("VERSION_NUM").ToLocalChecked(), GetterVersionNum, 0,
v8::Local<v8::Value>(), v8::DEFAULT, attributes);
Napi::SetMethod(obj, "globalInit", GlobalInit);
Napi::SetMethod(obj, "globalCleanup", GlobalCleanup);
Napi::SetMethod(obj, "getVersion", GetVersion);
Napi::SetMethod(obj, "getCount", GetCount);
Napi::SetAccessor(obj, Napi::String::New(env, "VERSION_NUM"), GetterVersionNum, 0,
Napi::Value(), v8::DEFAULT, attributes);

Nan::Set(target, Nan::New("Curl").ToLocalChecked(), obj);
(target).Set(Napi::String::New(env, "Curl"), obj);
}

int32_t IsInsideCurlConstantStruct(const std::vector<CurlConstant>& curlConstants,
const v8::Local<v8::Value>& searchFor) {
Nan::HandleScope scope;
const Napi::Value& searchFor) {
Napi::HandleScope scope(env);

bool isString = searchFor->IsString();
bool isInt = searchFor->IsInt32();
bool isString = searchFor.IsString();
bool isInt = searchFor.IsNumber();

std::string optionName = "";
int32_t optionId = -1;
Expand All @@ -794,14 +794,14 @@ int32_t IsInsideCurlConstantStruct(const std::vector<CurlConstant>& curlConstant
}

if (isString) {
Nan::Utf8String optionNameV8(searchFor);
std::string optionNameV8 = searchFor.As<Napi::String>();

optionName = std::string(*optionNameV8);

std::transform(optionName.begin(), optionName.end(), optionName.begin(), ::toupper);

} else { // int
optionId = Nan::To<int32_t>(searchFor).FromJust();
optionId = searchFor.As<Napi::Number>().Int32Value();
}

for (std::vector<CurlConstant>::const_iterator it = curlConstants.begin(),
Expand All @@ -817,7 +817,7 @@ int32_t IsInsideCurlConstantStruct(const std::vector<CurlConstant>& curlConstant

// based on https://github.com/libxmljs/libxmljs/blob/master/src/libxmljs.cc#L45
void AdjustMemory(ssize_t diff) {
Nan::HandleScope scope;
Napi::HandleScope scope(env);

addonAllocatedMemory += diff;

Expand All @@ -828,25 +828,25 @@ void AdjustMemory(ssize_t diff) {
return;
}

Nan::AdjustExternalMemory(static_cast<int>(diff));
Napi::AdjustExternalMemory(static_cast<int>(diff));
}

// Return human readable string with the version number of libcurl and some of its important
// components (like OpenSSL version).
NAN_METHOD(GetVersion) {
Nan::HandleScope scope;
Napi::Value GetVersion(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);

const char* version = curl_version();

v8::Local<v8::Value> versionObj = Nan::New<v8::String>(version).ToLocalChecked();
Napi::Value versionObj = Napi::String::New(env, version);

info.GetReturnValue().Set(versionObj);
return versionObj;
}

NAN_METHOD(GetCount) {
Nan::HandleScope scope;
Napi::Value GetCount(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);

info.GetReturnValue().Set(Easy::currentOpenedHandles);
return Easy::currentOpenedHandles;
}

// The following memory allocation wrappers are mostly the ones at
Expand Down Expand Up @@ -910,11 +910,11 @@ void* CallocCallback(size_t nmemb, size_t size) {
return ptr;
}

NAN_METHOD(GlobalInit) {
Nan::HandleScope scope;
Napi::Value GlobalInit(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);

long flags = info[0]->IsUndefined() // NOLINT(runtime/int)
? static_cast<long>(Nan::To<int32_t>(info[0]).FromJust()) // NOLINT(runtime/int)
long flags = info[0].IsUndefined() // NOLINT(runtime/int)
? static_cast<long>(info[0].As<Napi::Number>().Int32Value()) // NOLINT(runtime/int)
: CURL_GLOBAL_ALL;

curl_version_info_data* version = curl_version_info(CURLVERSION_NOW);
Expand All @@ -932,24 +932,24 @@ NAN_METHOD(GlobalInit) {
globalInitRetCode = curl_global_init(flags);
}

info.GetReturnValue().Set(globalInitRetCode);
return globalInitRetCode;
}

NAN_METHOD(GlobalCleanup) {
Nan::HandleScope scope;
Napi::Value GlobalCleanup(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);

curl_global_cleanup();

info.GetReturnValue().Set(Nan::Undefined());
return env.Undefined();
}

// Return hexdecimal representation of the libcurl version.
NAN_GETTER(GetterVersionNum) {
Nan::HandleScope scope;
Napi::Value GetterVersionNum(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);

v8::Local<v8::Int32> version = Nan::New(LIBCURL_VERSION_NUM);
v8::Local<v8::Int32> version = Napi::New(env, LIBCURL_VERSION_NUM);

info.GetReturnValue().Set(version);
return version;
}

} // namespace NodeLibcurl
22 changes: 12 additions & 10 deletions src/Curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
#include "macros.h"

#include <curl/curl.h>
#include <nan.h>
#include <node.h>
#include <napi.h>
#include <uv.h>
#include <napi.h>
#include <uv.h>

#include <functional>
#include <memory>
#include <vector>

using Nan::ObjectWrap;
using Napi::ObjectWrap;

namespace NodeLibcurl {

Expand Down Expand Up @@ -60,18 +62,18 @@ extern const std::vector<CurlConstant> curlMultiOptionStringArray;
extern const std::vector<CurlConstant> curlMultiOptionFunction;

// export Curl to js
NAN_MODULE_INIT(Initialize);
Napi::Object Initialize(Napi::Env env, Napi::Object exports);

// js exported Methods
NAN_METHOD(GlobalInit);
NAN_METHOD(GlobalCleanup);
NAN_METHOD(GetVersion);
NAN_METHOD(GetCount);
NAN_GETTER(GetterVersionNum);
Napi::Value GlobalInit(const Napi::CallbackInfo& info);
Napi::Value GlobalCleanup(const Napi::CallbackInfo& info);
Napi::Value GetVersion(const Napi::CallbackInfo& info);
Napi::Value GetCount(const Napi::CallbackInfo& info);
Napi::Value GetterVersionNum(const Napi::CallbackInfo& info);

// helper methods
int32_t IsInsideCurlConstantStruct(const std::vector<CurlConstant>& curlConstants,
const v8::Local<v8::Value>& searchFor);
const Napi::Value& searchFor);
void ThrowError(const char* message, const char* reason = nullptr);
void AdjustMemory(ssize_t size);

Expand Down
Loading

0 comments on commit d922476

Please sign in to comment.