1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-05 13:59:01 -05:00

feat: use buffer, introduce flushHeaders, flushBody

This commit is contained in:
Satya Rohith 2024-09-13 14:10:22 +05:30
parent a3363a9a6a
commit ae5d72a870
No known key found for this signature in database
GPG key ID: B2705CF40523EB05
2 changed files with 34 additions and 10 deletions

View file

@ -491,8 +491,34 @@ Object.defineProperties(
return ret;
},
_flushBody() {
const socket = this.socket;
const outputLength = this.outputData.length;
if (socket && socket.writable && outputLength > 0) {
console.log("flushing body");
for (let i = 0; i < outputLength; i++) {
const { data, encoding, callback } = this.outputData[i];
this._writeRaw(data, encoding, callback);
}
this.outputData = [];
}
},
/** Right after socket is ready, we need to writeHeader() to setup the request and
* client. This is invoked by onSocket(). */
_flushHeaders() {
console.log("flushHeaders");
if (this.socket) {
if (!this._headerSent && this._header !== null) {
this._writeHeader();
this._headerSent = true;
}
}
},
// deno-lint-ignore no-explicit-any
_send(data: any, encoding?: string | null, callback?: () => void) {
console.log("writing data:", data, "socket:", this.socket);
if (this.socket) {
if (!this._headerSent && this._header !== null) {
this._writeHeader();
@ -500,15 +526,8 @@ Object.defineProperties(
}
return this._writeRaw(data, encoding, callback);
} else {
this.on("socket", (socket) => {
socket.on("connect", () => {
if (!this._headerSent && this._header !== null) {
this._writeHeader();
this._headerSent = true;
}
return this._writeRaw(data, encoding, callback);
});
});
console.log("pushing data to outputData");
this.outputData.push({ data, encoding, callback });
}
},
@ -522,6 +541,7 @@ Object.defineProperties(
encoding?: string | null,
callback?: () => void,
) {
console.log("_writeRaw invoked:", data.length);
if (typeof data === "string") {
data = Buffer.from(data, encoding);
}

View file

@ -599,7 +599,11 @@ class ClientRequest extends OutgoingMessage {
nextTick(() => {
this.socket = socket;
this.emit("socket", socket);
console.trace("onSocket invoked", socket);
// Flush the internal buffers once socket is assigned.
// Note: the order is important, as the headers flush
// sets up the request.
this._flushHeaders();
this._flushBody();
});
}