Skip to content

Commit

Permalink
feat(engineio-core): create core for common types
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore committed Mar 1, 2025
1 parent 640f08e commit c92c9d0
Show file tree
Hide file tree
Showing 31 changed files with 130 additions and 58 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

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

19 changes: 19 additions & 0 deletions crates/engineioxide-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "engineioxide-core"
description = "Engineioxide core types and utilities"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true
keywords.workspace = true
categories.workspace = true
license.workspace = true
readme = "README.md"

[dependencies]
rand = "0.9"
base64 = "0.22"
serde.workspace = true
bytes.workspace = true
5 changes: 5 additions & 0 deletions crates/engineioxide-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Engineioxide-core

Common types and utilities for engineioxide and socketioxide-core.
* `Sid` a unique identifier for a socket connection.
* `Str` a wrapper around `Bytes` that ensures UTF-8 encoding.
37 changes: 37 additions & 0 deletions crates/engineioxide-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![warn(
clippy::all,
clippy::todo,
clippy::empty_enum,
clippy::mem_forget,
clippy::unused_self,
clippy::filter_map_next,
clippy::needless_continue,
clippy::needless_borrow,
clippy::match_wildcard_for_single_variants,
clippy::if_let_mutex,
clippy::await_holding_lock,
clippy::match_on_vec_items,
clippy::imprecise_flops,
clippy::suboptimal_flops,
clippy::lossy_float_literal,
clippy::rest_pat_in_fully_bound_structs,
clippy::fn_params_excessive_bools,
clippy::exit,
clippy::inefficient_to_string,
clippy::linkedlist,
clippy::macro_use_imports,
clippy::option_option,
clippy::verbose_file_reads,
clippy::unnested_or_patterns,
rust_2018_idioms,
future_incompatible,
nonstandard_style,
missing_docs
)]
#![doc = include_str!("../README.md")]

mod sid;
mod str;

pub use sid::Sid;
pub use str::Str;
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//! [`Socket`](crate::Socket) id type and generator
//!
//! It is stored as a 128-bit id and it represent a base64 16 char string
use std::{
fmt::{Debug, Display, Formatter},
str::FromStr,
};
use std::{fmt, str::FromStr};

use base64::Engine;
use rand::Rng;
Expand All @@ -29,16 +23,22 @@ impl Sid {
}

/// Error type for [`Sid::from_str`]
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub enum SidDecodeError {
/// Invalid base64 string
#[error("Invalid url base64 string")]
InvalidBase64String,

/// Invalid length
#[error("Invalid sid length")]
InvalidLength,
}
impl fmt::Display for SidDecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SidDecodeError::InvalidBase64String => write!(f, "Invalid url base64 string"),
SidDecodeError::InvalidLength => write!(f, "Invalid sid length"),
}
}
}
impl std::error::Error for SidDecodeError {}

impl FromStr for Sid {
type Err = SidDecodeError;
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Default for Sid {
let mut random = [0u8; 12]; // 12 bytes = 16 chars base64
let mut id = [0u8; 16];

rand::thread_rng().fill(&mut random);
rand::rng().fill(&mut random);

base64::prelude::BASE64_URL_SAFE_NO_PAD
.encode_slice(random, &mut id)
Expand All @@ -80,8 +80,8 @@ impl Default for Sid {
}
}

impl Display for Sid {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for Sid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
Expand All @@ -95,7 +95,7 @@ struct SidVisitor;
impl serde::de::Visitor<'_> for SidVisitor {
type Value = Sid;

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

Expand All @@ -109,8 +109,8 @@ impl<'de> serde::Deserialize<'de> for Sid {
}
}

impl Debug for Sid {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for Sid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion crates/engineioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ features = ["v3"]
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
engineioxide-core = { path = "../engineioxide-core", version = "0.1.0" }
bytes.workspace = true
futures-core.workspace = true
futures-util.workspace = true
Expand All @@ -38,7 +39,6 @@ smallvec.workspace = true
hyper-util = { workspace = true, features = ["tokio"] }

base64 = "0.22"
rand = "0.9"

# Tracing
tracing = { workspace = true, optional = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/engineioxide/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::{
sync::{Arc, RwLock},
};

use engineioxide_core::Sid;
use http::request::Parts;

use crate::{
config::EngineIoConfig,
handler::EngineIoHandler,
service::TransportType,
service::{ProtocolVersion, TransportType},
socket::{DisconnectReason, Socket},
};
use crate::{service::ProtocolVersion, sid::Sid};

type SocketMap<T> = RwLock<HashMap<Sid, Arc<T>>>;

Expand Down Expand Up @@ -95,8 +95,8 @@ impl<H: EngineIoHandler> EngineIo<H> {

#[cfg(test)]
mod tests {
use crate::str::Str;
use bytes::Bytes;
use engineioxide_core::Str;
use http::Request;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/engineioxide/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio_tungstenite::tungstenite;

use crate::body::ResponseBody;
use crate::packet::Packet;
use crate::sid::Sid;
use engineioxide_core::Sid;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down
2 changes: 1 addition & 1 deletion crates/engineioxide/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
use std::sync::Arc;

use bytes::Bytes;
use engineioxide_core::Str;

use crate::socket::{DisconnectReason, Socket};
use crate::str::Str;

/// The [`EngineIoHandler`] trait can be implemented on any struct to handle socket events
///
Expand Down
4 changes: 1 addition & 3 deletions crates/engineioxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)]
#![doc = include_str!("../Readme.md")]

pub use crate::str::Str;
pub use engineioxide_core::Str;
pub use service::{ProtocolVersion, TransportType};
pub use socket::{DisconnectReason, Socket};

Expand All @@ -43,13 +43,11 @@ pub mod config;
pub mod handler;
pub mod layer;
pub mod service;
pub mod sid;
pub mod socket;

mod body;
mod engine;
mod errors;
mod packet;
mod peekable;
mod str;
mod transport;
3 changes: 1 addition & 2 deletions crates/engineioxide/src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use base64::{engine::general_purpose, Engine};
use bytes::Bytes;
use engineioxide_core::{Sid, Str};
use serde::Serialize;

use crate::config::EngineIoConfig;
use crate::errors::Error;
use crate::sid::Sid;
use crate::str::Str;
use crate::TransportType;

/// A Packet type to use when receiving and sending data from the client
Expand Down
2 changes: 1 addition & 1 deletion crates/engineioxide/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
&self,
conn: S,
protocol: ProtocolVersion,
sid: Option<crate::sid::Sid>,
sid: Option<engineioxide_core::Sid>,
req_data: http::request::Parts,
) -> impl std::future::Future<Output = Result<(), crate::errors::Error>>
where
Expand Down
3 changes: 2 additions & 1 deletion crates/engineioxide/src/service/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::{str::FromStr, sync::Arc};
use futures_core::Future;
use http::{Method, Request, Response};

use engineioxide_core::Sid;

use crate::{
body::ResponseBody,
config::EngineIoConfig,
engine::EngineIo,
handler::EngineIoHandler,
service::futures::ResponseFuture,
sid::Sid,
transport::{polling, ws},
};

Expand Down
14 changes: 9 additions & 5 deletions crates/engineioxide/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ use std::{
time::Duration,
};

use crate::{
config::EngineIoConfig,
errors::Error,
packet::Packet,
peekable::PeekableReceiver,
service::{ProtocolVersion, TransportType},
};
use bytes::Bytes;
use engineioxide_core::Str;
use http::request::Parts;
use smallvec::{smallvec, SmallVec};
use tokio::{
Expand All @@ -76,11 +84,7 @@ use tokio::{
};
use tokio_tungstenite::tungstenite;

use crate::{
config::EngineIoConfig, errors::Error, packet::Packet, peekable::PeekableReceiver,
service::ProtocolVersion, Str,
};
use crate::{service::TransportType, sid::Sid};
pub use engineioxide_core::Sid;

/// A [`DisconnectReason`] represents the reason why a [`Socket`] was closed.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
3 changes: 2 additions & 1 deletion crates/engineioxide/src/transport/polling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use http::{Request, Response, StatusCode};
use http_body::Body;
use http_body_util::Full;

use engineioxide_core::Sid;

use crate::{
body::ResponseBody,
engine::EngineIo,
errors::Error,
handler::EngineIoHandler,
packet::{OpenPacket, Packet},
service::{ProtocolVersion, TransportType},
sid::Sid,
transport::polling::payload::Payload,
DisconnectReason,
};
Expand Down
3 changes: 2 additions & 1 deletion crates/engineioxide/src/transport/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use tokio_tungstenite::{
WebSocketStream,
};

use engineioxide_core::Sid;

use crate::{
body::ResponseBody,
config::EngineIoConfig,
Expand All @@ -33,7 +35,6 @@ use crate::{
packet::{OpenPacket, Packet},
service::ProtocolVersion,
service::TransportType,
sid::Sid,
DisconnectReason, Socket,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/engineioxide/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{

use bytes::Bytes;
use engineioxide::{
config::EngineIoConfig, handler::EngineIoHandler, service::EngineIoService, sid::Sid,
config::EngineIoConfig, handler::EngineIoHandler, service::EngineIoService, socket::Sid,
ProtocolVersion,
};
use http::Request;
Expand Down
2 changes: 1 addition & 1 deletion crates/socketioxide-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme.workspace = true

[dependencies]
bytes.workspace = true
engineioxide = { version = "0.16", path = "../engineioxide" }
engineioxide-core = { version = "0.1", path = "../engineioxide-core" }
serde.workspace = true
thiserror.workspace = true
futures-core.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/socketioxide-core/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
time::Duration,
};

use engineioxide::{sid::Sid, Str};
use engineioxide_core::{Sid, Str};
use futures_core::{FusedStream, Stream};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use smallvec::SmallVec;
Expand Down
Loading

0 comments on commit c92c9d0

Please sign in to comment.