Skip to main content

quickjs_jit_stdlib/llrt/llrt_compression/
streaming.rs

1// Vendored from LLRT d9fd873e793c (libs/llrt_compression/src/streaming.rs); module paths and
2// feature names adapted by scripts/import-stdlib.py.
3// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0
5
6use std::io::{self, Write};
7
8#[cfg(all(
9    not(feature = "compression-brotli-c"),
10    feature = "compression-brotli-rust"
11))]
12use brotli as brotlic;
13
14/// Streaming decompressor that maintains state across chunks
15pub enum StreamingDecoder {
16    #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
17    Gzip(flate2::write::GzDecoder<Vec<u8>>),
18    #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
19    Deflate(flate2::write::ZlibDecoder<Vec<u8>>),
20    #[cfg(any(feature = "compression-zstd-c", feature = "compression-zstd-rust"))]
21    Zstd(zstd::stream::write::Decoder<'static, Vec<u8>>),
22    #[cfg(any(feature = "compression-brotli-c", feature = "compression-brotli-rust"))]
23    Brotli(brotlic::DecompressorWriter<Vec<u8>>),
24    Identity,
25}
26
27impl StreamingDecoder {
28    pub fn new(encoding: &str) -> io::Result<Self> {
29        match encoding {
30            #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
31            "gzip" => Ok(Self::Gzip(flate2::write::GzDecoder::new(Vec::new()))),
32            #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
33            "deflate" => Ok(Self::Deflate(flate2::write::ZlibDecoder::new(Vec::new()))),
34            #[cfg(any(feature = "compression-zstd-c", feature = "compression-zstd-rust"))]
35            "zstd" => Ok(Self::Zstd(zstd::stream::write::Decoder::new(Vec::new())?)),
36            #[cfg(feature = "compression-brotli-c")]
37            "br" => Ok(Self::Brotli(brotlic::DecompressorWriter::new(Vec::new()))),
38            #[cfg(all(
39                not(feature = "compression-brotli-c"),
40                feature = "compression-brotli-rust"
41            ))]
42            "br" => Ok(Self::Brotli(brotlic::DecompressorWriter::new(
43                Vec::new(),
44                8_096,
45            ))),
46            "" | "identity" => Ok(Self::Identity),
47            _ => Err(io::Error::new(
48                io::ErrorKind::InvalidInput,
49                format!("unsupported encoding: {}", encoding),
50            )),
51        }
52    }
53
54    /// Decompress a chunk of data, returning the decompressed output
55    pub fn decompress_chunk(&mut self, input: &[u8]) -> io::Result<Vec<u8>> {
56        match self {
57            Self::Identity => Ok(input.to_vec()),
58            #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
59            Self::Gzip(decoder) => {
60                decoder.write_all(input)?;
61                decoder.flush()?;
62                Ok(std::mem::take(decoder.get_mut()))
63            }
64            #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
65            Self::Deflate(decoder) => {
66                decoder.write_all(input)?;
67                decoder.flush()?;
68                Ok(std::mem::take(decoder.get_mut()))
69            }
70            #[cfg(any(feature = "compression-zstd-c", feature = "compression-zstd-rust"))]
71            Self::Zstd(decoder) => {
72                decoder.write_all(input)?;
73                decoder.flush()?;
74                Ok(std::mem::take(decoder.get_mut()))
75            }
76            #[cfg(any(feature = "compression-brotli-c", feature = "compression-brotli-rust"))]
77            Self::Brotli(decoder) => {
78                decoder.write_all(input)?;
79                decoder.flush()?;
80                Ok(std::mem::take(decoder.get_mut()))
81            }
82        }
83    }
84
85    /// Finish decompression and return any remaining data
86    pub fn finish(self) -> io::Result<Vec<u8>> {
87        match self {
88            Self::Identity => Ok(Vec::new()),
89            #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
90            Self::Gzip(decoder) => decoder.finish(),
91            #[cfg(any(feature = "compression-flate2-c", feature = "compression-flate2-rust"))]
92            Self::Deflate(decoder) => decoder.finish(),
93            #[cfg(any(feature = "compression-zstd-c", feature = "compression-zstd-rust"))]
94            Self::Zstd(decoder) => Ok(decoder.into_inner()),
95            #[cfg(feature = "compression-brotli-c")]
96            Self::Brotli(decoder) => decoder
97                .into_inner()
98                .map_err(|e| io::Error::other(e.to_string())),
99            #[cfg(all(
100                not(feature = "compression-brotli-c"),
101                feature = "compression-brotli-rust"
102            ))]
103            Self::Brotli(decoder) => decoder
104                .into_inner()
105                .map_err(|_| io::Error::other("brotli decompression failed")),
106        }
107    }
108}