Skip to content

Commit c970858

Browse files
authored
@tus/s3-store: fix zero byte files (#700)
1 parent d1b1d42 commit c970858

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

.changeset/gold-adults-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tus/s3-store": patch
3+
---
4+
5+
Fix zero byte files only storing a .info file. Now correctly stores an empty file.

packages/s3-store/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class S3Store extends DataStore {
185185
'upload-id': Metadata?.['upload-id'] as string,
186186
file: new Upload({
187187
id,
188-
size: file.size ? Number.parseInt(file.size, 10) : undefined,
188+
size: Number.isFinite(file.size) ? Number.parseInt(file.size, 10) : undefined,
189189
offset: Number.parseInt(file.offset, 10),
190190
metadata: file.metadata,
191191
creation_date: file.creation_date,

packages/s3-store/test/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,42 @@ describe('S3DataStore', () => {
242242
}
243243
})
244244

245+
it('should successfully upload a zero byte file', async function () {
246+
const store = this.datastore as S3Store
247+
const size = 0
248+
const upload = new Upload({
249+
id: shared.testId('zero-byte-file'),
250+
size,
251+
offset: 0,
252+
})
253+
254+
await store.create(upload)
255+
256+
const offset = await store.write(
257+
Readable.from(Buffer.alloc(size)),
258+
upload.id,
259+
upload.offset
260+
)
261+
assert.equal(offset, size, 'Write should return 0 offset')
262+
263+
// Check .info file via getUpload
264+
const finalUpload = await store.getUpload(upload.id)
265+
assert.equal(finalUpload.offset, size, '.info file should show 0 offset')
266+
267+
// @ts-expect-error private
268+
const s3Client = store.client
269+
try {
270+
const headResult = await s3Client.getObject({
271+
Bucket: s3ClientConfig.bucket,
272+
Key: upload.id,
273+
})
274+
275+
assert.equal(headResult.ContentLength, size, 'File should exist in S3 with 0 bytes')
276+
} catch (error) {
277+
assert.fail(`Zero byte file was not uploaded to S3: ${error.message}`)
278+
}
279+
})
280+
245281
shared.shouldHaveStoreMethods()
246282
shared.shouldCreateUploads()
247283
shared.shouldRemoveUploads() // Termination extension

0 commit comments

Comments
 (0)