Skip to content

Commit 14236ba

Browse files
committed
converted to N-API
1 parent 2450137 commit 14236ba

File tree

7 files changed

+101
-66
lines changed

7 files changed

+101
-66
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

binding.gyp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
"targets": [
33
{
44
"target_name": "native",
5+
"cflags!": [ "-fno-exceptions" ],
6+
"cflags_cc!": [ "-fno-exceptions" ],
7+
"xcode_settings": { "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
8+
"CLANG_CXX_LIBRARY": "libc++",
9+
"MACOSX_DEPLOYMENT_TARGET": "10.7",
10+
},
11+
"msvs_settings": {
12+
"VCCLCompilerTool": { "ExceptionHandling": 1 },
13+
},
514
"sources": [
615
"src/addon.cc",
716
"src/worker.cc",
817
],
918
"include_dirs": [
10-
"<!(node -e \"require('nan')\")"
19+
"<!@(node -p \"require('node-addon-api').include\")",
1120
]
1221
}
1322
]

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "macos-native-processlist",
3-
"version": "1.0.2",
3+
"version": "2.0.0",
44
"description": "Loads the full process list on macOS",
55
"main": "index.js",
66
"types": "typings/macos-native-processlist.d.ts",
@@ -18,7 +18,7 @@
1818
"darwin"
1919
],
2020
"dependencies": {
21-
"nan": "^2.13.2"
21+
"node-addon-api": "3.0.0"
2222
},
2323
"devDependencies": {
2424
"ava": "^3.11.1"

src/addon.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
#include <nan.h>
1+
#include <napi.h>
2+
#include <uv.h>
23
#include "worker.h"
3-
using namespace Nan;
4+
using namespace Napi;
45

5-
NAN_METHOD(GetProcessList) {
6-
auto callback = new Callback(info[0].As<v8::Function>());
7-
AsyncQueueWorker(new Worker(callback));
6+
Napi::Value GetProcessList(const Napi::CallbackInfo &info)
7+
{
8+
auto env = info.Env();
9+
auto callback = info[0].As<Napi::Function>();
10+
(new Worker(callback))->Queue();
11+
return env.Null();
812
}
913

10-
NAN_MODULE_INIT(Init) {
11-
Export(target, "getProcessList", GetProcessList);
14+
Napi::Object Init(Napi::Env env, Napi::Object exports)
15+
{
16+
exports.Set("getProcessList", Napi::Function::New(env, GetProcessList));
17+
return exports;
1218
}
1319

14-
NAN_MODULE_WORKER_ENABLED(NODE_GYP_MODULE_NAME, Init)
20+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

src/worker.cc

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#include "worker.h"
22
#include <libproc.h>
33

4-
using namespace Nan;
5-
6-
Worker::Worker(Callback* callback) : AsyncWorker(callback) {
4+
Worker::Worker(Napi::Function &callback) : AsyncWorker(callback)
5+
{
76
this->processes = new kinfo_proc[1024];
87
processCount = new uint32_t;
98
}
109

11-
Worker::~Worker() {
12-
delete[] this->processes;
13-
delete processCount;
10+
Worker::~Worker()
11+
{
12+
delete[] this->processes;
13+
delete processCount;
1414
}
1515

16-
void Worker::Execute() {
16+
void Worker::Execute()
17+
{
1718
bool done;
1819
int err;
1920
size_t length = 0;
20-
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
21+
static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
2122

2223
*this->processCount = 0;
2324

@@ -33,69 +34,77 @@ void Worker::Execute() {
3334

3435
this->processes = NULL;
3536
done = false;
36-
do {
37+
do
38+
{
3739
assert(this->processes == NULL);
3840

3941
// Call sysctl with a NULL buffer.
4042

4143
length = 0;
42-
err = sysctl((int*)name, sizeof(name) / sizeof(*name) - 1, NULL, &length, NULL, 0);
43-
if (err == -1) {
44+
err = sysctl((int *)name, sizeof(name) / sizeof(*name) - 1, NULL, &length, NULL, 0);
45+
if (err == -1)
46+
{
4447
err = errno;
4548
}
4649

4750
// Allocate an appropriately sized buffer based on the results
4851
// from the previous call.
4952

50-
if (err == 0) {
51-
this->processes = (kinfo_proc*)malloc(length);
53+
if (err == 0)
54+
{
55+
this->processes = (kinfo_proc *)malloc(length);
5256
}
5357

5458
// Call sysctl again with the new buffer. If we get an ENOMEM
5559
// error, toss away our buffer and start again.
5660

57-
if (err == 0) {
58-
err = sysctl((int*)name, sizeof(name) / sizeof(*name) - 1, this->processes, &length, NULL, 0);
59-
if (err == -1) {
61+
if (err == 0)
62+
{
63+
err = sysctl((int *)name, sizeof(name) / sizeof(*name) - 1, this->processes, &length, NULL, 0);
64+
if (err == -1)
65+
{
6066
err = errno;
6167
}
62-
if (err == 0) {
68+
if (err == 0)
69+
{
6370
done = true;
64-
} else if (err == ENOMEM) {
71+
}
72+
else if (err == ENOMEM)
73+
{
6574
assert(this->processes != NULL);
6675
free(this->processes);
6776
this->processes = NULL;
6877
err = 0;
6978
}
7079
}
71-
} while (err == 0 && ! done);
80+
} while (err == 0 && !done);
7281

73-
if (err == 0) {
82+
if (err == 0)
83+
{
7484
*this->processCount = length / sizeof(kinfo_proc);
7585
}
7686
}
7787

7888
#define NAME_BUFFER_SIZE 4096
7989

80-
void Worker::HandleOKCallback() {
81-
HandleScope scope;
82-
83-
v8::Local<v8::Array> result = New<v8::Array>(*processCount);
84-
for (uint32_t i = 0; i < *processCount; i++) {
85-
v8::Local<v8::Object> object = New<v8::Object>();
86-
87-
char buffer[NAME_BUFFER_SIZE];
88-
proc_name(this->processes[i].kp_proc.p_pid, buffer, NAME_BUFFER_SIZE);
89-
Set(object, New<v8::String>("name").ToLocalChecked(), New<v8::String>(buffer).ToLocalChecked());
90-
proc_pidpath(this->processes[i].kp_proc.p_pid, buffer, NAME_BUFFER_SIZE);
91-
Set(object, New<v8::String>("path").ToLocalChecked(), New<v8::String>(buffer).ToLocalChecked());
92-
Set(object, New<v8::String>("pid").ToLocalChecked(), New<v8::Number>(this->processes[i].kp_proc.p_pid));
93-
Set(object, New<v8::String>("ppid").ToLocalChecked(), New<v8::Number>(this->processes[i].kp_eproc.e_ppid));
94-
Set(object, New<v8::String>("pgid").ToLocalChecked(), New<v8::Number>(this->processes[i].kp_eproc.e_pgid));
95-
Set(result, i, New<v8::Value>(object));
96-
}
97-
98-
v8::Local<v8::Value> argv[] = { result };
99-
AsyncResource resource("macos-native-processlist:addon.HandleOKCallback");
100-
callback->Call(1, argv, &resource);
90+
void Worker::OnOK()
91+
{
92+
auto env = this->Env();
93+
auto result = Napi::Array::New(env);
94+
for (uint32_t i = 0; i < *processCount; i++)
95+
{
96+
auto object = Napi::Object::New(env);
97+
98+
char buffer[NAME_BUFFER_SIZE];
99+
proc_name(this->processes[i].kp_proc.p_pid, buffer, NAME_BUFFER_SIZE);
100+
object.Set("name", Napi::String::New(env, buffer));
101+
proc_pidpath(this->processes[i].kp_proc.p_pid, buffer, NAME_BUFFER_SIZE);
102+
object.Set("path", Napi::String::New(env, buffer));
103+
object.Set("pid", Napi::Number::New(env, this->processes[i].kp_proc.p_pid));
104+
object.Set("ppid", Napi::Number::New(env, this->processes[i].kp_eproc.e_ppid));
105+
object.Set("pgid", Napi::Number::New(env, this->processes[i].kp_eproc.e_pgid));
106+
result.Set(i, object);
107+
}
108+
109+
this->Callback().Call({result});
101110
}

src/worker.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#pragma once
2-
#include <nan.h>
2+
#include <napi.h>
3+
#include <uv.h>
34
#include <sys/sysctl.h>
45
#include <pwd.h>
56

6-
class Worker : public Nan::AsyncWorker {
7-
public:
8-
Worker(Nan::Callback *callback);
9-
~Worker();
7+
class Worker : public Napi::AsyncWorker
8+
{
9+
public:
10+
Worker(Napi::Function &callback);
11+
~Worker();
1012

11-
void Execute();
12-
void HandleOKCallback();
13-
private:
14-
kinfo_proc* processes;
15-
uint32_t* processCount;
13+
void Execute();
14+
void OnOK();
15+
16+
private:
17+
kinfo_proc *processes;
18+
uint32_t *processCount;
1619
};

0 commit comments

Comments
 (0)