From aaaaea116726bdcb99cdfc886735d27ce4c6efab Mon Sep 17 00:00:00 2001 From: stainless-bot Date: Thu, 5 Sep 2024 20:43:27 +0000 Subject: [PATCH] fix(uploads): avoid making redundant memory copies --- src/uploads.ts | 8 +++++--- tests/uploads.test.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/uploads.ts b/src/uploads.ts index a920351..8fd2154 100755 --- a/src/uploads.ts +++ b/src/uploads.ts @@ -107,8 +107,10 @@ export async function toFile( // If it's a promise, resolve it. value = await value; - // Use the file's options if there isn't one provided - options ??= isFileLike(value) ? { lastModified: value.lastModified, type: value.type } : {}; + // If we've been given a `File` we don't need to do anything + if (isFileLike(value)) { + return value; + } if (isResponseLike(value)) { const blob = await value.blob(); @@ -126,7 +128,7 @@ export async function toFile( name ||= getName(value) ?? 'unknown_file'; - if (!options.type) { + if (!options?.type) { const type = (bits[0] as any)?.type; if (typeof type === 'string') { options = { ...options, type }; diff --git a/tests/uploads.test.ts b/tests/uploads.test.ts index df54adf..323e877 100755 --- a/tests/uploads.test.ts +++ b/tests/uploads.test.ts @@ -54,4 +54,12 @@ describe('toFile', () => { const file = await toFile(input); expect(file.name).toEqual('uploads.test.ts'); }); + + it('does not copy File objects', async () => { + const input = new File(['foo'], 'input.jsonl', { type: 'jsonl' }); + const file = await toFile(input); + expect(file).toBe(input); + expect(file.name).toEqual('input.jsonl'); + expect(file.type).toBe('jsonl'); + }); });