0.3.4 • Published 5 years ago

sfn-output-buffer v0.3.4

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

SFN-Output-Buffer

Simple Friendly Node.js Output Buffer.

Install

npm install sfn-output-buffer --save

Import

const OutputBuffer = require("sfn-output-buffer");

Example

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);

API

  • new OutputBuffer() Creates a new output buffer.
  • ob.push(...data: any[]) Pushes data into the buffer.
  • ob.get(): string Gets 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.closed Whether the buffer is closed.

new OutputBuffer()

  • new OutputBuffer(filename?: string)
  • new OutputBuffer(options?: object) Creates a new output buffer.
    • options Include these options:
      • ttl Time to live, default is 1000ms.
      • size Buffer size, if set, then ttl will be ignored.
      • filename Flush buffer to a disk file.
      • fileSize Maximum size of the output file.
      • limitHandler A function called when the output file's size up to limit, rewrite by default.
      • errorHandler A 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);
    }
});
0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago