Skip to content

Commit

Permalink
add TLSSocket socket first parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Jul 3, 2023
1 parent 58abc4d commit 234d1b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
24 changes: 18 additions & 6 deletions src/js/node/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ const Socket = (function (InternalSocket) {
_handle;
_parent;
_parentWrap;
#socket;

constructor(options) {
const { signal, write, read, allowHalfOpen = false, ...opts } = options || {};
const { socket, signal, write, read, allowHalfOpen = false, ...opts } = options || {};
super({
...opts,
allowHalfOpen,
Expand All @@ -313,6 +314,9 @@ const Socket = (function (InternalSocket) {
this._parent = this;
this._parentWrap = this;
this.#pendingRead = undefined;
if (socket instanceof Socket) {
this.#socket = socket;
}
signal?.once("abort", () => this.destroy());
this.once("connect", () => this.emit("ready"));
}
Expand Down Expand Up @@ -342,7 +346,7 @@ const Socket = (function (InternalSocket) {

connect(port, host, connectListener) {
var path;
var connection;
var connection = this.#socket;
if (typeof port === "string") {
path = port;
port = undefined;
Expand Down Expand Up @@ -383,9 +387,6 @@ const Socket = (function (InternalSocket) {

this.servername = servername;
if (socket) {
if (typeof socket !== "object" || !(socket instanceof Socket) || typeof socket[bunTlsSymbol] === "function") {
throw new TypeError("socket must be an instance of net.Socket");
}
connection = socket;
}
}
Expand Down Expand Up @@ -415,9 +416,20 @@ const Socket = (function (InternalSocket) {
} else {
tls.rejectUnauthorized = rejectUnauthorized;
tls.requestCert = true;
if (!connection && tls.socket) {
connection = tls.socket;
}
}
}
if (connection) {
if (
typeof connection !== "object" ||
!(connection instanceof Socket) ||
typeof connection[bunTlsSymbol] === "function"
) {
throw new TypeError("socket must be an instance of net.Socket");
}
}

this.authorized = false;
this.secureConnecting = true;
this._secureEstablished = false;
Expand Down
8 changes: 5 additions & 3 deletions src/js/node/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,18 @@ const TLSSocket = (function (InternalTLSSocket) {
class TLSSocket extends InternalTCPSocket {
#secureContext;
ALPNProtocols;
#socket;

constructor(socket, options) {
super(options || socket);
super(socket instanceof InternalTCPSocket ? options : options || socket);
options = options || socket || {};
if (options) {
if (typeof options === "object") {
const { ALPNProtocols } = options;
if (ALPNProtocols) {
convertALPNProtocols(ALPNProtocols, this);
}
if (socket instanceof InternalTCPSocket) {
options.socket = socket;
this.#socket = socket;
}
}

Expand Down Expand Up @@ -401,6 +402,7 @@ const TLSSocket = (function (InternalTLSSocket) {

[buntls](port, host) {
return {
socket: this.#socket,
ALPNProtocols: this.ALPNProtocols,
serverName: this.servername || host || "localhost",
...this.#secureContext,
Expand Down
25 changes: 15 additions & 10 deletions src/js/out/modules/node/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,18 @@ var isIPv4 = function(s) {
_handle;
_parent;
_parentWrap;
#socket;
constructor(options) {
const { signal, write, read, allowHalfOpen = !1, ...opts } = options || {};
const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};
super({
...opts,
allowHalfOpen,
readable: !0,
writable: !0
});
this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = void 0, signal?.once("abort", () => this.destroy()), this.once("connect", () => this.emit("ready"));
if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = void 0, socket instanceof Socket2)
this.#socket = socket;
signal?.once("abort", () => this.destroy()), this.once("connect", () => this.emit("ready"));
}
address() {
return {
Expand All @@ -196,7 +199,7 @@ var isIPv4 = function(s) {
this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, this.emit("connect", this), Socket2.#Drain(socket);
}
connect(port, host, connectListener) {
var path, connection;
var path, connection = this.#socket;
if (typeof port === "string") {
if (path = port, port = void 0, typeof host === "function")
connectListener = host, host = void 0;
Expand Down Expand Up @@ -224,26 +227,28 @@ var isIPv4 = function(s) {
pauseOnConnect,
servername
} = port;
if (this.servername = servername, socket) {
if (typeof socket !== "object" || !(socket instanceof Socket2) || typeof socket[bunTlsSymbol] === "function")
throw new TypeError("socket must be an instance of net.Socket");
if (this.servername = servername, socket)
connection = socket;
}
}
if (!pauseOnConnect)
this.resume();
this.connecting = !0, this.remotePort = port;
const bunTLS = this[bunTlsSymbol];
var tls = void 0;
if (typeof bunTLS === "function") {
if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls)
if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {
if (typeof tls !== "object")
tls = {
rejectUnauthorized,
requestCert: !0
};
else
tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0;
else if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, !connection && tls.socket)
connection = tls.socket;
}
if (connection) {
if (typeof connection !== "object" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === "function")
throw new TypeError("socket must be an instance of net.Socket");
}
if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)
this.on("secureConnect", connectListener);
} else if (connectListener)
Expand Down
8 changes: 5 additions & 3 deletions src/js/out/modules/node/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ var createServer = function(options, connectionListener) {
}(class TLSSocket2 extends InternalTCPSocket {
#secureContext;
ALPNProtocols;
#socket;
constructor(socket, options) {
super(options || socket);
if (options = options || socket || {}, options) {
super(socket instanceof InternalTCPSocket ? options : options || socket);
if (options = options || socket || {}, typeof options === "object") {
const { ALPNProtocols } = options;
if (ALPNProtocols)
convertALPNProtocols(ALPNProtocols, this);
if (socket instanceof InternalTCPSocket)
options.socket = socket;
this.#socket = socket;
}
this.#secureContext = options.secureContext || createSecureContext(options), this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0;
}
Expand Down Expand Up @@ -237,6 +238,7 @@ var createServer = function(options, connectionListener) {
}
[buntls](port, host2) {
return {
socket: this.#socket,
ALPNProtocols: this.ALPNProtocols,
serverName: this.servername || host2 || "localhost",
...this.#secureContext
Expand Down

0 comments on commit 234d1b3

Please sign in to comment.