22
33import stream from 'stream' ;
44import utils from '../utils.js' ;
5- import throttle from './throttle.js' ;
6- import speedometer from './speedometer.js' ;
75
86const kInternals = Symbol ( 'internals' ) ;
97
@@ -24,12 +22,8 @@ class AxiosTransformStream extends stream.Transform{
2422 readableHighWaterMark : options . chunkSize
2523 } ) ;
2624
27- const self = this ;
28-
2925 const internals = this [ kInternals ] = {
30- length : options . length ,
3126 timeWindow : options . timeWindow ,
32- ticksRate : options . ticksRate ,
3327 chunkSize : options . chunkSize ,
3428 maxRate : options . maxRate ,
3529 minChunkSize : options . minChunkSize ,
@@ -41,48 +35,13 @@ class AxiosTransformStream extends stream.Transform{
4135 onReadCallback : null
4236 } ;
4337
44- const _speedometer = speedometer ( internals . ticksRate * options . samplesCount , internals . timeWindow ) ;
45-
4638 this . on ( 'newListener' , event => {
4739 if ( event === 'progress' ) {
4840 if ( ! internals . isCaptured ) {
4941 internals . isCaptured = true ;
5042 }
5143 }
5244 } ) ;
53-
54- let bytesNotified = 0 ;
55-
56- internals . updateProgress = throttle ( function throttledHandler ( ) {
57- const totalBytes = internals . length ;
58- const bytesTransferred = internals . bytesSeen ;
59- const progressBytes = bytesTransferred - bytesNotified ;
60- if ( ! progressBytes || self . destroyed ) return ;
61-
62- const rate = _speedometer ( progressBytes ) ;
63-
64- bytesNotified = bytesTransferred ;
65-
66- process . nextTick ( ( ) => {
67- self . emit ( 'progress' , {
68- loaded : bytesTransferred ,
69- total : totalBytes ,
70- progress : totalBytes ? ( bytesTransferred / totalBytes ) : undefined ,
71- bytes : progressBytes ,
72- rate : rate ? rate : undefined ,
73- estimated : rate && totalBytes && bytesTransferred <= totalBytes ?
74- ( totalBytes - bytesTransferred ) / rate : undefined ,
75- lengthComputable : totalBytes != null
76- } ) ;
77- } ) ;
78- } , internals . ticksRate ) ;
79-
80- const onFinish = ( ) => {
81- internals . updateProgress . call ( true ) ;
82- } ;
83-
84- this . once ( 'end' , onFinish ) ;
85- this . once ( 'error' , onFinish ) ;
8645 }
8746
8847 _read ( size ) {
@@ -96,7 +55,6 @@ class AxiosTransformStream extends stream.Transform{
9655 }
9756
9857 _transform ( chunk , encoding , callback ) {
99- const self = this ;
10058 const internals = this [ kInternals ] ;
10159 const maxRate = internals . maxRate ;
10260
@@ -108,16 +66,14 @@ class AxiosTransformStream extends stream.Transform{
10866 const bytesThreshold = ( maxRate / divider ) ;
10967 const minChunkSize = internals . minChunkSize !== false ? Math . max ( internals . minChunkSize , bytesThreshold * 0.01 ) : 0 ;
11068
111- function pushChunk ( _chunk , _callback ) {
69+ const pushChunk = ( _chunk , _callback ) => {
11270 const bytes = Buffer . byteLength ( _chunk ) ;
11371 internals . bytesSeen += bytes ;
11472 internals . bytes += bytes ;
11573
116- if ( internals . isCaptured ) {
117- internals . updateProgress ( ) ;
118- }
74+ internals . isCaptured && this . emit ( 'progress' , internals . bytesSeen ) ;
11975
120- if ( self . push ( _chunk ) ) {
76+ if ( this . push ( _chunk ) ) {
12177 process . nextTick ( _callback ) ;
12278 } else {
12379 internals . onReadCallback = ( ) => {
@@ -182,11 +138,6 @@ class AxiosTransformStream extends stream.Transform{
182138 }
183139 } ) ;
184140 }
185-
186- setLength ( length ) {
187- this [ kInternals ] . length = + length ;
188- return this ;
189- }
190141}
191142
192143export default AxiosTransformStream ;
0 commit comments