@middy/http-content-encoding 3.0.0-alpha.6 → 3.0.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/README.md +26 -16
- package/index.cjs +3 -0
- package/index.js +2 -101
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy http-content-encoding middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy http-content-encoding middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>HTTP content encoding middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/http-content-encoding?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fhttp-content-encoding.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/http-content-encoding">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/http-content-encoding" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -36,9 +46,9 @@ npm install --save @middy/http-content-encoding
|
|
|
36
46
|
```
|
|
37
47
|
|
|
38
48
|
## Options
|
|
39
|
-
- `br` (object) (
|
|
40
|
-
- `gzip` (object) (
|
|
41
|
-
- `deflate` (object) (
|
|
49
|
+
- `br` (object) (default `{}`): `zlib.createBrotliCompress` [brotliOptions](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions)
|
|
50
|
+
- `gzip` (object) (default `{}`): `zlib.createGzip` [gzipOptions](https://nodejs.org/api/zlib.html#zlib_class_options)
|
|
51
|
+
- `deflate` (object) (default `{}`): `zlib.createDeflate` [deflateOptions](https://nodejs.org/api/zlib.html#zlib_class_options)
|
|
42
52
|
- `overridePreferredEncoding` (array[string]) (optional): Override the preferred encoding order, most browsers prefer `gzip` over `br`, even though `br` has higher compression. Default: `[]`
|
|
43
53
|
|
|
44
54
|
NOTES:
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _stream=_interopRequireWildcard(require("stream"));var _events=_interopRequireDefault(require("events"));var _util=require("util");var _zlib=require("zlib");var _util1=require("@middy/util");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _getRequireWildcardCache(){if(typeof WeakMap!=="function")return null;var cache=new WeakMap;_getRequireWildcardCache=function(){return cache};return cache}function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj}if(obj===null|| typeof obj!=="object"&& typeof obj!=="function"){return{default:obj}}var cache=_getRequireWildcardCache();if(cache&&cache.has(obj)){return cache.get(obj)}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc)}else{newObj[key]=obj[key]}}}newObj.default=obj;if(cache){cache.set(obj,newObj)}return newObj}const contentEncodingStreams={br:(opts={})=>(0,_zlib).createBrotliCompress(opts),gzip:(opts={})=>(0,_zlib).createGzip(opts),deflate:(opts={})=>(0,_zlib).createDeflate(opts)};const defaults={br:undefined,gzip:undefined,deflate:undefined,overridePreferredEncoding:[]};const httpContentEncodingMiddleware=opts=>{const options={...defaults,...opts};const supportedContentEncodings=Object.keys(contentEncodingStreams);const httpContentEncodingMiddlewareAfter=async request=>{(0,_util1).normalizeHttpResponse(request);const{event:{preferredEncoding,preferredEncodings},response}=request;if(response.isBase64Encoded||!preferredEncoding||!supportedContentEncodings.includes(preferredEncoding)){return}const bodyIsString=typeof response.body==="string";let contentEncodingStream=contentEncodingStreams[preferredEncoding](options[preferredEncoding]);let contentEncoding=preferredEncoding;for(const encoding of options.overridePreferredEncoding){if(!preferredEncodings.includes(encoding))continue;contentEncodingStream=contentEncodingStreams[encoding](options[encoding]);contentEncoding=encoding;break}if(bodyIsString){const readStream=_stream.Readable.from(response.body,{objectMode:false});const chunks=[];const writeStream=new _stream.Writable({write(chunk,encoding,callback){chunks.push(chunk);callback()}});await pipeline(readStream,contentEncodingStream,writeStream);const body=Buffer.concat(chunks).toString("base64");if(body.length<response.body.length){response.headers["Content-Encoding"]=contentEncoding;response.body=body;response.isBase64Encoded=true}}else if(isReadableStream(response.body)){response.headers["Content-Encoding"]=contentEncoding;response.body=response.body.pipe(contentEncodingStream);response.isBase64Encoded=true}request.response=response};const httpContentEncodingMiddlewareOnError=async request=>{if(request.response===undefined)return;return httpContentEncodingMiddlewareAfter(request)};return{after:httpContentEncodingMiddlewareAfter,onError:httpContentEncodingMiddlewareOnError}};const isReadableStream=stream=>{return stream instanceof _events.default&&stream.readable!==false};const polyfillPipelinePromise=()=>{if(process.version<"v15.0.0"){return(0,_util).promisify(_stream.default.pipeline)}else{return _stream.default.promises.pipeline}};const pipeline=polyfillPipelinePromise();var _default=httpContentEncodingMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,102 +1,3 @@
|
|
|
1
|
-
import stream, { Readable, Writable }
|
|
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
|
-
};
|
|
1
|
+
import stream,{Readable,Writable}from"stream";import eventEmitter from"events";import{promisify}from"util";import{createBrotliCompress,createGzip,createDeflate}from"zlib";import{normalizeHttpResponse}from"@middy/util";const contentEncodingStreams={br:(opts={})=>createBrotliCompress(opts),gzip:(opts={})=>createGzip(opts),deflate:(opts={})=>createDeflate(opts)};const defaults={br:undefined,gzip:undefined,deflate:undefined,overridePreferredEncoding:[]};const httpContentEncodingMiddleware=opts=>{const options={...defaults,...opts};const supportedContentEncodings=Object.keys(contentEncodingStreams);const httpContentEncodingMiddlewareAfter=async request=>{normalizeHttpResponse(request);const{event:{preferredEncoding,preferredEncodings},response}=request;if(response.isBase64Encoded||!preferredEncoding||!supportedContentEncodings.includes(preferredEncoding)){return}const bodyIsString=typeof response.body==="string";let contentEncodingStream=contentEncodingStreams[preferredEncoding](options[preferredEncoding]);let contentEncoding=preferredEncoding;for(const encoding of options.overridePreferredEncoding){if(!preferredEncodings.includes(encoding))continue;contentEncodingStream=contentEncodingStreams[encoding](options[encoding]);contentEncoding=encoding;break}if(bodyIsString){const readStream=Readable.from(response.body,{objectMode:false});const chunks=[];const writeStream=new Writable({write(chunk,encoding,callback){chunks.push(chunk);callback()}});await pipeline(readStream,contentEncodingStream,writeStream);const body=Buffer.concat(chunks).toString("base64");if(body.length<response.body.length){response.headers["Content-Encoding"]=contentEncoding;response.body=body;response.isBase64Encoded=true}}else if(isReadableStream(response.body)){response.headers["Content-Encoding"]=contentEncoding;response.body=response.body.pipe(contentEncodingStream);response.isBase64Encoded=true}request.response=response};const httpContentEncodingMiddlewareOnError=async request=>{if(request.response===undefined)return;return httpContentEncodingMiddlewareAfter(request)};return{after:httpContentEncodingMiddlewareAfter,onError:httpContentEncodingMiddlewareOnError}};const isReadableStream=stream1=>{return stream1 instanceof eventEmitter&&stream1.readable!==false};const polyfillPipelinePromise=()=>{if(process.version<"v15.0.0"){return promisify(stream.pipeline)}else{return stream.promises.pipeline}};const pipeline=polyfillPipelinePromise();export default httpContentEncodingMiddleware
|
|
17
2
|
|
|
18
|
-
|
|
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 => {
|
|
90
|
-
return stream instanceof eventEmitter && stream.readable !== false;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const polyfillPipelinePromise = () => {
|
|
94
|
-
if (process.version < 'v15.0.0') {
|
|
95
|
-
return promisify(stream.pipeline);
|
|
96
|
-
} else {
|
|
97
|
-
return stream.promises.pipeline;
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const pipeline = polyfillPipelinePromise();
|
|
102
|
-
export default httpContentEncodingMiddleware;
|
|
3
|
+
//# 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.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -49,12 +56,12 @@
|
|
|
49
56
|
"bugs": {
|
|
50
57
|
"url": "https://github.com/middyjs/middy/issues"
|
|
51
58
|
},
|
|
52
|
-
"homepage": "https://
|
|
59
|
+
"homepage": "https://middy.js.org",
|
|
53
60
|
"dependencies": {
|
|
54
|
-
"@middy/util": "^3.0.0
|
|
61
|
+
"@middy/util": "^3.0.0"
|
|
55
62
|
},
|
|
56
63
|
"devDependencies": {
|
|
57
|
-
"@middy/core": "^3.0.0
|
|
64
|
+
"@middy/core": "^3.0.0"
|
|
58
65
|
},
|
|
59
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
60
67
|
}
|