@ocubist/file-stream-manager
TypeScript icon, indicating that this package has built-in type declarations

0.4.13 • Public • Published

File Stream Manager

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.

Installation

npm install @ocubist/file-stream-manager

Usage

Example

Here's a quick tutorial on how to use the File Stream Manager to manage your file streams:

  1. 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);
  1. 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!");
  1. 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.`);
});
  1. 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);
  1. 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);
  1. 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.");
});
  1. 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);
  1. 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();
  1. 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);

Readme

Keywords

none

Package Sidebar

Install

npm i @ocubist/file-stream-manager

Weekly Downloads

319

Version

0.4.13

License

MIT

Unpacked Size

177 kB

Total Files

117

Last publish

Collaborators

  • ocubist