Fetch a Windows process tree fast in Node.js
Перейти к файлу
Daniel Imms bc08ed1093
Merge pull request #69 from microsoft/dependabot/npm_and_yarn/braces-3.0.3
build(deps-dev): bump braces from 3.0.2 to 3.0.3
2024-06-18 05:54:54 -07:00
.github/workflows refactor: switch to node-addon-api (#67) 2024-02-01 17:32:33 +01:00
azure-pipelines chore: add APIScan fields (#68) 2024-02-29 11:56:18 -08:00
lib chore: change package to throw on other platforms (#65) 2023-06-09 12:59:08 -07:00
src refactor: switch to node-addon-api (#67) 2024-02-01 17:32:33 +01:00
typings use @vscode (#61) 2023-04-03 12:21:14 -07:00
.editorconfig Add .editorconfig 2017-08-03 19:32:40 -07:00
.gitignore revert gitignore change 2018-10-16 08:32:01 +02:00
.npmignore chore: onboard onto unified pipeline 2023-02-03 13:42:40 -08:00
LICENSE Add readme and license 2017-08-01 12:50:48 -07:00
README.md move to @vscode (#60) 2023-03-31 15:29:52 +02:00
SECURITY.md Microsoft mandatory file 2022-08-29 14:03:00 +00:00
binding.gyp refactor: switch to node-addon-api (#67) 2024-02-01 17:32:33 +01:00
package-lock.json build(deps-dev): bump braces from 3.0.2 to 3.0.3 2024-06-16 20:19:50 +00:00
package.json refactor: switch to node-addon-api (#67) 2024-02-01 17:32:33 +01:00
tsconfig.json fix types 2022-03-29 12:44:55 -04:00
tslint.json Convert to TypeScript (#14) 2018-03-19 10:26:47 -07:00

README.md

@vscode/windows-process-tree

Build status

npm version

A Node.js library that enables quickly fetching process tree information for a particular process ID on Windows.

Usage

import * as child_process from 'child_process';
import { getProcessTree } from '@vscode/windows-process-tree';

if (process.platform === 'win32') {
  child_process.spawn('cmd.exe');
  getProcessTree(process.pid, (tree) => {
    console.log(tree);
  });
  // { pid: 11168,
  //   name: 'node.exe',
  //   children:
  //    [ { pid: 1472, name: 'cmd.exe', children:[] },

  getProcessTree(0, (tree) => {
    console.log(tree);
  });
  // undefined
}

For the full API look at the typings file.

Why a native node module?

The current convention is to run wmic.exe to query a particular process ID and then parse the output like so:

let cp = require('child_process');

function getChildProcessDetails(pid, cb) {
    let args = ['process', 'where', `parentProcessId=${pid}`, 'get', 'ExecutablePath,ProcessId'];
    cp.execFile('wmic.exe', args, (err, stdout, stderr) => {
        if (err) {
            throw new Error(err);
        }
        if (stderr.length > 0) {
            cb([]);
        }
        var childProcessLines = stdout.split('\n').slice(1).filter(str => !/^\s*$/.test(str));
        var childProcessDetails = childProcessLines.map(str => {
            var s = str.split('  ');
            return { executable: s[0], pid: Number(s[1]) };
        });
        cb(childProcessDetails);
    });
}

This has two problems:

  1. It takes > 100ms* to spin up a process and get the output returned.
  2. It only goes one level deep. Meaning, if the process tree is deeply nested or processes in the tree have many children it will take a lot more time and need a bunch of processes launched.

Both of which are only exacerbated by the fact that this information is something that a consumer may want to poll for.

The native node module uses Windows APIs to get the process details and then they are organized into a tree, getting the entire tree's details in < 20ms*.

* On my machine 🙂

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.