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

Layer tus-js-client on top of Axios #8281

Merged
merged 2 commits into from
Aug 15, 2024
Merged

Conversation

SpecLad
Copy link
Contributor

@SpecLad SpecLad commented Aug 8, 2024

Motivation and context

This ensures that requests coming from tus-js-client have the same defaults as the ones coming from the rest of the UI. In particular, this ensures that TUS requests include the X-CSRFTOKEN header.

Currently, this doesn't matter much, because TUS requests are authenticated using the token. However, I'd like to get rid of token authentication in the UI, after which X-CSRFTOKEN will become important.

How has this been tested?

Manual testing.

Checklist

  • I submit my changes into the develop branch
  • [ ] I have created a changelog fragment
  • [ ] I have updated the documentation accordingly
  • [ ] I have added tests to cover my changes
  • [ ] I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • New Features

    • Introduced a new integration for resumable uploads using Axios and the tus protocol, enhancing upload reliability and control.
  • Bug Fixes

    • Updated version from 15.1.0 to 15.1.1 for minor fixes and improvements.
  • Refactor

    • Streamlined the chunkUpload function by removing organization-specific parameters and callbacks, refining the upload process.

@SpecLad SpecLad requested a review from bsekachev as a code owner August 8, 2024 16:27
Copy link
Contributor

coderabbitai bot commented Aug 8, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The recent changes to the cvat-core project focus on improving the upload process by integrating Axios with the tus protocol through the introduction of new classes. The version has been updated to signify minor modifications, while the handling of organization-specific parameters in the upload function has been simplified. These adjustments aim to enhance maintainability and streamline the overall functionality without introducing breaking changes.

Changes

File Change Summary
cvat-core/package.json Version updated from 15.1.0 to 15.1.1 for a minor semantic change.
cvat-core/src/axios-tus.ts New classes (AxiosHttpResponse, AxiosHttpRequest, AxiosHttpStack) introduced for tus and Axios integration; added exported constant axiosTusHttpStack.
cvat-core/src/server-proxy.ts Significant modifications to chunkUpload: removed organization parameters and onBeforeRequest callback, focusing on new HTTP stack (axiosTusHttpStack).

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant AxiosHttpStack
    participant AxiosHttpRequest

    Client->>AxiosHttpStack: Create request
    AxiosHttpStack->>AxiosHttpRequest: Generate new HTTP request
    AxiosHttpRequest->>Server: Send upload request
    Server->>AxiosHttpRequest: Process upload
    AxiosHttpRequest->>AxiosHttpStack: Return response
    AxiosHttpStack->>Client: Deliver response
Loading

Poem

🐇 In the code, my heart does leap,
New paths we weave, our promise to keep.
With Axios and tus, we fly so high,
Simplifying uploads, oh me, oh my!
A hop, a skip, improvements here,
Let's celebrate, with joy and cheer! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 79ec6a1 and 5ab057c.

Files selected for processing (3)
  • cvat-core/package.json (1 hunks)
  • cvat-core/src/axios-tus.ts (1 hunks)
  • cvat-core/src/server-proxy.ts (4 hunks)
Files skipped from review due to trivial changes (1)
  • cvat-core/package.json
Additional context used
GitHub Check: Linter
cvat-core/src/axios-tus.ts

[warning] 35-35:
Missing return type on function


[warning] 38-38:
Missing return type on function

Additional comments not posted (9)
cvat-core/src/axios-tus.ts (7)

8-19: Ensure correct implementation of AxiosHttpResponse.

The AxiosHttpResponse class correctly implements the tus.HttpResponse interface. The methods getStatus, getHeader, getBody, and getUnderlyingObject are implemented correctly. Ensure that the getHeader method handles cases where the header might not exist, returning undefined or a default value.


21-30: Ensure correct implementation of AxiosHttpRequest.

The AxiosHttpRequest class correctly implements the tus.HttpRequest interface. The constructor initializes an Axios configuration with an AbortController. Ensure that the setHeader method handles header name casing consistently, and verify that the signal is correctly used for request cancellation.


42-46: Ensure correct implementation of setProgressHandler.

The setProgressHandler method correctly assigns a progress event handler to track upload progress. Ensure that this handler is compatible with the expected progress event structure.


48-51: Ensure correct implementation of send.

The send method correctly uses Axios to send requests. Ensure that error handling is robust, especially for network errors or unexpected responses.


53-53: Ensure correct implementation of abort.

The abort method correctly aborts the request using the AbortController. Ensure that this functionality is tested to confirm that requests are properly canceled.


58-63: Ensure correct implementation of AxiosHttpStack.

The AxiosHttpStack class correctly implements the tus.HttpStack interface. The createRequest method returns an AxiosHttpRequest instance. Ensure that the integration with tus-js-client is seamless.


65-65: Verify export of axiosTusHttpStack.

The axiosTusHttpStack is exported correctly. Verify that it is used appropriately in other parts of the codebase to ensure consistent request defaults.

Verification successful

Export and Usage of axiosTusHttpStack Verified

The axiosTusHttpStack is exported and used correctly within cvat-core/src/server-proxy.ts as part of a configuration object for handling HTTP requests. This ensures consistent request defaults and error handling. No issues found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of `axiosTusHttpStack` in the codebase.

# Test: Search for the usage of `axiosTusHttpStack`. Expect: Occurrences in relevant files.
rg --type ts 'axiosTusHttpStack'

Length of output: 265


Script:

#!/bin/bash
# Description: Search for the usage of `httpStack` in `cvat-core/src/server-proxy.ts` to verify the usage of `axiosTusHttpStack`.

rg --type ts 'httpStack' -A 5 cvat-core/src/server-proxy.ts

Length of output: 392

cvat-core/src/server-proxy.ts (2)

133-133: Ensure correct usage of axiosTusHttpStack.

The axiosTusHttpStack is used in the chunkUpload function for TUS uploads. Verify that it correctly handles the CSRF token inclusion and other Axios defaults.


13-13: Verify import of axiosTusHttpStack.

The axiosTusHttpStack is imported for use in the chunkUpload function. Ensure that it is correctly integrated and that the expected behavior aligns with the PR objectives.

Verification successful

Verified import of axiosTusHttpStack.

The axiosTusHttpStack is correctly imported and integrated into the chunkUpload function as part of the upload configuration, aligning with the PR objectives. No issues were found with its integration.

  • cvat-core/src/server-proxy.ts: axiosTusHttpStack is used in the httpStack property.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify correct integration of `axiosTusHttpStack` in `chunkUpload`.

# Test: Search for the usage of `axiosTusHttpStack` within `chunkUpload`. Expect: Correct integration.
rg --type ts 'axiosTusHttpStack' -A 10

Length of output: 2198

cvat-core/src/axios-tus.ts Outdated Show resolved Hide resolved
cvat-core/src/axios-tus.ts Outdated Show resolved Hide resolved
Currently, if `location` has a query string, the result is nonsense.
cvat-core/src/axios-tus.ts Outdated Show resolved Hide resolved
cvat-core/src/axios-tus.ts Outdated Show resolved Hide resolved
cvat-core/src/axios-tus.ts Outdated Show resolved Hide resolved
cvat-core/src/axios-tus.ts Outdated Show resolved Hide resolved
}

getStatus(): number { return this.#axiosResponse.status; }
getHeader(header: string): string { return this.#axiosResponse.headers[header.toLowerCase()]; }
Copy link
Member

Choose a reason for hiding this comment

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

Probably it also may return undefined as in AxiosHttpRequest

Suggested change
getHeader(header: string): string { return this.#axiosResponse.headers[header.toLowerCase()]; }
getHeader(header: string): string | undefined { return this.#axiosResponse.headers[header.toLowerCase()]; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, fixed. This is actually a bug in the interface definition, so I also filed tus/tus-js-client#706.

This ensures that requests coming from tus-js-client have the same defaults
as the ones coming from the rest of the UI. In particular, this ensures that
TUS requests include the `X-CSRFTOKEN` header.

Currently, this doesn't matter much, because TUS requests are authenticated
using the token. However, I'd like to get rid of token authentication in the
UI, after which `X-CSRFTOKEN` will become important.
Copy link

sonarcloud bot commented Aug 14, 2024

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 57.89474% with 16 lines in your changes missing coverage. Please review.

Project coverage is 83.35%. Comparing base (79ec6a1) to head (69fe13b).
Report is 12 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8281      +/-   ##
===========================================
- Coverage    83.37%   83.35%   -0.02%     
===========================================
  Files          390      391       +1     
  Lines        41549    41533      -16     
  Branches      3861     3839      -22     
===========================================
- Hits         34642    34621      -21     
- Misses        6907     6912       +5     
Components Coverage Δ
cvat-ui 79.62% <56.75%> (-0.06%) ⬇️
cvat-server 86.67% <100.00%> (+<0.01%) ⬆️

@SpecLad SpecLad merged commit e0a60d8 into cvat-ai:develop Aug 15, 2024
33 checks passed
@SpecLad SpecLad deleted the axios-tus branch August 15, 2024 10:42
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.

3 participants