1- #include < nan .h>
1+ #include < napi .h>
22
33extern " 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 ]. IsString ()) {
36+ std::string persStr = info[3 ]. As <Napi::String>(). Utf8Value ();
37+ pers = (char *)persStr. c_str ( );
38+ pers_len = ( uint32_t )persStr. length ( );
3739 }
3840
3941 char output[32 ];
4042
4143 yespower_hash (input, input_len, N, r, pers, pers_len, output);
4244
43- info. GetReturnValue (). Set ( Nan::CopyBuffer ( output, 32 ). ToLocalChecked () );
45+ return Napi::Buffer< char >:: Copy (env, output, 32 );
4446}
4547
46- NAN_MODULE_INIT (init) {
47- Nan::Export (target, " yespower" , yespower);
48+ Napi::Object Init (Napi::Env env, Napi::Object exports) {
49+ exports.Set (" yespower" , Napi::Function::New (env, YespowerFunc));
50+ return exports;
4851}
4952
50- NODE_MODULE (yespower, init )
53+ NODE_API_MODULE (yespower, Init )
0 commit comments