example.js 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. var stream = require("readable-stream");
  3. var duplexer2 = require("./");
  4. var writable = new stream.Writable({objectMode: true}),
  5. readable = new stream.Readable({objectMode: true});
  6. writable._write = function _write(input, encoding, done) {
  7. if (readable.push(input)) {
  8. return done();
  9. } else {
  10. readable.once("drain", done);
  11. }
  12. };
  13. readable._read = function _read(n) {
  14. // no-op
  15. };
  16. // simulate the readable thing closing after a bit
  17. writable.once("finish", function() {
  18. setTimeout(function() {
  19. readable.push(null);
  20. }, 500);
  21. });
  22. var duplex = duplexer2(writable, readable);
  23. duplex.on("data", function(e) {
  24. console.log("got data", JSON.stringify(e));
  25. });
  26. duplex.on("finish", function() {
  27. console.log("got finish event");
  28. });
  29. duplex.on("end", function() {
  30. console.log("got end event");
  31. });
  32. duplex.write("oh, hi there", function() {
  33. console.log("finished writing");
  34. });
  35. duplex.end(function() {
  36. console.log("finished ending");
  37. });