Skip to content

Commit c5450c4

Browse files
committed
versionning utilities migration
Issue: CLDSRV-724
1 parent c1fb5b5 commit c5450c4

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed
Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,59 @@
1-
const { S3 } = require('aws-sdk');
1+
const { S3Client } = require('@aws-sdk/client-s3');
2+
const { HttpRequest } = require('@smithy/protocol-http');
23
const querystring = require('querystring');
34

45
const getConfig = require('../../test/support/config');
56

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 => {
812

9-
function customS3Request(action, params, buildParams, callback) {
10-
const method = action.bind(s3);
11-
const request = method(params);
1213
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,
3133
});
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+
3349
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),
3753
};
38-
callback(err, resData);
39-
});
40-
request.send();
54+
55+
return resData;
56+
4157
}
4258

4359
module.exports = customS3Request;

tests/functional/aws-node-sdk/lib/utility/versioning-util.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ function _deleteVersionList(versionList, bucket, callback) {
2828
return s3Client.send(new DeleteObjectsCommand(params)).then(() => callback()).catch(err => callback(err));
2929
}
3030

31-
function checkOneVersion(s3, bucket, versionId, callback) {
31+
function checkOneVersion(s3, bucket, versionId) {
3232
return s3Client.send(new ListObjectVersionsCommand({ Bucket: bucket })).then(data => {
3333
assert.strictEqual(data.Versions.length, 1);
3434
if (versionId) {
3535
assert.strictEqual(data.Versions[0].VersionId, versionId);
3636
}
37-
assert.strictEqual(data.DeleteMarkers.length, 0);
38-
callback();
39-
}).catch(err => callback(err));
37+
assert.strictEqual(data.DeleteMarkers?.length, undefined);
38+
});
4039
}
4140

4241
function removeAllVersions(params, callback) {

0 commit comments

Comments
 (0)