mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
js: de-class-ify node:net.Socket (#17997)
This commit is contained in:
@@ -11,7 +11,7 @@ Bun.listen({
|
||||
socket: {
|
||||
data(socket, data) {}, // message received from client
|
||||
open(socket) {}, // socket opened
|
||||
close(socket) {}, // socket closed
|
||||
close(socket, error) {}, // socket closed
|
||||
drain(socket) {}, // socket ready for more data
|
||||
error(socket, error) {}, // error handler
|
||||
},
|
||||
@@ -30,7 +30,7 @@ Bun.listen({
|
||||
open(socket) {},
|
||||
data(socket, data) {},
|
||||
drain(socket) {},
|
||||
close(socket) {},
|
||||
close(socket, error) {},
|
||||
error(socket, error) {},
|
||||
},
|
||||
});
|
||||
@@ -122,7 +122,7 @@ const socket = await Bun.connect({
|
||||
socket: {
|
||||
data(socket, data) {},
|
||||
open(socket) {},
|
||||
close(socket) {},
|
||||
close(socket, error) {},
|
||||
drain(socket) {},
|
||||
error(socket, error) {},
|
||||
|
||||
|
||||
2
packages/bun-types/bun.d.ts
vendored
2
packages/bun-types/bun.d.ts
vendored
@@ -6297,7 +6297,7 @@ declare module "bun" {
|
||||
* @param socket
|
||||
*/
|
||||
open?(socket: Socket<Data>): void | Promise<void>;
|
||||
close?(socket: Socket<Data>): void | Promise<void>;
|
||||
close?(socket: Socket<Data>, error?: Error): void | Promise<void>;
|
||||
error?(socket: Socket<Data>, error: Error): void | Promise<void>;
|
||||
data?(socket: Socket<Data>, data: BinaryTypeList[DataBinaryType]): void | Promise<void>;
|
||||
drain?(socket: Socket<Data>): void | Promise<void>;
|
||||
|
||||
@@ -1220,37 +1220,9 @@ pub const Listener = struct {
|
||||
if (ssl.?.server_name) |s| {
|
||||
server_name = bun.default_allocator.dupe(u8, s[0..bun.len(s)]) catch bun.outOfMemory();
|
||||
}
|
||||
uws.NewSocketHandler(true).configure(
|
||||
socket_context,
|
||||
true,
|
||||
*TLSSocket,
|
||||
struct {
|
||||
pub const onOpen = NewSocket(true).onOpen;
|
||||
pub const onClose = NewSocket(true).onClose;
|
||||
pub const onData = NewSocket(true).onData;
|
||||
pub const onWritable = NewSocket(true).onWritable;
|
||||
pub const onTimeout = NewSocket(true).onTimeout;
|
||||
pub const onConnectError = NewSocket(true).onConnectError;
|
||||
pub const onEnd = NewSocket(true).onEnd;
|
||||
pub const onHandshake = NewSocket(true).onHandshake;
|
||||
},
|
||||
);
|
||||
uws.NewSocketHandler(true).configure(socket_context, true, *TLSSocket, NewSocket(true));
|
||||
} else {
|
||||
uws.NewSocketHandler(false).configure(
|
||||
socket_context,
|
||||
true,
|
||||
*TCPSocket,
|
||||
struct {
|
||||
pub const onOpen = NewSocket(false).onOpen;
|
||||
pub const onClose = NewSocket(false).onClose;
|
||||
pub const onData = NewSocket(false).onData;
|
||||
pub const onWritable = NewSocket(false).onWritable;
|
||||
pub const onTimeout = NewSocket(false).onTimeout;
|
||||
pub const onConnectError = NewSocket(false).onConnectError;
|
||||
pub const onEnd = NewSocket(false).onEnd;
|
||||
pub const onHandshake = NewSocket(false).onHandshake;
|
||||
},
|
||||
);
|
||||
uws.NewSocketHandler(false).configure(socket_context, true, *TCPSocket, NewSocket(false));
|
||||
}
|
||||
|
||||
default_data.ensureStillAlive();
|
||||
|
||||
@@ -1958,7 +1958,7 @@ static JSValue constructReportObjectComplete(VM& vm, Zig::GlobalObject* globalOb
|
||||
stackArray->push(globalObject, JSC::jsString(vm, line.toString().trim(isASCIIWhitespace)));
|
||||
});
|
||||
|
||||
javascriptStack->putDirect(vm, JSC::Identifier::fromString(vm, "stack"_s), stackArray, 0);
|
||||
javascriptStack->putDirect(vm, vm.propertyNames->stack, stackArray, 0);
|
||||
}
|
||||
|
||||
JSC::JSObject* errorProperties = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 1);
|
||||
|
||||
@@ -1631,12 +1631,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
|
||||
// debug("us_socket_shutdown({d})", .{@intFromPtr(this.socket)});
|
||||
switch (this.socket) {
|
||||
.connected => |socket| {
|
||||
debug("us_socket_shutdown({d})", .{@intFromPtr(socket)});
|
||||
return us_socket_shutdown(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
);
|
||||
},
|
||||
.connecting => |socket| {
|
||||
debug("us_connecting_socket_shutdown({d})", .{@intFromPtr(socket)});
|
||||
return us_connecting_socket_shutdown(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
@@ -1655,14 +1657,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
|
||||
pub fn shutdownRead(this: ThisSocket) void {
|
||||
switch (this.socket) {
|
||||
.connected => |socket| {
|
||||
// debug("us_socket_shutdown_read({d})", .{@intFromPtr(socket)});
|
||||
debug("us_socket_shutdown_read({d})", .{@intFromPtr(socket)});
|
||||
return us_socket_shutdown_read(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
);
|
||||
},
|
||||
.connecting => |socket| {
|
||||
// debug("us_connecting_socket_shutdown_read({d})", .{@intFromPtr(socket)});
|
||||
debug("us_connecting_socket_shutdown_read({d})", .{@intFromPtr(socket)});
|
||||
return us_connecting_socket_shutdown_read(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
@@ -1681,12 +1683,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
|
||||
pub fn isShutdown(this: ThisSocket) bool {
|
||||
switch (this.socket) {
|
||||
.connected => |socket| {
|
||||
debug("us_socket_is_shut_down({d})", .{@intFromPtr(socket)});
|
||||
return us_socket_is_shut_down(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
) > 0;
|
||||
},
|
||||
.connecting => |socket| {
|
||||
debug("us_connecting_socket_is_shut_down({d})", .{@intFromPtr(socket)});
|
||||
return us_connecting_socket_is_shut_down(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
@@ -1713,12 +1717,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
|
||||
pub fn getError(this: ThisSocket) i32 {
|
||||
switch (this.socket) {
|
||||
.connected => |socket| {
|
||||
debug("us_socket_get_error({d})", .{@intFromPtr(socket)});
|
||||
return us_socket_get_error(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
);
|
||||
},
|
||||
.connecting => |socket| {
|
||||
debug("us_connecting_socket_get_error({d})", .{@intFromPtr(socket)});
|
||||
return us_connecting_socket_get_error(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
@@ -1744,6 +1750,7 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
|
||||
pub fn localPort(this: ThisSocket) i32 {
|
||||
switch (this.socket) {
|
||||
.connected => |socket| {
|
||||
debug("us_socket_local_port({d})", .{@intFromPtr(socket)});
|
||||
return us_socket_local_port(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
@@ -1755,6 +1762,7 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
|
||||
pub fn remoteAddress(this: ThisSocket, buf: [*]u8, length: *i32) void {
|
||||
switch (this.socket) {
|
||||
.connected => |socket| {
|
||||
debug("us_socket_remote_address({d})", .{@intFromPtr(socket)});
|
||||
return us_socket_remote_address(
|
||||
comptime ssl_int,
|
||||
socket,
|
||||
|
||||
@@ -73,22 +73,9 @@ export default {
|
||||
validateObject: $newCppFunction("NodeValidator.cpp", "jsFunction_validateObject", 2),
|
||||
validateLinkHeaderValue: validateLinkHeaderValue,
|
||||
checkIsHttpToken: checkIsHttpToken,
|
||||
/**
|
||||
* @param value the value that should be an int
|
||||
* @paran name the name of the parameter. Used when creating error codes
|
||||
* @param min minimum value, inclusive. Defaults to {@link Number.MIN_SAFE_INTEGER}.
|
||||
* @param max maximum value, inclusive. Defaults to {@link Number.MAX_SAFE_INTEGER}.
|
||||
*
|
||||
* @throws if `value` is not an int
|
||||
* @throws if `value` is outside `[min, max]`
|
||||
*/
|
||||
/** `(value, name, min, max)` */
|
||||
validateInteger: $newCppFunction("NodeValidator.cpp", "jsFunction_validateInteger", 0),
|
||||
/**
|
||||
* @param value the value that should be an int
|
||||
* @paran name the name of the parameter. Used when creating error codes
|
||||
* @param min minimum value, exclusive. Defaults to {@link Number.MIN_SAFE_INTEGER}.
|
||||
* @param max maximum value, exclusive. Defaults to {@link Number.MAX_SAFE_INTEGER}.
|
||||
*/
|
||||
/** `(value, name, min, max)` */
|
||||
validateNumber: $newCppFunction("NodeValidator.cpp", "jsFunction_validateNumber", 0),
|
||||
/** `(value, name)` */
|
||||
validateString: $newCppFunction("NodeValidator.cpp", "jsFunction_validateString", 0),
|
||||
|
||||
1968
src/js/node/net.ts
1968
src/js/node/net.ts
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,11 @@
|
||||
// Hardcoded module "node:tls"
|
||||
const { isArrayBufferView, isArrayBuffer, isTypedArray } = require("node:util/types");
|
||||
const { addServerName } = require("../internal/net");
|
||||
const net = require("node:net");
|
||||
const { Duplex } = require("node:stream");
|
||||
const { addServerName } = require("internal/net");
|
||||
const { throwNotImplemented } = require("internal/shared");
|
||||
|
||||
const { Server: NetServer, [Symbol.for("::bunternal::")]: InternalTCPSocket } = net;
|
||||
const { Server: NetServer, Socket: NetSocket } = net;
|
||||
|
||||
const { rootCertificates, canonicalizeIP } = $cpp("NodeTLS.cpp", "createNodeTLSBinding");
|
||||
|
||||
@@ -305,7 +306,7 @@ const TLSSocket = (function (InternalTLSSocket) {
|
||||
},
|
||||
});
|
||||
})(
|
||||
class TLSSocket extends InternalTCPSocket {
|
||||
class TLSSocket extends NetSocket {
|
||||
#secureContext;
|
||||
ALPNProtocols;
|
||||
#checkServerIdentity;
|
||||
@@ -313,14 +314,14 @@ const TLSSocket = (function (InternalTLSSocket) {
|
||||
alpnProtocol = null;
|
||||
|
||||
constructor(socket, options) {
|
||||
super(socket instanceof InternalTCPSocket || socket instanceof Duplex ? options : options || socket);
|
||||
super(socket instanceof NetSocket || socket instanceof Duplex ? options : options || socket);
|
||||
options = options || socket || {};
|
||||
if (typeof options === "object") {
|
||||
const { ALPNProtocols } = options;
|
||||
if (ALPNProtocols) {
|
||||
convertALPNProtocols(ALPNProtocols, this);
|
||||
}
|
||||
if (socket instanceof InternalTCPSocket || socket instanceof Duplex) {
|
||||
if (socket instanceof NetSocket || socket instanceof Duplex) {
|
||||
this._handle = socket;
|
||||
// keep compatibility with http2-wrapper or other places that try to grab JSStreamSocket in node.js, with here is just the TLSSocket
|
||||
this._handle._parentWrap = this;
|
||||
|
||||
Reference in New Issue
Block a user