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