@ -50,9 +50,9 @@ Almost all Node.js programs, no matter how simple, use Streams in some
way. Here is an example of using Streams in an Node.js program:
way. Here is an example of using Streams in an Node.js program:
```javascript
```javascript
var http = require('http');
const http = require('http');
var server = http.createServer(function (req, res) {
var server = http.createServer( (req, res) => {
// req is an http.IncomingMessage, which is a Readable Stream
// req is an http.IncomingMessage, which is a Readable Stream
// res is an http.ServerResponse, which is a Writable Stream
// res is an http.ServerResponse, which is a Writable Stream
@ -62,18 +62,18 @@ var server = http.createServer(function (req, res) {
req.setEncoding('utf8');
req.setEncoding('utf8');
// Readable streams emit 'data' events once a listener is added
// Readable streams emit 'data' events once a listener is added
req.on('data', function (chunk) {
req.on('data', (chunk) => {
body += chunk;
body += chunk;
});
});
// the end event tells you that you have entire body
// the end event tells you that you have entire body
req.on('end', function () {
req.on('end', () => {
try {
try {
var data = JSON.parse(body);
var data = JSON.parse(body);
} catch (er) {
} catch (er) {
// uh oh! bad json!
// uh oh! bad json!
res.statusCode = 400;
res.statusCode = 400;
return res.end('error: ' + er.message );
return res.end(`error: ${er.message}` );
}
}
// write back something interesting to the user:
// write back something interesting to the user:
@ -176,7 +176,7 @@ possible, this is the best way to do so.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.on('data', function (chunk) {
readable.on('data', (chunk) => {
console.log('got %d bytes of data', chunk.length);
console.log('got %d bytes of data', chunk.length);
});
});
```
```
@ -191,10 +191,10 @@ or by calling `read()` repeatedly until you get to the end.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.on('data', function (chunk) {
readable.on('data', (chunk) => {
console.log('got %d bytes of data', chunk.length);
console.log('got %d bytes of data', chunk.length);
});
});
readable.on('end', function () {
readable.on('end', () => {
console.log('there will be no more data.');
console.log('there will be no more data.');
});
});
```
```
@ -216,7 +216,7 @@ hadn't already.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.on('readable', function () {
readable.on('readable', () => {
// there is some data to read now
// there is some data to read now
});
});
```
```
@ -234,12 +234,12 @@ In the former case, `.read()` will return that data. In the latter case,
is an empty file:
is an empty file:
```javascript
```javascript
var fs = require('fs');
const fs = require('fs');
var rr = fs.createReadStream('foo.txt');
var rr = fs.createReadStream('foo.txt');
rr.on('readable', function () {
rr.on('readable', () => {
console.log('readable:', rr.read());
console.log('readable:', rr.read());
});
});
rr.on('end', function () {
rr.on('end', () => {
console.log('end');
console.log('end');
});
});
```
```
@ -280,11 +280,11 @@ available will remain in the internal buffer.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.on('data', function (chunk) {
readable.on('data', (chunk) => {
console.log('got %d bytes of data', chunk.length);
console.log('got %d bytes of data', chunk.length);
readable.pause();
readable.pause();
console.log('there will be no more data for 1 second');
console.log('there will be no more data for 1 second');
setTimeout(function () {
setTimeout(() => {
console.log('now data will start flowing again');
console.log('now data will start flowing again');
readable.resume();
readable.resume();
}, 1000);
}, 1000);
@ -335,7 +335,7 @@ end.
```javascript
```javascript
reader.pipe(writer, { end: false });
reader.pipe(writer, { end: false });
reader.on('end', function () {
reader.on('end', () => {
writer.end('Goodbye\n');
writer.end('Goodbye\n');
});
});
```
```
@ -366,7 +366,7 @@ drained.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.on('readable', function () {
readable.on('readable', () => {
var chunk;
var chunk;
while (null !== (chunk = readable.read())) {
while (null !== (chunk = readable.read())) {
console.log('got %d bytes of data', chunk.length);
console.log('got %d bytes of data', chunk.length);
@ -395,7 +395,7 @@ data.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.resume();
readable.resume();
readable.on('end', function () {
readable.on('end', () => {
console.log('got to the end, but did not read anything');
console.log('got to the end, but did not read anything');
});
});
```
```
@ -420,7 +420,7 @@ as strings, always use this method.
```javascript
```javascript
var readable = getReadableStreamSomehow();
var readable = getReadableStreamSomehow();
readable.setEncoding('utf8');
readable.setEncoding('utf8');
readable.on('data', function (chunk) {
readable.on('data', (chunk) => {
assert.equal(typeof chunk, 'string');
assert.equal(typeof chunk, 'string');
console.log('got %d characters of string data', chunk.length);
console.log('got %d characters of string data', chunk.length);
});
});
@ -443,7 +443,7 @@ var writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt',
// All the data from readable goes into 'file.txt',
// but only for the first second
// but only for the first second
readable.pipe(writable);
readable.pipe(writable);
setTimeout(function () {
setTimeout(() => {
console.log('stop writing to file.txt');
console.log('stop writing to file.txt');
readable.unpipe(writable);
readable.unpipe(writable);
console.log('manually close the file stream');
console.log('manually close the file stream');
@ -471,7 +471,7 @@ for Stream Implementors, below.)
// Pull off a header delimited by \n\n
// Pull off a header delimited by \n\n
// use unshift() if we get too much
// use unshift() if we get too much
// Call the callback with (error, header, stream)
// Call the callback with (error, header, stream)
var StringDecoder = require('string_decoder').StringDecoder;
const StringDecoder = require('string_decoder').StringDecoder;
function parseHeader(stream, callback) {
function parseHeader(stream, callback) {
stream.on('error', callback);
stream.on('error', callback);
stream.on('readable', onReadable);
stream.on('readable', onReadable);
@ -528,12 +528,12 @@ as a convenience for interacting with old Node.js programs and libraries.
For example:
For example:
```javascript
```javascript
var OldReader = require('./old-api-module.js').OldReader;
const OldReader = require('./old-api-module.js').OldReader;
var oreader = new OldReader ;
const Readable = require('stream').Readable ;
var Readable = require('stream').Readable ;
const oreader = new OldReader ;
var myReader = new Readable().wrap(oreader);
const myReader = new Readable().wrap(oreader);
myReader.on('readable', function () {
myReader.on('readable', () => {
myReader.read(); // etc.
myReader.read(); // etc.
});
});
```
```
@ -615,10 +615,10 @@ to the underlying system, this event is emitted.
```javascript
```javascript
var writer = getWritableStreamSomehow();
var writer = getWritableStreamSomehow();
for (var i = 0; i < 100 ; i + + ) {
for (var i = 0; i < 100 ; i + + ) {
writer.write('hello, #' + i + ' !\n');
writer.write('hello, #${i} !\n');
}
}
writer.end('this is the end\n');
writer.end('this is the end\n');
writer.on('finish', function () {
writer.on('finish', () => {
console.error('all writes are now complete.');
console.error('all writes are now complete.');
});
});
```
```
@ -633,7 +633,7 @@ stream, adding this writable to its set of destinations.
```javascript
```javascript
var writer = getWritableStreamSomehow();
var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('pipe', function (src) {
writer.on('pipe', (src) => {
console.error('something is piping into the writer');
console.error('something is piping into the writer');
assert.equal(src, reader);
assert.equal(src, reader);
});
});
@ -650,7 +650,7 @@ readable stream, removing this writable from its set of destinations.
```javascript
```javascript
var writer = getWritableStreamSomehow();
var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('unpipe', function (src) {
writer.on('unpipe', (src) => {
console.error('something has stopped piping into the writer');
console.error('something has stopped piping into the writer');
assert.equal(src, reader);
assert.equal(src, reader);
});
});
@ -955,8 +955,8 @@ This is a basic example of a Readable stream. It emits the numerals
from 1 to 1,000,000 in ascending order, and then ends.
from 1 to 1,000,000 in ascending order, and then ends.
```javascript
```javascript
var Readable = require('stream').Readable;
const Readable = require('stream').Readable;
var util = require('util');
const util = require('util');
util.inherits(Counter, Readable);
util.inherits(Counter, Readable);
function Counter(opt) {
function Counter(opt) {
@ -995,8 +995,8 @@ below for a better implementation.
// Using Readable directly for this is sub-optimal. See the
// Using Readable directly for this is sub-optimal. See the
// alternative example below under the Transform section.
// alternative example below under the Transform section.
var Readable = require('stream').Readable;
const Readable = require('stream').Readable;
var util = require('util');
const util = require('util');
util.inherits(SimpleProtocol, Readable);
util.inherits(SimpleProtocol, Readable);
@ -1012,13 +1012,13 @@ function SimpleProtocol(source, options) {
this._source = source;
this._source = source;
var self = this;
var self = this;
source.on('end', function () {
source.on('end', () => {
self.push(null);
self.push(null);
});
});
// give it a kick whenever the source is readable
// give it a kick whenever the source is readable
// read(0) will not consume any bytes
// read(0) will not consume any bytes
source.on('readable', function () {
source.on('readable', () => {
self.read(0);
self.read(0);
});
});
@ -1210,8 +1210,8 @@ would be piped into the parser, which is a more idiomatic Node.js stream
approach.
approach.
```javascript
```javascript
var util = require('util');
const util = require('util');
var Transform = require('stream').Transform;
const Transform = require('stream').Transform;
util.inherits(SimpleProtocol, Transform);
util.inherits(SimpleProtocol, Transform);
function SimpleProtocol(options) {
function SimpleProtocol(options) {
@ -1518,10 +1518,10 @@ For example, consider the following code:
```javascript
```javascript
// WARNING! BROKEN!
// WARNING! BROKEN!
net.createServer(function (socket) {
net.createServer((socket) => {
// we add an 'end' method, but never consume the data
// we add an 'end' method, but never consume the data
socket.on('end', function () {
socket.on('end', () => {
// It will never get here.
// It will never get here.
socket.end('I got your message (but didnt read it)\n');
socket.end('I got your message (but didnt read it)\n');
});
});
@ -1538,9 +1538,9 @@ start the flow of data:
```javascript
```javascript
// Workaround
// Workaround
net.createServer(function (socket) {
net.createServer((socket) => {
socket.on('end', function () {
socket.on('end', () => {
socket.end('I got your message (but didnt read it)\n');
socket.end('I got your message (but didnt read it)\n');
});
});
@ -1589,9 +1589,9 @@ respectively. These options can be used to implement parsers and
serializers with Transform streams.
serializers with Transform streams.
```javascript
```javascript
var util = require('util');
const util = require('util');
var StringDecoder = require('string_decoder').StringDecoder;
const StringDecoder = require('string_decoder').StringDecoder;
var Transform = require('stream').Transform;
const Transform = require('stream').Transform;
util.inherits(JSONParseStream, Transform);
util.inherits(JSONParseStream, Transform);
// Gets \n-delimited JSON string data, and emits the parsed objects
// Gets \n-delimited JSON string data, and emits the parsed objects