@middy/http-content-encoding 3.0.0-alpha.0 → 3.0.0-alpha.1
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.
- package/index.js +90 -0
- package/package.json +5 -4
package/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
|
|
23
|
+
const httpContentEncodingMiddlewareAfter = async request => {
|
|
24
|
+
normalizeHttpResponse(request);
|
|
25
|
+
const {
|
|
26
|
+
event: {
|
|
27
|
+
preferredEncoding,
|
|
28
|
+
preferredEncodings
|
|
29
|
+
},
|
|
30
|
+
response
|
|
31
|
+
} = request;
|
|
32
|
+
|
|
33
|
+
if (!preferredEncoding || response.isBase64Encoded) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const bodyIsString = typeof response.body === 'string';
|
|
38
|
+
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
39
|
+
let contentEncoding = preferredEncoding;
|
|
40
|
+
|
|
41
|
+
for (const encoding of options.overridePreferredEncoding) {
|
|
42
|
+
if (!preferredEncodings.includes(encoding)) continue;
|
|
43
|
+
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
44
|
+
contentEncoding = encoding;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (bodyIsString) {
|
|
49
|
+
const readStream = Readable.from(response.body, {
|
|
50
|
+
objectMode: false
|
|
51
|
+
});
|
|
52
|
+
const chunks = [];
|
|
53
|
+
const writeStream = new Writable({
|
|
54
|
+
write(chunk, encoding, callback) {
|
|
55
|
+
chunks.push(chunk);
|
|
56
|
+
callback();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
});
|
|
60
|
+
await pipeline(readStream, contentEncodingStream, writeStream);
|
|
61
|
+
const body = Buffer.concat(chunks).toString('base64');
|
|
62
|
+
|
|
63
|
+
if (body.length < response.body.length) {
|
|
64
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
65
|
+
response.body = body;
|
|
66
|
+
response.isBase64Encoded = true;
|
|
67
|
+
}
|
|
68
|
+
} else if (isReadableStream(response.body)) {
|
|
69
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
70
|
+
response.body = response.body.pipe(contentEncodingStream);
|
|
71
|
+
response.isBase64Encoded = true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
request.response = response;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const httpContentEncodingMiddlewareOnError = async request => {
|
|
78
|
+
if (request.response === undefined) return;
|
|
79
|
+
return httpContentEncodingMiddlewareAfter(request);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
after: httpContentEncodingMiddlewareAfter,
|
|
84
|
+
onError: httpContentEncodingMiddlewareOnError
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const isReadableStream = stream => typeof stream.pipe === 'function' && stream.readable !== false && typeof stream._read === 'function' && typeof stream._readableState === 'object';
|
|
89
|
+
|
|
90
|
+
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.
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,6 +13,7 @@
|
|
|
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": {
|
|
@@ -49,10 +50,10 @@
|
|
|
49
50
|
},
|
|
50
51
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
51
52
|
"dependencies": {
|
|
52
|
-
"@middy/util": "^3.0.0-alpha.
|
|
53
|
+
"@middy/util": "^3.0.0-alpha.1"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^3.0.0-alpha.
|
|
56
|
+
"@middy/core": "^3.0.0-alpha.1"
|
|
56
57
|
},
|
|
57
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
|
|
58
59
|
}
|