|
1 | | -const { S3 } = require('aws-sdk'); |
| 1 | +const { S3Client } = require('@aws-sdk/client-s3'); |
| 2 | +const { HttpRequest } = require('@smithy/protocol-http'); |
2 | 3 | const querystring = require('querystring'); |
3 | 4 |
|
4 | 5 | const getConfig = require('../../test/support/config'); |
5 | 6 |
|
6 | | -const config = getConfig('default', { signatureVersion: 'v4' }); |
7 | | -const s3 = new S3(config); |
| 7 | +const config = getConfig('default'); |
| 8 | + |
| 9 | + |
| 10 | +// Custom middleware to modify requests without mutating args |
| 11 | +const customRequestMiddleware = buildParams => next => async args => { |
8 | 12 |
|
9 | | -function customS3Request(action, params, buildParams, callback) { |
10 | | - const method = action.bind(s3); |
11 | | - const request = method(params); |
12 | 13 | const { headers, query } = buildParams; |
13 | | - // modify underlying http request object created by aws sdk |
14 | | - request.on('build', () => { |
15 | | - Object.assign(request.httpRequest.headers, headers); |
16 | | - if (query) { |
17 | | - const qs = querystring.stringify(query); |
18 | | - // NOTE: that this relies on there not being a query string in the |
19 | | - // first place; if there is a qs then we have to search for ? and |
20 | | - // append &qs at the end of the string, if ? is not followed by '' |
21 | | - request.httpRequest.path = `${request.httpRequest.path}?${qs}`; |
22 | | - } |
23 | | - }); |
24 | | - request.on('success', response => { |
25 | | - const resData = { |
26 | | - statusCode: response.httpResponse.statusCode, |
27 | | - headers: response.httpResponse.headers, |
28 | | - body: response.httpResponse.body.toString('utf8'), |
29 | | - }; |
30 | | - callback(null, resData); |
| 14 | + |
| 15 | + const prevReq = args.request; |
| 16 | + const base = prevReq instanceof HttpRequest ? prevReq : new HttpRequest(prevReq); |
| 17 | + |
| 18 | + let newHeaders = base.headers || {}; |
| 19 | + if (headers) { |
| 20 | + newHeaders = { ...newHeaders, ...headers }; |
| 21 | + } |
| 22 | + |
| 23 | + let newQuery = base.query || {}; |
| 24 | + if (query) { |
| 25 | + const extra = querystring.parse(querystring.stringify(query)); |
| 26 | + newQuery = { ...newQuery, ...extra }; |
| 27 | + } |
| 28 | + |
| 29 | + const newReq = new HttpRequest({ |
| 30 | + ...base, |
| 31 | + headers: newHeaders, |
| 32 | + query: newQuery, |
31 | 33 | }); |
32 | | - request.on('error', err => { |
| 34 | + |
| 35 | + return next({ ...args, request: newReq }); |
| 36 | +}; |
| 37 | + |
| 38 | +async function customS3Request(CommandClass, params, buildParams) { |
| 39 | + const customS3 = new S3Client({ ...config }); |
| 40 | + |
| 41 | + customS3.middlewareStack.add( |
| 42 | + customRequestMiddleware(buildParams), |
| 43 | + { step: 'build', name: 'customRequestMiddleware', tags: ['CUSTOM'] } |
| 44 | + ); |
| 45 | + |
| 46 | + const command = new CommandClass(params); |
| 47 | + const response = await customS3.send(command); |
| 48 | + |
33 | 49 | const resData = { |
34 | | - statusCode: request.response.httpResponse.statusCode, |
35 | | - headers: request.response.httpResponse.headers, |
36 | | - body: request.response.httpResponse.body.toString('utf8'), |
| 50 | + statusCode: 200, |
| 51 | + headers: response.$metadata?.httpHeaders || {}, |
| 52 | + body: JSON.stringify(response), |
37 | 53 | }; |
38 | | - callback(err, resData); |
39 | | - }); |
40 | | - request.send(); |
| 54 | + |
| 55 | + return resData; |
| 56 | + |
41 | 57 | } |
42 | 58 |
|
43 | 59 | module.exports = customS3Request; |
0 commit comments