From a59dc0cb79af98ee601f352a7cf1b5fa9cc4d365 Mon Sep 17 00:00:00 2001 From: Tietew Date: Sat, 30 Dec 2023 23:22:31 +0900 Subject: [PATCH] feat(codepipeline-actions): more convenient methods to `CacheControl` (#28491) This PR adds following convenient methods to `CacheControl`; same as #25477. | method | directive | RFC | |-|-|-| | `CacheControl.noStore()` | `no-store` | [RFC9111](https://www.rfc-editor.org/rfc/rfc9111.html), Section 5.2.2.4 | | `CacheControl.mustUnderstand()` | `must-understand` | RFC9111, Section 5.2.2.3 | | `CacheControl.immutable()` | `immutable` | [RFC8246](https://www.rfc-editor.org/rfc/rfc8246.html) | | `CacheControl.staleWhileRevalidate(duration)` | `stale-while-revalidate=` | [RFC5861](https://www.rfc-editor.org/rfc/rfc5861.html) | | `CacheControl.staleIfError(duration)` | `stale-if-error=` | RFC5861 | For more information about these Cache-Control directives, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../lib/s3/deploy-action.ts | 10 ++++++++++ .../test/s3/s3-deploy-action.test.ts | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/aws-cdk-lib/aws-codepipeline-actions/lib/s3/deploy-action.ts b/packages/aws-cdk-lib/aws-codepipeline-actions/lib/s3/deploy-action.ts index 91b265d4e3b01..f4ed6e5be53a0 100644 --- a/packages/aws-cdk-lib/aws-codepipeline-actions/lib/s3/deploy-action.ts +++ b/packages/aws-cdk-lib/aws-codepipeline-actions/lib/s3/deploy-action.ts @@ -25,16 +25,26 @@ export class CacheControl { public static noCache() { return new CacheControl('no-cache'); } /** The 'no-transform' cache control directive. */ public static noTransform() { return new CacheControl('no-transform'); } + /** The 'no-store' cache control directive. */ + public static noStore() { return new CacheControl('no-store'); } + /** The 'must-understand' cache control directive. */ + public static mustUnderstand() { return new CacheControl('must-understand'); } /** The 'public' cache control directive. */ public static setPublic() { return new CacheControl('public'); } /** The 'private' cache control directive. */ public static setPrivate() { return new CacheControl('private'); } + /** The 'immutable' cache control directive. */ + public static immutable() { return new CacheControl('immutable'); } /** The 'proxy-revalidate' cache control directive. */ public static proxyRevalidate() { return new CacheControl('proxy-revalidate'); } /** The 'max-age' cache control directive. */ public static maxAge(t: Duration) { return new CacheControl(`max-age=${t.toSeconds()}`); } /** The 's-max-age' cache control directive. */ public static sMaxAge(t: Duration) { return new CacheControl(`s-maxage=${t.toSeconds()}`); } + /** The 'stale-while-revalidate' cache control directive. */ + public static staleWhileRevalidate(t: Duration) { return new CacheControl(`stale-while-revalidate=${t.toSeconds()}`); } + /** The 'stale-if-error' cache control directive. */ + public static staleIfError(t: Duration) { return new CacheControl(`stale-if-error=${t.toSeconds()}`); } /** * Allows you to create an arbitrary cache control directive, * in case our support is missing a method for a particular directive. diff --git a/packages/aws-cdk-lib/aws-codepipeline-actions/test/s3/s3-deploy-action.test.ts b/packages/aws-cdk-lib/aws-codepipeline-actions/test/s3/s3-deploy-action.test.ts index a2d362a9602e0..243fa7e0128af 100644 --- a/packages/aws-cdk-lib/aws-codepipeline-actions/test/s3/s3-deploy-action.test.ts +++ b/packages/aws-cdk-lib/aws-codepipeline-actions/test/s3/s3-deploy-action.test.ts @@ -125,6 +125,23 @@ describe('S3 Deploy Action', () => { }); }); + test('cache-control directive has correct values', () => { + expect(cpactions.CacheControl.mustRevalidate().value).toEqual('must-revalidate'); + expect(cpactions.CacheControl.noCache().value).toEqual('no-cache'); + expect(cpactions.CacheControl.noTransform().value).toEqual('no-transform'); + expect(cpactions.CacheControl.noStore().value).toEqual('no-store'); + expect(cpactions.CacheControl.mustUnderstand().value).toEqual('must-understand'); + expect(cpactions.CacheControl.setPublic().value).toEqual('public'); + expect(cpactions.CacheControl.setPrivate().value).toEqual('private'); + expect(cpactions.CacheControl.immutable().value).toEqual('immutable'); + expect(cpactions.CacheControl.proxyRevalidate().value).toEqual('proxy-revalidate'); + expect(cpactions.CacheControl.maxAge(Duration.minutes(1)).value).toEqual('max-age=60'); + expect(cpactions.CacheControl.sMaxAge(Duration.minutes(1)).value).toEqual('s-maxage=60'); + expect(cpactions.CacheControl.staleWhileRevalidate(Duration.minutes(1)).value).toEqual('stale-while-revalidate=60'); + expect(cpactions.CacheControl.staleIfError(Duration.minutes(1)).value).toEqual('stale-if-error=60'); + expect(cpactions.CacheControl.fromString('custom').value).toEqual('custom'); + }); + test('allow customizing objectKey (deployment path on S3)', () => { const stack = new Stack(); minimalPipeline(stack, {