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

Commit

Permalink
Merge branch 'master' into malonso/feat-better-error-printing
Browse files Browse the repository at this point in the history
  • Loading branch information
xortive authored Jul 22, 2019
2 parents 0ebbec1 + ec33a94 commit 431fdaa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ pub fn run_build(project: &Project) -> Result<(), failure::Error> {
.write(&wranglerjs_output)
.expect("could not write bundle to disk");

let mut msg = format!(
"Built successfully, script size is {}",
wranglerjs_output.script_size()
let msg = format!(
"Built successfully, built project size is {}",
wranglerjs_output.project_size()
);
if bundle.has_wasm() {
msg = format!("{} and Wasm size is {}", msg, wranglerjs_output.wasm_size());
}

message::success(&msg);
Ok(())
} else {
Expand Down
72 changes: 55 additions & 17 deletions src/commands/build/wranglerjs/output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::terminal::emoji;
use flate2::write::ZlibEncoder;
use flate2::Compression;
use number_prefix::{NumberPrefix, Prefixed, Standalone};
Expand All @@ -24,50 +25,87 @@ impl WranglerjsOutput {
self.errors.join("\n")
}

pub fn script_size(&self) -> String {
fn project_size_bytes(&self) -> u64 {
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());

//approximation of how projects are gzipped
e.write_all(&self.script.as_bytes())
.expect("could not write buffer");
let compressed_bytes = e.finish();
.expect("could not write script buffer");

match NumberPrefix::decimal(compressed_bytes.unwrap().len() as f64) {
Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
if let Some(wasm) = &self.wasm {
e.write_all(wasm.to_owned().as_bytes())
.expect("could not write wasm buffer");
}

e.finish().expect("failed to compress project").len() as u64
}

pub fn wasm_size(&self) -> String {
let size = self.wasm.to_owned().unwrap().len();
match NumberPrefix::decimal(size as f64) {
fn project_size_message(compressed_size: u64) -> String {
const MAX_PROJECT_SIZE: u64 = 1 << 20; // 1 MiB
const WARN_THRESHOLD: u64 = MAX_PROJECT_SIZE - 81_920; //Warn when less than 80 KiB left to grow, ~92% usage

let bytes_left = MAX_PROJECT_SIZE.checked_sub(compressed_size);

let human_size = match NumberPrefix::binary(compressed_size as f64) {
Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};

let human_leftover = if let Some(bytes_left) = bytes_left {
let msg = match NumberPrefix::binary(bytes_left as f64) {
Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};
Some(msg)
} else {
None
};

match compressed_size {
WARN_THRESHOLD...MAX_PROJECT_SIZE => format!("{}. {2} Your built project is {} away from reaching the 1MiB size limit. {2}", human_size, human_leftover.expect("failed to get leftover bytes"), emoji::WARN),
0...WARN_THRESHOLD => format!("{}.", human_size),
_ => format!("{}. {1} Your built project has grown past the 1MiB size limit and may fail to deploy. {1}", human_size, emoji::WARN)
}
}

pub fn project_size(&self) -> String {
Self::project_size_message(self.project_size_bytes())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_returns_gzip_script_size() {
fn it_warns_over_max_size() {
assert!(WranglerjsOutput::project_size_message(1 << 21).contains("grown past"));
}

#[test]
fn it_warns_near_max_size() {
assert!(WranglerjsOutput::project_size_message((1 << 20) - 4096).contains("reaching"));
}

#[test]
fn it_returns_project_size_with_wasm() {
let wranglerjs_output = WranglerjsOutput {
errors: vec![],
script: "aaaa".to_string(),
wasm: None,
script: "abcdefg".to_string(),
wasm: Some("123456".to_string()),
};

assert_eq!(wranglerjs_output.script_size(), "12 bytes");
assert_eq!(wranglerjs_output.project_size_bytes(), 21);
}

#[test]
fn it_returns_wasm_size() {
fn it_returns_project_size_without_wasm() {
let wranglerjs_output = WranglerjsOutput {
errors: vec![],
script: "".to_string(),
wasm: Some("abc".to_string()),
script: "abcdefg".to_string(),
wasm: None,
};

assert_eq!(wranglerjs_output.wasm_size(), "3 bytes");
assert_eq!(wranglerjs_output.project_size_bytes(), 15);
}
}

0 comments on commit 431fdaa

Please sign in to comment.