@@ -65,7 +65,7 @@ Resumes the incoming `'data'` events after a `pause()`.
6565
6666Closes the underlying file descriptor. Stream will not emit any more events.
6767
68- ### stream.pipe(destination, [ options] )
68+ ### stream.pipe(destination, [ options] , [ filter ] )
6969
7070This is a ` Stream.prototype ` method available on all ` Stream ` s.
7171
@@ -92,6 +92,48 @@ NOTE: If the source stream does not support `pause()` and `resume()`, this funct
9292adds simple definitions which simply emit ` 'pause' ` and ` 'resume' ` events on
9393the source stream.
9494
95+
96+ The ` filter ` argument is an optional callback which can be used to filter all
97+ data passing through the pipe. This makes it easy to do arbitrary transforms
98+ (like gzip) while still maintaining the proper throttling. ` filter ` gets
99+ three arguments: a buffer, a write function, and a done function. Here is an
100+ example of a chat which uses a ` filter ` to append each message with the
101+ address of the sender.
102+
103+ var net = require('net');
104+ var people = [];
105+
106+ function address(socket) {
107+ return '<' + socket.remoteAddress + ':' + socket.remotePort + '> ';
108+ }
109+
110+ net.Server(function (socket) {
111+ socket.write("hello!\r\n");
112+
113+ people.forEach(function (p) {
114+ socket.pipe(p, { end: false }, function (d, write, done) {
115+ write(address(socket));
116+ write(d);
117+ done();
118+ });
119+
120+ p.pipe(socket, { end: false }, function (d, write, done) {
121+ write(address(p));
122+ write(d);
123+ done();
124+ });
125+ });
126+
127+ people.push(socket);
128+
129+ socket.on('end', function () {
130+ people.splice(people.indexOf(socket), 1);
131+ });
132+ }).listen(8000);
133+
134+
135+
136+
95137## Writable Stream
96138
97139A ` Writable Stream ` has the following methods, members, and events.
0 commit comments