-
-
Notifications
You must be signed in to change notification settings - Fork 669
chore: make client-h2.js more resilient #4564
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
Draft
Uzlopak
wants to merge
1
commit into
main
Choose a base branch
from
h2-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,6 +32,10 @@ const { | |||||
} = require('../core/symbols.js') | ||||||
const { channels } = require('../core/diagnostics.js') | ||||||
|
||||||
/** @typedef {import('../../types/client.js').default} Client */ | ||||||
/** @typedef {import('http2').ClientHttp2Session} ClientHttp2Session */ | ||||||
/** @typedef {import('node:net').Socket} Socket */ | ||||||
|
||||||
const kOpenStreams = Symbol('open streams') | ||||||
|
||||||
let extractBody | ||||||
|
@@ -77,6 +81,10 @@ function parseH2Headers (headers) { | |||||
return result | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param {Client} client | ||||||
* @param {Socket} socket | ||||||
*/ | ||||||
function connectH2 (client, socket) { | ||||||
client[kSocket] = socket | ||||||
|
||||||
|
@@ -151,13 +159,23 @@ function resumeH2 (client) { | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* @this {ClientHttp2Session} | ||||||
* @param {Error} err | ||||||
*/ | ||||||
function onHttp2SessionError (err) { | ||||||
assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID') | ||||||
|
||||||
this[kSocket][kError] = err | ||||||
this[kClient][kOnError](err) | ||||||
} | ||||||
|
||||||
/** | ||||||
* @this {ClientHttp2Session} | ||||||
* @param {number} type | ||||||
* @param {number} code | ||||||
* @param {number} id | ||||||
*/ | ||||||
function onHttp2FrameError (type, code, id) { | ||||||
if (id === 0) { | ||||||
const err = new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`) | ||||||
|
@@ -166,6 +184,7 @@ function onHttp2FrameError (type, code, id) { | |||||
} | ||||||
} | ||||||
|
||||||
/** @this {ClientHttp2Session} */ | ||||||
function onHttp2SessionEnd () { | ||||||
const err = new SocketError('other side closed', util.getSocketInfo(this[kSocket])) | ||||||
this.destroy(err) | ||||||
|
@@ -177,23 +196,19 @@ function onHttp2SessionEnd () { | |||||
* We need to handle GOAWAY frames properly, and trigger the session close | ||||||
* along with the socket right away | ||||||
* | ||||||
* @this {import('http2').ClientHttp2Session} | ||||||
* @this {ClientHttp2Session} | ||||||
* @param {number} errorCode | ||||||
* @param {number} lastStreamID | ||||||
* @param {Buffer} opaqueData | ||||||
*/ | ||||||
function onHttp2SessionGoAway (errorCode) { | ||||||
function onHttp2SessionGoAway (errorCode, lastStreamID, opaqueData) { | ||||||
// TODO(mcollina): Verify if GOAWAY implements the spec correctly: | ||||||
// https://datatracker.ietf.org/doc/html/rfc7540#section-6.8 | ||||||
// Specifically, we do not verify the "valid" stream id. | ||||||
|
||||||
const err = this[kError] || new SocketError(`HTTP/2: "GOAWAY" frame received with code ${errorCode}`, util.getSocketInfo(this[kSocket])) | ||||||
const err = this[kError] || new SocketError(`HTTP/2: "GOAWAY" frame received with code ${errorCode}`, this[kSocket] && util.getSocketInfo(this[kSocket])) | ||||||
const client = this[kClient] | ||||||
|
||||||
client[kSocket] = null | ||||||
client[kHTTPContext] = null | ||||||
|
||||||
// this is an HTTP2 session | ||||||
this.close() | ||||||
this[kHTTP2Session] = null | ||||||
|
||||||
util.destroy(this[kSocket], err) | ||||||
|
||||||
|
@@ -213,9 +228,10 @@ function onHttp2SessionGoAway (errorCode) { | |||||
client[kResume]() | ||||||
} | ||||||
|
||||||
/** @this {ClientHttp2Session} */ | ||||||
function onHttp2SessionClose () { | ||||||
const { [kClient]: client } = this | ||||||
const { [kSocket]: socket } = client | ||||||
const client = this[kClient] | ||||||
const socket = client[kSocket] | ||||||
|
||||||
const err = this[kSocket][kError] || this[kError] || new SocketError('closed', util.getSocketInfo(socket)) | ||||||
|
||||||
|
@@ -234,6 +250,7 @@ function onHttp2SessionClose () { | |||||
} | ||||||
} | ||||||
|
||||||
/** @this {Socket} */ | ||||||
function onHttp2SocketClose () { | ||||||
const err = this[kError] || new SocketError('closed', util.getSocketInfo(this)) | ||||||
|
||||||
|
@@ -255,6 +272,10 @@ function onHttp2SocketClose () { | |||||
client[kResume]() | ||||||
} | ||||||
|
||||||
/** | ||||||
* @this {Socket} | ||||||
* @param {Error} err | ||||||
*/ | ||||||
function onHttp2SocketError (err) { | ||||||
assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID') | ||||||
|
||||||
|
@@ -263,15 +284,30 @@ function onHttp2SocketError (err) { | |||||
this[kClient][kOnError](err) | ||||||
} | ||||||
|
||||||
/** @this {Socket} */ | ||||||
function onHttp2SocketEnd () { | ||||||
util.destroy(this, new SocketError('other side closed', util.getSocketInfo(this))) | ||||||
} | ||||||
|
||||||
/** @this {Socket} */ | ||||||
function onSocketClose () { | ||||||
this[kClosed] = true | ||||||
} | ||||||
|
||||||
// https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2 | ||||||
/** | ||||||
* @overload | ||||||
* @param {'GET' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT'} method | ||||||
* @returns {false} | ||||||
*//** | ||||||
* @overload | ||||||
* @param {Uppercase<string>} method | ||||||
* @returns {true} | ||||||
*//** | ||||||
* @param {Uppercase<string>} method | ||||||
* @returns {boolean} | ||||||
* | ||||||
* @see https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2 | ||||||
*/ | ||||||
function shouldSendContentLength (method) { | ||||||
return method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS' && method !== 'TRACE' && method !== 'CONNECT' | ||||||
} | ||||||
|
@@ -318,7 +354,7 @@ function writeH2 (client, request) { | |||||
} | ||||||
|
||||||
/** @type {import('node:http2').ClientHttp2Stream} */ | ||||||
let stream = null | ||||||
let stream | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Variable
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
const { hostname, port } = client[kUrl] | ||||||
|
||||||
|
@@ -334,7 +370,7 @@ function writeH2 (client, request) { | |||||
|
||||||
util.errorRequest(client, request, err) | ||||||
|
||||||
if (stream != null) { | ||||||
if (stream !== undefined) { | ||||||
// Some chunks might still come after abort, | ||||||
// let's ignore them | ||||||
stream.removeAllListeners('data') | ||||||
|
@@ -371,7 +407,7 @@ function writeH2 (client, request) { | |||||
// `ready` event is triggered | ||||||
// We disabled endStream to allow the user to write to the stream | ||||||
stream = session.request(headers, { endStream: false, signal }) | ||||||
stream.on('response', headers => { | ||||||
stream.on('response', (headers, flags, rawHeaders) => { | ||||||
const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers | ||||||
|
||||||
request.onUpgrade(statusCode, parseH2Headers(realHeaders), stream) | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The null check
this[kSocket] &&
will passfalse
toutil.getSocketInfo()
when socket is null/undefined, which may cause unexpected behavior. Consider using a ternary operator:this[kSocket] ? util.getSocketInfo(this[kSocket]) : null
Copilot uses AI. Check for mistakes.