@@ -31,13 +31,13 @@ process.env.TMPDIR = '/tmpdir';
process.env.TMP = '/tmp';
process.env.TEMP = '/temp';
var t = ( process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp' );
assert.equal(os.tmpDir(), '/tmpdir');
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '';
assert.equal(os.tmpDir(), '/tmp');
assert.equal(os.tmpdir(), '/tmp');
process.env.TMP = '';
assert.equal(os.tmpDir(), '/temp');
assert.equal(os.tmpdir(), '/temp');
process.env.TEMP = '';
assert.equal(os.tmpDir(), t);
assert.equal(os.tmpdir(), t);

var endianness = os.endianness();
console.log('endianness = %s', endianness);
@@ -113,6 +113,49 @@ FakeInput.prototype.end = function() {};
assert.equal(callCount, expectedLines.length - 1);
rli.close();

// \r\n should emit one line event, not two
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
expectedLines = ['foo', 'bar', 'baz', 'bat'];
callCount = 0;
rli.on('line', function(line) {
assert.equal(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\r\n'));
assert.equal(callCount, expectedLines.length - 1);
rli.close();

// \r\n should emit one line event when split across multiple writes.
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
expectedLines = ['foo', 'bar', 'baz', 'bat'];
callCount = 0;
rli.on('line', function(line) {
assert.equal(line, expectedLines[callCount]);
callCount++;
});
expectedLines.forEach(function(line) {
fi.emit('data', line + '\r');
fi.emit('data', '\n');
});
assert.equal(callCount, expectedLines.length);
rli.close();

// \r should behave like \n when alone
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
expectedLines = ['foo', 'bar', 'baz', 'bat'];
callCount = 0;
rli.on('line', function(line) {
assert.equal(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\r'));
assert.equal(callCount, expectedLines.length - 1);
rli.close();


// sending a multi-byte utf8 char over multiple writes
var buf = Buffer('☮', 'utf8');
fi = new FakeInput();
@@ -0,0 +1,118 @@
// 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.

var common = require('../common');
var assert = require('assert');

var Readable = require('stream').Readable;

test1();
test2();

function test1() {
var r = new Readable();

// should not end when we get a Buffer(0) or '' as the _read result
// that just means that there is *temporarily* no data, but to go
// ahead and try again later.
//
// note that this is very unusual. it only works for crypto streams
// because the other side of the stream will call read(0) to cycle
// data through openssl. that's why we set the timeouts to call
// r.read(0) again later, otherwise there is no more work being done
// and the process just exits.

var buf = new Buffer(5);
buf.fill('x');
var reads = 5;
r._read = function(n, cb) {
switch (reads--) {
case 0:
return cb(null, null); // EOF
case 1:
return cb(null, buf);
case 2:
setTimeout(r.read.bind(r, 0), 10);
return cb(null, new Buffer(0)); // Not-EOF!
case 3:
setTimeout(r.read.bind(r, 0), 10);
return process.nextTick(function() {
return cb(null, new Buffer(0));
});
case 4:
setTimeout(r.read.bind(r, 0), 10);
return setTimeout(function() {
return cb(null, new Buffer(0));
});
case 5:
return setTimeout(function() {
return cb(null, buf);
});
default:
throw new Error('unreachable');
}
};

var results = [];
function flow() {
var chunk;
while (null !== (chunk = r.read()))
results.push(chunk + '');
}
r.on('readable', flow);
r.on('end', function() {
results.push('EOF');
});
flow();

process.on('exit', function() {
assert.deepEqual(results, [ 'xxxxx', 'xxxxx', 'EOF' ]);
console.log('ok');
});
}

function test2() {
var r = new Readable({ encoding: 'base64' });
var reads = 5;
r._read = function(n, cb) {
if (!reads--)
return cb(null, null); // EOF
else
return cb(null, new Buffer('x'));
};

var results = [];
function flow() {
var chunk;
while (null !== (chunk = r.read()))
results.push(chunk + '');
}
r.on('readable', flow);
r.on('end', function() {
results.push('EOF');
});
flow();

process.on('exit', function() {
assert.deepEqual(results, [ 'eHh4', 'eHg=', 'EOF' ]);
console.log('ok');
});
}
@@ -61,6 +61,33 @@ process.nextTick(run);

/////

test('writable side consumption', function(t) {
var tx = new Transform({
highWaterMark: 10
});

var transformed = 0;
tx._transform = function(chunk, output, cb) {
transformed += chunk.length;
output(chunk);
cb();
};

for (var i = 1; i <= 10; i++) {
tx.write(new Buffer(i));
}
tx.end();

t.equal(tx._readableState.length, 10);
t.equal(transformed, 10);
t.equal(tx._transformState.writechunk.length, 5);
t.same(tx._writableState.buffer.map(function(c) {
return c[0].length;
}), [6, 7, 8, 9, 10]);

t.end();
});

test('passthrough', function(t) {
var pt = new PassThrough();