Skip to content

Commit

Permalink
Update nan api calls for node 12+ support (#246)
Browse files Browse the repository at this point in the history
* Update nan api calls for node 12+ support

* Fix pipeline

* Bump up nan dependency

* Bump up node-pre-gyp dependency

* Fix pipeline publishing

* Fix pipeline publishing

* Fix array conversion
  • Loading branch information
bencefr authored Apr 27, 2020
1 parent e709b39 commit 391a9d8
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 58 deletions.
46 changes: 39 additions & 7 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,58 @@ jobs:
npmConfigTarget: 11.14.0
- template: azure-node.yml
parameters:
nodeVersion: 10.15.0
nodeVersion: 12.16.1
nodeArch: $(nodeArch)
npmConfigRuntime: 'node'
npmConfigTarget: 12.16.1
- template: azure-node.yml
parameters:
nodeVersion: 13.12.0
nodeArch: $(nodeArch)
npmConfigRuntime: 'node'
npmConfigTarget: 13.12.0
- template: azure-node.yml
parameters:
nodeVersion: 12.16.1
nodeArch: $(nodeArch)
npmConfigRuntime: 'electron'
npmConfigTarget: 2.0.17
- template: azure-node.yml
parameters:
nodeVersion: 10.15.0
nodeVersion: 12.16.1
nodeArch: $(nodeArch)
npmConfigRuntime: 'electron'
npmConfigTarget: 4.1.4
- template: azure-node.yml
parameters:
nodeVersion: 10.15.0
nodeVersion: 12.16.1
nodeArch: $(nodeArch)
npmConfigRuntime: 'electron'
npmConfigTarget: 5.0.6
- template: azure-node.yml
parameters:
nodeVersion: 12.16.1
nodeArch: $(nodeArch)
npmConfigRuntime: 'electron'
npmConfigTarget: 8.2.0

# Relese draft of artefacts in $(Build.ArtifactStagingDirectory).
- task: PublishPipelineArtifact@1
inputs:
path: $(Build.ArtifactStagingDirectory)
displayName: 'Publish artifacts'

- job: GitHubRelease
dependsOn: Build
steps:
- checkout: none
- task: DownloadPipelineArtifact@2
inputs:
patterns: '**/*.tar.gz'
path: $(Build.ArtifactStagingDirectory)
displayName: 'Download artifacts'
- bash: |
mv $(Build.ArtifactStagingDirectory)/*/* $(Build.ArtifactStagingDirectory)
displayName: 'Move files'
- task: GitHubRelease@0
inputs:
gitHubConnection: 'waylandCI'
Expand All @@ -122,9 +156,7 @@ jobs:
condition: ne(variables['Build.Reason'], 'PullRequest')

- job: Test
dependsOn: [
Build,
]
dependsOn: GitHubRelease
strategy:
matrix:
linux:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"cmake-js": "5.1.1",
"crc": "^3.8.0",
"jszip": "^3.2.2",
"nan": "^2.12.1",
"node-pre-gyp": "^0.13.0",
"nan": "^2.14.0",
"node-pre-gyp": "^0.14.0",
"underscore": "^1.9.1"
},
"devDependencies": {
Expand Down
27 changes: 19 additions & 8 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ uint8_t *ConversionUtility::getNativePointerToUint8(v8::Local<v8::Value> js)

for (uint32_t i = 0; i < length; ++i)
{
string[i] = static_cast<uint8_t>(jsarray->Get(Nan::New(i))->Uint32Value());
string[i] = static_cast<uint8_t>(
Nan::Get(jsarray, i).ToLocalChecked()->Uint32Value(Nan::GetCurrentContext()).FromJust());
}

return string;
Expand All @@ -349,7 +350,8 @@ uint16_t *ConversionUtility::getNativePointerToUint16(v8::Local<v8::Value>js)

for (uint32_t i = 0; i < length; ++i)
{
string[i] = static_cast<uint16_t>(jsarray->Get(Nan::New(i))->Uint32Value());
string[i] = static_cast<uint16_t>(
Nan::Get(jsarray, i).ToLocalChecked()->Uint32Value(Nan::GetCurrentContext()).FromJust());
}

return string;
Expand All @@ -362,7 +364,7 @@ v8::Local<v8::Object> ConversionUtility::getJsObject(v8::Local<v8::Value>js)
throw std::string("object");
}

return js->ToObject();
return Nan::To<v8::Object>(js).ToLocalChecked();
}

v8::Local<v8::Object> ConversionUtility::getJsObject(v8::Local<v8::Object> js, const char *name)
Expand Down Expand Up @@ -504,7 +506,7 @@ v8::Handle<v8::Value> ConversionUtility::toJsValueArray(uint8_t *nativeData, uin

for (int i = 0; i < length; ++i)
{
valueArray->Set(i, ConversionUtility::toJsNumber(nativeData[i]));
Nan::Set(valueArray, i, ConversionUtility::toJsNumber(nativeData[i]));
}

return scope.Escape(valueArray);
Expand Down Expand Up @@ -616,7 +618,7 @@ std::vector<uint8_t> ConversionUtility::extractHex(v8::Local<v8::Value> js)
auto length = jsString->Length();
auto cString = std::vector<char>(length + 1);

jsString->WriteUtf8(cString.data(), length);
Utility::WriteUtf8(jsString, cString.data(), length);

auto size = (length / 2);

Expand Down Expand Up @@ -787,6 +789,15 @@ bool Utility::EnsureAsciiNumbers(uint8_t *value, const int length)
return true;
}

int Utility::WriteUtf8(v8::Local<v8::String> &v8Str, char *buffer, int length)
{
#if NODE_MAJOR_VERSION >= 11
return v8Str->WriteUtf8(v8::Isolate::GetCurrent(), buffer, length);
#else
return v8Str->WriteUtf8(buffer, length);
#endif
}

v8::Local<v8::Value> ErrorMessage::getErrorMessage(const int errorCode, const std::string customMessage)
{
Nan::EscapableHandleScope scope;
Expand Down Expand Up @@ -821,7 +832,7 @@ v8::Local<v8::Value> ErrorMessage::getErrorMessage(const int errorCode, const st
errorStringStream << "Error occured when " << customMessage << ". "
<< "Errorcode: " << ConversionUtility::valueToString(errorCode, error_message_name_map) << " (0x" << std::hex << errorCode << ")" << std::endl;

v8::Local<v8::Value> error = Nan::Error(ConversionUtility::toJsString(errorStringStream.str())->ToString());
v8::Local<v8::Value> error = Nan::Error(errorStringStream.str().c_str());
v8::Local<v8::Object> errorObject = error.As<v8::Object>();

Utility::Set(errorObject, "errno", errorCode);
Expand Down Expand Up @@ -883,7 +894,7 @@ v8::Local<v8::String> ErrorMessage::getTypeErrorMessage(const int argumentNumber

stream << " argument must be a " << message;

return ConversionUtility::toJsString(stream.str())->ToString();
return Nan::To<v8::String>(ConversionUtility::toJsString(stream.str())).ToLocalChecked();
}

v8::Local<v8::String> ErrorMessage::getStructErrorMessage(const std::string name, const std::string message)
Expand All @@ -892,7 +903,7 @@ v8::Local<v8::String> ErrorMessage::getStructErrorMessage(const std::string name

stream << "Property: " << name << " Message: " << message;

return ConversionUtility::toJsString(stream.str())->ToString();
return Nan::To<v8::String>(ConversionUtility::toJsString(stream.str())).ToLocalChecked();
}

v8::Local<v8::Value> HciStatus::getHciStatus(int statusCode)
Expand Down
18 changes: 10 additions & 8 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class Utility

static bool IsBetween(const uint8_t value, const uint8_t min, const uint8_t max);
static bool EnsureAsciiNumbers(uint8_t *value, const int length);

static int WriteUtf8(v8::Local<v8::String>& v8Str, char *buffer, int length = -1);
};

template<typename EventType>
Expand Down Expand Up @@ -217,7 +219,7 @@ class ConvUtil
throw std::string("number");
}

return static_cast<NativeType>(js->Uint32Value());
return Nan::To<uint32_t>(js).FromJust();
}

static NativeType getNativeSigned(v8::Local<v8::Value> js)
Expand All @@ -227,7 +229,7 @@ class ConvUtil
throw std::string("number");
}

return static_cast<NativeType>(js->Int32Value());
return Nan::To<int32_t>(js).FromJust();
}

static NativeType getNativeFloat(v8::Local<v8::Value> js)
Expand All @@ -237,7 +239,7 @@ class ConvUtil
throw std::string("number");
}

return static_cast<NativeType>(js->NumberValue());
return Nan::To<double>(js).FromJust();
}

static NativeType getNativeBool(v8::Local<v8::Value> js)
Expand All @@ -247,27 +249,27 @@ class ConvUtil
throw std::string("bool");
}

return static_cast<NativeType>(js->ToBoolean()->BooleanValue());
return Nan::To<bool>(js).FromJust();
}

static NativeType getNativeUnsigned(v8::Local<v8::Object> js, const char *name)
{
return getNativeUnsigned(js->Get(Nan::New(name).ToLocalChecked()));
return getNativeUnsigned(Nan::Get(js, Nan::New(name).ToLocalChecked()).ToLocalChecked());
}

static NativeType getNativeSigned(v8::Local<v8::Object> js, const char *name)
{
return getNativeSigned(js->Get(Nan::New(name).ToLocalChecked()));
return getNativeSigned(Nan::Get(js, Nan::New(name).ToLocalChecked()).ToLocalChecked());
}

static NativeType getNativeFloat(v8::Local<v8::Object> js, const char *name)
{
return getNativeFloat(js->Get(Nan::New(name).ToLocalChecked()));
return getNativeFloat(Nan::Get(js, Nan::New(name).ToLocalChecked()).ToLocalChecked());
}

static NativeType getNativeBool(v8::Local<v8::Object> js, const char *name)
{
return getNativeBool(js->Get(Nan::New(name).ToLocalChecked()));
return getNativeBool(Nan::Get(js, Nan::New(name).ToLocalChecked()).ToLocalChecked());
}
};

Expand Down
38 changes: 19 additions & 19 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void Adapter::onRpcEvent(uv_async_t *handle)
{
auto keyset = getSecurityKey(event->evt.gap_evt.conn_handle);

v8::Local<v8::Object> obj = Utility::Get(array, arrayIndex)->ToObject();
v8::Local<v8::Object> obj = Nan::To<v8::Object>(Utility::Get(array, arrayIndex)).ToLocalChecked();

if (keyset != 0)
{
Expand Down Expand Up @@ -669,10 +669,10 @@ NAN_METHOD(Adapter::Open)
try
{
baton->baud_rate = ConversionUtility::getNativeUint32(options, "baudRate"); parameter++;
baton->parity = ToParityEnum(Utility::Get(options, "parity")->ToString()); parameter++;
baton->flow_control = ToFlowControlEnum(Utility::Get(options, "flowControl")->ToString()); parameter++;
baton->parity = ToParityEnum(ConversionUtility::getNativeString(options, "parity")); parameter++;
baton->flow_control = ToFlowControlEnum(ConversionUtility::getNativeString(options, "flowControl")); parameter++;
baton->evt_interval = ConversionUtility::getNativeUint32(options, "eventInterval"); parameter++;
baton->log_level = ToLogSeverityEnum(Utility::Get(options, "logLevel")->ToString()); parameter++;
baton->log_level = ToLogSeverityEnum(ConversionUtility::getNativeString(options, "logLevel")); parameter++;
baton->retransmission_interval = ConversionUtility::getNativeUint32(options, "retransmissionInterval"); parameter++;
baton->response_timeout = ConversionUtility::getNativeUint32(options, "responseTimeout"); parameter++;
baton->enable_ble = ConversionUtility::getBool(options, "enableBLE"); parameter++;
Expand Down Expand Up @@ -1011,59 +1011,59 @@ void Adapter::AfterAddVendorSpecificUUID(uv_work_t *req)
delete baton;
}

NAN_INLINE sd_rpc_parity_t ToParityEnum(const v8::Handle<v8::String>& v8str)
NAN_INLINE sd_rpc_parity_t ToParityEnum(const std::string& str)
{
sd_rpc_parity_t parity = SD_RPC_PARITY_NONE;

if (v8str->Equals(Nan::New("none").ToLocalChecked()))
if (str == "none")
{
parity = SD_RPC_PARITY_NONE;
}
else if (v8str->Equals(Nan::New("even").ToLocalChecked()))
else if (str == "even")
{
parity = SD_RPC_PARITY_EVEN;
}

return parity;
}

NAN_INLINE sd_rpc_flow_control_t ToFlowControlEnum(const v8::Handle<v8::String>& v8str)
NAN_INLINE sd_rpc_flow_control_t ToFlowControlEnum(const std::string &str)
{
sd_rpc_flow_control_t flow_control = SD_RPC_FLOW_CONTROL_NONE;

if (v8str->Equals(Nan::New("none").ToLocalChecked()))
if (str == "none")
{
flow_control = SD_RPC_FLOW_CONTROL_NONE;
}
else if (v8str->Equals(Nan::New("hw").ToLocalChecked()))
else if (str == "hw")
{
flow_control = SD_RPC_FLOW_CONTROL_HARDWARE;
}

return flow_control;
}

NAN_INLINE sd_rpc_log_severity_t ToLogSeverityEnum(const v8::Handle<v8::String>& v8str)
NAN_INLINE sd_rpc_log_severity_t ToLogSeverityEnum(const std::string &str)
{
sd_rpc_log_severity_t log_severity = SD_RPC_LOG_DEBUG;

if (v8str->Equals(Nan::New("trace").ToLocalChecked()))
if (str == "trace")
{
log_severity = SD_RPC_LOG_TRACE;
}
else if (v8str->Equals(Nan::New("debug").ToLocalChecked()))
else if (str == "debug")
{
log_severity = SD_RPC_LOG_DEBUG;
}
else if (v8str->Equals(Nan::New("info").ToLocalChecked()))
else if (str == "info")
{
log_severity = SD_RPC_LOG_INFO;
}
else if (v8str->Equals(Nan::New("error").ToLocalChecked()))
else if (str == "error")
{
log_severity = SD_RPC_LOG_ERROR;
}
else if (v8str->Equals(Nan::New("fatal").ToLocalChecked()))
else if (str == "fatal")
{
log_severity = SD_RPC_LOG_FATAL;
}
Expand Down Expand Up @@ -1218,7 +1218,7 @@ NAN_METHOD(Adapter::DecodeUUID)
le_len = ConversionUtility::getNativeUint8(info[argumentcount]);
argumentcount++;

uuid_le = info[argumentcount]->ToString();
uuid_le = Nan::To<v8::String>(info[argumentcount]).ToLocalChecked();
argumentcount++;

callback = ConversionUtility::getCallbackFunction(info[argumentcount]);
Expand Down Expand Up @@ -2043,7 +2043,7 @@ ble_uuid128_t *BleUUID128::ToNative()
uint32_t ptr[16];

v8::Local<v8::Value> uuidObject = Utility::Get(jsobj, "uuid128");
v8::Local<v8::String> uuidString = uuidObject->ToString();
v8::Local<v8::String> uuidString = Nan::To<v8::String>(uuidObject).ToLocalChecked();
size_t uuid_len = uuidString->Length() + 1;
auto uuidPtr = static_cast<char*>(malloc(uuid_len));

Expand All @@ -2053,7 +2053,7 @@ ble_uuid128_t *BleUUID128::ToNative()
std::terminate();
}

uuidString->WriteUtf8(uuidPtr, (int) uuid_len);
Utility::WriteUtf8(uuidString, uuidPtr, (int)uuid_len);

auto scan_count = sscanf(uuidPtr,
"%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
Expand Down
6 changes: 3 additions & 3 deletions src/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static name_map_t common_event_name_map = {
NAME_MAP_ENTRY(BLE_EVT_USER_MEM_RELEASE),
};

NAN_INLINE sd_rpc_parity_t ToParityEnum(const v8::Handle<v8::String>& str);
NAN_INLINE sd_rpc_flow_control_t ToFlowControlEnum(const v8::Handle<v8::String>& str);
NAN_INLINE sd_rpc_log_severity_t ToLogSeverityEnum(const v8::Handle<v8::String>& str);
NAN_INLINE sd_rpc_parity_t ToParityEnum(const std::string& str);
NAN_INLINE sd_rpc_flow_control_t ToFlowControlEnum(const std::string &str);
NAN_INLINE sd_rpc_log_severity_t ToLogSeverityEnum(const std::string &str);

#pragma region Struct conversions

Expand Down
Loading

0 comments on commit 391a9d8

Please sign in to comment.