mirror of
https://github.com/MercuryWorkshop/epoxy-tls.git
synced 2025-05-12 22:10:01 -04:00
workaround safari not supporting readable byte streams
This commit is contained in:
parent
7edb4ad76c
commit
1916a8e7c8
5 changed files with 173 additions and 91 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -511,7 +511,7 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "epoxy-client"
|
name = "epoxy-client"
|
||||||
version = "2.0.4"
|
version = "2.0.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-compression",
|
"async-compression",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "epoxy-client"
|
name = "epoxy-client"
|
||||||
version = "2.0.4"
|
version = "2.0.5"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@mercuryworkshop/epoxy-tls",
|
"name": "@mercuryworkshop/epoxy-tls",
|
||||||
"version": "2.0.4-2",
|
"version": "2.0.5-1",
|
||||||
"description": "A wasm library for using raw encrypted tls/ssl/https/websocket streams on the browser",
|
"description": "A wasm library for using raw encrypted tls/ssl/https/websocket streams on the browser",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "./build.sh"
|
"build": "./build.sh"
|
||||||
|
|
|
@ -22,8 +22,7 @@ use js_sys::{Array, Function, Object, Reflect};
|
||||||
use stream_provider::{StreamProvider, StreamProviderService};
|
use stream_provider::{StreamProvider, StreamProviderService};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use utils::{
|
use utils::{
|
||||||
convert_body, entries_of_object, is_null_body, is_redirect, object_get, object_set,
|
asyncread_to_readablestream_stream, convert_body, entries_of_object, is_null_body, is_redirect, object_get, object_set, IncomingBody, UriExt, WasmExecutor
|
||||||
IncomingBody, UriExt, WasmExecutor,
|
|
||||||
};
|
};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use wasm_streams::ReadableStream;
|
use wasm_streams::ReadableStream;
|
||||||
|
@ -529,14 +528,14 @@ impl EpoxyClient {
|
||||||
},
|
},
|
||||||
None => Either::Right(response_body),
|
None => Either::Right(response_body),
|
||||||
};
|
};
|
||||||
Some(ReadableStream::from_async_read(decompressed_body, 1024).into_raw())
|
Some(ReadableStream::from_stream(asyncread_to_readablestream_stream(decompressed_body)).into_raw())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
let response_stream = if !is_null_body(response.status().as_u16()) {
|
let response_stream = if !is_null_body(response.status().as_u16()) {
|
||||||
let response_body = IncomingBody::new(response.into_body()).into_async_read();
|
let response_body = IncomingBody::new(response.into_body()).into_async_read();
|
||||||
Some(ReadableStream::from_async_read(response_body, 1024).into_raw())
|
Some(ReadableStream::from_stream(asyncread_to_readablestream_stream(response_body)).into_raw())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,8 +3,8 @@ use std::{
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::{buf::UninitSlice, BufMut, Bytes, BytesMut};
|
||||||
use futures_util::{Future, Stream};
|
use futures_util::{ready, AsyncRead, Future, Stream, TryStreamExt};
|
||||||
use http::{HeaderValue, Uri};
|
use http::{HeaderValue, Uri};
|
||||||
use hyper::{body::Body, rt::Executor};
|
use hyper::{body::Body, rt::Executor};
|
||||||
use js_sys::{Array, ArrayBuffer, Object, Reflect, Uint8Array};
|
use js_sys::{Array, ArrayBuffer, Object, Reflect, Uint8Array};
|
||||||
|
@ -81,6 +81,81 @@ impl Stream for IncomingBody {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ReaderStream<R> {
|
||||||
|
#[pin]
|
||||||
|
reader: Option<R>,
|
||||||
|
buf: BytesMut,
|
||||||
|
capacity: usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: AsyncRead> ReaderStream<R> {
|
||||||
|
pub fn new(reader: R) -> Self {
|
||||||
|
ReaderStream {
|
||||||
|
reader: Some(reader),
|
||||||
|
buf: BytesMut::new(),
|
||||||
|
capacity: 4096,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn poll_read_buf<T: AsyncRead + ?Sized, B: BufMut>(
|
||||||
|
io: Pin<&mut T>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &mut B,
|
||||||
|
) -> Poll<std::io::Result<usize>> {
|
||||||
|
if !buf.has_remaining_mut() {
|
||||||
|
return Poll::Ready(Ok(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
let n = {
|
||||||
|
let dst = buf.chunk_mut();
|
||||||
|
|
||||||
|
let dst = unsafe { std::mem::transmute::<&mut UninitSlice, &mut [u8]>(dst) };
|
||||||
|
ready!(io.poll_read(cx, dst)?)
|
||||||
|
};
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
buf.advance_mut(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
Poll::Ready(Ok(n))
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: AsyncRead> Stream for ReaderStream<R> {
|
||||||
|
type Item = std::io::Result<Bytes>;
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
let mut this = self.as_mut().project();
|
||||||
|
|
||||||
|
let reader = match this.reader.as_pin_mut() {
|
||||||
|
Some(r) => r,
|
||||||
|
None => return Poll::Ready(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
if this.buf.capacity() == 0 {
|
||||||
|
this.buf.reserve(*this.capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
match poll_read_buf(reader, cx, &mut this.buf) {
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
|
Poll::Ready(Err(err)) => {
|
||||||
|
self.project().reader.set(None);
|
||||||
|
Poll::Ready(Some(Err(err)))
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(0)) => {
|
||||||
|
self.project().reader.set(None);
|
||||||
|
Poll::Ready(None)
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(_)) => {
|
||||||
|
let chunk = this.buf.split();
|
||||||
|
Poll::Ready(Some(Ok(chunk.freeze())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_redirect(code: u16) -> bool {
|
pub fn is_redirect(code: u16) -> bool {
|
||||||
[301, 302, 303, 307, 308].contains(&code)
|
[301, 302, 303, 307, 308].contains(&code)
|
||||||
}
|
}
|
||||||
|
@ -138,3 +213,11 @@ pub fn define_property_obj(value: JsValue, writable: bool) -> Result<Object, JsV
|
||||||
.collect::<Array>();
|
.collect::<Array>();
|
||||||
Object::from_entries(&entries)
|
Object::from_entries(&entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn asyncread_to_readablestream_stream<R: AsyncRead>(
|
||||||
|
read: R,
|
||||||
|
) -> impl Stream<Item = Result<JsValue, JsValue>> {
|
||||||
|
ReaderStream::new(read)
|
||||||
|
.map_ok(|x| Uint8Array::from(x.as_ref()).into())
|
||||||
|
.map_err(|x| EpoxyError::from(x).into())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue