@middy/http-content-encoding 3.0.2 → 3.1.0-rc.0
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/LICENSE +1 -1
- package/README.md +4 -3
- package/index.cjs +134 -1
- package/index.js +84 -1
- package/package.json +13 -7
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
3
|
+
Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
<a href="https://packagephobia.com/result?p=@middy/http-content-encoding">
|
|
10
10
|
<img src="https://packagephobia.com/badge?p=@middy/http-content-encoding" alt="npm install size" style="max-width:100%;">
|
|
11
11
|
</a>
|
|
12
|
-
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
-
<img src="https://github.com/middyjs/middy/workflows/
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
|
|
14
14
|
</a>
|
|
15
15
|
<br/>
|
|
16
16
|
<a href="https://standardjs.com/">
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
34
34
|
</a>
|
|
35
35
|
</p>
|
|
36
|
+
<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-content-encoding">https://middy.js.org/docs/middlewares/http-content-encoding</a></p>
|
|
36
37
|
</div>
|
|
37
38
|
|
|
38
39
|
This middleware take the `preferredEncoding` output from `@middy/http-content-negotiation` and applies the encoding to `response.body` when a string.
|
|
@@ -97,7 +98,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
97
98
|
|
|
98
99
|
## License
|
|
99
100
|
|
|
100
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
101
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
101
102
|
|
|
102
103
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
103
104
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.cjs
CHANGED
|
@@ -1,3 +1,136 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _stream = _interopRequireWildcard(require("stream"));
|
|
7
|
+
var _events = _interopRequireDefault(require("events"));
|
|
8
|
+
var _util = require("util");
|
|
9
|
+
var _zlib = require("zlib");
|
|
10
|
+
var _util1 = require("@middy/util");
|
|
11
|
+
function _interopRequireDefault(obj) {
|
|
12
|
+
return obj && obj.__esModule ? obj : {
|
|
13
|
+
default: obj
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function _getRequireWildcardCache() {
|
|
17
|
+
if (typeof WeakMap !== "function") return null;
|
|
18
|
+
var cache = new WeakMap();
|
|
19
|
+
_getRequireWildcardCache = function() {
|
|
20
|
+
return cache;
|
|
21
|
+
};
|
|
22
|
+
return cache;
|
|
23
|
+
}
|
|
24
|
+
function _interopRequireWildcard(obj) {
|
|
25
|
+
if (obj && obj.__esModule) {
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
29
|
+
return {
|
|
30
|
+
default: obj
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
var cache = _getRequireWildcardCache();
|
|
34
|
+
if (cache && cache.has(obj)) {
|
|
35
|
+
return cache.get(obj);
|
|
36
|
+
}
|
|
37
|
+
var newObj = {};
|
|
38
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
39
|
+
for(var key in obj){
|
|
40
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
41
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
42
|
+
if (desc && (desc.get || desc.set)) {
|
|
43
|
+
Object.defineProperty(newObj, key, desc);
|
|
44
|
+
} else {
|
|
45
|
+
newObj[key] = obj[key];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
newObj.default = obj;
|
|
50
|
+
if (cache) {
|
|
51
|
+
cache.set(obj, newObj);
|
|
52
|
+
}
|
|
53
|
+
return newObj;
|
|
54
|
+
}
|
|
55
|
+
const contentEncodingStreams = {
|
|
56
|
+
br: (opts = {})=>(0, _zlib).createBrotliCompress(opts),
|
|
57
|
+
gzip: (opts = {})=>(0, _zlib).createGzip(opts),
|
|
58
|
+
deflate: (opts = {})=>(0, _zlib).createDeflate(opts)
|
|
59
|
+
};
|
|
60
|
+
const defaults = {
|
|
61
|
+
br: undefined,
|
|
62
|
+
gzip: undefined,
|
|
63
|
+
deflate: undefined,
|
|
64
|
+
overridePreferredEncoding: []
|
|
65
|
+
};
|
|
66
|
+
const httpContentEncodingMiddleware = (opts)=>{
|
|
67
|
+
const options = {
|
|
68
|
+
...defaults,
|
|
69
|
+
...opts
|
|
70
|
+
};
|
|
71
|
+
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
72
|
+
const httpContentEncodingMiddlewareAfter = async (request)=>{
|
|
73
|
+
(0, _util1).normalizeHttpResponse(request);
|
|
74
|
+
const { event: { preferredEncoding , preferredEncodings } , response } = request;
|
|
75
|
+
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const bodyIsString = typeof response.body === 'string';
|
|
79
|
+
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
80
|
+
let contentEncoding = preferredEncoding;
|
|
81
|
+
for (const encoding of options.overridePreferredEncoding){
|
|
82
|
+
if (!preferredEncodings.includes(encoding)) continue;
|
|
83
|
+
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
84
|
+
contentEncoding = encoding;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
if (bodyIsString) {
|
|
88
|
+
const readStream = _stream.Readable.from(response.body, {
|
|
89
|
+
objectMode: false
|
|
90
|
+
});
|
|
91
|
+
const chunks = [];
|
|
92
|
+
const writeStream = new _stream.Writable({
|
|
93
|
+
write (chunk, encoding, callback) {
|
|
94
|
+
chunks.push(chunk);
|
|
95
|
+
callback();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
await pipeline(readStream, contentEncodingStream, writeStream);
|
|
99
|
+
const body = Buffer.concat(chunks).toString('base64');
|
|
100
|
+
if (body.length < response.body.length) {
|
|
101
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
102
|
+
response.body = body;
|
|
103
|
+
response.isBase64Encoded = true;
|
|
104
|
+
}
|
|
105
|
+
} else if (isReadableStream(response.body)) {
|
|
106
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
107
|
+
response.body = response.body.pipe(contentEncodingStream);
|
|
108
|
+
response.isBase64Encoded = true;
|
|
109
|
+
}
|
|
110
|
+
request.response = response;
|
|
111
|
+
};
|
|
112
|
+
const httpContentEncodingMiddlewareOnError = async (request)=>{
|
|
113
|
+
if (request.response === undefined) return;
|
|
114
|
+
return httpContentEncodingMiddlewareAfter(request);
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
after: httpContentEncodingMiddlewareAfter,
|
|
118
|
+
onError: httpContentEncodingMiddlewareOnError
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
const isReadableStream = (stream)=>{
|
|
122
|
+
return stream instanceof _events.default && stream.readable !== false;
|
|
123
|
+
};
|
|
124
|
+
const polyfillPipelinePromise = ()=>{
|
|
125
|
+
if (process.version < 'v15.0.0') {
|
|
126
|
+
return (0, _util).promisify(_stream.default.pipeline);
|
|
127
|
+
} else {
|
|
128
|
+
return _stream.default.promises.pipeline;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const pipeline = polyfillPipelinePromise();
|
|
132
|
+
var _default = httpContentEncodingMiddleware;
|
|
133
|
+
module.exports = _default;
|
|
134
|
+
|
|
2
135
|
|
|
3
136
|
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
import stream,
|
|
1
|
+
import stream, { Readable, Writable } from 'stream';
|
|
2
|
+
import eventEmitter from 'events';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
import { createBrotliCompress, createGzip, createDeflate } from 'zlib';
|
|
5
|
+
import { normalizeHttpResponse } from '@middy/util';
|
|
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
|
+
const httpContentEncodingMiddleware = (opts)=>{
|
|
18
|
+
const options = {
|
|
19
|
+
...defaults,
|
|
20
|
+
...opts
|
|
21
|
+
};
|
|
22
|
+
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
23
|
+
const httpContentEncodingMiddlewareAfter = async (request)=>{
|
|
24
|
+
normalizeHttpResponse(request);
|
|
25
|
+
const { event: { preferredEncoding , preferredEncodings } , response } = request;
|
|
26
|
+
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const bodyIsString = typeof response.body === 'string';
|
|
30
|
+
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
31
|
+
let contentEncoding = preferredEncoding;
|
|
32
|
+
for (const encoding of options.overridePreferredEncoding){
|
|
33
|
+
if (!preferredEncodings.includes(encoding)) continue;
|
|
34
|
+
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
35
|
+
contentEncoding = encoding;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
if (bodyIsString) {
|
|
39
|
+
const readStream = Readable.from(response.body, {
|
|
40
|
+
objectMode: false
|
|
41
|
+
});
|
|
42
|
+
const chunks = [];
|
|
43
|
+
const writeStream = new Writable({
|
|
44
|
+
write (chunk, encoding, callback) {
|
|
45
|
+
chunks.push(chunk);
|
|
46
|
+
callback();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
await pipeline(readStream, contentEncodingStream, writeStream);
|
|
50
|
+
const body = Buffer.concat(chunks).toString('base64');
|
|
51
|
+
if (body.length < response.body.length) {
|
|
52
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
53
|
+
response.body = body;
|
|
54
|
+
response.isBase64Encoded = true;
|
|
55
|
+
}
|
|
56
|
+
} else if (isReadableStream(response.body)) {
|
|
57
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
58
|
+
response.body = response.body.pipe(contentEncodingStream);
|
|
59
|
+
response.isBase64Encoded = true;
|
|
60
|
+
}
|
|
61
|
+
request.response = response;
|
|
62
|
+
};
|
|
63
|
+
const httpContentEncodingMiddlewareOnError = async (request)=>{
|
|
64
|
+
if (request.response === undefined) return;
|
|
65
|
+
return httpContentEncodingMiddlewareAfter(request);
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
after: httpContentEncodingMiddlewareAfter,
|
|
69
|
+
onError: httpContentEncodingMiddlewareOnError
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
const isReadableStream = (stream1)=>{
|
|
73
|
+
return stream1 instanceof eventEmitter && stream1.readable !== false;
|
|
74
|
+
};
|
|
75
|
+
const polyfillPipelinePromise = ()=>{
|
|
76
|
+
if (process.version < 'v15.0.0') {
|
|
77
|
+
return promisify(stream.pipeline);
|
|
78
|
+
} else {
|
|
79
|
+
return stream.promises.pipeline;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const pipeline = polyfillPipelinePromise();
|
|
83
|
+
export default httpContentEncodingMiddleware;
|
|
84
|
+
|
|
2
85
|
|
|
3
86
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-encoding",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.1.0-rc.0",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,11 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
+
"main": "./index.cjs",
|
|
13
14
|
"exports": {
|
|
14
15
|
".": {
|
|
15
|
-
"import":
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./index.cjs"
|
|
23
|
+
}
|
|
18
24
|
}
|
|
19
25
|
},
|
|
20
26
|
"types": "index.d.ts",
|
|
@@ -58,10 +64,10 @@
|
|
|
58
64
|
},
|
|
59
65
|
"homepage": "https://middy.js.org",
|
|
60
66
|
"dependencies": {
|
|
61
|
-
"@middy/util": "
|
|
67
|
+
"@middy/util": "3.1.0-rc.0"
|
|
62
68
|
},
|
|
63
69
|
"devDependencies": {
|
|
64
|
-
"@middy/core": "
|
|
70
|
+
"@middy/core": "3.1.0-rc.0"
|
|
65
71
|
},
|
|
66
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "03a8794d3cdb4319eca49ba4c55420bea5d66430"
|
|
67
73
|
}
|