diff --git a/binding.gyp b/binding.gyp index a90e779..4a3ee27 100644 --- a/binding.gyp +++ b/binding.gyp @@ -4,8 +4,20 @@ "target_name": "volume", "sources": [ "src/volume.cc" ], "include_dirs" : [ - " #include #include #include @@ -20,10 +22,11 @@ const char* OSErrDescription(OSErr err) { return "Could not get volume name"; } -NAN_METHOD(MethodGetVolumeName) { - Nan::Utf8String aPath(info[0]); +Napi::Value MethodGetVolumeName(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + std::string aPath = info[0].As(); - CFStringRef volumePath = CFStringCreateWithCString(NULL, *aPath, kCFStringEncodingUTF8); + CFStringRef volumePath = CFStringCreateWithCString(NULL, aPath.c_str(), kCFStringEncodingUTF8); CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true); OSErr err; @@ -32,16 +35,16 @@ NAN_METHOD(MethodGetVolumeName) { HFSUniStr255 outString; if (CFURLGetFSRef(url, &urlFS) == false) { - return Nan::ThrowError("Failed to convert URL to file or directory object"); + throw Napi::Error::New(env, "Failed to convert URL to file or directory object"); } if ((err = FSGetCatalogInfo(&urlFS, kFSCatInfoVolume, &urlInfo, NULL, NULL, NULL)) != noErr) { - return Nan::ThrowError(OSErrDescription(err)); + throw Napi::Error::New(env, OSErrDescription(err)); } if ((err = FSGetVolumeInfo(urlInfo.volume, 0, NULL, kFSVolInfoNone, NULL, &outString, NULL)) != noErr) { - return Nan::ThrowError(OSErrDescription(err)); + throw Napi::Error::New(env, OSErrDescription(err)); } - info.GetReturnValue().Set(Nan::New(outString.unicode, outString.length).ToLocalChecked()); + return Napi::String::New(env, reinterpret_cast(outString.unicode), outString.length); } diff --git a/src/impl-apple-lion.cc b/src/impl-apple-lion.cc index 26e90a4..44c0e08 100644 --- a/src/impl-apple-lion.cc +++ b/src/impl-apple-lion.cc @@ -1,15 +1,12 @@ +#define NAPI_VERSION 3 +#include #include #include -using v8::String; -using v8::Exception; -using v8::Local; -using v8::Value; - -Local MYCFStringGetV8String(CFStringRef aString) { +Napi::String MYCFStringGetV8String(Napi::Env env, CFStringRef aString) { if (aString == NULL) { - return Nan::EmptyString(); + return Napi::String::New(env, ""); } CFIndex length = CFStringGetLength(aString); @@ -17,27 +14,28 @@ Local MYCFStringGetV8String(CFStringRef aString) { char *buffer = (char *) malloc(maxSize); if (!CFStringGetCString(aString, buffer, maxSize, kCFStringEncodingUTF8)) { - return Nan::EmptyString(); + return Napi::String::New(env, ""); } - Local result = Nan::New(buffer).ToLocalChecked(); + Napi::String result = Napi::String::New(env, buffer); free(buffer); return result; } -NAN_METHOD(MethodGetVolumeName) { - Nan::Utf8String aPath(info[0]); +Napi::Value MethodGetVolumeName(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + std::string aPath = info[0].As(); CFStringRef out; CFErrorRef error; - CFStringRef volumePath = CFStringCreateWithCString(NULL, *aPath, kCFStringEncodingUTF8); + CFStringRef volumePath = CFStringCreateWithCString(NULL, aPath.c_str(), kCFStringEncodingUTF8); CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true); if(!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeNameKey, &out, &error)) { - return Nan::ThrowError(MYCFStringGetV8String(CFErrorCopyDescription(error))); + throw Napi::Error::New(env, MYCFStringGetV8String(env, CFErrorCopyDescription(error))); } - info.GetReturnValue().Set(MYCFStringGetV8String(out)); + return MYCFStringGetV8String(env, out); } diff --git a/src/volume.cc b/src/volume.cc index a5ff3ea..27247fc 100644 --- a/src/volume.cc +++ b/src/volume.cc @@ -1,19 +1,13 @@ -#include -#include -#include - #ifdef __APPLE__ #include "impl-apple.cc" #else #error This platform is not implemented yet #endif -using v8::FunctionTemplate; - -NAN_MODULE_INIT(Initialize) { - Nan::Set(target, Nan::New("getVolumeName").ToLocalChecked(), - Nan::GetFunction(Nan::New(MethodGetVolumeName)).ToLocalChecked()); +Napi::Object Initialize(Napi::Env env, Napi::Object exports) { + exports["getVolumeName"] = Napi::Function::New(env, MethodGetVolumeName, "getVolumeName"); + return exports; } -NODE_MODULE(volume, Initialize) +NODE_API_MODULE(volume, Initialize)