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: support HTTP2 #518

Merged
merged 3 commits into from
Jun 27, 2024
Merged

feat: support HTTP2 #518

merged 3 commits into from
Jun 27, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jun 26, 2024

closes #474

pick from #516

Summary by CodeRabbit

  • New Features

    • Introduced HTTP/2 support in HttpClient with the new allowH2 option.
    • Added getGlobalDispatcher function for managing global dispatchers.
  • Documentation

    • Updated README with a new section on making requests using HTTP/2 in HttpClient.
  • Tests

    • Added test cases for the allowH2 option in HttpClient.
  • Chores

    • Updated Node.js versions in GitHub Actions configuration to include additional versions and correct an existing version.

closes #474

pick from #516
@fengmk2 fengmk2 added the enhancement New feature or request label Jun 26, 2024
Copy link

coderabbitai bot commented Jun 26, 2024

Walkthrough

The recent changes introduce HTTP/2 support in the HttpClient through an allowH2 option. This update allows users to enable HTTP/2 when creating HTTP clients, enhancing Server-Sent Events (SSE) support. Modifications span across HttpClient, HttpAgent, and corresponding test files, along with README documentation to illustrate the new feature. Additionally, the Node.js workflow in the GitHub Actions configuration has been updated to support more versions.

Changes

File Change Summary
README.md Added section demonstrating HTTP/2 usage with HttpClient.
src/HttpAgent.ts Introduced allowH2 property in HttpAgentOptions and updated constructor.
src/HttpClient.ts Included allowH2 in ClientOptions and adjusted class instantiation logic.
test/HttpClient.test.ts Added tests for allowH2 and exported getGlobalDispatcher.
.github/workflows/nodejs.yml Corrected and expanded Node.js version list.

Sequence Diagram(s)

sequenceDiagram
    participant User as User
    participant Application as Application
    participant HttpClient as HttpClient
    participant HttpAgent as HttpAgent
    participant Server as Server

    Note over User, Application: Initial Client Setup
    User->>Application: Create HttpClient with allowH2
    Application->>HttpClient: New HttpClient({ allowH2: true })
    HttpClient->>HttpAgent: Instantiate HttpAgent with allowH2

    Note over Application, Server: Making a Request
    Application->>HttpClient: Make HTTP request
    HttpClient->>HttpAgent: Get Connection
    HttpAgent->>Server: Connect (HTTP/2 if supported)
    Server->>HttpAgent: Respond
    HttpAgent->>HttpClient: Relay Response
    HttpClient->>Application: Return Response
    Application->>User: Display Results
Loading

Assessment against linked issues

Objective Addressed Explanation
Support HTTP2 to make SSE easy [#474]

Poem

In the world of bytes and code so spruce,
Comes a client that sips HTTP2 juice.
With allowH2, the streams now breeze,
Over the wires where data leads.
From client to server, a path anew,
A rabbit's dream in a world brand new.
🌐🐰🚀


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 Configration 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

codecov bot commented Jun 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.86%. Comparing base (8247aa2) to head (9b0c129).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #518      +/-   ##
==========================================
+ Coverage   97.85%   97.86%   +0.01%     
==========================================
  Files          10       10              
  Lines        1586     1596      +10     
  Branches      255      256       +1     
==========================================
+ Hits         1552     1562      +10     
  Misses         30       30              
  Partials        4        4              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@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: 0

Outside diff range and nitpick comments (2)
src/HttpClient.ts (2)

Line range hint 364-364: Avoid using the delete operator for performance reasons.

Using delete can lead to performance degradations due to changes in object shape. Consider setting the property to undefined instead.

- delete headers['user-agent'];
+ headers['user-agent'] = undefined;

Line range hint 572-572: Correct the unsafe usage of optional chaining.

Ensure that the evaluation does not throw a TypeError when the optional chain short-circuits with 'undefined'.

- if (context.history?) {
+ if (context?.history && context.history.length > 0) {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8247aa2 and 8020b74.

Files selected for processing (4)
  • README.md (1 hunks)
  • src/HttpAgent.ts (2 hunks)
  • src/HttpClient.ts (2 hunks)
  • test/HttpClient.test.ts (2 hunks)
Additional context used
LanguageTool
README.md

[misspelling] ~55-~55: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
Context: ... The URL to request, either a String or a Object that return by [url.parse](https...


[style] ~57-~57: To form a complete sentence, be sure to include a subject. (MISSING_IT_THERE)
Context: ...ng - Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Al...


[grammar] ~60-~60: The past participle is required after “to be”. (BE_VBP_IN)
Context: ...m_class_stream_readable) - Stream to be pipe to the remote. If set, data and `cont...


[grammar] ~61-~61: There may an error in the verb form ‘be write’. (MD_BE_NON_VBP)
Context: ...e response stream. Responding data will be write to this stream and callback will be c...


[style] ~63-~63: To form a complete sentence, be sure to include a subject or ‘there’. (MISSING_IT_THERE)
Context: ...tType*** String - Type of request data. Could be json (Notes: not use `applicat...


[uncategorized] ~63-~63: It appears that a hyphen is missing (if ‘auto’ is not used in the context of ‘cars’). (AUTO_HYPHEN)
Context: ...ation/jsonhere). If it'sjson, will auto set Content-Type: application/json` header...


[style] ~64-~64: To form a complete sentence, be sure to include a subject. (MISSING_IT_THERE)
Context: ...Type*** String - Type of response data. Could be text or json. If it's text, th...


[uncategorized] ~64-~64: It appears that a hyphen is missing (if ‘auto’ is not used in the context of ‘cars’). (AUTO_HYPHEN)
Context: ... would be a parsed JSON Object and will auto set Accept: application/json header. Defa...


[uncategorized] ~67-~67: Possible missing preposition found. (AI_HYDRA_LEO_MISSING_TO)
Context: ... can use timeout: 5000 to tell urllib use same timeout on two phase or set them s...


[grammar] ~67-~67: Possible agreement error. The noun ‘phase’ seems to be countable. (CD_NN)
Context: ... to tell urllib use same timeout on two phase or set them seperately such as `timeout...


[misspelling] ~73-~73: Did you mean “yourself”? Remove the space in between. (YOUR_SELF_TO_YOURSELF)
Context: ...* Function - Format the redirect url by your self. Default is url.resolve(from, to). ...


[misspelling] ~74-~74: Use ‘every thing’ if you want to emphasize that the things are separate. Usually, you can use “everything”. (EVERYTHING)
Context: ...n - Before request hook, you can change every thing here. - streaming Boolean - let...


[uncategorized] ~76-~76: It appears that a hyphen is missing (if ‘auto’ is not used in the context of ‘cars’). (AUTO_HYPHEN)
Context: ... Accept gzip, br response content and auto decode it, default is true. - timing...


[misspelling] ~123-~123: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
Context: ...: 'world', }), }); It would make a HTTP request like: bash POST / HTTP...


[uncategorized] ~203-~203: Loose punctuation mark. (UNLIKELY_OPENING_PUNCTUATION)
Context: ...uest and response time in ms. - timing: timing object if timing enable. - `sock...


[uncategorized] ~204-~204: Loose punctuation mark. (UNLIKELY_OPENING_PUNCTUATION)
Context: ...ming object if timing enable. - socket: socket info ## Run test with debug log...


[misspelling] ~256-~256: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
Context: ...ad request' }); ``` ## Request through a http proxy export from [undici](https:...

Biome
src/HttpClient.ts

[error] 364-364: Avoid the delete operator which can impact performance. (lint/performance/noDelete)

Unsafe fix: Use an undefined assignment instead.


[error] 556-556: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.


[error] 572-572: Unsafe usage of optional chaining. (lint/correctness/noUnsafeOptionalChaining)

If it short-circuits with 'undefined' the evaluation will throw TypeError here:

Additional comments not posted (9)
src/HttpAgent.ts (2)

15-15: Addition of allowH2 property to HttpAgentOptions

The addition of the allowH2 property to the HttpAgentOptions type is crucial for enabling HTTP2 support. This change allows users to configure their HTTP agents to support HTTP2, aligning with the PR's objective to introduce HTTP2 support.


66-66: Inclusion of allowH2 in the connect object

The constructor now includes the allowH2 option in the connect configuration object. This change is essential for ensuring that the HTTP2 setting is respected when the HttpAgent is used. It is a necessary update to support the new functionality introduced by the PR.
[APROVED]

test/HttpClient.test.ts (3)

3-3: Import of sensitiveHeaders from node:http2

This import is crucial for testing HTTP2 functionality, specifically to check for sensitive headers that are specific to HTTP2 communications. It is a necessary addition for the new tests introduced.


5-5: Updated imports in HttpClient.test.ts

The inclusion of getGlobalDispatcher from ../src/index.js is essential for testing interactions with global dispatchers in the context of HTTP2. This change supports the new testing requirements introduced by the PR.


31-97: New test suite for allowH2 option

The new test suite specifically targets the allowH2 functionality. It includes tests to verify that HTTP2 is properly handled when allowH2 is set to true and that the standard HTTP/1.1 behavior remains unaffected when allowH2 is false. These tests are well-structured and crucial for ensuring the robustness of the new functionality.

README.md (1)

212-226: Documentation for making HTTP2 requests

The new section in the README file provides clear instructions on how to make HTTP2 requests by setting the allowH2 option. This documentation is well-written and crucial for helping users understand how to utilize the new HTTP2 support in the library.

src/HttpClient.ts (3)

60-61: Approved addition of allowH2 property.

The optional allowH2 property in ClientOptions is a clean implementation to support HTTP2, defaulting to false which is a safe default.


182-182: Proper handling of allowH2 in dispatcher configuration.

The conditional checks and subsequent setting of allowH2 in the dispatcher configurations are correctly implemented, ensuring HTTP2 is enabled based on user configuration.

Also applies to: 187-187, 192-192


Line range hint 556-556: Use optional chaining to simplify code.

Changing to optional chaining can make the code cleaner and less prone to errors.

- if (context?.history) {
+ if (context.history?) {

Copy link

@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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8020b74 and 87c214c.

Files selected for processing (1)
  • test/HttpClient.test.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • test/HttpClient.test.ts

@fengmk2 fengmk2 linked an issue Jun 27, 2024 that may be closed by this pull request
Copy link

@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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 87c214c and 9b0c129.

Files selected for processing (1)
  • .github/workflows/nodejs.yml (1 hunks)
Files skipped from review due to trivial changes (1)
  • .github/workflows/nodejs.yml

@fengmk2 fengmk2 merged commit 21d4260 into master Jun 27, 2024
17 checks passed
@fengmk2 fengmk2 deleted the allow-h2-for-master branch June 27, 2024 01:41
fengmk2 pushed a commit that referenced this pull request Jun 27, 2024
[skip ci]

## [4.1.0](v4.0.0...v4.1.0) (2024-06-27)

### Features

* support HTTP2 ([#518](#518)) ([21d4260](21d4260))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Track HTTP2 support feature merge into master support HTTP2 to make SSE easy
1 participant