File tree Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -107,8 +107,10 @@ export async function toFile(
107107 // If it's a promise, resolve it.
108108 value = await value ;
109109
110- // Use the file's options if there isn't one provided
111- options ??= isFileLike ( value ) ? { lastModified : value . lastModified , type : value . type } : { } ;
110+ // If we've been given a `File` we don't need to do anything
111+ if ( isFileLike ( value ) ) {
112+ return value ;
113+ }
112114
113115 if ( isResponseLike ( value ) ) {
114116 const blob = await value . blob ( ) ;
@@ -126,7 +128,7 @@ export async function toFile(
126128
127129 name ||= getName ( value ) ?? 'unknown_file' ;
128130
129- if ( ! options . type ) {
131+ if ( ! options ? .type ) {
130132 const type = ( bits [ 0 ] as any ) ?. type ;
131133 if ( typeof type === 'string' ) {
132134 options = { ...options , type } ;
Original file line number Diff line number Diff line change @@ -54,4 +54,12 @@ describe('toFile', () => {
5454 const file = await toFile ( input ) ;
5555 expect ( file . name ) . toEqual ( 'uploads.test.ts' ) ;
5656 } ) ;
57+
58+ it ( 'does not copy File objects' , async ( ) => {
59+ const input = new File ( [ 'foo' ] , 'input.jsonl' , { type : 'jsonl' } ) ;
60+ const file = await toFile ( input ) ;
61+ expect ( file ) . toBe ( input ) ;
62+ expect ( file . name ) . toEqual ( 'input.jsonl' ) ;
63+ expect ( file . type ) . toBe ( 'jsonl' ) ;
64+ } ) ;
5765} ) ;
You can’t perform that action at this time.
0 commit comments