1
1
'use strict' ;
2
- require ( '../common' ) ;
3
- var assert = require ( 'assert' ) ;
2
+ const common = require ( '../common' ) ;
3
+ const assert = require ( 'assert' ) ;
4
4
5
5
// This test verifies that:
6
6
// 1. unshift() does not cause colliding _read() calls.
@@ -9,19 +9,19 @@ var assert = require('assert');
9
9
// 3. push() after the EOF signaling null is an error.
10
10
// 4. _read() is not called after pushing the EOF null chunk.
11
11
12
- var stream = require ( 'stream' ) ;
13
- var hwm = 10 ;
14
- var r = stream . Readable ( { highWaterMark : hwm } ) ;
15
- var chunks = 10 ;
12
+ const stream = require ( 'stream' ) ;
13
+ const hwm = 10 ;
14
+ const r = stream . Readable ( { highWaterMark : hwm } ) ;
15
+ const chunks = 10 ;
16
16
17
- var data = Buffer . allocUnsafe ( chunks * hwm + Math . ceil ( hwm / 2 ) ) ;
18
- for ( var i = 0 ; i < data . length ; i ++ ) {
19
- var c = 'asdf' . charCodeAt ( i % 4 ) ;
17
+ const data = Buffer . allocUnsafe ( chunks * hwm + Math . ceil ( hwm / 2 ) ) ;
18
+ for ( let i = 0 ; i < data . length ; i ++ ) {
19
+ const c = 'asdf' . charCodeAt ( i % 4 ) ;
20
20
data [ i ] = c ;
21
21
}
22
22
23
- var pos = 0 ;
24
- var pushedNull = false ;
23
+ let pos = 0 ;
24
+ let pushedNull = false ;
25
25
r . _read = function ( n ) {
26
26
assert ( ! pushedNull , '_read after null push' ) ;
27
27
@@ -30,7 +30,7 @@ r._read = function(n) {
30
30
31
31
function push ( fast ) {
32
32
assert ( ! pushedNull , 'push() after null push' ) ;
33
- var c = pos >= data . length ? null : data . slice ( pos , pos + n ) ;
33
+ const c = pos >= data . length ? null : data . slice ( pos , pos + n ) ;
34
34
pushedNull = c === null ;
35
35
if ( fast ) {
36
36
pos += n ;
@@ -41,59 +41,54 @@ r._read = function(n) {
41
41
pos += n ;
42
42
r . push ( c ) ;
43
43
if ( c === null ) pushError ( ) ;
44
- } ) ;
44
+ } , 1 ) ;
45
45
}
46
46
}
47
47
} ;
48
48
49
49
function pushError ( ) {
50
50
assert . throws ( function ( ) {
51
51
r . push ( Buffer . allocUnsafe ( 1 ) ) ;
52
- } ) ;
52
+ } , / ^ E r r o r : s t r e a m . p u s h \( \) a f t e r E O F $ / ) ;
53
53
}
54
54
55
55
56
- var w = stream . Writable ( ) ;
57
- var written = [ ] ;
56
+ const w = stream . Writable ( ) ;
57
+ const written = [ ] ;
58
58
w . _write = function ( chunk , encoding , cb ) {
59
59
written . push ( chunk . toString ( ) ) ;
60
60
cb ( ) ;
61
61
} ;
62
62
63
- var ended = false ;
64
- r . on ( 'end' , function ( ) {
65
- assert ( ! ended , 'end emitted more than once' ) ;
63
+ r . on ( 'end' , common . mustCall ( function ( ) {
66
64
assert . throws ( function ( ) {
67
65
r . unshift ( Buffer . allocUnsafe ( 1 ) ) ;
68
- } ) ;
69
- ended = true ;
66
+ } , / ^ E r r o r : s t r e a m .u n s h i f t \( \) a f t e r e n d e v e n t $ / ) ;
70
67
w . end ( ) ;
71
- } ) ;
68
+ } ) ) ;
72
69
73
70
r . on ( 'readable' , function ( ) {
74
- var chunk ;
71
+ let chunk ;
75
72
while ( null !== ( chunk = r . read ( 10 ) ) ) {
76
73
w . write ( chunk ) ;
77
74
if ( chunk . length > 4 )
78
75
r . unshift ( Buffer . from ( '1234' ) ) ;
79
76
}
80
77
} ) ;
81
78
82
- var finished = false ;
83
- w . on ( 'finish' , function ( ) {
84
- finished = true ;
79
+ w . on ( 'finish' , common . mustCall ( function ( ) {
85
80
// each chunk should start with 1234, and then be asfdasdfasdf...
86
81
// The first got pulled out before the first unshift('1234'), so it's
87
82
// lacking that piece.
88
- assert . equal ( written [ 0 ] , 'asdfasdfas' ) ;
89
- var asdf = 'd' ;
83
+ assert . strictEqual ( written [ 0 ] , 'asdfasdfas' ) ;
84
+ let asdf = 'd' ;
90
85
console . error ( '0: %s' , written [ 0 ] ) ;
91
- for ( var i = 1 ; i < written . length ; i ++ ) {
86
+ for ( let i = 1 ; i < written . length ; i ++ ) {
92
87
console . error ( '%s: %s' , i . toString ( 32 ) , written [ i ] ) ;
93
- assert . equal ( written [ i ] . slice ( 0 , 4 ) , '1234' ) ;
94
- for ( var j = 4 ; j < written [ i ] . length ; j ++ ) {
95
- var c = written [ i ] . charAt ( j ) ;
96
- assert . equal ( c , asdf ) ;
88
+ assert . strictEqual ( written [ i ] . slice ( 0 , 4 ) , '1234' ) ;
89
+ for ( let j = 4 ; j < written [ i ] . length ; j ++ ) {
90
+ const c = written [ i ] . charAt ( j ) ;
91
+ assert . strictEqual ( c , asdf ) ;
97
92
switch ( asdf ) {
98
93
case 'a' : asdf = 's' ; break ;
99
94
case 's' : asdf = 'd' ; break ;
@@ -102,11 +97,9 @@ w.on('finish', function() {
102
97
}
103
98
}
104
99
}
105
- } ) ;
100
+ } ) ) ;
106
101
107
102
process . on ( 'exit' , function ( ) {
108
- assert . equal ( written . length , 18 ) ;
109
- assert ( ended , 'stream ended' ) ;
110
- assert ( finished , 'stream finished' ) ;
103
+ assert . strictEqual ( written . length , 18 ) ;
111
104
console . log ( 'ok' ) ;
112
105
} ) ;
0 commit comments