2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-01-24 12:03:06 -05:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2022-01-24 12:03:06 -05:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::ToJsBuffer;
|
2022-06-14 12:10:28 -04:00
|
|
|
use flate2::write::DeflateDecoder;
|
|
|
|
use flate2::write::DeflateEncoder;
|
2022-01-24 12:03:06 -05:00
|
|
|
use flate2::write::GzDecoder;
|
|
|
|
use flate2::write::GzEncoder;
|
|
|
|
use flate2::write::ZlibDecoder;
|
|
|
|
use flate2::write::ZlibEncoder;
|
|
|
|
use flate2::Compression;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CompressionResource(RefCell<Inner>);
|
|
|
|
|
2022-06-14 12:10:28 -04:00
|
|
|
/// https://wicg.github.io/compression/#supported-formats
|
2022-01-24 12:03:06 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Inner {
|
|
|
|
DeflateDecoder(ZlibDecoder<Vec<u8>>),
|
|
|
|
DeflateEncoder(ZlibEncoder<Vec<u8>>),
|
2022-06-14 12:10:28 -04:00
|
|
|
DeflateRawDecoder(DeflateDecoder<Vec<u8>>),
|
|
|
|
DeflateRawEncoder(DeflateEncoder<Vec<u8>>),
|
2022-01-24 12:03:06 -05:00
|
|
|
GzDecoder(GzDecoder<Vec<u8>>),
|
|
|
|
GzEncoder(GzEncoder<Vec<u8>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for CompressionResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"compression".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-01-24 12:03:06 -05:00
|
|
|
pub fn op_compression_new(
|
|
|
|
state: &mut OpState,
|
2023-03-03 08:34:10 -05:00
|
|
|
format: &str,
|
2022-01-24 12:03:06 -05:00
|
|
|
is_decoder: bool,
|
2022-05-13 04:36:31 -04:00
|
|
|
) -> ResourceId {
|
2022-01-24 12:03:06 -05:00
|
|
|
let w = Vec::new();
|
2023-03-03 08:34:10 -05:00
|
|
|
let inner = match (format, is_decoder) {
|
2022-01-24 12:03:06 -05:00
|
|
|
("deflate", true) => Inner::DeflateDecoder(ZlibDecoder::new(w)),
|
|
|
|
("deflate", false) => {
|
|
|
|
Inner::DeflateEncoder(ZlibEncoder::new(w, Compression::default()))
|
|
|
|
}
|
2022-06-14 12:10:28 -04:00
|
|
|
("deflate-raw", true) => Inner::DeflateRawDecoder(DeflateDecoder::new(w)),
|
|
|
|
("deflate-raw", false) => {
|
|
|
|
Inner::DeflateRawEncoder(DeflateEncoder::new(w, Compression::default()))
|
|
|
|
}
|
2022-01-24 12:03:06 -05:00
|
|
|
("gzip", true) => Inner::GzDecoder(GzDecoder::new(w)),
|
|
|
|
("gzip", false) => {
|
|
|
|
Inner::GzEncoder(GzEncoder::new(w, Compression::default()))
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let resource = CompressionResource(RefCell::new(inner));
|
2022-05-13 04:36:31 -04:00
|
|
|
state.resource_table.add(resource)
|
2022-01-24 12:03:06 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-01-24 12:03:06 -05:00
|
|
|
pub fn op_compression_write(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
2022-09-07 06:51:47 -04:00
|
|
|
input: &[u8],
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2022-01-24 12:03:06 -05:00
|
|
|
let resource = state.resource_table.get::<CompressionResource>(rid)?;
|
|
|
|
let mut inner = resource.0.borrow_mut();
|
|
|
|
let out: Vec<u8> = match &mut *inner {
|
|
|
|
Inner::DeflateDecoder(d) => {
|
2022-09-07 06:51:47 -04:00
|
|
|
d.write_all(input)?;
|
2022-01-24 12:03:06 -05:00
|
|
|
d.flush()?;
|
|
|
|
d.get_mut().drain(..)
|
|
|
|
}
|
|
|
|
Inner::DeflateEncoder(d) => {
|
2022-09-07 06:51:47 -04:00
|
|
|
d.write_all(input)?;
|
2022-01-24 12:03:06 -05:00
|
|
|
d.flush()?;
|
|
|
|
d.get_mut().drain(..)
|
|
|
|
}
|
2022-06-14 12:10:28 -04:00
|
|
|
Inner::DeflateRawDecoder(d) => {
|
2022-09-07 06:51:47 -04:00
|
|
|
d.write_all(input)?;
|
2022-06-14 12:10:28 -04:00
|
|
|
d.flush()?;
|
|
|
|
d.get_mut().drain(..)
|
|
|
|
}
|
|
|
|
Inner::DeflateRawEncoder(d) => {
|
2022-09-07 06:51:47 -04:00
|
|
|
d.write_all(input)?;
|
2022-06-14 12:10:28 -04:00
|
|
|
d.flush()?;
|
|
|
|
d.get_mut().drain(..)
|
|
|
|
}
|
2022-01-24 12:03:06 -05:00
|
|
|
Inner::GzDecoder(d) => {
|
2022-09-07 06:51:47 -04:00
|
|
|
d.write_all(input)?;
|
2022-01-24 12:03:06 -05:00
|
|
|
d.flush()?;
|
|
|
|
d.get_mut().drain(..)
|
|
|
|
}
|
|
|
|
Inner::GzEncoder(d) => {
|
2022-09-07 06:51:47 -04:00
|
|
|
d.write_all(input)?;
|
2022-01-24 12:03:06 -05:00
|
|
|
d.flush()?;
|
|
|
|
d.get_mut().drain(..)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.collect();
|
|
|
|
Ok(out.into())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-01-24 12:03:06 -05:00
|
|
|
pub fn op_compression_finish(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2022-01-24 12:03:06 -05:00
|
|
|
let resource = state.resource_table.take::<CompressionResource>(rid)?;
|
|
|
|
let resource = Rc::try_unwrap(resource).unwrap();
|
|
|
|
let inner = resource.0.into_inner();
|
|
|
|
let out: Vec<u8> = match inner {
|
|
|
|
Inner::DeflateDecoder(d) => d.finish()?,
|
|
|
|
Inner::DeflateEncoder(d) => d.finish()?,
|
2022-06-14 12:10:28 -04:00
|
|
|
Inner::DeflateRawDecoder(d) => d.finish()?,
|
|
|
|
Inner::DeflateRawEncoder(d) => d.finish()?,
|
2022-01-24 12:03:06 -05:00
|
|
|
Inner::GzDecoder(d) => d.finish()?,
|
|
|
|
Inner::GzEncoder(d) => d.finish()?,
|
|
|
|
};
|
|
|
|
Ok(out.into())
|
|
|
|
}
|