Skip to content

Commit 5a60237

Browse files
committed
Upgrafde aws sdk
1 parent f7153bd commit 5a60237

File tree

8 files changed

+8803
-5354
lines changed

8 files changed

+8803
-5354
lines changed

package-lock.json

Lines changed: 8734 additions & 5207 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
"release": "npm run build && npm run package && npm run deploy"
2626
},
2727
"devDependencies": {
28+
"@aws-sdk/client-cloudformation": "^3.507.0",
29+
"@aws-sdk/client-s3": "^3.507.0",
2830
"@types/jest": "^29.5.0",
2931
"@types/minimatch": "^5.1.2",
3032
"@types/node": "^18.16.3",
3133
"@typescript-eslint/eslint-plugin": "^5.59.6",
3234
"@typescript-eslint/parser": "^5.59.6",
33-
"aws-sdk": "^2.1381.0",
3435
"deepmerge": "^4.3.1",
3536
"eslint": "^8.41.0",
3637
"jest": "^29.5.0",

src/aws-helper.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/s3-upload-config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Minimatch } from "minimatch";
22
import deepmerge from "deepmerge";
3-
import S3 from "aws-sdk/clients/s3";
3+
import { PutObjectRequest } from "@aws-sdk/client-s3";
44

55
/** Format of the config file */
66
interface S3UploadFileStructure {
@@ -46,14 +46,14 @@ class S3UploadConfig {
4646
* @param objectKey The key name to match against in the config
4747
* @returns A partial S3.PutObjectRequest object based off the configuration
4848
*/
49-
public getS3ParamsForKey(objectKey: string): S3.PutObjectRequest {
49+
public getS3ParamsForKey(objectKey: string): PutObjectRequest {
5050
let mergedConfig: S3ObjectConfig = {};
5151
for (const [matcher, config] of this.parsedS3UploadConfig) {
5252
if (matcher.match(objectKey)) {
5353
mergedConfig = deepmerge(mergedConfig, config);
5454
}
5555
}
56-
const putObjectRequest = {} as S3.PutObjectRequest;
56+
const putObjectRequest = {} as PutObjectRequest;
5757
if (mergedConfig.metadata) {
5858
putObjectRequest.Metadata = mergedConfig.metadata;
5959
}

src/simple-s3.ts

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,59 @@
1-
import S3 from "aws-sdk/clients/s3";
2-
import { autoPaginate } from "./aws-helper";
1+
import { S3, paginateListObjectsV2, PutObjectRequest } from "@aws-sdk/client-s3";
32

43
export class SimpleS3 {
5-
private readonly s3: S3 = new S3();
4+
private readonly s3 = new S3();
65

76
public async deleteObject(bucketName: string, key: string): Promise<void> {
8-
await this.s3.deleteObject({ Bucket: bucketName, Key: key }).promise();
7+
await this.s3.deleteObject({ Bucket: bucketName, Key: key });
98
}
109

1110
public async listObjects(
1211
bucketName: string,
1312
objectPrefix: string | undefined
1413
): Promise<string[]> {
15-
const response = await autoPaginate<S3, S3.ListObjectsV2Request, S3.ListObjectsV2Output>(
16-
this.s3,
17-
// eslint-disable-next-line @typescript-eslint/unbound-method
18-
this.s3.listObjectsV2,
14+
const keys: string[] = [];
15+
for await (const page of paginateListObjectsV2(
16+
{ client: this.s3 },
1917
{
2018
Bucket: bucketName,
2119
Prefix: objectPrefix,
2220
}
23-
);
24-
const keys = response.reduce(
25-
(acc: string[], page) =>
26-
acc.concat(page.Contents?.map((object) => object?.Key) as string[]),
27-
[]
28-
);
21+
)) {
22+
for (const object of page.Contents ?? []) {
23+
if (object.Key) {
24+
keys.push(object.Key);
25+
}
26+
}
27+
}
2928
return keys;
3029
}
3130

3231
public async isBucketEmpty(
3332
bucketName: string,
3433
objectPrefix: string | undefined
3534
): Promise<boolean> {
36-
return await this.s3
37-
.listObjectsV2({
38-
Bucket: bucketName,
39-
Prefix: objectPrefix,
40-
MaxKeys: 1,
41-
})
42-
.promise()
43-
.then((data) => {
44-
if (data.KeyCount && data.KeyCount > 0) {
45-
return false;
46-
} else {
47-
return true;
48-
}
49-
});
35+
const objects = await this.s3.listObjectsV2({
36+
Bucket: bucketName,
37+
Prefix: objectPrefix,
38+
MaxKeys: 1,
39+
});
40+
return (objects.KeyCount ?? 0) === 0;
5041
}
5142

5243
public async uploadFile(
5344
bucketName: string,
5445
objectPrefix: string | undefined,
5546
localFilePath: string,
5647
localFileContents: Buffer,
57-
extraParams?: S3.PutObjectRequest
48+
extraParams?: PutObjectRequest
5849
): Promise<void> {
59-
const key = (objectPrefix || "") + localFilePath;
50+
const key = (objectPrefix ?? "") + localFilePath;
6051

61-
await this.s3
62-
.upload({
63-
Bucket: bucketName,
64-
Key: key,
65-
Body: localFileContents,
66-
...extraParams,
67-
})
68-
.promise();
52+
await this.s3.putObject({
53+
Bucket: bucketName,
54+
Key: key,
55+
Body: localFileContents,
56+
...extraParams,
57+
});
6958
}
7059
}

tst/integration.test.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import process from "process";
2-
import { CloudFormation, S3 } from "aws-sdk/clients/all";
2+
import {
3+
CloudFormation,
4+
ListStacksOutput,
5+
paginateListStacks,
6+
StackSummary,
7+
} from "@aws-sdk/client-cloudformation";
8+
import { S3 } from "@aws-sdk/client-s3";
39
import { SimpleFs } from "../src/simple-fs";
410
import path from "path";
511
import childProcess from "child_process";
612
import os from "os";
7-
import { autoPaginate } from "../src/aws-helper";
813

914
import {
1015
fooBarContents1,
@@ -44,7 +49,7 @@ async function compareBucketContents(
4449
ContentDisposition?: string;
4550
CacheControl?: string;
4651
};
47-
const response = await s3.listObjectsV2({ Bucket: bucketName }).promise();
52+
const response = await s3.listObjectsV2({ Bucket: bucketName });
4853
const simpleResponse: ObjectDescription[] =
4954
response.Contents?.map((c) => ({
5055
Key: c.Key as string,
@@ -54,9 +59,7 @@ async function compareBucketContents(
5459

5560
await Promise.all(
5661
simpleResponse.map(async (response) => {
57-
const description = await s3
58-
.headObject({ Bucket: bucketName, Key: response.Key })
59-
.promise();
62+
const description = await s3.headObject({ Bucket: bucketName, Key: response.Key });
6063
response.Metadata = description.Metadata;
6164
response.ContentType = description.ContentType;
6265

@@ -175,33 +178,24 @@ async function createStackAndExpectFailure(
175178

176179
await deleteStackIfExists(cloudFormation, stackName);
177180

178-
/* eslint-disable @typescript-eslint/unbound-method */
179-
const stacks: CloudFormation.ListStacksOutput[] = await autoPaginate<
180-
CloudFormation,
181-
CloudFormation.ListStacksInput,
182-
CloudFormation.ListStacksOutput
183-
>(cloudFormation, cloudFormation.listStacks, {
184-
StackStatusFilter: ["DELETE_COMPLETE"],
185-
});
186-
/* eslint-enable */
181+
const stacks: ListStacksOutput[] = [];
182+
for await (const page of paginateListStacks(
183+
{ client: cloudFormation },
184+
{ StackStatusFilter: ["DELETE_COMPLETE"] }
185+
)) {
186+
stacks.push(page);
187+
}
187188

188189
const mostRecentStackId = stacks
189-
.reduce(
190-
(acc: CloudFormation.StackSummary[], response) =>
191-
acc.concat(response.StackSummaries || []),
192-
[]
193-
)
190+
.reduce((acc: StackSummary[], response) => acc.concat(response.StackSummaries || []), [])
194191
.filter((summary) => summary.StackName === stackName)
195192
.map((summary) => ({
196193
id: summary.StackId,
197-
time: summary.DeletionTime!.getTime(), // eslint-disable-line @typescript-eslint/no-non-null-assertion
194+
time: summary.DeletionTime?.getTime() ?? 0,
198195
}))
199196
.sort((a, b) => b.time - a.time)[0].id;
200197

201-
const events = await cloudFormation
202-
.describeStackEvents({ StackName: mostRecentStackId })
203-
.promise();
204-
198+
const events = await cloudFormation.describeStackEvents({ StackName: mostRecentStackId });
205199
const reasons = events.StackEvents?.filter(
206200
(event) => event.ResourceStatus === "CREATE_FAILED"
207201
).map((event) => event.ResourceStatusReason);

tst/test-helpers.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import CloudFormation from "aws-sdk/clients/cloudformation";
1+
import {
2+
CloudFormation,
3+
waitUntilStackDeleteComplete,
4+
Output,
5+
} from "@aws-sdk/client-cloudformation";
26

37
interface StringMap {
48
[key: string]: string;
@@ -9,25 +13,25 @@ async function describeStack(
913
cloudFormation: CloudFormation,
1014
stackName: string
1115
): Promise<StringMap> {
12-
const descriptions = await cloudFormation.describeStacks({ StackName: stackName }).promise();
13-
return descriptions.Stacks![0]!.Outputs!.reduce(
14-
(accumulator: StringMap, output: CloudFormation.Output) => {
15-
return {
16-
[output.OutputKey!]: output.OutputValue,
17-
...accumulator,
18-
} as StringMap;
19-
},
20-
{}
21-
);
16+
const descriptions = await cloudFormation.describeStacks({ StackName: stackName });
17+
return descriptions.Stacks![0]!.Outputs!.reduce((accumulator: StringMap, output: Output) => {
18+
return {
19+
[output.OutputKey!]: output.OutputValue,
20+
...accumulator,
21+
} as StringMap;
22+
}, {});
2223
}
2324
/* eslint-enable */
2425

2526
async function deleteStackIfExists(
2627
cloudFormation: CloudFormation,
2728
stackName: string
2829
): Promise<void> {
29-
await cloudFormation.deleteStack({ StackName: stackName }).promise();
30-
await cloudFormation.waitFor("stackDeleteComplete", { StackName: stackName }).promise();
30+
await cloudFormation.deleteStack({ StackName: stackName });
31+
await waitUntilStackDeleteComplete(
32+
{ client: cloudFormation, maxWaitTime: Number.MAX_VALUE },
33+
{ StackName: stackName }
34+
);
3135
}
3236

3337
export { describeStack, deleteStackIfExists, StringMap };

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
output: {
66
filename: "s3-upload-custom-resource.js",
77
path: path.resolve(__dirname, "dist"),
8+
asyncChunks: false,
89
},
910
mode: "production",
1011
target: "node",

0 commit comments

Comments
 (0)