Install sonic-boom via npm
masterInstall the sonic-boom package using npm to use its high-performance UTF-8 stream implementation for writing to files and file descriptors.
npm i sonic-boomrepository·master·Indexed 43 days ago
https://github.com/pinojs/sonic-boomAn extremely fast, UTF-8 only stream implementation for high-performance writing to files and file descriptors, optimized to be significantly faster than Node.js core fs.createWriteStream(). Version 5.0.1 provides features such as synchronous writes, periodic flushing, and log rotation support via the reopen() method.
Install the sonic-boom package using npm to use its high-performance UTF-8 stream implementation for writing to files and file descriptors.
npm i sonic-boomSonicBoom is an EventEmitter and emits several useful events:
'ready': Emitted when the file descriptor is open and ready for writing. (Note: In sync: true mode, this is emitted via process.nextTick).'error': Emitted when an error occurs during file opening or writing.'write': Emitted when a write operation completes, with the number of bytes written as the argument.'drain': Emitted when the internal buffer is cleared and it is safe to write more data.'drop': Emitted when data is dropped because maxLength was exceeded.'finish': Emitted after end() is called and all buffered data has been successfully written.'close': Emitted when the file descriptor is closed.When creating a new SonicBoom instance, you can pass an options object to control buffering, synchronization, and file handling behavior.
| Option | Description |
|---|---|
fd | A file descriptor (e.g., from fs.open). |
dest | A string path to a file. |
minLength | Minimum internal buffer length required before flushing. |
maxLength | Maximum internal buffer length. If exceeded, data is dropped and a 'drop' event is emitted. |
maxWrite | Maximum number of bytes that can be written (default: 16384). |
periodicFlush | Calls flush every x ms. |
sync | Perform writes synchronously (similar to console.log). |
fsync | Perform an fsyncSync every time a write is completed. |
append | Appends to dest instead of truncating (default: true). |
mode | Specify the creating file mode (Node.js fs.open style). |
contentMode | Type of data for write(): 'utf8' or 'buffer' (default: 'utf8'). |
mkdir | Ensure directory for dest exists (default: false). |
retryEAGAIN | Function (err, writeBufferLen, remainingBufferLen) => boolean called on EAGAIN or EBUSY errors. Return true to retry. |
maxWriteRetries | Max consecutive EAGAIN/EBUSY retries before giving up (default: 0, which is unbounded). |
To use SonicBoom, instantiate it with either a file descriptor (fd) or a destination path (dest). You can then use the .write() method to send strings to the stream.
'use strict'
const SonicBoom = require('sonic-boom')
const sonic = new SonicBoom({ fd: process.stdout.fd }) // or { dest: '/path/to/destination' }
for (let i = 0; i < 10; i++) {
sonic.write('hello sonic\n')
}SonicBoom emits several events for lifecycle and error management:
'close': Emitted when the instance has been closed.'drain': Emitted when the source can resume sending data.'drop' <any>: Emitted with the dropped data when the destination file reaches maxLength.'error' <Error>: Emitted when the destination file cannot be opened or written.'finish': Emitted after calling .end() and all data has been written.'ready': Occurs when the instance is ready to process input. (For sync: false, this is emitted when a file descriptor is available. For sync: true, it fires immediately upon creation).'write' <number>: Emitted every time data is written, emitting the number of bytes written.The SonicBoom class is used for high-performance UTF-8 stream writing to files or file descriptors. You can initialize it by passing a file path (string) or a file descriptor (number) in the options object.
Key configuration options:
dest or fd: The file path (string) or file descriptor (number) to write to.sync: Boolean. If true, uses synchronous file operations. Note that in sync mode, ready and error events are emitted via process.nextTick.append: Boolean (default true). If true, opens the file in append mode (a); otherwise, write mode (w).mkdir: Boolean (default false). If true, creates the directory for the file if it doesn't exist.minLength: Number. The minimum amount of data to buffer before triggering a write.maxLength: Number. The maximum amount of data to buffer before dropping new writes (emits drop event).maxWrite: Number (default 16384). The maximum size of a single write operation.periodicFlush: Number. Interval in milliseconds to automatically call flush().contentMode: 'utf8' or 'buffer'. Use 'buffer' to write raw Buffers directly, avoiding string conversion overhead.fsync: Boolean. If true, calls fsync after writes.const SonicBoom = require('sonic-boom');
// Writing to a file with directory creation and append mode
const sonic = new SonicBoom({ dest: './logs/app.log', mkdir: true, append: true });
// Writing to a file descriptor
const sonicFd = new SonicBoom({ fd: 1 });minLength is zero or if it is already writing. The callback cb is called when the flush operation completes or fails with an error.false to signal the producer to slow down (backpressure).Reopens the file in place. This is useful for implementing log rotation.
const stream = new SonicBoom({ dest: './my.log' })
process.on('SIGUSR2', function () {
stream.reopen()
})