Skip to content

Commit

Permalink
Merge pull request #734 from JayFate/master
Browse files Browse the repository at this point in the history
fix: duplicate require DataLengthProbe, utils
  • Loading branch information
Stuk committed Jan 16, 2021
2 parents 25d401e + e534454 commit 3db5fdc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
31 changes: 15 additions & 16 deletions lib/compressedObject.js
Expand Up @@ -2,7 +2,6 @@

var external = require("./external");
var DataWorker = require('./stream/DataWorker');
var DataLengthProbe = require('./stream/DataLengthProbe');
var Crc32Probe = require('./stream/Crc32Probe');
var DataLengthProbe = require('./stream/DataLengthProbe');

Expand All @@ -28,14 +27,14 @@ CompressedObject.prototype = {
* Create a worker to get the uncompressed content.
* @return {GenericWorker} the worker.
*/
getContentWorker : function () {
getContentWorker: function () {
var worker = new DataWorker(external.Promise.resolve(this.compressedContent))
.pipe(this.compression.uncompressWorker())
.pipe(new DataLengthProbe("data_length"));
.pipe(this.compression.uncompressWorker())
.pipe(new DataLengthProbe("data_length"));

var that = this;
worker.on("end", function () {
if(this.streamInfo['data_length'] !== that.uncompressedSize) {
if (this.streamInfo['data_length'] !== that.uncompressedSize) {
throw new Error("Bug : uncompressed data size mismatch");
}
});
Expand All @@ -45,13 +44,13 @@ CompressedObject.prototype = {
* Create a worker to get the compressed content.
* @return {GenericWorker} the worker.
*/
getCompressedWorker : function () {
getCompressedWorker: function () {
return new DataWorker(external.Promise.resolve(this.compressedContent))
.withStreamInfo("compressedSize", this.compressedSize)
.withStreamInfo("uncompressedSize", this.uncompressedSize)
.withStreamInfo("crc32", this.crc32)
.withStreamInfo("compression", this.compression)
;
.withStreamInfo("compressedSize", this.compressedSize)
.withStreamInfo("uncompressedSize", this.uncompressedSize)
.withStreamInfo("crc32", this.crc32)
.withStreamInfo("compression", this.compression)
;
}
};

Expand All @@ -65,11 +64,11 @@ CompressedObject.prototype = {
*/
CompressedObject.createWorkerFrom = function (uncompressedWorker, compression, compressionOptions) {
return uncompressedWorker
.pipe(new Crc32Probe())
.pipe(new DataLengthProbe("uncompressedSize"))
.pipe(compression.compressWorker(compressionOptions))
.pipe(new DataLengthProbe("compressedSize"))
.withStreamInfo("compression", compression);
.pipe(new Crc32Probe())
.pipe(new DataLengthProbe("uncompressedSize"))
.pipe(compression.compressWorker(compressionOptions))
.pipe(new DataLengthProbe("compressedSize"))
.withStreamInfo("compression", compression);
};

module.exports = CompressedObject;
83 changes: 41 additions & 42 deletions lib/load.js
Expand Up @@ -2,7 +2,6 @@
var utils = require('./utils');
var external = require("./external");
var utf8 = require('./utf8');
var utils = require('./utils');
var ZipEntries = require('./zipEntries');
var Crc32Probe = require('./stream/Crc32Probe');
var nodejsUtils = require("./nodejsUtils");
Expand All @@ -18,18 +17,18 @@ function checkEntryCRC32(zipEntry) {
worker.on("error", function (e) {
reject(e);
})
.on("end", function () {
if (worker.streamInfo.crc32 !== zipEntry.decompressed.crc32) {
reject(new Error("Corrupted zip : CRC32 mismatch"));
} else {
resolve();
}
})
.resume();
.on("end", function () {
if (worker.streamInfo.crc32 !== zipEntry.decompressed.crc32) {
reject(new Error("Corrupted zip : CRC32 mismatch"));
} else {
resolve();
}
})
.resume();
});
}

module.exports = function(data, options) {
module.exports = function (data, options) {
var zip = this;
options = utils.extend(options || {}, {
base64: false,
Expand All @@ -44,39 +43,39 @@ module.exports = function(data, options) {
}

return utils.prepareContent("the loaded zip file", data, true, options.optimizedBinaryString, options.base64)
.then(function(data) {
var zipEntries = new ZipEntries(options);
zipEntries.load(data);
return zipEntries;
}).then(function checkCRC32(zipEntries) {
var promises = [external.Promise.resolve(zipEntries)];
var files = zipEntries.files;
if (options.checkCRC32) {
.then(function (data) {
var zipEntries = new ZipEntries(options);
zipEntries.load(data);
return zipEntries;
}).then(function checkCRC32(zipEntries) {
var promises = [external.Promise.resolve(zipEntries)];
var files = zipEntries.files;
if (options.checkCRC32) {
for (var i = 0; i < files.length; i++) {
promises.push(checkEntryCRC32(files[i]));
}
}
return external.Promise.all(promises);
}).then(function addFiles(results) {
var zipEntries = results.shift();
var files = zipEntries.files;
for (var i = 0; i < files.length; i++) {
promises.push(checkEntryCRC32(files[i]));
var input = files[i];
zip.file(input.fileNameStr, input.decompressed, {
binary: true,
optimizedBinaryString: true,
date: input.date,
dir: input.dir,
comment: input.fileCommentStr.length ? input.fileCommentStr : null,
unixPermissions: input.unixPermissions,
dosPermissions: input.dosPermissions,
createFolders: options.createFolders
});
}
if (zipEntries.zipComment.length) {
zip.comment = zipEntries.zipComment;
}
}
return external.Promise.all(promises);
}).then(function addFiles(results) {
var zipEntries = results.shift();
var files = zipEntries.files;
for (var i = 0; i < files.length; i++) {
var input = files[i];
zip.file(input.fileNameStr, input.decompressed, {
binary: true,
optimizedBinaryString: true,
date: input.date,
dir: input.dir,
comment : input.fileCommentStr.length ? input.fileCommentStr : null,
unixPermissions : input.unixPermissions,
dosPermissions : input.dosPermissions,
createFolders: options.createFolders
});
}
if (zipEntries.zipComment.length) {
zip.comment = zipEntries.zipComment;
}

return zip;
});
return zip;
});
};

0 comments on commit 3db5fdc

Please sign in to comment.