Skip to content

Commit

Permalink
refactor: unify base64 utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Sep 3, 2024
1 parent 025591a commit 823fa8b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/mako/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ swc_error_reporters = "0.16.1"
swc_node_comments = "0.19.1"

anyhow = "1.0.71"
base64 = "0.21.2"
base64 = "0.22.1"
chrono = "0.4.38"
clap = { version = "4.3.11", features = ["derive"] }
colored = "2"
Expand Down
9 changes: 4 additions & 5 deletions crates/mako/src/ast/css_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::fmt;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use base64::engine::general_purpose;
use base64::Engine;
use md5;
use swc_core::common::FileName;
use swc_core::css::ast::Stylesheet;
Expand All @@ -15,12 +13,13 @@ use swc_core::css::{parser, visit};
use swc_core::ecma::atoms;
use swc_core::ecma::parser::StringInput;

use crate::ast::error;
use crate::ast::file::{Content, File};
use crate::ast::sourcemap::build_source_map_to_buf;
use crate::ast::{error, utils};
use crate::compiler::Context;
use crate::config::{DevtoolConfig, Mode};
use crate::module::Dependency;
use crate::utils::{base64_encode, url_safe_base64_encode};
use crate::visitors::css_dep_analyzer::CSSDepAnalyzer;

#[derive(Clone)]
Expand Down Expand Up @@ -136,7 +135,7 @@ impl CssAst {
code.push_str(
format!(
"\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,{}*/",
utils::base64_encode(&sourcemap)
base64_encode(&sourcemap)

Check warning on line 138 in crates/mako/src/ast/css_ast.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/ast/css_ast.rs#L138

Added line #L138 was not covered by tests
)
.as_str(),
);
Expand Down Expand Up @@ -225,7 +224,7 @@ impl TransformConfig for CssModuleRename {
fn ident_name(path: &str, name: &str) -> String {
let source = format!("{}__{}", path, name);
let digest = md5::compute(source);
let hash = general_purpose::URL_SAFE.encode(digest.0);
let hash = url_safe_base64_encode(digest.0);

Check warning on line 227 in crates/mako/src/ast/css_ast.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/ast/css_ast.rs#L227

Added line #L227 was not covered by tests
let hash_slice = hash[..8].to_string();
format!("{}-{}", name, hash_slice)
}
9 changes: 3 additions & 6 deletions crates/mako/src/ast/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::path::PathBuf;
use std::sync::{Arc, OnceLock};

use anyhow::{anyhow, Result};
use base64::alphabet::STANDARD;
use base64::{engine, Engine};
use pathdiff::diff_paths;
use percent_encoding::percent_decode_str;
use regex::Regex;
Expand All @@ -15,7 +13,7 @@ use url::Url;
use {md5, mime_guess};

use crate::compiler::Context;
use crate::utils::base64_decode;
use crate::utils::{base64_decode, base64_encode};

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Asset {
Expand Down Expand Up @@ -228,14 +226,13 @@ impl File {

pub fn get_base64(&self) -> Result<String> {
let content = std::fs::read(&self.pathname)?;
let engine = engine::GeneralPurpose::new(&STANDARD, engine::general_purpose::PAD);
let content = engine.encode(content);
let content_base64 = base64_encode(content);
let guess = mime_guess::from_path(&self.pathname);
if let Some(mime) = guess.first() {
Ok(format!(
"data:{};base64,{}",
mime,
content.replace("\r\n", "")
content_base64.replace("\r\n", "")
))
} else {
Err(anyhow!(FileError::ToBase64Error {
Expand Down
6 changes: 0 additions & 6 deletions crates/mako/src/ast/utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use base64::engine::general_purpose;
use base64::Engine;
use swc_core::common::{Mark, DUMMY_SP};
use swc_core::ecma::ast::{
CallExpr, Callee, Expr, ExprOrSpread, Ident, Import, Lit, MemberExpr, MemberProp, MetaPropExpr,
MetaPropKind, Module, ModuleItem,
};

pub fn base64_encode<T: AsRef<[u8]>>(raw: T) -> String {
general_purpose::STANDARD.encode(raw)
}

pub fn is_remote_or_data(url: &str) -> bool {
let lower_url = url.to_lowercase();
// ref:
Expand Down
5 changes: 2 additions & 3 deletions crates/mako/src/generate/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::fmt::{Debug, Formatter};
use std::hash::Hasher;
use std::path::{Component, Path};

use base64::engine::general_purpose;
use base64::Engine;
use hashlink::LinkedHashSet;
use twox_hash::XxHash64;

use crate::ast::file::parse_path;
use crate::module::ModuleId;
use crate::module_graph::ModuleGraph;
use crate::utils::url_safe_base64_encode;

pub type ChunkId = ModuleId;

Expand Down Expand Up @@ -87,7 +86,7 @@ impl Chunk {

if !search.is_empty() {
let search_hash =
general_purpose::URL_SAFE.encode(md5::compute(search).0)[..4].to_string();
url_safe_base64_encode(md5::compute(search).0)[..4].to_string();

Check warning on line 89 in crates/mako/src/generate/chunk.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/chunk.rs#L89

Added line #L89 was not covered by tests
name = format!("{}_q_{}", name, search_hash);
}

Expand Down
5 changes: 2 additions & 3 deletions crates/mako/src/generate/optimize_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;
use std::string::String;

use base64::engine::general_purpose;
use base64::Engine;
use hashlink::LinkedHashSet;
use indexmap::{IndexMap, IndexSet};
use regex::Regex;
Expand All @@ -18,6 +16,7 @@ use crate::generate::chunk::{Chunk, ChunkId, ChunkType};
use crate::generate::group_chunk::GroupUpdateResult;
use crate::module::{Module, ModuleId, ModuleInfo};
use crate::resolve::{ResolvedResource, ResolverResource};
use crate::utils::url_safe_base64_encode;

pub struct OptimizeChunksInfo {
pub group_options: OptimizeChunkGroup,
Expand Down Expand Up @@ -663,6 +662,6 @@ fn md5_chunk_ids(chunk_ids: &[ChunkId]) -> String {
context.consume(cd.id.as_bytes());
});
let digest = context.compute();
let hash = general_purpose::URL_SAFE.encode(digest.0);
let hash = url_safe_base64_encode(digest.0);

Check warning on line 665 in crates/mako/src/generate/optimize_chunk.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/optimize_chunk.rs#L665

Added line #L665 was not covered by tests
hash[..8].to_string()
}
8 changes: 8 additions & 0 deletions crates/mako/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub fn base64_decode(bytes: &[u8]) -> Vec<u8> {
general_purpose::STANDARD.decode(bytes).unwrap()
}

pub fn url_safe_base64_encode<T: AsRef<[u8]>>(raw: T) -> String {
general_purpose::URL_SAFE.encode(raw)
}

Check warning on line 24 in crates/mako/src/utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/utils.rs#L22-L24

Added lines #L22 - L24 were not covered by tests

pub fn url_safe_base64_decode(bytes: &[u8]) -> Vec<u8> {
general_purpose::URL_SAFE.decode(bytes).unwrap()
}

Check warning on line 28 in crates/mako/src/utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/utils.rs#L26-L28

Added lines #L26 - L28 were not covered by tests

pub trait ParseRegex {
fn parse_into_regex(&self) -> Result<Option<Regex>>;
}
Expand Down

0 comments on commit 823fa8b

Please sign in to comment.