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

Commit

Permalink
support implementing + using durable object namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
xortive committed Feb 25, 2021
1 parent 525eb02 commit 6eac698
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 34 deletions.
12 changes: 10 additions & 2 deletions src/commands/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use server_config::Protocol;
pub use server_config::ServerConfig;

use crate::build::build_target;
use crate::deploy::{DeployTarget, DeploymentSet};
use crate::deploy::{self, DeployTarget, DeploymentSet};
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;
use crate::terminal::message::{Message, StdOut};
Expand All @@ -18,7 +18,7 @@ use crate::terminal::styles;
/// `wrangler dev` starts a server on a dev machine that routes incoming HTTP requests
/// to a Cloudflare Workers runtime and returns HTTP responses
pub fn dev(
target: Target,
mut target: Target,
deployments: DeploymentSet,
user: Option<GlobalUser>,
server_config: ServerConfig,
Expand All @@ -29,6 +29,14 @@ pub fn dev(
// before serving requests we must first build the Worker
build_target(&target)?;

if let Some(user) = &user {
deploy::pre_upload(user, &mut target, &deployments, true)?;
} else {
failure::bail!(
"Previewing a script that binds to a Durable Object namespace is not supported using unauthenticated preview. Please use wrangler login or wrangler config."
);
}

let deploy_target = {
let valid_targets = deployments
.into_iter()
Expand Down
1 change: 1 addition & 0 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ mod tests {
binding: "KV".to_string(),
},
],
used_durable_object_namespaces: vec![],
name: "test-target".to_string(),
target_type: TargetType::Webpack,
webpack_config: None,
Expand Down
69 changes: 42 additions & 27 deletions src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct PublishOutput {
pub name: String,
pub urls: Vec<String>,
pub schedules: Vec<String>,
pub durable_object_namespaces: Vec<String>,
}

pub fn publish(
Expand All @@ -31,33 +32,9 @@ pub fn publish(
) -> Result<(), failure::Error> {
validate_target_required_fields_present(target)?;

let deploy = |target: &Target| match deploy::worker(&user, &deployments) {
Ok(deploy::DeployResults { urls, schedules }) => {
let result_msg = match (urls.as_slice(), schedules.as_slice()) {
([], []) => "Successfully published your script".to_owned(),
([], schedules) => format!(
"Successfully published your script with this schedule\n {}",
schedules.join("\n ")
),
(urls, []) => format!(
"Successfully published your script to\n {}",
urls.join("\n ")
),
(urls, schedules) => format!(
"Successfully published your script to\n {}\nwith this schedule\n {}",
urls.join("\n "),
schedules.join("\n ")
),
};
StdErr::success(&result_msg);
if out == Output::Json {
StdOut::as_json(&PublishOutput {
success: true,
name: target.name.clone(),
urls,
schedules,
});
}
let deploy = |target: &Target| match deploy::deploy(&user, &deployments) {
Ok(results) => {
build_output_message(results, target.name.clone(), out);
Ok(())
}
Err(e) => Err(e),
Expand Down Expand Up @@ -110,6 +87,10 @@ pub fn publish(
pb.finish_with_message("Done Uploading");
}

// Workers Sites are currently limited to type = "webpack", and therefore service-worker format
// So there's no point in performing the full set of pre_upload tasks for Durable Objects.
deploy::pre_upload(user, target, &deployments, true)?;

let upload_client = http::featured_legacy_auth_client(user, Feature::Sites);

// Next, upload and deploy the worker with the updated asset_manifest
Expand Down Expand Up @@ -146,13 +127,47 @@ pub fn publish(
} else {
let upload_client = http::legacy_auth_client(user);

deploy::pre_upload(user, target, &deployments, false)?;
upload::script(&upload_client, &target, None)?;
deploy(target)?;
}

Ok(())
}

fn build_output_message(deploy_results: deploy::DeployResults, target_name: String, out: Output) {
let deploy::DeployResults {
urls,
schedules,
durable_object_namespaces,
} = deploy_results;

let mut msg = "Successfully published your script ".to_owned();
if !urls.is_empty() {
msg.push_str(&format!("to\n {}\n", urls.join("\n ")));
}
if !schedules.is_empty() {
msg.push_str(&format!("with this schedule\n {}\n", schedules.join("\n ")));
}
if !durable_object_namespaces.is_empty() {
msg.push_str(&format!(
"implementing these Durable Object namespaces\n {}\n",
durable_object_namespaces.join("\n ")
));
}

StdErr::success(&msg);
if out == Output::Json {
StdOut::as_json(&PublishOutput {
success: true,
name: target_name,
urls,
schedules,
durable_object_namespaces,
});
}
}

// We don't want folks setting their bucket to the top level directory,
// which is where wrangler commands are always called from.
pub fn validate_bucket_location(bucket: &PathBuf) -> Result<(), failure::Error> {
Expand Down
Loading

0 comments on commit 6eac698

Please sign in to comment.