Skip to content

Commit

Permalink
Fix [Symbol.dispose] on Bun.listen() & Bun.connect() + add types (ove…
Browse files Browse the repository at this point in the history
…n-sh#12739)

Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
  • Loading branch information
Jarred-Sumner and dylan-conway authored Jul 26, 2024
1 parent 5a18b7d commit 3bfeb83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3847,7 +3847,7 @@ declare module "bun" {
*/
const isMainThread: boolean;

interface Socket<Data = undefined> {
interface Socket<Data = undefined> extends Disposable {
/**
* Write `data` to the socket
*
Expand Down Expand Up @@ -4129,7 +4129,7 @@ declare module "bun" {
setMaxSendFragment(size: number): boolean;
}

interface SocketListener<Data = undefined> {
interface SocketListener<Data = undefined> extends Disposable {
stop(closeActiveConnections?: boolean): void;
ref(): void;
unref(): void;
Expand Down
18 changes: 13 additions & 5 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,22 @@ pub const Listener = struct {
return JSValue.jsUndefined();
}

pub fn dispose(this: *Listener, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue {
this.doStop(true);
return .undefined;
}

pub fn stop(this: *Listener, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue {
const arguments = callframe.arguments(1);
log("close", .{});

var listener = this.listener orelse return JSValue.jsUndefined();
this.doStop(if (arguments.len > 0 and arguments.ptr[0].isBoolean()) arguments.ptr[0].toBoolean() else false);

return .undefined;
}

fn doStop(this: *Listener, force_close: bool) void {
var listener = this.listener orelse return;
this.listener = null;

this.poll_ref.unref(this.handlers.vm);
Expand All @@ -891,17 +902,14 @@ pub const Listener = struct {
this.strong_self.clear();
this.strong_data.clear();
} else {
const forceClose = arguments.len > 0 and arguments.ptr[0].isBoolean() and arguments.ptr[0].toBoolean() and this.socket_context != null;
if (forceClose) {
if (force_close) {
// close all connections in this context and wait for them to close
this.socket_context.?.close(this.ssl);
} else {
// only close the listener and wait for the connections to close by it self
listener.close(this.ssl);
}
}

return JSValue.jsUndefined();
}

pub fn finalize(this: *Listener) callconv(.C) void {
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/api/sockets.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function generate(ssl) {
},

"@@dispose": {
fn: "shutdown",
fn: "end",
length: 0,
},

Expand Down Expand Up @@ -191,7 +191,7 @@ export default [
length: 1,
},
"@@dispose": {
fn: "stop",
fn: "dispose",
length: 0,
},

Expand Down

0 comments on commit 3bfeb83

Please sign in to comment.