Revert "remove websocket support"

This reverts commit df33f81340.
This commit is contained in:
Toshit Chawda 2024-10-12 13:27:06 -07:00
parent 642b4a32cc
commit 142067961d
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
5 changed files with 302 additions and 115 deletions

View file

@ -18,7 +18,7 @@ use futures_rustls::{
use futures_util::{ready, AsyncRead, AsyncWrite, Future, Stream, StreamExt, TryStreamExt};
use http::{HeaderValue, Uri};
use hyper::{body::Body, rt::Executor};
use js_sys::{Array, ArrayBuffer, Function, JsString, Object, Uint8Array};
use js_sys::{Array, ArrayBuffer, JsString, Object, Uint8Array};
use pin_project_lite::pin_project;
use rustls_pki_types::{CertificateDer, ServerName, UnixTime};
use send_wrapper::SendWrapper;
@ -372,55 +372,6 @@ pub fn is_null_body(code: u16) -> bool {
}
#[wasm_bindgen(inline_js = r#"
class WebSocketStreamPonyfill {
url;
opened;
closed;
close;
constructor(url, options = {}) {
if (options.signal?.aborted) {
throw new DOMException('This operation was aborted', 'AbortError');
}
this.url = url;
const ws = new WebSocket(url, options.protocols ?? []);
ws.binaryType = "arraybuffer";
const closeWithInfo = ({ closeCode: code, reason } = {}) => ws.close(code, reason);
this.opened = new Promise((resolve, reject) => {
ws.onopen = () => {
resolve({
readable: new ReadableStream({
start(controller) {
ws.onmessage = ({ data }) => controller.enqueue(data);
ws.onerror = e => controller.error(e);
},
cancel: closeWithInfo,
}),
writable: new WritableStream({
write(chunk) { ws.send(chunk); },
abort() { ws.close(); },
close: closeWithInfo,
}),
protocol: ws.protocol,
extensions: ws.extensions,
});
ws.removeEventListener('error', reject);
};
ws.addEventListener('error', reject);
});
this.closed = new Promise((resolve, reject) => {
ws.onclose = ({ code, reason }) => {
resolve({ closeCode: code, reason });
ws.removeEventListener('error', reject);
};
ws.addEventListener('error', reject);
});
if (options.signal) {
options.signal.onabort = () => ws.close();
}
this.close = closeWithInfo;
}
}
export function object_get(obj, k) {
try {
return obj[k]
@ -457,16 +408,6 @@ export function from_entries(entries){
for(var i = 0; i < entries.length; i++) ret[entries[i][0]] = entries[i][1];
return ret;
}
async function websocket_connect(url, protocols) {
let wss = new (typeof WebSocketStream !== "undefined" ? WebSocketStream : WebSocketStreamPonyfill)(url, { protocols: protocols });
let {readable, writable} = await wss.opened;
return {read: readable, write: writable};
}
export function bind_ws_connect(url, protocols) {
return websocket_connect.bind(undefined, url, protocols);
}
"#)]
extern "C" {
pub fn object_get(obj: &Object, key: &str) -> JsValue;
@ -481,8 +422,6 @@ extern "C" {
#[wasm_bindgen(catch)]
pub fn from_entries(iterable: &JsValue) -> Result<Object, JsValue>;
pub fn bind_ws_connect(url: String, protocols: Vec<String>) -> Function;
}
pub async fn convert_body(val: JsValue) -> Result<(Uint8Array, Option<String>), JsValue> {