1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -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) { switch (kind) {
case 0: { case 0: {
/* string */ /* string */
const data = op_ws_get_buffer_as_string(rid);
if (data === undefined) {
break;
}
this[_serverHandleIdleTimeout](); this[_serverHandleIdleTimeout]();
const event = new MessageEvent("message", { const event = new MessageEvent("message", {
data: op_ws_get_buffer_as_string(rid), data,
origin: this[_url], origin: this[_url],
}); });
setIsTrusted(event, true); setIsTrusted(event, true);
@ -428,9 +433,14 @@ class WebSocket extends EventTarget {
} }
case 1: { case 1: {
/* binary */ /* binary */
const d = op_ws_get_buffer(rid);
if (d == undefined) {
break;
}
this[_serverHandleIdleTimeout](); this[_serverHandleIdleTimeout]();
// deno-lint-ignore prefer-primordials // deno-lint-ignore prefer-primordials
const buffer = op_ws_get_buffer(rid).buffer; const buffer = d.buffer;
let data; let data;
if (this.binaryType === "blob") { if (this.binaryType === "blob") {
data = new Blob([buffer]); data = new Blob([buffer]);

View file

@ -703,9 +703,11 @@ pub async fn op_ws_close(
pub fn op_ws_get_buffer( pub fn op_ws_get_buffer(
state: &mut OpState, state: &mut OpState,
#[smi] rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<ToJsBuffer, AnyError> { ) -> Option<ToJsBuffer> {
let resource = state.resource_table.get::<ServerWebSocket>(rid)?; let Ok(resource) = state.resource_table.get::<ServerWebSocket>(rid) else {
Ok(resource.buffer.take().unwrap().into()) return None;
};
resource.buffer.take().map(ToJsBuffer::from)
} }
#[op2] #[op2]
@ -713,9 +715,11 @@ pub fn op_ws_get_buffer(
pub fn op_ws_get_buffer_as_string( pub fn op_ws_get_buffer_as_string(
state: &mut OpState, state: &mut OpState,
#[smi] rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<String, AnyError> { ) -> Option<String> {
let resource = state.resource_table.get::<ServerWebSocket>(rid)?; let Ok(resource) = state.resource_table.get::<ServerWebSocket>(rid) else {
Ok(resource.string.take().unwrap()) return None;
};
resource.string.take()
} }
#[op2] #[op2]