-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): move expiration from s3-deployments into core (#10192)
Move `Expires` class from s3-deployments to core. Rename to `Expiration` **BREAKING CHANGE**: s3-deployments property `expires` takes `cdk.Expiration` instead of `Expires` - **s3-deployments**: `BucketDeploymentProps.expires` now takes in type `cdk.Expiration` **Note**: PR separated from #9122 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
5f36f6b
commit 0e6e3c3
Showing
5 changed files
with
124 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Duration } from './duration'; | ||
/** | ||
* Represents a date of expiration. | ||
* | ||
* The amount can be specified either as a Date object, timestamp, Duration or string. | ||
*/ | ||
export class Expiration { | ||
/** | ||
* Expire at the specified date | ||
* @param d date to expire at | ||
*/ | ||
public static atDate(d: Date) { return new Expiration(d); } | ||
|
||
/** | ||
* Expire at the specified timestamp | ||
* @param t timestamp in unix milliseconds | ||
*/ | ||
public static atTimestamp(t: number) { return Expiration.atDate(new Date(t)); } | ||
|
||
/** | ||
* Expire once the specified duration has passed since deployment time | ||
* @param t the duration to wait before expiring | ||
*/ | ||
public static after(t: Duration) { return Expiration.atDate(new Date(Date.now() + t.toMilliseconds())); } | ||
|
||
/** | ||
* Expire at specified date, represented as a string | ||
* | ||
* @param s the string that represents date to expire at | ||
*/ | ||
public static fromString(s: string) { return new Expiration(new Date(s)); } | ||
|
||
/** | ||
* Expiration value as a Date object | ||
*/ | ||
public readonly date: Date; | ||
|
||
private constructor(date: Date) { | ||
this.date = date; | ||
} | ||
|
||
/** | ||
* Exipration Value in a formatted Unix Epoch Time in seconds | ||
*/ | ||
public toEpoch(): number { | ||
return Math.round(this.date.getTime() / 1000); | ||
} | ||
/** | ||
* Check if Exipiration expires before input | ||
* @param t the duration to check against | ||
*/ | ||
public isBefore(t: Duration): boolean { | ||
return this.date < new Date(Date.now() + t.toMilliseconds()); | ||
} | ||
|
||
/** | ||
* Check if Exipiration expires after input | ||
* @param t the duration to check against | ||
*/ | ||
public isAfter( t: Duration ): boolean { | ||
return this.date > new Date(Date.now() + t.toMilliseconds()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as nodeunit from 'nodeunit'; | ||
import { Duration, Expiration } from '../lib'; | ||
|
||
export = nodeunit.testCase({ | ||
'from string'(test: nodeunit.Test) { | ||
const date = new Date('Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.equal(Expiration.fromString('Sun, 26 Jan 2020 00:53:20 GMT').date.getDate(), date.getDate()); | ||
test.done(); | ||
}, | ||
|
||
'at specified date'(test: nodeunit.Test) { | ||
const date = new Date('Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.equal(Expiration.atDate(new Date('Sun, 26 Jan 2020 00:53:20 GMT')).date.toUTCString(), 'Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.equal(Expiration.atDate(new Date(1580000000000)).date.toUTCString(), 'Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.equal(Expiration.atDate(new Date(date)).date.toUTCString(), 'Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.done(); | ||
}, | ||
|
||
'at time stamp'(test: nodeunit.Test) { | ||
test.equal(Expiration.atDate(new Date(1580000000000)).date.toUTCString(), 'Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.done(); | ||
}, | ||
|
||
'after'(test: nodeunit.Test) { | ||
test.ok(Math.abs(new Date(Expiration.after(Duration.minutes(10)).date.toUTCString()).getTime() - (Date.now() + 600000)) < 15000); | ||
test.done(); | ||
}, | ||
|
||
'toEpoch returns correct value'(test: nodeunit.Test) { | ||
const date = new Date('Sun, 26 Jan 2020 00:53:20 GMT'); | ||
test.equal(Expiration.atDate(date).toEpoch(), 1580000000); | ||
test.done(); | ||
}, | ||
|
||
'isBefore'(test: nodeunit.Test) { | ||
const expire = Expiration.after(Duration.days(2)); | ||
test.ok(!expire.isBefore(Duration.days(1))); | ||
test.ok(expire.isBefore(Duration.days(3))); | ||
test.done(); | ||
}, | ||
|
||
'isAfter'(test: nodeunit.Test) { | ||
const expire = Expiration.after(Duration.days(2)); | ||
test.ok(expire.isAfter(Duration.days(1))); | ||
test.ok(!expire.isAfter(Duration.days(3))); | ||
test.done(); | ||
}, | ||
|
||
}); |