You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.4 KiB
56 lines
2.4 KiB
/// <reference types="vite/client" />
|
|
// The official fixtures shipped with the site. Vite bundles them straight
|
|
// from testdata/fixtures at build time, so testdata/ stays the single source
|
|
// of truth: every .dkc becomes a hashed same-origin file (vite.config.ts never
|
|
// inlines assets), and only the `description` field of its JSON record is
|
|
// imported. Neither the .dkk access keys, nor the plaintexts, nor the rest of
|
|
// the JSON records (payload_identity, control_cbor, …) reach the bundle.
|
|
|
|
const urls = import.meta.glob<string>('/testdata/fixtures/*.dkc', { query: '?url', import: 'default', eager: true });
|
|
// The records of the capsules only: neither the .dkk records nor the frozen
|
|
// outputs of `datekeys inspect -json` (*.inspect.json), which have no
|
|
// description.
|
|
const descriptions = import.meta.glob<unknown>(
|
|
['/testdata/fixtures/*.json', '!/testdata/fixtures/*.dkk.json', '!/testdata/fixtures/*.inspect.json'],
|
|
{ import: 'description', eager: true },
|
|
);
|
|
|
|
/** One official fixture capsule. */
|
|
export interface Fixture {
|
|
/** The file name, for example "time_only.dkc". */
|
|
readonly name: string;
|
|
/** The same-origin URL of its bytes. */
|
|
readonly url: string;
|
|
/** The description of its JSON record (in English, from the Go reference). */
|
|
readonly description: string | undefined;
|
|
}
|
|
|
|
/**
|
|
* Pairs each .dkc with the description of the JSON record of the same name,
|
|
* sorted by file name. Both arguments come from import.meta.glob, keyed by
|
|
* build-time paths, never by input.
|
|
*/
|
|
export function fixtureList(dkcUrls: Readonly<Record<string, string>>, jsonDescriptions: Readonly<Record<string, unknown>>): Fixture[] {
|
|
return Object.keys(dkcUrls)
|
|
.sort()
|
|
.map((path) => {
|
|
const json = path.replace(/\.dkc$/, '.json');
|
|
const d = Object.hasOwn(jsonDescriptions, json) ? jsonDescriptions[json] : undefined;
|
|
return {
|
|
name: path.slice(path.lastIndexOf('/') + 1),
|
|
url: dkcUrls[path]!,
|
|
description: typeof d === 'string' && d !== '' ? d : undefined,
|
|
};
|
|
});
|
|
}
|
|
|
|
/** The official fixtures, sorted by file name. */
|
|
export const FIXTURES: readonly Fixture[] = fixtureList(urls, descriptions);
|
|
|
|
/** Fetches the bytes of a fixture from the site's own origin. */
|
|
export async function fetchFixture(f: Fixture): Promise<Uint8Array> {
|
|
const res = await fetch(f.url);
|
|
if (!res.ok) throw new Error(`${f.name}: HTTP ${res.status}`);
|
|
return new Uint8Array(await res.arrayBuffer());
|
|
}
|