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