Skip to content
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

fix: body capture handling #1026

Merged
merged 11 commits into from
Feb 19, 2024
Merged

fix: body capture handling #1026

merged 11 commits into from
Feb 19, 2024

Conversation

pauldambra
Copy link
Member

@pauldambra pauldambra commented Feb 15, 2024

  • reads request body more safely for fetch
  • improves XHR request and response body reading
  • removes the need for content-length header for reading payloads

@pauldambra pauldambra added the bump patch Bump patch version when this PR gets merged label Feb 15, 2024
@pauldambra pauldambra requested a review from a team February 15, 2024 13:18
Copy link

github-actions bot commented Feb 15, 2024

Size Change: +2.17 kB (0%)

Total Size: 862 kB

Filename Size Change
dist/array.full.js 183 kB +254 B (0%)
dist/array.js 124 kB +252 B (0%)
dist/es.js 125 kB +252 B (0%)
dist/module.js 125 kB +252 B (0%)
dist/recorder-v2.js 106 kB +1.17 kB (+1%)
ℹ️ View Unchanged
Filename Size
dist/exception-autocapture.js 12 kB
dist/recorder.js 58.6 kB
dist/surveys.esm.js 64.2 kB
dist/surveys.js 64.3 kB

compressed-size-action

Copy link
Member Author

Choose a reason for hiding this comment

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

we have multiple almost identical linters now, but i'll deal with that in a separate PR

Comment on lines +96 to +97
let requestContentLength: string | number = headers?.['content-length'] || estimateBytes(payload)
if (_isString(requestContentLength)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

turns out we rarely/never have the content-length header for requests

so we have to have a way to estimate body size when there is no header

changes elsewhere in this PR meant we know this is always a string now so that's much simpler than I was worrying

Copy link
Member

@marandaneto marandaneto Feb 19, 2024

Choose a reason for hiding this comment

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

That is odd and might be a problem, see this issue.
Some providers require the content-length otherwise the provider will return 411 but we cannot control/force the user's requests headers, maybe something to add on the docs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, not that the request won't have it but fetch doesn't expose computed request headers to code here. So, we can't read content-length regardless of whether the browser adds it when sending.

I tried not reading the headers until after the request had been made and couldn't read request headers that were visible in the network panel of the browser

Copy link
Member

Choose a reason for hiding this comment

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

I see, gotcha.

* According to MDN https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response
* @param response an ArrayBuffer, a Blob, a Document, a JavaScript object, or a string, depending on the value of XMLHttpRequest.responseType, that contains the response entity body.
*/
function _tryReadXHRResponseBody(response: any): string | null {
Copy link
Member Author

Choose a reason for hiding this comment

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

in each of these _tryReadX methods we can ignore body types we don't want to process e.g. ArrayBuffer

@pauldambra pauldambra merged commit 5946bcc into main Feb 19, 2024
14 checks passed
@pauldambra pauldambra deleted the fix/body-capture-handling branch February 19, 2024 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants