from2
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
inherits | 2.0.4 | 1.98 kB | ISC | prod | |
readable-stream | 2.3.8 | 25.14 kB | MIT | prod |
From2 is a high-level JavaScript module that aids in creating readable streams with an excellent ability to handle backpressure. Taking its API from 'from' and 'through2', it acts as a convenient wrapper for 'ReadableStream's base class from 'readable-stream'. The primary use of from2 is to generate data on request, effectively managing memory and data flow by adhering to backpressure rules.
From2 is easy to use, requiring just a few steps to set up. Once the package is installed, it can be used by importing into a JavaScript file, invoking the from2 function and defining the read(size, next) function which will be called when data is requested from the stream. An example of its usage in code is given below:
var from = require('from2');
function fromString(string) {
return from(function(size, next) {
if (string.length <= 0) return next(null, null) // close the stream if there's no more content left
var chunk = string.slice(0, size) // retrieve a new chunk of text
string = string.slice(size) // remove it from the string
next(null, chunk) // emit "chunk" from the stream
})
}
fromString('hello world').pipe(process.stdout) // pipe "hello world" to stdout
This shows how you can create a readable stream from a string. You can easily modify the fromString
function for different uses.
Besides this, from2 also provides shorthand for object mode streams and a way to create a stream constructor for creating similar streams repeatedly, which improves performance.
The documentation for the from2 module is present within the README.md file of the module's Git repository (http://github.com/hughsk/from2). The documentation provides essential details about the library, the API, and ways to utilize it, along with code examples. The README.md also includes important notes about the usage of the library and links to related projects. If you are starting with the from2 module, this should be your start point.