Skip to content

Commit de0c77d

Browse files
committed
Upgraded deps + added CacheControl flag
- Allows setting cacheControl flag in .s3uploadconfig.json
1 parent 873c374 commit de0c77d

13 files changed

+3500
-3746
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Resources:
3434
Handler:
3535
s3-upload-custom-resource.handler # This is fixed and references a file provided by
3636
# this project and available in the Lambda layer.
37-
Runtime: nodejs14.x
37+
Runtime: nodejs18.x
3838
Policies:
3939
- S3CrudPolicy:
4040
BucketName: !Ref WebsiteBucket
@@ -69,7 +69,7 @@ Resources:
6969
Handler:
7070
s3-upload-custom-resource.handler # This is fixed and references a file provided by
7171
# this project and available in the Lambda layer.
72-
Runtime: nodejs12.x
72+
Runtime: nodejs18.x
7373
Policies:
7474
- S3CrudPolicy:
7575
BucketName: !Ref WebsiteBucket
@@ -138,7 +138,8 @@ Here is an example `.s3uploadconfig.json` that will make all HTML files vended b
138138
".*": {
139139
"metadata": {
140140
"hiddenFile": "true"
141-
}
141+
},
142+
"cacheControl": "max-age=600"
142143
}
143144
}
144145
```

cloudformation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Resources:
99
Type: AWS::Serverless::LayerVersion
1010
Properties:
1111
LayerName: !Ref LayerName
12-
CompatibleRuntimes: [nodejs14.x]
12+
CompatibleRuntimes: [nodejs18.x]
1313
ContentUri: dist/layer.zip
1414
LicenseInfo: Apache-2.0
1515
S3UploadCustomResourceLayerPermission:

package-lock.json

+3,443-3,719
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@
2525
"release": "npm run build && npm run package && npm run deploy"
2626
},
2727
"devDependencies": {
28-
"@types/jest": "^27.0.1",
29-
"@types/minimatch": "^3.0.4",
28+
"@types/jest": "^29.5.0",
29+
"@types/minimatch": "^5.1.2",
3030
"@types/node": "^16.9.4",
31-
"@typescript-eslint/eslint-plugin": "^5.5.0",
32-
"@typescript-eslint/parser": "^5.5.0",
33-
"aws-sdk": "^2.1354.0",
34-
"deepmerge": "^4.2.2",
35-
"eslint": "^8.3.0",
36-
"jest": "^27.2.5",
37-
"minimatch": "^3.0.5",
38-
"prettier": "^2.4.1",
39-
"ts-jest": "^27.0.5",
40-
"ts-loader": "^9.2.5",
41-
"typescript": "^4.4.2",
42-
"webpack": "^5.76.0",
43-
"webpack-cli": "^4.8.0"
31+
"@typescript-eslint/eslint-plugin": "^5.59.6",
32+
"@typescript-eslint/parser": "^5.59.6",
33+
"aws-sdk": "^2.1381.0",
34+
"deepmerge": "^4.3.1",
35+
"eslint": "^8.41.0",
36+
"jest": "^29.5.0",
37+
"minimatch": "^9.0.1",
38+
"prettier": "^2.8.8",
39+
"ts-jest": "^29.1.0",
40+
"ts-loader": "^9.4.2",
41+
"typescript": "^5.0.4",
42+
"webpack": "^5.83.1",
43+
"webpack-cli": "^5.1.1"
4444
}
4545
}

src/__tests__/s3-upload-config-test.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@ describe("S3UploadConfig", () => {
3232
newS3UploadConfig({ file1: {}, file2: {}, file3: {} });
3333
newS3UploadConfig({ file: { metadata: {} } });
3434
newS3UploadConfig({ file: { contentType: {} } });
35-
newS3UploadConfig({ file: { metadata: {}, contentType: "", contentDisposition: "" } });
3635
newS3UploadConfig({
37-
file: { metadata: { key: "value" }, contentType: "", contentDisposition: "" },
36+
file: { metadata: {}, contentType: "", contentDisposition: "", cacheControl: "" },
3837
});
3938
newS3UploadConfig({
40-
file: { metadata: null, contentType: null, contentDisposition: null },
39+
file: {
40+
metadata: { key: "value" },
41+
contentType: "",
42+
contentDisposition: "",
43+
cacheControl: "",
44+
},
45+
});
46+
newS3UploadConfig({
47+
file: {
48+
metadata: null,
49+
contentType: null,
50+
contentDisposition: null,
51+
cacheControl: null,
52+
},
4153
});
4254
newS3UploadConfig({ file: { unknownKey: "unknownValue" } });
4355
});
@@ -54,6 +66,7 @@ describe("S3UploadConfig", () => {
5466
"src/**/*.png": {
5567
contentType: "image/png",
5668
contentDisposition: 'attachment; filename="download.png"',
69+
cacheControl: "public",
5770
},
5871
});
5972
});
@@ -79,6 +92,7 @@ describe("S3UploadConfig", () => {
7992
},
8093
contentType: "text/plain",
8194
contentDisposition: "inline; filename=other-name.txt",
95+
cacheControl: "max-age=600",
8296
randomKey: "value",
8397
},
8498
}).getS3ParamsForKey("file.txt")
@@ -88,6 +102,7 @@ describe("S3UploadConfig", () => {
88102
},
89103
ContentType: "text/plain",
90104
ContentDisposition: "inline; filename=other-name.txt",
105+
CacheControl: "max-age=600",
91106
});
92107
});
93108

src/s3-upload-config.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IMinimatch, Minimatch } from "minimatch";
1+
import { Minimatch } from "minimatch";
22
import deepmerge from "deepmerge";
33
import S3 from "aws-sdk/clients/s3";
44

@@ -13,11 +13,12 @@ interface S3ObjectConfig {
1313
[key: string]: string;
1414
};
1515
contentType?: string;
16+
cacheControl?: string;
1617
contentDisposition?: string;
1718
}
1819

1920
/** Internal representation of the parsed configuration */
20-
type ParsedS3UploadConfig = [IMinimatch, S3ObjectConfig][];
21+
type ParsedS3UploadConfig = [Minimatch, S3ObjectConfig][];
2122

2223
/** Parses an S3 upload config file and provides S3 configuration for individual files */
2324
class S3UploadConfig {
@@ -59,6 +60,9 @@ class S3UploadConfig {
5960
if (mergedConfig.contentType) {
6061
putObjectRequest.ContentType = mergedConfig.contentType;
6162
}
63+
if (mergedConfig.cacheControl) {
64+
putObjectRequest.CacheControl = mergedConfig.cacheControl;
65+
}
6266
if (mergedConfig.contentDisposition) {
6367
putObjectRequest.ContentDisposition = mergedConfig.contentDisposition;
6468
}

tst/example-project/bucket-cloudformation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Resources:
2222
Layers: [!Ref S3UploadLambdaLayerArn]
2323
CodeUri: "!!!source-root!!!"
2424
Handler: s3-upload-custom-resource.handler
25-
Runtime: nodejs12.x
25+
Runtime: nodejs18.x
2626
Timeout: 30
2727
Policies:
2828
- S3CrudPolicy:

tst/example-project/cloudformation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Resources:
1818
Layers: [!Ref S3UploadLambdaLayerArn]
1919
CodeUri: "!!!source-root!!!"
2020
Handler: s3-upload-custom-resource.handler
21-
Runtime: nodejs12.x
21+
Runtime: nodejs18.x
2222
Timeout: 30
2323
Policies:
2424
- S3CrudPolicy:

tst/example-project/src-1/.s3uploadconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"made-up-key": "made-up-value",
1515
"to-be-overridden": "overridden"
1616
},
17-
"contentDisposition": "attachment"
17+
"contentDisposition": "attachment",
18+
"cacheControl": "max-age=600"
1819
}
1920
}

tst/integration.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async function compareBucketContents(
4242
Metadata?: StringMap;
4343
ContentType?: string;
4444
ContentDisposition?: string;
45+
CacheControl?: string;
4546
};
4647
const response = await s3.listObjectsV2({ Bucket: bucketName }).promise();
4748
const simpleResponse: ObjectDescription[] =
@@ -62,6 +63,10 @@ async function compareBucketContents(
6263
if (typeof description.ContentDisposition !== "undefined") {
6364
response.ContentDisposition = description.ContentDisposition;
6465
}
66+
67+
if (typeof description.CacheControl !== "undefined") {
68+
response.CacheControl = description.CacheControl;
69+
}
6570
})
6671
);
6772

tst/test-data/foo-bar-contents-1.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"to-be-overridden": "overridden"
2020
},
2121
"ContentType": "text/plain",
22+
"CacheControl": "max-age=600",
2223
"ContentDisposition": "attachment"
2324
},
2425
{
@@ -41,6 +42,7 @@
4142
"to-be-overridden": "overridden"
4243
},
4344
"ContentType": "text/plain",
45+
"CacheControl": "max-age=600",
4446
"ContentDisposition": "attachment"
4547
}
4648
]

tst/test-data/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface S3Object {
1010
Metadata: { [key: string]: string | undefined };
1111
ContentType: string;
1212
ContentDisposition?: string;
13+
CacheControl?: string;
1314
}
1415

1516
export { fooBarContents1, fooBarContents2, rootContents1, rootContents2, S3Object };

tst/test-data/root-contents-1.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"to-be-overridden": "overridden"
2020
},
2121
"ContentType": "text/plain",
22+
"CacheControl": "max-age=600",
2223
"ContentDisposition": "attachment"
2324
}
2425
]

0 commit comments

Comments
 (0)