Simple Friendly Node.js Output Buffer.
npm install sfn-output-buffer --saveconst OutputBuffer = require("sfn-output-buffer");var ob = new OutputBuffer({
size: 50,
filename: "example.log"
});
var count = 0;
var i = setInterval(() => {
ob.push("Hello, World!"); // Push data into the buffer.
count += 1;
if (count == 10) {
ob.close(); // Close the buffer.
clearInterval(i);
}
}, 1500);new OutputBuffer()Creates a new output buffer.ob.push(...data: any[])Pushes data into the buffer.ob.get(): stringGets buffer contents.ob.clean()Cleans buffer contents without flushing.ob.destroy()Destroys the buffer without flushing.ob.close()Closes the buffer safely, buffer will be flushed before destroying.ob.closedWhether the buffer is closed.
new OutputBuffer(filename?: string)new OutputBuffer(options?: object)Creates a new output buffer.-
optionsInclude these options:ttlTime to live, default is1000ms.sizeBuffer size, if set, thenttlwill be ignored.filenameFlush buffer to a disk file.fileSizeMaximum size of the output file.limitHandlerA function called when the output file's size up to limit, rewrite by default.errorHandlerA function called when any error occurred in the asynchronous timer scope.
If this parameter is passed as a string, then it will be treated as a
filename.
-
// Simplest way, buffer will be flushed to console every 1000 ms.
var ob = new OutputBuffer();
// Flush buffer to a file in 1000 ms:
var ob = new OutputBuffer("example.log");
// Flush buffer to a file when the buffer size up to 1 Mb:
var ob = new OutputBuffer({
size: 1024 * 1024,
filename: "example.log"
});
// Rewrite the output file when its size up to 10 Mb:
var ob = new OutputBuffer({
size: 1024 * 1024,
filename: "example.log",
fileSize: 1024 * 1024 * 10
});
// Customize handlers:
var ob = new OutputBuffer({
ttl: 10000, // Flush buffer every 10 seconds.
filename: "example.log",
fileSize: 1024 * 1024 * 10,
limitHandler: (filename, data, next) => {
// Do some stuffs...
next(); // Must call next(), otherwise the timer-chain will be broken.
},
errorHandler: (e) => {
console.error(e);
}
});