|
1 |
| -// All code from this module is extracted from https://github.com/Nemo157/async-compression and is under MIT or Apache-2 licence |
2 |
| -// it will be removed when we find a long lasting solution to https://github.com/Nemo157/async-compression/issues/154 |
3 |
| -//#[macro_use] |
4 |
| -//mod macros; |
| 1 | +use bytes::{Bytes, BytesMut}; |
| 2 | +use futures::{Stream, StreamExt}; |
| 3 | +use tokio::sync::mpsc; |
| 4 | +use tokio_stream::wrappers::ReceiverStream; |
| 5 | +use tower::BoxError; |
| 6 | + |
| 7 | +use self::{ |
| 8 | + codec::{DeflateEncoder, Encode, GzipEncoder}, |
| 9 | + util::PartialBuffer, |
| 10 | +}; |
| 11 | + |
5 | 12 | pub(crate) mod codec;
|
6 | 13 | pub(crate) mod unshared;
|
7 | 14 | pub(crate) mod util;
|
| 15 | + |
| 16 | +pub(crate) enum Compressor { |
| 17 | + //Identity, |
| 18 | + Deflate(DeflateEncoder), |
| 19 | + Gzip(GzipEncoder), |
| 20 | + //Brotli(BrotliEncoder), |
| 21 | + //Zstd, |
| 22 | + //others? |
| 23 | +} |
| 24 | + |
| 25 | +//FIXME: we should call finish at the end |
| 26 | +impl Compressor { |
| 27 | + pub(crate) fn process( |
| 28 | + mut self, |
| 29 | + mut stream: hyper::Body, |
| 30 | + ) -> impl Stream<Item = Result<Bytes, BoxError>> |
| 31 | +where { |
| 32 | + let (tx, rx) = mpsc::channel(10); |
| 33 | + |
| 34 | + tokio::task::spawn(async move { |
| 35 | + while let Some(data) = stream.next().await { |
| 36 | + match data { |
| 37 | + Err(e) => { |
| 38 | + tx.send(Err(e.into())).await; |
| 39 | + } |
| 40 | + Ok(data) => { |
| 41 | + let mut buf = BytesMut::zeroed(1024); |
| 42 | + let mut written = 0usize; |
| 43 | + |
| 44 | + let mut partial_input = PartialBuffer::new(&*data); |
| 45 | + loop { |
| 46 | + let mut partial_output = PartialBuffer::new(&mut buf); |
| 47 | + partial_output.advance(written); |
| 48 | + |
| 49 | + match self.encode(&mut partial_input, &mut partial_output) { |
| 50 | + Err(e) => panic!("{e:?}"), |
| 51 | + Ok(()) => {} |
| 52 | + } |
| 53 | + |
| 54 | + let read = partial_input.written().len(); |
| 55 | + written += partial_output.written().len(); |
| 56 | + println!("encode: read from input: {read}, written = {written}"); |
| 57 | + |
| 58 | + if !partial_input.unwritten().is_empty() { |
| 59 | + // there was not enough space in the output buffer to compress everything, |
| 60 | + // so we resize and add more data |
| 61 | + if partial_output.unwritten().is_empty() { |
| 62 | + let _ = partial_output.into_inner(); |
| 63 | + buf.reserve(written); |
| 64 | + } |
| 65 | + } else { |
| 66 | + // FIXME: what happens if we try to flush in a full buffer |
| 67 | + match self.flush(&mut partial_output) { |
| 68 | + Err(e) => panic!("{e:?}"), |
| 69 | + Ok(_) => { |
| 70 | + let flushed = partial_output.written().len() - written; |
| 71 | + println!("flush with buffer of size {flushed}"); |
| 72 | + let _ = partial_output.into_inner(); |
| 73 | + buf.resize(flushed, 0); |
| 74 | + tx.send(Ok(buf.freeze())).await; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + let buf = BytesMut::zeroed(64); |
| 85 | + let mut partial_output = PartialBuffer::new(buf); |
| 86 | + |
| 87 | + match self.finish(&mut partial_output) { |
| 88 | + Err(e) => panic!("{e:?}"), |
| 89 | + Ok(b) => { |
| 90 | + let len = partial_output.written().len(); |
| 91 | + println!("finish with buffer of size {}", len); |
| 92 | + |
| 93 | + let mut buf = partial_output.into_inner(); |
| 94 | + buf.resize(len, 0); |
| 95 | + tx.send(Ok(buf.freeze())).await; |
| 96 | + } |
| 97 | + } |
| 98 | + //tx.send(partial_output.into_inner().freeze()); |
| 99 | + }); |
| 100 | + ReceiverStream::new(rx) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Encode for Compressor { |
| 105 | + fn encode( |
| 106 | + &mut self, |
| 107 | + input: &mut PartialBuffer<impl AsRef<[u8]>>, |
| 108 | + output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, |
| 109 | + ) -> std::io::Result<()> { |
| 110 | + match self { |
| 111 | + Compressor::Deflate(e) => e.encode(input, output), |
| 112 | + Compressor::Gzip(e) => e.encode(input, output), |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + fn flush( |
| 117 | + &mut self, |
| 118 | + output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, |
| 119 | + ) -> std::io::Result<bool> { |
| 120 | + match self { |
| 121 | + Compressor::Deflate(e) => e.flush(output), |
| 122 | + Compressor::Gzip(e) => e.flush(output), |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + fn finish( |
| 127 | + &mut self, |
| 128 | + output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>, |
| 129 | + ) -> std::io::Result<bool> { |
| 130 | + match self { |
| 131 | + Compressor::Deflate(e) => e.finish(output), |
| 132 | + Compressor::Gzip(e) => e.finish(output), |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments