Skip to content

Commit 3a19d60

Browse files
committed
Use NAPI instead
1 parent 83eae3b commit 3a19d60

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
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?: Buffer): 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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const cases = [
4040
output: '1f0269acf565c49adc0ef9b8f26ab3808cdc38394a254fddeedcc3aacff6ad9d',
4141
N: 1024,
4242
r: 32,
43-
pers: 'personality test',
43+
pers: Buffer.from('personality test'),
4444
},
4545
{
4646
input: 'eebb7bf9a8c813b5e0a03ce627bd1a0c836e0a89793743666dc82b83e28e8f00',
@@ -165,27 +165,27 @@ const cases = [
165165
{
166166
input: '2538dad623dab29a3d5387804ab51dea411014fad9c47fb94b2d83e44358064b',
167167
output: '62c4ac19375787857e2e7c41282a58fb68638a25ba27402239edc8d2683b5126',
168-
pers: 'pers',
168+
pers: Buffer.from('pers'),
169169
},
170170
{
171171
input: '28d5ce4e064ddc5ecf7a62a65d245facb3b46d6d2d70cdcf5c413e1c4e205157',
172172
output: '9f82d3a2d796ac89aa012c71ccad798f5e10cf515dd6fb0fedceb44271029d43',
173-
pers: 'pers',
173+
pers: Buffer.from('pers'),
174174
},
175175
{
176176
input: '172aaa13d4b7d606134bc6989a84c403bac3b6c4f9f4504dd79b2618c11c63d3',
177177
output: '5bd719de97d5dbc0012bd576f778994ae9d3eeb31e12cfadeb58133a34c2ebef',
178-
pers: 'pers',
178+
pers: Buffer.from('pers'),
179179
},
180180
{
181181
input: 'c9d4866805b9e9788cf1c7f6a369e8932e5bc6e32a5c7a06c3ecb7e0ec8ebaff',
182182
output: 'ac22b64773effd8d0dbb088bd71b27449ec5c8c21ecc0262f615e920ea2df2a6',
183-
pers: 'pers',
183+
pers: Buffer.from('pers'),
184184
},
185185
{
186186
input: 'fae4f16aed075d1e94b6d913242840ad3933bc675aa72f2f647f1df60c431963',
187187
output: 'a625f58d4585b8bad21509f34f715f6a15a5e2741a89642e937587b5c4b68e8a',
188-
pers: 'pers',
188+
pers: Buffer.from('pers'),
189189
},
190190
];
191191

@@ -197,4 +197,4 @@ describe('Yespower', () => {
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: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
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;
33-
34-
if (info.Length() > 3 && info[3]->IsString()) {
35-
pers = (char*)*Nan::Utf8String(info[3]);
36-
pers_len = strlen(pers);
35+
if (info.Length() > 3 && info[3].IsBuffer()) {
36+
pers = reinterpret_cast<char*>(info[3].As<Napi::Buffer<char>>().Data());
37+
pers_len = info[3].As<Napi::Buffer<char>>().Length();
3738
}
3839

3940
char output[32];
4041

4142
yespower_hash(input, input_len, N, r, pers, pers_len, output);
4243

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

46-
NAN_MODULE_INIT(init) {
47-
Nan::Export(target, "yespower", yespower);
47+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
48+
exports.Set("yespower", Napi::Function::New(env, YespowerFunc));
49+
return exports;
4850
}
4951

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

0 commit comments

Comments
 (0)