@@ -104,6 +104,17 @@ function getResponseHeader(request, bucket, dataBuffer, lastModified, log) {
104104 responseMetaHeaders [ 'x-amz-request-id' ] = log . getSerializedUids ( ) ;
105105 return responseMetaHeaders ;
106106}
107+ /**
108+ * Builds a headless XML string wrapped in the standard SOSAPI XML declaration.
109+ *
110+ * @param {object } obj - JS object to serialize to XML
111+ * @returns {string } formatted XML file content
112+ */
113+ function buildXML ( obj ) {
114+ const builder = new xml2js . Builder ( { headless : true } ) ;
115+ return buildHeadXML ( builder . buildObject ( obj ) ) ;
116+ }
117+
107118/**
108119 * Generic function to respond to user with data using streams
109120 *
@@ -136,18 +147,26 @@ async function respondWithData(request, response, log, bucket, data, lastModifie
136147 }
137148
138149 response . writeHead ( 200 ) ;
139- // Use a single-element array so the Buffer is sent as one chunk rather
140- // than being iterated byte-by-byte by Readable.from.
141- await pipeline ( Readable . from ( [ dataBuffer ] ) , response ) ;
142150
143151 let contentLength = 0 ;
144152 if ( responseMetaHeaders && responseMetaHeaders [ 'Content-Length' ] ) {
145153 contentLength = responseMetaHeaders [ 'Content-Length' ] ;
146154 }
147155 log . end ( ) . addDefaultFields ( { contentLength } ) ;
148- log . end ( ) . info ( 'responded with streamed content' , {
149- httpCode : response . statusCode ,
150- } ) ;
156+
157+ try {
158+ // Use a single-element array so the Buffer is sent as one chunk rather
159+ // than being iterated byte-by-byte by Readable.from.
160+ await pipeline ( Readable . from ( [ dataBuffer ] ) , response ) ;
161+ log . end ( ) . info ( 'responded with streamed content' , {
162+ httpCode : response . statusCode ,
163+ } ) ;
164+ } catch ( err ) {
165+ log . end ( ) . error ( 'error streaming response' , {
166+ httpCode : response . statusCode ,
167+ error : err . message ,
168+ } ) ;
169+ }
151170}
152171
153172const validPath = '.system-d26a9498-cb7c-4a87-a44a-8ae204f5ba6c/' ;
@@ -278,8 +297,7 @@ async function buildVeeamFileData(request, bucketMd, log) {
278297 // ensuring no issue in a SOSAPI context. We should use this information.
279298 }
280299
281- const builder = new xml2js . Builder ( { headless : true } ) ;
282- const xmlContent = buildHeadXML ( builder . buildObject ( fileToBuild . value ) ) ;
300+ const xmlContent = buildXML ( fileToBuild . value ) ;
283301 const dataBuffer = Buffer . from ( xmlContent ) ;
284302 return { xmlContent, dataBuffer, modified, bucketData : data } ;
285303}
@@ -290,9 +308,11 @@ module.exports = {
290308 respondWithData,
291309 getResponseHeader,
292310 buildHeadXML,
311+ buildXML,
293312 validPath,
294313 isSystemXML,
295314 getFileToBuild,
296315 fetchCapacityMetrics,
297316 buildVeeamFileData,
317+ getBucket,
298318} ;
0 commit comments