From 5a21f940ba0ce16ae230d64a46b877fdfbf06c37 Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Thu, 3 Nov 2022 18:14:46 -0700 Subject: [PATCH 1/2] nit: pass getProcessCpuUsage callback directly --- lib/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.ts b/lib/index.ts index 1122eb7..2427141 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -137,7 +137,7 @@ export function getProcessList(rootPid: number, callback: (processList: IProcess * @param callback The callback to use with the returned list of processes */ export function getProcessCpuUsage(processList: IProcessInfo[], callback: (tree: IProcessCpuInfo[]) => void): void { - native.getProcessCpuUsage(processList, (processListWithCpu) => callback(processListWithCpu)); + native.getProcessCpuUsage(processList, callback); } /** From 5f5d4f48c794d87b438e6d1ca14cd33c2a504549 Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Thu, 3 Nov 2022 17:15:01 -0700 Subject: [PATCH 2/2] fix: incorrect getProcessCpuUsage type error messages --- lib/test.ts | 5 +++++ src/addon.cc | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/test.ts b/lib/test.ts index 2436f95..2f31b7c 100644 --- a/lib/test.ts +++ b/lib/test.ts @@ -144,6 +144,11 @@ describe('getProcessList', () => { }); describe('getProcessCpuUsage', () => { + it('should throw on incorrect argument types', done => { + assert.throws(() => getProcessCpuUsage('<…>' as any, () => null), /processList.*array/); + assert.throws(() => getProcessCpuUsage([], '<…>' as any), /callback.*function/); + done(); + }); it('should get process cpu usage', (done) => { getProcessCpuUsage([{ pid: process.pid, ppid: process.ppid, name: 'node.exe' }], (annotatedList) => { diff --git a/src/addon.cc b/src/addon.cc index 6fd43fd..cfb4d5d 100644 --- a/src/addon.cc +++ b/src/addon.cc @@ -37,12 +37,12 @@ void GetProcessCpuUsage(const Nan::FunctionCallbackInfo& args) { } if (!args[0]->IsArray()) { - Nan::ThrowTypeError("The first argument of GetProcessCpuUsage, callback, must be an array."); + Nan::ThrowTypeError("The first argument of GetProcessCpuUsage, processList, must be an array."); return; } if (!args[1]->IsFunction()) { - Nan::ThrowTypeError("The second argument of GetProcessCpuUsage, flags, must be a function."); + Nan::ThrowTypeError("The second argument of GetProcessCpuUsage, callback, must be a function."); return; }