diff --git a/.changeset/weak-panthers-fold.md b/.changeset/weak-panthers-fold.md new file mode 100644 index 00000000..70d24253 --- /dev/null +++ b/.changeset/weak-panthers-fold.md @@ -0,0 +1,5 @@ +--- +"@tus/azure-store": patch +--- + +Fix error on saving metadata when it contains non-ASCII characters diff --git a/packages/azure-store/src/index.ts b/packages/azure-store/src/index.ts index cd41c923..c80ecd5f 100644 --- a/packages/azure-store/src/index.ts +++ b/packages/azure-store/src/index.ts @@ -7,6 +7,7 @@ import { type KvStore, MemoryKvStore, TUS_RESUMABLE, + Metadata, } from '@tus/utils' import { type AppendBlobClient, @@ -78,7 +79,11 @@ export class AzureStore extends DataStore { await appendBlobClient.setMetadata( { tus_version: TUS_RESUMABLE, - upload: JSON.stringify(upload), + upload: JSON.stringify({ + ...upload, + // Base64 encode the metadata to avoid errors for non-ASCII characters + metadata: Metadata.stringify(upload.metadata ?? {}), + }), }, {} ) @@ -110,6 +115,9 @@ export class AzureStore extends DataStore { throw ERRORS.FILE_NOT_FOUND } const upload = JSON.parse(propertyData.metadata.upload) as Upload + // Metadata is base64 encoded to avoid errors for non-ASCII characters + // so we need to decode it separately + upload.metadata = Metadata.parse(JSON.stringify(upload.metadata ?? {})) await this.cache.set(appendBlobClient.url, upload) diff --git a/test/src/stores.test.ts b/test/src/stores.test.ts index 5463ca4e..264e55a8 100644 --- a/test/src/stores.test.ts +++ b/test/src/stores.test.ts @@ -71,6 +71,18 @@ export const shouldCreateUploads = () => { const upload = await this.datastore.getUpload(file.id) assert.deepStrictEqual(upload.metadata, file.metadata) }) + + it('should store `upload_metadata` with non-ASCII characters', async function () { + const file = new Upload({ + id: testId('create-test-non-ascii'), + size: 1000, + offset: 0, + metadata: {filename: '世界_domination_plan.pdf', is_confidential: null}, + }) + await this.datastore.create(file) + const upload = await this.datastore.getUpload(file.id) + assert.deepStrictEqual(upload.metadata, file.metadata) + }) }) }