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 9, 2021
1 parent 7b2acc5 commit fd2a38d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/write-entry.js
Expand Up @@ -144,8 +144,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
ctime: this.portable ? null : this.stat.ctime
})

if (this.header.encode() && !this.noPax)
this.write(new Pax({
if (this.header.encode() && !this.noPax) {
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 @@ -159,7 +159,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
ino: this.portable ? null : this.stat.ino,
nlink: this.portable ? null : this.stat.nlink
}).encode())
this.write(this.header.block)
}
super.write(this.header.block)
}

[DIRECTORY] () {
Expand Down Expand Up @@ -280,10 +281,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 @@ -296,10 +293,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](/* istanbul ignore next - legacy */
er => er ? this.emit('error', er) : this.end())
}
Expand Down Expand Up @@ -454,7 +464,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
Expand Up @@ -919,6 +919,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 fd2a38d

Please sign in to comment.