Skip to content

Commit

Permalink
@tus/server: do not emit chunkFinished on error (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored Jun 16, 2023
1 parent cb67592 commit 58ca5f1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/server/src/models/StreamSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class StreamSplitter extends stream.Writable {
this.directory = directory
this.filenameTemplate = randomString(10)
this.part = 0
this.on('error', this._finishChunk.bind(this))
this.on('error', this._handleError.bind(this))
}

async _write(chunk: Buffer, _: BufferEncoding, callback: Callback) {
Expand Down Expand Up @@ -85,6 +85,19 @@ export class StreamSplitter extends stream.Writable {
this.currentChunkSize += chunk.length
}

async _handleError() {
// If there was an error, we want to stop allowing to write on disk as we cannot advance further.
// At this point the chunk might be incomplete advancing further might cause data loss.
// some scenarios where this might happen is if the disk is full or if we abort the stream midway.
if (this.fileHandle === null) {
return
}

await this.fileHandle.close()
this.currentChunkPath = null
this.fileHandle = null
}

async _finishChunk(): Promise<void> {
if (this.fileHandle === null) {
return
Expand All @@ -96,6 +109,7 @@ export class StreamSplitter extends stream.Writable {
path: this.currentChunkPath,
size: this.currentChunkSize,
})

this.currentChunkPath = null
this.fileHandle = null
this.currentChunkSize = 0
Expand Down

0 comments on commit 58ca5f1

Please sign in to comment.