UNPKG

demos

Version:

106 lines (75 loc) 5.16 kB
A stream is an abstract interface implemented by various objects in Node. For example a request to an HTTP server is a stream, as is stdout. Streams are readable, writable, or both. All streams are instances of EventEmitter You can load the Stream base classes by doing require('stream'). There are base classes provided for Readable streams, Writable streams, Duplex streams, and Transform streams. This document is split up into 3 sections. The first explains the parts of the API that you need to be aware of to use streams in your programs. If you never implement a streaming API yourself, you can stop there. The second section explains the parts of the API that you need to use if you implement your own custom streams yourself. The API is designed to make this easy for you to do. The third section goes into more depth about how streams work, including some of the internal mechanisms and functions that you should probably not modify unless you definitely know what you are doing. API for Stream Consumers# Streams can be either Readable, Writable, or both (Duplex). All streams are EventEmitters, but they also have other custom methods and properties depending on whether they are Readable, Writable, or Duplex. If a stream is both Readable and Writable, then it implements all of the methods and events below. So, a Duplex or Transform stream is fully described by this API, though their implementation may be somewhat different. It is not necessary to implement Stream interfaces in order to consume streams in your programs. If you are implementing streaming interfaces in your own program, please also refer to API for Stream Implementors below. Almost all Node programs, no matter how simple, use Streams in some way. Here is an example of using Streams in a Node program: var http = require('http'); var server = http.createServer(function (req, res) { // req is an http.IncomingMessage, which is a Readable Stream // res is an http.ServerResponse, which is a Writable Stream var body = ''; // we want to get the data as utf8 strings // If you don't set an encoding, then you'll get Buffer objects req.setEncoding('utf8'); // Readable streams emit 'data' events once a listener is added req.on('data', function (chunk) { body += chunk; }); // the end event tells you that you have entire body req.on('end', function () { try { var data = JSON.parse(body); } catch (er) { // uh oh! bad json! res.statusCode = 400; return res.end('error: ' + er.message); } // write back something interesting to the user: res.write(typeof data); res.end(); }); }); server.listen(1337); // $ curl localhost:1337 -d '{}' // object // $ curl localhost:1337 -d '"foo"' // string // $ curl localhost:1337 -d 'not json' // error: Unexpected token o Class: stream.Readable# The Readable stream interface is the abstraction for a source of data that you are reading from. In other words, data comes out of a Readable stream. A Readable stream will not start emitting data until you indicate that you are ready to receive it. Readable streams have two "modes": a flowing mode and a paused mode. When in flowing mode, data is read from the underlying system and provided to your program as fast as possible. In paused mode, you must explicitly call stream.read() to get chunks of data out. Streams start out in paused mode. Note: If no data event handlers are attached, and there are no pipe() destinations, and the stream is switched into flowing mode, then data will be lost. You can switch to flowing mode by doing any of the following: Adding a 'data' event handler to listen for data. Calling the resume() method to explicitly open the flow. Calling the pipe() method to send the data to a Writable. You can switch back to paused mode by doing either of the following: If there are no pipe destinations, by calling the pause() method. If there are pipe destinations, by removing any 'data' event handlers, and removing all pipe destinations by calling the unpipe() method. Note that, for backwards compatibility reasons, removing 'data' event handlers will not automatically pause the stream. Also, if there are piped destinations, then calling pause() will not guarantee that the stream will remain paused once those destinations drain and ask for more data. Examples of readable streams include: http responses, on the client http requests, on the server fs read streams zlib streams crypto streams tcp sockets child process stdout and stderr process.stdin Event: 'readable'# When a chunk of data can be read from the stream, it will emit a 'readable' event. In some cases, listening for a 'readable' event will cause some data to be read into the internal buffer from the underlying system, if it hadn't already. var readable = getReadableStreamSomehow(); readable.on('readable', function() { // there is some data to read now }); Once the internal buffer is drained, a readable event will fire again when more data is available. Event: 'data'#