@middy/http-content-encoding 3.0.0-alpha.0 → 3.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +103 -0
  2. package/package.json +7 -5
package/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import { Readable, Writable, pipeline as pipelineCallback } from 'stream';
2
+ import { promisify } from 'util';
3
+ import { createBrotliCompress, createGzip, createDeflate } from 'zlib';
4
+ import { normalizeHttpResponse } from '@middy/util';
5
+ const pipeline = promisify(pipelineCallback);
6
+ const contentEncodingStreams = {
7
+ br: (opts = {}) => createBrotliCompress(opts),
8
+ gzip: (opts = {}) => createGzip(opts),
9
+ deflate: (opts = {}) => createDeflate(opts)
10
+ };
11
+ const defaults = {
12
+ br: undefined,
13
+ gzip: undefined,
14
+ deflate: undefined,
15
+ overridePreferredEncoding: []
16
+ };
17
+
18
+ const httpContentEncodingMiddleware = opts => {
19
+ const options = { ...defaults,
20
+ ...opts
21
+ };
22
+ const supportedContentEncodings = Object.keys(contentEncodingStreams);
23
+
24
+ const httpContentEncodingMiddlewareAfter = async request => {
25
+ normalizeHttpResponse(request);
26
+ const {
27
+ event: {
28
+ preferredEncoding,
29
+ preferredEncodings
30
+ },
31
+ response
32
+ } = request;
33
+
34
+ if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) {
35
+ return;
36
+ }
37
+
38
+ const bodyIsString = typeof response.body === 'string';
39
+ let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
40
+ let contentEncoding = preferredEncoding;
41
+
42
+ for (const encoding of options.overridePreferredEncoding) {
43
+ if (!preferredEncodings.includes(encoding)) continue;
44
+ contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
45
+ contentEncoding = encoding;
46
+ break;
47
+ }
48
+
49
+ if (bodyIsString) {
50
+ const readStream = Readable.from(response.body, {
51
+ objectMode: false
52
+ });
53
+ const chunks = [];
54
+ const writeStream = new Writable({
55
+ write(chunk, encoding, callback) {
56
+ chunks.push(chunk);
57
+ callback();
58
+ }
59
+
60
+ });
61
+ await pipeline(readStream, contentEncodingStream, writeStream);
62
+ const body = Buffer.concat(chunks).toString('base64');
63
+
64
+ if (body.length < response.body.length) {
65
+ response.headers['Content-Encoding'] = contentEncoding;
66
+ response.body = body;
67
+ response.isBase64Encoded = true;
68
+ }
69
+ } else if (isReadableStream(response.body)) {
70
+ response.headers['Content-Encoding'] = contentEncoding;
71
+ response.body = response.body.pipe(contentEncodingStream);
72
+ response.isBase64Encoded = true;
73
+ }
74
+
75
+ request.response = response;
76
+ };
77
+
78
+ const httpContentEncodingMiddlewareOnError = async request => {
79
+ if (request.response === undefined) return;
80
+ return httpContentEncodingMiddlewareAfter(request);
81
+ };
82
+
83
+ return {
84
+ after: httpContentEncodingMiddlewareAfter,
85
+ onError: httpContentEncodingMiddlewareOnError
86
+ };
87
+ };
88
+
89
+ const isReadableStream = stream => typeof (stream === null || stream === void 0 ? void 0 : stream.pipe) === 'function' && (stream === null || stream === void 0 ? void 0 : stream.readable) !== false && typeof (stream === null || stream === void 0 ? void 0 : stream._read) === 'function' && typeof (stream === null || stream === void 0 ? void 0 : stream._readableState) === 'object';
90
+
91
+ const polyfillPipelinePromise = async () => {
92
+ if (process.version < 'v15.0.0') {
93
+ const stream = await import('stream');
94
+ const util = await import('util');
95
+ return util.promisify(stream.pipeline);
96
+ } else {
97
+ const stream = await import('stream/promises');
98
+ return stream.pipeline;
99
+ }
100
+ };
101
+
102
+ global.pipeline = await polyfillPipelinePromise();
103
+ export default httpContentEncodingMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-content-encoding",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "Http content encoding middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -13,11 +13,13 @@
13
13
  "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -49,10 +51,10 @@
49
51
  },
50
52
  "homepage": "https://github.com/middyjs/middy#readme",
51
53
  "dependencies": {
52
- "@middy/util": "^3.0.0-alpha.0"
54
+ "@middy/util": "^3.0.0-alpha.4"
53
55
  },
54
56
  "devDependencies": {
55
- "@middy/core": "^3.0.0-alpha.0"
57
+ "@middy/core": "^3.0.0-alpha.4"
56
58
  },
57
- "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
59
+ "gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
58
60
  }