You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.6KB

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