Skip to content

Commit 0a6ae71

Browse files
fix(uploads): avoid making redundant memory copies (#1043)
1 parent 0ee81a2 commit 0a6ae71

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Diff for: src/uploads.ts

+5-3
Original file line numberDiff line numberDiff 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 };

Diff for: tests/uploads.test.ts

+8
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)