2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-11-16 01:10:43 +01:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2010-03-03 12:39:41 +01:00
|
|
|
|
2016-11-16 01:10:43 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const fn = path.join(common.fixturesDir, 'elipses.txt');
|
|
|
|
const 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;
|
2016-08-01 21:31:16 +02:00
|
|
|
var bytesRead = 0;
|
2010-05-25 00:47:40 +02:00
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var file = fs.ReadStream(fn);
|
2016-08-01 21:31:16 +02:00
|
|
|
var fileSize = fs.statSync(fn).size;
|
|
|
|
|
|
|
|
assert.strictEqual(file.bytesRead, 0);
|
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++;
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual('number', typeof fd);
|
2016-08-01 21:31:16 +02:00
|
|
|
assert.strictEqual(file.bytesRead, 0);
|
2010-05-25 00:47:40 +02:00
|
|
|
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;
|
|
|
|
|
2016-08-01 21:31:16 +02:00
|
|
|
bytesRead += data.length;
|
|
|
|
assert.strictEqual(file.bytesRead, bytesRead);
|
|
|
|
|
2010-05-25 00:47:40 +02:00
|
|
|
paused = true;
|
|
|
|
file.pause();
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
paused = false;
|
|
|
|
file.resume();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
|
2010-05-27 03:32:56 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
file.on('end', function(chunk) {
|
2016-08-01 21:31:16 +02:00
|
|
|
assert.strictEqual(bytesRead, fileSize);
|
|
|
|
assert.strictEqual(file.bytesRead, fileSize);
|
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() {
|
2016-08-01 21:31:16 +02:00
|
|
|
assert.strictEqual(bytesRead, fileSize);
|
|
|
|
assert.strictEqual(file.bytesRead, fileSize);
|
2010-05-25 00:47:40 +02:00
|
|
|
callbacks.close++;
|
|
|
|
|
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) {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual('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
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual('\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() {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual(1, callbacks.open);
|
|
|
|
assert.strictEqual(1, callbacks.end);
|
|
|
|
assert.strictEqual(2, callbacks.close);
|
|
|
|
assert.strictEqual(30000, file.length);
|
|
|
|
assert.strictEqual(10000, file3.length);
|
2012-10-05 02:44:48 +02:00
|
|
|
console.error('ok');
|
2010-05-27 03:32:56 +02:00
|
|
|
});
|
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) {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual(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() {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual(file5.data, 'yz\n');
|
2011-04-05 23:37:40 +02:00
|
|
|
});
|
|
|
|
|
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() {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual(file6.data, 'yz\n');
|
2011-12-13 16:30:53 +01:00
|
|
|
});
|
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});
|
2015-11-10 00:10:49 +01:00
|
|
|
}, /"start" option must be <= "end" option/);
|
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() {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual('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();
|
2012-12-17 16:03:19 +01: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 16:03:19 +01: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) {
|
2016-11-16 01:10:43 +01:00
|
|
|
assert.strictEqual(file7.data, 'xyz\n');
|
2012-12-17 16:03:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:03:19 +01:00
|
|
|
});
|