2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
use deno_core::ModuleCode;
|
2020-08-03 17:39:48 -04:00
|
|
|
use encoding_rs::*;
|
2022-11-25 18:38:08 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::io::Error;
|
|
|
|
use std::io::ErrorKind;
|
2020-08-03 17:39:48 -04:00
|
|
|
|
2021-08-16 03:28:29 -04:00
|
|
|
pub const BOM_CHAR: char = '\u{FEFF}';
|
|
|
|
|
2020-08-03 17:39:48 -04:00
|
|
|
/// Attempts to detect the character encoding of the provided bytes.
|
|
|
|
///
|
|
|
|
/// Supports UTF-8, UTF-16 Little Endian and UTF-16 Big Endian.
|
|
|
|
pub fn detect_charset(bytes: &'_ [u8]) -> &'static str {
|
|
|
|
const UTF16_LE_BOM: &[u8] = b"\xFF\xFE";
|
|
|
|
const UTF16_BE_BOM: &[u8] = b"\xFE\xFF";
|
|
|
|
|
|
|
|
if bytes.starts_with(UTF16_LE_BOM) {
|
|
|
|
"utf-16le"
|
|
|
|
} else if bytes.starts_with(UTF16_BE_BOM) {
|
|
|
|
"utf-16be"
|
|
|
|
} else {
|
|
|
|
// Assume everything else is utf-8
|
|
|
|
"utf-8"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to convert the provided bytes to a UTF-8 string.
|
|
|
|
///
|
|
|
|
/// Supports all encodings supported by the encoding_rs crate, which includes
|
|
|
|
/// all encodings specified in the WHATWG Encoding Standard, and only those
|
2021-09-05 10:22:45 -04:00
|
|
|
/// encodings (see: <https://encoding.spec.whatwg.org/>).
|
2020-08-03 17:39:48 -04:00
|
|
|
pub fn convert_to_utf8<'a>(
|
|
|
|
bytes: &'a [u8],
|
|
|
|
charset: &'_ str,
|
|
|
|
) -> Result<Cow<'a, str>, Error> {
|
|
|
|
match Encoding::for_label(charset.as_bytes()) {
|
|
|
|
Some(encoding) => encoding
|
|
|
|
.decode_without_bom_handling_and_without_replacement(bytes)
|
|
|
|
.ok_or_else(|| ErrorKind::InvalidData.into()),
|
|
|
|
None => Err(Error::new(
|
|
|
|
ErrorKind::InvalidInput,
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Unsupported charset: {charset}"),
|
2020-08-03 17:39:48 -04:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 03:28:29 -04:00
|
|
|
/// Strips the byte order mark from the provided text if it exists.
|
|
|
|
pub fn strip_bom(text: &str) -> &str {
|
|
|
|
if text.starts_with(BOM_CHAR) {
|
|
|
|
&text[BOM_CHAR.len_utf8()..]
|
|
|
|
} else {
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
static SOURCE_MAP_PREFIX: &[u8] =
|
|
|
|
b"//# sourceMappingURL=data:application/json;base64,";
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
pub fn source_map_from_code(code: &ModuleCode) -> Option<Vec<u8>> {
|
|
|
|
let bytes = code.as_bytes();
|
|
|
|
let last_line = bytes.rsplit(|u| *u == b'\n').next()?;
|
2022-07-19 11:58:18 -04:00
|
|
|
if last_line.starts_with(SOURCE_MAP_PREFIX) {
|
|
|
|
let input = last_line.split_at(SOURCE_MAP_PREFIX.len()).1;
|
|
|
|
let decoded_map = base64::decode(input)
|
|
|
|
.expect("Unable to decode source map from emitted file.");
|
|
|
|
Some(decoded_map)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
/// Truncate the source code before the source map.
|
|
|
|
pub fn code_without_source_map(mut code: ModuleCode) -> ModuleCode {
|
|
|
|
let bytes = code.as_bytes();
|
|
|
|
for i in (0..bytes.len()).rev() {
|
|
|
|
if bytes[i] == b'\n' {
|
|
|
|
if bytes[i + 1..].starts_with(SOURCE_MAP_PREFIX) {
|
|
|
|
code.truncate(i + 1);
|
|
|
|
}
|
|
|
|
return code;
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 18:33:12 -04:00
|
|
|
code
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:39:48 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn test_detection(test_data: &[u8], expected_charset: &str) {
|
|
|
|
let detected_charset = detect_charset(test_data);
|
|
|
|
assert_eq!(
|
|
|
|
expected_charset.to_lowercase(),
|
|
|
|
detected_charset.to_lowercase()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_detection_utf8_no_bom() {
|
|
|
|
let test_data = "Hello UTF-8 it is \u{23F0} for Deno!"
|
|
|
|
.to_owned()
|
|
|
|
.into_bytes();
|
|
|
|
test_detection(&test_data, "utf-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_detection_utf16_little_endian() {
|
|
|
|
let test_data = b"\xFF\xFEHello UTF-16LE".to_owned().to_vec();
|
|
|
|
test_detection(&test_data, "utf-16le");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_detection_utf16_big_endian() {
|
|
|
|
let test_data = b"\xFE\xFFHello UTF-16BE".to_owned().to_vec();
|
|
|
|
test_detection(&test_data, "utf-16be");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decoding_unsupported_charset() {
|
|
|
|
let test_data = Vec::new();
|
|
|
|
let result = convert_to_utf8(&test_data, "utf-32le");
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.expect_err("Err expected");
|
|
|
|
assert!(err.kind() == ErrorKind::InvalidInput);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decoding_invalid_utf8() {
|
|
|
|
let test_data = b"\xFE\xFE\xFF\xFF".to_vec();
|
|
|
|
let result = convert_to_utf8(&test_data, "utf-8");
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.expect_err("Err expected");
|
|
|
|
assert!(err.kind() == ErrorKind::InvalidData);
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_source_without_source_map() {
|
|
|
|
run_test("", "");
|
|
|
|
run_test("\n", "\n");
|
|
|
|
run_test("\r\n", "\r\n");
|
|
|
|
run_test("a", "a");
|
|
|
|
run_test("a\n", "a\n");
|
|
|
|
run_test("a\r\n", "a\r\n");
|
|
|
|
run_test("a\r\nb", "a\r\nb");
|
|
|
|
run_test("a\nb\n", "a\nb\n");
|
|
|
|
run_test("a\r\nb\r\n", "a\r\nb\r\n");
|
|
|
|
run_test(
|
|
|
|
"test\n//# sourceMappingURL=data:application/json;base64,test",
|
|
|
|
"test\n",
|
|
|
|
);
|
|
|
|
run_test(
|
|
|
|
"test\r\n//# sourceMappingURL=data:application/json;base64,test",
|
|
|
|
"test\r\n",
|
|
|
|
);
|
|
|
|
run_test(
|
|
|
|
"\n//# sourceMappingURL=data:application/json;base64,test",
|
|
|
|
"\n",
|
|
|
|
);
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
fn run_test(input: &'static str, output: &'static str) {
|
|
|
|
assert_eq!(
|
2023-04-04 08:46:31 -04:00
|
|
|
code_without_source_map(ModuleCode::from_static(input))
|
|
|
|
.as_str()
|
|
|
|
.to_owned(),
|
2023-03-21 18:33:12 -04:00
|
|
|
output
|
|
|
|
);
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|