Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d75e77c

Browse files
authored
fix: perform percent encoding to reserved chars in dataurl. (#1729)
resolves #1406. Some codes are migrated from @ikkz. Actually, he is also a contributor to this PR. Handle reserved chars in [rfc](https://datatracker.ietf.org/doc/html/rfc3986#section-2.2). cc @IWANABETHATGUY. Enabled more tests from `esbuild` related to `dataurl`.
1 parent bf1ef6e commit d75e77c

35 files changed

Lines changed: 261 additions & 39 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ tokio = { version = "1.38.0", default-features = false }
137137
tracing = "0.1.40"
138138
tracing-chrome = "0.7.2"
139139
tracing-subscriber = { version = "0.3.18", default-features = false }
140-
urlencoding = "2.1.3"
141140
vfs = "0.12.0"
142141
xxhash-rust = "0.8.10"
143142
# oxc crates share the same version

crates/rolldown/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ dunce = { workspace = true }
2020
futures = { workspace = true }
2121
indexmap = { workspace = true }
2222
itertools = { workspace = true }
23-
mime = { workspace = true }
2423
once_cell = { workspace = true }
2524
oxc = { workspace = true }
2625
regex = { workspace = true }
@@ -40,7 +39,6 @@ sugar_path = { workspace = true }
4039
tokio = { workspace = true, features = ["rt", "macros", "sync"] }
4140
tracing = { workspace = true }
4241
tracing-chrome = { workspace = true }
43-
urlencoding = { workspace = true }
4442
xxhash-rust = { workspace = true, features = ["xxh3"] }
4543

4644
[dev-dependencies]

crates/rolldown/src/utils/parse_to_ecma_ast.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,8 @@ pub fn parse_to_ecma_ast(
5656
ModuleType::Dataurl => {
5757
let data = source.try_into_bytes()?;
5858
let guessed_mime = guess_mime(path, &data)?;
59-
let is_plain_text = guessed_mime.type_() == mime::TEXT;
60-
let source = if is_plain_text {
61-
let text = String::from_utf8(data)?;
62-
if guessed_mime.type_() == mime::TEXT {
63-
format!("data:{guessed_mime};charset=utf-8,{text}")
64-
} else {
65-
let text = urlencoding::encode(&text);
66-
format!("data:{guessed_mime};charset=utf-8,{text}")
67-
}
68-
// TODO: should we support non-utf8 text?
69-
} else {
70-
let encoded = rolldown_utils::base64::to_standard_base64(&data);
71-
format!("data:{guessed_mime};base64,{encoded}")
72-
};
73-
(text_to_esm(&source)?, OxcParseType::Js)
59+
let dataurl = rolldown_utils::dataurl::encode_as_shortest_dataurl(&guessed_mime, &data);
60+
(text_to_esm(&dataurl)?, OxcParseType::Js)
7461
}
7562
ModuleType::Binary => {
7663
let source = source.try_into_bytes()?;

crates/rolldown/tests/esbuild/loader/.loader_data_url_base64_invalid_utf8/binary.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/rolldown/tests/esbuild/loader/.loader_data_url_base64_vs_percent_encoding/shouldUseBase64_1.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/rolldown/tests/esbuild/loader/.loader_data_url_base64_vs_percent_encoding/shouldUseBase64_2.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/rolldown/tests/esbuild/loader/.loader_data_url_base64_vs_percent_encoding/shouldUsePercent_1.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/rolldown/tests/esbuild/loader/.loader_data_url_base64_vs_percent_encoding/shouldUsePercent_2.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/rolldown/tests/esbuild/loader/.loader_data_url_base64_invalid_utf8/_config.json renamed to crates/rolldown/tests/esbuild/loader/loader_data_url_base64_invalid_utf8/_config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"name": "entry_js",
66
"import": "entry.js"
77
}
8-
]
8+
],
9+
"moduleTypes": {
10+
".txt": "dataurl"
11+
}
912
}
1013
}

0 commit comments

Comments
 (0)