Visitar URL original
fix(fetch): encode string chunks when tracking upload streams by mayur-dhavan · Pull Request #11380 · axios/axios · GitHub
Skip to content

fix(fetch): encode string chunks when tracking upload streams - #11380

Open
mayur-dhavan wants to merge 8 commits into
axios:v1.xfrom
mayur-dhavan:fix/fetch-string-chunk-upload-tracking
Open

mayur-dhavan wants to merge 8 commits into
axios:v1.xfrom
mayur-dhavan:fix/fetch-string-chunk-upload-tracking

Conversation

@mayur-dhavan

Copy link
Copy Markdown

Summary

With the fetch adapter, a ReadableStream request body that emits strings is sent correctly on its own. Once onUploadProgress or maxBodyLength makes axios wrap the stream in trackStream, the strings are read as chunks with no byteLength, so nothing is yielded and the request goes out with an empty body. With maxBodyLength the limit is never enforced either.

Linked issue

Closes #11379

Changes

  • lib/helpers/trackStream.js: encode string chunks as UTF-8 in readBytes before they are sized and counted. Byte chunks are untouched.
  • tests/unit/helpers/trackStream.test.js: new test for string chunks (UTF-8 content and byte counts reported to the progress callback) and for byte chunks passing through unchanged.
  • PRE_RELEASE_CHANGELOG.md: Bug Fixes entry.

Validation

  • Ran the local echo-server repro from the issue (Node v22.23.3). Before: onUploadProgress and maxBodyLength cases returned "". After: onUploadProgress returns "héllo🙂" and maxBodyLength: 5 rejects with ERR_BAD_REQUEST (Request body larger than maxBodyLength limit).
  • npx vitest run --project unit tests/unit/helpers/trackStream.test.js: 2 passed. With the lib/ change stashed, the string-chunk test fails (1 failed, 1 passed).
  • npx vitest run --project unit tests/unit/adapters/fetch.test.js tests/unit/adapters/bodyLength.test.js: 117 passed, 1 failed ("should not use inherited Symbol.iterator for request headers"). The same test fails on an unmodified v1.x checkout (aa54a5e), so it is not caused by this change.
  • npx eslint on the two changed JS files: no findings. npx prettier --check: clean after formatting.
  • Not run locally: npm run build, the browser suite, and the packed-package module/smoke suites. The change does not touch exports, types or browser code. CI results to follow below.

Compatibility and documentation

Patch. No API or type changes, so index.d.ts/index.d.cts and PRE_RELEASE_DOCS.md need no update. Strings in a streamed body now count by UTF-8 byte length for progress and maxBodyLength, matching what is sent.

Prepared with AI assistance (the ai-assisted label is requested by AGENTS.md; I may not be able to apply it as an outside contributor, so a maintainer may need to).

Fetch upload tracking read byteLength from string chunks, got undefined, and silently sent an empty body. Encode strings as UTF-8 first.
Adds the pre-release Bug Fixes entry for the string chunk fix.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 18:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@greptile-apps

greptile-apps Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] No outstanding findings block merging.

Summary

Encodes string chunks as UTF-8 before trackStream counts and forwards them, preserving streamed request content and enforcing byte-based upload limits. Adds coverage for string content, progress counts, maxBodyLength, and unchanged byte chunks.

The latest change adds the missing PR link to PRE_RELEASE_CHANGELOG.md, addressing the previous release-note finding. No new issues were found.

  • Tracked fetch uploads send string chunks as UTF-8 bytes.

Reviews (5) · Last reviewed commit: "docs(changelog): link pull request in pr..." · Reviewed by Greptile

@WillianAgostini

Copy link
Copy Markdown
Contributor

Hi @mayur-dhavan,

It would be great to run a test similar to the one reported in #11379 across all available adapters to ensure test coverage and consistency across the codebase. What do you think?

Added tests for handling ReadableStream bodies with upload progress tracking and maxBodyLength enforcement.
Comment thread PRE_RELEASE_CHANGELOG.md Resolved
@charan-rathore

Copy link
Copy Markdown

Some concrete adapter-matrix results for the coverage question above. Tested 5bdeb73 on Node 22.23.4 against a local HTTP echo server, sending the two chunks hé and llo🙂:

  • Fetch + WHATWG ReadableStream: plain and upload-progress requests both echo héllo🙂; maxBodyLength: 5 rejects with ERR_BAD_REQUEST.
  • HTTP + that same WHATWG stream: all three variants reject before sending with the existing unsupported-body-type error. That isn't a failure of this patch; this adapter needs a Node stream for the comparable case.
  • HTTP + Readable.from(['hé', 'llo🙂']): upload progress preserves the body, and the 5-byte limit rejects as expected.

Reverting only the trackStream implementation reproduces the fetch regression: plain still works, but progress and limit variants echo an empty body. The two helper tests pass on the PR head.

For a cross-adapter regression, I'd keep the HTTP Node-stream control separate from the fetch WHATWG-stream case rather than expect the same body object to work everywhere. XHR/browser coverage is still unverified here. AI-assisted testing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fetch upload tracking drops string chunks from a ReadableStream

4 participants