@middy/http-content-encoding 3.6.1 → 4.0.0-alpha.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.
Files changed (3) hide show
  1. package/index.cjs +15 -86
  2. package/index.js +13 -40
  3. package/package.json +6 -4
package/index.cjs CHANGED
@@ -6,59 +6,13 @@ Object.defineProperty(module, "exports", {
6
6
  enumerable: true,
7
7
  get: ()=>_default
8
8
  });
9
- const _stream = _interopRequireWildcard(require("stream"));
10
- const _events = _interopRequireDefault(require("events"));
11
- const _util = require("util");
12
- const _zlib = require("zlib");
13
- const _util1 = require("@middy/util");
14
- function _interopRequireDefault(obj) {
15
- return obj && obj.__esModule ? obj : {
16
- default: obj
17
- };
18
- }
19
- function _getRequireWildcardCache(nodeInterop) {
20
- if (typeof WeakMap !== "function") return null;
21
- var cacheBabelInterop = new WeakMap();
22
- var cacheNodeInterop = new WeakMap();
23
- return (_getRequireWildcardCache = function(nodeInterop) {
24
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
25
- })(nodeInterop);
26
- }
27
- function _interopRequireWildcard(obj, nodeInterop) {
28
- if (!nodeInterop && obj && obj.__esModule) {
29
- return obj;
30
- }
31
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
32
- return {
33
- default: obj
34
- };
35
- }
36
- var cache = _getRequireWildcardCache(nodeInterop);
37
- if (cache && cache.has(obj)) {
38
- return cache.get(obj);
39
- }
40
- var newObj = {};
41
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
42
- for(var key in obj){
43
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
44
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
45
- if (desc && (desc.get || desc.set)) {
46
- Object.defineProperty(newObj, key, desc);
47
- } else {
48
- newObj[key] = obj[key];
49
- }
50
- }
51
- }
52
- newObj.default = obj;
53
- if (cache) {
54
- cache.set(obj, newObj);
55
- }
56
- return newObj;
57
- }
9
+ const _core = require("@datastream/core");
10
+ const _compress = require("@datastream/compress");
11
+ const _util = require("@middy/util");
58
12
  const contentEncodingStreams = {
59
- br: (opts = {})=>(0, _zlib.createBrotliCompress)(opts),
60
- gzip: (opts = {})=>(0, _zlib.createGzip)(opts),
61
- deflate: (opts = {})=>(0, _zlib.createDeflate)(opts)
13
+ br: _compress.brotliCompressStream,
14
+ gzip: _compress.gzipCompressStream,
15
+ deflate: _compress.deflateCompressStream
62
16
  };
63
17
  const defaults = {
64
18
  br: undefined,
@@ -73,12 +27,11 @@ const httpContentEncodingMiddleware = (opts)=>{
73
27
  };
74
28
  const supportedContentEncodings = Object.keys(contentEncodingStreams);
75
29
  const httpContentEncodingMiddlewareAfter = async (request)=>{
76
- (0, _util1.normalizeHttpResponse)(request);
30
+ (0, _util.normalizeHttpResponse)(request);
77
31
  const { event: { preferredEncoding , preferredEncodings } , response } = request;
78
- if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) {
32
+ if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
79
33
  return;
80
34
  }
81
- const bodyIsString = typeof response.body === 'string';
82
35
  let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
83
36
  let contentEncoding = preferredEncoding;
84
37
  for (const encoding of options.overridePreferredEncoding){
@@ -87,27 +40,14 @@ const httpContentEncodingMiddleware = (opts)=>{
87
40
  contentEncoding = encoding;
88
41
  break;
89
42
  }
90
- if (bodyIsString) {
91
- const readStream = _stream.Readable.from(response.body, {
92
- objectMode: false
93
- });
94
- const chunks = [];
95
- const writeStream = new _stream.Writable({
96
- write (chunk, encoding, callback) {
97
- chunks.push(chunk);
98
- callback();
99
- }
100
- });
101
- await pipeline(readStream, contentEncodingStream, writeStream);
102
- const body = Buffer.concat(chunks).toString('base64');
103
- if (body.length < response.body.length) {
104
- response.headers['Content-Encoding'] = contentEncoding;
105
- response.body = body;
106
- response.isBase64Encoded = true;
107
- }
108
- } else if (isReadableStream(response.body)) {
43
+ const stream = (0, _core.pipejoin)([
44
+ (0, _core.createReadableStream)(response.body),
45
+ contentEncodingStream
46
+ ]);
47
+ const body = await (0, _core.streamToString)(stream);
48
+ if (body.length < response.body.length) {
109
49
  response.headers['Content-Encoding'] = contentEncoding;
110
- response.body = response.body.pipe(contentEncodingStream);
50
+ response.body = body;
111
51
  response.isBase64Encoded = true;
112
52
  }
113
53
  request.response = response;
@@ -121,17 +61,6 @@ const httpContentEncodingMiddleware = (opts)=>{
121
61
  onError: httpContentEncodingMiddlewareOnError
122
62
  };
123
63
  };
124
- const isReadableStream = (stream)=>{
125
- return stream instanceof _events.default && stream.readable !== false;
126
- };
127
- const polyfillPipelinePromise = ()=>{
128
- if (process.version < 'v15.0.0') {
129
- return (0, _util.promisify)(_stream.default.pipeline);
130
- } else {
131
- return _stream.default.promises.pipeline;
132
- }
133
- };
134
- const pipeline = polyfillPipelinePromise();
135
64
  const _default = httpContentEncodingMiddleware;
136
65
 
137
66
 
package/index.js CHANGED
@@ -1,12 +1,10 @@
1
- import stream, { Readable, Writable } from 'stream';
2
- import eventEmitter from 'events';
3
- import { promisify } from 'util';
4
- import { createBrotliCompress, createGzip, createDeflate } from 'zlib';
1
+ import { pipejoin, createReadableStream, streamToString } from '@datastream/core';
2
+ import { brotliCompressStream, gzipCompressStream, deflateCompressStream } from '@datastream/compress';
5
3
  import { normalizeHttpResponse } from '@middy/util';
6
4
  const contentEncodingStreams = {
7
- br: (opts = {})=>createBrotliCompress(opts),
8
- gzip: (opts = {})=>createGzip(opts),
9
- deflate: (opts = {})=>createDeflate(opts)
5
+ br: brotliCompressStream,
6
+ gzip: gzipCompressStream,
7
+ deflate: deflateCompressStream
10
8
  };
11
9
  const defaults = {
12
10
  br: undefined,
@@ -23,10 +21,9 @@ const httpContentEncodingMiddleware = (opts)=>{
23
21
  const httpContentEncodingMiddlewareAfter = async (request)=>{
24
22
  normalizeHttpResponse(request);
25
23
  const { event: { preferredEncoding , preferredEncodings } , response } = request;
26
- if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) {
24
+ if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
27
25
  return;
28
26
  }
29
- const bodyIsString = typeof response.body === 'string';
30
27
  let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
31
28
  let contentEncoding = preferredEncoding;
32
29
  for (const encoding of options.overridePreferredEncoding){
@@ -35,27 +32,14 @@ const httpContentEncodingMiddleware = (opts)=>{
35
32
  contentEncoding = encoding;
36
33
  break;
37
34
  }
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)) {
35
+ const stream = pipejoin([
36
+ createReadableStream(response.body),
37
+ contentEncodingStream
38
+ ]);
39
+ const body = await streamToString(stream);
40
+ if (body.length < response.body.length) {
57
41
  response.headers['Content-Encoding'] = contentEncoding;
58
- response.body = response.body.pipe(contentEncodingStream);
42
+ response.body = body;
59
43
  response.isBase64Encoded = true;
60
44
  }
61
45
  request.response = response;
@@ -69,17 +53,6 @@ const httpContentEncodingMiddleware = (opts)=>{
69
53
  onError: httpContentEncodingMiddlewareOnError
70
54
  };
71
55
  };
72
- const isReadableStream = (stream)=>{
73
- return stream instanceof eventEmitter && stream.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
56
  export default httpContentEncodingMiddleware;
84
57
 
85
58
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-content-encoding",
3
- "version": "3.6.1",
3
+ "version": "4.0.0-alpha.0",
4
4
  "description": "Http content encoding middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -65,10 +65,12 @@
65
65
  },
66
66
  "homepage": "https://middy.js.org",
67
67
  "dependencies": {
68
- "@middy/util": "3.6.1"
68
+ "@datastream/compress": "0.0.8",
69
+ "@datastream/core": "0.0.8",
70
+ "@middy/util": "4.0.0-alpha.0"
69
71
  },
70
72
  "devDependencies": {
71
- "@middy/core": "3.6.1"
73
+ "@middy/core": "4.0.0-alpha.0"
72
74
  },
73
- "gitHead": "31a7f9f8e93a73a8be382b3022b9cfcbc13b2961"
75
+ "gitHead": "306fb9aa633d5757d11ced3dc192f046ef3c2685"
74
76
  }