Skip to content

Commit

Permalink
build for 7.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Nov 9, 2016
1 parent 69d13e3 commit b6f6aaf
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 107 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# readable-stream

***Node-core v6.5.0 streams for userland*** [![Build Status](https://travis-ci.org/nodejs/readable-stream.svg?branch=master)](https://travis-ci.org/nodejs/readable-stream)
***Node-core v7.0.0 streams for userland*** [![Build Status](https://travis-ci.org/nodejs/readable-stream.svg?branch=master)](https://travis-ci.org/nodejs/readable-stream)


[![NPM](https://nodei.co/npm/readable-stream.png?downloads=true&downloadRank=true)](https://nodei.co/npm/readable-stream/)
Expand Down
5 changes: 3 additions & 2 deletions build/build.js
Expand Up @@ -55,7 +55,8 @@ function processFile (inputLoc, out, replacements) {
'transform-es2015-block-scoping',
'transform-es2015-template-literals',
'transform-es2015-shorthand-properties',
'transform-es2015-for-of'
'transform-es2015-for-of',
'transform-es2015-destructuring'
]
})
data = transformed.code
Expand Down Expand Up @@ -104,7 +105,7 @@ hyperquest(testlisturl).pipe(bl(function (err, data) {

$('table.files .js-navigation-open').each(function () {
var file = $(this).text()
if (/^test-stream/.test(file) && !/-wrap(?:-encoding)?\.js$/.test(file) && file !== 'test-stream2-httpclient-response-end.js' && file !== 'test-stream-base-no-abort.js' && file !== 'test-stream-preprocess.js')
if (/^test-stream/.test(file) && !/-wrap(?:-encoding)?\.js$/.test(file) && file !== 'test-stream2-httpclient-response-end.js' && file !== 'test-stream-base-no-abort.js' && file !== 'test-stream-preprocess.js' && file !== 'test-stream-inheritance.js')
processTestFile(file)
})
}))
Expand Down
1 change: 1 addition & 0 deletions build/package.json
Expand Up @@ -7,6 +7,7 @@
"babel-core": "^6.5.2",
"babel-plugin-transform-es2015-arrow-functions": "^6.5.2",
"babel-plugin-transform-es2015-block-scoping": "^6.5.0",
"babel-plugin-transform-es2015-destructuring": "^6.18.0",
"babel-plugin-transform-es2015-for-of": "^6.8.0",
"babel-plugin-transform-es2015-parameters": "^6.11.4",
"babel-plugin-transform-es2015-shorthand-properties": "^6.8.0",
Expand Down
68 changes: 36 additions & 32 deletions doc/stream.md
Expand Up @@ -19,14 +19,14 @@ The `stream` module can be accessed using:
const stream = require('stream');
```

While it is important for all Node.js users to understand how streams works,
While it is important for all Node.js users to understand how streams work,
the `stream` module itself is most useful for developers that are creating new
types of stream instances. Developer's who are primarily *consuming* stream
objects will rarely (if ever) have need to use the `stream` module directly.

## Organization of this document
## Organization of this Document

This document is divided into two primary sections and third section for
This document is divided into two primary sections with a third section for
additional notes. The first section explains the elements of the stream API that
are required to *use* streams within an application. The second section explains
the elements of the API that are required to *implement* new types of streams.
Expand All @@ -48,7 +48,7 @@ There are four fundamental stream types within Node.js:

All streams created by Node.js APIs operate exclusively on strings and `Buffer`
objects. It is possible, however, for stream implementations to work with other
types of JavaScript values (with the exception of `null` which serves a special
types of JavaScript values (with the exception of `null`, which serves a special
purpose within streams). Such streams are considered to operate in "object
mode".

Expand Down Expand Up @@ -83,11 +83,11 @@ used to fill the read buffer).
Data is buffered in Writable streams when the
[`writable.write(chunk)`][stream-write] method is called repeatedly. While the
total size of the internal write buffer is below the threshold set by
`highWaterMark`, calls to `writable.write()` will return `true`. Once the
`highWaterMark`, calls to `writable.write()` will return `true`. Once
the size of the internal buffer reaches or exceeds the `highWaterMark`, `false`
will be returned.

A key goal of the `stream` API, and in particular the [`stream.pipe()`] method,
A key goal of the `stream` API, particularly the [`stream.pipe()`] method,
is to limit the buffering of data to acceptable levels such that sources and
destinations of differing speeds will not overwhelm the available memory.

Expand All @@ -98,8 +98,8 @@ appropriate and efficient flow of data. For example, [`net.Socket`][] instances
are [Duplex][] streams whose Readable side allows consumption of data received
*from* the socket and whose Writable side allows writing data *to* the socket.
Because data may be written to the socket at a faster or slower rate than data
is received, it is important each side operate (and buffer) independently of
the other.
is received, it is important for each side to operate (and buffer) independently
of the other.

## API for Stream Consumers

Expand Down Expand Up @@ -963,10 +963,11 @@ function parseHeader(stream, callback) {
header += split.shift();
const remaining = split.join('\n\n');
const buf = Buffer.from(remaining, 'utf8');
if (buf.length)
stream.unshift(buf);
stream.removeListener('error', callback);
// set the readable listener before unshifting
stream.removeListener('readable', onReadable);
if (buf.length)
stream.unshift(buf);
// now the body of the message can be read from the stream.
callback(null, header, stream);
} else {
Expand Down Expand Up @@ -1060,7 +1061,7 @@ Examples of Transform streams include:
<!--type=misc-->

The `stream` module API has been designed to make it possible to easily
implement streams using JavaScript's prototypical inheritance model.
implement streams using JavaScript's prototypal inheritance model.

First, a stream developer would declare a new JavaScript class that extends one
of the four basic stream classes (`stream.Writable`, `stream.Readable`,
Expand Down Expand Up @@ -1558,7 +1559,9 @@ Because JavaScript does not have support for multiple inheritance, the
to extending the `stream.Readable` *and* `stream.Writable` classes).

*Note*: The `stream.Duplex` class prototypically inherits from `stream.Readable`
and parasitically from `stream.Writable`.
and parasitically from `stream.Writable`, but `instanceof` will work properly
for both base classes due to overriding [`Symbol.hasInstance`][]
on `stream.Writable`.

Custom Duplex streams *must* call the `new stream.Duplex([options])`
constructor and implement *both* the `readable._read()` and
Expand Down Expand Up @@ -1642,8 +1645,8 @@ class MyDuplex extends Duplex {
_write(chunk, encoding, callback) {
// The underlying source only deals with strings
if (Buffer.isBuffer(chunk))
chunk = chunk.toString(encoding);
this[kSource].writeSomeData(chunk, encoding);
chunk = chunk.toString();
this[kSource].writeSomeData(chunk);
callback();
}

Expand All @@ -1667,7 +1670,7 @@ respectively.

In the following example, for instance, a new Transform stream (which is a
type of [Duplex][] stream) is created that has an object mode Writable side
that accepts JavaScript numbers that are converted to hexidecimal strings on
that accepts JavaScript numbers that are converted to hexadecimal strings on
the Readable side.

```js
Expand Down Expand Up @@ -1783,7 +1786,7 @@ after all data has been output, which occurs after the callback in
#### transform.\_flush(callback)

* `callback` {Function} A callback function (optionally with an error
argument) to be called when remaining data has been flushed.
argument and data) to be called when remaining data has been flushed.

*Note*: **This function MUST NOT be called by application code directly.** It
should be implemented by child classes, and called only by the internal Readable
Expand Down Expand Up @@ -1967,31 +1970,31 @@ readable buffer so there is nothing for a user to consume.
[`'end'`]: #stream_event_end
[`'finish'`]: #stream_event_finish
[`'readable'`]: #stream_event_readable
[`EventEmitter`]: https://nodejs.org/docs/v6.5.0/api/events.html#events_class_eventemitter
[`process.stderr`]: https://nodejs.org/docs/v6.5.0/api/process.html#process_process_stderr
[`process.stdin`]: https://nodejs.org/docs/v6.5.0/api/process.html#process_process_stdin
[`process.stdout`]: https://nodejs.org/docs/v6.5.0/api/process.html#process_process_stdout
[`EventEmitter`]: https://nodejs.org/docs/v7.0.0/api/events.html#events_class_eventemitter
[`process.stderr`]: https://nodejs.org/docs/v7.0.0/api/process.html#process_process_stderr
[`process.stdin`]: https://nodejs.org/docs/v7.0.0/api/process.html#process_process_stdin
[`process.stdout`]: https://nodejs.org/docs/v7.0.0/api/process.html#process_process_stdout
[`stream.cork()`]: #stream_writable_cork
[`stream.pipe()`]: #stream_readable_pipe_destination_options
[`stream.uncork()`]: #stream_writable_uncork
[`stream.unpipe()`]: #stream_readable_unpipe_destination
[`stream.wrap()`]: #stream_readable_wrap_stream
[API for Stream Consumers]: #stream_api_for_stream_consumers
[API for Stream Implementers]: #stream_api_for_stream_implementers
[child process stdin]: https://nodejs.org/docs/v6.5.0/api/child_process.html#child_process_child_stdin
[child process stdout and stderr]: https://nodejs.org/docs/v6.5.0/api/child_process.html#child_process_child_stdout
[child process stdin]: https://nodejs.org/docs/v7.0.0/api/child_process.html#child_process_child_stdin
[child process stdout and stderr]: https://nodejs.org/docs/v7.0.0/api/child_process.html#child_process_child_stdout
[Compatibility]: #stream_compatibility_with_older_node_js_versions
[crypto]: crypto.html
[Duplex]: #stream_class_stream_duplex
[fs read streams]: https://nodejs.org/docs/v6.5.0/api/fs.html#fs_class_fs_readstream
[fs write streams]: https://nodejs.org/docs/v6.5.0/api/fs.html#fs_class_fs_writestream
[`fs.createReadStream()`]: https://nodejs.org/docs/v6.5.0/api/fs.html#fs_fs_createreadstream_path_options
[`fs.createWriteStream()`]: https://nodejs.org/docs/v6.5.0/api/fs.html#fs_fs_createwritestream_path_options
[`net.Socket`]: https://nodejs.org/docs/v6.5.0/api/net.html#net_class_net_socket
[`zlib.createDeflate()`]: https://nodejs.org/docs/v6.5.0/api/zlib.html#zlib_zlib_createdeflate_options
[HTTP requests, on the client]: https://nodejs.org/docs/v6.5.0/api/http.html#http_class_http_clientrequest
[HTTP responses, on the server]: https://nodejs.org/docs/v6.5.0/api/http.html#http_class_http_serverresponse
[http-incoming-message]: https://nodejs.org/docs/v6.5.0/api/http.html#http_class_http_incomingmessage
[fs read streams]: https://nodejs.org/docs/v7.0.0/api/fs.html#fs_class_fs_readstream
[fs write streams]: https://nodejs.org/docs/v7.0.0/api/fs.html#fs_class_fs_writestream
[`fs.createReadStream()`]: https://nodejs.org/docs/v7.0.0/api/fs.html#fs_fs_createreadstream_path_options
[`fs.createWriteStream()`]: https://nodejs.org/docs/v7.0.0/api/fs.html#fs_fs_createwritestream_path_options
[`net.Socket`]: https://nodejs.org/docs/v7.0.0/api/net.html#net_class_net_socket
[`zlib.createDeflate()`]: https://nodejs.org/docs/v7.0.0/api/zlib.html#zlib_zlib_createdeflate_options
[HTTP requests, on the client]: https://nodejs.org/docs/v7.0.0/api/http.html#http_class_http_clientrequest
[HTTP responses, on the server]: https://nodejs.org/docs/v7.0.0/api/http.html#http_class_http_serverresponse
[http-incoming-message]: https://nodejs.org/docs/v7.0.0/api/http.html#http_class_http_incomingmessage
[Readable]: #stream_class_stream_readable
[stream-_flush]: #stream_transform_flush_callback
[stream-_read]: #stream_readable_read_size_1
Expand All @@ -2004,7 +2007,8 @@ readable buffer so there is nothing for a user to consume.
[stream-read]: #stream_readable_read_size
[stream-resume]: #stream_readable_resume
[stream-write]: #stream_writable_write_chunk_encoding_callback
[TCP sockets]: https://nodejs.org/docs/v6.5.0/api/net.html#net_class_net_socket
[TCP sockets]: https://nodejs.org/docs/v7.0.0/api/net.html#net_class_net_socket
[Transform]: #stream_class_stream_transform
[Writable]: #stream_class_stream_writable
[zlib]: zlib.html
[`Symbol.hasInstance`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance
12 changes: 6 additions & 6 deletions lib/_stream_readable.js
Expand Up @@ -471,7 +471,7 @@ function maybeReadMore_(stream, state) {
// for virtual (non-string, non-buffer) streams, "length" is somewhat
// arbitrary, and perhaps not very meaningful.
Readable.prototype._read = function (n) {
this.emit('error', new Error('not implemented'));
this.emit('error', new Error('_read() is not implemented'));
};

Readable.prototype.pipe = function (dest, pipeOpts) {
Expand Down Expand Up @@ -649,16 +649,16 @@ Readable.prototype.unpipe = function (dest) {
state.pipesCount = 0;
state.flowing = false;

for (var _i = 0; _i < len; _i++) {
dests[_i].emit('unpipe', this);
for (var i = 0; i < len; i++) {
dests[i].emit('unpipe', this);
}return this;
}

// try to find the right one.
var i = indexOf(state.pipes, dest);
if (i === -1) return this;
var index = indexOf(state.pipes, dest);
if (index === -1) return this;

state.pipes.splice(i, 1);
state.pipes.splice(index, 1);
state.pipesCount -= 1;
if (state.pipesCount === 1) state.pipes = state.pipes[0];

Expand Down
12 changes: 7 additions & 5 deletions lib/_stream_transform.js
Expand Up @@ -94,7 +94,6 @@ function Transform(options) {

this._transformState = new TransformState(this);

// when the writable side finishes, then flush out anything remaining.
var stream = this;

// start out asking for a readable event once data is transformed.
Expand All @@ -111,9 +110,10 @@ function Transform(options) {
if (typeof options.flush === 'function') this._flush = options.flush;
}

// When the writable side finishes, then flush out anything remaining.
this.once('prefinish', function () {
if (typeof this._flush === 'function') this._flush(function (er) {
done(stream, er);
if (typeof this._flush === 'function') this._flush(function (er, data) {
done(stream, er, data);
});else done(stream);
});
}
Expand All @@ -134,7 +134,7 @@ Transform.prototype.push = function (chunk, encoding) {
// an error, then that'll put the hurt on the whole operation. If you
// never call cb(), then you'll never get another chunk.
Transform.prototype._transform = function (chunk, encoding, cb) {
throw new Error('Not implemented');
throw new Error('_transform() is not implemented');
};

Transform.prototype._write = function (chunk, encoding, cb) {
Expand Down Expand Up @@ -164,9 +164,11 @@ Transform.prototype._read = function (n) {
}
};

function done(stream, er) {
function done(stream, er, data) {
if (er) return stream.emit('error', er);

if (data !== null && data !== undefined) stream.push(data);

// if there's nothing in the write buffer, then that means
// that nothing more will ever be provided
var ws = stream._writableState;
Expand Down
35 changes: 30 additions & 5 deletions lib/_stream_writable.js
Expand Up @@ -150,7 +150,7 @@ function WritableState(options, stream) {
this.corkedRequestsFree = new CorkedRequest(this);
}

WritableState.prototype.getBuffer = function writableStateGetBuffer() {
WritableState.prototype.getBuffer = function getBuffer() {
var current = this.bufferedRequest;
var out = [];
while (current) {
Expand All @@ -170,13 +170,38 @@ WritableState.prototype.getBuffer = function writableStateGetBuffer() {
} catch (_) {}
})();

// Test _writableState for inheritance to account for Duplex streams,
// whose prototype chain only points to Readable.
var realHasInstance;
if (typeof Symbol === 'function' && Symbol.hasInstance) {
realHasInstance = Function.prototype[Symbol.hasInstance];
Object.defineProperty(Writable, Symbol.hasInstance, {
value: function (object) {
if (realHasInstance.call(this, object)) return true;

return object && object._writableState instanceof WritableState;
}
});
} else {
realHasInstance = function (object) {
return object instanceof this;
};
}

var Duplex;
function Writable(options) {
Duplex = Duplex || require('./_stream_duplex');

// Writable ctor is applied to Duplexes, though they're not
// instanceof Writable, they're instanceof Readable.
if (!(this instanceof Writable) && !(this instanceof Duplex)) return new Writable(options);
// Writable ctor is applied to Duplexes, too.
// `realHasInstance` is necessary because using plain `instanceof`
// would return false, as no `_writableState` property is attached.

// Trying to use the custom `instanceof` for Writable here will also break the
// Node.js LazyTransform implementation, which has a non-trivial getter for
// `_writableState` that would lead to infinite recursion.
if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) {
return new Writable(options);
}

this._writableState = new WritableState(options, this);

Expand Down Expand Up @@ -436,7 +461,7 @@ function clearBuffer(stream, state) {
}

Writable.prototype._write = function (chunk, encoding, cb) {
cb(new Error('not implemented'));
cb(new Error('_write() is not implemented'));
};

Writable.prototype._writev = null;
Expand Down

0 comments on commit b6f6aaf

Please sign in to comment.