ia-stream
Streams in Node.js don't have the ability to seek; and you can only read the data in them once. This is majorly inconvient for operations like random access to a file.
The obvious solution is to just use fs
methods directly, but this isn't ideal either — what if you need an abstraction where the data behind the stream can come from a place other than a file?
This package provides streams that are modeled after Stream
s in .NET, allowing random access / seeking, reading data more than once, and providing a Promise
-based API.
API
Docs generated using docts
Class
FileStream
Source code:
<>
Methods:
new( ) ⇒
FileStream
<>
▪ fdFileHandle
▪ optionsFileStreamOptions
.seek( ) ⇒Promise<boolean>
<>
Changes the position of the next read in the stream.
▪ positionnumber
.read( ) ⇒Promise<Buffer>
<>
Reads some bytes from the stream.
▪ lengthnumber
▫ exact?undefined | true | false
.readFrom( ) ⇒Promise<Buffer>
<>
Combinesseek
andread
.
▪ positionnumber
▪ lengthnumber
▫ exact?undefined | true | false
.write( ) ⇒Promise<number>
<>
Writes some data to the stream.
▪ dataBuffer
.resize( ) ⇒Promise<boolean>
<>
Resizes the source of this stream to a given number of bytes. Streams that
cannot be written to cannot be resized.
▪ lengthnumber
.close( ) ⇒Promise<void>
<>
Closes this stream. Once it is closed, any underlying resources (memory buffers,
file handles) will be freed and thisStream
will no longer be usable.
.substream( ) ⇒Promise<Stream>
<>
Creates an independentStream
that operates on the same source material as this stream.
This sub-stream will be read-only.
.substream( ) ⇒Promise<Stream>
<>
Creates an independentStream
that operates on the same source material as this stream.
This sub-stream will be read-only.
▪ fromnumber
▪ tonumber
Class
MemoryStream
Source code:
<>
Methods:
new( ) ⇒
MemoryStream
<>
▪ optionsMemoryStreamOptionsWithData | MemoryStreamOptionsWithLength
.seek( ) ⇒Promise<boolean>
<>
Changes the position of the next read in the stream.
▪ positionnumber
.read( ) ⇒Promise<Buffer>
<>
Reads some bytes from the stream.
▪ lengthnumber
▫ exact?undefined | true | false
.readFrom( ) ⇒Promise<Buffer>
<>
Combinesseek
andread
.
▪ positionnumber
▪ lengthnumber
▫ exact?undefined | true | false
.write( ) ⇒Promise<number>
<>
Writes some data to the stream.
▪ dataBuffer
.resize( ) ⇒Promise<boolean>
<>
Resizes the source of this stream to a given number of bytes. Streams that
cannot be written to cannot be resized.
▪ lengthnumber
.close( ) ⇒Promise<void>
<>
Closes this stream. Once it is closed, any underlying resources (memory buffers,
file handles) will be freed and thisStream
will no longer be usable.
.substream( ) ⇒Promise<Stream>
<>
Creates an independentStream
that operates on the same source material as this stream.
This sub-stream will be read-only.
.substream( ) ⇒Promise<Stream>
<>
Creates an independentStream
that operates on the same source material as this stream.
This sub-stream will be read-only.
▪ fromnumber
▪ tonumber
Interface
Stream
Source code:
<>
Methods:
.seek( ) ⇒
Promise<boolean>
<>
Changes the position of the next read in the stream.
▪ positionnumber
The position to set the cursor to.
.read( ) ⇒Promise<Buffer>
<>
Reads some bytes from the stream.
▪ lengthnumber
The maximum number of bytes to return.
▫ exact?undefined | true | false
Iftrue
, then this method will block untillength
bytes are available or fail..readFrom( ) ⇒
Promise<Buffer>
<>
Combinesseek
andread
.
▪ positionnumber
The starting position to read from.
▪ lengthnumber
The maximum number of bytes to return.
▫ exact?undefined | true | false
Iftrue
, then this method will block untillength
bytes are available or fail..write( ) ⇒
Promise<number>
<>
Writes some data to the stream.
▪ dataBuffer
ABuffer
containing the data to write.
.resize( ) ⇒Promise<boolean>
<>
Resizes the source of this stream to a given number of bytes. Streams that
cannot be written to cannot be resized.
▪ lengthnumber
The new length of this stream.
.close( ) ⇒Promise<void>
<>
Closes this stream. Once it is closed, any underlying resources (memory buffers,
file handles) will be freed and thisStream
will no longer be usable.
.substream( ) ⇒Promise<Stream>
<>
Creates an independentStream
that operates on the same source material as this stream.
This sub-stream will be read-only.
.substream( ) ⇒Promise<Stream>
<>
▪ fromnumber
▪ tonumber
.asNodeStream( ) ⇒Duplex
<>
Converts this stream into a Node.js-style stream.Properties:
.position
number
The current position of the cursor in the stream.
.lengthnumber
The length of the stream, in bytes.
.canWriteboolean
true
if this stream can be written to.
.canReadboolean
true
if this stream can be read from.
.canSeekboolean
true
if this stream can be seeked within.
.isOpenboolean
true
if this stream is currently open.
© 2019 Ibiyemi Abiodun (laptou) <ibiyemi.a.abiodun@gmail.com>