Skip to content

Commit 649ce34

Browse files
authored
Update tungstenite to 0.26 (#2539)
1 parent adf4255 commit 649ce34

File tree

6 files changed

+162
-57
lines changed

6 files changed

+162
-57
lines changed

Cargo.lock

Lines changed: 107 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ thin-vec = "0.2.13"
249249
thiserror = "1.0.37"
250250
tokio = { version = "1.37", features = ["full"] }
251251
tokio-postgres = { version = "0.7.8", features = ["with-chrono-0_4"] }
252-
tokio-tungstenite = { version = "0.21", features = ["native-tls"] }
252+
tokio-tungstenite = { version = "0.26.2", features = ["native-tls"] }
253253
tokio-util = { version = "0.7.4", features = ["time"] }
254254
toml = "0.8"
255255
toml_edit = "0.22.22"

crates/client-api/src/routes/subscribe.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use axum::extract::{Path, Query, State};
77
use axum::response::IntoResponse;
88
use axum::Extension;
99
use axum_extra::TypedHeader;
10+
use bytes::Bytes;
11+
use bytestring::ByteString;
1012
use futures::future::MaybeDone;
1113
use futures::{Future, FutureExt, SinkExt, StreamExt};
1214
use http::{HeaderValue, StatusCode};
@@ -22,6 +24,7 @@ use spacetimedb_client_api_messages::websocket::{self as ws_api, Compression};
2224
use spacetimedb_lib::connection_id::{ConnectionId, ConnectionIdForUrl};
2325
use std::time::Instant;
2426
use tokio::sync::mpsc;
27+
use tokio_tungstenite::tungstenite::Utf8Bytes;
2528

2629
use crate::auth::SpacetimeAuth;
2730
use crate::util::websocket::{
@@ -122,12 +125,10 @@ where
122125
name: ctx.client_actor_index().next_client_name(),
123126
};
124127

125-
let ws_config = WebSocketConfig {
126-
max_message_size: Some(0x2000000),
127-
max_frame_size: None,
128-
accept_unmasked_frames: false,
129-
..Default::default()
130-
};
128+
let ws_config = WebSocketConfig::default()
129+
.max_message_size(Some(0x2000000))
130+
.max_frame_size(None)
131+
.accept_unmasked_frames(false);
131132

132133
tokio::spawn(async move {
133134
let ws = match ws_upgrade.upgrade(ws_config).await {
@@ -340,7 +341,7 @@ async fn ws_client_actor_inner(
340341
if mem::take(&mut got_pong) {
341342
// Send a ping message while continuing to poll the `handle_queue`,
342343
// to avoid deadlocks or delays due to enqueued futures holding resources.
343-
if let Err(e) = also_poll(ws.send(WsMessage::Ping(Vec::new())), make_progress(&mut current_message)).await {
344+
if let Err(e) = also_poll(ws.send(WsMessage::Ping(Bytes::new())), make_progress(&mut current_message)).await {
344345
log::warn!("error sending ping: {e:#}");
345346
}
346347
continue;
@@ -416,14 +417,14 @@ async fn ws_client_actor_inner(
416417

417418
enum ClientMessage {
418419
Message(DataMessage),
419-
Ping(Vec<u8>),
420-
Pong(Vec<u8>),
421-
Close(Option<CloseFrame<'static>>),
420+
Ping(Bytes),
421+
Pong(Bytes),
422+
Close(Option<CloseFrame>),
422423
}
423424
impl ClientMessage {
424425
fn from_message(msg: WsMessage) -> Self {
425426
match msg {
426-
WsMessage::Text(s) => Self::Message(DataMessage::Text(s)),
427+
WsMessage::Text(s) => Self::Message(DataMessage::Text(utf8bytes_to_bytestring(s))),
427428
WsMessage::Binary(b) => Self::Message(DataMessage::Binary(b)),
428429
WsMessage::Ping(b) => Self::Ping(b),
429430
WsMessage::Pong(b) => Self::Pong(b),
@@ -436,7 +437,16 @@ impl ClientMessage {
436437

437438
fn datamsg_to_wsmsg(msg: DataMessage) -> WsMessage {
438439
match msg {
439-
DataMessage::Text(text) => WsMessage::Text(text),
440+
DataMessage::Text(text) => WsMessage::Text(bytestring_to_utf8bytes(text)),
440441
DataMessage::Binary(bin) => WsMessage::Binary(bin),
441442
}
442443
}
444+
445+
fn utf8bytes_to_bytestring(s: Utf8Bytes) -> ByteString {
446+
// SAFETY: `Utf8Bytes` and `ByteString` have the same invariant of UTF-8 validity
447+
unsafe { ByteString::from_bytes_unchecked(Bytes::from(s)) }
448+
}
449+
fn bytestring_to_utf8bytes(s: ByteString) -> Utf8Bytes {
450+
// SAFETY: `Utf8Bytes` and `ByteString` have the same invariant of UTF-8 validity
451+
unsafe { Utf8Bytes::from_bytes_unchecked(s.into_bytes()) }
452+
}

0 commit comments

Comments
 (0)