11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ciconst fs = require('fs'); 71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst fn = fixtures.path('elipses.txt'); 101cb0ef41Sopenharmony_ciconst rangeFile = fixtures.path('x.txt'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci{ 131cb0ef41Sopenharmony_ci let paused = false; 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci const file = fs.ReadStream(fn); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci file.on('open', common.mustCall(function(fd) { 181cb0ef41Sopenharmony_ci file.length = 0; 191cb0ef41Sopenharmony_ci assert.strictEqual(typeof fd, 'number'); 201cb0ef41Sopenharmony_ci assert.ok(file.readable); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci // GH-535 231cb0ef41Sopenharmony_ci file.pause(); 241cb0ef41Sopenharmony_ci file.resume(); 251cb0ef41Sopenharmony_ci file.pause(); 261cb0ef41Sopenharmony_ci file.resume(); 271cb0ef41Sopenharmony_ci })); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci file.on('data', common.mustCallAtLeast(function(data) { 301cb0ef41Sopenharmony_ci assert.ok(data instanceof Buffer); 311cb0ef41Sopenharmony_ci assert.ok(!paused); 321cb0ef41Sopenharmony_ci file.length += data.length; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci paused = true; 351cb0ef41Sopenharmony_ci file.pause(); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci setTimeout(function() { 381cb0ef41Sopenharmony_ci paused = false; 391cb0ef41Sopenharmony_ci file.resume(); 401cb0ef41Sopenharmony_ci }, 10); 411cb0ef41Sopenharmony_ci })); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci file.on('end', common.mustCall()); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci file.on('close', common.mustCall(function() { 481cb0ef41Sopenharmony_ci assert.strictEqual(file.length, 30000); 491cb0ef41Sopenharmony_ci })); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci{ 531cb0ef41Sopenharmony_ci const file = fs.createReadStream(fn, Object.create({ encoding: 'utf8' })); 541cb0ef41Sopenharmony_ci file.length = 0; 551cb0ef41Sopenharmony_ci file.on('data', function(data) { 561cb0ef41Sopenharmony_ci assert.strictEqual(typeof data, 'string'); 571cb0ef41Sopenharmony_ci file.length += data.length; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci for (let i = 0; i < data.length; i++) { 601cb0ef41Sopenharmony_ci // http://www.fileformat.info/info/unicode/char/2026/index.htm 611cb0ef41Sopenharmony_ci assert.strictEqual(data[i], '\u2026'); 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci }); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci file.on('close', common.mustCall(function() { 661cb0ef41Sopenharmony_ci assert.strictEqual(file.length, 10000); 671cb0ef41Sopenharmony_ci })); 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci{ 711cb0ef41Sopenharmony_ci const options = Object.create({ bufferSize: 1, start: 1, end: 2 }); 721cb0ef41Sopenharmony_ci const file = fs.createReadStream(rangeFile, options); 731cb0ef41Sopenharmony_ci assert.strictEqual(file.start, 1); 741cb0ef41Sopenharmony_ci assert.strictEqual(file.end, 2); 751cb0ef41Sopenharmony_ci let contentRead = ''; 761cb0ef41Sopenharmony_ci file.on('data', function(data) { 771cb0ef41Sopenharmony_ci contentRead += data.toString('utf-8'); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 801cb0ef41Sopenharmony_ci assert.strictEqual(contentRead, 'yz'); 811cb0ef41Sopenharmony_ci })); 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci{ 851cb0ef41Sopenharmony_ci const options = Object.create({ bufferSize: 1, start: 1 }); 861cb0ef41Sopenharmony_ci const file = fs.createReadStream(rangeFile, options); 871cb0ef41Sopenharmony_ci assert.strictEqual(file.start, 1); 881cb0ef41Sopenharmony_ci file.data = ''; 891cb0ef41Sopenharmony_ci file.on('data', function(data) { 901cb0ef41Sopenharmony_ci file.data += data.toString('utf-8'); 911cb0ef41Sopenharmony_ci }); 921cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 931cb0ef41Sopenharmony_ci assert.strictEqual(file.data, 'yz\n'); 941cb0ef41Sopenharmony_ci })); 951cb0ef41Sopenharmony_ci} 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/2320 981cb0ef41Sopenharmony_ci{ 991cb0ef41Sopenharmony_ci const options = Object.create({ bufferSize: 1.23, start: 1 }); 1001cb0ef41Sopenharmony_ci const file = fs.createReadStream(rangeFile, options); 1011cb0ef41Sopenharmony_ci assert.strictEqual(file.start, 1); 1021cb0ef41Sopenharmony_ci file.data = ''; 1031cb0ef41Sopenharmony_ci file.on('data', function(data) { 1041cb0ef41Sopenharmony_ci file.data += data.toString('utf-8'); 1051cb0ef41Sopenharmony_ci }); 1061cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 1071cb0ef41Sopenharmony_ci assert.strictEqual(file.data, 'yz\n'); 1081cb0ef41Sopenharmony_ci })); 1091cb0ef41Sopenharmony_ci} 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci{ 1121cb0ef41Sopenharmony_ci const message = 1131cb0ef41Sopenharmony_ci 'The value of "start" is out of range. It must be <= "end" (here: 2).' + 1141cb0ef41Sopenharmony_ci ' Received 10'; 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci assert.throws( 1171cb0ef41Sopenharmony_ci () => { 1181cb0ef41Sopenharmony_ci fs.createReadStream(rangeFile, Object.create({ start: 10, end: 2 })); 1191cb0ef41Sopenharmony_ci }, 1201cb0ef41Sopenharmony_ci { 1211cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1221cb0ef41Sopenharmony_ci message, 1231cb0ef41Sopenharmony_ci name: 'RangeError' 1241cb0ef41Sopenharmony_ci }); 1251cb0ef41Sopenharmony_ci} 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci{ 1281cb0ef41Sopenharmony_ci const options = Object.create({ start: 0, end: 0 }); 1291cb0ef41Sopenharmony_ci const stream = fs.createReadStream(rangeFile, options); 1301cb0ef41Sopenharmony_ci assert.strictEqual(stream.start, 0); 1311cb0ef41Sopenharmony_ci assert.strictEqual(stream.end, 0); 1321cb0ef41Sopenharmony_ci stream.data = ''; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci stream.on('data', function(chunk) { 1351cb0ef41Sopenharmony_ci stream.data += chunk; 1361cb0ef41Sopenharmony_ci }); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci stream.on('end', common.mustCall(function() { 1391cb0ef41Sopenharmony_ci assert.strictEqual(stream.data, 'x'); 1401cb0ef41Sopenharmony_ci })); 1411cb0ef41Sopenharmony_ci} 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci// Pause and then resume immediately. 1441cb0ef41Sopenharmony_ci{ 1451cb0ef41Sopenharmony_ci const pauseRes = fs.createReadStream(rangeFile); 1461cb0ef41Sopenharmony_ci pauseRes.pause(); 1471cb0ef41Sopenharmony_ci pauseRes.resume(); 1481cb0ef41Sopenharmony_ci} 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci{ 1511cb0ef41Sopenharmony_ci let data = ''; 1521cb0ef41Sopenharmony_ci let file = 1531cb0ef41Sopenharmony_ci fs.createReadStream(rangeFile, Object.create({ autoClose: false })); 1541cb0ef41Sopenharmony_ci assert.strictEqual(file.autoClose, false); 1551cb0ef41Sopenharmony_ci file.on('data', (chunk) => { data += chunk; }); 1561cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 1571cb0ef41Sopenharmony_ci process.nextTick(common.mustCall(function() { 1581cb0ef41Sopenharmony_ci assert(!file.closed); 1591cb0ef41Sopenharmony_ci assert(!file.destroyed); 1601cb0ef41Sopenharmony_ci assert.strictEqual(data, 'xyz\n'); 1611cb0ef41Sopenharmony_ci fileNext(); 1621cb0ef41Sopenharmony_ci })); 1631cb0ef41Sopenharmony_ci })); 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci function fileNext() { 1661cb0ef41Sopenharmony_ci // This will tell us if the fd is usable again or not. 1671cb0ef41Sopenharmony_ci file = fs.createReadStream(null, Object.create({ fd: file.fd, start: 0 })); 1681cb0ef41Sopenharmony_ci file.data = ''; 1691cb0ef41Sopenharmony_ci file.on('data', function(data) { 1701cb0ef41Sopenharmony_ci file.data += data; 1711cb0ef41Sopenharmony_ci }); 1721cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 1731cb0ef41Sopenharmony_ci assert.strictEqual(file.data, 'xyz\n'); 1741cb0ef41Sopenharmony_ci })); 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci process.on('exit', function() { 1771cb0ef41Sopenharmony_ci assert(file.closed); 1781cb0ef41Sopenharmony_ci assert(file.destroyed); 1791cb0ef41Sopenharmony_ci }); 1801cb0ef41Sopenharmony_ci} 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci// Just to make sure autoClose won't close the stream because of error. 1831cb0ef41Sopenharmony_ci{ 1841cb0ef41Sopenharmony_ci const options = Object.create({ fd: 13337, autoClose: false }); 1851cb0ef41Sopenharmony_ci const file = fs.createReadStream(null, options); 1861cb0ef41Sopenharmony_ci file.on('data', common.mustNotCall()); 1871cb0ef41Sopenharmony_ci file.on('error', common.mustCall()); 1881cb0ef41Sopenharmony_ci process.on('exit', function() { 1891cb0ef41Sopenharmony_ci assert(!file.closed); 1901cb0ef41Sopenharmony_ci assert(!file.destroyed); 1911cb0ef41Sopenharmony_ci assert(file.fd); 1921cb0ef41Sopenharmony_ci }); 1931cb0ef41Sopenharmony_ci} 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci// Make sure stream is destroyed when file does not exist. 1961cb0ef41Sopenharmony_ci{ 1971cb0ef41Sopenharmony_ci const file = fs.createReadStream('/path/to/file/that/does/not/exist'); 1981cb0ef41Sopenharmony_ci file.on('data', common.mustNotCall()); 1991cb0ef41Sopenharmony_ci file.on('error', common.mustCall()); 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci process.on('exit', function() { 2021cb0ef41Sopenharmony_ci assert(file.closed); 2031cb0ef41Sopenharmony_ci assert(file.destroyed); 2041cb0ef41Sopenharmony_ci }); 2051cb0ef41Sopenharmony_ci} 206