-
Notifications
You must be signed in to change notification settings - Fork 51
chore: [JS] Document Request Compression Middleware #2369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
davidkna-sap
wants to merge
5
commits into
main
Choose a base branch
from
davidkna-sap_doc-http-compress
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a779e91
chore: [JS] Document Request Compression Middleware
davidkna-sap f355629
use updated api
davidkna-sap 841014b
fix: Changes from lint
4cd4940
Update middleware function name from compressRequest to compress and …
davidkna-sap b5b18af
fix: Changes from lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,6 +221,82 @@ const response = await executeHttpRequest( | |
| ); | ||
| ``` | ||
|
|
||
| ### Request Compression | ||
|
|
||
| :::caution | ||
|
|
||
| Not all servers support decompressing request payloads with every supported algorithm - or any algorithm at all. | ||
| Verify your target server supports decompression of compressed requests before enabling this middleware. | ||
|
|
||
| ::: | ||
|
|
||
| :::info | ||
|
|
||
| Place compression middleware at the beginning of your middleware array. | ||
| This ensures the payload is compressed once and reused across retry attempts. | ||
|
|
||
| ::: | ||
|
|
||
| The SAP Cloud SDK provides a [`compress()`](pathname:///api/v4/functions/sap-cloud-sdk_http-client.compress.html) middleware to compress HTTP request payloads using a selected compression algorithm. | ||
| This can reduce bandwidth usage and improve performance when sending large payloads to remote APIs. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nth] Consider also mentioning the tradeoff between compression overhead and bandwith reduction. Compression might be worse for small payloads. |
||
|
|
||
| Compression requires additional computational resources to encode the payload, and the compressed payload could be slightly larger than the original if the payload is not suited for compression (e.g., too small or already compressed). | ||
| For small payloads or in a high-bandwidth environment, the overhead may outweigh potential bandwidth savings. | ||
| The default "auto" mode addresses this by only compressing payloads above a size threshold. | ||
|
|
||
| For details on supported algorithms and options, see the [API documentation](pathname:///api/v4/interfaces/sap-cloud-sdk_http-client.RequestCompressionMiddlewareOptions.html). | ||
|
|
||
| To enable automatic compression with `gzip` based on payload size: | ||
|
|
||
| ```ts | ||
| import { compress } from '@sap-cloud-sdk/http-client'; | ||
|
|
||
| const response = await executeHttpRequest( | ||
| { | ||
| url: 'https://example.com' | ||
| }, | ||
| { | ||
| method: 'post', | ||
| data: largePayload, | ||
| middleware: [compress()] | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| The [`compress()`](pathname:///api/v4/functions/sap-cloud-sdk_http-client.compress.html) middleware supports [four modes of operation](pathname:///api/v4/interfaces/sap-cloud-sdk_http-client.RequestCompressionMiddlewareOptions.html#mode): | ||
|
|
||
| **Auto mode (recommended, default):** Compresses payloads only if they exceed a size threshold. | ||
|
|
||
| ```ts | ||
| // Use default threshold of 1024 bytes | ||
| middleware: [compress()]; | ||
| ``` | ||
|
|
||
| ```ts | ||
| // Use custom threshold | ||
| middleware: [compress({ mode: 'auto', autoCompressMinSize: 5000 })]; | ||
| ``` | ||
|
|
||
| **Always compress:** Forces compression regardless of payload size. | ||
|
|
||
| ```ts | ||
| middleware: [compress({ mode: 'always', compressOptions: { level: 1 } })]; | ||
| ``` | ||
|
|
||
| **Header-only mode:** Sets the `Content-Encoding` header without compressing. | ||
| Use this mode when the payload is already compressed with the selected algorithm. | ||
|
|
||
| ```ts | ||
| middleware: [compress({ algorithm: 'zstd', mode: 'header-only' })]; | ||
| ``` | ||
|
|
||
| **No compression:** Disables compression even if the middleware is included. | ||
| This mode is useful for conditional logic. | ||
|
|
||
| ```ts | ||
| middleware: [compress({ mode: 'never' })]; | ||
| ``` | ||
|
|
||
| ## `executeHttpRequestWithOrigin()` | ||
|
|
||
| The `executeHttpRequestWithOrigin()` function is a variation of `executeHttpRequest()` which allows more fine-grained control over configuration precedence. | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.