mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
feat: support brotli compression for fetch API (#4082)
This commit is contained in:
parent
8b646e8657
commit
14129b6c8f
5 changed files with 40 additions and 43 deletions
46
Cargo.lock
generated
46
Cargo.lock
generated
|
@ -33,6 +33,21 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[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"
|
||||
|
@ -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",
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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_));
|
||||
};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
gzip
|
||||
brotli
|
||||
console.log('gzip')
|
||||
console.log('brotli');
|
||||
|
|
|
@ -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())
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue