Calculates body size from stream or content-length for HTTP client requests/responses.
import type {Options} from '../types/options.js';
import {usualFormBoundarySize} from '../core/constants.js';
const encoder = new TextEncoder();
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export const getBodySize = (body?: BodyInit | null): number => {
if (!body) {
return 0;
}
if (body instanceof FormData) {
// This is an approximation, as FormData size calculation is not straightforward
let size = 0;
for (const [key, value] of body) {
size += usualFormBoundarySize;
size += encoder.encode(`Content-Disposition: form-data; name="${key}"`).byteLength;
size += typeof value === 'string'
? encoder.encode(value).byteLength
: value.size;
}
return size;
}
if (body instanceof Blob) {
return body.size;
}
if (body instanceof ArrayBuf
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key