@mizchi/vlmkit-core 0.9.1 → 0.11.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.
Files changed (57) hide show
  1. package/dist/apng.d.mts +42 -0
  2. package/dist/apng.mjs +199 -0
  3. package/dist/auth-state.d.mts +14 -3
  4. package/dist/auth-state.mjs +30 -6
  5. package/dist/bitmap-font.d.mts +48 -0
  6. package/dist/bitmap-font.mjs +945 -0
  7. package/dist/browser-launch.d.mts +65 -0
  8. package/dist/browser-launch.mjs +194 -0
  9. package/dist/cli-error.d.mts +73 -1
  10. package/dist/cli-error.mjs +164 -32
  11. package/dist/element-compare.mjs +8 -11
  12. package/dist/filmstrip.d.mts +121 -0
  13. package/dist/filmstrip.mjs +181 -0
  14. package/dist/gate-config.d.mts +107 -22
  15. package/dist/gate-config.mjs +138 -9
  16. package/dist/heatmap.d.mts +48 -4
  17. package/dist/heatmap.mjs +157 -12
  18. package/dist/index.d.mts +6 -4
  19. package/dist/index.mjs +4 -2
  20. package/dist/mask.d.mts +66 -2
  21. package/dist/mask.mjs +91 -4
  22. package/dist/page-load.d.mts +83 -0
  23. package/dist/page-load.mjs +116 -0
  24. package/dist/page-open.d.mts +123 -2
  25. package/dist/page-open.mjs +176 -6
  26. package/dist/plugin/args.d.mts +86 -0
  27. package/dist/plugin/args.mjs +162 -0
  28. package/dist/plugin/browser.d.mts +6 -0
  29. package/dist/plugin/browser.mjs +6 -0
  30. package/dist/plugin/cli-entry.d.mts +11 -0
  31. package/dist/plugin/cli-entry.mjs +69 -0
  32. package/dist/plugin/contract.d.mts +270 -0
  33. package/dist/plugin/contract.mjs +51 -0
  34. package/dist/plugin/index.d.mts +82 -0
  35. package/dist/plugin/index.mjs +81 -0
  36. package/dist/plugin/load.d.mts +36 -0
  37. package/dist/plugin/load.mjs +98 -0
  38. package/dist/plugin/registry.d.mts +60 -0
  39. package/dist/plugin/registry.mjs +160 -0
  40. package/dist/plugin/rule-tier.d.mts +65 -0
  41. package/dist/plugin/rule-tier.mjs +77 -0
  42. package/dist/plugin/rules.d.mts +112 -0
  43. package/dist/plugin/rules.mjs +188 -0
  44. package/dist/plugin/runner.d.mts +198 -0
  45. package/dist/plugin/runner.mjs +440 -0
  46. package/dist/png-utils.d.mts +31 -1
  47. package/dist/png-utils.mjs +24 -1
  48. package/dist/rect-overlap.d.mts +41 -0
  49. package/dist/rect-overlap.mjs +28 -0
  50. package/dist/run-ledger.d.mts +56 -2
  51. package/dist/run-ledger.mjs +102 -11
  52. package/dist/types.d.mts +60 -1
  53. package/dist/version.d.mts +20 -0
  54. package/dist/version.mjs +20 -0
  55. package/dist/webp.d.mts +31 -0
  56. package/dist/webp.mjs +105 -0
  57. package/package.json +17 -3
@@ -0,0 +1,42 @@
1
+ import { PngData } from "./png-utils.mjs";
2
+
3
+ //#region src/apng.d.ts
4
+ interface ApngOptions {
5
+ /** Per-frame delay in milliseconds. Default 200. */
6
+ delayMs?: number;
7
+ /** Loop count; 0 means forever, which is the default a reviewer expects. */
8
+ loops?: number;
9
+ /**
10
+ * Per-frame delays, when they differ — an animation sampled at 0/100/250/600ms should play on
11
+ * that timeline, not evenly. Falls back to `delayMs` for any frame not listed.
12
+ */
13
+ delaysMs?: readonly number[];
14
+ }
15
+ declare class ApngFrameMismatchError extends Error {
16
+ readonly name = "ApngFrameMismatchError";
17
+ }
18
+ /**
19
+ * Encode frames as an animated PNG.
20
+ *
21
+ * Every frame must be the same size: APNG can place smaller frames at an offset, and this
22
+ * deliberately does not — the callers here (`snapshot strip --animated`, `check animation --strip`)
23
+ * produce equal-sized frames of one component, and a silent offset would let a mis-sized frame
24
+ * animate in a corner instead of failing.
25
+ */
26
+ declare function encodeApng(frames: readonly PngData[], options?: ApngOptions): Uint8Array;
27
+ interface ApngChunk {
28
+ type: string;
29
+ length: number;
30
+ /** Sequence number for `fcTL` / `fdAT`, which is the field a viewer validates. */
31
+ sequence?: number;
32
+ }
33
+ /**
34
+ * Walk an APNG's chunks — for tests, and for anything that needs to verify a file it was handed.
35
+ *
36
+ * Exported because "the bytes are a valid animation" is otherwise only checkable by opening a
37
+ * browser, and this file's whole risk is byte-level: a wrong CRC, a wrong chunk order or a broken
38
+ * sequence produces a file that looks fine in a hex dump and is rejected on load.
39
+ */
40
+ declare function readApngChunks(bytes: Uint8Array): ApngChunk[];
41
+ //#endregion
42
+ export { ApngChunk, ApngFrameMismatchError, ApngOptions, encodeApng, readApngChunks };
package/dist/apng.mjs ADDED
@@ -0,0 +1,199 @@
1
+ import { deflateSync } from "node:zlib";
2
+ //#region src/apng.ts
3
+ /**
4
+ * Animated PNG, for the "one file that actually plays" output.
5
+ *
6
+ * ## Why APNG and not the animated WebP the item asked for
7
+ *
8
+ * The request was `snapshot strip --animated` / `check animation --strip x.webp --animated`, whose
9
+ * stated purpose is a single file you can paste into a PR and have it play, in contexts where you
10
+ * cannot hand someone the flipbook's HTML. Animated WebP cannot be encoded with what this repo
11
+ * has:
12
+ *
13
+ * - `@jsquash/webp` (the optional peer `webp.ts` already uses) wraps libwebp's SINGLE-image
14
+ * encoder. libwebp's `WebPAnimEncoder` is not exposed by it.
15
+ * - `sharp` can, and `webp.ts` already measured and rejected it: 29 MB against 1.1 MB for
16
+ * identical output bytes on the static case. Adding it for animation would put that install
17
+ * weight on everyone who wants a strip.
18
+ *
19
+ * APNG needs **no dependency at all**: it is a PNG with three extra chunks, and this file assembles
20
+ * them around IDAT payloads that `pngjs` — already a core dependency — produced. It is lossless
21
+ * and full-colour (WebP's animation is the same on that axis), every current browser plays it, and
22
+ * GitHub serves it as a PNG so it animates in a comment. A viewer that does not know APNG shows
23
+ * frame 0, which is a still strip frame rather than a broken image.
24
+ *
25
+ * The one honest cost against animated WebP: size. APNG stores whole frames deflated, so a 6-frame
26
+ * animation is roughly 6 PNGs, where animated WebP would inter-frame compress. For the frame
27
+ * counts this tool produces (4-12 states of one component) that is tens of KB, not MB.
28
+ *
29
+ * ## The format, as implemented
30
+ *
31
+ * Chunk order per the APNG spec: `IHDR`, `acTL`, then for the first frame `fcTL` + `IDAT`, then for
32
+ * each later frame `fcTL` + `fdAT`. `fcTL` and `fdAT` share ONE sequence-number counter — that
33
+ * shared counter is the detail most hand-rolled encoders get wrong, and a viewer rejects the whole
34
+ * file when it is off.
35
+ *
36
+ * Frame 0 is both the still fallback and the first animation frame: it is written as a plain `IDAT`
37
+ * with an `fcTL` in front of it, which is what makes a non-APNG viewer show it.
38
+ */
39
+ const SIGNATURE = new Uint8Array([
40
+ 137,
41
+ 80,
42
+ 78,
43
+ 71,
44
+ 13,
45
+ 10,
46
+ 26,
47
+ 10
48
+ ]);
49
+ /** CRC-32, table built once. Every PNG chunk carries one and a viewer checks it. */
50
+ const CRC_TABLE = (() => {
51
+ const table = new Uint32Array(256);
52
+ for (let n = 0; n < 256; n++) {
53
+ let c = n;
54
+ for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
55
+ table[n] = c >>> 0;
56
+ }
57
+ return table;
58
+ })();
59
+ function crc32(bytes) {
60
+ let c = 4294967295;
61
+ for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 255] ^ c >>> 8;
62
+ return (c ^ 4294967295) >>> 0;
63
+ }
64
+ function chunk(type, payload) {
65
+ const out = new Uint8Array(12 + payload.length);
66
+ const view = new DataView(out.buffer);
67
+ view.setUint32(0, payload.length);
68
+ for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
69
+ out.set(payload, 8);
70
+ view.setUint32(8 + payload.length, crc32(out.subarray(4, 8 + payload.length)));
71
+ return out;
72
+ }
73
+ /**
74
+ * RGBA scanlines to a deflated PNG data stream, filter type 0 (None) per row.
75
+ *
76
+ * `pngjs` picks smarter filters and would compress better, but reading its IDAT back out means
77
+ * parsing its output — and the byte-level work this file already does is the part worth keeping
78
+ * small. Measured on a 6-frame strip of `dashboard.html`: 152 KB here against 138 KB with pngjs's
79
+ * filtering, i.e. 10% for not adding a parser.
80
+ */
81
+ function frameStream(frame) {
82
+ const stride = frame.width * 4;
83
+ const raw = new Uint8Array((stride + 1) * frame.height);
84
+ for (let y = 0; y < frame.height; y++) {
85
+ raw[y * (stride + 1)] = 0;
86
+ raw.set(frame.data.subarray(y * stride, y * stride + stride), y * (stride + 1) + 1);
87
+ }
88
+ return new Uint8Array(deflateSync(raw, { level: 9 }));
89
+ }
90
+ /**
91
+ * Delay as the APNG numerator/denominator pair.
92
+ *
93
+ * `delay_num / delay_den` seconds, both 16-bit. A 1000 denominator makes the numerator the
94
+ * millisecond value directly — until 65535ms, past which it is clamped rather than wrapped: a
95
+ * wrapped 16-bit delay turns a 70-second frame into a 4-millisecond one, which reads as a
96
+ * corrupted animation rather than a long pause.
97
+ */
98
+ function delayPair(ms) {
99
+ return {
100
+ num: Math.max(0, Math.min(65535, Math.round(ms))),
101
+ den: 1e3
102
+ };
103
+ }
104
+ var ApngFrameMismatchError = class extends Error {
105
+ name = "ApngFrameMismatchError";
106
+ };
107
+ /**
108
+ * Encode frames as an animated PNG.
109
+ *
110
+ * Every frame must be the same size: APNG can place smaller frames at an offset, and this
111
+ * deliberately does not — the callers here (`snapshot strip --animated`, `check animation --strip`)
112
+ * produce equal-sized frames of one component, and a silent offset would let a mis-sized frame
113
+ * animate in a corner instead of failing.
114
+ */
115
+ function encodeApng(frames, options = {}) {
116
+ if (frames.length === 0) throw new ApngFrameMismatchError("encodeApng needs at least one frame");
117
+ const { width, height } = frames[0];
118
+ for (const [index, frame] of frames.entries()) {
119
+ if (frame.width !== width || frame.height !== height) throw new ApngFrameMismatchError(`frame ${index} is ${frame.width}x${frame.height}, frame 0 is ${width}x${height} — every frame of an APNG must be the same size here`);
120
+ if (frame.data.length < width * height * 4) throw new ApngFrameMismatchError(`frame ${index} carries ${frame.data.length} bytes, ${width * height * 4} needed for RGBA`);
121
+ }
122
+ const parts = [SIGNATURE];
123
+ const ihdr = new Uint8Array(13);
124
+ const ihdrView = new DataView(ihdr.buffer);
125
+ ihdrView.setUint32(0, width);
126
+ ihdrView.setUint32(4, height);
127
+ ihdr[8] = 8;
128
+ ihdr[9] = 6;
129
+ ihdr[10] = 0;
130
+ ihdr[11] = 0;
131
+ ihdr[12] = 0;
132
+ parts.push(chunk("IHDR", ihdr));
133
+ const actl = new Uint8Array(8);
134
+ const actlView = new DataView(actl.buffer);
135
+ actlView.setUint32(0, frames.length);
136
+ actlView.setUint32(4, options.loops ?? 0);
137
+ parts.push(chunk("acTL", actl));
138
+ let sequence = 0;
139
+ frames.forEach((frame, index) => {
140
+ const { num, den } = delayPair(options.delaysMs?.[index] ?? options.delayMs ?? 200);
141
+ const fctl = new Uint8Array(26);
142
+ const fctlView = new DataView(fctl.buffer);
143
+ fctlView.setUint32(0, sequence++);
144
+ fctlView.setUint32(4, width);
145
+ fctlView.setUint32(8, height);
146
+ fctlView.setUint32(12, 0);
147
+ fctlView.setUint32(16, 0);
148
+ fctlView.setUint16(20, num);
149
+ fctlView.setUint16(22, den);
150
+ fctl[24] = 0;
151
+ fctl[25] = 0;
152
+ parts.push(chunk("fcTL", fctl));
153
+ const stream = frameStream(frame);
154
+ if (index === 0) parts.push(chunk("IDAT", stream));
155
+ else {
156
+ const fdat = new Uint8Array(4 + stream.length);
157
+ new DataView(fdat.buffer).setUint32(0, sequence++);
158
+ fdat.set(stream, 4);
159
+ parts.push(chunk("fdAT", fdat));
160
+ }
161
+ });
162
+ parts.push(chunk("IEND", new Uint8Array(0)));
163
+ const total = parts.reduce((n, part) => n + part.length, 0);
164
+ const out = new Uint8Array(total);
165
+ let offset = 0;
166
+ for (const part of parts) {
167
+ out.set(part, offset);
168
+ offset += part.length;
169
+ }
170
+ return out;
171
+ }
172
+ /**
173
+ * Walk an APNG's chunks — for tests, and for anything that needs to verify a file it was handed.
174
+ *
175
+ * Exported because "the bytes are a valid animation" is otherwise only checkable by opening a
176
+ * browser, and this file's whole risk is byte-level: a wrong CRC, a wrong chunk order or a broken
177
+ * sequence produces a file that looks fine in a hex dump and is rejected on load.
178
+ */
179
+ function readApngChunks(bytes) {
180
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
181
+ const chunks = [];
182
+ let offset = 8;
183
+ while (offset + 8 <= bytes.length) {
184
+ const length = view.getUint32(offset);
185
+ const type = String.fromCharCode(...bytes.subarray(offset + 4, offset + 8));
186
+ const payload = bytes.subarray(offset + 8, offset + 8 + length);
187
+ const stored = view.getUint32(offset + 8 + length);
188
+ if (crc32(bytes.subarray(offset + 4, offset + 8 + length)) !== stored) throw new Error(`chunk ${type} at ${offset} has a bad CRC`);
189
+ chunks.push({
190
+ type,
191
+ length,
192
+ ...type === "fcTL" || type === "fdAT" ? { sequence: new DataView(payload.buffer, payload.byteOffset).getUint32(0) } : {}
193
+ });
194
+ offset += 12 + length;
195
+ }
196
+ return chunks;
197
+ }
198
+ //#endregion
199
+ export { ApngFrameMismatchError, encodeApng, readApngChunks };
@@ -24,7 +24,18 @@ declare function readStorageState(path: string): {
24
24
  declare function withAuthState<T extends object>(base: T, explicit?: string): T & {
25
25
  storageState?: string;
26
26
  };
27
- /** One-line notice so a run that used auth says so in its output. */
28
- declare function authStateNotice(explicit?: string): string | null;
27
+ /** Forget any session applied by a previous run. Called per gate invocation. */
28
+ declare function resetAuthStateNotice(): void;
29
+ /**
30
+ * One line naming the session a run used, or `null` when it ran unauthenticated.
31
+ *
32
+ * This existed and was never called, which left the gates able to measure a
33
+ * logged-in page while saying nothing about it. `VLMKIT_STORAGE_STATE` makes that
34
+ * the easy case rather than the exotic one: no flag appears on the command line, so
35
+ * neither the terminal output nor the markdown report distinguished "the dashboard"
36
+ * from "the login wall it redirected to". Reported from what was applied rather than
37
+ * from what was configured, so a gate that ignores auth never claims to have used it.
38
+ */
39
+ declare function authStateNotice(): string | null;
29
40
  //#endregion
30
- export { STORAGE_STATE_ENV, authStateNotice, readStorageState, storageStatePath, withAuthState };
41
+ export { STORAGE_STATE_ENV, authStateNotice, readStorageState, resetAuthStateNotice, storageStatePath, withAuthState };
@@ -68,15 +68,39 @@ function withAuthState(base, explicit) {
68
68
  const path = storageStatePath(explicit);
69
69
  if (!path) return base;
70
70
  readStorageState(path);
71
+ const abs = resolve(path);
72
+ appliedPath = abs;
71
73
  return {
72
74
  ...base,
73
- storageState: resolve(path)
75
+ storageState: abs
74
76
  };
75
77
  }
76
- /** One-line notice so a run that used auth says so in its output. */
77
- function authStateNotice(explicit) {
78
- const path = storageStatePath(explicit);
79
- return path ? `auth: storage state from ${resolve(path)}` : null;
78
+ /**
79
+ * Set by `withAuthState` when a session was actually applied, and read by whoever
80
+ * prints — the same module-level choke point the run ledger uses, for the same
81
+ * reason: the fact worth announcing is discovered deep inside a measurement, and
82
+ * the thing that announces it is the runner.
83
+ *
84
+ * `withAuthState` is the only place a storage state reaches Playwright, so a gate
85
+ * cannot acquire a session without passing through here.
86
+ */
87
+ let appliedPath = null;
88
+ /** Forget any session applied by a previous run. Called per gate invocation. */
89
+ function resetAuthStateNotice() {
90
+ appliedPath = null;
91
+ }
92
+ /**
93
+ * One line naming the session a run used, or `null` when it ran unauthenticated.
94
+ *
95
+ * This existed and was never called, which left the gates able to measure a
96
+ * logged-in page while saying nothing about it. `VLMKIT_STORAGE_STATE` makes that
97
+ * the easy case rather than the exotic one: no flag appears on the command line, so
98
+ * neither the terminal output nor the markdown report distinguished "the dashboard"
99
+ * from "the login wall it redirected to". Reported from what was applied rather than
100
+ * from what was configured, so a gate that ignores auth never claims to have used it.
101
+ */
102
+ function authStateNotice() {
103
+ return appliedPath ? `auth: storage state from ${appliedPath}` : null;
80
104
  }
81
105
  //#endregion
82
- export { STORAGE_STATE_ENV, authStateNotice, readStorageState, storageStatePath, withAuthState };
106
+ export { STORAGE_STATE_ENV, authStateNotice, readStorageState, resetAuthStateNotice, storageStatePath, withAuthState };
@@ -0,0 +1,48 @@
1
+ //#region src/bitmap-font.d.ts
2
+ /**
3
+ * A 5x7 bitmap font, drawn here rather than taken from the system.
4
+ *
5
+ * Labels were kept out of the filmstrip on the grounds that text would make the
6
+ * output depend on font rendering — the class of platform-dependent pixel this
7
+ * toolkit exists to catch. A dogfood agent answered that precisely:
8
+ *
9
+ * "The rationale (font rendering is platform-dependent pixels) is sound for VRT
10
+ * baselines, but this artifact is *not* a baseline — it is an attachment whose
11
+ * whole job is to be read by a human out of context."
12
+ *
13
+ * Nothing pixel-compares a filmstrip. And glyphs we own settle it either way:
14
+ * identical bytes on every platform, no fontconfig, no hinting, no metrics, no
15
+ * async font load to race a screenshot.
16
+ *
17
+ * Coverage is printable ASCII (0x20-0x7e), which is what a CSS selector, a
18
+ * millisecond count and a row index need. Anything outside it is dropped rather
19
+ * than substituted, so a label can never come out as a row of boxes.
20
+ */
21
+ declare const GLYPH_WIDTH = 5;
22
+ declare const GLYPH_HEIGHT = 7;
23
+ /** Target of a draw: any RGBA buffer that knows its own dimensions. */
24
+ interface RgbaTarget {
25
+ width: number;
26
+ height: number;
27
+ data: Uint8Array;
28
+ }
29
+ /** Height in px of one line at `scale`. */
30
+ declare function textHeight(scale?: number): number;
31
+ /** Width in px of `text` at `scale`, excluding the trailing blank column. */
32
+ declare function measureText(text: string, scale?: number): number;
33
+ /**
34
+ * The longest prefix of `text` that fits in `maxWidth`, with a trailing `..` when
35
+ * something was dropped.
36
+ *
37
+ * Truncation is at the *end* deliberately, even though the distinguishing part of a
38
+ * CSS selector is usually its tail: a label whose head is cut cannot be matched back
39
+ * to the terminal output at all, while one whose tail is cut still can.
40
+ */
41
+ declare function fitText(text: string, maxWidth: number, scale?: number): string;
42
+ /**
43
+ * Draw `text` into an RGBA buffer with its top-left at (x, y). Writes fully opaque
44
+ * ink; pixels outside the buffer are skipped rather than wrapped.
45
+ */
46
+ declare function drawText(target: RgbaTarget, text: string, x: number, y: number, rgb: readonly [number, number, number], scale?: number): void;
47
+ //#endregion
48
+ export { GLYPH_HEIGHT, GLYPH_WIDTH, RgbaTarget, drawText, fitText, measureText, textHeight };