@middy/s3-object-response 2.5.6 → 2.5.7
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 +61 -54
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,109 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
let https = require('https');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
URL
|
|
7
|
+
} = require('url');
|
|
3
8
|
|
|
4
9
|
const {
|
|
5
10
|
canPrefetch,
|
|
6
11
|
createPrefetchClient,
|
|
7
12
|
createClient
|
|
8
|
-
} = require('@middy/util')
|
|
13
|
+
} = require('@middy/util');
|
|
9
14
|
|
|
10
|
-
const S3 = require('aws-sdk/clients/s3') // v2
|
|
15
|
+
const S3 = require('aws-sdk/clients/s3'); // v2
|
|
11
16
|
// const { S3 } = require('@aws-sdk/client-s3') // v3
|
|
12
17
|
|
|
18
|
+
|
|
13
19
|
const defaults = {
|
|
14
|
-
AwsClient: S3,
|
|
20
|
+
AwsClient: S3,
|
|
21
|
+
// Allow for XRay
|
|
15
22
|
awsClientOptions: {},
|
|
16
23
|
awsClientAssumeRole: undefined,
|
|
17
24
|
awsClientCapture: undefined,
|
|
18
25
|
httpsCapture: undefined,
|
|
19
26
|
disablePrefetch: false,
|
|
20
27
|
bodyType: undefined
|
|
21
|
-
}
|
|
28
|
+
};
|
|
22
29
|
|
|
23
30
|
const s3ObjectResponseMiddleware = (opts = {}) => {
|
|
24
|
-
const options = { ...defaults,
|
|
31
|
+
const options = { ...defaults,
|
|
32
|
+
...opts
|
|
33
|
+
};
|
|
25
34
|
|
|
26
35
|
if (!['stream', 'promise'].includes(options.bodyType)) {
|
|
27
|
-
throw new Error('bodyType is invalid.')
|
|
36
|
+
throw new Error('bodyType is invalid.');
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
if (options.httpsCapture) {
|
|
31
|
-
https = options.httpsCapture(https)
|
|
40
|
+
https = options.httpsCapture(https);
|
|
32
41
|
}
|
|
33
42
|
|
|
34
|
-
let client
|
|
43
|
+
let client;
|
|
44
|
+
|
|
35
45
|
if (canPrefetch(options)) {
|
|
36
|
-
client = createPrefetchClient(options)
|
|
46
|
+
client = createPrefetchClient(options);
|
|
37
47
|
}
|
|
38
48
|
|
|
39
|
-
const s3ObjectResponseMiddlewareBefore = async
|
|
49
|
+
const s3ObjectResponseMiddlewareBefore = async request => {
|
|
40
50
|
const {
|
|
41
51
|
inputS3Url,
|
|
42
52
|
outputRoute,
|
|
43
53
|
outputToken
|
|
44
|
-
} = request.event.getObjectContext
|
|
45
|
-
|
|
54
|
+
} = request.event.getObjectContext;
|
|
46
55
|
request.internal.s3ObjectResponse = {
|
|
47
56
|
RequestRoute: outputRoute,
|
|
48
57
|
RequestToken: outputToken
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const parsedInputS3Url = new URL(inputS3Url)
|
|
58
|
+
};
|
|
59
|
+
const parsedInputS3Url = new URL(inputS3Url);
|
|
52
60
|
const fetchOptions = {
|
|
53
61
|
method: 'GET',
|
|
54
62
|
host: parsedInputS3Url.hostname,
|
|
55
63
|
path: parsedInputS3Url.pathname
|
|
56
|
-
}
|
|
64
|
+
};
|
|
65
|
+
request.context.s3Object = fetchType(options.bodyType, fetchOptions);
|
|
66
|
+
};
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
const s3ObjectResponseMiddlewareAfter = async request => {
|
|
69
|
+
var _request$response$Bod;
|
|
60
70
|
|
|
61
|
-
const s3ObjectResponseMiddlewareAfter = async (request) => {
|
|
62
71
|
if (!client) {
|
|
63
|
-
client = await createClient(options, request)
|
|
72
|
+
client = await createClient(options, request);
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
request.response.Body = request.response.Body
|
|
67
|
-
delete request.response.body
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
.promise()
|
|
75
|
-
.then(() => ({ statusCode: 200 })) // TODO test if needed
|
|
76
|
-
}
|
|
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
|
+
};
|
|
77
83
|
|
|
78
84
|
return {
|
|
79
85
|
before: s3ObjectResponseMiddlewareBefore,
|
|
80
86
|
after: s3ObjectResponseMiddlewareAfter
|
|
81
|
-
}
|
|
82
|
-
}
|
|
87
|
+
};
|
|
88
|
+
};
|
|
83
89
|
|
|
84
90
|
const fetchType = (type, fetchOptions) => {
|
|
85
91
|
if (type === 'stream') {
|
|
86
|
-
return fetchStream(fetchOptions)
|
|
92
|
+
return fetchStream(fetchOptions);
|
|
87
93
|
} else if (type === 'promise') {
|
|
88
|
-
return fetchPromise(fetchOptions)
|
|
94
|
+
return fetchPromise(fetchOptions);
|
|
89
95
|
}
|
|
90
|
-
return null
|
|
91
|
-
}
|
|
92
96
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
97
|
+
return null;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const fetchStream = fetchOptions => {
|
|
101
|
+
return https.request(fetchOptions);
|
|
102
|
+
};
|
|
96
103
|
|
|
97
|
-
const fetchPromise =
|
|
104
|
+
const fetchPromise = fetchOptions => {
|
|
98
105
|
return new Promise((resolve, reject) => {
|
|
99
|
-
let data = ''
|
|
100
|
-
const stream = fetchStream(fetchOptions)
|
|
101
|
-
stream.on('data',
|
|
102
|
-
data += chunk
|
|
103
|
-
})
|
|
104
|
-
stream.on('end', () => resolve(data))
|
|
105
|
-
stream.on('error',
|
|
106
|
-
})
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
module.exports = s3ObjectResponseMiddleware
|
|
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/s3-object-response",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.7",
|
|
4
4
|
"description": "S3 object response handling middleware for the middy framework",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"engines": {
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^2.5.
|
|
49
|
+
"@middy/util": "^2.5.7"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^2.5.
|
|
52
|
+
"@middy/core": "^2.5.7",
|
|
53
53
|
"aws-sdk": "^2.939.0",
|
|
54
54
|
"aws-xray-sdk": "^3.3.3"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "3983c4b138e1a4d7fcb3ed805d3b8832fff06fc1"
|
|
57
57
|
}
|