mirror of
https://github.com/replicatedhq/ttl.sh.git
synced 2026-05-23 08:42:54 +00:00
handle invalid duration tags and change default to 24 hours (#89)
* handle invalid duration tags and change default to 24 hours
This commit is contained in:
@@ -2,6 +2,8 @@ import { TagManifestParams } from "./";
|
||||
import * as parseDuration from "parse-duration";
|
||||
import * as moment from "moment";
|
||||
|
||||
const durationRegex = /^(?=\d+[ywdhms])(( ?\d+y)?(?!\d))?(( ?\d+w)?(?!\d))?(( ?\d+d)?(?!\d))?(( ?\d+h)?(?!\d))?(( ?\d+m)?(?!\d))?(( ?\d+s)?(?!\d))?( ?\d+ms)?$/;
|
||||
|
||||
export function tryParsePutTagRequest(method: string, url: URL): TagManifestParams | void {
|
||||
const split = url.pathname.split("/");
|
||||
|
||||
@@ -66,7 +68,11 @@ export async function handleTagManifestRequest(r: Request, params: TagManifestPa
|
||||
}
|
||||
|
||||
function expirationFromTag(tag: string): string {
|
||||
const parsed = parseDuration(tag);
|
||||
let parsed = parseDuration(tag);
|
||||
if (!durationRegex.test(tag)) {
|
||||
// invalid duration, default to 24h
|
||||
parsed = 24 * 60 * 60 * 1000;
|
||||
}
|
||||
const now = new Date();
|
||||
const then = moment(now.getTime() + parsed);
|
||||
return then.fromNow();
|
||||
|
||||
Reference in New Issue
Block a user