Skip to content

Commit fe2de84

Browse files
committed
build for v6.1.0 plus fixes for assert and buffer changes
1 parent 06754ee commit fe2de84

File tree

60 files changed

+562
-434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+562
-434
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# readable-stream
22

3-
***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)
3+
***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)
44

55

66
[![NPM](https://nodei.co/npm/readable-stream.png?downloads=true&downloadRank=true)](https://nodei.co/npm/readable-stream/)
@@ -16,7 +16,7 @@ npm install --save readable-stream
1616
***Node-core streams for userland***
1717

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

2121
If you want to guarantee a stable streams base, regardless of what version of
2222
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).

build/build.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ hyperquest(testlisturl).pipe(bl(function (err, data) {
9797

9898
$('table.files .js-navigation-open').each(function () {
9999
var file = $(this).text()
100-
if (/^test-stream/.test(file) && !/-wrap(?:-encoding)?\.js$/.test(file))
100+
if (/^test-stream/.test(file) && !/-wrap(?:-encoding)?\.js$/.test(file) && file !== 'test-stream2-httpclient-response-end.js')
101101
processTestFile(file)
102102
})
103103
}))
104104

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

107107

108108
//--------------------------------------------------------------------

build/common-replacements.js

+13
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,16 @@ module.exports.objectKeysReplacement = [
3939
/Object\.keys/g
4040
, 'objectKeys'
4141
]
42+
43+
44+
module.exports.bufferShimFix = [
45+
/^('use strict';)$/m,
46+
`/*<replacement>*/
47+
const bufferShim = require('buffer-shims');
48+
/*</replacement>*/`
49+
]
50+
51+
module.exports.bufferStaticMethods = [
52+
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
53+
`bufferShim.$1`
54+
]

build/files.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ const headRegexp = /(^module.exports = \w+;?)/m
2525
, '$1$2/$3'
2626
]
2727

28-
, bufferReplacement = [
29-
headRegexp
30-
, '$1\n\n/*<replacement>*/\nvar Buffer = require(\'buffer\').Buffer;\n/*</replacement>*/\n'
31-
]
32-
3328
// The browser build ends up with a circular dependency, so the require is
3429
// done lazily, but cached.
3530
, addDuplexRequire = [
@@ -90,8 +85,8 @@ const headRegexp = /(^module.exports = \w+;?)/m
9085
, objectKeysReplacement = require('./common-replacements').objectKeysReplacement
9186

9287
, eventEmittterReplacement = [
93-
/(require\('events'\)(?:\.EventEmitter)?;)/
94-
, '$1\n\n/*<replacement>*/\n'
88+
/^(const EE = require\('events'\));$/m
89+
, '/*<replacement>*/\n$1.EventEmitter;\n\n'
9590
+ 'var EElistenerCount = function(emitter, type) {\n'
9691
+ ' return emitter.listeners(type).length;\n'
9792
+ '};\n/*</replacement>*/\n'
@@ -165,6 +160,17 @@ const headRegexp = /(^module.exports = \w+;?)/m
165160
166161
`
167162
]
163+
, bufferShimFix = [
164+
/const Buffer = require\('buffer'\)\.Buffer;/,
165+
`const Buffer = require('buffer').Buffer;
166+
/*<replacement>*/
167+
const bufferShim = require('buffer-shims');
168+
/*</replacement>*/`
169+
]
170+
, bufferStaticMethods = [
171+
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
172+
`bufferShim.$1`
173+
]
168174

169175
module.exports['_stream_duplex.js'] = [
170176
requireReplacement
@@ -190,7 +196,6 @@ module.exports['_stream_readable.js'] = [
190196
addDuplexRequire
191197
, requireReplacement
192198
, instanceofReplacement
193-
, bufferReplacement
194199
, altForEachImplReplacement
195200
, altForEachUseReplacement
196201
, altIndexOfImplReplacement
@@ -208,6 +213,8 @@ module.exports['_stream_readable.js'] = [
208213
, processNextTickImport
209214
, processNextTickReplacement
210215
, eventEmittterListenerCountReplacement
216+
, bufferShimFix
217+
, bufferStaticMethods
211218
]
212219

213220
module.exports['_stream_transform.js'] = [
@@ -221,7 +228,6 @@ module.exports['_stream_writable.js'] = [
221228
addDuplexRequire
222229
, requireReplacement
223230
, instanceofReplacement
224-
, bufferReplacement
225231
, utilReplacement
226232
, stringDecoderReplacement
227233
, debugLogReplacement
@@ -237,5 +243,6 @@ module.exports['_stream_writable.js'] = [
237243
, processNextTickReplacement
238244
, internalUtilReplacement
239245
, fixSyncWrite
240-
246+
, bufferShimFix
247+
, bufferStaticMethods
241248
]

build/test-replacements.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const altForEachImplReplacement = require('./common-replacements').altForEachImp
66
require('./common-replacements').objectKeysDefine
77
, objectKeysReplacement =
88
require('./common-replacements').objectKeysReplacement
9+
, bufferShimFix =
10+
require('./common-replacements').bufferShimFix
11+
, bufferStaticMethods =
12+
require('./common-replacements').bufferStaticMethods
913

1014
module.exports.all = [
1115
[
@@ -30,7 +34,12 @@ module.exports.all = [
3034
/Stream.(Readable|Writable|Duplex|Transform|PassThrough)/g
3135
, 'require(\'../../\').$1'
3236
]
33-
37+
, bufferShimFix
38+
, bufferStaticMethods
39+
, [
40+
/require\(['"]assert['"]\)/g
41+
, 'require(\'assert/\')'
42+
]
3443
]
3544

3645
module.exports['test-stream2-basic.js'] = [

doc/stream.markdown renamed to doc/stream.md

+30-19
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ Emitted when the stream and any of its underlying resources (a file
160160
descriptor, for example) have been closed. The event indicates that
161161
no more events will be emitted, and no further computation will occur.
162162

163-
Not all streams will emit the `'close'` event.
163+
Not all streams will emit the `'close'` event as the `'close'` event is
164+
optional.
164165

165166
#### Event: 'data'
166167

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

202203
#### Event: 'error'
203204

204-
* {Error Object}
205+
* {Error}
205206

206207
Emitted if there was an error receiving data.
207208

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

577+
#### Event: 'close'
578+
579+
Emitted when the stream and any of its underlying resources (a file descriptor,
580+
for example) have been closed. The event indicates that no more events will be
581+
emitted, and no further computation will occur.
582+
583+
Not all streams will emit the `'close'` event as the `'close'` event is
584+
optional.
585+
576586
#### Event: 'drain'
577587

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

697707
* `encoding` {String} The new default encoding
708+
* Return: `this`
698709

699710
Sets the default encoding for a writable stream.
700711

@@ -985,7 +996,7 @@ Counter.prototype._read = function() {
985996
this.push(null);
986997
else {
987998
var str = '' + i;
988-
var buf = new Buffer(str, 'ascii');
999+
var buf = Buffer.from(str, 'ascii');
9891000
this.push(buf);
9901001
}
9911002
};
@@ -1717,30 +1728,30 @@ horribly wrong.
17171728
[`'end'`]: #stream_event_end
17181729
[`'finish'`]: #stream_event_finish
17191730
[`'readable'`]: #stream_event_readable
1720-
[`buf.toString(encoding)`]: https://nodejs.org/docs/v5.9.1/api/buffer.html#buffer_buf_tostring_encoding_start_end
1721-
[`EventEmitter`]: https://nodejs.org/docs/v5.9.1/api/events.html#events_class_eventemitter
1722-
[`process.stderr`]: https://nodejs.org/docs/v5.9.1/api/process.html#process_process_stderr
1723-
[`process.stdin`]: https://nodejs.org/docs/v5.9.1/api/process.html#process_process_stdin
1724-
[`process.stdout`]: https://nodejs.org/docs/v5.9.1/api/process.html#process_process_stdout
1731+
[`buf.toString(encoding)`]: https://nodejs.org/docs/v6.1.0/api/buffer.html#buffer_buf_tostring_encoding_start_end
1732+
[`EventEmitter`]: https://nodejs.org/docs/v6.1.0/api/events.html#events_class_eventemitter
1733+
[`process.stderr`]: https://nodejs.org/docs/v6.1.0/api/process.html#process_process_stderr
1734+
[`process.stdin`]: https://nodejs.org/docs/v6.1.0/api/process.html#process_process_stdin
1735+
[`process.stdout`]: https://nodejs.org/docs/v6.1.0/api/process.html#process_process_stdout
17251736
[`stream.cork()`]: #stream_writable_cork
17261737
[`stream.pipe()`]: #stream_readable_pipe_destination_options
17271738
[`stream.uncork()`]: #stream_writable_uncork
17281739
[`stream.unpipe()`]: #stream_readable_unpipe_destination
17291740
[`stream.wrap()`]: #stream_readable_wrap_stream
1730-
[`tls.CryptoStream`]: https://nodejs.org/docs/v5.9.1/api/tls.html#tls_class_cryptostream
1731-
[`util.inherits()`]: https://nodejs.org/docs/v5.9.1/api/util.html#util_util_inherits_constructor_superconstructor
1741+
[`tls.CryptoStream`]: https://nodejs.org/docs/v6.1.0/api/tls.html#tls_class_cryptostream
1742+
[`util.inherits()`]: https://nodejs.org/docs/v6.1.0/api/util.html#util_util_inherits_constructor_superconstructor
17321743
[API for Stream Consumers]: #stream_api_for_stream_consumers
17331744
[API for Stream Implementors]: #stream_api_for_stream_implementors
1734-
[child process stdin]: https://nodejs.org/docs/v5.9.1/api/child_process.html#child_process_child_stdin
1735-
[child process stdout and stderr]: https://nodejs.org/docs/v5.9.1/api/child_process.html#child_process_child_stdout
1745+
[child process stdin]: https://nodejs.org/docs/v6.1.0/api/child_process.html#child_process_child_stdin
1746+
[child process stdout and stderr]: https://nodejs.org/docs/v6.1.0/api/child_process.html#child_process_child_stdout
17361747
[Compatibility]: #stream_compatibility_with_older_node_js_versions
17371748
[crypto]: crypto.html
17381749
[Duplex]: #stream_class_stream_duplex
1739-
[fs read streams]: https://nodejs.org/docs/v5.9.1/api/fs.html#fs_class_fs_readstream
1740-
[fs write streams]: https://nodejs.org/docs/v5.9.1/api/fs.html#fs_class_fs_writestream
1741-
[HTTP requests, on the client]: https://nodejs.org/docs/v5.9.1/api/http.html#http_class_http_clientrequest
1742-
[HTTP responses, on the server]: https://nodejs.org/docs/v5.9.1/api/http.html#http_class_http_serverresponse
1743-
[http-incoming-message]: https://nodejs.org/docs/v5.9.1/api/http.html#http_class_http_incomingmessage
1750+
[fs read streams]: https://nodejs.org/docs/v6.1.0/api/fs.html#fs_class_fs_readstream
1751+
[fs write streams]: https://nodejs.org/docs/v6.1.0/api/fs.html#fs_class_fs_writestream
1752+
[HTTP requests, on the client]: https://nodejs.org/docs/v6.1.0/api/http.html#http_class_http_clientrequest
1753+
[HTTP responses, on the server]: https://nodejs.org/docs/v6.1.0/api/http.html#http_class_http_serverresponse
1754+
[http-incoming-message]: https://nodejs.org/docs/v6.1.0/api/http.html#http_class_http_incomingmessage
17441755
[Object mode]: #stream_object_mode
17451756
[Readable]: #stream_class_stream_readable
17461757
[SimpleProtocol v2]: #stream_example_simpleprotocol_parser_v2
@@ -1755,7 +1766,7 @@ horribly wrong.
17551766
[stream-read]: #stream_readable_read_size
17561767
[stream-resume]: #stream_readable_resume
17571768
[stream-write]: #stream_writable_write_chunk_encoding_callback
1758-
[TCP sockets]: https://nodejs.org/docs/v5.9.1/api/net.html#net_class_net_socket
1769+
[TCP sockets]: https://nodejs.org/docs/v6.1.0/api/net.html#net_class_net_socket
17591770
[Transform]: #stream_class_stream_transform
17601771
[Writable]: #stream_class_stream_writable
17611772
[zlib]: zlib.html

lib/_stream_readable.js

+33-20
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ var processNextTick = require('process-nextick-args');
1010
var isArray = require('isarray');
1111
/*</replacement>*/
1212

13-
/*<replacement>*/
14-
var Buffer = require('buffer').Buffer;
15-
/*</replacement>*/
16-
1713
Readable.ReadableState = ReadableState;
1814

19-
var EE = require('events');
20-
2115
/*<replacement>*/
16+
var EE = require('events').EventEmitter;
17+
2218
var EElistenerCount = function (emitter, type) {
2319
return emitter.listeners(type).length;
2420
};
@@ -36,6 +32,9 @@ var Stream;
3632
/*</replacement>*/
3733

3834
var Buffer = require('buffer').Buffer;
35+
/*<replacement>*/
36+
var bufferShim = require('buffer-shims');
37+
/*</replacement>*/
3938

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

4544
/*<replacement>*/
4645
var debugUtil = require('util');
47-
var debug = undefined;
46+
var debug = void 0;
4847
if (debugUtil && debugUtil.debuglog) {
4948
debug = debugUtil.debuglog('stream');
5049
} else {
@@ -56,6 +55,19 @@ var StringDecoder;
5655

5756
util.inherits(Readable, Stream);
5857

58+
var hasPrependListener = typeof EE.prototype.prependListener === 'function';
59+
60+
function prependListener(emitter, event, fn) {
61+
if (hasPrependListener) return emitter.prependListener(event, fn);
62+
63+
// This is a brutally ugly hack to make sure that our error handler
64+
// is attached before any userland ones. NEVER DO THIS. This is here
65+
// only because this code needs to continue to work with older versions
66+
// of Node.js that do not include the prependListener() method. The goal
67+
// is to eventually remove this hack.
68+
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]];
69+
}
70+
5971
var Duplex;
6072
function ReadableState(options, stream) {
6173
Duplex = Duplex || require('./_stream_duplex');
@@ -149,7 +161,7 @@ Readable.prototype.push = function (chunk, encoding) {
149161
if (!state.objectMode && typeof chunk === 'string') {
150162
encoding = encoding || state.defaultEncoding;
151163
if (encoding !== state.encoding) {
152-
chunk = new Buffer(chunk, encoding);
164+
chunk = bufferShim.from(chunk, encoding);
153165
encoding = '';
154166
}
155167
}
@@ -179,8 +191,8 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
179191
var e = new Error('stream.push() after EOF');
180192
stream.emit('error', e);
181193
} else if (state.endEmitted && addToFront) {
182-
var e = new Error('stream.unshift() after end event');
183-
stream.emit('error', e);
194+
var _e = new Error('stream.unshift() after end event');
195+
stream.emit('error', _e);
184196
} else {
185197
var skipAdd;
186198
if (state.decoder && !addToFront && !encoding) {
@@ -533,7 +545,8 @@ Readable.prototype.pipe = function (dest, pipeOpts) {
533545
// If the user unpiped during `dest.write()`, it is possible
534546
// to get stuck in a permanently paused state if that write
535547
// also returned false.
536-
if (state.pipesCount === 1 && state.pipes[0] === dest && src.listenerCount('data') === 1 && !cleanedUp) {
548+
// => Check whether `dest` is still a piping destination.
549+
if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
537550
debug('false write response, pause', src._readableState.awaitDrain);
538551
src._readableState.awaitDrain++;
539552
}
@@ -549,9 +562,9 @@ Readable.prototype.pipe = function (dest, pipeOpts) {
549562
dest.removeListener('error', onerror);
550563
if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
551564
}
552-
// This is a brutally ugly hack to make sure that our error handler
553-
// is attached before any userland ones. NEVER DO THIS.
554-
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];
565+
566+
// Make sure our error handler is attached before userland ones.
567+
prependListener(dest, 'error', onerror);
555568

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

830843
var c = 0;
831844
for (var i = 0, l = list.length; i < l && c < n; i++) {
832-
var buf = list[0];
833-
var cpy = Math.min(n - c, buf.length);
845+
var _buf = list[0];
846+
var cpy = Math.min(n - c, _buf.length);
834847

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

837-
if (cpy < buf.length) list[0] = buf.slice(cpy);else list.shift();
850+
if (cpy < _buf.length) list[0] = _buf.slice(cpy);else list.shift();
838851

839852
c += cpy;
840853
}
@@ -849,7 +862,7 @@ function endReadable(stream) {
849862

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

854867
if (!state.endEmitted) {
855868
state.ended = true;

0 commit comments

Comments
 (0)