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

Added support for HTTP/2 #93

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open

Added support for HTTP/2 #93

wants to merge 42 commits into from

Conversation

parthverma1
Copy link

@parthverma1 parthverma1 commented Jun 5, 2024

PR Checklist:

  • I have run npm test locally and all tests are passing.
  • I have added/updated tests for any new behavior.
  • If this is a significant change, an issue has already been created where the problem / solution was discussed: [N/A, or add link to issue here]

PR Description

Introduces support for HTTP/2 by disguising HTTP/2 as HTTP/1 requests and writing appropriate translation/proxy layers.

HTTP/2 is disabled automatically when

  • HTTP Version specific Agents are not specified when passing agents
  • When either tunnel or proxy options are passed

Also adds changes to the redirection logic, where the redirection plugin will now wait for the redirected request to finish before making the next request

run: npm install --legacy-peer-deps

- name: Compile TS
run: tsc lib/**/*.ts
Copy link

Choose a reason for hiding this comment

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

How is this library published? We'll just want to make sure we don't accidentally not perform the compilation step. I think there's a prepare or prepublish script we can invoke in package.json to minimize errors.

@@ -14,7 +14,7 @@
},
"license": "Apache-2.0",
"engines": {
"node": ">= 6"
"node": ">= 16"
Copy link

Choose a reason for hiding this comment

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

How and where is this library consumed and used? Does bumping this up so heavily cause any issues?

Copy link
Author

Choose a reason for hiding this comment

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

It would, but http2 support was only introduced in node 15, and node 16 is the lowest LTS version that supports http2 hence decided/forced to make this change

request.js Outdated
@@ -3,6 +3,8 @@
var tls = require('tls')
var http = require('http')
var https = require('https')
var http2 = require('./lib/http2').default;
Copy link

Choose a reason for hiding this comment

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

Nit maybe, but I think there's a way to publish TypeScript modules still as commonjs. I think it's export = last I checked.

Mostly just the default export is kinda wack in a project which is mostly CommonJS.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah we still need to look into the pipeline of it all, mainly focus on feature completeness and readiness.
Will fix this up before making the PR ready for review

tests/server.js Outdated
@@ -1,142 +1,196 @@
'use strict'
"use strict";
Copy link

Choose a reason for hiding this comment

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

Another nit that I know will be resolved, but make sure any stylistic preferences are fixed in future commits so that the history stays mostly clean and just what was changed.

@parthverma1 parthverma1 marked this pull request as ready for review June 14, 2024 04:27
@codenirvana codenirvana changed the title Feature/http2 Added support for HTTP/2 Jun 14, 2024
index.js Outdated
@@ -34,6 +35,22 @@ function initParams (uri, options, callback) {
}

params.callback = callback || params.callback

// Disable http/2 when using custom agents that don't handle different versions separately
if (params.agents && !(params.agents.http1 || params.agents.auto || params.agents.h2)) {
Copy link
Member

Choose a reason for hiding this comment

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

How agents option looks like now?

// therefore we split on the index of the first ':'
var splitIndex = arr[i].indexOf(':')
// HTTP/2 specific headers beging with :, so we find the index of the first colon skipping the first character
var splitIndex = arr[i].indexOf(':', 1)
Copy link
Member

Choose a reason for hiding this comment

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

foo=bar:baz

Copy link
Author

Choose a reason for hiding this comment

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

image

The header section of the HTTP message splits the key and value on :, thus in order to safeguard against HTTP/2 headers that start with :, the above indexof will skip the first character to find the index.

Example
For the header foo: bar:baz, the index of will find the index as 3 and the above code will contniue working as expected

request.js Outdated Show resolved Hide resolved
// Reference to request, so if _reqResInfo is updated (in case of redirects), we still can update the headers
const request = self._reqResInfo.request
Promise.resolve(self.req._header).then(function (header) {
request.headers = parseRequestHeaders(header)
Copy link
Member

Choose a reason for hiding this comment

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

self.req._header = header

Copy link
Author

Choose a reason for hiding this comment

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

Not needed, since once the _header value is resolved, it means that we have access to the final request object that was sent, thus subsequent gets for _header will return the value instead of the promise. Additionally, since _header is implemented as a getter, it cannot be reassigned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants