Initial commit
[yaffs-website] / node_modules / duplexer2 / README.md
1 duplexer2 [![build status](https://travis-ci.org/deoxxa/duplexer2.png)](https://travis-ci.org/deoxxa/fork)
2 =========
3
4 Like duplexer (http://npm.im/duplexer) but using streams2.
5
6 Overview
7 --------
8
9 duplexer2 is a reimplementation of [duplexer](http://npm.im/duplexer) using the
10 readable-stream API which is standard in node as of v0.10. Everything largely
11 works the same.
12
13 Installation
14 ------------
15
16 Available via [npm](http://npmjs.org/):
17
18 > $ npm install duplexer2
19
20 Or via git:
21
22 > $ git clone git://github.com/deoxxa/duplexer2.git node_modules/duplexer2
23
24 API
25 ---
26
27 **duplexer2**
28
29 Creates a new `DuplexWrapper` object, which is the actual class that implements
30 most of the fun stuff. All that fun stuff is hidden. DON'T LOOK.
31
32 ```javascript
33 duplexer2([options], writable, readable)
34 ```
35
36 ```javascript
37 var duplex = duplexer2(new stream.Writable(), new stream.Readable());
38 ```
39
40 Arguments
41
42 * __options__ - an object specifying the regular `stream.Duplex` options, as
43   well as the properties described below.
44 * __writable__ - a writable stream
45 * __readable__ - a readable stream
46
47 Options
48
49 * __bubbleErrors__ - a boolean value that specifies whether to bubble errors
50   from the underlying readable/writable streams. Default is `true`.
51
52 Example
53 -------
54
55 Also see [example.js](https://github.com/deoxxa/duplexer2/blob/master/example.js).
56
57 Code:
58
59 ```javascript
60 var stream = require("stream");
61
62 var duplexer2 = require("duplexer2");
63
64 var writable = new stream.Writable({objectMode: true}),
65     readable = new stream.Readable({objectMode: true});
66
67 writable._write = function _write(input, encoding, done) {
68   if (readable.push(input)) {
69     return done();
70   } else {
71     readable.once("drain", done);
72   }
73 };
74
75 readable._read = function _read(n) {
76   // no-op
77 };
78
79 // simulate the readable thing closing after a bit
80 writable.once("finish", function() {
81   setTimeout(function() {
82     readable.push(null);
83   }, 500);
84 });
85
86 var duplex = duplexer2(writable, readable);
87
88 duplex.on("data", function(e) {
89   console.log("got data", JSON.stringify(e));
90 });
91
92 duplex.on("finish", function() {
93   console.log("got finish event");
94 });
95
96 duplex.on("end", function() {
97   console.log("got end event");
98 });
99
100 duplex.write("oh, hi there", function() {
101   console.log("finished writing");
102 });
103
104 duplex.end(function() {
105   console.log("finished ending");
106 });
107 ```
108
109 Output:
110
111 ```
112 got data "oh, hi there"
113 finished writing
114 got finish event
115 finished ending
116 got end event
117 ```
118
119 License
120 -------
121
122 3-clause BSD. A copy is included with the source.
123
124 Contact
125 -------
126
127 * GitHub ([deoxxa](http://github.com/deoxxa))
128 * Twitter ([@deoxxa](http://twitter.com/deoxxa))
129 * Email ([deoxxa@fknsrs.biz](mailto:deoxxa@fknsrs.biz))