Skip to content

Commit d898ad8

Browse files
perf(cli): commands start sooner by compiling registry schemas on first use (#5935)
* perf(cli): commands start sooner by compiling registry schemas on first use * fix(cli): registry validation keeps its file comment-free
1 parent 0693839 commit d898ad8

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, it, vi } from "vitest";
2+
3+
const compiles = vi.hoisted(() => ({ count: 0 }));
4+
5+
vi.mock("ajv/dist/2020.js", async (importOriginal) => {
6+
const real = await importOriginal<typeof import("ajv/dist/2020.js")>();
7+
const compile = real.Ajv2020.prototype.compile;
8+
real.Ajv2020.prototype.compile = function (this: unknown, ...args: unknown[]) {
9+
compiles.count += 1;
10+
return Reflect.apply(compile, this, args);
11+
} as typeof compile;
12+
return { ...real };
13+
});
14+
15+
it("compiles the registry schemas on first use, not when the module is imported", async () => {
16+
const { validRegistryItem } = await import("./validation.js");
17+
expect(compiles.count).toBe(0);
18+
19+
validRegistryItem({}, "demo", "hyperframes:component");
20+
expect(compiles.count).toBe(2);
21+
22+
validRegistryItem({}, "demo", "hyperframes:component");
23+
expect(compiles.count).toBe(2);
24+
});

‎packages/cli/src/registry/validation.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import registrySchema from "@hyperframes/core/schemas/registry.json";
33
import itemSchema from "@hyperframes/core/schemas/registry-item.json";
44
import type { RegistryManifest, RegistryItem, ItemType } from "@hyperframes/core";
55

6-
const ajv = new Ajv2020({ strict: false, validateFormats: false });
7-
const manifestShape = ajv.compile<RegistryManifest>(registrySchema);
8-
const itemShape = ajv.compile<RegistryItem>(itemSchema);
6+
function compileShapes() {
7+
const ajv = new Ajv2020({ strict: false, validateFormats: false });
8+
return {
9+
manifest: ajv.compile<RegistryManifest>(registrySchema),
10+
item: ajv.compile<RegistryItem>(itemSchema),
11+
};
12+
}
13+
let shapesCompiledOnFirstUse: ReturnType<typeof compileShapes> | undefined;
914

1015
export function validRegistryName(name: string): boolean {
1116
return name.length <= 128 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(name);
1217
}
1318

1419
export function validRegistryManifest(value: unknown): value is RegistryManifest {
1520
return (
16-
manifestShape(value) &&
21+
(shapesCompiledOnFirstUse ??= compileShapes()).manifest(value) &&
1722
value.items.length <= 10_000 &&
1823
value.items.every((item) => validRegistryName(item.name))
1924
);
@@ -25,7 +30,7 @@ export function validRegistryItem(
2530
type: ItemType,
2631
): value is RegistryItem {
2732
return (
28-
itemShape(value) &&
33+
(shapesCompiledOnFirstUse ??= compileShapes()).item(value) &&
2934
value.name === name &&
3035
value.type === type &&
3136
validRegistryName(value.name) &&

0 commit comments

Comments
 (0)