From 488ab8c01de69379406d937419fa3e5550e651c0 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 2 Aug 2021 16:25:12 -0700 Subject: [PATCH] chore: WriteEntry cleaner write() handling --- lib/write-entry.js | 25 +++++++++++++++++-------- test/write-entry.js | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/write-entry.js b/lib/write-entry.js index 4385c040..d9f4123f 100644 --- a/lib/write-entry.js +++ b/lib/write-entry.js @@ -150,7 +150,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass { }) if (this.header.encode() && !this.noPax) { - this.write(new Pax({ + super.write(new Pax({ atime: this.portable ? null : this.header.atime, ctime: this.portable ? null : this.header.ctime, gid: this.portable ? null : this.header.gid, @@ -165,7 +165,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass { nlink: this.portable ? null : this.stat.nlink, }).encode()) } - this.write(this.header.block) + super.write(this.header.block) } [DIRECTORY] () { @@ -286,10 +286,6 @@ const WriteEntry = warner(class WriteEntry extends MiniPass { const writeBuf = this.offset === 0 && bytesRead === this.buf.length ? this.buf : this.buf.slice(this.offset, this.offset + bytesRead) - this.remain -= writeBuf.length - this.blockRemain -= writeBuf.length - this.pos += writeBuf.length - this.offset += writeBuf.length const flushed = this.write(writeBuf) if (!flushed) @@ -302,10 +298,23 @@ const WriteEntry = warner(class WriteEntry extends MiniPass { this.once('drain', cb) } + write (writeBuf) { + if (this.blockRemain < writeBuf.length) { + const er = new Error('writing more data than expected') + er.path = this.absolute + return this.emit('error', er) + } + this.remain -= writeBuf.length + this.blockRemain -= writeBuf.length + this.pos += writeBuf.length + this.offset += writeBuf.length + return super.write(writeBuf) + } + [ONDRAIN] () { if (!this.remain) { if (this.blockRemain) - this.write(Buffer.alloc(this.blockRemain)) + super.write(Buffer.alloc(this.blockRemain)) return this[CLOSE](er => er ? this.emit('error', er) : this.end()) } @@ -461,7 +470,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass { end () { if (this.blockRemain) - this.write(Buffer.alloc(this.blockRemain)) + super.write(Buffer.alloc(this.blockRemain)) return super.end() } }) diff --git a/test/write-entry.js b/test/write-entry.js index 9e5e33cd..30df5ebf 100644 --- a/test/write-entry.js +++ b/test/write-entry.js @@ -927,6 +927,20 @@ t.test('portable dir entries, no mtime', t => { }) }) +t.test('writing more data than is appropriate', t => { + const path = t.testdir({ + file: 'hello', + }) + const wss = new WriteEntry(`${path}/file`) + wss.on('error', er => { + t.match(er, { + message: 'writing more data than expected', + }) + t.end() + }) + wss.on('stat', () => wss.write(Buffer.from('some more stuff'))) +}) + t.test('write entry from read entry', t => { const data = makeTar([ {