Skip to content

Commit

Permalink
chore: upgrade fastwebsockets to 0.4.4 (denoland#19089)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Aug 10, 2023
1 parent 8f85478 commit 91dc6fa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ data-url = "=0.2.0"
dlopen = "0.1.8"
encoding_rs = "=0.8.31"
ecb = "=0.1.1"
fastwebsockets = "=0.3.1"
fastwebsockets = "=0.4.4"
filetime = "0.2.16"
flate2 = { version = "1.0.26", features = ["zlib-ng"], default-features = false }
fs3 = "0.5.0"
Expand Down
6 changes: 3 additions & 3 deletions cli/tests/integration/inspector_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl InspectorTester {
for msg in messages {
let result = self
.socket
.write_frame(Frame::text(msg.to_string().into_bytes()))
.write_frame(Frame::text(msg.to_string().into_bytes().into()))
.await
.map_err(|e| anyhow!(e));
self.handle_error(result);
Expand Down Expand Up @@ -148,7 +148,7 @@ impl InspectorTester {
loop {
let result = self.socket.read_frame().await.map_err(|e| anyhow!(e));
let message =
String::from_utf8(self.handle_error(result).payload).unwrap();
String::from_utf8(self.handle_error(result).payload.to_vec()).unwrap();
if (self.notification_filter)(&message) {
return message;
}
Expand Down Expand Up @@ -540,7 +540,7 @@ async fn inspector_does_not_hang() {
// Check that we can gracefully close the websocket connection.
tester
.socket
.write_frame(Frame::close_raw(vec![]))
.write_frame(Frame::close_raw(vec![].into()))
.await
.unwrap();

Expand Down
12 changes: 6 additions & 6 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4158,26 +4158,26 @@ async fn websocketstream_ping() {
tokio::spawn(async move {
let mut ws = upgrade_fut.await.unwrap();

ws.write_frame(fastwebsockets::Frame::text("A".as_bytes().to_vec()))
ws.write_frame(fastwebsockets::Frame::text(b"A"[..].into()))
.await
.unwrap();
ws.write_frame(fastwebsockets::Frame::new(
true,
fastwebsockets::OpCode::Ping,
None,
vec![],
vec![].into(),
))
.await
.unwrap();
ws.write_frame(fastwebsockets::Frame::text("B".as_bytes().to_vec()))
ws.write_frame(fastwebsockets::Frame::text(b"B"[..].into()))
.await
.unwrap();
let message = ws.read_frame().await.unwrap();
assert_eq!(message.opcode, fastwebsockets::OpCode::Pong);
ws.write_frame(fastwebsockets::Frame::text("C".as_bytes().to_vec()))
ws.write_frame(fastwebsockets::Frame::text(b"C"[..].into()))
.await
.unwrap();
ws.write_frame(fastwebsockets::Frame::close_raw(vec![]))
ws.write_frame(fastwebsockets::Frame::close_raw(vec![].into()))
.await
.unwrap();
});
Expand Down Expand Up @@ -4271,7 +4271,7 @@ async fn websocket_server_multi_field_connection_header() {
assert_eq!(message.opcode, fastwebsockets::OpCode::Close);
assert!(message.payload.is_empty());
socket
.write_frame(fastwebsockets::Frame::close_raw(vec![]))
.write_frame(fastwebsockets::Frame::close_raw(vec![].into()))
.await
.unwrap();
assert!(child.wait().unwrap().success());
Expand Down
28 changes: 17 additions & 11 deletions ext/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl ServerWebSocket {
pub async fn write_frame(
self: &Rc<Self>,
lock: AsyncMutFuture<()>,
frame: Frame,
frame: Frame<'_>,
) -> Result<(), AnyError> {
lock.await;

Expand Down Expand Up @@ -401,7 +401,6 @@ pub fn ws_create_server_stream(
ws.set_writev(*USE_WRITEV);
ws.set_auto_close(true);
ws.set_auto_pong(true);

let rid = state.resource_table.add(ServerWebSocket::new(ws));
Ok(rid)
}
Expand All @@ -415,7 +414,7 @@ pub fn op_ws_send_binary(state: &mut OpState, rid: ResourceId, data: &[u8]) {
let lock = resource.reserve_lock();
deno_core::task::spawn(async move {
if let Err(err) = resource
.write_frame(lock, Frame::new(true, OpCode::Binary, None, data))
.write_frame(lock, Frame::new(true, OpCode::Binary, None, data.into()))
.await
{
resource.set_error(Some(err.to_string()));
Expand All @@ -435,7 +434,7 @@ pub fn op_ws_send_text(state: &mut OpState, rid: ResourceId, data: String) {
if let Err(err) = resource
.write_frame(
lock,
Frame::new(true, OpCode::Text, None, data.into_bytes()),
Frame::new(true, OpCode::Text, None, data.into_bytes().into()),
)
.await
{
Expand All @@ -460,7 +459,7 @@ pub async fn op_ws_send_binary_async(
let data = data.to_vec();
let lock = resource.reserve_lock();
resource
.write_frame(lock, Frame::new(true, OpCode::Binary, None, data))
.write_frame(lock, Frame::new(true, OpCode::Binary, None, data.into()))
.await
}

Expand All @@ -479,11 +478,13 @@ pub async fn op_ws_send_text_async(
resource
.write_frame(
lock,
Frame::new(true, OpCode::Text, None, data.into_bytes()),
Frame::new(true, OpCode::Text, None, data.into_bytes().into()),
)
.await
}

const EMPTY_PAYLOAD: &[u8] = &[];

#[op(fast)]
pub fn op_ws_get_buffered_amount(state: &mut OpState, rid: ResourceId) -> u32 {
state
Expand All @@ -504,7 +505,9 @@ pub async fn op_ws_send_pong(
.resource_table
.get::<ServerWebSocket>(rid)?;
let lock = resource.reserve_lock();
resource.write_frame(lock, Frame::pong(vec![])).await
resource
.write_frame(lock, Frame::pong(EMPTY_PAYLOAD.into()))
.await
}

#[op]
Expand All @@ -518,7 +521,10 @@ pub async fn op_ws_send_ping(
.get::<ServerWebSocket>(rid)?;
let lock = resource.reserve_lock();
resource
.write_frame(lock, Frame::new(true, OpCode::Ping, None, vec![]))
.write_frame(
lock,
Frame::new(true, OpCode::Ping, None, EMPTY_PAYLOAD.into()),
)
.await
}

Expand All @@ -535,7 +541,7 @@ pub async fn op_ws_close(
.get::<ServerWebSocket>(rid)?;
let frame = reason
.map(|reason| Frame::close(code.unwrap_or(1005), reason.as_bytes()))
.unwrap_or_else(|| Frame::close_raw(vec![]));
.unwrap_or_else(|| Frame::close_raw(vec![].into()));

resource.closed.set(true);
let lock = resource.reserve_lock();
Expand Down Expand Up @@ -602,7 +608,7 @@ pub async fn op_ws_next_event(
};

break match val.opcode {
OpCode::Text => match String::from_utf8(val.payload) {
OpCode::Text => match String::from_utf8(val.payload.to_vec()) {
Ok(s) => {
resource.string.set(Some(s));
MessageKind::Text as u16
Expand All @@ -613,7 +619,7 @@ pub async fn op_ws_next_event(
}
},
OpCode::Binary => {
resource.buffer.set(Some(val.payload));
resource.buffer.set(Some(val.payload.to_vec()));
MessageKind::Binary as u16
}
OpCode::Close => {
Expand Down
4 changes: 2 additions & 2 deletions runtime/inspector_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,13 @@ async fn pump_websocket_messages(
'pump: loop {
tokio::select! {
Some(msg) = outbound_rx.next() => {
let msg = Frame::text(msg.content.into_bytes());
let msg = Frame::text(msg.content.into_bytes().into());
let _ = websocket.write_frame(msg).await;
}
Ok(msg) = websocket.read_frame() => {
match msg.opcode {
OpCode::Text => {
if let Ok(s) = String::from_utf8(msg.payload) {
if let Ok(s) = String::from_utf8(msg.payload.to_vec()) {
let _ = inbound_tx.unbounded_send(s);
}
}
Expand Down
12 changes: 7 additions & 5 deletions test_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,19 @@ async fn ping_websocket_handler(
let mut ws = fastwebsockets::FragmentCollector::new(ws);

for i in 0..9 {
ws.write_frame(Frame::new(true, OpCode::Ping, None, vec![]))
ws.write_frame(Frame::new(true, OpCode::Ping, None, vec![].into()))
.await
.unwrap();

let frame = ws.read_frame().await.unwrap();
assert_eq!(frame.opcode, OpCode::Pong);
assert!(frame.payload.is_empty());

ws.write_frame(Frame::text(format!("hello {}", i).as_bytes().to_vec()))
.await
.unwrap();
ws.write_frame(Frame::text(
format!("hello {}", i).as_bytes().to_vec().into(),
))
.await
.unwrap();

let frame = ws.read_frame().await.unwrap();
assert_eq!(frame.opcode, OpCode::Text);
Expand All @@ -419,7 +421,7 @@ async fn close_websocket_handler(
) -> Result<(), anyhow::Error> {
let mut ws = fastwebsockets::FragmentCollector::new(ws);

ws.write_frame(fastwebsockets::Frame::close_raw(vec![]))
ws.write_frame(fastwebsockets::Frame::close_raw(vec![].into()))
.await
.unwrap();

Expand Down

0 comments on commit 91dc6fa

Please sign in to comment.