@valkey/valkey-glide
Version:
General Language Independent Driver for the Enterprise (GLIDE) for Valkey
66 lines (65 loc) • 2.7 kB
JavaScript
;
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompressionBackend = void 0;
exports.validateCompressionConfiguration = validateCompressionConfiguration;
exports.compressionConfigToProtobuf = compressionConfigToProtobuf;
const Errors_1 = require("./Errors");
/**
* Compression backend to use for automatic value compression.
*/
var CompressionBackend;
(function (CompressionBackend) {
/** Use zstd compression backend. */
CompressionBackend[CompressionBackend["ZSTD"] = 0] = "ZSTD";
/** Use lz4 compression backend. */
CompressionBackend[CompressionBackend["LZ4"] = 1] = "LZ4";
})(CompressionBackend || (exports.CompressionBackend = CompressionBackend = {}));
/** Minimum allowed value for minCompressionSize (header size + 1). */
const MIN_COMPRESSED_SIZE = 6;
/** Default minimum size in bytes for values to be compressed. */
const DEFAULT_MIN_COMPRESSION_SIZE = 64;
/**
* Validates a CompressionConfiguration and throws ConfigurationError if invalid.
* @internal
*/
function validateCompressionConfiguration(config) {
const minSize = config.minCompressionSize ?? DEFAULT_MIN_COMPRESSION_SIZE;
if (minSize < MIN_COMPRESSED_SIZE) {
throw new Errors_1.ConfigurationError(`minCompressionSize must be at least ${MIN_COMPRESSED_SIZE} bytes`);
}
if (config.compressionLevel !== undefined &&
!Number.isInteger(config.compressionLevel)) {
throw new Errors_1.ConfigurationError("compressionLevel must be an integer");
}
if (config.maxDecompressedSize !== undefined &&
config.maxDecompressedSize <= 0) {
throw new Errors_1.ConfigurationError("maxDecompressedSize must be positive");
}
}
/**
* Converts a CompressionConfiguration to the protobuf format.
* @internal
*/
function compressionConfigToProtobuf(config,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
connection_request) {
validateCompressionConfiguration(config);
const proto = {
enabled: config.enabled,
backend: config.backend ?? CompressionBackend.ZSTD,
minCompressionSize: config.minCompressionSize ?? DEFAULT_MIN_COMPRESSION_SIZE,
};
if (config.compressionLevel !== undefined) {
proto.compressionLevel = config.compressionLevel;
}
// Handle maxDecompressedSize:
// - undefined = don't set field, let Rust use its default (512MB)
// - number > 0 = use that value
if (config.maxDecompressedSize !== undefined) {
proto.maxDecompressedSize = config.maxDecompressedSize;
}
return connection_request.CompressionConfig.create(proto);
}