Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

34 changes: 14 additions & 20 deletions crates/rspack_binding_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,26 +480,6 @@ fn concurrent_compiler_error() -> Error<ErrorCode> {
)
}

#[cfg(target_family = "wasm")]
const _: () = {
#[used]
#[unsafe(link_section = ".init_array")]
static __CTOR: unsafe extern "C" fn() = init;

unsafe extern "C" fn init() {
#[cfg(feature = "browser")]
rspack_browser::panic::install_panic_handler();
#[cfg(not(feature = "browser"))]
panic::install_panic_handler();
let rt = tokio::runtime::Builder::new_multi_thread()
.max_blocking_threads(1)
.enable_all()
.build()
.expect("Create tokio runtime failed");
create_custom_tokio_runtime(rt);
}
};

#[cfg(not(target_family = "wasm"))]
#[napi::ctor::ctor(crate_path = ::napi::ctor)]
fn init() {
Expand Down Expand Up @@ -589,6 +569,20 @@ fn node_init(mut _exports: Object, env: Env) -> Result<()> {

#[napi(module_exports)]
fn rspack_module_exports(exports: Object, env: Env) -> Result<()> {
#[cfg(target_family = "wasm")]
{
#[cfg(feature = "browser")]
rspack_browser::panic::install_panic_handler();
#[cfg(not(feature = "browser"))]
panic::install_panic_handler();
let rt = tokio::runtime::Builder::new_multi_thread()
.max_blocking_threads(1)
.enable_all()
.build()
.expect("Create tokio runtime failed");
create_custom_tokio_runtime(rt);
}

node_init(exports, env)?;
module::export_symbols(exports, env)?;
build_info::export_symbols(exports, env)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl HttpClient for JsHttpClient {
Ok(HttpResponse {
status: result.status,
headers: result.headers,
body: result.body.to_vec(),
body: result.body,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_plugin_schemes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version.workspace = true
anyhow = { workspace = true }
async-trait = { workspace = true }
cow-utils = { workspace = true }
napi = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rspack_core = { workspace = true }
Expand Down
75 changes: 45 additions & 30 deletions crates/rspack_plugin_schemes/src/http_uri/http_cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
fmt::Debug,
path::PathBuf,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
Expand All @@ -8,6 +9,7 @@ use std::{
use anyhow::Result;
use async_trait::async_trait;
use cow_utils::CowUtils;
use napi::bindgen_prelude::Buffer;
use rspack_fs::WritableFileSystem;
use rspack_paths::Utf8Path;
use rspack_util::{base64, fx_hash::FxHashMap};
Expand All @@ -18,10 +20,25 @@ use url::Url;
use super::lockfile::{LockfileCache, LockfileEntry};
use crate::http_uri::HttpUriPluginOptions;

/// This enum is used for avoiding [Buffer::to_vec] overhead
pub enum BufferOrBytes {
Buffer(Buffer),
Bytes(Vec<u8>),
}

impl Clone for BufferOrBytes {
fn clone(&self) -> Self {
match self {
Self::Buffer(buffer) => Self::Bytes(buffer.to_vec()),
Self::Bytes(bytes) => Self::Bytes(bytes.clone()),
}
}
}

pub struct HttpResponse {
pub status: u16,
pub headers: FxHashMap<String, String>,
pub body: Vec<u8>,
pub body: Buffer,
}

#[async_trait]
Expand All @@ -38,16 +55,20 @@ pub struct FetchResultMeta {
fresh: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Clone)]
pub struct ContentFetchResult {
pub(crate) entry: LockfileEntry,
content: Vec<u8>,
content: BufferOrBytes,
meta: FetchResultMeta,
}

impl ContentFetchResult {
#[inline(always)]
pub fn content(&self) -> &[u8] {
&self.content
match &self.content {
BufferOrBytes::Buffer(buffer) => buffer.as_ref(),
BufferOrBytes::Bytes(items) => items.as_ref(),
}
}
}

Expand All @@ -57,7 +78,6 @@ pub struct RedirectFetchResult {
meta: FetchResultMeta,
}

#[derive(Debug)]
pub enum FetchResultType {
Content(ContentFetchResult),
#[allow(dead_code)]
Expand Down Expand Up @@ -171,23 +191,23 @@ impl HttpCache {
};

// If we had a cached redirect that's unchanged, use the cached meta
if let Some(cached) = &cached_result
&& let FetchResultType::Redirect(cached_redirect) =
fetch_cache_result_to_fetch_result_type(cached)
&& cached_redirect.location == absolute_location
&& cached_redirect.meta.valid_until >= valid_until
&& cached_redirect.meta.store_lock == store_lock
&& cached_redirect.meta.store_cache == store_cache
&& cached_redirect.meta.etag == etag
{
return Ok(FetchResultType::Redirect(RedirectFetchResult {
meta: FetchResultMeta {
fresh: true,
..cached_redirect.meta
},
..cached_redirect
}));
}
// if let Some(cached) = &cached_result
// && let FetchResultType::Redirect(cached_redirect) =
// fetch_cache_result_to_fetch_result_type(cached)
// && cached_redirect.location == absolute_location
// && cached_redirect.meta.valid_until >= valid_until
// && cached_redirect.meta.store_lock == store_lock
// && cached_redirect.meta.store_cache == store_cache
// && cached_redirect.meta.etag == etag
// {
// return Ok(FetchResultType::Redirect(RedirectFetchResult {
// meta: FetchResultMeta {
// fresh: true,
// ..cached_redirect.meta
// },
// ..cached_redirect
// }));
// }

return Ok(FetchResultType::Redirect(RedirectFetchResult {
location: absolute_location,
Expand Down Expand Up @@ -226,7 +246,7 @@ impl HttpCache {

let result = ContentFetchResult {
entry: entry.clone(),
content: content.to_vec(),
content: BufferOrBytes::Buffer(content),
meta: FetchResultMeta {
fresh: true,
store_lock,
Expand All @@ -247,7 +267,7 @@ impl HttpCache {

if should_update {
if store_cache {
self.write_to_cache(url, &result.content).await?;
self.write_to_cache(url, result.content()).await?;
}

let lockfile = self.lockfile_cache.get_lockfile().await?;
Expand Down Expand Up @@ -284,7 +304,7 @@ impl HttpCache {

let result = ContentFetchResult {
entry: entry.clone(),
content,
content: BufferOrBytes::Bytes(content),
meta,
};

Expand Down Expand Up @@ -467,8 +487,3 @@ fn compute_integrity(content: &[u8]) -> String {
// Use base64 for integrity as that's the standard format
format!("sha512-{}", base64::encode_to_string(digest))
}

// Helper function to convert ContentFetchResult to FetchResultType
fn fetch_cache_result_to_fetch_result_type(result: &ContentFetchResult) -> FetchResultType {
FetchResultType::Content(result.clone())
}
4 changes: 3 additions & 1 deletion packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ class Compiler {
);
new JsLoaderRspackPlugin(this).apply(this);
new ExecuteModulePlugin().apply(this);
new TraceHookPlugin().apply(this);
if (!IS_BROWSER) {
new TraceHookPlugin().apply(this);
}

// this.hooks.shutdown.tap("rspack:cleanup", () => {
// // Delayed rspack cleanup to the next tick.
Expand Down
Loading