1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
//!
//! WebSocket client configuration options
//!
use super::{error::Error, result::Result, Handshake, Resolver};
use cfg_if::cfg_if;
use js_sys::Object;
use std::sync::Arc;
use wasm_bindgen::prelude::*;
use workflow_wasm::extensions::object::*;
///
/// Configuration struct for WebSocket client (native Tungstenite and NodeJs connections only)
///
#[derive(Clone)]
pub struct WebSocketConfig {
/// The target minimum size of the write buffer to reach before writing the data
/// to the underlying stream.
/// The default value is 128 KiB.
///
/// If set to `0` each message will be eagerly written to the underlying stream.
/// It is often more optimal to allow them to buffer a little, hence the default value.
///
/// Note: [`flush`](WebSocket::flush) will always fully write the buffer regardless.
pub write_buffer_size: usize,
/// The max size of the write buffer in bytes. Setting this can provide backpressure
/// in the case the write buffer is filling up due to write errors.
/// The default value is unlimited.
///
/// Note: The write buffer only builds up past [`write_buffer_size`](Self::write_buffer_size)
/// when writes to the underlying stream are failing. So the **write buffer can not
/// fill up if you are not observing write errors even if not flushing**.
///
/// Note: Should always be at least [`write_buffer_size + 1 message`](Self::write_buffer_size)
/// and probably a little more depending on error handling strategy.
pub max_write_buffer_size: usize,
/// The maximum size of a message. `None` means no size limit. The default value is 64 MiB
/// which should be reasonably big for all normal use-cases but small enough to prevent
/// memory eating by a malicious user.
pub max_message_size: Option<usize>,
/// The maximum size of a single message frame. `None` means no size limit. The limit is for
/// frame payload NOT including the frame header. The default value is 16 MiB which should
/// be reasonably big for all normal use-cases but small enough to prevent memory eating
/// by a malicious user.
pub max_frame_size: Option<usize>,
/// When set to `true`, the server will accept and handle unmasked frames
/// from the client. According to the RFC 6455, the server must close the
/// connection to the client in such cases, however it seems like there are
/// some popular libraries that are sending unmasked frames, ignoring the RFC.
/// By default this option is set to `false`, i.e. according to RFC 6455.
pub accept_unmasked_frames: bool,
/// The capacity of the channel used to queue incoming messages from WebSocket.
pub receiver_channel_cap: Option<usize>,
/// The capacity of the channel used to queue outgoing messages to WebSocket.
pub sender_channel_cap: Option<usize>,
/// Handshake handler for WebSocket connections. If supplied, it will be called
/// when the connection is established. The handshake handler can be used to
/// perform additional validation or setup before the connection is used.
pub handshake: Option<Arc<dyn Handshake>>,
/// Resolver for WebSocket connections. If supplied, it will be called to resolve
/// the URL before the connection is established. The resolver can be used as
/// an alternative to supplying the URL and will be invoked each time the
/// websocket needs to be connected or reconnected.
pub resolver: Option<Arc<dyn Resolver>>,
}
impl Default for WebSocketConfig {
fn default() -> Self {
WebSocketConfig {
write_buffer_size: 128 * 1024,
max_write_buffer_size: usize::MAX,
max_message_size: Some(64 << 20),
max_frame_size: Some(16 << 20),
accept_unmasked_frames: false,
receiver_channel_cap: None,
sender_channel_cap: None,
handshake: None,
resolver: None,
}
}
}
cfg_if! {
if #[cfg(feature = "wasm32-sdk")] {
#[wasm_bindgen(typescript_custom_section)]
const TS_WEBSOCKET_CONFIG: &'static str = r#"
/**
* `WebSocketConfig` is used to configure the `WebSocket`.
*
* @category WebSocket
*/
export interface IWebSocketConfig {
/** Maximum size of the WebSocket message. */
maxMessageSize: number,
/** Maximum size of the WebSocket frame. */
maxFrameSize: number,
}
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = js_sys::Object, typescript_type = "IWebSocketConfig | undefined")]
pub type IWebSocketConfig;
}
impl TryFrom<IWebSocketConfig> for WebSocketConfig {
type Error = Error;
fn try_from(args: IWebSocketConfig) -> Result<Self> {
let config = if let Some(args) = args.dyn_ref::<Object>() {
let mut config = WebSocketConfig::default();
if let Some(max_frame_size) = args.get_value("maxFrameSize")?.as_f64() {
config.max_frame_size = Some(max_frame_size as usize);
}
if let Some(max_message_size) = args.get_value("maxMessageSize")?.as_f64() {
config.max_message_size = Some(max_message_size as usize);
}
config
} else {
Default::default()
};
Ok(config)
}
}
}
}
pub(crate) struct WebSocketNodeJsConfig {
pub protocols: JsValue,
pub origin: JsValue,
pub headers: JsValue,
pub request_options: JsValue,
pub client_config: JsValue,
}
impl Default for WebSocketNodeJsConfig {
fn default() -> Self {
Self {
protocols: JsValue::UNDEFINED,
origin: JsValue::UNDEFINED,
headers: JsValue::UNDEFINED,
request_options: JsValue::UNDEFINED,
client_config: JsValue::UNDEFINED,
}
}
}
impl TryFrom<&WebSocketConfig> for WebSocketNodeJsConfig {
type Error = Error;
fn try_from(config: &WebSocketConfig) -> Result<Self> {
let client_config = Object::new();
if let Some(max_frame_size) = config.max_frame_size {
client_config.set("maxReceivedFrameSize", &JsValue::from(max_frame_size))?;
}
if let Some(max_message_size) = config.max_message_size {
client_config.set("maxReceivedMessageSize", &JsValue::from(max_message_size))?;
}
let nodejs_config = WebSocketNodeJsConfig {
protocols: JsValue::UNDEFINED,
origin: JsValue::UNDEFINED,
headers: JsValue::UNDEFINED,
request_options: JsValue::UNDEFINED,
client_config: client_config.into(),
};
Ok(nodejs_config)
}
}