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

Commit

Permalink
feat: add kv_namespaces bindings for Rust and javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed Jul 2, 2019
1 parent d7fbd78 commit 64808c5
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ fs_extra = "1.1.0"

[features]
vendored-openssl = ['openssl/vendored']
slow_tests = []
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
- template: ci/azure-install-node.yml
- script: cargo install cargo-generate
displayName: "cargo install cargo-generate"
- script: cargo test --locked
displayName: "cargo test --locked"
- script: cargo test --locked --features slow_tests
displayName: "cargo test --locked --features slow_tests"
env:
RUST_LOG: warn,wrangler=info

Expand Down
44 changes: 44 additions & 0 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
pub mod wranglerjs;

use crate::settings::binding::Binding;
use crate::settings::metadata;
use crate::settings::project::KvNamespace;
use crate::settings::project::{Project, ProjectType};
use crate::{commands, install};
use std::env;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::Command;

use crate::terminal::message;

fn write_kv_metadata(maybe_kv_namespaces: &Option<Vec<KvNamespace>>) -> Result<(), failure::Error> {
let mut bindings = vec![];

if let Some(kv_namespaces) = maybe_kv_namespaces {
for namespace in kv_namespaces {
bindings.push(Binding::new_kv_namespace(
namespace.binding.clone(), // local_binding
namespace.id.clone(), // namespace_id
));
}
};

let metadata = serde_json::to_string(&metadata::Metadata {
body_part: "script".to_string(),
bindings,
})
.expect("could not generate metadata");

let worker_path = env::current_dir()
.expect("could not get cwd")
.join("worker");
if !worker_path.clone().exists() {
fs::create_dir(&worker_path)?;
}

let metadata_file = worker_path.join("metadata.json");

File::create(&metadata_file)
.unwrap()
.write_all(metadata.as_bytes())?;

Ok(())
}

pub fn build(project: &Project) -> Result<(), failure::Error> {
let project_type = &project.project_type;
match project_type {
ProjectType::JavaScript => {
write_kv_metadata(&project.kv_namespaces).expect("could not write metadata");

message::info("JavaScript project found. Skipping unnecessary build!")
}
ProjectType::Rust => {
write_kv_metadata(&project.kv_namespaces).expect("could not write metadata");

let tool_name = "wasm-pack";
let binary_path = install::install(tool_name, "rustwasm")?.binary(tool_name)?;
let args = ["build", "--target", "no-modules"];
Expand Down
67 changes: 67 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,73 @@ fn it_builds_with_webpack_wast() {
cleanup(fixture);
}

#[test]
fn it_builds_with_kv_metadata_webpack() {
let fixture = "kv_metadata_webpack";
create_temporary_copy(fixture);

settings! {fixture, r#"
type = "webpack"
[[kv-namespaces]]
binding = "bind"
id = "bind"
"#};

build(fixture);
assert!(fixture_out_path(fixture).join("metadata.json").exists());

let metadata = fs::read_to_string(fixture_out_path(fixture).join("metadata.json"))
.expect("could not read metadata");
assert_eq!(metadata, r#"{"body_part":"script","bindings":[{"type":"kv_namespace","name":"bind","namespace_id":"bind"}]}"#);
cleanup(fixture);
}

#[test]
fn it_builds_with_kv_metadata_js() {
let fixture = "kv_metadata_js";
create_temporary_copy(fixture);

settings! {fixture, r#"
type = "javascript"
[[kv-namespaces]]
binding = "bind"
id = "bind"
"#};

build(fixture);
assert!(fixture_out_path(fixture).join("metadata.json").exists());

let metadata = fs::read_to_string(fixture_out_path(fixture).join("metadata.json"))
.expect("could not read metadata");
assert_eq!(metadata, r#"{"body_part":"script","bindings":[{"type":"kv_namespace","name":"bind","namespace_id":"bind"}]}"#);
cleanup(fixture);
}

#[cfg(feature = "slow_tests")]
#[test]
fn it_builds_with_kv_metadata_rust() {
let fixture = "kv_metadata_rust";
create_temporary_copy(fixture);

settings! {fixture, r#"
type = "rust"
[[kv-namespaces]]
binding = "bind"
id = "bind"
"#};

build(fixture);
assert!(fixture_out_path(fixture).join("metadata.json").exists());

let metadata = fs::read_to_string(fixture_out_path(fixture).join("metadata.json"))
.expect("could not read metadata");
assert_eq!(metadata, r#"{"body_part":"script","bindings":[{"type":"kv_namespace","name":"bind","namespace_id":"bind"}]}"#);
cleanup(fixture);
}

fn cleanup(fixture: &str) {
let path = fixture_path(fixture);
assert!(path.exists(), format!("{:?} does not exist", path));
Expand Down
1 change: 1 addition & 0 deletions tests/kv_metadata_js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//
3 changes: 3 additions & 0 deletions tests/kv_metadata_js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "./index.js"
}
1 change: 1 addition & 0 deletions tests/kv_metadata_rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
4 changes: 4 additions & 0 deletions tests/kv_metadata_rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/kv_metadata_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "kv_metadata_rust"
version = "0.1.0"
authors = ["Sven Sauleau <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasm-bindgen = "0.2"

1 change: 1 addition & 0 deletions tests/kv_metadata_rust/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//
3 changes: 3 additions & 0 deletions tests/kv_metadata_rust/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "./index.js"
}
Empty file.
1 change: 1 addition & 0 deletions tests/kv_metadata_webpack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//
3 changes: 3 additions & 0 deletions tests/kv_metadata_webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "./index.js"
}

0 comments on commit 64808c5

Please sign in to comment.