@@ -10,72 +10,44 @@ module.exports = class OutputRequest {
1010 }
1111
1212 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 } ) ;
1414 }
1515
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 ,
2121 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+ } ) ;
2936
30- throw e ;
31- }
32- }
37+ if ( this . #needToByWritten ( this . #options ) ) {
38+ requestOutputStream . write ( this . #options . body ) ;
39+ }
3340
34- #sendRequestOutputStream( requestOutputStream , options , reject ) {
35- try {
36- requestOutputStream . once ( 'error' , e => reject ( e ) ) ;
41+ requestOutputStream . end ( ) ;
3742
38- if ( this . #needToByWritten ( options ) ) {
39- requestOutputStream . write ( options . body ) ;
43+ } catch ( e ) {
44+ reject ( new Error ( e . message , { cause : 'INVALID_REQUEST' } ) ) ;
4045 }
41-
42- requestOutputStream . end ( ) ;
43-
44- } catch ( e ) {
45- reject ( e ) ;
46- }
46+ } ) ;
4747 }
4848
4949 #needToByWritten( options ) {
5050 return [ 'POST' , 'PUT' ] . some ( method => method === options . method . toString ( ) . toUpperCase ( ) )
5151 && ( options . body != null && typeof options . body === 'string' ) ;
5252 }
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- }
8153} ;
0 commit comments