Skip to content

Commit

Permalink
handle list slt streamed progressed output
Browse files Browse the repository at this point in the history
  • Loading branch information
mansurt committed Jun 22, 2017
1 parent 952ee16 commit 4bb2a17
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ module.exports = function (archive, options, filesRequiredFields) {

if (options && options.slt) {
try {
let retval = parseSltOutput(data, filesRequiredFields);
return resolve(retval);
buffer = parseSltOutputStream(data, filesRequiredFields, spec);
return progress(spec);
}
catch(err) {
return reject(retval);
return reject(err);
};
}

Expand Down Expand Up @@ -147,22 +147,39 @@ function parseProperty(data, propObj, requiredPropertyArr) {
/*
* Output of 7z with slt option contains 7z version. '--', metaData, '----------', and fileData for each archived file separated by an empty line.
* Both metaData and fileData are set of <key> = <value> separated by a new line
*
* params:
* data - cmd output to parse
* filesRequiredFields - file required field, if not null, then other fields won't be parsed
*
* return value - data to aggregate for next iteration
*/
function parseSltOutput(data, filesRequiredFields) {
let lines = data.split('\n');
let retval = {metaData: {}, files: []};
function parseSltOutputStream(data, filesRequiredFields, parsingProgress) {
let lines = data.split('\n');
var metaData = {};
let i = 0;

if (!parsingProgress.metaData) {
i = lines.indexOf(META_BEGINNING);
if (i < 0) {
throw new Error('Invalid data ' + JSON.stringify(data));
}

let i = lines.indexOf(META_BEGINNING);
if (i < 0) {
throw new Error('Invalid data ' + JSON.stringify(data));
}
for (i = i + 1; i < lines.length && lines[i] !== META_DATA_DELIM; ++i) {
if (lines[i].length === 0) {
continue;
}

for (i = i + 1; i < lines.length && lines[i] !== META_DATA_DELIM; ++i) {
if (lines[i].length === 0) {
continue;
parseProperty(lines[i], metaData, null /* shouldn't filter metaData properties */);
}

if (i >= lines.length) {
//need more data to parse matadata, returns all data to be aggregated for next iteration
return data;
}

parseProperty(lines[i], retval.metaData, null /* shouldn't filter metaData properties */);
parsingProgress.metaData = metaData;
parsingProgress.files = [];
}

//parse files
Expand All @@ -171,12 +188,17 @@ function parseSltOutput(data, filesRequiredFields) {
continue;
}
let file = {};
let startFileDataLine = i;
for (; i < lines.length && lines[i] !== FILE_DELIM; ++i) {
parseProperty(lines[i], file, filesRequiredFields);
}

retval.files.push(file);
if (i >= lines.length) {
//need more data to parse file, ignoring it and returns all file data to be aggregated for next iteration
return lines.slice(startFileDataLine - 1).join("\n");
}
parsingProgress.files.push(file);
}

return retval;
return "";
};

0 comments on commit 4bb2a17

Please sign in to comment.