als-fs-events
is a Node.js module that wraps the standard fs
module to add event emitting capabilities for each file system operation. This allows easy integration of file operation logging and monitoring in your application.
Install the module using npm:
npm install als-fs-events
Import the module in your project and start using the proxied versions of fs
or fs.promises
:
const fs = require('als-fs-events');
// Subscribe to a writeFile event
fs.emitter.on('writeFile', (event) => {
const {prop,args,error,result} = event
console.log(`File ${args[0]} has been written.`);
});
// Write a file asynchronously
await fs.promises.writeFile('example.txt', 'Hello, world!');
The module exports proxied versions of fs
and fs.promises
, which support all standard methods and properties, but also emit events for each operation:
- writeFile
- readFile
- unlink
- and others...
Events are emitted through emitter
, which supports the following events:
- all: an event for any operation
- errors: an event for operation errors
-
operation name (e.g.,
writeFile
): an event for a specific operation
Each event returns an object with the following structure:
-
prop
: name of the operation -
args
: arguments of the operation -
error
: error (if any) -
result
: result of the operation (if no error)
als-fs-events
is released under the MIT License.