var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
  DEFAULT_DISABLE_REQUEST_COMPRESSION: () => DEFAULT_DISABLE_REQUEST_COMPRESSION,
  DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES: () => DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES,
  NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS: () => NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS,
  NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME: () => NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME,
  NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME: () => NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME,
  NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS: () => NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS,
  NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME: () => NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME,
  NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME: () => NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME,
  compressionMiddleware: () => compressionMiddleware,
  compressionMiddlewareOptions: () => compressionMiddlewareOptions,
  getCompressionPlugin: () => getCompressionPlugin,
  resolveCompressionConfig: () => resolveCompressionConfig
});
module.exports = __toCommonJS(src_exports);

// src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.ts
var import_util_config_provider = require("@smithy/util-config-provider");
var NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME = "AWS_DISABLE_REQUEST_COMPRESSION";
var NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME = "disable_request_compression";
var DEFAULT_DISABLE_REQUEST_COMPRESSION = false;
var NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS = {
  environmentVariableSelector: (env) => (0, import_util_config_provider.booleanSelector)(env, NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME, import_util_config_provider.SelectorType.ENV),
  configFileSelector: (profile) => (0, import_util_config_provider.booleanSelector)(profile, NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME, import_util_config_provider.SelectorType.CONFIG),
  default: DEFAULT_DISABLE_REQUEST_COMPRESSION
};

// src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.ts

var NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME = "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES";
var NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME = "request_min_compression_size_bytes";
var DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES = 10240;
var NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS = {
  environmentVariableSelector: (env) => (0, import_util_config_provider.numberSelector)(env, NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME, import_util_config_provider.SelectorType.ENV),
  configFileSelector: (profile) => (0, import_util_config_provider.numberSelector)(profile, NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME, import_util_config_provider.SelectorType.CONFIG),
  default: DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES
};

// src/compressionMiddleware.ts
var import_protocol_http = require("@smithy/protocol-http");

// src/compressStream.ts
var import_zlib = require("zlib");
var compressStream = /* @__PURE__ */ __name(async (body) => body.pipe((0, import_zlib.createGzip)()), "compressStream");

// src/compressString.ts
var import_util_utf8 = require("@smithy/util-utf8");
var import_util = require("util");

var gzipAsync = (0, import_util.promisify)(import_zlib.gzip);
var compressString = /* @__PURE__ */ __name(async (body) => {
  try {
    const compressedBuffer = await gzipAsync((0, import_util_utf8.toUint8Array)(body || ""));
    return (0, import_util_utf8.toUint8Array)(compressedBuffer);
  } catch (err) {
    throw new Error("Failure during compression: " + err.message);
  }
}, "compressString");

// src/constants.ts
var CLIENT_SUPPORTED_ALGORITHMS = ["gzip" /* GZIP */];

// src/isStreaming.ts
var import_is_array_buffer = require("@smithy/is-array-buffer");
var isStreaming = /* @__PURE__ */ __name((body) => body !== void 0 && typeof body !== "string" && !ArrayBuffer.isView(body) && !(0, import_is_array_buffer.isArrayBuffer)(body), "isStreaming");

// src/compressionMiddleware.ts
var compressionMiddleware = /* @__PURE__ */ __name((config, middlewareConfig) => (next) => async (args) => {
  if (!import_protocol_http.HttpRequest.isInstance(args.request)) {
    return next(args);
  }
  const disableRequestCompression = await config.disableRequestCompression();
  if (disableRequestCompression) {
    return next(args);
  }
  const { request } = args;
  const { body, headers } = request;
  const { encodings, streamRequiresLength } = middlewareConfig;
  let updatedBody = body;
  let updatedHeaders = headers;
  for (const algorithm of encodings) {
    if (CLIENT_SUPPORTED_ALGORITHMS.includes(algorithm)) {
      let isRequestCompressed = false;
      if (isStreaming(body)) {
        if (!streamRequiresLength) {
          updatedBody = await compressStream(body);
          isRequestCompressed = true;
        } else {
          throw new Error("Compression is not supported for streaming blobs that require a length.");
        }
      } else {
        const bodyLength = config.bodyLengthChecker(body);
        const requestMinCompressionSizeBytes = await config.requestMinCompressionSizeBytes();
        if (bodyLength && bodyLength >= requestMinCompressionSizeBytes) {
          updatedBody = await compressString(body);
          isRequestCompressed = true;
        }
      }
      if (isRequestCompressed) {
        if (headers["Content-Encoding"]) {
          updatedHeaders = {
            ...headers,
            "Content-Encoding": `${headers["Content-Encoding"]},${algorithm}`
          };
        } else {
          updatedHeaders = { ...headers, "Content-Encoding": algorithm };
        }
        break;
      }
    }
  }
  return next({
    ...args,
    request: {
      ...request,
      body: updatedBody,
      headers: updatedHeaders
    }
  });
}, "compressionMiddleware");
var compressionMiddlewareOptions = {
  name: "compressionMiddleware",
  step: "build",
  tags: ["REQUEST_BODY_COMPRESSION", "GZIP"],
  override: true,
  priority: "high"
};

// src/getCompressionPlugin.ts
var getCompressionPlugin = /* @__PURE__ */ __name((config, middlewareConfig) => ({
  applyToStack: (clientStack) => {
    clientStack.add(compressionMiddleware(config, middlewareConfig), compressionMiddlewareOptions);
  }
}), "getCompressionPlugin");

// src/resolveCompressionConfig.ts
var import_util_middleware = require("@smithy/util-middleware");
var resolveCompressionConfig = /* @__PURE__ */ __name((input) => ({
  ...input,
  disableRequestCompression: (0, import_util_middleware.normalizeProvider)(input.disableRequestCompression),
  requestMinCompressionSizeBytes: async () => {
    const requestMinCompressionSizeBytes = await (0, import_util_middleware.normalizeProvider)(input.requestMinCompressionSizeBytes)();
    if (requestMinCompressionSizeBytes < 0 || requestMinCompressionSizeBytes > 10485760) {
      throw new RangeError(
        `The value for requestMinCompressionSizeBytes must be between 0 and 10485760 inclusive. The provided value ${requestMinCompressionSizeBytes} is outside this range."`
      );
    }
    return requestMinCompressionSizeBytes;
  }
}), "resolveCompressionConfig");
// Annotate the CommonJS export names for ESM import in node:

0 && (module.exports = {
  NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME,
  NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME,
  DEFAULT_DISABLE_REQUEST_COMPRESSION,
  NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS,
  NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME,
  NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME,
  DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES,
  NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS,
  compressionMiddleware,
  compressionMiddlewareOptions,
  getCompressionPlugin,
  resolveCompressionConfig
});

