@@ -10,72 +10,44 @@ module.exports = class OutputRequest {
10
10
}
11
11
12
12
copy ( options = this . #options, response = this . #response, http = this . #requestFunction) {
13
- return new OutputRequest ( response , http , { method : 'GET' , ...options } ) ;
13
+ return new OutputRequest ( response , http , { method : 'GET' , ...options } ) ;
14
14
}
15
15
16
- async send ( ) {
17
- try {
18
- return await new Promise ( ( resolve , reject ) => {
19
- this . #sendRequestOutputStream (
20
- this . #configureRequestOutputStream ( this . #requestFunction , this . #response , this . # options, resolve , reject ) ,
16
+ send ( ) {
17
+ return new Promise ( ( resolve , reject ) => {
18
+ try {
19
+ const requestOutputStream = this . #requestFunction (
20
+ this . #options. url ,
21
21
this . #options,
22
- reject ) ;
23
- } ) ;
24
-
25
- } catch ( e ) {
26
- if ( e . cause == null ) {
27
- throw new Error ( e . message , { cause : 'INVALID_REQUEST' } ) ;
28
- }
22
+ async ( responseInputStream ) => {
23
+ try {
24
+ resolve ( await this . #response
25
+ . copy ( responseInputStream )
26
+ . flush ( ) ) ;
27
+
28
+ } catch ( e ) {
29
+ reject ( new Error ( e . message , { cause : 'INVALID_REQUEST' } ) ) ;
30
+ }
31
+ } ) ;
32
+
33
+ requestOutputStream . once ( 'error' , e => {
34
+ reject ( new Error ( e . message , { cause : 'INVALID_REQUEST' } ) ) ;
35
+ } ) ;
29
36
30
- throw e ;
31
- }
32
- }
37
+ if ( this . #needToByWritten ( this . #options ) ) {
38
+ requestOutputStream . write ( this . #options . body ) ;
39
+ }
33
40
34
- #sendRequestOutputStream( requestOutputStream , options , reject ) {
35
- try {
36
- requestOutputStream . once ( 'error' , e => reject ( e ) ) ;
41
+ requestOutputStream . end ( ) ;
37
42
38
- if ( this . #needToByWritten ( options ) ) {
39
- requestOutputStream . write ( options . body ) ;
43
+ } catch ( e ) {
44
+ reject ( new Error ( e . message , { cause : 'INVALID_REQUEST' } ) ) ;
40
45
}
41
-
42
- requestOutputStream . end ( ) ;
43
-
44
- } catch ( e ) {
45
- reject ( e ) ;
46
- }
46
+ } ) ;
47
47
}
48
48
49
49
#needToByWritten( options ) {
50
50
return [ 'POST' , 'PUT' ] . some ( method => method === options . method . toString ( ) . toUpperCase ( ) )
51
51
&& ( options . body != null && typeof options . body === 'string' ) ;
52
52
}
53
-
54
- #configureRequestOutputStream( requestFunction , response , options , resolve , reject ) {
55
- if ( options . url != null ) {
56
- return requestFunction (
57
- options . url ,
58
- options ,
59
- async ( responseInputStream ) => {
60
- await this . #flushResponseInputStream( responseInputStream , response , resolve , reject ) ;
61
- } ) ;
62
- }
63
-
64
- return requestFunction (
65
- options ,
66
- async ( responseInputStream ) => {
67
- await this . #flushResponseInputStream( responseInputStream , response , resolve , reject ) ;
68
- } ) ;
69
- }
70
-
71
- async #flushResponseInputStream( responseInputStream , response , resolve , reject ) {
72
- try {
73
- resolve ( await response
74
- . copy ( responseInputStream )
75
- . flush ( ) ) ;
76
-
77
- } catch ( e ) {
78
- reject ( e ) ;
79
- }
80
- }
81
53
} ;
0 commit comments