Skip to content

Commit 488ab8c

Browse files
committed
chore: WriteEntry cleaner write() handling
1 parent be89aaf commit 488ab8c

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

lib/write-entry.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
150150
})
151151

152152
if (this.header.encode() && !this.noPax) {
153-
this.write(new Pax({
153+
super.write(new Pax({
154154
atime: this.portable ? null : this.header.atime,
155155
ctime: this.portable ? null : this.header.ctime,
156156
gid: this.portable ? null : this.header.gid,
@@ -165,7 +165,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
165165
nlink: this.portable ? null : this.stat.nlink,
166166
}).encode())
167167
}
168-
this.write(this.header.block)
168+
super.write(this.header.block)
169169
}
170170

171171
[DIRECTORY] () {
@@ -286,10 +286,6 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
286286

287287
const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
288288
this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
289-
this.remain -= writeBuf.length
290-
this.blockRemain -= writeBuf.length
291-
this.pos += writeBuf.length
292-
this.offset += writeBuf.length
293289

294290
const flushed = this.write(writeBuf)
295291
if (!flushed)
@@ -302,10 +298,23 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
302298
this.once('drain', cb)
303299
}
304300

301+
write (writeBuf) {
302+
if (this.blockRemain < writeBuf.length) {
303+
const er = new Error('writing more data than expected')
304+
er.path = this.absolute
305+
return this.emit('error', er)
306+
}
307+
this.remain -= writeBuf.length
308+
this.blockRemain -= writeBuf.length
309+
this.pos += writeBuf.length
310+
this.offset += writeBuf.length
311+
return super.write(writeBuf)
312+
}
313+
305314
[ONDRAIN] () {
306315
if (!this.remain) {
307316
if (this.blockRemain)
308-
this.write(Buffer.alloc(this.blockRemain))
317+
super.write(Buffer.alloc(this.blockRemain))
309318
return this[CLOSE](er => er ? this.emit('error', er) : this.end())
310319
}
311320

@@ -461,7 +470,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
461470

462471
end () {
463472
if (this.blockRemain)
464-
this.write(Buffer.alloc(this.blockRemain))
473+
super.write(Buffer.alloc(this.blockRemain))
465474
return super.end()
466475
}
467476
})

test/write-entry.js

+14
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,20 @@ t.test('portable dir entries, no mtime', t => {
927927
})
928928
})
929929

930+
t.test('writing more data than is appropriate', t => {
931+
const path = t.testdir({
932+
file: 'hello',
933+
})
934+
const wss = new WriteEntry(`${path}/file`)
935+
wss.on('error', er => {
936+
t.match(er, {
937+
message: 'writing more data than expected',
938+
})
939+
t.end()
940+
})
941+
wss.on('stat', () => wss.write(Buffer.from('some more stuff')))
942+
})
943+
930944
t.test('write entry from read entry', t => {
931945
const data = makeTar([
932946
{

0 commit comments

Comments
 (0)