2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2010-12-04 15:20:34 -08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-03-03 12:39:41 +01:00
|
|
|
|
2010-12-04 15:20:34 -08: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-24 15:47:40 -07:00
|
|
|
|
2012-08-06 00:45:30 +02:00
|
|
|
var callbacks = { open: 0, end: 0, close: 0 };
|
2010-05-24 15:47:40 -07:00
|
|
|
|
2010-12-04 15:20:34 -08:00
|
|
|
var paused = false;
|
2010-05-24 15:47:40 -07:00
|
|
|
|
2010-12-04 15:20:34 -08:00
|
|
|
var file = fs.ReadStream(fn);
|
2010-05-24 15:47:40 -07:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('open', function(fd) {
|
2010-05-26 18:32:56 -07:00
|
|
|
file.length = 0;
|
2010-05-24 15:47:40 -07:00
|
|
|
callbacks.open++;
|
|
|
|
assert.equal('number', typeof fd);
|
|
|
|
assert.ok(file.readable);
|
|
|
|
|
2011-03-29 14:54:49 -07:00
|
|
|
// GH-535
|
|
|
|
file.pause();
|
|
|
|
file.resume();
|
|
|
|
file.pause();
|
|
|
|
file.resume();
|
|
|
|
});
|
2010-05-24 15:47:40 -07:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('data', function(data) {
|
2010-05-24 15:47:40 -07:00
|
|
|
assert.ok(data instanceof Buffer);
|
|
|
|
assert.ok(!paused);
|
2010-05-26 18:32:56 -07:00
|
|
|
file.length += data.length;
|
|
|
|
|
2010-05-24 15:47:40 -07:00
|
|
|
paused = true;
|
|
|
|
file.pause();
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
paused = false;
|
|
|
|
file.resume();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
|
2010-05-26 18:32:56 -07:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('end', function(chunk) {
|
2010-05-24 15:47:40 -07:00
|
|
|
callbacks.end++;
|
|
|
|
});
|
|
|
|
|
2010-05-26 18:32:56 -07:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('close', function() {
|
2010-05-24 15:47:40 -07:00
|
|
|
callbacks.close++;
|
|
|
|
|
2010-05-26 18:32:56 -07:00
|
|
|
//assert.equal(fs.readFileSync(fn), fileContent);
|
2010-05-24 15:47:40 -07:00
|
|
|
});
|
2010-03-03 12:39:41 +01:00
|
|
|
|
2010-09-27 13:34:16 -04:00
|
|
|
var file3 = fs.createReadStream(fn, {encoding: 'utf8'});
|
2010-05-26 18:32:56 -07:00
|
|
|
file3.length = 0;
|
2011-10-15 01:08:36 +02:00
|
|
|
file3.on('data', function(data) {
|
2016-02-03 12:27:40 -08:00
|
|
|
assert.equal('string', typeof data);
|
2010-05-26 18:32:56 -07: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 22:15:30 +03:00
|
|
|
assert.equal('\u2026', data[i]);
|
2010-03-03 12:39:41 +01:00
|
|
|
}
|
2010-03-19 19:22:04 -07:00
|
|
|
});
|
2010-05-26 18:32:56 -07:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file3.on('close', function() {
|
2010-05-26 18:32:56 -07:00
|
|
|
callbacks.close++;
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-05-26 18:32:56 -07: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);
|
2012-10-04 17:44:48 -07:00
|
|
|
console.error('ok');
|
2010-05-26 18:32:56 -07:00
|
|
|
});
|
2010-07-20 18:16:27 +05:30
|
|
|
|
2010-10-30 15:36:59 +05:30
|
|
|
var file4 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1, end: 2});
|
2010-07-20 18:16:27 +05:30
|
|
|
var contentRead = '';
|
2011-10-15 01:08:36 +02:00
|
|
|
file4.on('data', function(data) {
|
2010-10-30 15:36:59 +05:30
|
|
|
contentRead += data.toString('utf-8');
|
2010-07-20 18:16:27 +05:30
|
|
|
});
|
2011-10-15 01:08:36 +02:00
|
|
|
file4.on('end', function(data) {
|
2010-10-30 15:36:59 +05:30
|
|
|
assert.equal(contentRead, 'yz');
|
2010-07-20 18:16:27 +05:30
|
|
|
});
|
2010-09-08 12:09:13 -07: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 12:09:13 -07:00
|
|
|
fs.createReadStream(rangeFile, {start: 10, end: 2});
|
2015-11-09 18:10:49 -05:00
|
|
|
}, /"start" option must be <= "end" option/);
|
2010-09-22 10:11:37 -07:00
|
|
|
|
|
|
|
var stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
|
|
|
|
stream.data = '';
|
|
|
|
|
2010-12-05 22:15:30 +03:00
|
|
|
stream.on('data', function(chunk) {
|
2010-09-22 10:11:37 -07:00
|
|
|
stream.data += chunk;
|
|
|
|
});
|
|
|
|
|
2010-12-05 22:15:30 +03:00
|
|
|
stream.on('end', function() {
|
2010-09-22 10:11:37 -07:00
|
|
|
assert.equal('x', stream.data);
|
2010-10-30 15:36:59 +05:30
|
|
|
});
|
2011-02-17 17:38:36 -08:00
|
|
|
|
|
|
|
// pause and then resume immediately.
|
|
|
|
var pauseRes = fs.createReadStream(rangeFile);
|
|
|
|
pauseRes.pause();
|
|
|
|
pauseRes.resume();
|
2012-12-17 23:03:19 +08:00
|
|
|
|
|
|
|
var file7 = fs.createReadStream(rangeFile, {autoClose: false });
|
|
|
|
file7.on('data', function() {});
|
|
|
|
file7.on('end', function() {
|
|
|
|
process.nextTick(function() {
|
|
|
|
assert(!file7.closed);
|
|
|
|
assert(!file7.destroyed);
|
|
|
|
file7Next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
function file7Next() {
|
2012-12-17 23:03:19 +08:00
|
|
|
// This will tell us if the fd is usable again or not.
|
|
|
|
file7 = fs.createReadStream(null, {fd: file7.fd, start: 0 });
|
|
|
|
file7.data = '';
|
|
|
|
file7.on('data', function(data) {
|
|
|
|
file7.data += data;
|
|
|
|
});
|
|
|
|
file7.on('end', function(err) {
|
|
|
|
assert.equal(file7.data, 'xyz\n');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just to make sure autoClose won't close the stream because of error.
|
|
|
|
var file8 = fs.createReadStream(null, {fd: 13337, autoClose: false });
|
|
|
|
file8.on('data', function() {});
|
|
|
|
file8.on('error', common.mustCall(function() {}));
|
2013-08-20 15:53:54 +02:00
|
|
|
|
|
|
|
// Make sure stream is destroyed when file does not exist.
|
|
|
|
var file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
|
|
|
|
file9.on('data', function() {});
|
|
|
|
file9.on('error', common.mustCall(function() {}));
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
assert(file7.closed);
|
|
|
|
assert(file7.destroyed);
|
|
|
|
|
|
|
|
assert(!file8.closed);
|
|
|
|
assert(!file8.destroyed);
|
|
|
|
assert(file8.fd);
|
|
|
|
|
|
|
|
assert(!file9.closed);
|
|
|
|
assert(file9.destroyed);
|
2012-12-17 23:03:19 +08:00
|
|
|
});
|