• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3Like [duplexer2](https://github.com/deoxxa/duplexer2) but using Streams3 without readable-stream dependency
4
5```javascript
6var stream = require("stream");
7
8var duplexer3 = require("duplexer3");
9
10var writable = new stream.Writable({objectMode: true}),
11    readable = new stream.Readable({objectMode: true});
12
13writable._write = function _write(input, encoding, done) {
14  if (readable.push(input)) {
15    return done();
16  } else {
17    readable.once("drain", done);
18  }
19};
20
21readable._read = function _read(n) {
22  // no-op
23};
24
25// simulate the readable thing closing after a bit
26writable.once("finish", function() {
27  setTimeout(function() {
28    readable.push(null);
29  }, 500);
30});
31
32var duplex = duplexer3(writable, readable);
33
34duplex.on("data", function(e) {
35  console.log("got data", JSON.stringify(e));
36});
37
38duplex.on("finish", function() {
39  console.log("got finish event");
40});
41
42duplex.on("end", function() {
43  console.log("got end event");
44});
45
46duplex.write("oh, hi there", function() {
47  console.log("finished writing");
48});
49
50duplex.end(function() {
51  console.log("finished ending");
52});
53```
54
55```
56got data "oh, hi there"
57finished writing
58got finish event
59finished ending
60got end event
61```
62
63## Overview
64
65This is a reimplementation of [duplexer](https://www.npmjs.com/package/duplexer) using the
66Streams3 API which is standard in Node as of v4. Everything largely
67works 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
83Creates a new `DuplexWrapper` object, which is the actual class that implements
84most of the fun stuff. All that fun stuff is hidden. DON'T LOOK.
85
86```javascript
87duplexer3([options], writable, readable)
88```
89
90```javascript
91const duplex = duplexer3(new stream.Writable(), new stream.Readable());
92```
93
94Arguments
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
101Options
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
1093-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))
116