#Deserialize a Response in Rust

6 messages · Page 1 of 1 (latest)

rough plank
#

I'm trying to deserialize a response from a fetch request in deno within Rust. I'm using serde_v8 to deserialize the response. I'm trying to deserialize the body field into a StringOrBuffer type, but have tried other types as well

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
    pub body: serde_v8::StringOrBuffer, // => `body` is a ReadableStream in the JS object
    pub headers: Map<String, Value>,
    pub status: u16,
    pub status_text: ByteString,
    pub url: ByteString,
    pub redirected: bool,
    pub ok: bool,
}

// ....

let resolved = js_runtime.resolve_value(promise).await?;
let scope = &mut js_runtime.handle_scope();
let resolved = v8::Local::<v8::Value>::new(scope, resolved);
//  this throws an error as long as `body` is present in the Response struct. Works when `body` is defined as `serde_v8::Value` or not present at all.
let resolved = deno_core::serde_v8::from_v8::<Response>(scope, resolved);

My goal is, to access the body within Rust and use it for further steps.

tawdry imp
#

StringOrBuffer accepts a JS object that is either a string, an ArrayBuffers, or an view on an ArrayBuffer (DataView, Uint8Array and the rest of TypedArrays)

#

A ReadableStream is a JS class which is none of those

#

at no point do ReadableStreams are passed into an op or returned from one

rough plank
#

Thanks for providing the background! Before I resort to wrap this entire thing in a JS script, one more question: would it be possible to get the resource id and somehow resolve the data directly from the resource table?