@middy/s3-object-response 2.5.4 → 3.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.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/package.json +7 -7
- package/index.js +0 -116
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell 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
|
@@ -104,7 +104,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
104
104
|
|
|
105
105
|
## License
|
|
106
106
|
|
|
107
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
107
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
108
108
|
|
|
109
109
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
110
110
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/s3-object-response",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
4
|
"description": "S3 object response handling middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
13
|
+
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"index.d.ts"
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^
|
|
49
|
+
"@middy/util": "^3.0.0-alpha.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^
|
|
52
|
+
"@middy/core": "^3.0.0-alpha.0",
|
|
53
53
|
"aws-sdk": "^2.939.0",
|
|
54
54
|
"aws-xray-sdk": "^3.3.3"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
|
|
57
57
|
}
|
package/index.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
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;
|