Skip to content

Commit

Permalink
fix(ext/websocket): do not continue reading if socket rid closes (#21849
Browse files Browse the repository at this point in the history
)

Fixes #21379
  • Loading branch information
littledivy authored and nathanwhit committed Mar 14, 2024
1 parent 4419fc3 commit 010272a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 12 additions & 2 deletions ext/websocket/01_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,14 @@ class WebSocket extends EventTarget {
switch (kind) {
case 0: {
/* string */
const data = op_ws_get_buffer_as_string(rid);
if (data === undefined) {
break;
}

this[_serverHandleIdleTimeout]();
const event = new MessageEvent("message", {
data: op_ws_get_buffer_as_string(rid),
data,
origin: this[_url],
});
setIsTrusted(event, true);
Expand All @@ -428,9 +433,14 @@ class WebSocket extends EventTarget {
}
case 1: {
/* binary */
const d = op_ws_get_buffer(rid);
if (d == undefined) {
break;
}

this[_serverHandleIdleTimeout]();
// deno-lint-ignore prefer-primordials
const buffer = op_ws_get_buffer(rid).buffer;
const buffer = d.buffer;
let data;
if (this.binaryType === "blob") {
data = new Blob([buffer]);
Expand Down
16 changes: 10 additions & 6 deletions ext/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,19 +703,23 @@ pub async fn op_ws_close(
pub fn op_ws_get_buffer(
state: &mut OpState,
#[smi] rid: ResourceId,
) -> Result<ToJsBuffer, AnyError> {
let resource = state.resource_table.get::<ServerWebSocket>(rid)?;
Ok(resource.buffer.take().unwrap().into())
) -> Option<ToJsBuffer> {
let Ok(resource) = state.resource_table.get::<ServerWebSocket>(rid) else {
return None;
};
resource.buffer.take().map(ToJsBuffer::from)
}

#[op2]
#[string]
pub fn op_ws_get_buffer_as_string(
state: &mut OpState,
#[smi] rid: ResourceId,
) -> Result<String, AnyError> {
let resource = state.resource_table.get::<ServerWebSocket>(rid)?;
Ok(resource.string.take().unwrap())
) -> Option<String> {
let Ok(resource) = state.resource_table.get::<ServerWebSocket>(rid) else {
return None;
};
resource.string.take()
}

#[op2]
Expand Down

0 comments on commit 010272a

Please sign in to comment.