Skip to content

Commit

Permalink
feat(codepipeline-actions): more convenient methods to CacheControl (
Browse files Browse the repository at this point in the history
…aws#28491)

This PR adds following convenient methods to `CacheControl`; same as aws#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=<duration>` | [RFC5861](https://www.rfc-editor.org/rfc/rfc5861.html) |
| `CacheControl.staleIfError(duration)` | `stale-if-error=<duration>` | 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*
  • Loading branch information
Tietew authored and paulhcsun committed Jan 5, 2024
1 parent 454ce16 commit 366a946
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 366a946

Please sign in to comment.