mirror of
https://github.com/kou029w/websri.git
synced 2025-01-18 16:08:16 +00:00
add IIntegrityMetadata
interface
This commit is contained in:
parent
5c5d171791
commit
e5f17c3631
1 changed files with 17 additions and 14 deletions
31
src/index.ts
31
src/index.ts
|
@ -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 = " ") {
|
||||||
|
|
Loading…
Add table
Reference in a new issue