Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@smithy/middleware-compression

Package Overview
Dependencies
Maintainers
2
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smithy/middleware-compression - npm Package Compare versions

Comparing version 2.0.2 to 2.1.0

73

dist-cjs/compressionMiddleware.js

@@ -1,72 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressionMiddlewareOptions = exports.compressionMiddleware = void 0;
const protocol_http_1 = require("@smithy/protocol-http");
const compressStream_1 = require("./compressStream");
const compressString_1 = require("./compressString");
const constants_1 = require("./constants");
const isStreaming_1 = require("./isStreaming");
const compressionMiddleware = (config, middlewareConfig) => (next) => async (args) => {
if (!protocol_http_1.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 (constants_1.CLIENT_SUPPORTED_ALGORITHMS.includes(algorithm)) {
let isRequestCompressed = false;
if ((0, isStreaming_1.isStreaming)(body)) {
if (!streamRequiresLength) {
updatedBody = await (0, compressStream_1.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 (0, compressString_1.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,
},
});
};
exports.compressionMiddleware = compressionMiddleware;
exports.compressionMiddlewareOptions = {
name: "compressionMiddleware",
step: "build",
tags: ["REQUEST_BODY_COMPRESSION", "GZIP"],
override: true,
priority: "high",
};
module.exports = require("./index.js");

@@ -1,37 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressStream = void 0;
const fflate_1 = require("fflate");
const compressStream = async (body) => {
let endCallback;
const asyncGzip = new fflate_1.AsyncGzip();
const compressionStream = new TransformStream({
start(controller) {
asyncGzip.ondata = (err, data, final) => {
if (err) {
controller.error(err);
}
else {
controller.enqueue(data);
if (final) {
if (endCallback)
endCallback();
else
controller.terminate();
}
}
};
},
transform(chunk) {
asyncGzip.push(chunk);
},
flush() {
return new Promise((resolve) => {
endCallback = resolve;
asyncGzip.push(new Uint8Array(0), true);
});
},
});
return body.pipeThrough(compressionStream);
};
exports.compressStream = compressStream;
module.exports = require("./index.js");

@@ -1,6 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressStream = void 0;
const zlib_1 = require("zlib");
const compressStream = async (body) => body.pipe((0, zlib_1.createGzip)());
exports.compressStream = compressStream;
module.exports = require("./index.js");

@@ -1,16 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressString = void 0;
const util_utf8_1 = require("@smithy/util-utf8");
const fflate_1 = require("fflate");
const compressString = async (body) => new Promise((resolve, reject) => {
(0, fflate_1.gzip)((0, util_utf8_1.toUint8Array)(body || ""), (err, data) => {
if (err) {
reject(new Error("Failure during compression: " + err.message));
}
else {
resolve(data);
}
});
});
exports.compressString = compressString;
module.exports = require("./index.js");

@@ -1,17 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressString = void 0;
const util_utf8_1 = require("@smithy/util-utf8");
const util_1 = require("util");
const zlib_1 = require("zlib");
const gzipAsync = (0, util_1.promisify)(zlib_1.gzip);
const compressString = async (body) => {
try {
const compressedBuffer = await gzipAsync((0, util_utf8_1.toUint8Array)(body || ""));
return (0, util_utf8_1.toUint8Array)(compressedBuffer);
}
catch (err) {
throw new Error("Failure during compression: " + err.message);
}
};
exports.compressString = compressString;
module.exports = require("./index.js");

3

dist-cjs/configurations.js

@@ -1,2 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = require("./index.js");

@@ -1,8 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLIENT_SUPPORTED_ALGORITHMS = exports.CompressionAlgorithm = void 0;
var CompressionAlgorithm;
(function (CompressionAlgorithm) {
CompressionAlgorithm["GZIP"] = "gzip";
})(CompressionAlgorithm = exports.CompressionAlgorithm || (exports.CompressionAlgorithm = {}));
exports.CLIENT_SUPPORTED_ALGORITHMS = [CompressionAlgorithm.GZIP];
module.exports = require("./index.js");

@@ -1,10 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCompressionPlugin = void 0;
const compressionMiddleware_1 = require("./compressionMiddleware");
const getCompressionPlugin = (config, middlewareConfig) => ({
applyToStack: (clientStack) => {
clientStack.add((0, compressionMiddleware_1.compressionMiddleware)(config, middlewareConfig), compressionMiddleware_1.compressionMiddlewareOptions);
},
});
exports.getCompressionPlugin = getCompressionPlugin;
module.exports = require("./index.js");

@@ -1,9 +0,186 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS"), exports);
tslib_1.__exportStar(require("./NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS"), exports);
tslib_1.__exportStar(require("./compressionMiddleware"), exports);
tslib_1.__exportStar(require("./configurations"), exports);
tslib_1.__exportStar(require("./getCompressionPlugin"), exports);
tslib_1.__exportStar(require("./resolveCompressionConfig"), exports);
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 = {
DEFAULT_DISABLE_REQUEST_COMPRESSION,
DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES,
NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS,
NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME,
NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME,
NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS,
NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME,
NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME,
compressionMiddleware,
compressionMiddlewareOptions,
getCompressionPlugin,
resolveCompressionConfig
});

@@ -1,6 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isStreaming = void 0;
const is_array_buffer_1 = require("@smithy/is-array-buffer");
const isStreaming = (body) => body !== undefined && typeof body !== "string" && !ArrayBuffer.isView(body) && !(0, is_array_buffer_1.isArrayBuffer)(body);
exports.isStreaming = isStreaming;
module.exports = require("./index.js");

@@ -1,12 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS = exports.DEFAULT_DISABLE_REQUEST_COMPRESSION = exports.NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME = exports.NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME = void 0;
const util_config_provider_1 = require("@smithy/util-config-provider");
exports.NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME = "AWS_DISABLE_REQUEST_COMPRESSION";
exports.NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME = "disable_request_compression";
exports.DEFAULT_DISABLE_REQUEST_COMPRESSION = false;
exports.NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS = {
environmentVariableSelector: (env) => (0, util_config_provider_1.booleanSelector)(env, exports.NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME, util_config_provider_1.SelectorType.ENV),
configFileSelector: (profile) => (0, util_config_provider_1.booleanSelector)(profile, exports.NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME, util_config_provider_1.SelectorType.CONFIG),
default: exports.DEFAULT_DISABLE_REQUEST_COMPRESSION,
};
module.exports = require("./index.js");

@@ -1,12 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS = exports.DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES = exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME = exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME = void 0;
const util_config_provider_1 = require("@smithy/util-config-provider");
exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME = "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES";
exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME = "request_min_compression_size_bytes";
exports.DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES = 10240;
exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS = {
environmentVariableSelector: (env) => (0, util_config_provider_1.numberSelector)(env, exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME, util_config_provider_1.SelectorType.ENV),
configFileSelector: (profile) => (0, util_config_provider_1.numberSelector)(profile, exports.NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME, util_config_provider_1.SelectorType.CONFIG),
default: exports.DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES,
};
module.exports = require("./index.js");

@@ -1,17 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveCompressionConfig = void 0;
const util_middleware_1 = require("@smithy/util-middleware");
const resolveCompressionConfig = (input) => ({
...input,
disableRequestCompression: (0, util_middleware_1.normalizeProvider)(input.disableRequestCompression),
requestMinCompressionSizeBytes: async () => {
const requestMinCompressionSizeBytes = await (0, util_middleware_1.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;
},
});
exports.resolveCompressionConfig = resolveCompressionConfig;
module.exports = require("./index.js");
{
"name": "@smithy/middleware-compression",
"version": "2.0.2",
"version": "2.1.0",
"description": "Middleware and Plugin for request compression.",
"scripts": {
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
"build:cjs": "yarn g:tsc -p tsconfig.cjs.json",
"build:cjs": "node ../../scripts/inline middleware-compression",
"build:es": "yarn g:tsc -p tsconfig.es.json",

@@ -18,6 +18,9 @@ "build:types": "yarn g:tsc -p tsconfig.types.json",

"dependencies": {
"@smithy/node-config-provider": "^2.1.9",
"@smithy/types": "^2.8.0",
"@smithy/util-config-provider": "^2.1.0",
"@smithy/util-middleware": "^2.0.9",
"@smithy/is-array-buffer": "^2.1.0",
"@smithy/node-config-provider": "^2.2.0",
"@smithy/protocol-http": "^3.1.0",
"@smithy/types": "^2.9.0",
"@smithy/util-config-provider": "^2.2.0",
"@smithy/util-middleware": "^2.1.0",
"@smithy/util-utf8": "^2.1.0",
"fflate": "0.8.1",

@@ -24,0 +27,0 @@ "tslib": "^2.5.0"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc