1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

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

Fixes https://github.com/denoland/deno/issues/21379
This commit is contained in:
Divy Srivastava 2024-03-09 09:36:17 +05:30 committed by GitHub
parent 0bed4d3e51
commit e1fb174f86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 8 deletions

View file

@ -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);
@ -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]);

View file

@ -703,9 +703,11 @@ 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]
@ -713,9 +715,11 @@ pub fn op_ws_get_buffer(
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]