2011-03-10 09:54:52 +01:00
|
|
|
// 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.
|
|
|
|
|
2011-11-03 00:26:43 +01:00
|
|
|
|
2011-08-24 08:42:23 +02:00
|
|
|
|
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-03-03 12:39:41 +01:00
|
|
|
|
2010-05-25 00:47:40 +02:00
|
|
|
// TODO Improved this test. test_ca.pem is too small. A proper test would
|
|
|
|
// great a large utf8 (with multibyte chars) file and stream it in,
|
|
|
|
// performing sanity checks throughout.
|
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var fn = path.join(common.fixturesDir, 'elipses.txt');
|
|
|
|
var rangeFile = path.join(common.fixturesDir, 'x.txt');
|
2010-05-25 00:47:40 +02:00
|
|
|
|
2012-08-06 00:45:30 +02:00
|
|
|
var callbacks = { open: 0, end: 0, close: 0 };
|
2010-05-25 00:47:40 +02:00
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var paused = false;
|
2010-05-25 00:47:40 +02:00
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var file = fs.ReadStream(fn);
|
2010-05-25 00:47:40 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('open', function(fd) {
|
2010-05-27 03:32:56 +02:00
|
|
|
file.length = 0;
|
2010-05-25 00:47:40 +02:00
|
|
|
callbacks.open++;
|
|
|
|
assert.equal('number', typeof fd);
|
|
|
|
assert.ok(file.readable);
|
|
|
|
|
2011-03-29 23:54:49 +02:00
|
|
|
// GH-535
|
|
|
|
file.pause();
|
|
|
|
file.resume();
|
|
|
|
file.pause();
|
|
|
|
file.resume();
|
|
|
|
});
|
2010-05-25 00:47:40 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('data', function(data) {
|
2010-05-25 00:47:40 +02:00
|
|
|
assert.ok(data instanceof Buffer);
|
|
|
|
assert.ok(!paused);
|
2010-05-27 03:32:56 +02:00
|
|
|
file.length += data.length;
|
|
|
|
|
2010-05-25 00:47:40 +02:00
|
|
|
paused = true;
|
|
|
|
file.pause();
|
|
|
|
assert.ok(file.paused);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
paused = false;
|
|
|
|
file.resume();
|
|
|
|
assert.ok(!file.paused);
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
|
2010-05-27 03:32:56 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('end', function(chunk) {
|
2010-05-25 00:47:40 +02:00
|
|
|
callbacks.end++;
|
|
|
|
});
|
|
|
|
|
2010-05-27 03:32:56 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('close', function() {
|
2010-05-25 00:47:40 +02:00
|
|
|
callbacks.close++;
|
|
|
|
assert.ok(!file.readable);
|
|
|
|
|
2010-05-27 03:32:56 +02:00
|
|
|
//assert.equal(fs.readFileSync(fn), fileContent);
|
2010-05-25 00:47:40 +02:00
|
|
|
});
|
2010-03-03 12:39:41 +01:00
|
|
|
|
2010-09-27 19:34:16 +02:00
|
|
|
var file3 = fs.createReadStream(fn, {encoding: 'utf8'});
|
2010-05-27 03:32:56 +02:00
|
|
|
file3.length = 0;
|
2011-10-15 01:08:36 +02:00
|
|
|
file3.on('data', function(data) {
|
2010-12-05 20:15:30 +01:00
|
|
|
assert.equal('string', typeof(data));
|
2010-05-27 03:32:56 +02:00
|
|
|
file3.length += data.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
// http://www.fileformat.info/info/unicode/char/2026/index.htm
|
2010-12-05 20:15:30 +01:00
|
|
|
assert.equal('\u2026', data[i]);
|
2010-03-03 12:39:41 +01:00
|
|
|
}
|
2010-03-20 03:22:04 +01:00
|
|
|
});
|
2010-05-27 03:32:56 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file3.on('close', function() {
|
2010-05-27 03:32:56 +02:00
|
|
|
callbacks.close++;
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-05-27 03:32:56 +02:00
|
|
|
assert.equal(1, callbacks.open);
|
|
|
|
assert.equal(1, callbacks.end);
|
|
|
|
assert.equal(2, callbacks.close);
|
|
|
|
assert.equal(30000, file.length);
|
|
|
|
assert.equal(10000, file3.length);
|
|
|
|
});
|
2010-07-20 14:46:27 +02:00
|
|
|
|
2010-10-30 12:06:59 +02:00
|
|
|
var file4 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1, end: 2});
|
2010-07-20 14:46:27 +02:00
|
|
|
var contentRead = '';
|
2011-10-15 01:08:36 +02:00
|
|
|
file4.on('data', function(data) {
|
2010-10-30 12:06:59 +02:00
|
|
|
contentRead += data.toString('utf-8');
|
2010-07-20 14:46:27 +02:00
|
|
|
});
|
2011-10-15 01:08:36 +02:00
|
|
|
file4.on('end', function(data) {
|
2010-10-30 12:06:59 +02:00
|
|
|
assert.equal(contentRead, 'yz');
|
2010-07-20 14:46:27 +02:00
|
|
|
});
|
2010-09-08 21:09:13 +02:00
|
|
|
|
2011-04-05 23:37:40 +02:00
|
|
|
var file5 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1});
|
|
|
|
file5.data = '';
|
2011-10-15 01:08:36 +02:00
|
|
|
file5.on('data', function(data) {
|
2011-04-05 23:37:40 +02:00
|
|
|
file5.data += data.toString('utf-8');
|
|
|
|
});
|
2011-10-15 01:08:36 +02:00
|
|
|
file5.on('end', function() {
|
2011-04-05 23:37:40 +02:00
|
|
|
assert.equal(file5.data, 'yz\n');
|
|
|
|
});
|
|
|
|
|
2011-12-13 16:30:53 +01:00
|
|
|
// https://github.com/joyent/node/issues/2320
|
|
|
|
var file6 = fs.createReadStream(rangeFile, {bufferSize: 1.23, start: 1});
|
|
|
|
file6.data = '';
|
|
|
|
file6.on('data', function(data) {
|
|
|
|
file6.data += data.toString('utf-8');
|
|
|
|
});
|
|
|
|
file6.on('end', function() {
|
|
|
|
assert.equal(file6.data, 'yz\n');
|
|
|
|
});
|
2011-04-05 23:37:40 +02:00
|
|
|
|
|
|
|
assert.throws(function() {
|
2010-09-08 21:09:13 +02:00
|
|
|
fs.createReadStream(rangeFile, {start: 10, end: 2});
|
2011-04-05 23:37:40 +02:00
|
|
|
}, /start must be <= end/);
|
2010-09-22 19:11:37 +02:00
|
|
|
|
|
|
|
var stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
|
|
|
|
stream.data = '';
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
stream.on('data', function(chunk) {
|
2010-09-22 19:11:37 +02:00
|
|
|
stream.data += chunk;
|
|
|
|
});
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
stream.on('end', function() {
|
2010-09-22 19:11:37 +02:00
|
|
|
assert.equal('x', stream.data);
|
2010-10-30 12:06:59 +02:00
|
|
|
});
|
2011-02-18 02:38:36 +01:00
|
|
|
|
|
|
|
// pause and then resume immediately.
|
|
|
|
var pauseRes = fs.createReadStream(rangeFile);
|
|
|
|
pauseRes.pause();
|
|
|
|
pauseRes.resume();
|