Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

[WIP] Migrate to N-API #105

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@
'targets': [
{
'target_name': 'fs_admin',
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'sources': [
'src/main.cc',
],
'include_dirs': [
'<!(node -e "require(\'nan\')")'
'<!(node -p "require(\'node-addon-api\').include_dir")',
],
'conditions': [
['OS=="win"', {
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"url": "https://github.com/atom/fs-admin/issues"
},
"dependencies": {
"nan": "^2.13.2",
"node-addon-api": "^3.1.0",
"prebuild-install": "^6.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/fs-admin-darwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AuthorizationRef GetAuthorizationRef() {
return authorization_ref;
}

void ClearAuthorizationCache() {
void ClearAuthorizationCacheImpl() {
if (authorization_ref) {
AuthorizationFree(authorization_ref, kAuthorizationFlagDefaults);
authorization_ref = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/fs-admin-linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ int WaitForChildProcessToExit(void *child_process, bool test_mode) {
}

string CreateAuthorizationForm() { return ""; }
void ClearAuthorizationCache() {}
void ClearAuthorizationCacheImpl() {}

} // namespace fs_admin
2 changes: 1 addition & 1 deletion src/fs-admin-win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ int WaitForChildProcessToExit(void *child_process, bool test_mode) {
}

std::string CreateAuthorizationForm() { return ""; }
void ClearAuthorizationCache() {}
void ClearAuthorizationCacheImpl() {}

} // namespace fs_admin
2 changes: 1 addition & 1 deletion src/fs-admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace fs_admin {

std::string CreateAuthorizationForm();

void ClearAuthorizationCache();
void ClearAuthorizationCacheImpl();

void *StartChildProcess(const std::string &command, const std::vector<std::string> &args, bool test_mode);

Expand Down
94 changes: 47 additions & 47 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#include "nan.h"
#include "napi.h"
#include "uv.h"
#include "fs-admin.h"

namespace fs_admin {

using namespace v8;
using namespace Napi;

class Worker : public Nan::AsyncWorker {
class Worker : public Napi::AsyncWorker {
void *child_process;
int exit_code;
bool test_mode;

public:
Worker(Nan::Callback *callback, void *child_process, bool test_mode) :
Nan::AsyncWorker(callback),
Worker(const Function& callback, void *child_process, bool test_mode) :
Napi::AsyncWorker(callback),
child_process(child_process),
exit_code(-1),
test_mode(test_mode) {}
Expand All @@ -21,77 +22,76 @@ class Worker : public Nan::AsyncWorker {
exit_code = WaitForChildProcessToExit(child_process, test_mode);
}

void HandleOKCallback() {
Local<Value> argv[] = {Nan::New<Integer>(exit_code)};
callback->Call(1, argv, async_resource);
void OnOK() {
const std::vector<napi_value> argv{Napi::Number::New(Env(), exit_code)};
Callback().Call(argv);
}
};

void GetAuthorizationForm(const Nan::FunctionCallbackInfo<Value>& info) {
Napi::Value GetAuthorizationForm(const Napi::CallbackInfo& info) {
const Napi::Env& env = info.Env();
auto auth_form = CreateAuthorizationForm();
auto buffer = Nan::CopyBuffer(auth_form.c_str(), auth_form.size());
info.GetReturnValue().Set(buffer.ToLocalChecked());
auto buffer = Napi::Buffer<char>::Copy(env, auth_form.c_str(), auth_form.size());
return buffer;
}

void ClearAuthorizationCache(const Nan::FunctionCallbackInfo<Value>& info) {
ClearAuthorizationCache();
Napi::Value ClearAuthorizationCache(const Napi::CallbackInfo& info) {
ClearAuthorizationCacheImpl();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return info.Env().Undefined();
}

void SpawnAsAdmin(const Nan::FunctionCallbackInfo<Value>& info) {
if (!info[0]->IsString()) {
Nan::ThrowTypeError("Command must be a string");
return;
Napi::Value SpawnAsAdmin(const Napi::CallbackInfo& info) {
const Napi::Env& env = info.Env();
if (!info[0].IsString()) {
Napi::TypeError::New(env, "Command must be a string").ThrowAsJavaScriptException();
return env.Null();
}

Nan::Utf8String commandNan(info[0]);
std::string command(*commandNan, commandNan.length());
std::string command = info[0].As<Napi::String>();

if (!info[1]->IsArray()) {
Nan::ThrowTypeError("Arguments must be an array");
return;
if (!info[1].IsArray()) {
Napi::TypeError::New(env, "Arguments must be an array").ThrowAsJavaScriptException();
return env.Undefined();
}

Local<Array> js_args = Local<Array>::Cast(info[1]);
Napi::Array js_args = info[1].As<Napi::Array>();
std::vector<std::string> args;
args.reserve(js_args->Length());
for (uint32_t i = 0; i < js_args->Length(); ++i) {
Local<Context> context = Nan::GetCurrentContext();
Local<Value> js_arg = js_args->Get(context, i).ToLocalChecked();
if (!js_arg->IsString()) {
Nan::ThrowTypeError("Arguments must be an array of strings");
return;
args.reserve(js_args.Length());
for (uint32_t i = 0; i < js_args.Length(); ++i) {
Napi::Value js_arg = js_args.Get(i);
if (!js_arg.IsString()) {
Napi::TypeError::New(env, "Arguments must be an array of strings").ThrowAsJavaScriptException();
return env.Undefined();
}

args.push_back(*Nan::Utf8String(js_arg));
args.push_back(js_arg.As<Napi::String>());
}

bool test_mode = false;
if (info[2]->IsTrue()) test_mode = true;
if (info[2].ToBoolean().Value()) test_mode = true;

if (!info[3]->IsFunction()) {
Nan::ThrowTypeError("Callback must be a function");
return;
if (!info[3].IsFunction()) {
Napi::TypeError::New(env, "Callback must be a function").ThrowAsJavaScriptException();
return env.Undefined();
}

void *child_process = StartChildProcess(command, args, test_mode);
if (!child_process) {
info.GetReturnValue().Set(Nan::False());
return Napi::Number::New(env, false);
} else {
Nan::AsyncQueueWorker(new Worker(new Nan::Callback(info[3].As<Function>()), child_process, test_mode));
info.GetReturnValue().Set(Nan::True());
auto worker = new Worker(info[3].As<Napi::Function>(), child_process, test_mode);
worker->Queue();
return Napi::Number::New(env, true);
}
}

NAN_MODULE_INIT(Init) {
Nan::SetMethod(target, "getAuthorizationForm", GetAuthorizationForm);
Nan::SetMethod(target, "clearAuthorizationCache", ClearAuthorizationCache);
Nan::SetMethod(target, "spawnAsAdmin", SpawnAsAdmin);
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "getAuthorizationForm"), Napi::Function::New(env, GetAuthorizationForm));
exports.Set(Napi::String::New(env, "clearAuthorizationCache"), Napi::Function::New(env, ClearAuthorizationCache));
exports.Set(Napi::String::New(env, "spawnAsAdmin"), Napi::Function::New(env, SpawnAsAdmin));
return exports;
}

#if NODE_MAJOR_VERSION >= 10
NAN_MODULE_WORKER_ENABLED(fs_admin, Init)
#else
NODE_MODULE(fs_admin, Init)
#endif
NODE_API_MODULE(fs_admin, Init)

} // namespace spawn_as_admin