aboutsummaryrefslogtreecommitdiff
path: root/src/js/node/net.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/node/net.js')
-rw-r--r--src/js/node/net.js24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/js/node/net.js b/src/js/node/net.js
index fc0aba548..f8da33d53 100644
--- a/src/js/node/net.js
+++ b/src/js/node/net.js
@@ -66,6 +66,9 @@ const bunSocketServerOptions = Symbol.for("::bunnetserveroptions::");
const bunSocketInternal = Symbol.for("::bunnetsocketinternal::");
const bunTLSConnectOptions = Symbol.for("::buntlsconnectoptions::");
+function closeNT(self) {
+ self.emit("close");
+}
function endNT(socket, callback, err) {
socket.end();
callback(err);
@@ -320,7 +323,7 @@ const Socket = (function (InternalSocket) {
this._parent = this;
this._parentWrap = this;
this.#pendingRead = undefined;
- this.#upgraded = false;
+ this.#upgraded = null;
if (socket instanceof Socket) {
this.#socket = socket;
}
@@ -355,6 +358,14 @@ const Socket = (function (InternalSocket) {
Socket.#Drain(socket);
}
+ #closeRawConnection() {
+ const connection = this.#upgraded;
+ connection[bunSocketInternal] = null;
+ connection.unref();
+ connection.destroy();
+ process.nextTick(closeNT, connection);
+ }
+
connect(port, host, connectListener) {
var path;
var connection = this.#socket;
@@ -455,7 +466,7 @@ const Socket = (function (InternalSocket) {
if (socket) {
this.connecting = true;
- this.#upgraded = true;
+ this.#upgraded = connection;
const result = socket.upgradeTLS({
data: this,
tls,
@@ -466,6 +477,7 @@ const Socket = (function (InternalSocket) {
// replace socket
connection[bunSocketInternal] = raw;
raw.timeout(raw.timeout);
+ this.once("end", this.#closeRawConnection);
raw.connecting = false;
this[bunSocketInternal] = tls;
} else {
@@ -479,7 +491,7 @@ const Socket = (function (InternalSocket) {
if (!socket) return;
this.connecting = true;
- this.#upgraded = true;
+ this.#upgraded = connection;
const result = socket.upgradeTLS({
data: this,
tls,
@@ -491,6 +503,7 @@ const Socket = (function (InternalSocket) {
// replace socket
connection[bunSocketInternal] = raw;
raw.timeout(raw.timeout);
+ this.once("end", this.#closeRawConnection);
raw.connecting = false;
this[bunSocketInternal] = tls;
} else {
@@ -537,6 +550,7 @@ const Socket = (function (InternalSocket) {
_final(callback) {
this[bunSocketInternal]?.end();
callback();
+ process.nextTick(closeNT, this);
}
get localAddress() {
@@ -559,8 +573,10 @@ const Socket = (function (InternalSocket) {
const queue = this.#readQueue;
let chunk;
while ((chunk = queue.peek())) {
- if (!this.push(chunk)) return;
+ const can_continue = !this.push(chunk);
+ // always remove from queue push will queue it internally if needed
queue.shift();
+ if (!can_continue) break;
}
}