summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEnokMan <416828041@qq.com>2020-02-23 08:37:43 -0600
committerGitHub <noreply@github.com>2020-02-23 09:37:43 -0500
commit14129b6c8f3335b215a2d9614c4523e8e3200e82 (patch)
treedc672d36e39e27a725b8e96c36f18bc6eb5048db
parent8b646e8657309e57bd4e907f911529e33e3a05fb (diff)
feat: support brotli compression for fetch API (#4082)
-rw-r--r--Cargo.lock46
-rw-r--r--cli/Cargo.toml3
-rw-r--r--cli/http_util.rs28
-rw-r--r--cli/tests/053_import_compression.out1
-rw-r--r--cli/tests/053_import_compression/main.ts5
5 files changed, 40 insertions, 43 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b4392a8d4..94e5d3240 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -34,6 +34,21 @@ dependencies = [
]
[[package]]
+name = "alloc-no-stdlib"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5192ec435945d87bc2f70992b4d818154b5feede43c09fb7592146374eac90a6"
+
+[[package]]
+name = "alloc-stdlib"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "697ed7edc0f1711de49ce108c541623a0af97c6c60b2f6e2b65229847ac843c2"
+dependencies = [
+ "alloc-no-stdlib",
+]
+
+[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -88,10 +103,11 @@ dependencies = [
[[package]]
name = "async-compression"
-version = "0.2.0"
+version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2c5c52622726d68ec35fec88edfb4ccb862d4f3b3bfa4af2f45142e69ef9b220"
+checksum = "670843da104a1339d46c967382f78d9debb1713aee20e09ba1b25445076f40f4"
dependencies = [
+ "brotli",
"bytes 0.5.4",
"flate2",
"futures-core",
@@ -177,23 +193,24 @@ dependencies = [
]
[[package]]
-name = "brotli-sys"
-version = "0.3.2"
+name = "brotli"
+version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4445dea95f4c2b41cde57cc9fee236ae4dbae88d8fcbdb4750fc1bb5d86aaecd"
+checksum = "7f29919120f08613aadcd4383764e00526fc9f18b6c0895814faeed0dd78613e"
dependencies = [
- "cc",
- "libc",
+ "alloc-no-stdlib",
+ "alloc-stdlib",
+ "brotli-decompressor",
]
[[package]]
-name = "brotli2"
-version = "0.3.2"
+name = "brotli-decompressor"
+version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0cb036c3eade309815c15ddbacec5b22c4d1f3983a774ab2eac2e3e9ea85568e"
+checksum = "2a9f2b517b96b19d8f91c1ff5b1cf498e688850b32eae5d58e02d15c4d4fdc0c"
dependencies = [
- "brotli-sys",
- "libc",
+ "alloc-no-stdlib",
+ "alloc-stdlib",
]
[[package]]
@@ -419,7 +436,6 @@ dependencies = [
"ansi_term",
"atty",
"base64 0.11.0",
- "brotli2",
"byteorder",
"bytes 0.5.4",
"clap",
@@ -1742,9 +1758,9 @@ dependencies = [
[[package]]
name = "reqwest"
-version = "0.10.1"
+version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c0e798e19e258bf6c30a304622e3e9ac820e483b06a1857a026e1f109b113fe4"
+checksum = "bae3fc32eacd4a5200c6b34bd6c057b07fb64f5a1e55bb67d624cc1393354621"
dependencies = [
"async-compression",
"base64 0.11.0",
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index b70826d72..fdca9cceb 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -31,7 +31,6 @@ atty = "0.2.13"
base64 = "0.11.0"
bytes = "0.5.3"
byteorder = "1.3.2"
-brotli2 = "0.3.2"
clap = "2.33.0"
dirs = "2.0.2"
dlopen = "0.1.8"
@@ -47,7 +46,7 @@ notify = { version = "5.0.0-pre.2" }
rand = "0.7.2"
regex = "1.3.1"
remove_dir_all = "0.5.2"
-reqwest = { version = "0.10.1", default-features = false, features = ["rustls-tls", "stream", "gzip"] }
+reqwest = { version = "0.10.2", default-features = false, features = ["rustls-tls", "stream", "gzip", "brotli"] }
ring = "0.16.9"
rustyline = "5.0.6"
serde = { version = "1.0.104", features = ["derive"] }
diff --git a/cli/http_util.rs b/cli/http_util.rs
index 7da2c7dbd..c24792a7f 100644
--- a/cli/http_util.rs
+++ b/cli/http_util.rs
@@ -3,15 +3,12 @@ use crate::deno_error;
use crate::deno_error::DenoError;
use crate::deno_error::ErrorKind;
use crate::version;
-use brotli2::read::BrotliDecoder;
use bytes::Bytes;
use deno_core::ErrBox;
use futures::future::FutureExt;
use reqwest;
use reqwest::header::HeaderMap;
use reqwest::header::HeaderValue;
-use reqwest::header::ACCEPT_ENCODING;
-use reqwest::header::CONTENT_ENCODING;
use reqwest::header::IF_NONE_MATCH;
use reqwest::header::LOCATION;
use reqwest::header::USER_AGENT;
@@ -107,9 +104,7 @@ pub fn fetch_once(
let url = url.clone();
let fut = async move {
- let mut request = client
- .get(url.clone())
- .header(ACCEPT_ENCODING, HeaderValue::from_static("gzip, br"));
+ let mut request = client.get(url.clone());
if let Some(etag) = cached_etag {
let if_none_match_val = HeaderValue::from_str(&etag).unwrap();
@@ -157,26 +152,7 @@ pub fn fetch_once(
return Err(err.into());
}
- let content_encoding = response
- .headers()
- .get(CONTENT_ENCODING)
- .map(|content_encoding| content_encoding.to_str().unwrap().to_owned());
-
- let body;
- if let Some(content_encoding) = content_encoding {
- body = match content_encoding {
- _ if content_encoding == "br" => {
- let full_bytes = response.bytes().await?;
- let mut decoder = BrotliDecoder::new(full_bytes.as_ref());
- let mut body = vec![];
- decoder.read_to_end(&mut body)?;
- body
- }
- _ => response.bytes().await?.to_vec(),
- }
- } else {
- body = response.bytes().await?.to_vec();
- }
+ let body = response.bytes().await?.to_vec();
return Ok(FetchOnceResult::Code(body, headers_));
};
diff --git a/cli/tests/053_import_compression.out b/cli/tests/053_import_compression.out
index 5815b8ae2..371994979 100644
--- a/cli/tests/053_import_compression.out
+++ b/cli/tests/053_import_compression.out
@@ -1,3 +1,4 @@
gzip
brotli
console.log('gzip')
+console.log('brotli');
diff --git a/cli/tests/053_import_compression/main.ts b/cli/tests/053_import_compression/main.ts
index eb19cc75d..b6f7e2c9a 100644
--- a/cli/tests/053_import_compression/main.ts
+++ b/cli/tests/053_import_compression/main.ts
@@ -6,3 +6,8 @@ console.log(
"http://127.0.0.1:4545/cli/tests/053_import_compression/gziped"
).then(res => res.text())
);
+console.log(
+ await fetch(
+ "http://127.0.0.1:4545/cli/tests/053_import_compression/brotli"
+ ).then(res => res.text())
+);