Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: RawDataUrl and encoding #19

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ jobs:

steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- uses: denoland/setup-deno@v1
- uses: dsherret/rust-toolchain-file@v1

- name: Cache
Expand All @@ -37,12 +36,18 @@ jobs:
run: cargo fmt --all -- --check

- name: Lint
run: cargo clippy --all-targets --all-features --release
run: cargo clippy --all-targets --all-features

- name: Build
run: cargo build --all-targets --all-features --release
run: cargo build --all-targets --all-features
- name: Build --no-default-features
run: cargo build --no-default-features
- name: Build (data_url)
run: cargo build --no-default-features --features data_url
- name: Build (decoding)
run: cargo build --no-default-features --features decoding
- name: Test
run: cargo test --all-targets --all-features --release
run: cargo test --all-targets --all-features

- name: Publish
if: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:

steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
token: ${{ secrets.DENOBOT_PAT }}

- uses: denoland/setup-deno@v1
- uses: denoland/setup-deno@v2
- uses: dtolnay/rust-toolchain@stable

- name: Tag and release
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.vscode
/target
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ all-features = true

[features]
default = ["module_specifier"]
module_specifier = ["data-url", "url"]
decoding = ["encoding_rs"]
data_url = ["dep:data-url", "url"]
module_specifier = ["dep:data-url", "url"]

[dependencies]
serde = { version = "1.0.130", features = ["derive", "rc"] }
data-url = { version = "0.3.0", optional = true }
encoding_rs = { version = "0.8.33", optional = true }
serde = { version = "1.0.130", features = ["derive", "rc"] }
url = { version = "2.3.1", optional = true }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018-2024 the Deno authors
Copyright (c) 2018-2025 the Deno authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
134 changes: 134 additions & 0 deletions src/data_url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2018-2025 the Deno authors. MIT license.

use url::Url;

use crate::MediaType;

pub fn get_mime_type_charset(mime_type: &str) -> Option<&str> {
mime_type
.split(';')
.skip(1)
.map(str::trim)
.find_map(|s| s.strip_prefix("charset="))
}

#[derive(Debug, Clone)]
pub struct RawDataUrl {
pub mime_type: String,
pub bytes: Vec<u8>,
}

impl RawDataUrl {
pub fn parse(specifier: &Url) -> Result<Self, std::io::Error> {
use std::io::Error;
use std::io::ErrorKind;

fn unable_to_decode() -> Error {
Error::new(ErrorKind::InvalidData, "Unable to decode data url.")
}

let url = data_url::DataUrl::process(specifier.as_str())
.map_err(|_| unable_to_decode())?;
let (bytes, _) = url.decode_to_vec().map_err(|_| unable_to_decode())?;
Ok(RawDataUrl {
mime_type: url.mime_type().to_string(),
bytes,
})
}

pub fn charset(&self) -> Option<&str> {
get_mime_type_charset(&self.mime_type)
}

pub fn media_type(&self) -> MediaType {
let mut content_types = self.mime_type.split(';');
let Some(content_type) = content_types.next() else {
return MediaType::Unknown;
};
MediaType::from_content_type(
// this data url will be ignored when resolving the MediaType
// as in this rare case the MediaType is determined solely based
// on the provided content type
&Url::parse("data:image/png;base64,").unwrap(),
content_type,
)
}

#[cfg(feature = "decoding")]
pub fn decode(self) -> Result<String, std::io::Error> {
let charset = get_mime_type_charset(&self.mime_type).unwrap_or("utf-8");
crate::encoding::decode_owned_source(charset, self.bytes)
}

pub fn into_bytes_and_mime_type(self) -> (Vec<u8>, String) {
(self.bytes, self.mime_type)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_parse_valid_data_url() {
let valid_data_url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==";
let specifier = Url::parse(valid_data_url).unwrap();
let raw_data_url = RawDataUrl::parse(&specifier).unwrap();
assert_eq!(raw_data_url.mime_type, "text/plain");
assert_eq!(raw_data_url.bytes, b"Hello, World!");
}

#[test]
fn test_charset_with_valid_mime_type() {
let raw_data_url = RawDataUrl {
mime_type: "text/plain; charset=utf-8".to_string(),
bytes: vec![],
};
assert_eq!(raw_data_url.charset(), Some("utf-8"));
}

#[test]
fn test_charset_with_no_charset_in_mime_type() {
let raw_data_url = RawDataUrl {
mime_type: "text/plain".to_string(),
bytes: vec![],
};
assert_eq!(raw_data_url.charset(), None);
}

#[test]
fn test_media_type_with_known_type() {
let raw_data_url = RawDataUrl {
mime_type: "application/javascript;charset=utf-8".to_string(),
bytes: vec![],
};
assert_eq!(raw_data_url.media_type(), MediaType::JavaScript);
}

#[test]
fn test_media_type_with_unknown_type() {
let raw_data_url = RawDataUrl {
mime_type: "unknown/unknown".to_string(),
bytes: vec![],
};
assert_eq!(raw_data_url.media_type(), MediaType::Unknown);
}

#[test]
fn test_decode_with_valid_charset() {
let raw_data_url = RawDataUrl {
mime_type: "text/plain; charset=utf-8".to_string(),
bytes: "Hello, World!".as_bytes().to_vec(),
};
assert_eq!(raw_data_url.decode().unwrap(), "Hello, World!");
}

#[test]
fn test_decode_with_invalid_charset() {
let raw_data_url = RawDataUrl {
mime_type: "text/plain; charset=invalid-charset".to_string(),
bytes: vec![],
};
assert!(raw_data_url.decode().is_err());
}
}
Loading
Loading