Skip to content

Commit cce57c4

Browse files
committed
add more algorithms
1 parent 3a3412c commit cce57c4

File tree

1 file changed

+38
-5
lines changed
  • apollo-router/src/axum_factory/compression

1 file changed

+38
-5
lines changed

apollo-router/src/axum_factory/compression/mod.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use brotli::enc::BrotliEncoderParams;
12
use bytes::{Bytes, BytesMut};
3+
use flate2::Compression;
24
use futures::{Stream, StreamExt};
35
use tokio::sync::mpsc;
46
use tokio_stream::wrappers::ReceiverStream;
57
use tower::BoxError;
68

79
use self::{
8-
codec::{DeflateEncoder, Encode, GzipEncoder},
10+
codec::{BrotliEncoder, DeflateEncoder, Encode, GzipEncoder, ZstdEncoder},
911
util::PartialBuffer,
1012
};
1113

@@ -14,16 +16,41 @@ pub(crate) mod unshared;
1416
pub(crate) mod util;
1517

1618
pub(crate) enum Compressor {
17-
//Identity,
1819
Deflate(DeflateEncoder),
1920
Gzip(GzipEncoder),
20-
//Brotli(BrotliEncoder),
21-
//Zstd,
22-
//others?
21+
Brotli(BrotliEncoder),
22+
Zstd(ZstdEncoder),
2323
}
2424

2525
//FIXME: we should call finish at the end
2626
impl Compressor {
27+
pub(crate) fn new<'a, It: 'a>(it: It) -> Option<Self>
28+
where
29+
It: Iterator<Item = &'a str>,
30+
{
31+
for s in it {
32+
match s {
33+
"gzip" => return Some(Compressor::Gzip(GzipEncoder::new(Compression::fast()))),
34+
"deflate" => {
35+
return Some(Compressor::Deflate(
36+
DeflateEncoder::new(Compression::fast()),
37+
))
38+
}
39+
// FIXME: find the "fast" brotli encoder params
40+
"br" => {
41+
return Some(Compressor::Brotli(BrotliEncoder::new(
42+
BrotliEncoderParams::default(),
43+
)))
44+
}
45+
"zstd" => {
46+
return Some(Compressor::Zstd(ZstdEncoder::new(zstd_safe::min_c_level())))
47+
}
48+
_ => {}
49+
}
50+
}
51+
None
52+
}
53+
2754
pub(crate) fn process(
2855
mut self,
2956
mut stream: hyper::Body,
@@ -110,6 +137,8 @@ impl Encode for Compressor {
110137
match self {
111138
Compressor::Deflate(e) => e.encode(input, output),
112139
Compressor::Gzip(e) => e.encode(input, output),
140+
Compressor::Brotli(e) => e.encode(input, output),
141+
Compressor::Zstd(e) => e.encode(input, output),
113142
}
114143
}
115144

@@ -120,6 +149,8 @@ impl Encode for Compressor {
120149
match self {
121150
Compressor::Deflate(e) => e.flush(output),
122151
Compressor::Gzip(e) => e.flush(output),
152+
Compressor::Brotli(e) => e.flush(output),
153+
Compressor::Zstd(e) => e.flush(output),
123154
}
124155
}
125156

@@ -130,6 +161,8 @@ impl Encode for Compressor {
130161
match self {
131162
Compressor::Deflate(e) => e.finish(output),
132163
Compressor::Gzip(e) => e.finish(output),
164+
Compressor::Brotli(e) => e.finish(output),
165+
Compressor::Zstd(e) => e.finish(output),
133166
}
134167
}
135168
}

0 commit comments

Comments
 (0)