diff --git a/src/commands/kv/mod.rs b/src/commands/kv/mod.rs index c6e2bcc31..0e533fd57 100644 --- a/src/commands/kv/mod.rs +++ b/src/commands/kv/mod.rs @@ -124,6 +124,7 @@ mod tests { webpack_config: None, site: None, vars: None, + text_blobs: None, }; assert!(kv::get_namespace_id(&target_with_dup_kv_bindings, "").is_err()); } diff --git a/src/fixtures/wrangler_toml.rs b/src/fixtures/wrangler_toml.rs index 0a2a48c96..b87bf363b 100644 --- a/src/fixtures/wrangler_toml.rs +++ b/src/fixtures/wrangler_toml.rs @@ -44,6 +44,7 @@ pub struct EnvConfig { #[serde(alias = "kv-namespaces")] pub kv_namespaces: Option>, pub vars: Option>, + pub text_blobs: Option>, pub triggers: Option, } @@ -104,6 +105,7 @@ pub struct WranglerToml { pub kv_namespaces: Option>, pub site: Option, pub vars: Option>, + pub text_blobs: Option>, pub triggers: Option, } diff --git a/src/settings/toml/environment.rs b/src/settings/toml/environment.rs index 8703579a0..1ae426216 100644 --- a/src/settings/toml/environment.rs +++ b/src/settings/toml/environment.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::path::PathBuf; use serde::{Deserialize, Serialize}; use serde_with::rust::string_empty_as_none; @@ -25,6 +26,7 @@ pub struct Environment { #[serde(alias = "kv-namespaces")] pub kv_namespaces: Option>, pub vars: Option>, + pub text_blobs: Option>, pub triggers: Option, } diff --git a/src/settings/toml/manifest.rs b/src/settings/toml/manifest.rs index ebc7a9356..fd48508c3 100644 --- a/src/settings/toml/manifest.rs +++ b/src/settings/toml/manifest.rs @@ -49,6 +49,7 @@ pub struct Manifest { pub kv_namespaces: Option>, pub env: Option>, pub vars: Option>, + pub text_blobs: Option>, pub triggers: Option, } @@ -295,6 +296,7 @@ impl Manifest { kv_namespaces: get_namespaces(self.kv_namespaces.clone(), preview)?, // Not inherited site: self.site.clone(), // Inherited vars: self.vars.clone(), // Not inherited + text_blobs: self.text_blobs.clone(), // Inherited }; let environment = self.get_environment(environment_name)?; diff --git a/src/settings/toml/target.rs b/src/settings/toml/target.rs index 7823f23d2..a68365249 100644 --- a/src/settings/toml/target.rs +++ b/src/settings/toml/target.rs @@ -16,6 +16,7 @@ pub struct Target { pub webpack_config: Option, pub site: Option, pub vars: Option>, + pub text_blobs: Option>, } impl Target { diff --git a/src/sites/mod.rs b/src/sites/mod.rs index d2c0aed75..2b4e74978 100644 --- a/src/sites/mod.rs +++ b/src/sites/mod.rs @@ -320,6 +320,7 @@ mod tests { webpack_config: None, site: Some(site), vars: None, + text_blobs: None, } } diff --git a/src/upload/form/mod.rs b/src/upload/form/mod.rs index 09de2c0e8..c2c1fe8eb 100644 --- a/src/upload/form/mod.rs +++ b/src/upload/form/mod.rs @@ -33,6 +33,13 @@ pub fn build( let mut plain_texts: Vec = Vec::new(); let mut wasm_modules: Vec<WasmModule> = Vec::new(); + if let Some(blobs) = &target.text_blobs { + for (key, blob_path) in blobs.iter() { + let blob = fs::read_to_string(blob_path)?; + text_blobs.push(TextBlob::new(blob, key.clone())?); + } + } + if let Some(vars) = &target.vars { for (key, value) in vars.iter() { plain_texts.push(PlainText::new(key.clone(), value.clone())?) diff --git a/tests/preview.rs b/tests/preview.rs index f9e9f8430..565c10e42 100644 --- a/tests/preview.rs +++ b/tests/preview.rs @@ -227,6 +227,35 @@ fn it_previews_with_config_text() { preview_succeeds_with(&fixture, None, test_value); } +#[test] +fn it_previews_with_text_blob() { + let fixture = Fixture::new(); + fixture.create_file( + "index.js", + r#" + addEventListener('fetch', event => { + event.respondWith(handleRequest(event.request)) + }) + + async function handleRequest(request) { + return new Response(BLOB) + } + "#, + ); + fixture.create_default_package_json(); + + let test_value: &'static str = "sdhftiuyrtdhfjgpoopuyrdfjgkyitudrhf"; + fixture.create_file("blob.txt", test_value); + + let mut wrangler_toml = WranglerToml::javascript("test-preview-with-config"); + let mut blobs: HashMap<&'static str, &'static str> = HashMap::new(); + blobs.insert("BLOB", "blob.txt"); + wrangler_toml.text_blobs = Some(blobs); + fixture.create_wrangler_toml(wrangler_toml); + + preview_succeeds_with(&fixture, None, test_value); +} + fn preview_succeeds_with(fixture: &Fixture, env: Option<&str>, expected: &str) { env::remove_var("CF_ACCOUNT_ID"); env::remove_var("CF_ZONE_ID");