|
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; |
| 35 | + if (info.Length() > 3 && info[3].IsString()) { |
| 36 | + std::string jsStr = info[3].As<Napi::String>(); |
33 | 37 |
|
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(); |
37 | 41 | } |
38 | 42 |
|
39 | 43 | char output[32]; |
40 | 44 |
|
41 | 45 | yespower_hash(input, input_len, N, r, pers, pers_len, output); |
42 | 46 |
|
43 | | - info.GetReturnValue().Set(Nan::CopyBuffer(output, 32).ToLocalChecked()); |
| 47 | + return Napi::Buffer<char>::Copy(env, output, 32); |
44 | 48 | } |
45 | 49 |
|
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; |
48 | 53 | } |
49 | 54 |
|
50 | | -NODE_MODULE(yespower, init) |
| 55 | +NODE_API_MODULE(yespower, Init) |
0 commit comments