Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/websocket): do not continue reading if socket rid closes #21849

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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