fs operation monitoring for heimdalljs
npm install heimdalljs-fs-monitor
file system monitor plugin for heimdalljs
> [!Note]
> This has been forked from the archived repo https://github.com/heimdalljs/heimdall-fs-monitor to fix an issue with Node 24
```
npm install --save-dev heimdalljs-fs-monitor
`js
const FSMonitor = require('heimdalljs-fs-monitor');
// create a new file system monitor
const monitor = new FSMonitor();
// start monitoring
monitor.start();
// ... do some file system work ...
var fs = require('fs');
fs.readFileSync('package.json');
// stop monitoring
monitor.stop();
// read file system call stats
const heimdall = require('heimdalljs');
const stats = heimdall.statsFor('fs');
`
In order to get more information about where fs calls are coming from, simply adding an environment variable called HEIMDALL_FS_MONITOR_CALL_TRACING and setting that to 1 will add an invocation object to the fs.stats[{fsFunctionName}] object. This could be a potential place where memory backpressure will occur, as this will continue to grow during a build. Since we are not storing every invocation as an entry in an array, and instead just increasing the count for a particular stack trace, this should only be used when needed.
`js
process.env.HEIMDALL_FS_MONITOR_CALL_TRACING = 1;
const FSMonitor = require('heimdalljs-fs-monitor');
// create a new file system monitor
const monitor = new FSMonitor();
// start monitoring
monitor.start();
// ... do some file system work ...
var fs = require('fs');
fs.readFileSync('package.json');
// stop monitoring
monitor.stop();
`
Accessing heimdall.fs.stats.readFileSync, for example will yeild a _Metric_ object that looks like the following:
`js`
{
count: 1,
time: 821855,
invocations: {
' at Object.readFileSync (~/heimdall-fs-monitor/index.js:112:35)\n at Context.
lineNumber: 89,
fileName: '~/heimdall-fs-monitor/tests.js',
count: 1
}
}
}