|
1 | | -#include <nan.h> |
| 1 | +#include <napi.h> |
2 | 2 |
|
3 | 3 | extern "C" { |
4 | 4 | #include "../yespower-c/yespower.h" |
5 | 5 | } |
6 | 6 |
|
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(); |
10 | 14 | } |
11 | 15 |
|
12 | 16 | // 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(); |
15 | 19 |
|
16 | 20 | // Optional N (default: 2048) |
17 | 21 | 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(); |
21 | 24 | } |
22 | 25 |
|
23 | 26 | // Optional r (default: 32) |
24 | 27 | 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(); |
28 | 30 | } |
29 | 31 |
|
30 | 32 | // Optional pers (default: null) |
31 | 33 | char* pers = nullptr; |
32 | 34 | 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(); |
37 | 38 | } |
38 | 39 |
|
39 | 40 | char output[32]; |
40 | 41 |
|
41 | 42 | yespower_hash(input, input_len, N, r, pers, pers_len, output); |
42 | 43 |
|
43 | | - info.GetReturnValue().Set(Nan::CopyBuffer(output, 32).ToLocalChecked()); |
| 44 | + return Napi::Buffer<char>::Copy(env, output, 32); |
44 | 45 | } |
45 | 46 |
|
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; |
48 | 50 | } |
49 | 51 |
|
50 | | -NODE_MODULE(yespower, init) |
| 52 | +NODE_API_MODULE(yespower, Init) |
0 commit comments