mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-10-10 18:04:06 +02:00
perf(extensions): reuse owned UTF-8 buffers at text boundaries
This commit is contained in:
1 parent
a9a395b9f8
commit
e984c1ddba
3 files changed
+105
-7
No files matched your search
@@ -172,7 +172,7 @@ fn response_object<'js>(
|
||||
object.set("bytes", TypedArray::new_copy(ctx.clone(), response.body)?)?;
|
||||
} else {
|
||||
object.set("statusCode", response.status)?;
|
||||
object.set("body", decode_go_utf8(&response.body))?;
|
||||
object.set("body", crate::host::decode_go_utf8_owned(response.body))?;
|
||||
}
|
||||
Ok(object)
|
||||
}
|
||||
@@ -192,3 +192,47 @@ fn encode_buffer(bytes: &TypedArray<'_, u8>) -> String {
|
||||
let bytes = unsafe { bytes.as_bytes() }.unwrap_or_default();
|
||||
base64::engine::general_purpose::STANDARD.encode(bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn text_response_preserves_status_headers_unicode_and_invalid_bytes() {
|
||||
let runtime = rquickjs::Runtime::new().unwrap();
|
||||
let context = rquickjs::Context::full(&runtime).unwrap();
|
||||
context.with(|ctx| {
|
||||
for body in ["Music 音楽 🎵\0".as_bytes(), &b"a\xe2\x82b"[..]] {
|
||||
let response = response_object(
|
||||
&ctx,
|
||||
HttpResponse {
|
||||
status: 201,
|
||||
status_text: "Created".into(),
|
||||
url: "https://example.invalid/metadata".into(),
|
||||
headers: BTreeMap::from([(
|
||||
"Content-Type".into(),
|
||||
vec!["text/plain".into()],
|
||||
)]),
|
||||
body: body.to_vec(),
|
||||
},
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response.get::<_, String>("body").unwrap(),
|
||||
decode_go_utf8(body)
|
||||
);
|
||||
assert_eq!(response.get::<_, u16>("statusCode").unwrap(), 201);
|
||||
assert!(response.get::<_, bool>("ok").unwrap());
|
||||
assert_eq!(
|
||||
response
|
||||
.get::<_, Object>("headers")
|
||||
.unwrap()
|
||||
.get::<_, String>("Content-Type")
|
||||
.unwrap(),
|
||||
"text/plain",
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user