1 |
3a515b92
|
cagy
|
# from2 [![Flattr this!](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=hughskennedy&url=http://github.com/hughsk/from2&title=from2&description=hughsk/from2%20on%20GitHub&language=en_GB&tags=flattr,github,javascript&category=software)[![experimental](http://hughsk.github.io/stability-badges/dist/experimental.svg)](http://github.com/hughsk/stability-badges) #
|
2 |
|
|
|
3 |
|
|
`from2` is a high-level module for creating readable streams that properly handle backpressure.
|
4 |
|
|
|
5 |
|
|
Convience wrapper for
|
6 |
|
|
[readable-stream](http://github.com/isaacs/readable-stream)'s `ReadableStream`
|
7 |
|
|
base class, with an API lifted from
|
8 |
|
|
[from](http://github.com/dominictarr/from) and
|
9 |
|
|
[through2](http://github.com/rvagg/through2).
|
10 |
|
|
|
11 |
|
|
## Usage ##
|
12 |
|
|
|
13 |
|
|
[![from2](https://nodei.co/npm/from2.png?mini=true)](https://nodei.co/npm/from2)
|
14 |
|
|
|
15 |
|
|
### `stream = from2([opts], read)` ###
|
16 |
|
|
|
17 |
|
|
Where `opts` are the options to pass on to the `ReadableStream` constructor,
|
18 |
|
|
and `read(size, next)` is called when data is requested from the stream.
|
19 |
|
|
|
20 |
|
|
* `size` is the recommended amount of data (in bytes) to retrieve.
|
21 |
|
|
* `next(err)` should be called when you're ready to emit more data.
|
22 |
|
|
|
23 |
|
|
For example, here's a readable stream that emits the contents of a given
|
24 |
|
|
string:
|
25 |
|
|
|
26 |
|
|
``` javascript
|
27 |
|
|
var from = require('from2')
|
28 |
|
|
|
29 |
|
|
function fromString(string) {
|
30 |
|
|
return from(function(size, next) {
|
31 |
|
|
// if there's no more content
|
32 |
|
|
// left in the string, close the stream.
|
33 |
|
|
if (string.length <= 0) return next(null, null)
|
34 |
|
|
|
35 |
|
|
// Pull in a new chunk of text,
|
36 |
|
|
// removing it from the string.
|
37 |
|
|
var chunk = string.slice(0, size)
|
38 |
|
|
string = string.slice(size)
|
39 |
|
|
|
40 |
|
|
// Emit "chunk" from the stream.
|
41 |
|
|
next(null, chunk)
|
42 |
|
|
})
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
// pipe "hello world" out
|
46 |
|
|
// to stdout.
|
47 |
|
|
fromString('hello world').pipe(process.stdout)
|
48 |
|
|
```
|
49 |
|
|
|
50 |
|
|
### `stream = from2.obj([opts], read)` ###
|
51 |
|
|
|
52 |
|
|
Shorthand for `from2({ objectMode: true }, read)`.
|
53 |
|
|
|
54 |
|
|
### `createStream = from2.ctor([opts], read)` ###
|
55 |
|
|
|
56 |
|
|
If you're creating similar streams in quick succession you can improve
|
57 |
|
|
performance by generating a stream **constructor** that you can reuse instead
|
58 |
|
|
of creating one-off streams on each call.
|
59 |
|
|
|
60 |
|
|
Takes the same options as `from2`, instead returning a constructor which you
|
61 |
|
|
can use to create new streams.
|
62 |
|
|
|
63 |
|
|
### See Also
|
64 |
|
|
|
65 |
|
|
- [from2-array](https://github.com/binocarlos/from2-array) - Create a from2 stream based on an array of source values.
|
66 |
|
|
- [from2-string](https://github.com/yoshuawuyts/from2-string) - Create a stream from a string. Sugary wrapper around from2.
|
67 |
|
|
|
68 |
|
|
## License ##
|
69 |
|
|
|
70 |
|
|
MIT. See [LICENSE.md](http://github.com/hughsk/from2/blob/master/LICENSE.md) for details.
|