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

feat(s3): custom xhr transfer handler #11471

Conversation

AllanZhengYP
Copy link
Member

@AllanZhengYP AllanZhengYP commented Jun 8, 2023

Description of changes

An XHR based HTTP transfer handler to be used by custom S3 client. This is to replace the current Axios based handler injected to AWS SDK S3 client. Like Axios handler, this handler also supports abort and download & upload progress events. The thrown error and events to intentionally compatible with Axios ones.

This xhr handler is 1/4 the size of Axios handler

 Axios handler
  Size:       8.66 kB with all dependencies, minified and gzipped
  
  XHR handler
  Size:       2.07 kB with all dependencies, minified and gzipped

Description of how you validated changes

Manually tested on React Native, browsers.

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@AllanZhengYP AllanZhengYP requested review from a team as code owners June 8, 2023 06:43
@codecov-commenter
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (v5/custom-clients@3cec37e). Click here to learn what that means.
The diff coverage is 99.23%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@                 Coverage Diff                  @@
##             v5/custom-clients   #11471   +/-   ##
====================================================
  Coverage                     ?   83.33%           
====================================================
  Files                        ?      276           
  Lines                        ?    20653           
  Branches                     ?     4459           
====================================================
  Hits                         ?    17212           
  Misses                       ?     3153           
  Partials                     ?      288           
Impacted Files Coverage Δ
...rage/src/AwsClients/S3/utils/xhrTransferHandler.ts 98.93% <98.93%> (ø)
...es/storage/__tests__/AwsClients/testUtils/mocks.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@elorzafe
Copy link
Contributor

elorzafe commented Jun 8, 2023

Do you have bundle size metrics for this?


// Handle browser request cancellation (as opposed to a manual cancellation)
xhr.addEventListener('abort', () => {
if (!xhr) return;
Copy link
Member

Choose a reason for hiding this comment

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

If it's possible to fire the event listener while the reference to xhr is nulled out, does that mean there's a memory leak?

Copy link
Member Author

Choose a reason for hiding this comment

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

Axios does the same to clean up the xhr instance. It's not for the memory leak, but more of for unexpected event listener invocations. For example, more data coming in when the abort is already called by server.

Copy link
Member

Choose a reason for hiding this comment

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

Weird. If the event listeners registered against an instance of xhr can still fire even after the reference to it is set to null.. Doesn't that mean that listener is technically now a memory leak? What happens to that listener when I point xhr at a new instance again and add listeners to that? 🤔

Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

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

looks good overall, just a few questions

Comment on lines 143 to 148
// TODO: V6 use xhr.onloadend() when we officially drop support for IE11. Keep it to reduce surprise even though we
// don't support IE11 in v5.
setTimeout(onloadend);
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure we need this

ResponseBodyMixin,
} from '@aws-amplify/core/internals/aws-client-utils';
import { ConsoleLogger as Logger } from '@aws-amplify/core';
import type * as events from 'events';
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

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

@elorzafe It's for typing the emitter, which will listen to the upload/download progress events

Copy link
Contributor

@elorzafe elorzafe Jun 9, 2023

Choose a reason for hiding this comment

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

Why using emitter and not Hub?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a parity with current Axios handler to reduce future refactor to S3 provider

@AllanZhengYP AllanZhengYP force-pushed the v5/custom-clients-s3-merge-xhr branch from a31bdef to b09af04 Compare June 9, 2023 00:41
@AllanZhengYP AllanZhengYP requested a review from cshfang June 9, 2023 00:48
Copy link
Member

@cshfang cshfang left a comment

Choose a reason for hiding this comment

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

Still curious about that one comment about xhr reference being lost to event listeners.. Those are technically orphaned aren't they? 🤔

But I think it's non blocking. Should be an edge edge case at worst

packages/storage/package.json Outdated Show resolved Hide resolved
Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

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

Great work @AllanZhengYP

@AllanZhengYP AllanZhengYP merged commit e1f2ca1 into aws-amplify:v5/custom-clients Jun 9, 2023
kvramyasri7 pushed a commit to kvramyasri7/amplify-js that referenced this pull request Jul 7, 2023
* feat(s3): implement an XHR transfer handler to replace Axios

* fix(s3): address feedbacks
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.

4 participants