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