ws close code and reason, ws headers

This commit is contained in:
Toshit Chawda 2024-07-02 20:54:21 -07:00
parent cef912f86b
commit 0aec6e563c
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
6 changed files with 23 additions and 10 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "epoxy-client"
version = "2.0.0"
version = "2.0.1"
edition = "2021"
[lib]

View file

@ -2,7 +2,7 @@
set -euo pipefail
shopt -s inherit_errexit
mkdir out/
mkdir out/ || true
rm -r pkg/ || true
mkdir pkg/

View file

@ -182,8 +182,9 @@ onmessage = async (msg) => {
);
let ws = await epoxy_client.connect_websocket(
handlers,
"wss://echo.websocket.events",
"ws://localhost:5000",
[],
{ "x-header": "abc" },
);
while (true) {
log("sending `data`");

View file

@ -1,6 +1,6 @@
{
"name": "@mercuryworkshop/epoxy-tls",
"version": "2.0.0-3",
"version": "2.0.1-1",
"description": "A wasm library for using raw encrypted tls/ssl/https/websocket streams on the browser",
"scripts": {
"build": "./build.sh"

View file

@ -245,8 +245,9 @@ impl EpoxyClient {
handlers: EpoxyHandlers,
url: String,
protocols: Vec<String>,
headers: JsValue,
) -> Result<EpoxyWebSocket, EpoxyError> {
EpoxyWebSocket::connect(self, handlers, url, protocols).await
EpoxyWebSocket::connect(self, handlers, url, protocols, headers).await
}
pub async fn connect_tcp(

View file

@ -3,7 +3,7 @@ use std::{str::from_utf8, sync::Arc};
use base64::{prelude::BASE64_STANDARD, Engine};
use bytes::Bytes;
use fastwebsockets::{
CloseCode, FragmentCollectorRead, Frame, OpCode, Payload, Role, WebSocket, WebSocketWrite,
FragmentCollectorRead, Frame, OpCode, Payload, Role, WebSocket, WebSocketWrite,
};
use futures_util::lock::Mutex;
use getrandom::getrandom;
@ -17,12 +17,12 @@ use hyper::{
body::Incoming,
upgrade::{self, Upgraded},
};
use js_sys::{ArrayBuffer, Function, Uint8Array};
use js_sys::{ArrayBuffer, Function, Object, Uint8Array};
use tokio::io::WriteHalf;
use wasm_bindgen::{prelude::*, JsError, JsValue};
use wasm_bindgen_futures::spawn_local;
use crate::{tokioio::TokioIo, EpoxyClient, EpoxyError, EpoxyHandlers, HttpBody};
use crate::{tokioio::TokioIo, utils::entries_of_object, EpoxyClient, EpoxyError, EpoxyHandlers, HttpBody};
#[wasm_bindgen]
pub struct EpoxyWebSocket {
@ -37,6 +37,7 @@ impl EpoxyWebSocket {
handlers: EpoxyHandlers,
url: String,
protocols: Vec<String>,
headers: JsValue,
) -> Result<Self, EpoxyError> {
let EpoxyHandlers {
onopen,
@ -66,6 +67,16 @@ impl EpoxyWebSocket {
request = request.header(SEC_WEBSOCKET_PROTOCOL, protocols.join(","));
}
if web_sys::Headers::instanceof(&headers) && let Ok(entries) = Object::from_entries(&headers) {
for header in entries_of_object(&entries) {
request = request.header(&header[0], &header[1]);
}
} else if headers.is_truthy() {
for header in entries_of_object(&headers.into()) {
request = request.header(&header[0], &header[1]);
}
}
let request = request.body(HttpBody::new(Bytes::new()))?;
let mut response = client.client.request(request).await?;
@ -170,12 +181,12 @@ impl EpoxyWebSocket {
}
}
pub async fn close(&self) -> Result<(), EpoxyError> {
pub async fn close(&self, code: u16, reason: String) -> Result<(), EpoxyError> {
let ret = self
.tx
.lock()
.await
.write_frame(Frame::close(CloseCode::Normal.into(), b""))
.write_frame(Frame::close(code, reason.as_bytes()))
.await;
match ret {
Ok(ok) => Ok(ok),