Skip to content

Commit

Permalink
build for v6.1.0 plus fixes for assert and buffer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed May 18, 2016
1 parent 06754ee commit fe2de84
Show file tree
Hide file tree
Showing 60 changed files with 562 additions and 434 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# readable-stream

***Node-core v5.9.1 streams for userland*** [![Build Status](https://travis-ci.org/nodejs/readable-stream.svg?branch=master)](https://travis-ci.org/nodejs/readable-stream)
***Node-core v6.1.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 All @@ -16,7 +16,7 @@ npm install --save readable-stream
***Node-core streams for userland***

This package is a mirror of the Streams2 and Streams3 implementations in
Node-core, including [documentation](doc/stream.markdown).
Node-core, including [documentation](doc/stream.md).

If you want to guarantee a stable streams base, regardless of what version of
Node you, or the users of your libraries are using, use **readable-stream** *only* and avoid the *"stream"* module in Node-core, for background see [this blogpost](http://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html).
Expand Down
4 changes: 2 additions & 2 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ 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))
if (/^test-stream/.test(file) && !/-wrap(?:-encoding)?\.js$/.test(file) && file !== 'test-stream2-httpclient-response-end.js')
processTestFile(file)
})
}))

processFile(docurlpfx + 'stream.markdown', path.join(docourroot, 'stream.markdown'), docReplace)
processFile(docurlpfx + 'stream.md', path.join(docourroot, 'stream.md'), docReplace)


//--------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions build/common-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ module.exports.objectKeysReplacement = [
/Object\.keys/g
, 'objectKeys'
]


module.exports.bufferShimFix = [
/^('use strict';)$/m,
`/*<replacement>*/
const bufferShim = require('buffer-shims');
/*</replacement>*/`
]

module.exports.bufferStaticMethods = [
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
`bufferShim.$1`
]
27 changes: 17 additions & 10 deletions build/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ const headRegexp = /(^module.exports = \w+;?)/m
, '$1$2/$3'
]

, bufferReplacement = [
headRegexp
, '$1\n\n/*<replacement>*/\nvar Buffer = require(\'buffer\').Buffer;\n/*</replacement>*/\n'
]

// The browser build ends up with a circular dependency, so the require is
// done lazily, but cached.
, addDuplexRequire = [
Expand Down Expand Up @@ -90,8 +85,8 @@ const headRegexp = /(^module.exports = \w+;?)/m
, objectKeysReplacement = require('./common-replacements').objectKeysReplacement

, eventEmittterReplacement = [
/(require\('events'\)(?:\.EventEmitter)?;)/
, '$1\n\n/*<replacement>*/\n'
/^(const EE = require\('events'\));$/m
, '/*<replacement>*/\n$1.EventEmitter;\n\n'
+ 'var EElistenerCount = function(emitter, type) {\n'
+ ' return emitter.listeners(type).length;\n'
+ '};\n/*</replacement>*/\n'
Expand Down Expand Up @@ -165,6 +160,17 @@ const headRegexp = /(^module.exports = \w+;?)/m
`
]
, bufferShimFix = [
/const Buffer = require\('buffer'\)\.Buffer;/,
`const Buffer = require('buffer').Buffer;
/*<replacement>*/
const bufferShim = require('buffer-shims');
/*</replacement>*/`
]
, bufferStaticMethods = [
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
`bufferShim.$1`
]

module.exports['_stream_duplex.js'] = [
requireReplacement
Expand All @@ -190,7 +196,6 @@ module.exports['_stream_readable.js'] = [
addDuplexRequire
, requireReplacement
, instanceofReplacement
, bufferReplacement
, altForEachImplReplacement
, altForEachUseReplacement
, altIndexOfImplReplacement
Expand All @@ -208,6 +213,8 @@ module.exports['_stream_readable.js'] = [
, processNextTickImport
, processNextTickReplacement
, eventEmittterListenerCountReplacement
, bufferShimFix
, bufferStaticMethods
]

module.exports['_stream_transform.js'] = [
Expand All @@ -221,7 +228,6 @@ module.exports['_stream_writable.js'] = [
addDuplexRequire
, requireReplacement
, instanceofReplacement
, bufferReplacement
, utilReplacement
, stringDecoderReplacement
, debugLogReplacement
Expand All @@ -237,5 +243,6 @@ module.exports['_stream_writable.js'] = [
, processNextTickReplacement
, internalUtilReplacement
, fixSyncWrite

, bufferShimFix
, bufferStaticMethods
]
11 changes: 10 additions & 1 deletion build/test-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const altForEachImplReplacement = require('./common-replacements').altForEachImp
require('./common-replacements').objectKeysDefine
, objectKeysReplacement =
require('./common-replacements').objectKeysReplacement
, bufferShimFix =
require('./common-replacements').bufferShimFix
, bufferStaticMethods =
require('./common-replacements').bufferStaticMethods

module.exports.all = [
[
Expand All @@ -30,7 +34,12 @@ module.exports.all = [
/Stream.(Readable|Writable|Duplex|Transform|PassThrough)/g
, 'require(\'../../\').$1'
]

, bufferShimFix
, bufferStaticMethods
, [
/require\(['"]assert['"]\)/g
, 'require(\'assert/\')'
]
]

module.exports['test-stream2-basic.js'] = [
Expand Down
49 changes: 30 additions & 19 deletions doc/stream.markdown → doc/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ Emitted when the stream and any of its underlying resources (a file
descriptor, for example) have been closed. The event indicates that
no more events will be emitted, and no further computation will occur.

Not all streams will emit the `'close'` event.
Not all streams will emit the `'close'` event as the `'close'` event is
optional.

#### Event: 'data'

Expand Down Expand Up @@ -201,7 +202,7 @@ readable.on('end', () => {

#### Event: 'error'

* {Error Object}
* {Error}

Emitted if there was an error receiving data.

Expand Down Expand Up @@ -490,7 +491,7 @@ function parseHeader(stream, callback) {
var split = str.split(/\n\n/);
header += split.shift();
var remaining = split.join('\n\n');
var buf = new Buffer(remaining, 'utf8');
var buf = Buffer.from(remaining, 'utf8');
if (buf.length)
stream.unshift(buf);
stream.removeListener('error', callback);
Expand Down Expand Up @@ -573,6 +574,15 @@ Examples of writable streams include:
* [child process stdin][]
* [`process.stdout`][], [`process.stderr`][]

#### Event: 'close'

Emitted when the stream and any of its underlying resources (a file descriptor,
for example) have been closed. The event indicates that no more events will be
emitted, and no further computation will occur.

Not all streams will emit the `'close'` event as the `'close'` event is
optional.

#### Event: 'drain'

If a [`stream.write(chunk)`][stream-write] call returns `false`, then the
Expand Down Expand Up @@ -695,6 +705,7 @@ file.end('world!');
#### writable.setDefaultEncoding(encoding)

* `encoding` {String} The new default encoding
* Return: `this`

Sets the default encoding for a writable stream.

Expand Down Expand Up @@ -985,7 +996,7 @@ Counter.prototype._read = function() {
this.push(null);
else {
var str = '' + i;
var buf = new Buffer(str, 'ascii');
var buf = Buffer.from(str, 'ascii');
this.push(buf);
}
};
Expand Down Expand Up @@ -1717,30 +1728,30 @@ horribly wrong.
[`'end'`]: #stream_event_end
[`'finish'`]: #stream_event_finish
[`'readable'`]: #stream_event_readable
[`buf.toString(encoding)`]: https://nodejs.org/docs/v5.9.1/api/buffer.html#buffer_buf_tostring_encoding_start_end
[`EventEmitter`]: https://nodejs.org/docs/v5.9.1/api/events.html#events_class_eventemitter
[`process.stderr`]: https://nodejs.org/docs/v5.9.1/api/process.html#process_process_stderr
[`process.stdin`]: https://nodejs.org/docs/v5.9.1/api/process.html#process_process_stdin
[`process.stdout`]: https://nodejs.org/docs/v5.9.1/api/process.html#process_process_stdout
[`buf.toString(encoding)`]: https://nodejs.org/docs/v6.1.0/api/buffer.html#buffer_buf_tostring_encoding_start_end
[`EventEmitter`]: https://nodejs.org/docs/v6.1.0/api/events.html#events_class_eventemitter
[`process.stderr`]: https://nodejs.org/docs/v6.1.0/api/process.html#process_process_stderr
[`process.stdin`]: https://nodejs.org/docs/v6.1.0/api/process.html#process_process_stdin
[`process.stdout`]: https://nodejs.org/docs/v6.1.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
[`tls.CryptoStream`]: https://nodejs.org/docs/v5.9.1/api/tls.html#tls_class_cryptostream
[`util.inherits()`]: https://nodejs.org/docs/v5.9.1/api/util.html#util_util_inherits_constructor_superconstructor
[`tls.CryptoStream`]: https://nodejs.org/docs/v6.1.0/api/tls.html#tls_class_cryptostream
[`util.inherits()`]: https://nodejs.org/docs/v6.1.0/api/util.html#util_util_inherits_constructor_superconstructor
[API for Stream Consumers]: #stream_api_for_stream_consumers
[API for Stream Implementors]: #stream_api_for_stream_implementors
[child process stdin]: https://nodejs.org/docs/v5.9.1/api/child_process.html#child_process_child_stdin
[child process stdout and stderr]: https://nodejs.org/docs/v5.9.1/api/child_process.html#child_process_child_stdout
[child process stdin]: https://nodejs.org/docs/v6.1.0/api/child_process.html#child_process_child_stdin
[child process stdout and stderr]: https://nodejs.org/docs/v6.1.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/v5.9.1/api/fs.html#fs_class_fs_readstream
[fs write streams]: https://nodejs.org/docs/v5.9.1/api/fs.html#fs_class_fs_writestream
[HTTP requests, on the client]: https://nodejs.org/docs/v5.9.1/api/http.html#http_class_http_clientrequest
[HTTP responses, on the server]: https://nodejs.org/docs/v5.9.1/api/http.html#http_class_http_serverresponse
[http-incoming-message]: https://nodejs.org/docs/v5.9.1/api/http.html#http_class_http_incomingmessage
[fs read streams]: https://nodejs.org/docs/v6.1.0/api/fs.html#fs_class_fs_readstream
[fs write streams]: https://nodejs.org/docs/v6.1.0/api/fs.html#fs_class_fs_writestream
[HTTP requests, on the client]: https://nodejs.org/docs/v6.1.0/api/http.html#http_class_http_clientrequest
[HTTP responses, on the server]: https://nodejs.org/docs/v6.1.0/api/http.html#http_class_http_serverresponse
[http-incoming-message]: https://nodejs.org/docs/v6.1.0/api/http.html#http_class_http_incomingmessage
[Object mode]: #stream_object_mode
[Readable]: #stream_class_stream_readable
[SimpleProtocol v2]: #stream_example_simpleprotocol_parser_v2
Expand All @@ -1755,7 +1766,7 @@ horribly wrong.
[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/v5.9.1/api/net.html#net_class_net_socket
[TCP sockets]: https://nodejs.org/docs/v6.1.0/api/net.html#net_class_net_socket
[Transform]: #stream_class_stream_transform
[Writable]: #stream_class_stream_writable
[zlib]: zlib.html
53 changes: 33 additions & 20 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ var processNextTick = require('process-nextick-args');
var isArray = require('isarray');
/*</replacement>*/

/*<replacement>*/
var Buffer = require('buffer').Buffer;
/*</replacement>*/

Readable.ReadableState = ReadableState;

var EE = require('events');

/*<replacement>*/
var EE = require('events').EventEmitter;

var EElistenerCount = function (emitter, type) {
return emitter.listeners(type).length;
};
Expand All @@ -36,6 +32,9 @@ var Stream;
/*</replacement>*/

var Buffer = require('buffer').Buffer;
/*<replacement>*/
var bufferShim = require('buffer-shims');
/*</replacement>*/

/*<replacement>*/
var util = require('core-util-is');
Expand All @@ -44,7 +43,7 @@ util.inherits = require('inherits');

/*<replacement>*/
var debugUtil = require('util');
var debug = undefined;
var debug = void 0;
if (debugUtil && debugUtil.debuglog) {
debug = debugUtil.debuglog('stream');
} else {
Expand All @@ -56,6 +55,19 @@ var StringDecoder;

util.inherits(Readable, Stream);

var hasPrependListener = typeof EE.prototype.prependListener === 'function';

function prependListener(emitter, event, fn) {
if (hasPrependListener) return emitter.prependListener(event, fn);

// This is a brutally ugly hack to make sure that our error handler
// is attached before any userland ones. NEVER DO THIS. This is here
// only because this code needs to continue to work with older versions
// of Node.js that do not include the prependListener() method. The goal
// is to eventually remove this hack.
if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
}

var Duplex;
function ReadableState(options, stream) {
Duplex = Duplex || require('./_stream_duplex');
Expand Down Expand Up @@ -149,7 +161,7 @@ Readable.prototype.push = function (chunk, encoding) {
if (!state.objectMode && typeof chunk === 'string') {
encoding = encoding || state.defaultEncoding;
if (encoding !== state.encoding) {
chunk = new Buffer(chunk, encoding);
chunk = bufferShim.from(chunk, encoding);
encoding = '';
}
}
Expand Down Expand Up @@ -179,8 +191,8 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
var e = new Error('stream.push() after EOF');
stream.emit('error', e);
} else if (state.endEmitted && addToFront) {
var e = new Error('stream.unshift() after end event');
stream.emit('error', e);
var _e = new Error('stream.unshift() after end event');
stream.emit('error', _e);
} else {
var skipAdd;
if (state.decoder && !addToFront && !encoding) {
Expand Down Expand Up @@ -533,7 +545,8 @@ Readable.prototype.pipe = function (dest, pipeOpts) {
// If the user unpiped during `dest.write()`, it is possible
// to get stuck in a permanently paused state if that write
// also returned false.
if (state.pipesCount === 1 && state.pipes[0] === dest && src.listenerCount('data') === 1 && !cleanedUp) {
// => Check whether `dest` is still a piping destination.
if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
debug('false write response, pause', src._readableState.awaitDrain);
src._readableState.awaitDrain++;
}
Expand All @@ -549,9 +562,9 @@ Readable.prototype.pipe = function (dest, pipeOpts) {
dest.removeListener('error', onerror);
if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
}
// This is a brutally ugly hack to make sure that our error handler
// is attached before any userland ones. NEVER DO THIS.
if (!dest._events || !dest._events.error) dest.on('error', onerror);else if (isArray(dest._events.error)) dest._events.error.unshift(onerror);else dest._events.error = [onerror, dest._events.error];

// Make sure our error handler is attached before userland ones.
prependListener(dest, 'error', onerror);

// Both close and finish should trigger unpipe, but only once.
function onclose() {
Expand Down Expand Up @@ -825,16 +838,16 @@ function fromList(n, state) {
} else {
// complex case.
// we have enough to cover it, but it spans past the first buffer.
if (stringMode) ret = '';else ret = new Buffer(n);
if (stringMode) ret = '';else ret = bufferShim.allocUnsafe(n);

var c = 0;
for (var i = 0, l = list.length; i < l && c < n; i++) {
var buf = list[0];
var cpy = Math.min(n - c, buf.length);
var _buf = list[0];
var cpy = Math.min(n - c, _buf.length);

if (stringMode) ret += buf.slice(0, cpy);else buf.copy(ret, c, 0, cpy);
if (stringMode) ret += _buf.slice(0, cpy);else _buf.copy(ret, c, 0, cpy);

if (cpy < buf.length) list[0] = buf.slice(cpy);else list.shift();
if (cpy < _buf.length) list[0] = _buf.slice(cpy);else list.shift();

c += cpy;
}
Expand All @@ -849,7 +862,7 @@ function endReadable(stream) {

// If we get here before consuming all the bytes, then that is a
// bug in node. Should never happen.
if (state.length > 0) throw new Error('endReadable called on non-empty stream');
if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');

if (!state.endEmitted) {
state.ended = true;
Expand Down
Loading

0 comments on commit fe2de84

Please sign in to comment.