What is @isaacs/fs-minipass?
@isaacs/fs-minipass is a minimalistic file system streaming library for Node.js. It provides a simplified interface for reading and writing files using streams, making it easier to handle file operations in an efficient and non-blocking manner.
What are @isaacs/fs-minipass's main functionalities?
Reading Files
This feature allows you to read files using a stream. The code sample demonstrates how to create a ReadStream to read the contents of 'example.txt' and log each chunk of data to the console.
const { ReadStream } = require('@isaacs/fs-minipass');
const fs = require('fs');
const readStream = new ReadStream('example.txt');
readStream.on('data', (chunk) => {
console.log('Read chunk:', chunk.toString());
});
readStream.on('end', () => {
console.log('Finished reading file');
});
Writing Files
This feature allows you to write data to files using a stream. The code sample demonstrates how to create a WriteStream to write 'Hello, world!' to 'output.txt' and log a message when the writing is finished.
const { WriteStream } = require('@isaacs/fs-minipass');
const fs = require('fs');
const writeStream = new WriteStream('output.txt');
writeStream.write('Hello, world!');
writeStream.end();
writeStream.on('finish', () => {
console.log('Finished writing to file');
});
Piping Streams
This feature allows you to pipe data from a read stream to a write stream, effectively copying the contents of one file to another. The code sample demonstrates how to read from 'example.txt' and write to 'copy.txt' using streams.
const { ReadStream, WriteStream } = require('@isaacs/fs-minipass');
const fs = require('fs');
const readStream = new ReadStream('example.txt');
const writeStream = new WriteStream('copy.txt');
readStream.pipe(writeStream);
writeStream.on('finish', () => {
console.log('Finished copying file');
});
Other packages similar to @isaacs/fs-minipass
fs-extra
fs-extra is a popular library that extends the native Node.js fs module with additional methods and promises support. It provides a higher-level API for file system operations, including copying, moving, and removing files and directories. Compared to @isaacs/fs-minipass, fs-extra offers a broader range of file system utilities but does not focus specifically on streaming.
stream
The stream module is a core Node.js module that provides a foundation for working with streaming data. It includes classes for readable, writable, duplex, and transform streams. While @isaacs/fs-minipass is built on top of the stream module and offers a simplified interface for file system streams, the stream module itself provides more general-purpose streaming capabilities.
through2
through2 is a small and simple utility for creating transform streams in Node.js. It allows you to easily create streams that can modify or transform data as it passes through. While @isaacs/fs-minipass focuses on file system streams, through2 is more general-purpose and can be used for a variety of streaming data transformations.
fs-minipass
Filesystem streams based on minipass.
4 classes are exported:
- ReadStream
- ReadStreamSync
- WriteStream
- WriteStreamSync
When using ReadStreamSync
, all of the data is made available
immediately upon consuming the stream. Nothing is buffered in memory
when the stream is constructed. If the stream is piped to a writer,
then it will synchronously read()
and emit data into the writer as
fast as the writer can consume it. (That is, it will respect
backpressure.) If you call stream.read()
then it will read the
entire file and return the contents.
When using WriteStreamSync
, every write is flushed to the file
synchronously. If your writes all come in a single tick, then it'll
write it all out in a single tick. It's as synchronous as you are.
The async versions work much like their node builtin counterparts,
with the exception of introducing significantly less Stream machinery
overhead.
USAGE
It's just streams, you pipe them or read() them or write() to them.
import { ReadStream, WriteStream } from 'fs-minipass'
const readStream = new ReadStream('file.txt')
const writeStream = new WriteStream('output.txt')
writeStream.write('some file header or whatever\n')
readStream.pipe(writeStream)
ReadStream(path, options)
Path string is required, but somewhat irrelevant if an open file
descriptor is passed in as an option.
Options:
fd
Pass in a numeric file descriptor, if the file is already open.readSize
The size of reads to do, defaults to 16MBsize
The size of the file, if known. Prevents zero-byte read()
call at the end.autoClose
Set to false
to prevent the file descriptor from being
closed when the file is done being read.
WriteStream(path, options)
Path string is required, but somewhat irrelevant if an open file
descriptor is passed in as an option.
Options:
fd
Pass in a numeric file descriptor, if the file is already open.mode
The mode to create the file with. Defaults to 0o666
.start
The position in the file to start reading. If not
specified, then the file will start writing at position zero, and be
truncated by default.autoClose
Set to false
to prevent the file descriptor from being
closed when the stream is ended.flags
Flags to use when opening the file. Irrelevant if fd
is
passed in, since file won't be opened in that case. Defaults to
'a'
if a pos
is specified, or 'w'
otherwise.