前幾天在維護一個nodejs寫的命令行工具,要增加一個壓縮zip文件時加密碼功能。壓縮文件時使用了 archiver 庫,加密碼使用了 archiver-zip-encrypted 庫。在windows系統上測試時,發現會概率的出現以下異常:
events.js:174 throw er; // Unhandled 'error' event ^Error: file data stream has unexpected number of bytes at ByteCounter. (xxx/node_modules/yazl/index.js:162:99) at ByteCounter.emit (events.js:194:15) at endReadableNT (_stream_readable.js:1103:12) at process._tickCallback (internal/process/next_tick.js:63:19) Emitted 'error' event at: at ByteCounter. (xxx/node_modules/yazl/index.js:162:85) at ByteCounter.emit (events.js:194:15) at endReadableNT (_stream_readable.js:1103:12) at process._tickCallback (internal/process/next_t
我的本機環境是:
npm:6.9.0
node: v10.16.3
在另外一個同事的windows系統上測試,他那邊是上面異常必現,對應的node版本是v10.15。
具體使用的代碼不貼了,基本上是參照官方 demo 來寫的,壓縮完成最后調用代碼如下所示:
archive.finalize().then(() => { // 到這里認為是壓縮完成,進行后續處理,實際并沒有,參照后面分析 anotherProcess();}).catch(err => { // 壓縮出現異常處理...});出現異常后一一檢查代碼和官方demo不一樣的地方,并沒有發現什么異常之處,網上搜索也沒有發現這種異常記錄。由于剛接觸JS,不是很熟,就從問題開始下手,找到出現問題的代碼,開始調試。
錯誤日志中提示是在 yzal/index.js 文件中發生異常,找到出現異常的代碼如下所示:
function pumpFileDataReadStream(self, entry, readStream) { var crc32Watcher = new Crc32Watcher(); var uncompressedSizeCounter = new ByteCounter(); var compressor = entry.compress ? new zlib.DeflateRaw() : new PassThrough(); var compressedSizeCounter = new ByteCounter(); readStream.pipe(crc32Watcher) .pipe(uncompressedSizeCounter) .pipe(compressor) .pipe(compressedSizeCounter) .pipe(self.outputStream, {end: false}); compressedSizeCounter.on("end", function() { entry.crc32 = crc32Watcher.crc32; if (entry.uncompressedSize == null) { entry.uncompressedSize = uncompressedSizeCounter.byteCount; } else { // 異常從這里拋出來的 if (entry.uncompressedSize !== uncompressedSizeCounter.byteCount) return self.emit("error", new Error("file data stream has unexpected number of bytes")); } entry.compressedSize = compressedSizeCounter.byteCount; self.outputStreamCursor += entry.compressedSize; writeToOutputStream(self, entry.getDataDescriptor()); entry.state = Entry.FILE_DATA_DONE; pumpEntries(self); });}從上面代碼可以看出來: uncompressedSizeCounter.byteCount 是從 pumpFileDataReadStream()
新聞熱點
疑難解答
圖片精選