Fix `uploadProgress` with form data (#725)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent d7320b9 · Browse files · 3 files·+63 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+63 added−3 removed
- tests:+57 added−0 removed
- other:+6 added−3 removed
Provenance
- Commit
3a8f89c- Tree
7eee913- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d7320b9
3 files changed+63 added−3 removed
modified:source/core/Ky.ts+2 added-1 removed
226 unmodified lines227 }228 229 const originalBody = this.request.body;230 if (originalBody) {231+ // Pass original body to calculate size correctly (before it becomes a stream)231- this.request = streamRequest(this.request, this._options.onUploadProgress);232+ this.request = streamRequest(this.request, this._options.onUploadProgress, this._options.body);233 }234 }235 }236 104 unmodified linesmodified:source/utils/body.ts+4 added-2 removed
108 unmodified linesExpand all10911011111211311411311411511611711811912036 unmodified lines108 unmodified linesExpand all }, );};
export const streamRequest = (request: Request, onUploadProgress: Options['onUploadProgress']) => { const totalBytes = getBodySize(request.body);// eslint-disable-next-line @typescript-eslint/ban-typesexport const streamRequest = (request: Request, onUploadProgress: Options['onUploadProgress'], originalBody?: BodyInit | null) => { // Use original body for size calculation since request.body is already a stream const totalBytes = getBodySize(originalBody ?? request.body); let transferredBytes = 0;
return new Request(request, { // @ts-expect-error - Types are outdated.36 unmodified linesadded:test/formdata-searchparams.ts+57 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import test from 'ava';import ky from '../source/index.js';import {createHttpTestServer} from './helpers/create-http-test-server.js';
test('FormData with searchParams and onUploadProgress', async t => { const server = await createHttpTestServer();
server.post('/', (request, response) => { const url = new URL(request.url!, `http://${request.headers.host}`);
let body = ''; request.on('data', chunk => { body += chunk; // eslint-disable-line @typescript-eslint/restrict-plus-operands });
request.on('end', () => { response.json({ params: Object.fromEntries(url.searchParams), bodyLength: body.length, contentType: request.headers['content-type'] ?? '', }); }); });
const formData = new FormData(); formData.append('field', 'value'); formData.append('file', new Blob(['test content'], {type: 'text/plain'}), 'test.txt');
let wasProgressCalled = false; let lastProgress = 0;
const response = await ky.post(server.url, { body: formData, searchParams: { foo: 'bar', test: '123', }, onUploadProgress(progress) { wasProgressCalled = true; lastProgress = progress.percent; }, }).json<{params: Record<string, string>; bodyLength: number; contentType: string}>();
// Check that searchParams were added to URL t.is(response.params.foo, 'bar'); t.is(response.params.test, '123');
// Check that FormData body was sent (should be multipart with content) t.true(response.bodyLength > 0, 'Body should not be empty'); t.true(response.contentType.includes('multipart/form-data'), 'Should have multipart content-type');
// Check that progress callback was called t.true(wasProgressCalled, 'Upload progress callback should have been called'); t.is(lastProgress, 1, 'Final progress should be 100%');
await server.close();});