@middy/s3-object-response 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 +33 -17
- 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 s3-object-response 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 s3-object-response middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>S3 object response 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/s3-object-response?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fs3-object-response.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/s3-object-response">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/s3-object-response" 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>
|
|
@@ -39,10 +49,10 @@ npm install --save @middy/s3-object-response
|
|
|
39
49
|
## Options
|
|
40
50
|
- `bodyType` (string) (required): How to pass in the s3 object through the handler. Can be `stream` or `promise`.
|
|
41
51
|
- `AwsClient` (object) (default `AWS.S3`): AWS.S3 class constructor (e.g. that has been instrumented with AWS XRay). Must be from `aws-sdk` v2.
|
|
42
|
-
- `awsClientOptions` (object) (
|
|
43
|
-
- `awsClientAssumeRole` (string) (
|
|
44
|
-
- `awsClientCapture` (function) (
|
|
45
|
-
- `httpsCapture` (function) (
|
|
52
|
+
- `awsClientOptions` (object) (default `undefined`): Options to pass to AWS.STS class constructor.
|
|
53
|
+
- `awsClientAssumeRole` (string) (default `undfined`): Internal key where secrets are stored. See [@middy/sts](/packages/sts/README.md) on to set this.
|
|
54
|
+
- `awsClientCapture` (function) (default `undefined`): Enable XRay by passing `captureAWSClient` from `aws-xray-sdk` in.
|
|
55
|
+
- `httpsCapture` (function) (default `undefined`): Enable XRay by passing `captureHTTPsGlobal` from `aws-xray-sdk` in.
|
|
46
56
|
- `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
|
|
47
57
|
|
|
48
58
|
NOTES:
|
|
@@ -55,6 +65,7 @@ NOTES:
|
|
|
55
65
|
import zlib from 'zlib'
|
|
56
66
|
import middy from '@middy/core'
|
|
57
67
|
import s3ObjectResponse from '@middy/s3-object-response'
|
|
68
|
+
import {captureAWSClient, captureHTTPsGlobal} from 'aws-xray-sdk-core'
|
|
58
69
|
|
|
59
70
|
const handler = middy((event, context) => {
|
|
60
71
|
const readStream = context.s3Object
|
|
@@ -66,6 +77,8 @@ const handler = middy((event, context) => {
|
|
|
66
77
|
|
|
67
78
|
handler
|
|
68
79
|
.use(s3ObjectResponse({
|
|
80
|
+
awsClientCapture: captureAWSClient,
|
|
81
|
+
httpsCapture:captureHTTPsGlobal,
|
|
69
82
|
bodyType: 'stream'
|
|
70
83
|
}))
|
|
71
84
|
```
|
|
@@ -75,6 +88,7 @@ handler
|
|
|
75
88
|
import zlib from 'zlib'
|
|
76
89
|
import middy from '@middy/core'
|
|
77
90
|
import s3ObjectResponse from '@middy/s3-object-response'
|
|
91
|
+
import {captureAWSClient, captureHTTPsGlobal} from 'aws-xray-sdk-core'
|
|
78
92
|
|
|
79
93
|
const handler = middy(async (event, context) => {
|
|
80
94
|
let body = await context.s3Object
|
|
@@ -86,6 +100,8 @@ const handler = middy(async (event, context) => {
|
|
|
86
100
|
|
|
87
101
|
handler
|
|
88
102
|
.use(s3ObjectResponse({
|
|
103
|
+
awsClientCapture: captureAWSClient,
|
|
104
|
+
httpsCapture:captureHTTPsGlobal,
|
|
89
105
|
bodyType: 'promise'
|
|
90
106
|
}))
|
|
91
107
|
```
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
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
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,102 +1,3 @@
|
|
|
1
|
-
import __https from
|
|
2
|
-
import { URL } from 'url';
|
|
3
|
-
import { canPrefetch, createPrefetchClient, createClient } from '@middy/util';
|
|
4
|
-
import S3 from 'aws-sdk/clients/s3.js';
|
|
5
|
-
const defaults = {
|
|
6
|
-
AwsClient: S3,
|
|
7
|
-
awsClientOptions: {},
|
|
8
|
-
awsClientAssumeRole: undefined,
|
|
9
|
-
awsClientCapture: undefined,
|
|
10
|
-
httpsCapture: undefined,
|
|
11
|
-
disablePrefetch: false,
|
|
12
|
-
bodyType: undefined,
|
|
13
|
-
__https
|
|
14
|
-
};
|
|
15
|
-
let https = __https;
|
|
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
|
|
16
2
|
|
|
17
|
-
|
|
18
|
-
const options = { ...defaults,
|
|
19
|
-
...opts
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
if (!['stream', 'promise'].includes(options.bodyType)) {
|
|
23
|
-
throw new Error('[s3-object-response] bodyType is invalid');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (options.httpsCapture) {
|
|
27
|
-
https = options.httpsCapture(options.__https);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let client;
|
|
31
|
-
|
|
32
|
-
if (canPrefetch(options)) {
|
|
33
|
-
client = createPrefetchClient(options);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const s3ObjectResponseMiddlewareBefore = async request => {
|
|
37
|
-
const {
|
|
38
|
-
inputS3Url,
|
|
39
|
-
outputRoute,
|
|
40
|
-
outputToken
|
|
41
|
-
} = request.event.getObjectContext;
|
|
42
|
-
request.internal.s3ObjectResponse = {
|
|
43
|
-
RequestRoute: outputRoute,
|
|
44
|
-
RequestToken: outputToken
|
|
45
|
-
};
|
|
46
|
-
const parsedInputS3Url = new URL(inputS3Url);
|
|
47
|
-
const fetchOptions = {
|
|
48
|
-
method: 'GET',
|
|
49
|
-
host: parsedInputS3Url.hostname,
|
|
50
|
-
path: parsedInputS3Url.pathname
|
|
51
|
-
};
|
|
52
|
-
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const s3ObjectResponseMiddlewareAfter = async request => {
|
|
56
|
-
var _request$response;
|
|
57
|
-
|
|
58
|
-
if (!client) {
|
|
59
|
-
client = await createClient(options, request);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
(_request$response = request.response).Body ?? (_request$response.Body = request.response.body);
|
|
63
|
-
delete request.response.body;
|
|
64
|
-
await client.writeGetObjectResponse({ ...request.response,
|
|
65
|
-
...request.internal.s3ObjectResponse
|
|
66
|
-
}).promise();
|
|
67
|
-
return {
|
|
68
|
-
statusCode: 200
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
before: s3ObjectResponseMiddlewareBefore,
|
|
74
|
-
after: s3ObjectResponseMiddlewareAfter
|
|
75
|
-
};
|
|
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
|
-
|
|
86
|
-
const fetchStream = fetchOptions => {
|
|
87
|
-
return https.request(fetchOptions);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const fetchPromise = fetchOptions => {
|
|
91
|
-
return new Promise((resolve, reject) => {
|
|
92
|
-
let data = '';
|
|
93
|
-
const stream = fetchStream(fetchOptions);
|
|
94
|
-
stream.on('data', chunk => {
|
|
95
|
-
data += chunk;
|
|
96
|
-
});
|
|
97
|
-
stream.on('end', () => resolve(data));
|
|
98
|
-
stream.on('error', error => reject(error));
|
|
99
|
-
});
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export default s3ObjectResponseMiddleware;
|
|
3
|
+
//# 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.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "S3 object response handling 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": {
|
|
@@ -46,14 +53,14 @@
|
|
|
46
53
|
"bugs": {
|
|
47
54
|
"url": "https://github.com/middyjs/middy/issues"
|
|
48
55
|
},
|
|
49
|
-
"homepage": "https://
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
50
57
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0
|
|
58
|
+
"@middy/util": "^3.0.0"
|
|
52
59
|
},
|
|
53
60
|
"devDependencies": {
|
|
54
|
-
"@middy/core": "^3.0.0
|
|
61
|
+
"@middy/core": "^3.0.0",
|
|
55
62
|
"aws-sdk": "^2.939.0",
|
|
56
63
|
"aws-xray-sdk": "^3.3.3"
|
|
57
64
|
},
|
|
58
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
59
66
|
}
|