Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
### 4.0.2

- gwmi: invoke powershell with `-NoProfile -NonInteractive` so stats polling no longer executes user profile scripts
- win: detect wmic once without spawning a process, add `useGwmi` option to skip the detection
- fixes DEP0190 deprecation warning on node 24 (#191)

### 4.0.1

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Setting the options programatically will override environment variables
| Param | Type | Environment variable | Default | Description |
| --- | --- | --- | --- | --- |
| [usePs] | <code>boolean</code> | `PIDUSAGE_USE_PS`| `false` | When true uses `ps` instead of proc files to fetch process information |
| [useGwmi] | <code>boolean</code> | | `false` | Windows only, use gwmi (PowerShell) directly without checking whether wmic is available |
| [maxage] | <code>number</code> | `PIDUSAGE_MAXAGE`| `60000` | Max age of a process on history. |

`PIDUSAGE_SILENT=1` can be used to remove every console message triggered by pidusage.
Expand Down
25 changes: 11 additions & 14 deletions lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const spawn = require('child_process').spawn
const path = require('path')

const requireMap = {
ps: () => require('./ps'),
Expand Down Expand Up @@ -41,6 +41,15 @@ try {
stat = requireMap[platformToMethod[platform]]()
} catch (err) {}

let wmicAvailable

function hasWmic () {
if (wmicAvailable === undefined) {
wmicAvailable = fs.existsSync(path.join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'wbem', 'WMIC.exe'))
}
return wmicAvailable
}

/**
* @callback pidCallback
* @param {Error} err A possible error.
Expand All @@ -60,19 +69,7 @@ function get (pids, options, callback) {
fn = requireMap.ps()
}
if (platform === 'win') {
// TODO: use https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/where to avoid try/catch
let child
try {
child = spawn('wmic', function (err) {
if (err) throw new Error(err)
})
} catch (err) {
fn = requireMap.gwmi()
} finally {
if (child) {
child.kill()
}
}
fn = options.useGwmi === true || !hasWmic() ? requireMap.gwmi() : requireMap.wmic()
}

if (fn === undefined) {
Expand Down
68 changes: 68 additions & 0 deletions test/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const mockery = require('mockery')
const test = require('ava')
const os = require('os')
const fs = require('fs')
const path = require('path')

const wmicPath = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'wbem', 'WMIC.exe')

function load (wmicExists) {
const wmicChecks = []

mockery.registerMock('os', { ...os, platform: () => 'win32' })
mockery.registerMock('fs', {
...fs,
existsSync: (p) => {
if (p === wmicPath) {
wmicChecks.push(p)
return wmicExists
}
return false
}
})
mockery.registerMock('./wmic', (pids, options, done) => done(null, { [pids[0]]: { marker: 'wmic' } }))
mockery.registerMock('./gwmi', (pids, options, done) => done(null, { [pids[0]]: { marker: 'gwmi' } }))

const get = require('../lib/stats')
const call = (options = {}) => new Promise((resolve, reject) => {
get(1, options, (err, stat) => err ? reject(err) : resolve(stat))
})

return { call, wmicChecks }
}

test.beforeEach(() => {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true
})
})

test.afterEach.always(() => {
mockery.deregisterAll()
mockery.disable()
})

test.serial('should use wmic when WMIC.exe exists', async t => {
const { call } = load(true)
t.is((await call()).marker, 'wmic')
})

test.serial('should use gwmi when WMIC.exe is missing', async t => {
const { call } = load(false)
t.is((await call()).marker, 'gwmi')
})

test.serial('should use gwmi without detection when useGwmi is true', async t => {
const { call, wmicChecks } = load(true)
t.is((await call({ useGwmi: true })).marker, 'gwmi')
t.is(wmicChecks.length, 0)
})

test.serial('should detect wmic only once', async t => {
const { call, wmicChecks } = load(true)
await call()
await call()
t.is(wmicChecks.length, 1)
})
Loading