@middy/s3-object-response 3.0.1 → 3.0.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/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/s3-object-response">
10
10
  <img src="https://packagephobia.com/badge?p=@middy/s3-object-response" 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/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
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/s3-object-response">https://middy.js.org/docs/middlewares/s3-object-response</a></p>
36
37
  </div>
37
38
 
38
39
  Fetches S3 object as a stream and writes back to s3 object response.
@@ -119,7 +120,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
119
120
 
120
121
  ## License
121
122
 
122
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
123
+ 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).
123
124
 
124
125
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
125
126
  <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,103 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _https=_interopRequireDefault(require("https"));var _url=require("url");var _util=require("@middy/util");var _s3Js=_interopRequireDefault(require("aws-sdk/clients/s3.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var _response;const defaults={AwsClient:_s3Js.default,awsClientOptions:{},awsClientAssumeRole:undefined,awsClientCapture:undefined,httpsCapture:undefined,disablePrefetch:false,bodyType:undefined,__https:_https.default};let https=_https.default;const s3ObjectResponseMiddleware=(opts={})=>{const options={...defaults,...opts};if(!["stream","promise"].includes(options.bodyType)){throw new Error("[s3-object-response] bodyType is invalid")}if(options.httpsCapture){https=options.httpsCapture(options.__https)}let client;if((0,_util).canPrefetch(options)){client=(0,_util).createPrefetchClient(options)}const s3ObjectResponseMiddlewareBefore=async request=>{const{inputS3Url,outputRoute,outputToken}=request.event.getObjectContext;request.internal.s3ObjectResponse={RequestRoute:outputRoute,RequestToken:outputToken};const parsedInputS3Url=new _url.URL(inputS3Url);const fetchOptions={method:"GET",host:parsedInputS3Url.hostname,path:parsedInputS3Url.pathname};request.context.s3Object=fetchType(options.bodyType,fetchOptions)};const s3ObjectResponseMiddlewareAfter=async request=>{if(!client){client=await (0,_util).createClient(options,request)}(_response=request.response).Body??(_response.Body=request.response.body);delete request.response.body;await client.writeGetObjectResponse({...request.response,...request.internal.s3ObjectResponse}).promise();return{statusCode:200}};return{before:s3ObjectResponseMiddlewareBefore,after:s3ObjectResponseMiddlewareAfter}};const fetchType=(type,fetchOptions)=>{if(type==="stream"){return fetchStream(fetchOptions)}else if(type==="promise"){return fetchPromise(fetchOptions)}};const fetchStream=fetchOptions=>{return https.request(fetchOptions)};const fetchPromise=fetchOptions=>{return new Promise((resolve,reject)=>{let data="";const stream=fetchStream(fetchOptions);stream.on("data",chunk=>{data+=chunk});stream.on("end",()=>resolve(data));stream.on("error",error=>reject(error))})};var _default=s3ObjectResponseMiddleware;exports.default=_default
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ module.exports = void 0;
6
+ var _https = _interopRequireDefault(require("https"));
7
+ var _url = require("url");
8
+ var _util = require("@middy/util");
9
+ var _s3Js = _interopRequireDefault(require("aws-sdk/clients/s3.js"));
10
+ function _interopRequireDefault(obj) {
11
+ return obj && obj.__esModule ? obj : {
12
+ default: obj
13
+ };
14
+ }
15
+ var _response;
16
+ const defaults = {
17
+ AwsClient: _s3Js.default,
18
+ awsClientOptions: {},
19
+ awsClientAssumeRole: undefined,
20
+ awsClientCapture: undefined,
21
+ httpsCapture: undefined,
22
+ disablePrefetch: false,
23
+ bodyType: undefined,
24
+ __https: _https.default
25
+ };
26
+ let https = _https.default;
27
+ const s3ObjectResponseMiddleware = (opts = {})=>{
28
+ const options = {
29
+ ...defaults,
30
+ ...opts
31
+ };
32
+ if (![
33
+ 'stream',
34
+ 'promise'
35
+ ].includes(options.bodyType)) {
36
+ throw new Error('[s3-object-response] bodyType is invalid');
37
+ }
38
+ if (options.httpsCapture) {
39
+ https = options.httpsCapture(options.__https);
40
+ }
41
+ let client;
42
+ if ((0, _util).canPrefetch(options)) {
43
+ client = (0, _util).createPrefetchClient(options);
44
+ }
45
+ const s3ObjectResponseMiddlewareBefore = async (request)=>{
46
+ const { inputS3Url , outputRoute , outputToken } = request.event.getObjectContext;
47
+ request.internal.s3ObjectResponse = {
48
+ RequestRoute: outputRoute,
49
+ RequestToken: outputToken
50
+ };
51
+ const parsedInputS3Url = new _url.URL(inputS3Url);
52
+ const fetchOptions = {
53
+ method: 'GET',
54
+ host: parsedInputS3Url.hostname,
55
+ path: parsedInputS3Url.pathname
56
+ };
57
+ request.context.s3Object = fetchType(options.bodyType, fetchOptions);
58
+ };
59
+ const s3ObjectResponseMiddlewareAfter = async (request)=>{
60
+ if (!client) {
61
+ client = await (0, _util).createClient(options, request);
62
+ }
63
+ (_response = request.response).Body ?? (_response.Body = request.response.body);
64
+ delete request.response.body;
65
+ await client.writeGetObjectResponse({
66
+ ...request.response,
67
+ ...request.internal.s3ObjectResponse
68
+ }).promise();
69
+ return {
70
+ statusCode: 200
71
+ };
72
+ };
73
+ return {
74
+ before: s3ObjectResponseMiddlewareBefore,
75
+ after: s3ObjectResponseMiddlewareAfter
76
+ };
77
+ };
78
+ const fetchType = (type, fetchOptions)=>{
79
+ if (type === 'stream') {
80
+ return fetchStream(fetchOptions);
81
+ } else if (type === 'promise') {
82
+ return fetchPromise(fetchOptions);
83
+ }
84
+ };
85
+ const fetchStream = (fetchOptions)=>{
86
+ return https.request(fetchOptions);
87
+ };
88
+ const fetchPromise = (fetchOptions)=>{
89
+ return new Promise((resolve, reject)=>{
90
+ let data = '';
91
+ const stream = fetchStream(fetchOptions);
92
+ stream.on('data', (chunk)=>{
93
+ data += chunk;
94
+ });
95
+ stream.on('end', ()=>resolve(data));
96
+ stream.on('error', (error)=>reject(error));
97
+ });
98
+ };
99
+ var _default = s3ObjectResponseMiddleware;
100
+ module.exports = _default;
101
+
2
102
 
3
103
  //# sourceMappingURL=index.cjs.map
package/index.js CHANGED
@@ -1,3 +1,92 @@
1
- var _response;import __https from"https";import{URL}from"url";import{canPrefetch,createPrefetchClient,createClient}from"@middy/util";import S3 from"aws-sdk/clients/s3.js";const defaults={AwsClient:S3,awsClientOptions:{},awsClientAssumeRole:undefined,awsClientCapture:undefined,httpsCapture:undefined,disablePrefetch:false,bodyType:undefined,__https};let https=__https;const s3ObjectResponseMiddleware=(opts={})=>{const options={...defaults,...opts};if(!["stream","promise"].includes(options.bodyType)){throw new Error("[s3-object-response] bodyType is invalid")}if(options.httpsCapture){https=options.httpsCapture(options.__https)}let client;if(canPrefetch(options)){client=createPrefetchClient(options)}const s3ObjectResponseMiddlewareBefore=async request=>{const{inputS3Url,outputRoute,outputToken}=request.event.getObjectContext;request.internal.s3ObjectResponse={RequestRoute:outputRoute,RequestToken:outputToken};const parsedInputS3Url=new URL(inputS3Url);const fetchOptions={method:"GET",host:parsedInputS3Url.hostname,path:parsedInputS3Url.pathname};request.context.s3Object=fetchType(options.bodyType,fetchOptions)};const s3ObjectResponseMiddlewareAfter=async request=>{if(!client){client=await createClient(options,request)}(_response=request.response).Body??(_response.Body=request.response.body);delete request.response.body;await client.writeGetObjectResponse({...request.response,...request.internal.s3ObjectResponse}).promise();return{statusCode:200}};return{before:s3ObjectResponseMiddlewareBefore,after:s3ObjectResponseMiddlewareAfter}};const fetchType=(type,fetchOptions)=>{if(type==="stream"){return fetchStream(fetchOptions)}else if(type==="promise"){return fetchPromise(fetchOptions)}};const fetchStream=fetchOptions=>{return https.request(fetchOptions)};const fetchPromise=fetchOptions=>{return new Promise((resolve,reject)=>{let data="";const stream=fetchStream(fetchOptions);stream.on("data",chunk=>{data+=chunk});stream.on("end",()=>resolve(data));stream.on("error",error=>reject(error))})};export default s3ObjectResponseMiddleware
1
+ var _response;
2
+ import __https from 'https';
3
+ import { URL } from 'url';
4
+ import { canPrefetch, createPrefetchClient, createClient } from '@middy/util';
5
+ import S3 from 'aws-sdk/clients/s3.js';
6
+ const defaults = {
7
+ AwsClient: S3,
8
+ awsClientOptions: {},
9
+ awsClientAssumeRole: undefined,
10
+ awsClientCapture: undefined,
11
+ httpsCapture: undefined,
12
+ disablePrefetch: false,
13
+ bodyType: undefined,
14
+ __https
15
+ };
16
+ let https = __https;
17
+ const s3ObjectResponseMiddleware = (opts = {})=>{
18
+ const options = {
19
+ ...defaults,
20
+ ...opts
21
+ };
22
+ if (![
23
+ 'stream',
24
+ 'promise'
25
+ ].includes(options.bodyType)) {
26
+ throw new Error('[s3-object-response] bodyType is invalid');
27
+ }
28
+ if (options.httpsCapture) {
29
+ https = options.httpsCapture(options.__https);
30
+ }
31
+ let client;
32
+ if (canPrefetch(options)) {
33
+ client = createPrefetchClient(options);
34
+ }
35
+ const s3ObjectResponseMiddlewareBefore = async (request)=>{
36
+ const { inputS3Url , outputRoute , outputToken } = request.event.getObjectContext;
37
+ request.internal.s3ObjectResponse = {
38
+ RequestRoute: outputRoute,
39
+ RequestToken: outputToken
40
+ };
41
+ const parsedInputS3Url = new URL(inputS3Url);
42
+ const fetchOptions = {
43
+ method: 'GET',
44
+ host: parsedInputS3Url.hostname,
45
+ path: parsedInputS3Url.pathname
46
+ };
47
+ request.context.s3Object = fetchType(options.bodyType, fetchOptions);
48
+ };
49
+ const s3ObjectResponseMiddlewareAfter = async (request)=>{
50
+ if (!client) {
51
+ client = await createClient(options, request);
52
+ }
53
+ (_response = request.response).Body ?? (_response.Body = request.response.body);
54
+ delete request.response.body;
55
+ await client.writeGetObjectResponse({
56
+ ...request.response,
57
+ ...request.internal.s3ObjectResponse
58
+ }).promise();
59
+ return {
60
+ statusCode: 200
61
+ };
62
+ };
63
+ return {
64
+ before: s3ObjectResponseMiddlewareBefore,
65
+ after: s3ObjectResponseMiddlewareAfter
66
+ };
67
+ };
68
+ const fetchType = (type, fetchOptions)=>{
69
+ if (type === 'stream') {
70
+ return fetchStream(fetchOptions);
71
+ } else if (type === 'promise') {
72
+ return fetchPromise(fetchOptions);
73
+ }
74
+ };
75
+ const fetchStream = (fetchOptions)=>{
76
+ return https.request(fetchOptions);
77
+ };
78
+ const fetchPromise = (fetchOptions)=>{
79
+ return new Promise((resolve, reject)=>{
80
+ let data = '';
81
+ const stream = fetchStream(fetchOptions);
82
+ stream.on('data', (chunk)=>{
83
+ data += chunk;
84
+ });
85
+ stream.on('end', ()=>resolve(data));
86
+ stream.on('error', (error)=>reject(error));
87
+ });
88
+ };
89
+ export default s3ObjectResponseMiddleware;
90
+
2
91
 
3
92
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/s3-object-response",
3
- "version": "3.0.1",
3
+ "version": "3.0.4",
4
4
  "description": "S3 object response handling middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -55,12 +55,12 @@
55
55
  },
56
56
  "homepage": "https://middy.js.org",
57
57
  "dependencies": {
58
- "@middy/util": "^3.0.1"
58
+ "@middy/util": "3.0.4"
59
59
  },
60
60
  "devDependencies": {
61
- "@middy/core": "^3.0.1",
61
+ "@middy/core": "3.0.4",
62
62
  "aws-sdk": "^2.939.0",
63
63
  "aws-xray-sdk": "^3.3.3"
64
64
  },
65
- "gitHead": "797455e2c2be445867bc03597e0a2d5b0560c9de"
65
+ "gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
66
66
  }