Skip to content
Myr

Improve body size estimation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Reuse a single TextEncoder instance instead of creating new ones per call - Use `ArrayBuffer.isView()` to properly handle TypedArrays - Use `.byteLength` consistently on encode results - Remove unreachable code paths for non-BodyInit types Closes #734

authored by Sindre Sorhus · committed by Sindre Sorhus · parent bb8412e · Browse files · 3 files+135 added−21 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+135 added−21 removed

  • tests:+125 added−0 removed
  • other:+10 added−21 removed

3 files+135 added−21 removed

modified:source/core/constants.ts+2 added-2 removed
55 unmodified lines
5657// The maximum value of a 32bit int (see issue #117)58export const maxSafeTimeout = 2_147_483_647;5960// Size in bytes of a typical form boundary, used to help estimate upload size61export const usualFormBoundarySize = new TextEncoder().encode('------WebKitFormBoundaryaxpyiPgbbPti10Rw').length;60// Size in bytes of a typical form boundary (e.g. '------WebKitFormBoundaryaxpyiPgbbPti10Rw'), used to help estimate upload size61export const usualFormBoundarySize = 40;6263/**64Symbol that can be returned by a `beforeRetry` hook to stop retrying without throwing an error.65*/
223 unmodified lines
modified:source/utils/body.ts+8 added-19 removed
added:test/body-size.ts+125 added
1import test from 'ava';2import {getBodySize} from '../source/utils/body.js';34test('returns 0 for undefined', t => {5 t.is(getBodySize(undefined), 0);6});78test('returns 0 for null', t => {9 t.is(getBodySize(null), 0);10});1112test('returns correct size for ASCII string', t => {13 t.is(getBodySize('hello'), 5);14});1516test('returns correct size for multi-byte string', t => {17 // Emoji is 4 bytes in UTF-818 t.is(getBodySize('😀'), 4);19 t.is(getBodySize('hello 😀 world'), 16);20});2122test('returns correct size for empty string', t => {23 t.is(getBodySize(''), 0);24});2526test('returns correct size for ArrayBuffer', t => {27 const buffer = new ArrayBuffer(16);28 t.is(getBodySize(buffer), 16);29});3031test('returns correct size for Uint8Array', t => {32 const array = new Uint8Array([1, 2, 3, 4, 5]);33 t.is(getBodySize(array), 5);34});3536test('returns correct size for Uint16Array', t => {37 const array = new Uint16Array([1, 2, 3]);38 t.is(getBodySize(array), 6); // 3 elements × 2 bytes39});4041test('returns correct size for Float64Array', t => {42 const array = new Float64Array([1, 2]);43 t.is(getBodySize(array), 16); // 2 elements × 8 bytes44});4546test('returns correct size for DataView', t => {47 const buffer = new ArrayBuffer(10);48 const view = new DataView(buffer);49 t.is(getBodySize(view), 10);50});5152test('returns correct size for TypedArray subarray', t => {53 const full = new Uint8Array([1, 2, 3, 4, 5]);54 const sub = full.subarray(1, 3);55 // Should return the view's byteLength (2), not the underlying buffer's (5)56 t.is(getBodySize(sub), 2);57});5859test('returns 0 for zero-length ArrayBuffer', t => {60 t.is(getBodySize(new ArrayBuffer(0)), 0);61});6263test('returns correct size for Blob', t => {64 const blob = new Blob(['hello world']);65 t.is(getBodySize(blob), 11);66});6768test('returns correct size for Blob with multi-byte content', t => {69 const blob = new Blob(['😀']);70 t.is(getBodySize(blob), 4);71});7273test('returns correct size for File', t => {74 const file = new File(['hello world'], 'test.txt', {type: 'text/plain'});75 t.is(getBodySize(file), 11);76});7778test('returns correct size for URLSearchParams', t => {79 const parameters = new URLSearchParams({foo: 'bar', baz: 'qux'});80 t.is(getBodySize(parameters), 15); // 'foo=bar&baz=qux'81});8283test('returns correct size for empty URLSearchParams', t => {84 const parameters = new URLSearchParams();85 t.is(getBodySize(parameters), 0);86});8788test('returns 0 for ReadableStream', t => {89 const stream = new ReadableStream();90 t.is(getBodySize(stream), 0);91});9293test('returns 0 for empty FormData', t => {94 const formData = new FormData();95 t.is(getBodySize(formData), 0);96});9798test('FormData returns a positive size', t => {99 const formData = new FormData();100 formData.append('key', 'value');101 const size = getBodySize(formData);102 t.true(size > 0, `Expected positive size, got ${size}`);103});104105test('FormData size increases with more fields', t => {106 const formData1 = new FormData();107 formData1.append('a', '1');108 const size1 = getBodySize(formData1);109110 const formData2 = new FormData();111 formData2.append('a', '1');112 formData2.append('b', '2');113 const size2 = getBodySize(formData2);114115 t.true(size2 > size1, `Two fields (${size2}) should be larger than one field (${size1})`);116});117118test('FormData with Blob value accounts for blob size', t => {119 const formData = new FormData();120 formData.append('file', new Blob(['hello world']));121122 const size = getBodySize(formData);123 // Size should be at least the blob content size (11 bytes)124 t.true(size >= 11, `Expected size >= 11, got ${size}`);125});