Skip to content

Commit 2ac26ce

Browse files
authored
Merge pull request #38 from microsoft/fixUndefined
fix types
2 parents d15a627 + 22537df commit 2ac26ce

5 files changed

Lines changed: 117 additions & 93 deletions

File tree

lib/index.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum ProcessDataFlag {
1313
}
1414

1515
interface IRequest {
16-
callback: (processes: IProcessTreeNode | IProcessInfo[]) => void;
16+
callback: (processes: IProcessTreeNode | IProcessInfo[] | undefined) => void;
1717
rootPid: number;
1818
}
1919

@@ -33,20 +33,34 @@ const MAX_FILTER_DEPTH = 10;
3333
* @param processList The list of processes
3434
* @param maxDepth The maximum depth to search
3535
*/
36-
export function buildProcessTree(rootPid: number, processList: IProcessInfo[], maxDepth: number): IProcessTreeNode {
36+
export function buildProcessTree(rootPid: number, processList: IProcessInfo[] | undefined, maxDepth: number): IProcessTreeNode | undefined {
37+
if (!processList) {
38+
return undefined;
39+
}
3740
const rootIndex = processList.findIndex(v => v.pid === rootPid);
3841
if (rootIndex === -1) {
3942
return undefined;
4043
}
4144
const rootProcess = processList[rootIndex];
4245
const childIndexes = processList.filter(v => v.ppid === rootPid);
4346

47+
const children: IProcessTreeNode[] = [];
48+
49+
if (maxDepth !== 0) {
50+
for (const c of childIndexes) {
51+
const tree = buildProcessTree(c.pid, processList, maxDepth - 1);
52+
if (tree) {
53+
children.push(tree);
54+
}
55+
}
56+
}
57+
4458
return {
4559
pid: rootProcess.pid,
4660
name: rootProcess.name,
4761
memory: rootProcess.memory,
4862
commandLine: rootProcess.commandLine,
49-
children: maxDepth === 0 ? [] : childIndexes.map(c => buildProcessTree(c.pid, processList, maxDepth - 1))
63+
children
5064
};
5165
}
5266

@@ -56,7 +70,7 @@ export function buildProcessTree(rootPid: number, processList: IProcessInfo[], m
5670
* @param processList The list of all processes
5771
* @param maxDepth The maximum depth to search
5872
*/
59-
export function filterProcessList(rootPid: number, processList: IProcessInfo[], maxDepth: number): IProcessInfo[] {
73+
export function filterProcessList(rootPid: number, processList: IProcessInfo[], maxDepth: number): IProcessInfo[] | undefined {
6074
const rootIndex = processList.findIndex(v => v.pid === rootPid);
6175
if (rootIndex === -1) {
6276
return undefined;
@@ -68,14 +82,22 @@ export function filterProcessList(rootPid: number, processList: IProcessInfo[],
6882

6983
const rootProcess = processList[rootIndex];
7084
const childIndexes = processList.filter(v => v.ppid === rootPid);
71-
return childIndexes.map(c => filterProcessList(c.pid, processList, maxDepth - 1)).reduce((prev, current) => prev.concat(current), [rootProcess]);
85+
86+
const children: IProcessInfo[][] = [];
87+
for (const c of childIndexes) {
88+
const list = filterProcessList(c.pid, processList, maxDepth - 1);
89+
if (list) {
90+
children.push(list);
91+
}
92+
}
93+
return children.reduce((prev, current) => prev.concat(current), [rootProcess]);
7294
}
7395

7496
function getRawProcessList(
7597
pid: number,
7698
queue: RequestQueue,
77-
callback: (processList: IProcessInfo[] | IProcessTreeNode) => void,
78-
filter: (pid: number, processList: IProcessInfo[], maxDepth: number) => IProcessInfo[] | IProcessTreeNode,
99+
callback: (processList: IProcessInfo[] | IProcessTreeNode | undefined) => void,
100+
filter: (pid: number, processList: IProcessInfo[] | undefined, maxDepth: number) => IProcessInfo[] | IProcessTreeNode | undefined,
79101
flags?: ProcessDataFlag
80102
): void {
81103
queue.push({
@@ -105,7 +127,7 @@ function getRawProcessList(
105127
* @param callback The callback to use with the returned set of processes
106128
* @param flags The flags for what process data should be included
107129
*/
108-
export function getProcessList(rootPid: number, callback: (processList: IProcessInfo[]) => void, flags?: ProcessDataFlag): void {
130+
export function getProcessList(rootPid: number, callback: (processList: IProcessInfo[] | undefined) => void, flags?: ProcessDataFlag): void {
109131
getRawProcessList(rootPid, processListRequestQueue, callback, filterProcessList, flags);
110132
}
111133

@@ -124,6 +146,6 @@ export function getProcessCpuUsage(processList: IProcessInfo[], callback: (tree:
124146
* @param callback The callback to use with the returned list of processes
125147
* @param flags Flags indicating what process data should be written on each node
126148
*/
127-
export function getProcessTree(rootPid: number, callback: (tree: IProcessTreeNode) => void, flags?: ProcessDataFlag): void {
149+
export function getProcessTree(rootPid: number, callback: (tree: IProcessTreeNode | undefined) => void, flags?: ProcessDataFlag): void {
128150
getRawProcessList(rootPid, processTreeRequestQueue, callback, buildProcessTree, flags);
129151
}

lib/test.ts

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ describe('getRawProcessList', () => {
4040

4141
it('should return a list containing this process', (done) => {
4242
native.getProcessList((list) => {
43-
assert.notStrictEqual(list.find(p => p.pid === process.pid), undefined);
43+
assert.notStrictEqual(list?.find(p => p.pid === process.pid), undefined);
4444
done();
4545
}, 0);
4646
});
4747

4848
it('should handle multiple calls gracefully', (done) => {
4949
let counter = 0;
5050
const callback = (list) => {
51-
assert.notStrictEqual(list.find(p => p.pid === process.pid), undefined);
51+
assert.notStrictEqual(list?.find(p => p.pid === process.pid), undefined);
5252
if (++counter === 2) {
5353
done();
5454
}
@@ -60,11 +60,11 @@ describe('getRawProcessList', () => {
6060
it('should return memory information only when the flag is set', (done) => {
6161
// Memory should be undefined when flag is not set
6262
native.getProcessList((list) => {
63-
assert.strictEqual(list.every(p => p.memory === undefined), true);
63+
assert.strictEqual(list?.every(p => p.memory === undefined), true);
6464

6565
// Memory should be a number when flag is set
6666
native.getProcessList((list) => {
67-
assert.strictEqual(list.some(p => p.memory > 0), true);
67+
assert.strictEqual(list?.some(p => p.memory > 0), true);
6868
done();
6969
}, ProcessDataFlag.Memory);
7070
}, ProcessDataFlag.None);
@@ -73,11 +73,11 @@ describe('getRawProcessList', () => {
7373
it('should return command line information only when the flag is set', (done) => {
7474
// commandLine should be undefined when flag is not set
7575
native.getProcessList((list) => {
76-
assert.strictEqual(list.every(p => p.commandLine === undefined), true);
76+
assert.strictEqual(list?.every(p => p.commandLine === undefined), true);
7777

7878
// commandLine should be a string when flag is set
7979
native.getProcessList((list) => {
80-
assert.strictEqual(list.every(p => typeof p.commandLine === 'string'), true);
80+
assert.strictEqual(list?.every(p => typeof p.commandLine === 'string'), true);
8181
done();
8282
}, ProcessDataFlag.CommandLine);
8383
}, ProcessDataFlag.None);
@@ -99,34 +99,34 @@ describe('getProcessList', () => {
9999

100100
it('should return a list containing this process', (done) => {
101101
getProcessList(process.pid, (list) => {
102-
assert.strictEqual(list.length, 1);
103-
assert.strictEqual(list[0].name, 'node.exe');
104-
assert.strictEqual(list[0].pid, process.pid);
105-
assert.strictEqual(list[0].memory, undefined);
106-
assert.strictEqual(list[0].commandLine, undefined);
102+
assert.strictEqual(list?.length, 1);
103+
assert.strictEqual(list![0].name, 'node.exe');
104+
assert.strictEqual(list![0].pid, process.pid);
105+
assert.strictEqual(list![0].memory, undefined);
106+
assert.strictEqual(list![0].commandLine, undefined);
107107
done();
108108
});
109109
});
110110

111111
it('should return a list containing this process\'s memory if the flag is set', done => {
112112
getProcessList(process.pid, (list) => {
113-
assert.strictEqual(list.length, 1);
114-
assert.strictEqual(list[0].name, 'node.exe');
115-
assert.strictEqual(list[0].pid, process.pid);
116-
assert.strictEqual(typeof list[0].memory, 'number');
113+
assert.strictEqual(list?.length, 1);
114+
assert.strictEqual(list![0].name, 'node.exe');
115+
assert.strictEqual(list![0].pid, process.pid);
116+
assert.strictEqual(typeof list![0].memory, 'number');
117117
done();
118118
}, ProcessDataFlag.Memory);
119119
});
120120

121121
it('should return command line information only if the flag is set', (done) => {
122122
getProcessList(process.pid, (list) => {
123-
assert.strictEqual(list.length, 1);
124-
assert.strictEqual(list[0].name, 'node.exe');
125-
assert.strictEqual(list[0].pid, process.pid);
126-
assert.strictEqual(typeof list[0].commandLine, 'string');
123+
assert.strictEqual(list?.length, 1);
124+
assert.strictEqual(list![0].name, 'node.exe');
125+
assert.strictEqual(list![0].pid, process.pid);
126+
assert.strictEqual(typeof list![0].commandLine, 'string');
127127
// CommandLine is "<path to node> <path to mocha> lib/test.js"
128-
assert.strictEqual(list[0].commandLine.indexOf('mocha') > 0, true);
129-
assert.strictEqual(list[0].commandLine.indexOf('lib/test.js') > 0, true);
128+
assert.strictEqual(list![0].commandLine!.indexOf('mocha') > 0, true);
129+
assert.strictEqual(list![0].commandLine!.indexOf('lib/test.js') > 0, true);
130130
done();
131131
}, ProcessDataFlag.CommandLine);
132132
});
@@ -136,7 +136,7 @@ describe('getProcessList', () => {
136136
pollUntil(() => {
137137
return new Promise((resolve) => {
138138
getProcessList(process.pid, (list) => {
139-
resolve(list.length === 2 && list[0].pid === process.pid && list[1].pid === cps[0].pid);
139+
resolve(list?.length === 2 && list![0].pid === process.pid && list[1].pid === cps[0].pid);
140140
});
141141
});
142142
}, () => done(), 20, 500);
@@ -148,11 +148,11 @@ describe('getProcessCpuUsage', () => {
148148
it('should get process cpu usage', (done) => {
149149
getProcessCpuUsage([{ pid: process.pid, ppid: process.ppid, name: 'node.exe' }], (annotatedList) => {
150150
assert.strictEqual(annotatedList.length, 1);
151-
assert.strictEqual(annotatedList[0].name, 'node.exe');
152-
assert.strictEqual(annotatedList[0].pid, process.pid);
153-
assert.strictEqual(annotatedList[0].memory, undefined);
154-
assert.strictEqual(typeof annotatedList[0].cpu, 'number');
155-
assert.strictEqual(0 <= annotatedList[0].cpu && annotatedList[0].cpu <= 100, true);
151+
assert.strictEqual(annotatedList![0].name, 'node.exe');
152+
assert.strictEqual(annotatedList![0].pid, process.pid);
153+
assert.strictEqual(annotatedList![0].memory, undefined);
154+
assert.strictEqual(typeof annotatedList![0].cpu, 'number');
155+
assert.strictEqual(0 <= annotatedList![0].cpu! && annotatedList![0].cpu! <= 100, true);
156156
done();
157157
});
158158
});
@@ -162,7 +162,7 @@ describe('getProcessCpuUsage', () => {
162162

163163
let counter = 0;
164164
const callback = (list) => {
165-
assert.notStrictEqual(list.find(p => p.pid === process.pid), undefined);
165+
assert.notStrictEqual(list?.find(p => p.pid === process.pid), undefined);
166166
if (++counter === 2) {
167167
done();
168168
}
@@ -187,31 +187,31 @@ describe('getProcessTree', () => {
187187

188188
it('should return a tree containing this process', done => {
189189
getProcessTree(process.pid, (tree) => {
190-
assert.strictEqual(tree.name, 'node.exe');
191-
assert.strictEqual(tree.pid, process.pid);
192-
assert.strictEqual(tree.memory, undefined);
193-
assert.strictEqual(tree.commandLine, undefined);
194-
assert.strictEqual(tree.children.length, 0);
190+
assert.strictEqual(tree!.name, 'node.exe');
191+
assert.strictEqual(tree!.pid, process.pid);
192+
assert.strictEqual(tree!.memory, undefined);
193+
assert.strictEqual(tree!.commandLine, undefined);
194+
assert.strictEqual(tree!.children.length, 0);
195195
done();
196196
});
197197
});
198198

199199
it('should return a tree containing this process\'s memory if the flag is set', done => {
200200
getProcessTree(process.pid, (tree) => {
201-
assert.strictEqual(tree.name, 'node.exe');
202-
assert.strictEqual(tree.pid, process.pid);
203-
assert.notStrictEqual(tree.memory, undefined);
204-
assert.strictEqual(tree.children.length, 0);
201+
assert.strictEqual(tree!.name, 'node.exe');
202+
assert.strictEqual(tree!.pid, process.pid);
203+
assert.notStrictEqual(tree!.memory, undefined);
204+
assert.strictEqual(tree!.children.length, 0);
205205
done();
206206
}, ProcessDataFlag.Memory);
207207
});
208208

209209
it('should return a tree containing this process\'s command line if the flag is set', done => {
210210
getProcessTree(process.pid, (tree) => {
211-
assert.strictEqual(tree.name, 'node.exe');
212-
assert.strictEqual(tree.pid, process.pid);
213-
assert.strictEqual(typeof tree.commandLine, 'string');
214-
assert.strictEqual(tree.children.length, 0);
211+
assert.strictEqual(tree!.name, 'node.exe');
212+
assert.strictEqual(tree!.pid, process.pid);
213+
assert.strictEqual(typeof tree!.commandLine, 'string');
214+
assert.strictEqual(tree!.children.length, 0);
215215
done();
216216
}, ProcessDataFlag.CommandLine);
217217
});
@@ -221,7 +221,7 @@ describe('getProcessTree', () => {
221221
pollUntil(() => {
222222
return new Promise((resolve) => {
223223
getProcessTree(process.pid, (tree) => {
224-
resolve(tree.children.length === 1);
224+
resolve(tree!.children.length === 1);
225225
});
226226
});
227227
}, () => done(), 20, 500);
@@ -233,13 +233,13 @@ describe('getProcessTree', () => {
233233
pollUntil(() => {
234234
return new Promise((resolve) => {
235235
getProcessTree(process.pid, (tree) => {
236-
resolve(tree.children.length === 2 &&
237-
tree.children[0].name === 'powershell.exe' &&
238-
tree.children[0].children.length === 0 &&
239-
tree.children[1].name === 'cmd.exe' &&
240-
tree.children[1].children &&
241-
tree.children[1].children.length === 1 &&
242-
tree.children[1].children[0].name === 'powershell.exe');
236+
resolve(tree!.children.length === 2 &&
237+
tree!.children[0].name === 'powershell.exe' &&
238+
tree!.children[0].children.length === 0 &&
239+
tree!.children[1].name === 'cmd.exe' &&
240+
tree!.children[1].children &&
241+
tree!.children[1].children.length === 1 &&
242+
tree!.children[1].children[0].name === 'powershell.exe');
243243
});
244244
});
245245
}, () => done(), 20, 500);
@@ -251,14 +251,14 @@ describe('buildProcessTree', () => {
251251
const tree = buildProcessTree(0, [
252252
{ pid: 0, ppid: 0, name: '0' }
253253
], 3);
254-
assert.strictEqual(tree.pid, 0);
255-
assert.strictEqual(tree.children.length, 1);
256-
assert.strictEqual(tree.children[0].pid, 0);
257-
assert.strictEqual(tree.children[0].children.length, 1);
258-
assert.strictEqual(tree.children[0].children[0].pid, 0);
259-
assert.strictEqual(tree.children[0].children[0].children.length, 1);
260-
assert.strictEqual(tree.children[0].children[0].children[0].pid, 0);
261-
assert.strictEqual(tree.children[0].children[0].children[0].children.length, 0);
254+
assert.strictEqual(tree!.pid, 0);
255+
assert.strictEqual(tree!.children.length, 1);
256+
assert.strictEqual(tree!.children[0].pid, 0);
257+
assert.strictEqual(tree!.children[0].children.length, 1);
258+
assert.strictEqual(tree!.children[0].children[0].pid, 0);
259+
assert.strictEqual(tree!.children[0].children[0].children.length, 1);
260+
assert.strictEqual(tree!.children[0].children[0].children[0].pid, 0);
261+
assert.strictEqual(tree!.children[0].children[0].children[0].children.length, 0);
262262
});
263263
});
264264

@@ -267,11 +267,11 @@ describe('filterProcessList', () => {
267267
const list = filterProcessList(0, [
268268
{ pid: 0, ppid: 0, name: '0' }
269269
], 3);
270-
assert.strictEqual(list.length, 4);
271-
assert.strictEqual(list[0].pid, 0);
272-
assert.strictEqual(list[1].pid, 0);
273-
assert.strictEqual(list[2].pid, 0);
274-
assert.strictEqual(list[3].pid, 0);
270+
assert.strictEqual(list?.length, 4);
271+
assert.strictEqual(list![0].pid, 0);
272+
assert.strictEqual(list![1].pid, 0);
273+
assert.strictEqual(list![2].pid, 0);
274+
assert.strictEqual(list![3].pid, 0);
275275
});
276276
});
277277

@@ -293,11 +293,11 @@ describe('contextAware', () => {
293293
});
294294
const processListPromise: Promise<boolean> = new Promise(resolve => {
295295
getProcessList(process.pid, (list) => {
296-
assert.strictEqual(list.length >= 1, true);
297-
assert.strictEqual(list[0].name, 'node.exe');
298-
assert.strictEqual(list[0].pid, process.pid);
299-
assert.strictEqual(list[0].memory, undefined);
300-
assert.strictEqual(list[0].commandLine, undefined);
296+
assert.strictEqual(list!.length >= 1, true);
297+
assert.strictEqual(list![0].name, 'node.exe');
298+
assert.strictEqual(list![0].pid, process.pid);
299+
assert.strictEqual(list![0].memory, undefined);
300+
assert.strictEqual(list![0].commandLine, undefined);
301301
resolve(true);
302302
});
303303
});
@@ -327,17 +327,17 @@ describe('contextAware', () => {
327327
});
328328
});
329329
};
330-
const workerPromises = [];
330+
const workerPromises: Promise<boolean>[] = [];
331331
for (let i = 0; i < 50; i++) {
332332
workerPromises.push(makeWorkerPromise());
333333
}
334334
const processListPromise: Promise<boolean> = new Promise(resolve => {
335335
getProcessList(process.pid, (list) => {
336-
assert.strictEqual(list.length >= 1, true);
337-
assert.strictEqual(list[0].name, 'node.exe');
338-
assert.strictEqual(list[0].pid, process.pid);
339-
assert.strictEqual(list[0].memory, undefined);
340-
assert.strictEqual(list[0].commandLine, undefined);
336+
assert.strictEqual(list!.length >= 1, true);
337+
assert.strictEqual(list![0].name, 'node.exe');
338+
assert.strictEqual(list![0].pid, process.pid);
339+
assert.strictEqual(list![0].memory, undefined);
340+
assert.strictEqual(list![0].commandLine, undefined);
341341
resolve(true);
342342
});
343343
});

0 commit comments

Comments
 (0)