Skip to content

Commit

Permalink
chore: WriteEntry cleaner write() handling
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 3, 2021
1 parent be89aaf commit 488ab8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/write-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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] () {
Expand Down Expand Up @@ -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)
Expand All @@ -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())
}

Expand Down Expand Up @@ -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()
}
})
Expand Down
14 changes: 14 additions & 0 deletions test/write-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down

0 comments on commit 488ab8c

Please sign in to comment.