@risinghq/lienzo 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -2
- package/dist/shell/assets/index-Cna8wlzF.js +4 -0
- package/dist/shell/assets/index-CoJSASvW.css +2 -0
- package/dist/shell/index.html +13 -0
- package/package.json +76 -4
- package/src/bench/bench.ts +132 -0
- package/src/bridge/page.ts +443 -0
- package/src/bridge/protocol.ts +124 -0
- package/src/bridge/record.ts +75 -0
- package/src/bridge/script.ts +24 -0
- package/src/bridge/state.ts +143 -0
- package/src/capture/capture.ts +125 -0
- package/src/capture/compare.ts +46 -0
- package/src/capture/diff.ts +16 -0
- package/src/capture/manifest.ts +60 -0
- package/src/capture/refs.ts +20 -0
- package/src/capture/replay.ts +25 -0
- package/src/capture/review.ts +78 -0
- package/src/capture/serve.ts +32 -0
- package/src/capture/snap.ts +62 -0
- package/src/capture/store.ts +28 -0
- package/src/cli.ts +287 -0
- package/src/cloud/checkout.ts +33 -0
- package/src/cloud/credentials.ts +25 -0
- package/src/cloud/link.ts +60 -0
- package/src/cloud/login.ts +85 -0
- package/src/commands/canvas.ts +46 -0
- package/src/commands/change.ts +218 -0
- package/src/commands/class.ts +66 -0
- package/src/commands/command.ts +27 -0
- package/src/commands/create.ts +95 -0
- package/src/commands/element.ts +118 -0
- package/src/commands/file.ts +96 -0
- package/src/commands/frame.ts +58 -0
- package/src/commands/override.ts +38 -0
- package/src/commands/prop.ts +18 -0
- package/src/commands/registry.ts +76 -0
- package/src/commands/rule.ts +131 -0
- package/src/commands/stage.ts +52 -0
- package/src/commands/story.ts +112 -0
- package/src/commands/style.ts +123 -0
- package/src/commands/text.ts +17 -0
- package/src/commands/token.ts +60 -0
- package/src/commands/transaction.ts +66 -0
- package/src/job/docker.ts +109 -0
- package/src/job/job.ts +125 -0
- package/src/job/server.ts +62 -0
- package/src/mirror/apply.ts +121 -0
- package/src/mirror/entry.ts +7 -0
- package/src/mirror/host.ts +68 -0
- package/src/mirror/observe.ts +271 -0
- package/src/mirror/script.ts +18 -0
- package/src/mirror/transport.ts +7 -0
- package/src/mirror/wire.ts +25 -0
- package/src/project/config.ts +98 -0
- package/src/project/design.ts +59 -0
- package/src/project/detect.ts +184 -0
- package/src/project/files.ts +30 -0
- package/src/project/git.ts +68 -0
- package/src/project/github.ts +42 -0
- package/src/project/history.ts +89 -0
- package/src/project/project.ts +106 -0
- package/src/project/spawn.ts +23 -0
- package/src/project/step.ts +15 -0
- package/src/protocol.ts +125 -0
- package/src/sandbox/forward.ts +73 -0
- package/src/sandbox/install.ts +44 -0
- package/src/sandbox/sandbox.ts +57 -0
- package/src/serve/build.ts +49 -0
- package/src/serve/canvases.ts +65 -0
- package/src/serve/dev.ts +296 -0
- package/src/serve/env.ts +16 -0
- package/src/serve/mcp.ts +65 -0
- package/src/serve/middleware.ts +172 -0
- package/src/serve/next/config.ts +68 -0
- package/src/serve/next/hook.ts +74 -0
- package/src/serve/next/loader.ts +37 -0
- package/src/serve/nuxt.ts +14 -0
- package/src/serve/plugin.ts +95 -0
- package/src/serve/preload.ts +167 -0
- package/src/serve/resend.ts +29 -0
- package/src/serve/running.ts +115 -0
- package/src/serve/screenshot.ts +26 -0
- package/src/serve/sources.ts +46 -0
- package/src/serve/uncached.ts +25 -0
- package/src/serve/vite-build.ts +15 -0
- package/src/serve/vite.ts +19 -0
- package/src/session/session.ts +96 -0
- package/src/share/ask.ts +24 -0
- package/src/share/client.ts +140 -0
- package/src/share/consent.ts +39 -0
- package/src/share/frame.ts +26 -0
- package/src/source/astro/astro.ts +10 -0
- package/src/source/astro/elements.ts +60 -0
- package/src/source/blocks.ts +42 -0
- package/src/source/css.ts +48 -0
- package/src/source/estree.ts +55 -0
- package/src/source/extract.ts +68 -0
- package/src/source/insert.ts +189 -0
- package/src/source/jsx/elements.ts +136 -0
- package/src/source/jsx/jsx.ts +5 -0
- package/src/source/language.ts +176 -0
- package/src/source/location.ts +34 -0
- package/src/source/modules.ts +41 -0
- package/src/source/overrides.ts +35 -0
- package/src/source/page.ts +69 -0
- package/src/source/patch.ts +79 -0
- package/src/source/prop.ts +34 -0
- package/src/source/refusal.ts +7 -0
- package/src/source/rules.ts +298 -0
- package/src/source/stamp.ts +28 -0
- package/src/source/story-file.ts +40 -0
- package/src/source/story.ts +162 -0
- package/src/source/structure.ts +253 -0
- package/src/source/style.ts +143 -0
- package/src/source/svelte/elements.ts +154 -0
- package/src/source/svelte/svelte.ts +11 -0
- package/src/source/tag.ts +99 -0
- package/src/source/tailwind.ts +46 -0
- package/src/source/tokens.ts +56 -0
- package/src/source/vue/elements.ts +236 -0
- package/src/source/vue/vue.ts +11 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ATTRIBUTE } from "../protocol.ts";
|
|
2
|
+
import type { AnimationInfo, ForcedState, Ref } from "./protocol.ts";
|
|
3
|
+
|
|
4
|
+
const FORCE = "data-lz-force";
|
|
5
|
+
const FORCING = /:(hover|focus-visible|focus-within|focus|active)(?![\w-])/g;
|
|
6
|
+
const HOVER_ONLY_MEDIA = /^\(\s*(any-)?hover\s*:\s*hover\s*\)$/;
|
|
7
|
+
const INHERITED_BY_ANCESTORS = new Set<ForcedState>(["hover", "active"]);
|
|
8
|
+
|
|
9
|
+
const veil = document.createElement("style");
|
|
10
|
+
let forced: { node: Element; states: ForcedState[] } | null = null;
|
|
11
|
+
|
|
12
|
+
function forcedRule(rule: CSSStyleRule): string | null {
|
|
13
|
+
const selector = rule.selectorText.replace(FORCING, `[${FORCE}~="$1"]`);
|
|
14
|
+
|
|
15
|
+
return selector === rule.selectorText ? null : `${selector}{${rule.style.cssText}}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function forcedRules(rules: CSSRuleList): string {
|
|
19
|
+
return [...rules]
|
|
20
|
+
.map((rule) => {
|
|
21
|
+
if (rule instanceof CSSStyleRule) return forcedRule(rule) ?? "";
|
|
22
|
+
|
|
23
|
+
if (rule instanceof CSSMediaRule) {
|
|
24
|
+
const inner = forcedRules(rule.cssRules);
|
|
25
|
+
if (!inner) return "";
|
|
26
|
+
|
|
27
|
+
return HOVER_ONLY_MEDIA.test(rule.conditionText)
|
|
28
|
+
? inner
|
|
29
|
+
: `@media ${rule.conditionText}{${inner}}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (rule instanceof CSSSupportsRule) {
|
|
33
|
+
const inner = forcedRules(rule.cssRules);
|
|
34
|
+
|
|
35
|
+
return inner && `@supports ${rule.conditionText}{${inner}}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return rule instanceof CSSLayerBlockRule ? forcedRules(rule.cssRules) : "";
|
|
39
|
+
})
|
|
40
|
+
.join("");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function mirroredSheets(): string {
|
|
44
|
+
return [...document.styleSheets]
|
|
45
|
+
.map((sheet) => {
|
|
46
|
+
try {
|
|
47
|
+
return forcedRules(sheet.cssRules);
|
|
48
|
+
} catch {
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
.join("");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function mark(node: Element, states: ForcedState[]) {
|
|
56
|
+
const own = states.join(" ");
|
|
57
|
+
if (own) node.setAttribute(FORCE, own);
|
|
58
|
+
else node.removeAttribute(FORCE);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function clear() {
|
|
62
|
+
for (const node of document.querySelectorAll(`[${FORCE}]`)) node.removeAttribute(FORCE);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function within(states: ForcedState[]): ForcedState[] {
|
|
66
|
+
const inherited = states.filter((state) => INHERITED_BY_ANCESTORS.has(state));
|
|
67
|
+
const focused = states.some((state) => state.startsWith("focus"));
|
|
68
|
+
|
|
69
|
+
return focused ? [...inherited, "focus-within"] : inherited;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function force(node: Element | null, states: ForcedState[]) {
|
|
73
|
+
clear();
|
|
74
|
+
forced = node && states.length > 0 ? { node, states } : null;
|
|
75
|
+
|
|
76
|
+
if (forced) {
|
|
77
|
+
mark(forced.node, forced.states);
|
|
78
|
+
const above = within(forced.states);
|
|
79
|
+
for (let parent = forced.node.parentElement; parent; parent = parent.parentElement) {
|
|
80
|
+
mark(parent, above);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
refreshForced();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function refreshForced() {
|
|
88
|
+
const text = forced ? mirroredSheets() : "";
|
|
89
|
+
if (veil.textContent !== text) veil.textContent = text;
|
|
90
|
+
if (!veil.isConnected && text) document.head.append(veil);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function nameOf(animation: Animation): string {
|
|
94
|
+
if (animation instanceof CSSAnimation) return animation.animationName;
|
|
95
|
+
if (animation instanceof CSSTransition) return animation.transitionProperty;
|
|
96
|
+
|
|
97
|
+
return animation.id || "script";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function kindOf(animation: Animation): AnimationInfo["kind"] {
|
|
101
|
+
if (animation instanceof CSSAnimation) return "css-animation";
|
|
102
|
+
|
|
103
|
+
return animation instanceof CSSTransition ? "css-transition" : "script";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function targetOf(animation: Animation, refOf: (node: Element) => Ref | null): Ref | null {
|
|
107
|
+
const target = (animation.effect as KeyframeEffect | null)?.target;
|
|
108
|
+
|
|
109
|
+
return target && target.closest(`[${ATTRIBUTE}]`) ? refOf(target) : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function animationsOf(refOf: (node: Element) => Ref | null): AnimationInfo[] {
|
|
113
|
+
return document.getAnimations().map((animation, index) => {
|
|
114
|
+
const timing = animation.effect?.getComputedTiming();
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
index,
|
|
118
|
+
name: nameOf(animation),
|
|
119
|
+
kind: kindOf(animation),
|
|
120
|
+
target: targetOf(animation, refOf),
|
|
121
|
+
duration: typeof timing?.duration === "number" ? timing.duration : 0,
|
|
122
|
+
delay: timing?.delay ?? 0,
|
|
123
|
+
iterations: timing?.iterations ?? 1,
|
|
124
|
+
time: Number(animation.currentTime ?? 0),
|
|
125
|
+
paused: animation.playState === "paused",
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function scrub(index: number, time: number) {
|
|
131
|
+
const animation = document.getAnimations()[index];
|
|
132
|
+
if (!animation) throw new Error(`no animation at ${index}`);
|
|
133
|
+
|
|
134
|
+
animation.pause();
|
|
135
|
+
animation.currentTime = time;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function playback(paused: boolean) {
|
|
139
|
+
for (const animation of document.getAnimations()) {
|
|
140
|
+
if (paused) animation.pause();
|
|
141
|
+
else animation.play();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { chromium, type Browser, type Page } from "playwright-core";
|
|
3
|
+
import type { Step } from "../project/step.ts";
|
|
4
|
+
|
|
5
|
+
export type Frame = {
|
|
6
|
+
id: string;
|
|
7
|
+
path: string;
|
|
8
|
+
steps: Step[];
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
};
|
|
12
|
+
export type Shot = { png: Uint8Array; files: string[] };
|
|
13
|
+
|
|
14
|
+
// The instant every page sees as now.
|
|
15
|
+
const NOW = new Date("2026-01-01T00:00:00Z");
|
|
16
|
+
|
|
17
|
+
// A page's random numbers are the same on every capture.
|
|
18
|
+
const SEEDED = `(() => {
|
|
19
|
+
let seed = 0x2f6e2b1;
|
|
20
|
+
Math.random = () => {
|
|
21
|
+
seed = (seed + 0x6d2b79f5) | 0;
|
|
22
|
+
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
|
|
23
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
24
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
25
|
+
};
|
|
26
|
+
})();`;
|
|
27
|
+
|
|
28
|
+
// Waits for what changes a page after it loads: fonts, then two frames of
|
|
29
|
+
// layout.
|
|
30
|
+
const SETTLED = `(async () => {
|
|
31
|
+
await document.fonts.ready;
|
|
32
|
+
await new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(done)));
|
|
33
|
+
})()`;
|
|
34
|
+
|
|
35
|
+
// The source files the page's stamps name.
|
|
36
|
+
const STAMPED = `[...new Set([...document.querySelectorAll("[data-lz]")]
|
|
37
|
+
.map((element) => element.getAttribute("data-lz").replace(/:\\d+:\\d+$/, "")))].sort()`;
|
|
38
|
+
|
|
39
|
+
// Performs one step on the element its stamp names. A stamp that matches no
|
|
40
|
+
// element, or several, fails the frame: a flow that cannot find its target
|
|
41
|
+
// would otherwise capture the wrong state.
|
|
42
|
+
async function act(page: Page, frame: string, step: Step): Promise<void> {
|
|
43
|
+
const all = page.locator(`[data-lz="${step.at}"]`);
|
|
44
|
+
const found = await all.count();
|
|
45
|
+
if (step.nth === undefined && found !== 1) {
|
|
46
|
+
throw new Error(`frame "${frame}": ${step.at} matches ${found} elements`);
|
|
47
|
+
}
|
|
48
|
+
if (step.nth !== undefined && step.nth >= found) {
|
|
49
|
+
throw new Error(`frame "${frame}": ${step.at} has no element ${step.nth} of ${found}`);
|
|
50
|
+
}
|
|
51
|
+
const target = step.nth === undefined ? all : all.nth(step.nth);
|
|
52
|
+
|
|
53
|
+
if (step.do === "click") await target.click();
|
|
54
|
+
else if (step.do === "hover") await target.hover();
|
|
55
|
+
else if (step.do === "fill") await target.fill(step.value);
|
|
56
|
+
else if (step.do === "press") await target.press(step.key);
|
|
57
|
+
await page.evaluate(SETTLED);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The browser every capture runs in. It rasterizes in software: Chrome's GPU
|
|
61
|
+
// raster moves an antialiased edge by one level between two loads of the same
|
|
62
|
+
// page, which fails a determinism check for no change a person made.
|
|
63
|
+
export function launch(): Promise<Browser> {
|
|
64
|
+
return chromium.launch({
|
|
65
|
+
channel: "chrome",
|
|
66
|
+
args: ["--use-gl=angle", "--use-angle=swiftshader"],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function rendererOf(browser: Browser): Promise<string> {
|
|
71
|
+
return `chrome-${browser.version()}/${os.platform()}-${os.arch()}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Draws each frame of a served build in a fresh page of one browser. A frame
|
|
75
|
+
// differs between two captures only by what the page itself does: the clock,
|
|
76
|
+
// random numbers, animation, fonts and the network are fixed.
|
|
77
|
+
export async function capture(browser: Browser, site: string, frames: Frame[]): Promise<Shot[]> {
|
|
78
|
+
// A site may be given with a path or a trailing slash; a request's origin has neither.
|
|
79
|
+
const origin = new URL(site).origin;
|
|
80
|
+
const shots: Shot[] = [];
|
|
81
|
+
|
|
82
|
+
for (const frame of frames) {
|
|
83
|
+
const context = await browser.newContext({
|
|
84
|
+
viewport: { width: frame.width, height: frame.height },
|
|
85
|
+
deviceScaleFactor: 1,
|
|
86
|
+
locale: "en-US",
|
|
87
|
+
timezoneId: "UTC",
|
|
88
|
+
colorScheme: "light",
|
|
89
|
+
reducedMotion: "reduce",
|
|
90
|
+
serviceWorkers: "block",
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
await context.clock.setFixedTime(NOW);
|
|
95
|
+
await context.addInitScript(SEEDED);
|
|
96
|
+
await context.route("**/*", (route) =>
|
|
97
|
+
new URL(route.request().url()).origin === origin ? route.continue() : route.abort(),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const page = await context.newPage();
|
|
101
|
+
const response = await page.goto(new URL(frame.path, origin).href, {
|
|
102
|
+
waitUntil: "networkidle",
|
|
103
|
+
});
|
|
104
|
+
if (!response?.ok()) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`frame "${frame.id}": ${frame.path} answered ${response?.status() ?? "nothing"}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await page.evaluate(SETTLED);
|
|
111
|
+
for (const [index, step] of frame.steps.entries()) {
|
|
112
|
+
await act(page, frame.id, step).catch((error: Error) => {
|
|
113
|
+
throw new Error(`step ${index + 1} of ${error.message}`);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const png = await page.screenshot({ animations: "disabled", caret: "hide" });
|
|
117
|
+
const files = await page.evaluate<string[]>(STAMPED);
|
|
118
|
+
shots.push({ png, files });
|
|
119
|
+
} finally {
|
|
120
|
+
await context.close();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return shots;
|
|
125
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Manifest } from "./store.ts";
|
|
2
|
+
|
|
3
|
+
export type Comparison = {
|
|
4
|
+
// Frames in both captures whose PNGs differ.
|
|
5
|
+
changed: string[];
|
|
6
|
+
added: string[];
|
|
7
|
+
removed: string[];
|
|
8
|
+
unchanged: string[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// What changed between a baseline capture and a head capture. The PNGs of two
|
|
12
|
+
// renderers are not compared, so a pair that differs there is refused.
|
|
13
|
+
export function compare(base: Manifest, head: Manifest): Comparison | { refused: string } {
|
|
14
|
+
if (base.renderer !== head.renderer) {
|
|
15
|
+
return { refused: `the baseline was drawn by ${base.renderer}, the head by ${head.renderer}` };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const keys = (manifest: Manifest) => Object.keys(manifest.frames).sort();
|
|
19
|
+
const inBoth = keys(head).filter((key) => key in base.frames);
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
changed: inBoth.filter((key) => base.frames[key]!.png !== head.frames[key]!.png),
|
|
23
|
+
unchanged: inBoth.filter((key) => base.frames[key]!.png === head.frames[key]!.png),
|
|
24
|
+
added: keys(head).filter((key) => !(key in base.frames)),
|
|
25
|
+
removed: keys(base).filter((key) => !(key in head.frames)),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Check = { conclusion: "success" | "neutral"; title: string; summary: string };
|
|
30
|
+
|
|
31
|
+
// The status a host shows for a comparison. Changed frames are for a person to
|
|
32
|
+
// look at, so they never fail a check.
|
|
33
|
+
export function checkOf(comparison: Comparison, page?: string): Check {
|
|
34
|
+
const { changed, added, removed } = comparison;
|
|
35
|
+
const count = changed.length + added.length + removed.length;
|
|
36
|
+
const title =
|
|
37
|
+
count === 0 ? "No frames changed" : `${count} frame${count === 1 ? "" : "s"} changed`;
|
|
38
|
+
const lines = [
|
|
39
|
+
...changed.map((key) => `- changed ${key}`),
|
|
40
|
+
...added.map((key) => `- added ${key}`),
|
|
41
|
+
...removed.map((key) => `- removed ${key}`),
|
|
42
|
+
...(page && count > 0 ? ["", page] : []),
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
return { conclusion: count === 0 ? "success" : "neutral", title, summary: lines.join("\n") };
|
|
46
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import pixelmatch from "pixelmatch";
|
|
2
|
+
import { PNG } from "pngjs";
|
|
3
|
+
|
|
4
|
+
// A PNG that shows where two PNGs differ: red where a pixel does, the head
|
|
5
|
+
// faded elsewhere. Two sizes cannot be laid over each other, so a size change
|
|
6
|
+
// has no mask.
|
|
7
|
+
export function mask(before: Uint8Array, after: Uint8Array): Uint8Array | undefined {
|
|
8
|
+
const a = PNG.sync.read(Buffer.from(before));
|
|
9
|
+
const b = PNG.sync.read(Buffer.from(after));
|
|
10
|
+
if (a.width !== b.width || a.height !== b.height) return undefined;
|
|
11
|
+
|
|
12
|
+
const out = new PNG({ width: a.width, height: a.height });
|
|
13
|
+
pixelmatch(a.data, b.data, out.data, a.width, a.height, { threshold: 0, includeAA: true });
|
|
14
|
+
|
|
15
|
+
return PNG.sync.write(out);
|
|
16
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { mkdir } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Manifest, Store } from "./store.ts";
|
|
4
|
+
|
|
5
|
+
export { keyOf, type Manifest, type Store } from "./store.ts";
|
|
6
|
+
|
|
7
|
+
export const FOLDER = ".lienzo";
|
|
8
|
+
|
|
9
|
+
export function hashOf(bytes: Uint8Array): string {
|
|
10
|
+
return new Bun.CryptoHasher("sha256").update(bytes).digest("hex");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// The store in a project's `.lienzo` folder.
|
|
14
|
+
export function diskStore(root: string): Store {
|
|
15
|
+
return {
|
|
16
|
+
putBlob: (bytes) => putBlob(root, bytes),
|
|
17
|
+
async getBlob(hash) {
|
|
18
|
+
const file = Bun.file(blobPath(root, hash));
|
|
19
|
+
return (await file.exists()) ? await file.bytes() : undefined;
|
|
20
|
+
},
|
|
21
|
+
async putManifest(manifest) {
|
|
22
|
+
await writeManifest(root, manifest);
|
|
23
|
+
},
|
|
24
|
+
getManifest: (commit) => readManifest(root, commit),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function putBlob(root: string, bytes: Uint8Array): Promise<string> {
|
|
29
|
+
const hash = hashOf(bytes);
|
|
30
|
+
const file = path.join(root, FOLDER, "blobs", `${hash}.png`);
|
|
31
|
+
|
|
32
|
+
if (!(await Bun.file(file).exists())) {
|
|
33
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
34
|
+
await Bun.write(file, bytes);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return hash;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function blobPath(root: string, hash: string): string {
|
|
41
|
+
return path.join(root, FOLDER, "blobs", `${hash}.png`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function readManifest(root: string, commit: string): Promise<Manifest | undefined> {
|
|
45
|
+
const file = Bun.file(manifestPath(root, commit));
|
|
46
|
+
|
|
47
|
+
return (await file.exists()) ? ((await file.json()) as Manifest) : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function manifestPath(root: string, commit: string): string {
|
|
51
|
+
return path.join(root, FOLDER, "captures", `${commit}.json`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function writeManifest(root: string, manifest: Manifest): Promise<string> {
|
|
55
|
+
const file = manifestPath(root, manifest.commit);
|
|
56
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
57
|
+
await Bun.write(file, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
58
|
+
|
|
59
|
+
return file;
|
|
60
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export async function git(root: string, ...args: string[]): Promise<string> {
|
|
2
|
+
const run = Bun.spawn(["git", ...args], { cwd: root, stdout: "pipe", stderr: "ignore" });
|
|
3
|
+
const [text, code] = await Promise.all([new Response(run.stdout).text(), run.exited]);
|
|
4
|
+
|
|
5
|
+
return code === 0 ? text.trim() : "";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// The commit a capture is filed under: HEAD when the tracked files are clean,
|
|
9
|
+
// else "working", which never serves as a baseline.
|
|
10
|
+
export async function commitOf(root: string): Promise<string> {
|
|
11
|
+
const head = await git(root, "rev-parse", "HEAD");
|
|
12
|
+
const dirty = (await git(root, "status", "--porcelain", "--untracked-files=no")) !== "";
|
|
13
|
+
|
|
14
|
+
return head && !dirty ? head : "working";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// The commit a branch left its base at, or "" when git has none.
|
|
18
|
+
export async function mergeBase(root: string, base: string): Promise<string> {
|
|
19
|
+
return await git(root, "merge-base", base, "HEAD");
|
|
20
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { capture, launch, type Frame } from "./capture.ts";
|
|
2
|
+
import { hashOf } from "./manifest.ts";
|
|
3
|
+
|
|
4
|
+
// Plays a frame on a page that is served, twice, in fresh pages. A flow is
|
|
5
|
+
// worth saving when it finds every target and leaves the same pixels each
|
|
6
|
+
// time, which is what `snap` will later ask of it.
|
|
7
|
+
export async function replay(
|
|
8
|
+
origin: string,
|
|
9
|
+
frame: Frame,
|
|
10
|
+
): Promise<{ ok: true } | { ok: false; reason: string }> {
|
|
11
|
+
const browser = await launch();
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const first = await capture(browser, origin, [frame]);
|
|
15
|
+
const second = await capture(browser, origin, [frame]);
|
|
16
|
+
|
|
17
|
+
return hashOf(first[0]!.png) === hashOf(second[0]!.png)
|
|
18
|
+
? { ok: true }
|
|
19
|
+
: { ok: false, reason: `"${frame.id}" does not draw the same twice` };
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return { ok: false, reason: error instanceof Error ? error.message : String(error) };
|
|
22
|
+
} finally {
|
|
23
|
+
await browser.close();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { mkdir } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { checkOf, compare, type Check, type Comparison } from "./compare.ts";
|
|
4
|
+
import { mask } from "./diff.ts";
|
|
5
|
+
import { diskStore, FOLDER, type Manifest, type Store } from "./manifest.ts";
|
|
6
|
+
import { commitOf, mergeBase } from "./refs.ts";
|
|
7
|
+
|
|
8
|
+
export type Reviewed =
|
|
9
|
+
| { ok: true; page: string; comparison: Comparison; check: Check }
|
|
10
|
+
| { ok: false; reason: string };
|
|
11
|
+
|
|
12
|
+
function escape(text: string): string {
|
|
13
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/"/g, """);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function figure(label: string, hash: string | undefined): string {
|
|
17
|
+
const body = hash ? `<img src="../blobs/${hash}.png" alt="${label}">` : "<p>none</p>";
|
|
18
|
+
return `<figure><figcaption>${label}</figcaption>${body}</figure>`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function section(key: string, state: string, base?: string, head?: string, diff?: string): string {
|
|
22
|
+
const note = state === "changed" && !diff ? "<p>The size changed, so there is no mask.</p>" : "";
|
|
23
|
+
return `<section><h2>${escape(key)} <small>${state}</small></h2>${note}<div>${figure("before", base)}${figure("after", head)}${diff ? figure("difference", diff) : ""}</div></section>`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function maskBlob(store: Store, base: string, head: string): Promise<string | undefined> {
|
|
27
|
+
const [a, b] = await Promise.all([store.getBlob(base), store.getBlob(head)]);
|
|
28
|
+
if (!a || !b) return undefined;
|
|
29
|
+
|
|
30
|
+
const png = mask(a, b);
|
|
31
|
+
return png && (await store.putBlob(png));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function render(
|
|
35
|
+
store: Store,
|
|
36
|
+
base: Manifest,
|
|
37
|
+
head: Manifest,
|
|
38
|
+
c: Comparison,
|
|
39
|
+
): Promise<string> {
|
|
40
|
+
const parts: string[] = [];
|
|
41
|
+
for (const key of c.changed) {
|
|
42
|
+
const diff = await maskBlob(store, base.frames[key]!.png, head.frames[key]!.png);
|
|
43
|
+
parts.push(section(key, "changed", base.frames[key]!.png, head.frames[key]!.png, diff));
|
|
44
|
+
}
|
|
45
|
+
for (const key of c.added) parts.push(section(key, "added", undefined, head.frames[key]!.png));
|
|
46
|
+
for (const key of c.removed) parts.push(section(key, "removed", base.frames[key]!.png));
|
|
47
|
+
|
|
48
|
+
const title = `${c.changed.length} changed, ${c.added.length} added, ${c.removed.length} removed, ${c.unchanged.length} unchanged`;
|
|
49
|
+
return `<!doctype html><meta charset="utf-8"><title>lienzo review</title><style>body{font:14px system-ui;margin:2rem}div{display:flex;gap:1rem;align-items:flex-start}img{max-width:30vw;border:1px solid #ccc}</style><h1>${title}</h1>${parts.join("")}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Compares the capture of this checkout with the capture at its merge-base
|
|
53
|
+
// with `base`, and writes a static page of the differences.
|
|
54
|
+
export async function review(
|
|
55
|
+
root: string,
|
|
56
|
+
base: string,
|
|
57
|
+
store: Store = diskStore(root),
|
|
58
|
+
): Promise<Reviewed> {
|
|
59
|
+
const head = await commitOf(root);
|
|
60
|
+
const baseCommit = await mergeBase(root, base);
|
|
61
|
+
if (!baseCommit) return { ok: false, reason: `no merge-base with ${base}` };
|
|
62
|
+
|
|
63
|
+
const headManifest = await store.getManifest(head);
|
|
64
|
+
if (!headManifest) return { ok: false, reason: `no capture of ${head}: run lienzo snap` };
|
|
65
|
+
const baseManifest = await store.getManifest(baseCommit);
|
|
66
|
+
if (!baseManifest) {
|
|
67
|
+
return { ok: false, reason: `no capture of ${baseCommit}: run lienzo snap at that commit` };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const comparison = compare(baseManifest, headManifest);
|
|
71
|
+
if ("refused" in comparison) return { ok: false, reason: comparison.refused };
|
|
72
|
+
|
|
73
|
+
const page = path.join(root, FOLDER, "review", "index.html");
|
|
74
|
+
await mkdir(path.dirname(page), { recursive: true });
|
|
75
|
+
await Bun.write(page, await render(store, baseManifest, headManifest, comparison));
|
|
76
|
+
|
|
77
|
+
return { ok: true, page, comparison, check: checkOf(comparison, page) };
|
|
78
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
export type Served = { origin: string; close(): void };
|
|
4
|
+
|
|
5
|
+
// Serves a build's output folder on a free port. A path with no file falls back
|
|
6
|
+
// to the root page, as a single-page app's host does.
|
|
7
|
+
export function serveBuild(output: string): Served {
|
|
8
|
+
const server = Bun.serve({
|
|
9
|
+
port: 0,
|
|
10
|
+
hostname: "127.0.0.1",
|
|
11
|
+
async fetch(request) {
|
|
12
|
+
const { pathname } = new URL(request.url);
|
|
13
|
+
const relative = decodeURIComponent(pathname).replace(/^\/+/, "");
|
|
14
|
+
const file = path.resolve(
|
|
15
|
+
output,
|
|
16
|
+
relative.endsWith("/") || !relative ? `${relative}index.html` : relative,
|
|
17
|
+
);
|
|
18
|
+
if (file !== output && !file.startsWith(`${output}${path.sep}`))
|
|
19
|
+
return new Response("", { status: 403 });
|
|
20
|
+
|
|
21
|
+
const page = path.extname(file) === "" ? [path.join(output, "index.html")] : [];
|
|
22
|
+
for (const candidate of [file, ...page]) {
|
|
23
|
+
const found = Bun.file(candidate);
|
|
24
|
+
if (await found.exists()) return new Response(found);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return new Response("", { status: 404 });
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return { origin: `http://127.0.0.1:${server.port}`, close: () => void server.stop(true) };
|
|
32
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { loadConfig } from "../project/config.ts";
|
|
3
|
+
import type { Project } from "../project/project.ts";
|
|
4
|
+
import { build } from "../serve/build.ts";
|
|
5
|
+
import { capture, launch, rendererOf, type Frame, type Shot } from "./capture.ts";
|
|
6
|
+
import { commitOf } from "./refs.ts";
|
|
7
|
+
import { diskStore, hashOf, keyOf, type Manifest, type Store } from "./manifest.ts";
|
|
8
|
+
import { serveBuild } from "./serve.ts";
|
|
9
|
+
|
|
10
|
+
export type Snapped = { ok: true; manifest: Manifest } | { ok: false; reason: string };
|
|
11
|
+
|
|
12
|
+
// Builds the project, captures every frame twice and requires the two captures
|
|
13
|
+
// to match byte for byte. A frame that differs from itself would make every
|
|
14
|
+
// comparison against it noise, so the run fails and names it.
|
|
15
|
+
export async function snap(
|
|
16
|
+
project: Project,
|
|
17
|
+
store: Store = diskStore(project.root),
|
|
18
|
+
): Promise<Snapped> {
|
|
19
|
+
const { output, frames } = await loadConfig(project.root);
|
|
20
|
+
|
|
21
|
+
const code = await build(project);
|
|
22
|
+
if (code !== 0) return { ok: false, reason: `the build exited ${code}` };
|
|
23
|
+
|
|
24
|
+
const served = serveBuild(path.resolve(project.root, output));
|
|
25
|
+
const browser = await launch();
|
|
26
|
+
try {
|
|
27
|
+
let first: Shot[];
|
|
28
|
+
let second: Shot[];
|
|
29
|
+
try {
|
|
30
|
+
first = await capture(browser, served.origin, frames satisfies Frame[]);
|
|
31
|
+
second = await capture(browser, served.origin, frames);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return { ok: false, reason: error instanceof Error ? error.message : String(error) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const unstable = frames.filter(
|
|
37
|
+
(_, index) => hashOf(first[index]!.png) !== hashOf(second[index]!.png),
|
|
38
|
+
);
|
|
39
|
+
if (unstable.length > 0) {
|
|
40
|
+
return { ok: false, reason: `not deterministic: ${unstable.map(keyOf).join(", ")}` };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const entries = await Promise.all(
|
|
44
|
+
frames.map(async (frame, index) => {
|
|
45
|
+
const shot = first[index]!;
|
|
46
|
+
return [keyOf(frame), { png: await store.putBlob(shot.png), files: shot.files }] as const;
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
const manifest: Manifest = {
|
|
50
|
+
commit: await commitOf(project.root),
|
|
51
|
+
renderer: await rendererOf(browser),
|
|
52
|
+
frames: Object.fromEntries(entries),
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
await store.putManifest(manifest);
|
|
56
|
+
|
|
57
|
+
return { ok: true, manifest };
|
|
58
|
+
} finally {
|
|
59
|
+
await browser.close();
|
|
60
|
+
served.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// What `snap` records about one commit: for each frame, the hash of its PNG
|
|
2
|
+
// and the source files whose stamps the page shows.
|
|
3
|
+
export type Manifest = {
|
|
4
|
+
// The commit captured, or "working" when the tree held uncommitted changes.
|
|
5
|
+
// A working capture is never a baseline.
|
|
6
|
+
commit: string;
|
|
7
|
+
|
|
8
|
+
// The browser and system that drew it. PNGs of two renderers are not compared.
|
|
9
|
+
renderer: string;
|
|
10
|
+
|
|
11
|
+
// Frames by `<id>@<width>x<height>`.
|
|
12
|
+
frames: Record<string, { png: string; files: string[] }>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function keyOf(frame: { id: string; width: number; height: number }): string {
|
|
16
|
+
return `${frame.id}@${frame.width}x${frame.height}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Where captures live: PNGs by the SHA-256 of their bytes, in hex, and one
|
|
20
|
+
// manifest per commit. `snap` and `review` reach captures only through it, so
|
|
21
|
+
// a hosted store replaces the folder without either changing. This file has no
|
|
22
|
+
// imports, so a Worker can load it.
|
|
23
|
+
export interface Store {
|
|
24
|
+
putBlob(bytes: Uint8Array): Promise<string>;
|
|
25
|
+
getBlob(hash: string): Promise<Uint8Array | undefined>;
|
|
26
|
+
putManifest(manifest: Manifest): Promise<void>;
|
|
27
|
+
getManifest(commit: string): Promise<Manifest | undefined>;
|
|
28
|
+
}
|