ferrijs-std 0.2.0

Node and web standard library for the ferrijs QuickJS runtime: WHATWG Streams, Events, AbortController, Buffer, crypto, fs, os, url, zlib and the capability model they enforce (partly derived from awslabs/llrt, Apache-2.0).
Documentation
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::buffer::Buffer;
use crate::context::CtxExtension;
use crate::utils::{bytes::ObjectBytes, result::ResultExt};
use rquickjs::{
    prelude::{Opt, Rest},
    Ctx, Error, Exception, Function, IntoJs, Null, Result, Value,
};

use crate::{define_cb_function, define_sync_function};
use super::{max_output_length, read_to_end_limited};

enum BrotliCommand {
    Compress,
    Decompress,
}

fn brotli_converter<'js>(
    ctx: Ctx<'js>,
    bytes: ObjectBytes<'js>,
    options: Opt<Value<'js>>,
    command: BrotliCommand,
) -> Result<Value<'js>> {
    let src = bytes.as_bytes(&ctx)?;
    let limit = max_output_length(&options)?;

    let dst = match command {
        BrotliCommand::Compress => read_to_end_limited(
            &ctx,
            crate::compression::brotli::encoder(src),
            limit,
            src.len(),
        )?,
        BrotliCommand::Decompress => read_to_end_limited(
            &ctx,
            crate::compression::brotli::decoder(src),
            limit,
            src.len(),
        )?,
    };

    Buffer(dst).into_js(&ctx)
}

define_cb_function!(br_comp, brotli_converter, BrotliCommand::Compress);
define_sync_function!(br_comp_sync, brotli_converter, BrotliCommand::Compress);

define_cb_function!(br_decomp, brotli_converter, BrotliCommand::Decompress);
define_sync_function!(br_decomp_sync, brotli_converter, BrotliCommand::Decompress);