Skip to content

Commit fd2a38d

Browse files
committed
chore: WriteEntry cleaner write() handling
1 parent 7b2acc5 commit fd2a38d

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Diff for: lib/write-entry.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
144144
ctime: this.portable ? null : this.stat.ctime
145145
})
146146

147-
if (this.header.encode() && !this.noPax)
148-
this.write(new Pax({
147+
if (this.header.encode() && !this.noPax) {
148+
super.write(new Pax({
149149
atime: this.portable ? null : this.header.atime,
150150
ctime: this.portable ? null : this.header.ctime,
151151
gid: this.portable ? null : this.header.gid,
@@ -159,7 +159,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
159159
ino: this.portable ? null : this.stat.ino,
160160
nlink: this.portable ? null : this.stat.nlink
161161
}).encode())
162-
this.write(this.header.block)
162+
}
163+
super.write(this.header.block)
163164
}
164165

165166
[DIRECTORY] () {
@@ -280,10 +281,6 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
280281

281282
const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
282283
this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
283-
this.remain -= writeBuf.length
284-
this.blockRemain -= writeBuf.length
285-
this.pos += writeBuf.length
286-
this.offset += writeBuf.length
287284

288285
const flushed = this.write(writeBuf)
289286
if (!flushed)
@@ -296,10 +293,23 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
296293
this.once('drain', cb)
297294
}
298295

296+
write (writeBuf) {
297+
if (this.blockRemain < writeBuf.length) {
298+
const er = new Error('writing more data than expected')
299+
er.path = this.absolute
300+
return this.emit('error', er)
301+
}
302+
this.remain -= writeBuf.length
303+
this.blockRemain -= writeBuf.length
304+
this.pos += writeBuf.length
305+
this.offset += writeBuf.length
306+
return super.write(writeBuf)
307+
}
308+
299309
[ONDRAIN] () {
300310
if (!this.remain) {
301311
if (this.blockRemain)
302-
this.write(Buffer.alloc(this.blockRemain))
312+
super.write(Buffer.alloc(this.blockRemain))
303313
return this[CLOSE](/* istanbul ignore next - legacy */
304314
er => er ? this.emit('error', er) : this.end())
305315
}
@@ -454,7 +464,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
454464

455465
end () {
456466
if (this.blockRemain)
457-
this.write(Buffer.alloc(this.blockRemain))
467+
super.write(Buffer.alloc(this.blockRemain))
458468
return super.end()
459469
}
460470
})

Diff for: test/write-entry.js

+14
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,20 @@ t.test('portable dir entries, no mtime', t => {
919919
})
920920
})
921921

922+
t.test('writing more data than is appropriate', t => {
923+
const path = t.testdir({
924+
file: 'hello',
925+
})
926+
const wss = new WriteEntry(`${path}/file`)
927+
wss.on('error', er => {
928+
t.match(er, {
929+
message: 'writing more data than expected',
930+
})
931+
t.end()
932+
})
933+
wss.on('stat', () => wss.write(Buffer.from('some more stuff')))
934+
})
935+
922936
t.test('write entry from read entry', t => {
923937
const data = makeTar([
924938
{

0 commit comments

Comments
 (0)