@@ -27,124 +27,119 @@ if (!common.hasCrypto)
27
27
const assert = require ( 'assert' ) ;
28
28
const crypto = require ( 'crypto' ) ;
29
29
const stream = require ( 'stream' ) ;
30
- const util = require ( 'util' ) ;
31
30
const zlib = require ( 'zlib' ) ;
32
31
33
32
const Stream = stream . Stream ;
34
33
35
34
// emit random bytes, and keep a shasum
36
- function RandomReadStream ( opt ) {
37
- Stream . call ( this ) ;
35
+ class RandomReadStream extends Stream {
36
+ constructor ( opt ) {
37
+ super ( ) ;
38
38
39
- this . readable = true ;
40
- this . _paused = false ;
41
- this . _processing = false ;
42
-
43
- this . _hasher = crypto . createHash ( 'sha1' ) ;
44
- opt = opt || { } ;
45
-
46
- // base block size.
47
- opt . block = opt . block || 256 * 1024 ;
39
+ this . readable = true ;
40
+ this . _paused = false ;
41
+ this . _processing = false ;
48
42
49
- // total number of bytes to emit
50
- opt . total = opt . total || 256 * 1024 * 1024 ;
51
- this . _remaining = opt . total ;
43
+ this . _hasher = crypto . createHash ( 'sha1' ) ;
44
+ opt = opt || { } ;
52
45
53
- // how variable to make the block sizes
54
- opt . jitter = opt . jitter || 1024 ;
46
+ // base block size.
47
+ opt . block = opt . block || 256 * 1024 ;
55
48
56
- this . _opt = opt ;
49
+ // total number of bytes to emit
50
+ opt . total = opt . total || 256 * 1024 * 1024 ;
51
+ this . _remaining = opt . total ;
57
52
58
- this . _process = this . _process . bind ( this ) ;
53
+ // how variable to make the block sizes
54
+ opt . jitter = opt . jitter || 1024 ;
59
55
60
- process . nextTick ( this . _process ) ;
61
- }
56
+ this . _opt = opt ;
62
57
63
- util . inherits ( RandomReadStream , Stream ) ;
58
+ this . _process = this . _process . bind ( this ) ;
64
59
65
- RandomReadStream . prototype . pause = function ( ) {
66
- this . _paused = true ;
67
- this . emit ( 'pause' ) ;
68
- } ;
60
+ process . nextTick ( this . _process ) ;
61
+ }
69
62
70
- RandomReadStream . prototype . resume = function ( ) {
71
- // console.error("rrs resume");
72
- this . _paused = false ;
73
- this . emit ( 'resume' ) ;
74
- this . _process ( ) ;
75
- } ;
63
+ pause ( ) {
64
+ this . _paused = true ;
65
+ this . emit ( 'pause' ) ;
66
+ }
76
67
77
- RandomReadStream . prototype . _process = function ( ) {
78
- if ( this . _processing ) return ;
79
- if ( this . _paused ) return ;
68
+ resume ( ) {
69
+ // console.error("rrs resume");
70
+ this . _paused = false ;
71
+ this . emit ( 'resume' ) ;
72
+ this . _process ( ) ;
73
+ }
80
74
81
- this . _processing = true ;
75
+ _process ( ) {
76
+ if ( this . _processing ) return ;
77
+ if ( this . _paused ) return ;
82
78
83
- if ( ! this . _remaining ) {
84
- this . _hash = this . _hasher . digest ( 'hex' ) . toLowerCase ( ) . trim ( ) ;
85
- this . _processing = false ;
79
+ this . _processing = true ;
86
80
87
- this . emit ( 'end' ) ;
88
- return ;
89
- }
81
+ if ( ! this . _remaining ) {
82
+ this . _hash = this . _hasher . digest ( 'hex' ) . toLowerCase ( ) . trim ( ) ;
83
+ this . _processing = false ;
90
84
91
- // figure out how many bytes to output
92
- // if finished, then just emit end.
93
- let block = this . _opt . block ;
94
- const jitter = this . _opt . jitter ;
95
- if ( jitter ) {
96
- block += Math . ceil ( Math . random ( ) * jitter - ( jitter / 2 ) ) ;
97
- }
98
- block = Math . min ( block , this . _remaining ) ;
99
- const buf = Buffer . allocUnsafe ( block ) ;
100
- for ( let i = 0 ; i < block ; i ++ ) {
101
- buf [ i ] = Math . random ( ) * 256 ;
102
- }
85
+ this . emit ( 'end' ) ;
86
+ return ;
87
+ }
103
88
104
- this . _hasher . update ( buf ) ;
89
+ // figure out how many bytes to output
90
+ // if finished, then just emit end.
91
+ let block = this . _opt . block ;
92
+ const jitter = this . _opt . jitter ;
93
+ if ( jitter ) {
94
+ block += Math . ceil ( Math . random ( ) * jitter - ( jitter / 2 ) ) ;
95
+ }
96
+ block = Math . min ( block , this . _remaining ) ;
97
+ const buf = Buffer . allocUnsafe ( block ) ;
98
+ for ( let i = 0 ; i < block ; i ++ ) {
99
+ buf [ i ] = Math . random ( ) * 256 ;
100
+ }
105
101
106
- this . _remaining -= block ;
102
+ this . _hasher . update ( buf ) ;
107
103
108
- console . error ( 'block=%d\nremain=%d\n' , block , this . _remaining ) ;
109
- this . _processing = false ;
104
+ this . _remaining -= block ;
110
105
111
- this . emit ( 'data' , buf ) ;
112
- process . nextTick ( this . _process ) ;
113
- } ;
106
+ this . _processing = false ;
114
107
108
+ this . emit ( 'data' , buf ) ;
109
+ process . nextTick ( this . _process ) ;
110
+ }
111
+ }
115
112
116
113
// a filter that just verifies a shasum
117
- function HashStream ( ) {
118
- Stream . call ( this ) ;
114
+ class HashStream extends Stream {
115
+ constructor ( ) {
116
+ super ( ) ;
117
+ this . readable = this . writable = true ;
118
+ this . _hasher = crypto . createHash ( 'sha1' ) ;
119
+ }
119
120
120
- this . readable = this . writable = true ;
121
- this . _hasher = crypto . createHash ( 'sha1' ) ;
122
- }
121
+ write ( c ) {
122
+ // Simulate the way that an fs.ReadStream returns false
123
+ // on *every* write, only to resume a moment later.
124
+ this . _hasher . update ( c ) ;
125
+ process . nextTick ( ( ) => this . resume ( ) ) ;
126
+ return false ;
127
+ }
128
+
129
+ resume ( ) {
130
+ this . emit ( 'resume' ) ;
131
+ process . nextTick ( ( ) => this . emit ( 'drain' ) ) ;
132
+ }
123
133
124
- util . inherits ( HashStream , Stream ) ;
125
-
126
- HashStream . prototype . write = function ( c ) {
127
- // Simulate the way that an fs.ReadStream returns false
128
- // on *every* write like a jerk, only to resume a
129
- // moment later.
130
- this . _hasher . update ( c ) ;
131
- process . nextTick ( this . resume . bind ( this ) ) ;
132
- return false ;
133
- } ;
134
-
135
- HashStream . prototype . resume = function ( ) {
136
- this . emit ( 'resume' ) ;
137
- process . nextTick ( this . emit . bind ( this , 'drain' ) ) ;
138
- } ;
139
-
140
- HashStream . prototype . end = function ( c ) {
141
- if ( c ) {
142
- this . write ( c ) ;
134
+ end ( c ) {
135
+ if ( c ) {
136
+ this . write ( c ) ;
137
+ }
138
+ this . _hash = this . _hasher . digest ( 'hex' ) . toLowerCase ( ) . trim ( ) ;
139
+ this . emit ( 'data' , this . _hash ) ;
140
+ this . emit ( 'end' ) ;
143
141
}
144
- this . _hash = this . _hasher . digest ( 'hex' ) . toLowerCase ( ) . trim ( ) ;
145
- this . emit ( 'data' , this . _hash ) ;
146
- this . emit ( 'end' ) ;
147
- } ;
142
+ }
148
143
149
144
150
145
const inp = new RandomReadStream ( { total : 1024 , block : 256 , jitter : 16 } ) ;
@@ -154,23 +149,6 @@ const gunz = zlib.createGunzip();
154
149
155
150
inp . pipe ( gzip ) . pipe ( gunz ) . pipe ( out ) ;
156
151
157
- inp . on ( 'data' , function ( c ) {
158
- console . error ( 'inp data' , c . length ) ;
159
- } ) ;
160
-
161
- gzip . on ( 'data' , function ( c ) {
162
- console . error ( 'gzip data' , c . length ) ;
163
- } ) ;
164
-
165
- gunz . on ( 'data' , function ( c ) {
166
- console . error ( 'gunz data' , c . length ) ;
167
- } ) ;
168
-
169
- out . on ( 'data' , function ( c ) {
170
- console . error ( 'out data' , c . length ) ;
171
- } ) ;
172
-
173
- out . on ( 'data' , common . mustCall ( function ( c ) {
174
- console . error ( 'hash=%s' , c ) ;
152
+ out . on ( 'data' , common . mustCall ( ( c ) => {
175
153
assert . strictEqual ( c , inp . _hash , 'hashes should match' ) ;
176
154
} ) ) ;
0 commit comments