Skip to content

Commit 7e693e2

Browse files
committed
Use NAPI instead
1 parent 83eae3b commit 7e693e2

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

node/binding.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"../yespower-c/yespower-opt.c",
1010
],
1111
"include_dirs": [
12-
"<!(node -e \"require('nan')\")"
12+
"<!@(node -p \"require('node-addon-api').include\")"
1313
],
1414
"cflags_cc": [
1515
"-std=c++17"
1616
],
17+
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
1718
}
1819
]
1920
}

node/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function yespower(input: Buffer, n?: number, r?: number, pers?: string): Buffer;
1+
export function yespower(input: Buffer, n?: number, r?: number, pers?: string): Buffer;

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
},
1919
"dependencies": {
2020
"bindings": "*",
21-
"nan": "^2.22.2"
21+
"node-addon-api": "^8.3.1"
2222
}
2323
}

node/test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ describe('Yespower', () => {
193193
it('Test Cases', () => {
194194
for (const { input, output, N, r, pers } of cases) {
195195
const hashed = yespower(Buffer.from(input, 'hex'), N, r, pers);
196-
196+
197197
assert.strictEqual(output, hashed.toString('hex'));
198198
}
199199
});
200-
});
200+
});

node/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ file-uri-to-path@1.0.0:
1414
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
1515
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
1616

17-
nan@^2.22.2:
18-
version "2.22.2"
19-
resolved "https://registry.yarnpkg.com/nan/-/nan-2.22.2.tgz#6b504fd029fb8f38c0990e52ad5c26772fdacfbb"
20-
integrity sha512-DANghxFkS1plDdRsX0X9pm0Z6SJNN6gBdtXfanwoZ8hooC5gosGFSBGRYHUVPz1asKA/kMRqDRdHrluZ61SpBQ==
17+
node-addon-api@^8.3.1:
18+
version "8.3.1"
19+
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-8.3.1.tgz#53bc8a4f8dbde3de787b9828059da94ba9fd4eed"
20+
integrity sha512-lytcDEdxKjGJPTLEfW4mYMigRezMlyJY8W4wxJK8zE533Jlb8L8dRuObJFWg2P+AuOIxoCgKF+2Oq4d4Zd0OUA==

node/yespower.cc

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
1-
#include <nan.h>
1+
#include <napi.h>
22

33
extern "C" {
44
#include "../yespower-c/yespower.h"
55
}
66

7-
NAN_METHOD(yespower) {
8-
if (info.Length() < 1 || !node::Buffer::HasInstance(info[0])) {
9-
return Nan::ThrowTypeError("First argument must be a Buffer");
7+
Napi::Value YespowerFunc(const Napi::CallbackInfo& info) {
8+
Napi::Env env = info.Env();
9+
10+
// Check input buffer
11+
if (info.Length() < 1 || !info[0].IsBuffer()) {
12+
Napi::TypeError::New(env, "First argument must be a Buffer").ThrowAsJavaScriptException();
13+
return env.Null();
1014
}
1115

1216
// Input buffer
13-
char* input = node::Buffer::Data(info[0]);
14-
uint32_t input_len = node::Buffer::Length(info[0]);
17+
char* input = reinterpret_cast<char*>(info[0].As<Napi::Buffer<char>>().Data());
18+
uint32_t input_len = info[0].As<Napi::Buffer<char>>().Length();
1519

1620
// Optional N (default: 2048)
1721
uint32_t N = 2048;
18-
19-
if (info.Length() > 1 && info[1]->IsUint32()) {
20-
N = Nan::To<uint32_t>(info[1]).FromJust();
22+
if (info.Length() > 1 && info[1].IsNumber()) {
23+
N = info[1].As<Napi::Number>().Uint32Value();
2124
}
2225

2326
// Optional r (default: 32)
2427
uint32_t r = 32;
25-
26-
if (info.Length() > 2 && info[2]->IsUint32()) {
27-
r = Nan::To<uint32_t>(info[2]).FromJust();
28+
if (info.Length() > 2 && info[2].IsNumber()) {
29+
r = info[2].As<Napi::Number>().Uint32Value();
2830
}
2931

3032
// Optional pers (default: null)
3133
char* pers = nullptr;
3234
uint32_t pers_len = 0;
35+
if (info.Length() > 3 && info[3].IsString()) {
36+
std::string jsStr = info[3].As<Napi::String>();
3337

34-
if (info.Length() > 3 && info[3]->IsString()) {
35-
pers = (char*)*Nan::Utf8String(info[3]);
36-
pers_len = strlen(pers);
38+
pers = (char*)std::malloc(jsStr.length() + 1);
39+
std::strcpy(pers, jsStr.c_str());
40+
pers_len = jsStr.size();
3741
}
3842

3943
char output[32];
4044

4145
yespower_hash(input, input_len, N, r, pers, pers_len, output);
4246

43-
info.GetReturnValue().Set(Nan::CopyBuffer(output, 32).ToLocalChecked());
47+
return Napi::Buffer<char>::Copy(env, output, 32);
4448
}
4549

46-
NAN_MODULE_INIT(init) {
47-
Nan::Export(target, "yespower", yespower);
50+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
51+
exports.Set("yespower", Napi::Function::New(env, YespowerFunc));
52+
return exports;
4853
}
4954

50-
NODE_MODULE(yespower, init)
55+
NODE_API_MODULE(yespower, Init)

0 commit comments

Comments
 (0)