Skip to content

Commit

Permalink
fix: allow empty persistent cache options (#8813)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a authored Dec 23, 2024
1 parent 8fda3ba commit db6a793
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
15 changes: 5 additions & 10 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1367,24 +1367,19 @@ export interface RawEvalDevToolModulePluginOptions {
sourceUrlComment?: string
}

export interface RawExperimentCacheOptionsMemory {
type: "memory" | "disable"
}

export interface RawExperimentCacheOptionsPersistent {
type: "persistent"
buildDependencies: Array<string>
version: string
snapshot: RawExperimentSnapshotOptions
storage: RawStorageOptions
buildDependencies?: Array<string>
version?: string
snapshot?: RawExperimentSnapshotOptions
storage?: RawStorageOptions
}

export interface RawExperiments {
layers: boolean
topLevelAwait: boolean
incremental?: false | { [key: string]: boolean }
rspackFuture?: RawRspackFuture
cache: RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsMemory | boolean
cache: boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }
}

export interface RawExperimentSnapshotOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct RawExperiments {
pub incremental: Option<WithFalse<RawIncremental>>,
pub rspack_future: Option<RawRspackFuture>,
#[napi(
ts_type = r#"RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsMemory | boolean"#
ts_type = r#"boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }"#
)]
pub cache: RawExperimentCacheOptions,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,96 @@ mod raw_storage;

use core::panic;

use napi::bindgen_prelude::Either3;
use napi::{
bindgen_prelude::{FromNapiValue, Object, TypeName, ValidateNapiValue},
Either,
};
use napi_derive::napi;
use raw_snapshot::RawExperimentSnapshotOptions;
use raw_storage::RawStorageOptions;
use rspack_core::{cache::persistent::PersistentCacheOptions, ExperimentCacheOptions};

pub type RawExperimentCacheOptions =
Either3<bool, RawExperimentCacheOptionsMemory, RawExperimentCacheOptionsPersistent>;
pub type RawExperimentCacheOptions = Either<bool, RawExperimentCache>;

#[derive(Debug, Default)]
#[napi(object)]
pub struct RawExperimentCacheOptionsPersistent {
#[napi(ts_type = r#""persistent""#)]
pub r#type: String,
pub build_dependencies: Vec<String>,
pub version: String,
pub snapshot: RawExperimentSnapshotOptions,
pub storage: RawStorageOptions,
pub build_dependencies: Option<Vec<String>>,
pub version: Option<String>,
pub snapshot: Option<RawExperimentSnapshotOptions>,
pub storage: Option<RawStorageOptions>,
}

impl From<RawExperimentCacheOptionsPersistent> for PersistentCacheOptions {
fn from(value: RawExperimentCacheOptionsPersistent) -> Self {
Self {
build_dependencies: value
.build_dependencies
.unwrap_or_default()
.into_iter()
.map(Into::into)
.collect(),
version: value.version.unwrap_or_default(),
snapshot: value.snapshot.unwrap_or_default().into(),
storage: value.storage.unwrap_or_default().into(),
}
}
}

#[derive(Debug, Default)]
#[napi(object)]
pub struct RawExperimentCacheOptionsMemory {
#[napi(ts_type = r#""memory" | "disable""#)]
pub r#type: String,
pub enum RawExperimentCache {
#[default]
Memory,
Persistent(RawExperimentCacheOptionsPersistent),
}

impl TypeName for RawExperimentCache {
fn type_name() -> &'static str {
"RawExperimentCache"
}

fn value_type() -> napi::ValueType {
napi::ValueType::Object
}
}

impl ValidateNapiValue for RawExperimentCache {}

impl FromNapiValue for RawExperimentCache {
unsafe fn from_napi_value(
env: napi::sys::napi_env,
napi_val: napi::sys::napi_value,
) -> napi::Result<Self> {
let o = Object::from_napi_value(env, napi_val)?;
let t = o.get_named_property::<String>("type")?;

let v = match &*t {
"persistent" => {
let o = RawExperimentCacheOptionsPersistent::from_napi_value(env, napi_val)?;
Self::Persistent(o)
}
"memory" => Self::Memory,
_ => panic!("Unexpected cache type: {t}, expected 'persistent' or 'memory'"),
};

Ok(v)
}
}

pub fn normalize_raw_experiment_cache_options(
options: RawExperimentCacheOptions,
) -> ExperimentCacheOptions {
match options {
Either3::C(persistent_options) if persistent_options.r#type == "persistent" => {
ExperimentCacheOptions::Persistent(PersistentCacheOptions {
build_dependencies: persistent_options
.build_dependencies
.into_iter()
.map(Into::into)
.collect(),
version: persistent_options.version,
snapshot: persistent_options.snapshot.into(),
storage: persistent_options.storage.into(),
})
}
Either3::B(options) if options.r#type == "memory" => ExperimentCacheOptions::Memory,
Either3::B(options) if options.r#type == "disable" => ExperimentCacheOptions::Disabled,
Either3::A(options) => {
Either::A(options) => {
if options {
ExperimentCacheOptions::Memory
} else {
ExperimentCacheOptions::Disabled
}
}
_ => panic!("Invalid cache options"),
Either::B(options) => match options {
RawExperimentCache::Persistent(options) => ExperimentCacheOptions::Persistent(options.into()),
RawExperimentCache::Memory => ExperimentCacheOptions::Memory,
},
}
}

0 comments on commit db6a793

Please sign in to comment.