Skip to content

Commit 7554628

Browse files
committed
♻️ factorise XML build at the same place
Issue: CLDSRV-878
1 parent 3f4a308 commit 7554628

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

lib/routes/veeam/list.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
const url = require('url');
2-
const xml2js = require('xml2js');
32
const { errors, errorInstances } = require('arsenal');
43
const querystring = require('querystring');
5-
const metadata = require('../../metadata/wrapper');
64
const { responseXMLBody } = require('arsenal/build/lib/s3routes/routesUtils');
7-
const { respondWithData, getResponseHeader, buildHeadXML, validPath, fetchCapacityMetrics } = require('./utils');
5+
const { respondWithData, getResponseHeader, buildXML, validPath, fetchCapacityMetrics, getBucket } = require('./utils');
86
const { processVersions, processMasterVersions } = require('../../api/bucketGet');
9-
const { promisify } = require('util');
10-
11-
const getBucket = promisify((...args) => metadata.getBucket(...args));
127

138
/**
149
* Utility function to build a standard response for the LIST route.
@@ -135,10 +130,7 @@ async function listVeeamFiles(request, response, bucketMd, log) {
135130
: file.LastModified;
136131
// eslint-disable-next-line no-param-reassign
137132
delete file.LastModified;
138-
const builder = new xml2js.Builder({
139-
headless: true,
140-
});
141-
const dataBuffer = Buffer.from(buildHeadXML(builder.buildObject(file)));
133+
const dataBuffer = Buffer.from(buildXML(file));
142134
filesToBuild.push({
143135
...getResponseHeader(request, data,
144136
dataBuffer, lastModified, log),

lib/routes/veeam/utils.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

153172
const 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

Comments
 (0)