Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Nov 16, 2024
1 parent 3c74282 commit ec10ebd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/get-runner-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
jobs:
main:
name: Get Runner Labels
runs-on: [self-hosted, Linux, ci]
runs-on: ubuntu-latest
outputs:
LINUX_RUNNER_LABELS: ${{ steps.run.outputs.LINUX_RUNNER_LABELS }}
MACOS_RUNNER_LABELS: ${{ steps.run.outputs.MACOS_RUNNER_LABELS }}
Expand Down
21 changes: 20 additions & 1 deletion crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ impl AssetParserAndGenerator {
}
}

fn decode_data_uri_content(encoding: &str, content: &str, source: &BoxSource) -> Vec<u8> {
if encoding == "base64"
&& let Some(cleaned) = rspack_base64::clean_base64(content)
{
return rspack_base64::decode_to_vec(cleaned.as_bytes())
.unwrap_or_else(|_| source.buffer().to_vec());
}

match urlencoding::decode(content) {
Ok(decoded_content) => decoded_content.as_bytes().to_vec(),
Err(_) => content.as_bytes().to_vec(),
}
}

fn hash_for_source(
&self,
source: &BoxSource,
Expand Down Expand Up @@ -184,7 +198,12 @@ impl AssetParserAndGenerator {
encoding: &str,
source: &BoxSource,
) -> Result<String> {
if let Some(encoded_content) = &resource_data.encoded_content {
if let Some(encoded_content) = &resource_data.encoded_content
&& let Some(resource_encoding) = &resource_data.encoding
&& resource_encoding == encoding
&& AssetParserAndGenerator::decode_data_uri_content(encoding, encoded_content, source)
.eq(&source.buffer().to_vec())
{
return Ok(encoded_content.to_owned());
}
if encoding.is_empty() {
Expand Down
12 changes: 5 additions & 7 deletions crates/rspack_plugin_schemes/src/data_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ async fn read_resource(&self, resource_data: &ResourceData) -> Result<Option<Con
Err(_) => Ok(Some(Content::String(resource_data.resource.to_string()))),
};
}
if !body.is_ascii() {
return Ok(Some(Content::Buffer(
urlencoding::decode_binary(body.as_bytes()).into_owned(),
)));
} else {
return Ok(Some(Content::Buffer(body.bytes().collect())));
}

return match urlencoding::decode(body) {
Ok(decoded_content) => Ok(Some(Content::Buffer(decoded_content.as_bytes().to_vec()))),
Err(_) => Ok(Some(Content::Buffer(body.as_bytes().to_vec()))),
};
}
Ok(None)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ it("should generate various data-url types", () => {
expect(urlSvg2.href).toContain(
"data:image/svg+xml;p=1;q=2,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke=\"%23343a40\" stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e"
);
// TODO: fixme
// expect(helloWorld.href).toContain("data:text/plain,Hello%2C%20World%21");
// expect(helloWorldBase64.href).toContain(
// "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
// );
expect(helloWorld.href).toContain("data:text/plain,Hello%2C%20World%21");
expect(helloWorldBase64.href).toContain(
"data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
);
});

This file was deleted.

0 comments on commit ec10ebd

Please sign in to comment.