diff --git a/packages/core/storage-js/src/lib/types.ts b/packages/core/storage-js/src/lib/types.ts index b4344c58a..7a4142229 100644 --- a/packages/core/storage-js/src/lib/types.ts +++ b/packages/core/storage-js/src/lib/types.ts @@ -88,6 +88,10 @@ export interface FileOptions { * the `Content-Type` header value. Should be specified if using a `fileBody` that is neither `Blob` nor `File` nor `FormData`, otherwise will default to `text/plain;charset=UTF-8`. */ contentType?: string + /** + * The `Content-Encoding` header value (e.g., `gzip`). + */ + contentEncoding?: string /** * When upsert is set to true, the file is overwritten if it exists. When set to false, an error is thrown if the object already exists. Defaults to false. */ diff --git a/packages/core/storage-js/src/packages/StorageFileApi.ts b/packages/core/storage-js/src/packages/StorageFileApi.ts index 53ba2859a..b8e8ad5c0 100644 --- a/packages/core/storage-js/src/packages/StorageFileApi.ts +++ b/packages/core/storage-js/src/packages/StorageFileApi.ts @@ -124,6 +124,10 @@ export default class StorageFileApi { headers['cache-control'] = `max-age=${options.cacheControl}` headers['content-type'] = options.contentType as string + if (options.contentEncoding) { + headers['content-encoding'] = options.contentEncoding + } + if (metadata) { headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata)) } @@ -291,6 +295,9 @@ export default class StorageFileApi { body = fileBody headers['cache-control'] = `max-age=${options.cacheControl}` headers['content-type'] = options.contentType as string + if (options.contentEncoding) { + headers['content-encoding'] = options.contentEncoding + } } const data = await put(this.fetch, url.toString(), body as object, { headers }) diff --git a/packages/core/storage-js/test/storageFileApi.test.ts b/packages/core/storage-js/test/storageFileApi.test.ts index 645de3e00..fccff8966 100644 --- a/packages/core/storage-js/test/storageFileApi.test.ts +++ b/packages/core/storage-js/test/storageFileApi.test.ts @@ -7,6 +7,7 @@ import ReadableStream from 'node:stream' import { StorageApiError, StorageError } from '../src/lib/errors' import BlobDownloadBuilder from '../src/packages/BlobDownloadBuilder' import StreamDownloadBuilder from '../src/packages/StreamDownloadBuilder' +import StorageFileApi from '../src/packages/StorageFileApi' // TODO: need to setup storage-api server for this test const URL = 'http://localhost:8000/storage/v1' @@ -851,5 +852,17 @@ describe('StorageFileApi Edge Cases', () => { expect(body).toBe(testFormData) expect(headers[testHeaderKey]).toBe(testHeaderValue) }) + + test('upload sets Content-Encoding when provided', async () => { + await storage.from('test-bucket').upload('test-path', new Uint8Array([1, 2, 3]), { + contentType: 'application/octet-stream', + contentEncoding: 'gzip', + }) + + expect(mockPost).toHaveBeenCalled() + const [, , body, { headers }] = mockPost.mock.calls[0] + expect(headers['content-type']).toBe('application/octet-stream') + expect(headers['content-encoding']).toBe('gzip') + }) }) })