A utility library for managing file streams efficiently using a singleton pattern. This library provides functions to open, read, write, and manage file streams, ensuring that only one instance of each stream is active at any time.
npm install @ocubist/file-stream-manager
Here's a quick tutorial on how to use the File Stream Manager to manage your file streams:
- Open a File Stream
To open a file stream for writing, use the openFileStream function. This ensures that only one instance of the stream is active.
/**
* Opens a file stream and stores it in a singleton for shared access.
* @param filePath - Path to the file where the stream will write.
* @param options - Optional configuration options for the file stream.
* @returns The key of the singleton object for the opened stream.
*/
openFileStream(filePath, options);
- Write to a File Stream
Once the file stream is open, you can write data to it using the writeFileStream function.
import { writeFileStream } from "@ocubist/file-stream-manager";
// Write data to the file stream
writeFileStream(streamKey, "Hello, world!");
- Read from a File Stream
To read data from a file, use the readFileStream function which returns a readable stream.
import { readFileStream } from "@ocubist/file-stream-manager";
// Open a readable stream
const readStream = readFileStream("path/to/file.txt");
// Handle the data event
readStream.on("data", (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
- Subscribe to Stream Events
You can subscribe to various events on the file stream using the subscribeToFileStream function.
import { subscribeToFileStream } from "@ocubist/file-stream-manager";
// Subscribe to the 'finish' event
const listener = () => {
console.log("Stream finished.");
};
subscribeToFileStream(streamKey, "finish", listener);
- Unsubscribe from Stream Events
To unsubscribe from an event, use the unsubscribeFromFileStream function.
import { unsubscribeFromFileStream } from "@ocubist/file-stream-manager";
// Unsubscribe from the 'finish' event
unsubscribeFromFileStream(streamKey, "finish", listener);
- Flush a File Stream
If you need to flush the file stream, use the flushFileStream function.
import { flushFileStream } from "@ocubist/file-stream-manager";
// Flush the file stream
flushFileStream(streamKey).then(() => {
console.log("Stream flushed.");
});
- Close a File Stream
To close the file stream, use the closeFileStream function.
import { closeFileStream } from "@ocubist/file-stream-manager";
// Close the file stream
closeFileStream(streamKey);
- Force Close All File Streams
If you need to force close all open file streams, use the forceCloseOfAllFileStreams function.
import { forceCloseOfAllFileStreams } from "@ocubist/file-stream-manager";
// Force close all file streams
forceCloseOfAllFileStreams();
- Get All Active File Stream Keys
To get a list of all active file stream keys, use the getAllFileStreamSingletonKeys function.
import { getAllFileStreamSingletonKeys } from "@ocubist/file-stream-manager";
// Get all active file stream keys
const allKeys = getAllFileStreamSingletonKeys();
console.log("All stream keys:", allKeys);