Files
moli/moli-encoding/src/script.rs
T
ldm0 6d08975a69 refactor(fetch): preserve response header bytes end to end
Carry raw response header values through fetch, caches, redirects,
renderer delivery, workers, and protocol records. Convert explicitly
at WebIDL and protocol text boundaries while preserving opaque bytes.

Keep legacy CacheStorage and service worker metadata readable, and
advance the HTTP cache format for raw header values.

Validation:
- cargo fmt --all
- cargo clippy --workspace --all-targets --all-features -- -D warnings
- cargo nextest run --no-fail-fast
2026-09-23 02:02:54 +08:00

26 lines
801 B
Rust

use encoding_rs::Encoding;
use crate::{encoding_for_label, encoding_from_response_headers};
pub fn decode_utf8(bytes: &[u8]) -> String {
encoding_rs::UTF_8
.decode_with_bom_removal(bytes)
.0
.into_owned()
}
pub fn decode_classic_script_source(
bytes: &[u8],
headers: &[(String, Vec<u8>)],
script_charset: Option<&str>,
document_character_set: Option<&str>,
) -> String {
let encoding = Encoding::for_bom(bytes)
.map(|(encoding, _)| encoding)
.or_else(|| encoding_from_response_headers(headers))
.or_else(|| script_charset.and_then(encoding_for_label))
.or_else(|| document_character_set.and_then(encoding_for_label))
.unwrap_or(encoding_rs::UTF_8);
encoding.decode_with_bom_removal(bytes).0.into_owned()
}