mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* perf(cli): commands start sooner by compiling registry schemas on first use * fix(cli): registry validation keeps its file comment-free
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import registrySchema from "@hyperframes/core/schemas/registry.json";
|
|
import itemSchema from "@hyperframes/core/schemas/registry-item.json";
|
|
import type { RegistryManifest, RegistryItem, ItemType } from "@hyperframes/core";
|
|
|
|
function compileShapes() {
|
|
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
|
return {
|
|
manifest: ajv.compile<RegistryManifest>(registrySchema),
|
|
item: ajv.compile<RegistryItem>(itemSchema),
|
|
};
|
|
}
|
|
let shapesCompiledOnFirstUse: ReturnType<typeof compileShapes> | undefined;
|
|
|
|
export function validRegistryName(name: string): boolean {
|
|
return name.length <= 128 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(name);
|
|
}
|
|
|
|
export function validRegistryManifest(value: unknown): value is RegistryManifest {
|
|
return (
|
|
(shapesCompiledOnFirstUse ??= compileShapes()).manifest(value) &&
|
|
value.items.length <= 10_000 &&
|
|
value.items.every((item) => validRegistryName(item.name))
|
|
);
|
|
}
|
|
|
|
export function validRegistryItem(
|
|
value: unknown,
|
|
name: string,
|
|
type: ItemType,
|
|
): value is RegistryItem {
|
|
return (
|
|
(shapesCompiledOnFirstUse ??= compileShapes()).item(value) &&
|
|
value.name === name &&
|
|
value.type === type &&
|
|
validRegistryName(value.name) &&
|
|
value.files.length <= 1_000
|
|
);
|
|
}
|