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