enhance headers type, error on invalid header

pull/542/head
Lucas Nogueira 2 years ago
parent 8ce517412e
commit 5f5a3dd3af
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

@ -6,7 +6,7 @@ export interface ConnectionConfig {
maxMessageSize?: number; maxMessageSize?: number;
maxFrameSize?: number; maxFrameSize?: number;
acceptUnmaskedFrames?: boolean; acceptUnmaskedFrames?: boolean;
headers?: Record<string, string>; headers?: HeadersInit;
} }
export interface MessageKind<T, D> { export interface MessageKind<T, D> {
@ -44,6 +44,10 @@ export default class WebSocket {
listeners.forEach((l) => l(message)); listeners.forEach((l) => l(message));
}; };
if (config?.headers) {
config.headers = Array.from(new Headers(config.headers).entries());
}
return await invoke<number>("plugin:websocket|connect", { return await invoke<number>("plugin:websocket|connect", {
url, url,
callbackFunction: transformCallback(handler), callbackFunction: transformCallback(handler),

@ -58,8 +58,7 @@ pub struct ConnectionConfig {
pub max_frame_size: Option<usize>, pub max_frame_size: Option<usize>,
#[serde(default)] #[serde(default)]
pub accept_unmasked_frames: bool, pub accept_unmasked_frames: bool,
#[serde(default)] pub headers: Option<Vec<(String, String)>>,
pub headers: HashMap<String, String>,
} }
impl From<ConnectionConfig> for WebSocketConfig { impl From<ConnectionConfig> for WebSocketConfig {
@ -105,14 +104,12 @@ async fn connect<R: Runtime>(
let id = rand::random(); let id = rand::random();
let mut request = url.into_client_request()?; let mut request = url.into_client_request()?;
if let Some(ref config) = config { if let Some(headers) = config.as_ref().and_then(|c| c.headers.as_ref()) {
let config_headers = config.headers.iter().map(|(k, v)| { for (k, v) in headers {
let header_name = HeaderName::from_str(k.as_str())?; let header_name = HeaderName::from_str(k.as_str())?;
let header_value = HeaderValue::from_str(v.as_str())?; let header_value = HeaderValue::from_str(v.as_str())?;
Ok((header_name, header_value)) request.headers_mut().insert(header_name, header_value);
}); }
request.headers_mut().extend(config_headers.filter_map(Result::ok));
} }
let (ws_stream, _) = connect_async_with_config(request, config.map(Into::into), false).await?; let (ws_stream, _) = connect_async_with_config(request, config.map(Into::into), false).await?;

@ -2,7 +2,7 @@
"compilerOptions": { "compilerOptions": {
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"esModuleInterop": true, "esModuleInterop": true,
"lib": ["ES2019", "ES2020.Promise", "ES2020.String", "DOM"], "lib": ["ES2019", "ES2020.Promise", "ES2020.String", "DOM", "DOM.Iterable"],
"module": "ESNext", "module": "ESNext",
"moduleResolution": "node", "moduleResolution": "node",
"noEmit": true, "noEmit": true,

Loading…
Cancel
Save