add IIntegrityMetadata interface

This commit is contained in:
Nebel 2024-09-09 18:35:38 +09:00
parent 5c5d171791
commit e5f17c3631
Signed by: nebel
GPG key ID: 79807D08C6EF6460

View file

@ -26,7 +26,14 @@ export const IntegrityMetadataRegex =
export const SeparatorRegex = /[^\x21-\x7e]+/; export const SeparatorRegex = /[^\x21-\x7e]+/;
/** Integrity Metadata */ /** Integrity Metadata */
export class IntegrityMetadata { export interface IIntegrityMetadata {
alg: PrioritizedHashAlgorithm;
val: string;
opt?: string[];
}
/** Integrity Metadata */
export class IntegrityMetadata implements IIntegrityMetadata {
alg: PrioritizedHashAlgorithm; alg: PrioritizedHashAlgorithm;
val: string; val: string;
opt: string[]; opt: string[];
@ -45,8 +52,8 @@ export class IntegrityMetadata {
}); });
} }
match(integrity: { alg: PrioritizedHashAlgorithm; val: string }): boolean { match({ alg, val }: IIntegrityMetadata): boolean {
return integrity.alg === this.alg && integrity.val === this.val; return alg === this.alg && val === this.val;
} }
toString(): string { toString(): string {
@ -57,15 +64,11 @@ export class IntegrityMetadata {
return this.toString(); return this.toString();
} }
static stringify(integrity: { static stringify({ alg, val, opt = [] }: IIntegrityMetadata): string {
alg: PrioritizedHashAlgorithm; if (!alg) return "";
val: string; if (!val) return "";
opt: string[]; if (!(alg in supportedHashAlgorithms)) return "";
}): string { return `${alg}-${[val, ...opt].join("?")}`;
if (!integrity.alg) return "";
if (!integrity.val) return "";
if (!(integrity.alg in supportedHashAlgorithms)) return "";
return `${integrity.alg}-${[integrity.val, ...integrity.opt].join("?")}`;
} }
} }
@ -133,8 +136,8 @@ export class IntegrityMetadataSet extends Map<
return this.get(hashAlgorithm) ?? new IntegrityMetadata(""); return this.get(hashAlgorithm) ?? new IntegrityMetadata("");
} }
match(integrity: { alg: PrioritizedHashAlgorithm; val: string }): boolean { match(integrityMetadata: IIntegrityMetadata): boolean {
return this.strongest.match(integrity); return this.strongest.match(integrityMetadata);
} }
join(separator = " ") { join(separator = " ") {