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

rebases & updates durable-objects-rc branch #1919

Merged
merged 4 commits into from
May 13, 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
3 changes: 3 additions & 0 deletions src/commands/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ pub fn dev(
);
}

if target.durable_objects.is_some() {
anyhow::bail!("Previewing a script that binds to a Durable Object Class is not supported using unauthenticated preview. Please use wrangler login or wrangler config.")
}
gcs::dev(target, server_config, local_protocol, verbose)
}
4 changes: 3 additions & 1 deletion src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ mod tests {
binding: "KV".to_string(),
},
],
durable_objects: None,
migrations: None,
name: "test-target".to_string(),
target_type: TargetType::Webpack,
webpack_config: None,
site: None,
vars: None,
text_blobs: None,
usage_model: None,
build: None,
wasm_modules: None,
usage_model: None,
};
assert!(kv::get_namespace_id(&target_with_dup_kv_bindings, "").is_err());
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate text_io;

mod build;
pub mod preview;
pub mod util;
pub use build::build_target;
pub mod commands;
pub mod deploy;
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ use wrangler::preview::{HttpMethod, PreviewOpt};
use wrangler::reporter;
use wrangler::settings;
use wrangler::settings::global_user::GlobalUser;
use wrangler::settings::toml::migrations::{MigrationConfig, Migrations};
use wrangler::settings::toml::TargetType;
use wrangler::terminal::message::{Message, Output, StdOut};
use wrangler::terminal::{emoji, interactive, styles};
use wrangler::util::ApplyToApp;
use wrangler::version::background_check_for_updates;

fn main() -> Result<()> {
Expand Down Expand Up @@ -577,6 +579,7 @@ fn run() -> Result<()> {
.takes_value(true)
.possible_value("json")
)
.apply(MigrationConfig::add_to_app)
,
)
.subcommand(
Expand Down Expand Up @@ -878,6 +881,11 @@ fn run() -> Result<()> {
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let mut target = manifest.get_target(env, is_preview)?;
if let Some(adhoc) = MigrationConfig::from_matches(&matches) {
target.migrations = Some(Migrations {
migrations: vec![adhoc],
});
}

let deploy_config = manifest.get_deployments(env)?;
if matches.is_present("output") && matches.value_of("output") == Some("json") {
Expand Down
4 changes: 4 additions & 0 deletions src/preview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub fn preview(
}
}

if target.durable_objects.is_some() {
anyhow::bail!("wrangler preview does not support previewing scripts that bind to durable object classes. Please use wrangler dev instead.");
}

build_target(&target)?;

let sites_preview: bool = target.site.is_some();
Expand Down
72 changes: 48 additions & 24 deletions src/reporter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::commands::DEFAULT_CONFIG_PATH;
use crate::settings::{self, toml::Manifest};
use crate::{
commands::DEFAULT_CONFIG_PATH,
settings::toml::{DurableObjects, UploadFormat},
};

use std::env::args;
use std::fs::{read_dir, File};
Expand All @@ -23,8 +26,10 @@ const BACKTRACE_PATH_PREFIX: &str = "backtrace::";
pub struct Report {
uuid: Uuid,
timestamp_ms: u128,
host_env: HashMap<String, String>, // "os": "..." TODO: consider struct over HashMaps
host_env: HashMap<String, String>,
project_info: HashMap<String, String>,
durable_objects: Option<DurableObjects>,
upload_format: Option<UploadFormat>,
args: Vec<String>,
panic: Option<String>,
location: Option<String>,
Expand Down Expand Up @@ -53,11 +58,14 @@ pub fn read_log(log: Option<&str>) -> Result<Report> {

/// gathers necessary error report information, and stores on disk until uploaded. the
pub fn generate_report(panic_info: Option<&PanicInfo>) {
let project_info = load_project_info();
let mut report = Report {
uuid: Uuid::new_v4(),
timestamp_ms: 0,
host_env: load_host_info(),
project_info: load_project_info(),
project_info: project_info.base,
durable_objects: project_info.durable_objects,
upload_format: project_info.upload_format,
args: args().collect::<Vec<_>>(),
panic: panic_payload(panic_info),
location: None,
Expand Down Expand Up @@ -177,49 +185,65 @@ fn load_host_info() -> HashMap<String, String> {
host_info
}

#[derive(Default)]
struct ProjectInfo {
base: HashMap<String, String>,
durable_objects: Option<DurableObjects>,
upload_format: Option<UploadFormat>,
}

// wrangler project information
fn load_project_info() -> HashMap<String, String> {
let mut project_info = HashMap::new();
fn load_project_info() -> ProjectInfo {
let mut project_info: ProjectInfo = Default::default();

if let Ok(manifest) = Manifest::new(Path::new(DEFAULT_CONFIG_PATH)) {
project_info.insert("script_name".into(), manifest.name);
project_info.insert("account_id".into(), manifest.account_id);
project_info.insert(
project_info
.base
.insert("script_name".into(), manifest.name);
project_info
.base
.insert("account_id".into(), manifest.account_id);
project_info.base.insert(
"zone_id".into(),
manifest.zone_id.unwrap_or_else(|| "".into()),
);
project_info.insert("target_type".into(), manifest.target_type.to_string());
project_info.insert(
project_info
.base
.insert("target_type".into(), manifest.target_type.to_string());
project_info.base.insert(
"workers_dev".into(),
manifest.workers_dev.unwrap_or(false).to_string(),
);
if let Some(builder) = manifest.build {
project_info.insert("using_custom_build".into(), "true".into());
project_info
.base
.insert("using_custom_build".into(), "true".into());

if let Some((command, _)) = builder.build_command() {
project_info.insert("custom_build_command".into(), command.into());
project_info
.base
.insert("custom_build_command".into(), command.into());
}

// TODO: encode the format's struct members in map field instead of only string literal
project_info.insert(
"upload_format".into(),
match builder.upload {
settings::toml::UploadFormat::ServiceWorker { .. } => "service-worker".into(),
settings::toml::UploadFormat::Modules { .. } => "modules".into(),
},
);
project_info.upload_format = Some(builder.upload);
}

if let Some(routes) = manifest.routes {
project_info.insert("routes".into(), routes.join(","));
project_info.base.insert("routes".into(), routes.join(","));
}

if let Some(route) = manifest.route {
project_info.insert("route".into(), route);
project_info.base.insert("route".into(), route);
}

if let Some(usage_model) = &manifest.usage_model {
project_info
.base
.insert("usage_model".into(), usage_model.as_ref().to_string());
}

if let Some(usage_model) = manifest.usage_model {
project_info.insert("usage_model".into(), usage_model.as_ref().into());
if let Some(durable_objects) = &manifest.durable_objects {
project_info.durable_objects = Some(durable_objects.clone());
}
}

Expand Down
38 changes: 34 additions & 4 deletions src/settings/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ use serde::Serialize;
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum Binding {
WasmModule { name: String, part: String },
KvNamespace { name: String, namespace_id: String },
TextBlob { name: String, part: String },
PlainText { name: String, text: String },
WasmModule {
name: String,
part: String,
},
KvNamespace {
name: String,
namespace_id: String,
},
#[serde(rename = "durable_object_namespace")]
DurableObjectsClass {
name: String,
class_name: String,
script_name: Option<String>,
},
TextBlob {
name: String,
part: String,
},
PlainText {
name: String,
text: String,
},
}

impl Binding {
Expand All @@ -19,6 +37,18 @@ impl Binding {
Binding::KvNamespace { name, namespace_id }
}

pub fn new_durable_object_namespace(
name: String,
class_name: String,
script_name: Option<String>,
) -> Binding {
Binding::DurableObjectsClass {
name,
class_name,
script_name,
}
}

pub fn new_text_blob(name: String, part: String) -> Binding {
Binding::TextBlob { name, part }
}
Expand Down
27 changes: 27 additions & 0 deletions src/settings/toml/durable_objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{Deserialize, Serialize};

use crate::settings::binding::Binding;

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct DurableObjects {
#[serde(alias = "bindings")]
pub classes: Option<Vec<DurableObjectsClass>>,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct DurableObjectsClass {
#[serde(alias = "name")]
pub binding: String,
pub class_name: String,
pub script_name: Option<String>,
}

impl DurableObjectsClass {
pub fn binding(&self) -> Binding {
Binding::new_durable_object_namespace(
self.binding.clone(),
self.class_name.clone(),
self.script_name.clone(),
)
}
}
2 changes: 2 additions & 0 deletions src/settings/toml/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use serde_with::rust::string_empty_as_none;

use crate::settings::toml::builder::Builder;
use crate::settings::toml::durable_objects::DurableObjects;
use crate::settings::toml::kv_namespace::ConfigKvNamespace;
use crate::settings::toml::route::RouteConfig;
use crate::settings::toml::site::Site;
Expand All @@ -30,6 +31,7 @@ pub struct Environment {
pub vars: Option<HashMap<String, String>>,
pub text_blobs: Option<HashMap<String, PathBuf>>,
pub triggers: Option<Triggers>,
pub durable_objects: Option<DurableObjects>,
}

impl Environment {
Expand Down
14 changes: 13 additions & 1 deletion src/settings/toml/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::commands::{validate_worker_name, whoami, DEFAULT_CONFIG_PATH};
use crate::deploy::{self, DeployTarget, DeploymentSet};
use crate::settings::toml::builder::Builder;
use crate::settings::toml::dev::Dev;
use crate::settings::toml::durable_objects::DurableObjects;
use crate::settings::toml::environment::Environment;
use crate::settings::toml::kv_namespace::{ConfigKvNamespace, KvNamespace};
use crate::settings::toml::route::RouteConfig;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub struct Manifest {
pub text_blobs: Option<HashMap<String, PathBuf>>,
pub wasm_modules: Option<HashMap<String, PathBuf>>,
pub triggers: Option<Triggers>,
pub durable_objects: Option<DurableObjects>,
#[serde(default, with = "string_empty_as_none")]
pub usage_model: Option<UsageModel>,
}
Expand Down Expand Up @@ -272,7 +274,12 @@ impl Manifest {
deployments.push(DeployTarget::Schedule(scheduled));
}

if deployments.is_empty() {
let durable_objects = match env {
Some(e) => e.durable_objects.as_ref(),
None => self.durable_objects.as_ref(),
};

if durable_objects.is_none() && deployments.is_empty() {
anyhow::bail!("No deployments specified!")
}

Expand Down Expand Up @@ -341,6 +348,8 @@ impl Manifest {
// to include the name of the environment
name: self.name.clone(), // Inherited
kv_namespaces: get_namespaces(self.kv_namespaces.clone(), preview)?, // Not inherited
durable_objects: self.durable_objects.clone(), // Not inherited
migrations: None, // TODO(soon) Allow migrations in wrangler.toml
site: self.site.clone(), // Inherited
vars: self.vars.clone(), // Not inherited
text_blobs: self.text_blobs.clone(), // Inherited
Expand All @@ -365,6 +374,9 @@ impl Manifest {
// don't inherit kv namespaces because it is an anti-pattern to use the same namespaces across multiple environments
target.kv_namespaces = get_namespaces(environment.kv_namespaces.clone(), preview)?;

// don't inherit durable object configuration
target.durable_objects = environment.durable_objects.clone();

// inherit site configuration
if let Some(site) = &environment.site {
target.site = Some(site.clone());
Expand Down
Loading