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