11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst child_process = require('child_process'); 271cb0ef41Sopenharmony_ciconst assert = require('assert'); 281cb0ef41Sopenharmony_ciconst fs = require('fs'); 291cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst fn = fixtures.path('elipses.txt'); 321cb0ef41Sopenharmony_ciconst rangeFile = fixtures.path('x.txt'); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_cifunction test1(options) { 351cb0ef41Sopenharmony_ci let paused = false; 361cb0ef41Sopenharmony_ci let bytesRead = 0; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci const file = fs.createReadStream(fn, options); 391cb0ef41Sopenharmony_ci const fileSize = fs.statSync(fn).size; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesRead, 0); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci file.on('open', common.mustCall(function(fd) { 441cb0ef41Sopenharmony_ci file.length = 0; 451cb0ef41Sopenharmony_ci assert.strictEqual(typeof fd, 'number'); 461cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesRead, 0); 471cb0ef41Sopenharmony_ci assert.ok(file.readable); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci // GH-535 501cb0ef41Sopenharmony_ci file.pause(); 511cb0ef41Sopenharmony_ci file.resume(); 521cb0ef41Sopenharmony_ci file.pause(); 531cb0ef41Sopenharmony_ci file.resume(); 541cb0ef41Sopenharmony_ci })); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci file.on('data', function(data) { 571cb0ef41Sopenharmony_ci assert.ok(data instanceof Buffer); 581cb0ef41Sopenharmony_ci assert.ok(data.byteOffset % 8 === 0); 591cb0ef41Sopenharmony_ci assert.ok(!paused); 601cb0ef41Sopenharmony_ci file.length += data.length; 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci bytesRead += data.length; 631cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesRead, bytesRead); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci paused = true; 661cb0ef41Sopenharmony_ci file.pause(); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci setTimeout(function() { 691cb0ef41Sopenharmony_ci paused = false; 701cb0ef41Sopenharmony_ci file.resume(); 711cb0ef41Sopenharmony_ci }, 10); 721cb0ef41Sopenharmony_ci }); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function(chunk) { 761cb0ef41Sopenharmony_ci assert.strictEqual(bytesRead, fileSize); 771cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesRead, fileSize); 781cb0ef41Sopenharmony_ci })); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci file.on('close', common.mustCall(function() { 821cb0ef41Sopenharmony_ci assert.strictEqual(bytesRead, fileSize); 831cb0ef41Sopenharmony_ci assert.strictEqual(file.bytesRead, fileSize); 841cb0ef41Sopenharmony_ci })); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci process.on('exit', function() { 871cb0ef41Sopenharmony_ci assert.strictEqual(file.length, 30000); 881cb0ef41Sopenharmony_ci }); 891cb0ef41Sopenharmony_ci} 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_citest1({}); 921cb0ef41Sopenharmony_citest1({ 931cb0ef41Sopenharmony_ci fs: { 941cb0ef41Sopenharmony_ci open: common.mustCall(fs.open), 951cb0ef41Sopenharmony_ci read: common.mustCallAtLeast(fs.read, 1), 961cb0ef41Sopenharmony_ci close: common.mustCall(fs.close), 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci}); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci{ 1011cb0ef41Sopenharmony_ci const file = fs.createReadStream(fn, common.mustNotMutateObjectDeep({ encoding: 'utf8' })); 1021cb0ef41Sopenharmony_ci file.length = 0; 1031cb0ef41Sopenharmony_ci file.on('data', function(data) { 1041cb0ef41Sopenharmony_ci assert.strictEqual(typeof data, 'string'); 1051cb0ef41Sopenharmony_ci file.length += data.length; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci for (let i = 0; i < data.length; i++) { 1081cb0ef41Sopenharmony_ci // http://www.fileformat.info/info/unicode/char/2026/index.htm 1091cb0ef41Sopenharmony_ci assert.strictEqual(data[i], '\u2026'); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci }); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci file.on('close', common.mustCall()); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci process.on('exit', function() { 1161cb0ef41Sopenharmony_ci assert.strictEqual(file.length, 10000); 1171cb0ef41Sopenharmony_ci }); 1181cb0ef41Sopenharmony_ci} 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci{ 1211cb0ef41Sopenharmony_ci const file = 1221cb0ef41Sopenharmony_ci fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ bufferSize: 1, start: 1, end: 2 })); 1231cb0ef41Sopenharmony_ci let contentRead = ''; 1241cb0ef41Sopenharmony_ci file.on('data', function(data) { 1251cb0ef41Sopenharmony_ci contentRead += data.toString('utf-8'); 1261cb0ef41Sopenharmony_ci }); 1271cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function(data) { 1281cb0ef41Sopenharmony_ci assert.strictEqual(contentRead, 'yz'); 1291cb0ef41Sopenharmony_ci })); 1301cb0ef41Sopenharmony_ci} 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci{ 1331cb0ef41Sopenharmony_ci const file = fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ bufferSize: 1, start: 1 })); 1341cb0ef41Sopenharmony_ci file.data = ''; 1351cb0ef41Sopenharmony_ci file.on('data', function(data) { 1361cb0ef41Sopenharmony_ci file.data += data.toString('utf-8'); 1371cb0ef41Sopenharmony_ci }); 1381cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 1391cb0ef41Sopenharmony_ci assert.strictEqual(file.data, 'yz\n'); 1401cb0ef41Sopenharmony_ci })); 1411cb0ef41Sopenharmony_ci} 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci{ 1441cb0ef41Sopenharmony_ci // Ref: https://github.com/nodejs/node-v0.x-archive/issues/2320 1451cb0ef41Sopenharmony_ci const file = fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ bufferSize: 1.23, start: 1 })); 1461cb0ef41Sopenharmony_ci file.data = ''; 1471cb0ef41Sopenharmony_ci file.on('data', function(data) { 1481cb0ef41Sopenharmony_ci file.data += data.toString('utf-8'); 1491cb0ef41Sopenharmony_ci }); 1501cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 1511cb0ef41Sopenharmony_ci assert.strictEqual(file.data, 'yz\n'); 1521cb0ef41Sopenharmony_ci })); 1531cb0ef41Sopenharmony_ci} 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ciassert.throws( 1561cb0ef41Sopenharmony_ci () => { 1571cb0ef41Sopenharmony_ci fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ start: 10, end: 2 })); 1581cb0ef41Sopenharmony_ci }, 1591cb0ef41Sopenharmony_ci { 1601cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1611cb0ef41Sopenharmony_ci message: 'The value of "start" is out of range. It must be <= "end"' + 1621cb0ef41Sopenharmony_ci ' (here: 2). Received 10', 1631cb0ef41Sopenharmony_ci name: 'RangeError' 1641cb0ef41Sopenharmony_ci }); 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci{ 1671cb0ef41Sopenharmony_ci const stream = fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ start: 0, end: 0 })); 1681cb0ef41Sopenharmony_ci stream.data = ''; 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci stream.on('data', function(chunk) { 1711cb0ef41Sopenharmony_ci stream.data += chunk; 1721cb0ef41Sopenharmony_ci }); 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci stream.on('end', common.mustCall(function() { 1751cb0ef41Sopenharmony_ci assert.strictEqual(stream.data, 'x'); 1761cb0ef41Sopenharmony_ci })); 1771cb0ef41Sopenharmony_ci} 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci{ 1801cb0ef41Sopenharmony_ci // Verify that end works when start is not specified. 1811cb0ef41Sopenharmony_ci const stream = new fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ end: 1 })); 1821cb0ef41Sopenharmony_ci stream.data = ''; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci stream.on('data', function(chunk) { 1851cb0ef41Sopenharmony_ci stream.data += chunk; 1861cb0ef41Sopenharmony_ci }); 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci stream.on('end', common.mustCall(function() { 1891cb0ef41Sopenharmony_ci assert.strictEqual(stream.data, 'xy'); 1901cb0ef41Sopenharmony_ci })); 1911cb0ef41Sopenharmony_ci} 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ciif (!common.isWindows) { 1941cb0ef41Sopenharmony_ci // Verify that end works when start is not specified, and we do not try to 1951cb0ef41Sopenharmony_ci // use positioned reads. This makes sure that this keeps working for 1961cb0ef41Sopenharmony_ci // non-seekable file descriptors. 1971cb0ef41Sopenharmony_ci tmpdir.refresh(); 1981cb0ef41Sopenharmony_ci const filename = `${tmpdir.path}/foo.pipe`; 1991cb0ef41Sopenharmony_ci const mkfifoResult = child_process.spawnSync('mkfifo', [filename]); 2001cb0ef41Sopenharmony_ci if (!mkfifoResult.error) { 2011cb0ef41Sopenharmony_ci child_process.exec(`echo "xyz foobar" > '${filename}'`); 2021cb0ef41Sopenharmony_ci const stream = new fs.createReadStream(filename, common.mustNotMutateObjectDeep({ end: 1 })); 2031cb0ef41Sopenharmony_ci stream.data = ''; 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci stream.on('data', function(chunk) { 2061cb0ef41Sopenharmony_ci stream.data += chunk; 2071cb0ef41Sopenharmony_ci }); 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci stream.on('end', common.mustCall(function() { 2101cb0ef41Sopenharmony_ci assert.strictEqual(stream.data, 'xy'); 2111cb0ef41Sopenharmony_ci fs.unlinkSync(filename); 2121cb0ef41Sopenharmony_ci })); 2131cb0ef41Sopenharmony_ci } else { 2141cb0ef41Sopenharmony_ci common.printSkipMessage('mkfifo not available'); 2151cb0ef41Sopenharmony_ci } 2161cb0ef41Sopenharmony_ci} 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci{ 2191cb0ef41Sopenharmony_ci // Pause and then resume immediately. 2201cb0ef41Sopenharmony_ci const pauseRes = fs.createReadStream(rangeFile); 2211cb0ef41Sopenharmony_ci pauseRes.pause(); 2221cb0ef41Sopenharmony_ci pauseRes.resume(); 2231cb0ef41Sopenharmony_ci} 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci{ 2261cb0ef41Sopenharmony_ci let file = fs.createReadStream(rangeFile, common.mustNotMutateObjectDeep({ autoClose: false })); 2271cb0ef41Sopenharmony_ci let data = ''; 2281cb0ef41Sopenharmony_ci file.on('data', function(chunk) { data += chunk; }); 2291cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function() { 2301cb0ef41Sopenharmony_ci assert.strictEqual(data, 'xyz\n'); 2311cb0ef41Sopenharmony_ci process.nextTick(function() { 2321cb0ef41Sopenharmony_ci assert(!file.closed); 2331cb0ef41Sopenharmony_ci assert(!file.destroyed); 2341cb0ef41Sopenharmony_ci fileNext(); 2351cb0ef41Sopenharmony_ci }); 2361cb0ef41Sopenharmony_ci })); 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ci function fileNext() { 2391cb0ef41Sopenharmony_ci // This will tell us if the fd is usable again or not. 2401cb0ef41Sopenharmony_ci file = fs.createReadStream(null, common.mustNotMutateObjectDeep({ fd: file.fd, start: 0 })); 2411cb0ef41Sopenharmony_ci file.data = ''; 2421cb0ef41Sopenharmony_ci file.on('data', function(data) { 2431cb0ef41Sopenharmony_ci file.data += data; 2441cb0ef41Sopenharmony_ci }); 2451cb0ef41Sopenharmony_ci file.on('end', common.mustCall(function(err) { 2461cb0ef41Sopenharmony_ci assert.strictEqual(file.data, 'xyz\n'); 2471cb0ef41Sopenharmony_ci })); 2481cb0ef41Sopenharmony_ci process.on('exit', function() { 2491cb0ef41Sopenharmony_ci assert(file.closed); 2501cb0ef41Sopenharmony_ci assert(file.destroyed); 2511cb0ef41Sopenharmony_ci }); 2521cb0ef41Sopenharmony_ci } 2531cb0ef41Sopenharmony_ci} 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ci{ 2561cb0ef41Sopenharmony_ci // Just to make sure autoClose won't close the stream because of error. 2571cb0ef41Sopenharmony_ci const file = fs.createReadStream(null, common.mustNotMutateObjectDeep({ fd: 13337, autoClose: false })); 2581cb0ef41Sopenharmony_ci file.on('data', common.mustNotCall()); 2591cb0ef41Sopenharmony_ci file.on('error', common.mustCall()); 2601cb0ef41Sopenharmony_ci process.on('exit', function() { 2611cb0ef41Sopenharmony_ci assert(!file.closed); 2621cb0ef41Sopenharmony_ci assert(!file.destroyed); 2631cb0ef41Sopenharmony_ci assert(file.fd); 2641cb0ef41Sopenharmony_ci }); 2651cb0ef41Sopenharmony_ci} 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci{ 2681cb0ef41Sopenharmony_ci // Make sure stream is destroyed when file does not exist. 2691cb0ef41Sopenharmony_ci const file = fs.createReadStream('/path/to/file/that/does/not/exist'); 2701cb0ef41Sopenharmony_ci file.on('data', common.mustNotCall()); 2711cb0ef41Sopenharmony_ci file.on('error', common.mustCall()); 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci process.on('exit', function() { 2741cb0ef41Sopenharmony_ci assert(file.closed); 2751cb0ef41Sopenharmony_ci assert(file.destroyed); 2761cb0ef41Sopenharmony_ci }); 2771cb0ef41Sopenharmony_ci} 278