mirror of
https://github.com/denoland/deno.git
synced 2025-01-06 22:35:51 -05:00
feat: use buffer, introduce flushHeaders, flushBody
This commit is contained in:
parent
a3363a9a6a
commit
ae5d72a870
2 changed files with 34 additions and 10 deletions
|
@ -491,8 +491,34 @@ Object.defineProperties(
|
||||||
return ret;
|
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
|
// deno-lint-ignore no-explicit-any
|
||||||
_send(data: any, encoding?: string | null, callback?: () => void) {
|
_send(data: any, encoding?: string | null, callback?: () => void) {
|
||||||
|
console.log("writing data:", data, "socket:", this.socket);
|
||||||
if (this.socket) {
|
if (this.socket) {
|
||||||
if (!this._headerSent && this._header !== null) {
|
if (!this._headerSent && this._header !== null) {
|
||||||
this._writeHeader();
|
this._writeHeader();
|
||||||
|
@ -500,15 +526,8 @@ Object.defineProperties(
|
||||||
}
|
}
|
||||||
return this._writeRaw(data, encoding, callback);
|
return this._writeRaw(data, encoding, callback);
|
||||||
} else {
|
} else {
|
||||||
this.on("socket", (socket) => {
|
console.log("pushing data to outputData");
|
||||||
socket.on("connect", () => {
|
this.outputData.push({ data, encoding, callback });
|
||||||
if (!this._headerSent && this._header !== null) {
|
|
||||||
this._writeHeader();
|
|
||||||
this._headerSent = true;
|
|
||||||
}
|
|
||||||
return this._writeRaw(data, encoding, callback);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -522,6 +541,7 @@ Object.defineProperties(
|
||||||
encoding?: string | null,
|
encoding?: string | null,
|
||||||
callback?: () => void,
|
callback?: () => void,
|
||||||
) {
|
) {
|
||||||
|
console.log("_writeRaw invoked:", data.length);
|
||||||
if (typeof data === "string") {
|
if (typeof data === "string") {
|
||||||
data = Buffer.from(data, encoding);
|
data = Buffer.from(data, encoding);
|
||||||
}
|
}
|
||||||
|
|
|
@ -599,7 +599,11 @@ class ClientRequest extends OutgoingMessage {
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.emit("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();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue