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(engineio/packet): array expand for packet event only when not empty #134

Merged
merged 4 commits into from
Nov 4, 2023
Merged
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
13 changes: 10 additions & 3 deletions socketioxide/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,13 @@ impl<'a> TryInto<String> for Packet<'a> {
// pre-serializing allows to preallocate the buffer
let data = match &mut self.inner {
Event(e, data, _) | BinaryEvent(e, BinaryPacket { data, .. }, _) => {
// Expand the packet if it is an array -> ["event", ...data]
// Expand the packet if it is an array with data -> ["event", ...data]
let packet = match data {
Value::Array(ref mut v) => {
Value::Array(ref mut v) if !v.is_empty() => {
v.insert(0, Value::String(e.to_string()));
serde_json::to_string(&v)
}
Value::Array(_) => serde_json::to_string::<(_, [(); 0])>(&(e, [])),
_ => serde_json::to_string(&(e, data)),
}?;
Some(packet)
Expand Down Expand Up @@ -610,6 +611,12 @@ mod test {

assert_eq!(packet, payload);

// Encode empty data
let payload = format!("2{}", json!(["event", []]));
let packet: String = Packet::event("/", "event", json!([])).try_into().unwrap();

assert_eq!(packet, payload);

// Encode with ack ID
let payload = format!("21{}", json!(["event", { "data": "value™" }]));
let mut packet = Packet::event("/", "event", json!({ "data": "value™" }));
Expand All @@ -620,7 +627,7 @@ mod test {

// Encode with NS
let payload = format!("2/admin™,{}", json!(["event", { "data": "value™" }]));
let packet: String = Packet::event("/admin™", "event", json!([{"data": "value™"}]))
let packet: String = Packet::event("/admin™", "event", json!({"data": "value™"}))
.try_into()
.unwrap();

Expand Down