Skip to content

Commit 383a681

Browse files
authored
Merge pull request #3 from k4connect/enhancement/node-v8-build-compatibility
Enhancement/node v8 build compatibility
2 parents 909eb81 + 53f6b19 commit 383a681

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-mbed-dtls-client",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "DTLS client created by wrapping mbedtls",
55
"main": "index.js",
66
"scripts": {
@@ -27,6 +27,6 @@
2727
"node": ">=4.0.0"
2828
},
2929
"dependencies": {
30-
"nan": "^2.1.0"
30+
"nan": "2.14.1"
3131
}
3232
}

src/DtlsSocket.cc

+31-9
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ DtlsSocket::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
3737
Nan::SetPrototypeMethod(ctor, "close", Close);
3838
Nan::SetPrototypeMethod(ctor, "send", Send);
3939
Nan::SetPrototypeMethod(ctor, "connect", Connect);
40-
41-
Nan::Set(target, Nan::New("DtlsSocket").ToLocalChecked(), ctor->GetFunction());
40+
41+
Nan::Set(
42+
target,
43+
Nan::New("DtlsSocket").ToLocalChecked(),
44+
Nan::GetFunction(ctor).ToLocalChecked()
45+
);
4246
}
4347

4448
void DtlsSocket::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
@@ -58,7 +62,10 @@ void DtlsSocket::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
5862

5963
int debug_level = 0;
6064
if (info.Length() > 7) {
61-
debug_level = info[7]->Uint32Value();
65+
v8::Maybe<uint32_t> debug_level_maybe = info[7]->Uint32Value(Nan::GetCurrentContext());
66+
if (debug_level_maybe.IsJust()) {
67+
debug_level = debug_level_maybe.FromJust();
68+
}
6269
}
6370

6471
DtlsSocket *socket = new DtlsSocket(
@@ -78,7 +85,7 @@ void DtlsSocket::ReceiveDataFromNode(const Nan::FunctionCallbackInfo<v8::Value>&
7885
socket->store_data(recv_data, Buffer::Length(info[0]));
7986

8087
int len = 1024;
81-
unsigned char buf[len];
88+
unsigned char buf[len];
8289
len = socket->receive_data(buf, len);
8390

8491
if (len > 0) {
@@ -99,7 +106,7 @@ void DtlsSocket::Close(const Nan::FunctionCallbackInfo<v8::Value>& info) {
99106

100107
void DtlsSocket::Send(const Nan::FunctionCallbackInfo<v8::Value>& info) {
101108
DtlsSocket *socket = Nan::ObjectWrap::Unwrap<DtlsSocket>(info.This());
102-
109+
103110
const unsigned char *send_data = (const unsigned char *)Buffer::Data(info[0]);
104111
socket->send(send_data, Buffer::Length(info[0]));
105112
}
@@ -225,7 +232,12 @@ int DtlsSocket::send_encrypted(const unsigned char *buf, size_t len) {
225232
Nan::CopyBuffer((char *)buf, len).ToLocalChecked()
226233
};
227234
v8::Local<v8::Function> sendCallbackDirect = send_cb->GetFunction();
228-
sendCallbackDirect->Call(Nan::GetCurrentContext()->Global(), 1, argv);
235+
Nan::Call(
236+
sendCallbackDirect,
237+
Nan::GetCurrentContext()->Global(),
238+
1,
239+
argv
240+
);
229241
return len;
230242
}
231243

@@ -257,7 +269,7 @@ int DtlsSocket::receive_data(unsigned char *buf, int len) {
257269
int ret;
258270

259271
if (ssl_context.state == MBEDTLS_SSL_HANDSHAKE_OVER) {
260-
// normal reading of unencrypted data
272+
// normal reading of unencrypted data
261273
memset(buf, 0, len);
262274
ret = mbedtls_ssl_read(&ssl_context, buf, len);
263275
if (ret <= 0) {
@@ -290,7 +302,12 @@ int DtlsSocket::step() {
290302

291303
// this should only be called once when we first finish the handshake
292304
v8::Local<v8::Function> handshakeCallbackDirect = handshake_cb->GetFunction();
293-
handshakeCallbackDirect->Call(Nan::GetCurrentContext()->Global(), 0, NULL);
305+
Nan::Call(
306+
handshakeCallbackDirect,
307+
Nan::GetCurrentContext()->Global(),
308+
0,
309+
NULL
310+
);
294311
return 0;
295312
}
296313

@@ -308,7 +325,12 @@ void DtlsSocket::error(int ret) {
308325
Nan::New(error_buf).ToLocalChecked()
309326
};
310327
v8::Local<v8::Function> errorCallbackDirect = error_cb->GetFunction();
311-
errorCallbackDirect->Call(Nan::GetCurrentContext()->Global(), 2, argv);
328+
Nan::Call(
329+
errorCallbackDirect,
330+
Nan::GetCurrentContext()->Global(),
331+
2,
332+
argv
333+
);
312334
}
313335

314336
void DtlsSocket::store_data(const unsigned char *buf, size_t len) {

0 commit comments

Comments
 (0)