Skip to content

Commit 697bebf

Browse files
authored
@tus/s3-store: forward content encoding (#899)
<!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/tus/tus-node-server/pull/899" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> Signed-off-by: Ferhat Elmas <elmas.ferhat@gmail.com>
1 parent e94dad4 commit 697bebf

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tus/s3-store": minor
3+
---
4+
5+
Forward the `contentEncoding` upload metadata to S3 as `Content-Encoding` when creating the multipart upload, matching `contentType` and `cacheControl`.

‎packages/s3-store/README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Use](#use)
1111
- [API](#api)
1212
- [`new S3Store(options)`](#new-s3storeoptions)
13+
- [Metadata](#metadata)
1314
- [Extensions](#extensions)
1415
- [Examples](#examples)
1516
- [Example: using `credentials` to fetch credentials inside a AWS container](#example-using-credentials-to-fetch-credentials-inside-a-aws-container)
@@ -138,6 +139,19 @@ overall upload bandwidth available for other concurrent uploads.
138139
throughput. Instead, it will result in additional resource consumption without
139140
proportional gains in transfer speed.
140141

142+
## Metadata
143+
144+
These upload metadata keys are forwarded to S3 as headers on the stored object:
145+
146+
| Metadata key | S3 header |
147+
| ----------------- | ------------------ |
148+
| `contentType` | `Content-Type` |
149+
| `cacheControl` | `Cache-Control` |
150+
| `contentEncoding` | `Content-Encoding` |
151+
152+
`contentEncoding` only labels the object; S3 does not compress it. Set it (e.g. `gzip`)
153+
only when the client uploads bytes that are already encoded that way.
154+
141155
## Extensions
142156

143157
The tus protocol supports optional [extensions][]. Below is a table of the supported

‎packages/s3-store/src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ export class S3Store extends DataStore {
594594
request.CacheControl = upload.metadata.cacheControl
595595
}
596596

597+
if (upload.metadata?.contentEncoding) {
598+
request.ContentEncoding = upload.metadata.contentEncoding
599+
}
600+
597601
upload.creation_date = new Date().toISOString()
598602

599603
const res = await this.client.createMultipartUpload(request)

‎packages/s3-store/src/test/index.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {AddressInfo} from 'node:net'
77
import {Readable} from 'node:stream'
88
import stream from 'node:stream/promises'
99
import {setTimeout as sleep} from 'node:timers/promises'
10+
import {gzipSync} from 'node:zlib'
1011

1112
import {NoSuchKey, NoSuchUpload, NotFound} from '@aws-sdk/client-s3'
1213
import sinon from 'sinon'
@@ -330,6 +331,32 @@ describe('S3DataStore', () => {
330331
assert.equal(await store.getIncompletePartSize(upload.id), undefined)
331332
})
332333

334+
it('should preserve content headers metadata on the completed object', async function () {
335+
const store = this.datastore as S3Store
336+
const body = gzipSync('x')
337+
const upload = new Upload({
338+
id: shared.testId('content-headers'),
339+
size: body.length,
340+
offset: 0,
341+
metadata: {
342+
contentType: 'text/plain',
343+
cacheControl: 'max-age=3600',
344+
contentEncoding: 'gzip',
345+
},
346+
})
347+
348+
await store.create(upload)
349+
const offset = await store.write(Readable.from(body), upload.id, 0)
350+
assert.equal(offset, body.length)
351+
352+
// @ts-expect-error protected property
353+
const client = store.client
354+
const head = await client.headObject({Bucket: s3ClientConfig.bucket, Key: upload.id})
355+
assert.equal(head.ContentType, 'text/plain')
356+
assert.equal(head.CacheControl, 'max-age=3600')
357+
assert.equal(head.ContentEncoding, 'gzip')
358+
})
359+
333360
for (const [S3Error, completed] of [
334361
[NoSuchUpload, true],
335362
[NoSuchKey, true],

0 commit comments

Comments
 (0)