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