perf: skip request-body compression for already-compressed content types - #1053
Merged
Merged
Conversation
Contributor
|
See more at https://github.com/apify/apify-client-js/actions/runs/34461187304#summary-102819158595 |
The one conflict was the `contentType` docstring in `setRecord`: v3 reworded the `application/octet-stream` bullet while this branch appended a note about skipped compression. Both are kept. v3 also widened the `setRecord` value type to accept binary data, so the two new compression tests drop their `as any` casts.
Axios stores a header under whatever casing the caller used, and bracket access is case-sensitive, so `Content-Encoding: gzip` missed the guard that skips compression for an already-encoded body. The body was then compressed a second time, and axios collapsed the two header keys last-write-wins, leaving `br` on a brotli-wrapped gzip stream. The same lookup in `serializeRequest` dropped function-valued Actor input fields when the content type carried an unconventional casing.
…ompressed-types # Conflicts: # test/http_client.test.ts
janbuchar
approved these changes
Sep 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
maybeCompressRequest()now skips compression when the requestContent-Typeis a media type that already carries its own compression:image/*,audio/*,video/*, archives, ZIP-based office formats and packages, and web fonts. Raw formats sitting under those prefixes (image/bmp,audio/wav) still compress, as do+json/+xmlstructured suffixes such asimage/svg+xml.application/octet-streamis deliberately off the list: it is the catch-all for unknown binary andsetRecord()'s fallback when nocontentTypeis passed.Compressing an incompressible body burns CPU and holds a second copy of it in memory for nothing. Brotli on a 50 MiB PNG, which is what
setRecord()did before this change, costs 0.44 s of CPU and 111 MiB of resident memory, and the output comes out 120 bytes larger than the input.Header lookups also go through a case-insensitive
getHeader()now. Axios stores a request header under whatever casing the caller used, so bracket access oncontent-encodingmissed a caller writing the conventionalContent-Encoding: gzip: the body got compressed a second time andbrreplaced the caller's header over a brotli-wrapped gzip stream. The same bug inserializeRequest()dropped function-valued Actor input fields onactor.call()whenContent-Type: application/jsonwas oddly cased.Issue
Closes #1033.
Ports apify/apify-client-python#987, with one deliberate difference: the Python client strips a caller-supplied
Content-Encodingwhen it skips compression, while this client reads it as "the body is already encoded and the header describes it" and keeps both, so that escape hatch survives.✍️ Drafted by Claude Code