22import followRedirect from 'follow-redirects' ;
33import zlib from 'zlib' ;
44import debug from './NrcDebugger.js' ;
5+ import stream from 'stream' ;
56
67
78const { http, https} = followRedirect ;
@@ -121,16 +122,7 @@ export class NrcConnectManager {
121122 self . #configureRequest( request , requestConfig , clientRequest ) ;
122123 clientRequest . setHttpRequest ( request ) ;
123124
124- // write POST/PUT data to request body;
125- // find valid serializer to be used to serialize request data,
126- // first one found
127- // is the one to be used.if none found for match condition,
128- // default serializer is used
129- if ( options . data ) {
130- request . write ( options . data ) ;
131- }
132-
133- request . end ( ) ;
125+ self . #writeRequest( options . data , request ) ;
134126
135127 // handle request errors and handle them by request or general
136128 // error handler
@@ -204,15 +196,9 @@ export class NrcConnectManager {
204196 clientRequest . setHttpRequest ( request ) ;
205197
206198 debug ( 'options data' , options . data ) ;
207- // write POST/PUT data to request body;
208- // find valid serializer to be used to serialize request data,
209- // first one found
210- // is the one to be used.if none found for match condition,
211- // default serializer is used
212- if ( options . data ) {
213- request . write ( options . data ) ;
214- }
215- request . end ( ) ; // end request when data is written
199+
200+
201+ this . #writeRequest( options . data , request ) ;
216202
217203 // handle request errors and handle them by request or general
218204 // error handler
@@ -230,6 +216,38 @@ export class NrcConnectManager {
230216 } ) ;
231217 }
232218
219+
220+ /**
221+ * Description placeholder
222+ * @date 10/19/2023 - 7:06:36 PM
223+ *
224+ * @param {* } data
225+ * @param {* } request
226+ */
227+ #writeRequest( data , request ) {
228+ if ( data ) {
229+ if ( data instanceof stream ) {
230+ // close request when stream ends
231+ data . pipe ( request ) ;
232+ data . on ( 'end' , ( ) => {
233+ request . end ( ) ;
234+ data . close ( ) ;
235+ } ) ;
236+ } else {
237+ // write POST/PUT data to request body;
238+ // find valid serializer to be used to serialize request data,
239+ // first one found
240+ // is the one to be used.if none found for match condition,
241+ // default serializer is used
242+ request . write ( data ) ;
243+ request . end ( ) ;
244+ }
245+ } else {
246+ // no data, end request
247+ request . end ( ) ;
248+ }
249+ }
250+
233251 /**
234252 * prepare request using global or method config
235253 * @author aacerox
0 commit comments