[windows] fix(http2) (#8734)

* fix win http2

* add includes

* cleanup

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ciro Spaciari
2024-02-06 20:37:39 -03:00
committed by GitHub
parent d3d5c7a1d6
commit 53814dda00
7 changed files with 313 additions and 596 deletions

View File

@@ -679,7 +679,10 @@ function emitAbortedNT(self, streams, streamId, error) {
self.emit("streamError", error_instance);
}
class ClientHttp2Session extends Http2Session {
/// close indicates that we called closed
#closed: boolean = false;
/// connected indicates that the connection/socket is connected
#connected: boolean = false;
#queue: Array<Buffer> = [];
#connections: number = 0;
[bunHTTP2Socket]: TLSSocket | Socket | null;
@@ -763,33 +766,33 @@ class ClientHttp2Session extends Http2Session {
) {
if (!self) return;
var stream = self.#streams.get(streamId);
if (stream) {
let status: string | number = headers[":status"] as string;
if (status) {
// client status is always number
status = parseInt(status as string, 10);
(headers as Record<string, string | number>)[":status"] = status;
}
if (!stream) return;
let set_cookies = headers["set-cookie"];
if (typeof set_cookies === "string") {
(headers as Record<string, string | string[]>)["set-cookie"] = [set_cookies];
}
let status: string | number = headers[":status"] as string;
if (status) {
// client status is always number
status = parseInt(status as string, 10);
(headers as Record<string, string | number>)[":status"] = status;
}
let cookie = headers["cookie"];
if ($isArray(cookie)) {
headers["cookie"] = (headers["cookie"] as string[]).join(";");
}
if (stream[bunHTTP2StreamResponded]) {
try {
stream.emit("trailers", headers, flags);
} catch {
process.nextTick(emitStreamErrorNT, self, self.#streams, streamId, constants.NGHTTP2_PROTOCOL_ERROR, true);
}
} else {
stream[bunHTTP2StreamResponded] = true;
stream.emit("response", headers, flags);
let set_cookies = headers["set-cookie"];
if (typeof set_cookies === "string") {
(headers as Record<string, string | string[]>)["set-cookie"] = [set_cookies];
}
let cookie = headers["cookie"];
if ($isArray(cookie)) {
headers["cookie"] = (headers["cookie"] as string[]).join(";");
}
if (stream[bunHTTP2StreamResponded]) {
try {
stream.emit("trailers", headers, flags);
} catch {
process.nextTick(emitStreamErrorNT, self, self.#streams, streamId, constants.NGHTTP2_PROTOCOL_ERROR, true);
}
} else {
stream[bunHTTP2StreamResponded] = true;
stream.emit("response", headers, flags);
}
},
localSettings(self: ClientHttp2Session, settings: Settings) {
@@ -874,12 +877,13 @@ class ClientHttp2Session extends Http2Session {
write(self: ClientHttp2Session, buffer: Buffer) {
if (!self) return;
const socket = self[bunHTTP2Socket];
if (self.#closed) {
//queue
self.#queue.push(buffer);
} else {
if (!socket) return;
if (self.#connected) {
// redirect writes to socket
socket.write(buffer);
} else {
//queue
self.#queue.push(buffer);
}
},
};
@@ -899,6 +903,7 @@ class ClientHttp2Session extends Http2Session {
#onConnect() {
const socket = this[bunHTTP2Socket];
if (!socket) return;
this.#connected = true;
// check if h2 is supported only for TLSSocket
if (socket instanceof TLSSocket) {
if (socket.alpnProtocol !== "h2") {
@@ -918,6 +923,7 @@ class ClientHttp2Session extends Http2Session {
// TODO: make a native bindings on data and write and fallback to non-native
socket.on("data", this.#onRead.bind(this));
// redirect the queued buffers
const queue = this.#queue;
while (queue.length) {
@@ -1129,6 +1135,7 @@ class ClientHttp2Session extends Http2Session {
destroy(error?: Error, code?: number) {
const socket = this[bunHTTP2Socket];
this.#closed = true;
this.#connected = false;
code = code || constants.NGHTTP2_NO_ERROR;
if (socket) {
this.goaway(code, 0, Buffer.alloc(0));