Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Add support for text blob bindings #1543

Merged
merged 4 commits into from
Jan 14, 2021
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
1 change: 1 addition & 0 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 2 additions & 0 deletions src/fixtures/wrangler_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct EnvConfig {
#[serde(alias = "kv-namespaces")]
pub kv_namespaces: Option<Vec<KvConfig>>,
pub vars: Option<HashMap<&'static str, &'static str>>,
pub text_blobs: Option<HashMap<&'static str, &'static str>>,
pub triggers: Option<Triggers>,
}

Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct WranglerToml {
pub kv_namespaces: Option<Vec<KvConfig>>,
pub site: Option<SiteConfig>,
pub vars: Option<HashMap<&'static str, &'static str>>,
pub text_blobs: Option<HashMap<&'static str, &'static str>>,
pub triggers: Option<Triggers>,
}

Expand Down
2 changes: 2 additions & 0 deletions src/settings/toml/environment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use serde_with::rust::string_empty_as_none;
Expand All @@ -25,6 +26,7 @@ pub struct Environment {
#[serde(alias = "kv-namespaces")]
pub kv_namespaces: Option<Vec<ConfigKvNamespace>>,
pub vars: Option<HashMap<String, String>>,
pub text_blobs: Option<HashMap<String, PathBuf>>,
pub triggers: Option<Triggers>,
}

Expand Down
2 changes: 2 additions & 0 deletions src/settings/toml/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Manifest {
pub kv_namespaces: Option<Vec<ConfigKvNamespace>>,
pub env: Option<HashMap<String, Environment>>,
pub vars: Option<HashMap<String, String>>,
pub text_blobs: Option<HashMap<String, PathBuf>>,
pub triggers: Option<Triggers>,
}

Expand Down Expand Up @@ -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)?;
Expand Down
1 change: 1 addition & 0 deletions src/settings/toml/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Target {
pub webpack_config: Option<String>,
pub site: Option<Site>,
pub vars: Option<HashMap<String, String>>,
pub text_blobs: Option<HashMap<String, PathBuf>>,
}

impl Target {
Expand Down
1 change: 1 addition & 0 deletions src/sites/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ mod tests {
webpack_config: None,
site: Some(site),
vars: None,
text_blobs: None,
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/upload/form/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ pub fn build(
let mut plain_texts: Vec<PlainText> = 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())?)
Expand Down
29 changes: 29 additions & 0 deletions tests/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down