Skip to content

Commit

Permalink
Merge pull request #98 from chrisdickinson/backport
Browse files Browse the repository at this point in the history
backport iojs v1.0.0 streams changes
  • Loading branch information
sonewman committed Jan 24, 2015
2 parents b1036fb + 2bb6a6a commit 07604f3
Show file tree
Hide file tree
Showing 70 changed files with 864 additions and 2,284 deletions.
14 changes: 7 additions & 7 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const hyperquest = require('hyperzip')(require('hyperdirect'))
, files = require('./files')
, testReplace = require('./test-replacements')

, srcurlpfx = 'https://raw.githubusercontent.com/joyent/node/v' + process.argv[2] + '-release/'
, srcurlpfx = 'https://raw.githubusercontent.com/iojs/io.js/v' + process.argv[2] + '-release/'
, libsrcurl = srcurlpfx + 'lib/'
, testsrcurl = srcurlpfx + 'test/simple/'
, testlisturl = 'https://github.com/joyent/node/tree/v' + process.argv[2] + '-release/test/simple'
, testsrcurl = srcurlpfx + 'test/parallel/'
, testlisturl = 'https://github.com/iojs/io.js/tree/v' + process.argv[2] + '-release/test/parallel'
, libourroot = path.join(__dirname, '../lib/')
, testourroot = path.join(__dirname, '../test/simple/')
, testourroot = path.join(__dirname, '../test/parallel/')


if (!/^0\.1\d+\.\d+$/.test(process.argv[2])) {
if (!/\d\.\d\.\d+/.test(process.argv[2])) {
console.error('Usage: build.js xx.yy.zz')
return process.exit(1);
}
Expand Down Expand Up @@ -62,7 +62,7 @@ function processTestFile (file) {
}


if (!/0\.1\d\.\d+/.test(process.argv[2])) {
if (!/\d\.\d\.\d+/.test(process.argv[2])) {
console.log('Usage: build.js <node version>')
return process.exit(-1)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ hyperquest(testlisturl).pipe(bl(function (err, data) {
// Grab the joyent/node test/common.js

processFile(
testsrcurl.replace(/simple\/$/, 'common.js')
testsrcurl.replace(/parallel\/$/, 'common.js')
, path.join(testourroot, '../common.js')
, testReplace['common.js']
)
14 changes: 14 additions & 0 deletions build/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ const headRegexp = /(^module.exports = \w+;?)/m
+ '}\n/*</replacement>*/\n'
]

, deprecateReplacement = [
/util.deprecate/
, 'require(\'util-deprecate\')'
]

, objectDefinePropertyReplacement = [
/Object.defineProperties/
, 'if (Object.defineProperties) $1'
]


, isArrayDefine = [
headRegexp
, '$1\n\n/*<replacement>*/\nvar isArray = require(\'isarray\');\n/*</replacement>*/\n'
Expand Down Expand Up @@ -142,5 +153,8 @@ module.exports['_stream_writable.js'] = [
, bufferReplacement
, utilReplacement
, stringDecoderReplacement
, debugLogReplacement
, deprecateReplacement
, objectDefinePropertyReplacement
, [ /^var assert = require\('assert'\);$/m, '' ]
]
29 changes: 6 additions & 23 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// a duplex stream is just a stream that is both readable and writable.
// Since JS doesn't have multiple prototypal inheritance, this class
// prototypally inherits from Readable, and then parasitically from
// Writable.

'use strict';

module.exports = Duplex;

/*<replacement>*/
Expand All @@ -45,10 +26,12 @@ var Writable = require('./_stream_writable');

util.inherits(Duplex, Readable);

forEach(objectKeys(Writable.prototype), function(method) {
var keys = objectKeys(Writable.prototype);
for (var v = 0; v < keys.length; v++) {
var method = keys[v];
if (!Duplex.prototype[method])
Duplex.prototype[method] = Writable.prototype[method];
});
}

function Duplex(options) {
if (!(this instanceof Duplex))
Expand Down
23 changes: 2 additions & 21 deletions lib/_stream_passthrough.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// a passthrough stream.
// basically just the most minimal sort of Transform stream.
// Every written chunk gets output as-is.

'use strict';

module.exports = PassThrough;

var Transform = require('./_stream_transform');
Expand Down
62 changes: 26 additions & 36 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';

module.exports = Readable;

Expand Down Expand Up @@ -67,10 +48,17 @@ function ReadableState(options, stream) {

options = options || {};

// object stream flag. Used to make read(n) ignore n and to
// make all the buffer merging and length checks go away
this.objectMode = !!options.objectMode;

if (stream instanceof Duplex)
this.objectMode = this.objectMode || !!options.readableObjectMode;

// the point at which it stops calling _read() to fill the buffer
// Note: 0 is a valid value, means "don't call _read preemptively ever"
var hwm = options.highWaterMark;
var defaultHwm = options.objectMode ? 16 : 16 * 1024;
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;

// cast to ints.
Expand All @@ -97,14 +85,6 @@ function ReadableState(options, stream) {
this.emittedReadable = false;
this.readableListening = false;


// object stream flag. Used to make read(n) ignore n and to
// make all the buffer merging and length checks go away
this.objectMode = !!options.objectMode;

if (stream instanceof Duplex)
this.objectMode = this.objectMode || !!options.readableObjectMode;

// Crypto is kind of old and crusty. Historically, its default string
// encoding is 'binary' so we have to make this configurable.
// Everything else in the universe uses 'utf8', though.
Expand Down Expand Up @@ -168,11 +148,15 @@ Readable.prototype.unshift = function(chunk) {
return readableAddChunk(this, state, chunk, '', true);
};

Readable.prototype.isPaused = function() {
return this._readableState.flowing === false;
};

function readableAddChunk(stream, state, chunk, encoding, addToFront) {
var er = chunkInvalid(state, chunk);
if (er) {
stream.emit('error', er);
} else if (util.isNullOrUndefined(chunk)) {
} else if (chunk === null) {
state.reading = false;
if (!state.ended)
onEofChunk(stream, state);
Expand Down Expand Up @@ -261,7 +245,7 @@ function howMuchToRead(n, state) {
if (state.objectMode)
return n === 0 ? 0 : 1;

if (isNaN(n) || util.isNull(n)) {
if (util.isNull(n) || isNaN(n)) {
// only flow one buffer at a time
if (state.flowing && state.buffer.length)
return state.buffer[0].length;
Expand Down Expand Up @@ -737,10 +721,6 @@ Readable.prototype.resume = function() {
if (!state.flowing) {
debug('resume');
state.flowing = true;
if (!state.reading) {
debug('resume read 0');
this.read(0);
}
resume(this, state);
}
return this;
Expand All @@ -756,6 +736,11 @@ function resume(stream, state) {
}

function resume_(stream, state) {
if (!state.reading) {
debug('resume read 0');
stream.read(0);
}

state.resumeScheduled = false;
stream.emit('resume');
flow(stream);
Expand Down Expand Up @@ -806,7 +791,12 @@ Readable.prototype.wrap = function(stream) {
debug('wrapped data');
if (state.decoder)
chunk = state.decoder.write(chunk);
if (!chunk || !state.objectMode && !chunk.length)

// don't skip over falsy values in objectMode
//if (state.objectMode && util.isNullOrUndefined(chunk))
if (state.objectMode && (chunk === null || chunk === undefined))
return;
else if (!state.objectMode && (!chunk || !chunk.length))
return;

var ret = self.push(chunk);
Expand Down
28 changes: 4 additions & 24 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.


// a transform stream is a readable/writable stream where you do
// something with the data. Sometimes it's called a "filter",
// but that's not a great name for it, since that implies a thing where
Expand Down Expand Up @@ -62,6 +40,8 @@
// would be consumed, and then the rest would wait (un-transformed) until
// the results of the previous transformed chunk were consumed.

'use strict';

module.exports = Transform;

var Duplex = require('./_stream_duplex');
Expand All @@ -74,7 +54,7 @@ util.inherits = require('inherits');
util.inherits(Transform, Duplex);


function TransformState(options, stream) {
function TransformState(stream) {
this.afterTransform = function(er, data) {
return afterTransform(stream, er, data);
};
Expand Down Expand Up @@ -117,7 +97,7 @@ function Transform(options) {

Duplex.call(this, options);

this._transformState = new TransformState(options, this);
this._transformState = new TransformState(this);

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

0 comments on commit 07604f3

Please sign in to comment.