add options to createIntegrityMetadataSet

This commit is contained in:
Nebel 2024-09-09 18:52:43 +09:00
parent e5f17c3631
commit bd29bf4dc1
Signed by: nebel
GPG key ID: 79807D08C6EF6460

View file

@ -10,6 +10,11 @@ export const supportedHashAlgorithms = {
export type PrioritizedHashAlgorithm = "" | HashAlgorithm; export type PrioritizedHashAlgorithm = "" | HashAlgorithm;
export type GetPrioritizedHashAlgorithm = (
a: HashAlgorithm,
b: HashAlgorithm,
) => PrioritizedHashAlgorithm;
export function getPrioritizedHashAlgorithm( export function getPrioritizedHashAlgorithm(
a: HashAlgorithm, a: HashAlgorithm,
b: HashAlgorithm, b: HashAlgorithm,
@ -92,17 +97,24 @@ export async function createIntegrityMetadata(
}); });
} }
/** Integrity Metadata Set Options */
export type IntegrityMetadataSetOptions = {
getPrioritizedHashAlgorithm: GetPrioritizedHashAlgorithm;
};
/** Integrity Metadata Set */ /** Integrity Metadata Set */
export class IntegrityMetadataSet extends Map< export class IntegrityMetadataSet extends Map<
HashAlgorithm, HashAlgorithm,
IntegrityMetadata IntegrityMetadata
> { > {
getPrioritizedHashAlgorithm: ( getPrioritizedHashAlgorithm: GetPrioritizedHashAlgorithm;
a: HashAlgorithm,
b: HashAlgorithm,
) => PrioritizedHashAlgorithm;
constructor(integrity: string, options = { getPrioritizedHashAlgorithm }) { constructor(
integrity: string,
options: IntegrityMetadataSetOptions = {
getPrioritizedHashAlgorithm,
},
) {
super(); super();
const integrityMetadata = integrity.split(SeparatorRegex); const integrityMetadata = integrity.split(SeparatorRegex);
@ -156,10 +168,13 @@ export class IntegrityMetadataSet extends Map<
export async function createIntegrityMetadataSet( export async function createIntegrityMetadataSet(
hashAlgorithms: HashAlgorithm[], hashAlgorithms: HashAlgorithm[],
data: ArrayBuffer, data: ArrayBuffer,
options: IntegrityMetadataSetOptions = {
getPrioritizedHashAlgorithm,
},
): Promise<IntegrityMetadataSet> { ): Promise<IntegrityMetadataSet> {
const integrityMetadata = await Promise.all( const integrityMetadata = await Promise.all(
hashAlgorithms.map((alg) => createIntegrityMetadata(alg, data)), hashAlgorithms.map((alg) => createIntegrityMetadata(alg, data)),
); );
return new IntegrityMetadataSet(integrityMetadata.join(" ")); return new IntegrityMetadataSet(integrityMetadata.join(" "), options);
} }