@middy/s3-object-response 3.0.0-alpha.0 → 3.0.0-alpha.1
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/index.js +103 -0
- package/package.json +5 -4
package/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import __https from 'https';
|
|
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;
|
|
16
|
+
|
|
17
|
+
const s3ObjectResponseMiddleware = (opts = {}) => {
|
|
18
|
+
const options = { ...defaults,
|
|
19
|
+
...opts
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
if (!['stream', 'promise'].includes(options.bodyType)) {
|
|
23
|
+
throw new Error('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
|
+
return client.writeGetObjectResponse({ ...request.response,
|
|
65
|
+
...request.internal.s3ObjectResponse
|
|
66
|
+
}).promise().then(() => ({
|
|
67
|
+
statusCode: 200
|
|
68
|
+
}));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
before: s3ObjectResponseMiddlewareBefore,
|
|
73
|
+
after: s3ObjectResponseMiddlewareAfter
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const fetchType = (type, fetchOptions) => {
|
|
78
|
+
if (type === 'stream') {
|
|
79
|
+
return fetchStream(fetchOptions);
|
|
80
|
+
} else if (type === 'promise') {
|
|
81
|
+
return fetchPromise(fetchOptions);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return null;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const fetchStream = fetchOptions => {
|
|
88
|
+
return https.request(fetchOptions);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const fetchPromise = fetchOptions => {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
let data = '';
|
|
94
|
+
const stream = fetchStream(fetchOptions);
|
|
95
|
+
stream.on('data', chunk => {
|
|
96
|
+
data += chunk;
|
|
97
|
+
});
|
|
98
|
+
stream.on('end', () => resolve(data));
|
|
99
|
+
stream.on('error', error => reject(error));
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export default s3ObjectResponseMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/s3-object-response",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "S3 object response handling middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
@@ -46,12 +47,12 @@
|
|
|
46
47
|
},
|
|
47
48
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
49
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^3.0.0-alpha.
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.1"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^3.0.0-alpha.
|
|
53
|
+
"@middy/core": "^3.0.0-alpha.1",
|
|
53
54
|
"aws-sdk": "^2.939.0",
|
|
54
55
|
"aws-xray-sdk": "^3.3.3"
|
|
55
56
|
},
|
|
56
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
|
|
57
58
|
}
|