@middy/http-content-encoding 3.0.0-alpha.6 → 3.0.0-alpha.7

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 (3) hide show
  1. package/README.md +26 -16
  2. package/index.js +2 -101
  3. package/package.json +4 -4
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="http://badge.fury.io/js/%40middy%2Fhttp-content-encoding">
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://standardjs.com/">
20
- <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
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" style="max-width:100%;">
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) (optional): `zlib.createBrotliCompress` [brotliOptions](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions)
40
- - `gzip` (object) (optional): `zlib.createGzip` [gzipOptions](https://nodejs.org/api/zlib.html#zlib_class_options)
41
- - `deflate` (object) (optional): `zlib.createDeflate` [deflateOptions](https://nodejs.org/api/zlib.html#zlib_class_options)
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.js CHANGED
@@ -1,102 +1,3 @@
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
- };
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
- 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 => {
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-alpha.6",
3
+ "version": "3.0.0-alpha.7",
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.6"
54
+ "@middy/util": "^3.0.0-alpha.7"
55
55
  },
56
56
  "devDependencies": {
57
- "@middy/core": "^3.0.0-alpha.6"
57
+ "@middy/core": "^3.0.0-alpha.7"
58
58
  },
59
- "gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720"
59
+ "gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
60
60
  }