@middy/s3-object-response 2.5.2
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 +21 -0
- package/README.md +111 -0
- package/index.d.ts +16 -0
- package/index.js +116 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
<div align="center">
|
|
8
|
+
<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
|
+
<p>
|
|
13
|
+
<a href="http://badge.fury.io/js/%40middy%2Fs3-object-response">
|
|
14
|
+
<img src="https://badge.fury.io/js/%40middy%2Fs3-object-response.svg" alt="npm version" style="max-width:100%;">
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
|
+
<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
|
+
</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%;">
|
|
21
|
+
</a>
|
|
22
|
+
<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%;">
|
|
24
|
+
</a>
|
|
25
|
+
</p>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
** This middleware is a Proof of Concept and requires real world testing before use, not recommended for production **
|
|
29
|
+
|
|
30
|
+
Fetches S3 object as a stream and writes back to s3 object response.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
To install this middleware you can use NPM:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install --save @middy/s3-object-response
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Options
|
|
42
|
+
- `bodyType` (string) (required): How to pass in the s3 object through the handler. Can be `stream` or `promise`.
|
|
43
|
+
- `AwsClient` (object) (default `AWS.S3`): AWS.STS class constructor (e.g. that has been instrumented with AWS XRay). Must be from `aws-sdk` v2.
|
|
44
|
+
- `awsClientOptions` (object) (optional): Options to pass to AWS.STS class constructor.
|
|
45
|
+
- `awsClientCapture` (function) (optional): Enable XRay by passing `captureAWSClient` from `aws-xray-sdk` in.
|
|
46
|
+
- `httpsCapture` (function) (optional): Enable XRay by passing `captureHTTPsGlobal` from `aws-xray-sdk` in.
|
|
47
|
+
- `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
|
|
48
|
+
|
|
49
|
+
NOTES:
|
|
50
|
+
- The response from the handler must match the allowed parameters for [`S3.writeGetObjectResponse`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#writeGetObjectResponse-property), excluding `RequestRoute` and `RequestToken`.
|
|
51
|
+
- Lambda is required to have IAM permission for `s3-object-lambda:WriteGetObjectResponse`
|
|
52
|
+
|
|
53
|
+
## Sample usage
|
|
54
|
+
### Stream
|
|
55
|
+
```javascript
|
|
56
|
+
import zlib from 'zlib'
|
|
57
|
+
import middy from '@middy/core'
|
|
58
|
+
import s3ObjectResponse from '@middy/s3-object-response'
|
|
59
|
+
|
|
60
|
+
const handler = middy((event, context) => {
|
|
61
|
+
const readStream = context.s3Object
|
|
62
|
+
const transformStream = zlib.createBrotliCompress()
|
|
63
|
+
return {
|
|
64
|
+
Body: readStream.pipe(transformStream)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
handler
|
|
69
|
+
.use(s3ObjectResponse({
|
|
70
|
+
bodyType: 'stream'
|
|
71
|
+
}))
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Promise
|
|
75
|
+
```javascript
|
|
76
|
+
import zlib from 'zlib'
|
|
77
|
+
import middy from '@middy/core'
|
|
78
|
+
import s3ObjectResponse from '@middy/s3-object-response'
|
|
79
|
+
|
|
80
|
+
const handler = middy(async (event, context) => {
|
|
81
|
+
let body = await context.s3Object
|
|
82
|
+
// change body
|
|
83
|
+
return {
|
|
84
|
+
Body: JSON.stringify(body)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
handler
|
|
89
|
+
.use(s3ObjectResponse({
|
|
90
|
+
bodyType: 'promise'
|
|
91
|
+
}))
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
## Middy documentation and examples
|
|
96
|
+
|
|
97
|
+
For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## Contributing
|
|
101
|
+
|
|
102
|
+
Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
108
|
+
|
|
109
|
+
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
110
|
+
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
|
111
|
+
</a>
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { S3 } from 'aws-sdk'
|
|
2
|
+
import { captureAWSClient } from 'aws-xray-sdk'
|
|
3
|
+
import middy from '@middy/core'
|
|
4
|
+
|
|
5
|
+
interface Options<S = S3> {
|
|
6
|
+
AwsClient?: new() => S
|
|
7
|
+
awsClientOptions?: Partial<S3.Types.ClientConfiguration>
|
|
8
|
+
awsClientAssumeRole?: string
|
|
9
|
+
awsClientCapture?: typeof captureAWSClient
|
|
10
|
+
disablePrefetch?: boolean
|
|
11
|
+
bodyType?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function s3ObjectResponse (options?: Options): middy.MiddlewareObj
|
|
15
|
+
|
|
16
|
+
export default s3ObjectResponse
|
package/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
let https = require('https');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
URL
|
|
7
|
+
} = require('url');
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
canPrefetch,
|
|
11
|
+
createPrefetchClient,
|
|
12
|
+
createClient
|
|
13
|
+
} = require('@middy/util');
|
|
14
|
+
|
|
15
|
+
const S3 = require('aws-sdk/clients/s3'); // v2
|
|
16
|
+
// const { S3 } = require('@aws-sdk/client-s3') // v3
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const defaults = {
|
|
20
|
+
AwsClient: S3,
|
|
21
|
+
// Allow for XRay
|
|
22
|
+
awsClientOptions: {},
|
|
23
|
+
awsClientAssumeRole: undefined,
|
|
24
|
+
awsClientCapture: undefined,
|
|
25
|
+
httpsCapture: undefined,
|
|
26
|
+
disablePrefetch: false,
|
|
27
|
+
bodyType: undefined
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const s3ObjectResponseMiddleware = (opts = {}) => {
|
|
31
|
+
const options = { ...defaults,
|
|
32
|
+
...opts
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (!['stream', 'promise'].includes(options.bodyType)) {
|
|
36
|
+
throw new Error('bodyType is invalid.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (options.httpsCapture) {
|
|
40
|
+
https = options.httpsCapture(https);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let client;
|
|
44
|
+
|
|
45
|
+
if (canPrefetch(options)) {
|
|
46
|
+
client = createPrefetchClient(options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const s3ObjectResponseMiddlewareBefore = async request => {
|
|
50
|
+
const {
|
|
51
|
+
inputS3Url,
|
|
52
|
+
outputRoute,
|
|
53
|
+
outputToken
|
|
54
|
+
} = request.event.getObjectContext;
|
|
55
|
+
request.internal.s3ObjectResponse = {
|
|
56
|
+
RequestRoute: outputRoute,
|
|
57
|
+
RequestToken: outputToken
|
|
58
|
+
};
|
|
59
|
+
const parsedInputS3Url = new URL(inputS3Url);
|
|
60
|
+
const fetchOptions = {
|
|
61
|
+
method: 'GET',
|
|
62
|
+
host: parsedInputS3Url.hostname,
|
|
63
|
+
path: parsedInputS3Url.pathname
|
|
64
|
+
};
|
|
65
|
+
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const s3ObjectResponseMiddlewareAfter = async request => {
|
|
69
|
+
var _request$response$Bod;
|
|
70
|
+
|
|
71
|
+
if (!client) {
|
|
72
|
+
client = await createClient(options, request);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
request.response.Body = (_request$response$Bod = request.response.Body) !== null && _request$response$Bod !== void 0 ? _request$response$Bod : request.response.body;
|
|
76
|
+
delete request.response.body;
|
|
77
|
+
return client.writeGetObjectResponse({ ...request.response,
|
|
78
|
+
...request.internal.s3ObjectResponse
|
|
79
|
+
}).promise().then(() => ({
|
|
80
|
+
statusCode: 200
|
|
81
|
+
})); // TODO test if needed
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
before: s3ObjectResponseMiddlewareBefore,
|
|
86
|
+
after: s3ObjectResponseMiddlewareAfter
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const fetchType = (type, fetchOptions) => {
|
|
91
|
+
if (type === 'stream') {
|
|
92
|
+
return fetchStream(fetchOptions);
|
|
93
|
+
} else if (type === 'promise') {
|
|
94
|
+
return fetchPromise(fetchOptions);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return null;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const fetchStream = fetchOptions => {
|
|
101
|
+
return https.request(fetchOptions);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const fetchPromise = fetchOptions => {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
let data = '';
|
|
107
|
+
const stream = fetchStream(fetchOptions);
|
|
108
|
+
stream.on('data', chunk => {
|
|
109
|
+
data += chunk;
|
|
110
|
+
});
|
|
111
|
+
stream.on('end', () => resolve(data));
|
|
112
|
+
stream.on('error', error => reject(error));
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
module.exports = s3ObjectResponseMiddleware;
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@middy/s3-object-response",
|
|
3
|
+
"version": "2.5.2",
|
|
4
|
+
"description": "S3 object response handling middleware for the middy framework",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=12"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"main": "index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"index.d.ts"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "npm run test:unit",
|
|
20
|
+
"test:unit": "ava"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"Lambda",
|
|
25
|
+
"Middleware",
|
|
26
|
+
"Serverless",
|
|
27
|
+
"Framework",
|
|
28
|
+
"AWS",
|
|
29
|
+
"AWS Lambda",
|
|
30
|
+
"Middy",
|
|
31
|
+
"S3",
|
|
32
|
+
"GetObjectResponse",
|
|
33
|
+
"WriteObjectREsponse"
|
|
34
|
+
],
|
|
35
|
+
"author": {
|
|
36
|
+
"name": "Middy contributors",
|
|
37
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "github:middyjs/middy",
|
|
42
|
+
"directory": "packages/sts"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@middy/util": "^2.5.2"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@middy/core": "^2.5.2",
|
|
53
|
+
"aws-sdk": "^2.939.0",
|
|
54
|
+
"aws-xray-sdk": "^3.3.3"
|
|
55
|
+
},
|
|
56
|
+
"gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
|
|
57
|
+
}
|