@middy/http-content-encoding 3.0.0-alpha.3 → 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.
- package/index.js +65 -81
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,107 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { Readable, Writable, pipeline as pipelineCallback } from 'stream'
|
|
8
|
-
// import {pipeline} from 'stream/promises' // available in node >=15
|
|
9
|
-
import { promisify } from 'util'
|
|
10
|
-
|
|
11
|
-
import { createBrotliCompress, createGzip, createDeflate } from 'zlib'
|
|
12
|
-
// import {ZSTDCompress as createZstdCompress} from 'simple-zstd'
|
|
13
|
-
|
|
14
|
-
import { normalizeHttpResponse } from '@middy/util'
|
|
15
|
-
const pipeline = promisify(pipelineCallback)
|
|
16
|
-
|
|
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);
|
|
17
6
|
const contentEncodingStreams = {
|
|
18
7
|
br: (opts = {}) => createBrotliCompress(opts),
|
|
19
|
-
// zstd: (opt = {}) => createZstdCompress(opts),
|
|
20
8
|
gzip: (opts = {}) => createGzip(opts),
|
|
21
9
|
deflate: (opts = {}) => createDeflate(opts)
|
|
22
|
-
}
|
|
23
|
-
|
|
10
|
+
};
|
|
24
11
|
const defaults = {
|
|
25
12
|
br: undefined,
|
|
26
|
-
// zstd: undefined,
|
|
27
13
|
gzip: undefined,
|
|
28
14
|
deflate: undefined,
|
|
29
15
|
overridePreferredEncoding: []
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const httpContentEncodingMiddleware =
|
|
33
|
-
const options = { ...defaults,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
}
|
|
43
37
|
|
|
44
|
-
const bodyIsString = typeof response.body === 'string'
|
|
38
|
+
const bodyIsString = typeof response.body === 'string';
|
|
39
|
+
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
40
|
+
let contentEncoding = preferredEncoding;
|
|
45
41
|
|
|
46
|
-
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding])
|
|
47
|
-
let contentEncoding = preferredEncoding
|
|
48
42
|
for (const encoding of options.overridePreferredEncoding) {
|
|
49
|
-
if (!preferredEncodings.includes(encoding)) continue
|
|
50
|
-
contentEncodingStream = contentEncodingStreams[encoding](options[encoding])
|
|
51
|
-
contentEncoding = encoding
|
|
52
|
-
break
|
|
43
|
+
if (!preferredEncodings.includes(encoding)) continue;
|
|
44
|
+
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
45
|
+
contentEncoding = encoding;
|
|
46
|
+
break;
|
|
53
47
|
}
|
|
54
48
|
|
|
55
49
|
if (bodyIsString) {
|
|
56
|
-
const readStream = Readable.from(response.body, {
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
const readStream = Readable.from(response.body, {
|
|
51
|
+
objectMode: false
|
|
52
|
+
});
|
|
53
|
+
const chunks = [];
|
|
59
54
|
const writeStream = new Writable({
|
|
60
|
-
write
|
|
61
|
-
chunks.push(chunk)
|
|
62
|
-
callback()
|
|
55
|
+
write(chunk, encoding, callback) {
|
|
56
|
+
chunks.push(chunk);
|
|
57
|
+
callback();
|
|
63
58
|
}
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
await pipeline(
|
|
67
|
-
readStream,
|
|
68
|
-
contentEncodingStream,
|
|
69
|
-
writeStream
|
|
70
|
-
)
|
|
71
59
|
|
|
72
|
-
|
|
60
|
+
});
|
|
61
|
+
await pipeline(readStream, contentEncodingStream, writeStream);
|
|
62
|
+
const body = Buffer.concat(chunks).toString('base64');
|
|
73
63
|
|
|
74
|
-
// Only apply encoding if it's smaller
|
|
75
64
|
if (body.length < response.body.length) {
|
|
76
|
-
response.headers['Content-Encoding'] = contentEncoding
|
|
77
|
-
response.body = body
|
|
78
|
-
response.isBase64Encoded = true
|
|
65
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
66
|
+
response.body = body;
|
|
67
|
+
response.isBase64Encoded = true;
|
|
79
68
|
}
|
|
80
69
|
} else if (isReadableStream(response.body)) {
|
|
81
|
-
|
|
82
|
-
response.
|
|
83
|
-
response.
|
|
84
|
-
response.isBase64Encoded = true
|
|
70
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
71
|
+
response.body = response.body.pipe(contentEncodingStream);
|
|
72
|
+
response.isBase64Encoded = true;
|
|
85
73
|
}
|
|
86
|
-
request.response = response
|
|
87
|
-
}
|
|
88
74
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
75
|
+
request.response = response;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const httpContentEncodingMiddlewareOnError = async request => {
|
|
79
|
+
if (request.response === undefined) return;
|
|
80
|
+
return httpContentEncodingMiddlewareAfter(request);
|
|
81
|
+
};
|
|
93
82
|
|
|
94
83
|
return {
|
|
95
84
|
after: httpContentEncodingMiddlewareAfter,
|
|
96
85
|
onError: httpContentEncodingMiddlewareOnError
|
|
97
|
-
}
|
|
98
|
-
}
|
|
86
|
+
};
|
|
87
|
+
};
|
|
99
88
|
|
|
100
|
-
const isReadableStream = (stream)
|
|
101
|
-
typeof stream?.pipe === 'function' &&
|
|
102
|
-
stream?.readable !== false &&
|
|
103
|
-
typeof stream?._read === 'function' &&
|
|
104
|
-
typeof stream?._readableState === 'object'
|
|
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';
|
|
105
90
|
|
|
106
91
|
const polyfillPipelinePromise = async () => {
|
|
107
92
|
if (process.version < 'v15.0.0') {
|
|
@@ -109,11 +94,10 @@ const polyfillPipelinePromise = async () => {
|
|
|
109
94
|
const util = await import('util');
|
|
110
95
|
return util.promisify(stream.pipeline);
|
|
111
96
|
} else {
|
|
112
|
-
const stream = await import('stream/promises')
|
|
113
|
-
return stream.pipeline
|
|
97
|
+
const stream = await import('stream/promises');
|
|
98
|
+
return stream.pipeline;
|
|
114
99
|
}
|
|
115
|
-
}
|
|
116
|
-
global.pipeline = await polyfillPipelinePromise()
|
|
117
|
-
// import {pipeline} from 'stream/promises'
|
|
100
|
+
};
|
|
118
101
|
|
|
119
|
-
|
|
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.
|
|
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": {
|
|
@@ -51,10 +51,10 @@
|
|
|
51
51
|
},
|
|
52
52
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@middy/util": "^3.0.0-alpha.
|
|
54
|
+
"@middy/util": "^3.0.0-alpha.4"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@middy/core": "^3.0.0-alpha.
|
|
57
|
+
"@middy/core": "^3.0.0-alpha.4"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
|
|
60
60
|
}
|