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

Feat custom parser #352

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions .github/workflows/bencher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
BENCHER_API_TOKEN: ${{ secrets.BENCHER_API_TOKEN }}
BENCHER_CMD: cargo bench --all-features
BENCHER_PROJECT: socketioxide
RUSTFLAGS: --cfg=socketioxide_test

benchmark_pr:
if: github.event_name == 'workflow_dispatch' && github.event.pull_request.head.repo.full_name == github.repository
Expand Down Expand Up @@ -62,4 +61,3 @@ jobs:
BENCHER_API_TOKEN: ${{ secrets.BENCHER_API_TOKEN }}
BENCHER_CMD: cargo bench --all-features
BENCHER_PROJECT: socketioxide
RUSTFLAGS: --cfg=socketioxide_test
42 changes: 17 additions & 25 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt
- run: cargo fmt --all -- --check
env:
RUSTFLAGS: --cfg=socketioxide_test

- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt
- run: cargo fmt --all -- --check

test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand All @@ -39,8 +37,6 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- run: cargo test --tests --all-features --workspace
env:
RUSTFLAGS: --cfg=socketioxide_test
udeps:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -85,13 +81,11 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ steps.msrv.outputs.version }}
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ steps.msrv.outputs.version }}
components: rustfmt, clippy
toolchain: ${{ steps.msrv.outputs.version }}
components: rustfmt, clippy

- name: check crates
run: cargo check -p socketioxide -p engineioxide --all-features
env:
RUSTFLAGS: --cfg=socketioxide_test

feature_set:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -183,20 +177,18 @@ jobs:
run: cargo install clippy-sarif sarif-fmt || true

- name: Run rust-clippy
run:
cargo clippy
run: cargo clippy
--all-features
--tests
--message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
continue-on-error: true
env:
RUSTFLAGS: --cfg=socketioxide_test

- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: rust-clippy-results.sarif
wait-for-processing: true

engine_io:
runs-on: ubuntu-latest
needs: [test]
Expand Down Expand Up @@ -226,7 +218,7 @@ jobs:
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-release
- name: Install deps & run tests
- name: Install deps & run tests
run: |
cd engine.io-protocol/test-suite && npm install && cd ../..
cargo build -p engineioxide-e2e --bin engineioxide-e2e --features ${{ matrix.engineio-version }} --release
Expand All @@ -236,7 +228,7 @@ jobs:
run: cat server.txt
- name: Client output
if: always()
run: cat client.txt
run: cat client.txt

socket_io:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -266,7 +258,7 @@ jobs:
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-release
- name: Install deps & run tests
- name: Install deps & run tests
run: |
cd socket.io-protocol/test-suite && npm install && cd ../..
cargo build -p socketioxide-e2e --bin socketioxide-e2e --features ${{ matrix.socketio-version }} --release
Expand All @@ -276,4 +268,4 @@ jobs:
run: cat server.txt
- name: Client output
if: always()
run: cat client.txt
run: cat client.txt
9 changes: 2 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ today! As a contributor, here are the guidelines we would like you to follow:

## <a name="question"></a> Got a Question or Problem?

Please open a discussion on the [Q&A discussions](https://github.com/totodore/socketioxide/discussions) page.
Please open a discussion on the [Q&A discussions](https://github.com/totodore/socketioxide/discussions) page.
We want to keep GitHub Issues for bugs and feature requests. If you open an issue it will be moved to Discussions.

## <a name="issue"></a> Found a Bug?
Expand Down Expand Up @@ -139,11 +139,6 @@ You will need [rustc and cargo](www.rust-lang.org/tools/install).
```shell
git clone https://github.com/totodore/socketioxide
```
2. To test socketioxide don't forget to enable the flag `socketioxide_test` through the `RUSTFLAGS` environment variable:

```shell
export RUSTFLAGS="--cfg socketioxide_test"
```
2. Depending on what you want to change, clone the [socketio/engine.io-protocol](https://github.com/socketio/engine.io-protocol) repo or the [socketio/socket.io-protocol](https://github.com/socketio/socket.io-protocol) repo or both
```shell
git clone https://github.com/socketio/engine.io-protocol
Expand Down Expand Up @@ -261,4 +256,4 @@ The subject contains succinct description of the change:
### Body

Just as in the **subject**, use the imperative, present tense: "change" not "changed" nor "changes".
The body should include the motivation for the change and contrast this with previous behavior.
The body should include the motivation for the change and contrast this with previous behavior.
1 change: 1 addition & 0 deletions engineioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ hyper-util = { workspace = true, features = ["tokio", "client-legacy"] }
[features]
v3 = ["memchr", "unicode-segmentation", "itoa"]
tracing = ["dep:tracing"]
__test_harness = []

[[bench]]
name = "packet_encode"
Expand Down
3 changes: 2 additions & 1 deletion engineioxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub use crate::str::Str;
pub use service::{ProtocolVersion, TransportType};
pub use socket::{DisconnectReason, Socket};

#[cfg(any(test, socketioxide_test))]
#[doc(hidden)]
#[cfg(feature = "__test_harness")]
pub use packet::*;

pub mod config;
Expand Down
26 changes: 20 additions & 6 deletions engineioxide/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ pub struct Permit<'a> {
impl Permit<'_> {
/// Consume the permit and emit a message to the client.
#[inline]
pub fn emit(self, msg: String) {
self.inner.send(smallvec![Packet::Message(msg.into())]);
pub fn emit(self, msg: Str) {
self.inner.send(smallvec![Packet::Message(msg)]);
}
/// Consume the permit and emit a binary message to the client.
#[inline]
Expand All @@ -138,9 +138,21 @@ impl Permit<'_> {
/// Consume the permit and emit a message with multiple binary data to the client.
///
/// It can be used to ensure atomicity when sending a string packet with adjacent binary packets.
pub fn emit_many(self, msg: String, data: Vec<Bytes>) {
pub fn emit_many(self, msg: Str, data: Vec<Bytes>) {
let mut packets = SmallVec::with_capacity(data.len() + 1);
packets.push(Packet::Message(msg.into()));
packets.push(Packet::Message(msg));
for d in data {
packets.push(Packet::Binary(d));
}
self.inner.send(packets);
}

/// Consume the permit and emit a message with multiple binary data to the client.
///
/// It can be used to ensure atomicity when sending a string packet with adjacent binary packets.
pub fn emit_many_binary(self, bin: Bytes, data: Vec<Bytes>) {
let mut packets = SmallVec::with_capacity(data.len() + 1);
packets.push(Packet::Binary(bin));
for d in data {
packets.push(Packet::Binary(d));
}
Expand Down Expand Up @@ -469,7 +481,8 @@ impl<D: Default + Send + Sync + 'static> std::fmt::Debug for Socket<D> {
}
}

#[cfg(socketioxide_test)]
#[doc(hidden)]
#[cfg(feature = "__test_harness")]
impl<D> Drop for Socket<D>
where
D: Default + Send + Sync + 'static,
Expand All @@ -480,7 +493,8 @@ where
}
}

#[cfg(any(socketioxide_test, test))]
#[doc(hidden)]
#[cfg(feature = "__test_harness")]
impl<D> Socket<D>
where
D: Default + Send + Sync + 'static,
Expand Down
41 changes: 39 additions & 2 deletions engineioxide/src/str.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::{Borrow, Cow};

use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::borrow::{Borrow, Cow};

/// A custom [`Bytes`] wrapper to efficiently store string packets
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd)]
Expand Down Expand Up @@ -92,6 +92,43 @@ impl From<Str> for String {
unsafe { String::from_utf8_unchecked(vec) }
}
}
impl Serialize for Str {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for Str {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct StrVisitor;
impl<'de> serde::de::Visitor<'de> for StrVisitor {
type Value = Str;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a str")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Str::copy_from_slice(v))
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Str::from(v))
}
}
deserializer.deserialize_str(StrVisitor)
}
}

impl std::cmp::PartialEq<&str> for Str {
fn eq(&self, other: &&str) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions socketioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tower.workspace = true
http.workspace = true
http-body.workspace = true
thiserror.workspace = true
smallvec.workspace = true
itoa.workspace = true
hyper.workspace = true
matchit.workspace = true
Expand All @@ -41,6 +42,7 @@ v4 = ["engineioxide/v3"]
tracing = ["dep:tracing", "engineioxide/tracing"]
extensions = []
state = ["dep:state"]
__test_harness = ["engineioxide/__test_harness"]

[dev-dependencies]
engineioxide = { path = "../engineioxide", features = ["v3", "tracing"] }
Expand Down
Loading
Loading