• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5const assert = require('assert');
6const fs = require('fs');
7const fixtures = require('../common/fixtures');
8
9const fn = fixtures.path('elipses.txt');
10const rangeFile = fixtures.path('x.txt');
11
12{
13  let paused = false;
14
15  const file = fs.ReadStream(fn);
16
17  file.on('open', common.mustCall(function(fd) {
18    file.length = 0;
19    assert.strictEqual(typeof fd, 'number');
20    assert.ok(file.readable);
21
22    // GH-535
23    file.pause();
24    file.resume();
25    file.pause();
26    file.resume();
27  }));
28
29  file.on('data', common.mustCallAtLeast(function(data) {
30    assert.ok(data instanceof Buffer);
31    assert.ok(!paused);
32    file.length += data.length;
33
34    paused = true;
35    file.pause();
36
37    setTimeout(function() {
38      paused = false;
39      file.resume();
40    }, 10);
41  }));
42
43
44  file.on('end', common.mustCall());
45
46
47  file.on('close', common.mustCall(function() {
48    assert.strictEqual(file.length, 30000);
49  }));
50}
51
52{
53  const file = fs.createReadStream(fn, Object.create({ encoding: 'utf8' }));
54  file.length = 0;
55  file.on('data', function(data) {
56    assert.strictEqual(typeof data, 'string');
57    file.length += data.length;
58
59    for (let i = 0; i < data.length; i++) {
60      // http://www.fileformat.info/info/unicode/char/2026/index.htm
61      assert.strictEqual(data[i], '\u2026');
62    }
63  });
64
65  file.on('close', common.mustCall(function() {
66    assert.strictEqual(file.length, 10000);
67  }));
68}
69
70{
71  const options = Object.create({ bufferSize: 1, start: 1, end: 2 });
72  const file = fs.createReadStream(rangeFile, options);
73  assert.strictEqual(file.start, 1);
74  assert.strictEqual(file.end, 2);
75  let contentRead = '';
76  file.on('data', function(data) {
77    contentRead += data.toString('utf-8');
78  });
79  file.on('end', common.mustCall(function() {
80    assert.strictEqual(contentRead, 'yz');
81  }));
82}
83
84{
85  const options = Object.create({ bufferSize: 1, start: 1 });
86  const file = fs.createReadStream(rangeFile, options);
87  assert.strictEqual(file.start, 1);
88  file.data = '';
89  file.on('data', function(data) {
90    file.data += data.toString('utf-8');
91  });
92  file.on('end', common.mustCall(function() {
93    assert.strictEqual(file.data, 'yz\n');
94  }));
95}
96
97// https://github.com/joyent/node/issues/2320
98{
99  const options = Object.create({ bufferSize: 1.23, start: 1 });
100  const file = fs.createReadStream(rangeFile, options);
101  assert.strictEqual(file.start, 1);
102  file.data = '';
103  file.on('data', function(data) {
104    file.data += data.toString('utf-8');
105  });
106  file.on('end', common.mustCall(function() {
107    assert.strictEqual(file.data, 'yz\n');
108  }));
109}
110
111{
112  const message =
113    'The value of "start" is out of range. It must be <= "end" (here: 2).' +
114    ' Received 10';
115
116  assert.throws(
117    () => {
118      fs.createReadStream(rangeFile, Object.create({ start: 10, end: 2 }));
119    },
120    {
121      code: 'ERR_OUT_OF_RANGE',
122      message,
123      name: 'RangeError'
124    });
125}
126
127{
128  const options = Object.create({ start: 0, end: 0 });
129  const stream = fs.createReadStream(rangeFile, options);
130  assert.strictEqual(stream.start, 0);
131  assert.strictEqual(stream.end, 0);
132  stream.data = '';
133
134  stream.on('data', function(chunk) {
135    stream.data += chunk;
136  });
137
138  stream.on('end', common.mustCall(function() {
139    assert.strictEqual(stream.data, 'x');
140  }));
141}
142
143// Pause and then resume immediately.
144{
145  const pauseRes = fs.createReadStream(rangeFile);
146  pauseRes.pause();
147  pauseRes.resume();
148}
149
150{
151  let data = '';
152  let file =
153    fs.createReadStream(rangeFile, Object.create({ autoClose: false }));
154  assert.strictEqual(file.autoClose, false);
155  file.on('data', (chunk) => { data += chunk; });
156  file.on('end', common.mustCall(function() {
157    process.nextTick(common.mustCall(function() {
158      assert(!file.closed);
159      assert(!file.destroyed);
160      assert.strictEqual(data, 'xyz\n');
161      fileNext();
162    }));
163  }));
164
165  function fileNext() {
166    // This will tell us if the fd is usable again or not.
167    file = fs.createReadStream(null, Object.create({ fd: file.fd, start: 0 }));
168    file.data = '';
169    file.on('data', function(data) {
170      file.data += data;
171    });
172    file.on('end', common.mustCall(function() {
173      assert.strictEqual(file.data, 'xyz\n');
174    }));
175  }
176  process.on('exit', function() {
177    assert(file.closed);
178    assert(file.destroyed);
179  });
180}
181
182// Just to make sure autoClose won't close the stream because of error.
183{
184  const options = Object.create({ fd: 13337, autoClose: false });
185  const file = fs.createReadStream(null, options);
186  file.on('data', common.mustNotCall());
187  file.on('error', common.mustCall());
188  process.on('exit', function() {
189    assert(!file.closed);
190    assert(!file.destroyed);
191    assert(file.fd);
192  });
193}
194
195// Make sure stream is destroyed when file does not exist.
196{
197  const file = fs.createReadStream('/path/to/file/that/does/not/exist');
198  file.on('data', common.mustNotCall());
199  file.on('error', common.mustCall());
200
201  process.on('exit', function() {
202    assert(!file.closed);
203    assert(file.destroyed);
204  });
205}
206