"use strict";
// Copyright 2021-2025 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressionBrotli = exports.compressionGzip = void 0;
const zlib = require("node:zlib");
const node_util_1 = require("node:util");
const connect_1 = require("@connectrpc/connect");
const node_error_js_1 = require("./node-error.js");
const gzip = (0, node_util_1.promisify)(zlib.gzip);
const gunzip = (0, node_util_1.promisify)(zlib.gunzip);
const brotliCompress = (0, node_util_1.promisify)(zlib.brotliCompress);
const brotliDecompress = (0, node_util_1.promisify)(zlib.brotliDecompress);
/**
 * The gzip compression algorithm, implemented with the Node.js built-in module
 * zlib. This value can be used for the `sendCompression` and `acceptCompression`
 * option of client transports, or for the `acceptCompression` option of server
 * plugins like `fastifyConnectPlugin` from @connectrpc/connect-fastify.
 */
exports.compressionGzip = {
    name: "gzip",
    compress(bytes) {
        return asUint8ArrayArrayBuffer(gzip(bytes, {}));
    },
    decompress(bytes, readMaxBytes) {
        if (bytes.length == 0) {
            return Promise.resolve(new Uint8Array(0));
        }
        return asUint8ArrayArrayBuffer(wrapZLibErrors(gunzip(bytes, {
            maxOutputLength: readMaxBytes,
        }), readMaxBytes));
    },
};
/**
 * The brotli compression algorithm, implemented with the Node.js built-in module
 * zlib. This value can be used for the `sendCompression` and `acceptCompression`
 * option of client transports, or for the `acceptCompression` option of server
 * plugins like `fastifyConnectPlugin` from @connectrpc/connect-fastify.
 */
exports.compressionBrotli = {
    name: "br",
    compress(bytes) {
        return asUint8ArrayArrayBuffer(brotliCompress(bytes, {}));
    },
    decompress(bytes, readMaxBytes) {
        if (bytes.length == 0) {
            return Promise.resolve(new Uint8Array(0));
        }
        return asUint8ArrayArrayBuffer(wrapZLibErrors(brotliDecompress(bytes, {
            maxOutputLength: readMaxBytes,
        }), readMaxBytes));
    },
};
function asUint8ArrayArrayBuffer(bytes) {
    return bytes.then((b) => {
        if (b.buffer instanceof ArrayBuffer) {
            return b;
        }
        return new Uint8Array(b);
    });
}
function wrapZLibErrors(promise, readMaxBytes) {
    return promise.catch((e) => {
        var _a;
        const props = (0, node_error_js_1.getNodeErrorProps)(e);
        let code = connect_1.Code.Internal;
        let message = "decompression failed";
        switch (props.code) {
            case "ERR_BUFFER_TOO_LARGE":
                code = connect_1.Code.ResourceExhausted;
                message = `message is larger than configured readMaxBytes ${readMaxBytes} after decompression`;
                break;
            case "Z_DATA_ERROR":
            case "ERR_PADDING_2":
                code = connect_1.Code.InvalidArgument;
                break;
            default:
                if ((_a = props.code) === null || _a === void 0 ? void 0 : _a.startsWith("ERR__ERROR_FORMAT_")) {
                    code = connect_1.Code.InvalidArgument;
                }
                break;
        }
        return Promise.reject(new connect_1.ConnectError(message, code, undefined, undefined, e));
    });
}
