@@ -9,6 +9,7 @@ const is = require('@sindresorhus/is');
9
9
const timedOut = require ( './timed-out' ) ;
10
10
const getBodySize = require ( './get-body-size' ) ;
11
11
const getResponse = require ( './get-response' ) ;
12
+ const progress = require ( './progress' ) ;
12
13
const { CacheError, UnsupportedProtocolError, MaxRedirectsError, RequestError} = require ( './errors' ) ;
13
14
14
15
const getMethodRedirectCodes = new Set ( [ 300 , 301 , 302 , 303 , 304 , 305 , 307 , 308 ] ) ;
@@ -23,7 +24,6 @@ module.exports = (options = {}) => {
23
24
let retryTries = 0 ;
24
25
let redirectUrl ;
25
26
let uploadBodySize ;
26
- let uploaded = 0 ;
27
27
28
28
const get = options => {
29
29
if ( options . protocol !== 'http:' && options . protocol !== 'https:' ) {
@@ -43,20 +43,9 @@ module.exports = (options = {}) => {
43
43
fn = electron . net || electron . remote . net ;
44
44
}
45
45
46
- let progressInterval ;
47
-
48
46
const cacheableRequest = new CacheableRequest ( fn . request , options . cache ) ;
49
47
const cacheReq = cacheableRequest ( options , response => {
50
- clearInterval ( progressInterval ) ;
51
-
52
- emitter . emit ( 'uploadProgress' , {
53
- percent : 1 ,
54
- transferred : uploaded ,
55
- total : uploadBodySize
56
- } ) ;
57
-
58
48
const { statusCode} = response ;
59
-
60
49
response . retryCount = retryCount ;
61
50
response . url = redirectUrl || requestUrl ;
62
51
response . requestUrl = requestUrl ;
@@ -99,7 +88,6 @@ module.exports = (options = {}) => {
99
88
emitter . emit ( 'redirect' , response , redirectOpts ) ;
100
89
101
90
get ( redirectOpts ) ;
102
-
103
91
return ;
104
92
}
105
93
@@ -127,8 +115,6 @@ module.exports = (options = {}) => {
127
115
} ) ;
128
116
129
117
req . once ( 'error' , error => {
130
- clearInterval ( progressInterval ) ;
131
-
132
118
if ( aborted ) {
133
119
return ;
134
120
}
@@ -141,62 +127,9 @@ module.exports = (options = {}) => {
141
127
} ) ;
142
128
} ) ;
143
129
144
- emitter . once ( 'request' , req => {
145
- emitter . emit ( 'uploadProgress' , {
146
- percent : 0 ,
147
- transferred : 0 ,
148
- total : uploadBodySize
149
- } ) ;
150
-
151
- const socket = req . connection ;
152
- if ( socket ) {
153
- const onSocketConnect = ( ) => {
154
- const uploadEventFrequency = 150 ;
155
-
156
- progressInterval = setInterval ( ( ) => {
157
- if ( socket . destroyed ) {
158
- clearInterval ( progressInterval ) ;
159
- return ;
160
- }
161
-
162
- const lastUploaded = uploaded ;
163
- const headersSize = req . _header ? Buffer . byteLength ( req . _header ) : 0 ;
164
- uploaded = socket . bytesWritten - headersSize ;
165
-
166
- // Prevent the known issue of `bytesWritten` being larger than body size
167
- if ( uploadBodySize && uploaded > uploadBodySize ) {
168
- uploaded = uploadBodySize ;
169
- }
170
-
171
- // Don't emit events with unchanged progress and
172
- // prevent last event from being emitted, because
173
- // it's emitted when `response` is emitted
174
- if ( uploaded === lastUploaded || uploaded === uploadBodySize ) {
175
- return ;
176
- }
177
-
178
- emitter . emit ( 'uploadProgress' , {
179
- percent : uploadBodySize ? uploaded / uploadBodySize : 0 ,
180
- transferred : uploaded ,
181
- total : uploadBodySize
182
- } ) ;
183
- } , uploadEventFrequency ) ;
184
- } ;
185
-
186
- // Only subscribe to `connect` event if we're actually connecting a new
187
- // socket, otherwise if we're already connected (because this is a
188
- // keep-alive connection) do not bother. This is important since we won't
189
- // get a `connect` event for an already connected socket.
190
- if ( socket . connecting ) {
191
- socket . once ( 'connect' , onSocketConnect ) ;
192
- } else {
193
- onSocketConnect ( ) ;
194
- }
195
- }
196
- } ) ;
130
+ progress . upload ( req , emitter , uploadBodySize ) ;
197
131
198
132
if ( options . gotTimeout ) {
199
- clearInterval ( progressInterval ) ;
200
133
timedOut ( req , options . gotTimeout ) ;
201
134
}
202
135
0 commit comments