Skip to content

Commit 01aa1c1

Browse files
committed
Use Nan for extcrypto compatibility across node versions
1 parent 41bd902 commit 01aa1c1

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: "node_js"
22
node_js:
3+
- "13"
4+
- "12"
35
- "11"
46
- "10"
57
- "8"

binding.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"targets": [
33
{
44
"target_name": "extcrypto_addon",
5-
"sources": [ "./extcrypto/extcrypto.cc" ]
5+
"sources": [ "./extcrypto/extcrypto.cc" ],
6+
"include_dirs": [ "<!(node -e \"require('nan')\")" ]
67
}
78
]
89
}

extcrypto/extcrypto.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// extcrypto.cc - some openssl wrappers to extend RSA support
22
#include <node.h>
3+
#include <nan.h>
34
#include <stdint.h>
45
#include <string.h>
56
#include <openssl/rsa.h>
@@ -24,15 +25,15 @@ namespace extcrypto {
2425
const unsigned argc = 2;
2526
Local<Value> argv[argc] = { Null(isolate), rval };
2627

27-
cb->Call(Null(isolate), argc, argv);
28+
Nan::Call(Nan::Callback(cb), argc, argv);
2829
}
2930

3031

3132
void async_eret(Isolate* isolate, Local<Function> cb, Local<String> rval) {
3233
const uint64_t argc = 1;
3334
Local<Value> argv[argc] = { Exception::Error(rval) };
3435

35-
cb->Call(Null(isolate), argc, argv);
36+
Nan::Call(Nan::Callback(cb), argc, argv);
3637
}
3738

3839

@@ -53,7 +54,7 @@ namespace extcrypto {
5354
int64_t kg = RSA_generate_key_ex(rsa, 2048, exp, NULL);
5455

5556
if (!kg) {
56-
rval = String::NewFromUtf8(isolate, "Unable to generate key");
57+
rval = Nan::New("Unable to generate key").ToLocalChecked();
5758

5859
if (!async) {
5960
args.GetReturnValue().Set(rval);
@@ -71,7 +72,7 @@ namespace extcrypto {
7172

7273
if (!key || (kl == UINT64_MAX)) {
7374
free(key); // in case compiler dependent behavior for calloc after overflow returns a non-null pointer
74-
rval = String::NewFromUtf8(isolate, "Unable to generate key");
75+
rval = Nan::New("Unable to generate key").ToLocalChecked();
7576

7677
if (!async) {
7778
args.GetReturnValue().Set(rval);
@@ -89,7 +90,7 @@ namespace extcrypto {
8990

9091
// return
9192

92-
rval = String::NewFromUtf8(isolate, key);
93+
rval = Nan::New(key).ToLocalChecked();
9394
free(key);
9495

9596
if (!async) {
@@ -112,7 +113,7 @@ namespace extcrypto {
112113
bool async = args.Length() > 1;
113114
if (async) { cb = Local<Function>::Cast(args[1]); }
114115

115-
String::Utf8Value ipem(pem);
116+
Nan::Utf8String ipem(pem);
116117
char* skey(*ipem);
117118

118119
BIO* bio = BIO_new(BIO_s_mem());
@@ -127,7 +128,7 @@ namespace extcrypto {
127128

128129
if (!pkey || (kl == UINT64_MAX)) {
129130
free(pkey); // in case compiler dependent behavior for calloc after overflow returns a non-null pointer
130-
rval = String::NewFromUtf8(isolate, "Unable to extract key");
131+
rval = Nan::New("Unable to extract key").ToLocalChecked();
131132

132133
if (!async) {
133134
args.GetReturnValue().Set(rval);
@@ -144,7 +145,7 @@ namespace extcrypto {
144145

145146
// return
146147

147-
rval = String::NewFromUtf8(isolate, pkey);
148+
rval = Nan::New(pkey).ToLocalChecked();
148149
free(pkey);
149150

150151
if (!async) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"author": "Samuel Judson",
88
"license": "MIT",
99
"dependencies": {
10-
"libsodium-wrappers": "0.7.6"
10+
"libsodium-wrappers": "0.7.6",
11+
"nan": "^2.14.0"
1112
},
1213
"devDependencies": {
1314
"@types/node": "^11.11.3",

0 commit comments

Comments
 (0)