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:
Salah Al Saleh
2022-07-26 14:33:46 -07:00
committed by GitHub
parent 7b35b76304
commit a57748cbff
2 changed files with 25 additions and 13 deletions

View File

@@ -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();