@mizchi/vlmkit-capture 0.6.0 → 0.7.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/package.json +3 -3
- package/src/batch-prescan.ts +115 -0
- package/src/cloudflare-quick-actions.ts +304 -0
- package/src/crater-client.ts +100 -6
- package/src/crater-smoke.ts +336 -0
- package/src/crater-wasm.ts +260 -0
- package/src/detection-types.ts +7 -0
- package/src/index.ts +71 -0
- package/src/prescanner.ts +56 -2
- package/src/viewport-discovery.ts +205 -17
package/src/index.ts
CHANGED
|
@@ -25,14 +25,21 @@ export {
|
|
|
25
25
|
// ---- Viewport / breakpoint discovery ----
|
|
26
26
|
export {
|
|
27
27
|
discoverViewports,
|
|
28
|
+
discoverViewportsViaCrater,
|
|
29
|
+
discoverViewportsWithBackend,
|
|
28
30
|
generateViewports,
|
|
29
31
|
extractBreakpoints,
|
|
30
32
|
extractBreakpointsFromHtml,
|
|
33
|
+
extractBreakpointsFromHtmlWithStylesheets,
|
|
31
34
|
extractResponsiveBreakpointsFromHtml,
|
|
35
|
+
extractResponsiveBreakpointsFromHtmlWithStylesheets,
|
|
36
|
+
extractStylesheetHrefsFromHtml,
|
|
32
37
|
toResponsiveBreakpoints,
|
|
33
38
|
mergeResponsiveBreakpoints,
|
|
34
39
|
type Breakpoint,
|
|
40
|
+
type CraterViewportSource,
|
|
35
41
|
type ResponsiveBreakpoint,
|
|
42
|
+
type ViewportSource,
|
|
36
43
|
type ViewportSpec,
|
|
37
44
|
type ViewportOptions,
|
|
38
45
|
type DiscoveryResult,
|
|
@@ -44,6 +51,26 @@ export {
|
|
|
44
51
|
formatPlaywrightLaunchError,
|
|
45
52
|
} from "./playwright-launch-error.ts";
|
|
46
53
|
|
|
54
|
+
// ---- Cloudflare Browser Run Quick Actions ----
|
|
55
|
+
export {
|
|
56
|
+
buildCloudflareQuickActionEndpoint,
|
|
57
|
+
createCloudflareQuickActionsClient,
|
|
58
|
+
extractCloudflareCrawlRoutes,
|
|
59
|
+
resolveCloudflareQuickActionsConfig,
|
|
60
|
+
type CloudflareCrawlRecord,
|
|
61
|
+
type CloudflareCrawlRecordStatus,
|
|
62
|
+
type CloudflareCrawlRequest,
|
|
63
|
+
type CloudflareCrawlResult,
|
|
64
|
+
type CloudflareCrawlRoute,
|
|
65
|
+
type CloudflareCrawlStartResult,
|
|
66
|
+
type CloudflareQuickAction,
|
|
67
|
+
type CloudflareQuickActionEndpointInput,
|
|
68
|
+
type CloudflareQuickActionsConfig,
|
|
69
|
+
type CloudflareQuickActionsEnv,
|
|
70
|
+
type CloudflareScreenshotRequest,
|
|
71
|
+
type CloudflareScreenshotResult,
|
|
72
|
+
} from "./cloudflare-quick-actions.ts";
|
|
73
|
+
|
|
47
74
|
// ---- Playwright report analyzer ----
|
|
48
75
|
export {
|
|
49
76
|
analyzeReport,
|
|
@@ -68,19 +95,63 @@ export {
|
|
|
68
95
|
// ---- Crater client (Bidi protocol) ----
|
|
69
96
|
export {
|
|
70
97
|
CraterClient,
|
|
98
|
+
CRATER_BIDI_URL_ENV,
|
|
99
|
+
CRATER_ROOT_ENV,
|
|
71
100
|
DEFAULT_BIDI_URL,
|
|
101
|
+
VLMKIT_CRATER_BIDI_URL_ENV,
|
|
102
|
+
VLMKIT_CRATER_ROOT_ENV,
|
|
103
|
+
resolveCraterBidiUrl,
|
|
72
104
|
type CraterResponsiveBreakpoint,
|
|
73
105
|
type CraterBreakpointDiscoveryDiagnostics,
|
|
74
106
|
type CraterRuleViewportMapEntry,
|
|
75
107
|
type CraterRuleViewportMap,
|
|
76
108
|
type CraterComputedStyleWithState,
|
|
109
|
+
type CraterCssMutation,
|
|
110
|
+
type CraterCssMutationAction,
|
|
111
|
+
type CraterRenderVariant,
|
|
77
112
|
type CraterBatchRenderResult,
|
|
78
113
|
type CraterBreakpointDiscoveryResult,
|
|
79
114
|
type PaintNode,
|
|
80
115
|
type PaintProps,
|
|
81
116
|
} from "./crater-client.ts";
|
|
82
117
|
export { isCraterAvailable } from "./crater-client.ts";
|
|
118
|
+
export {
|
|
119
|
+
CRATER_WASM_MODULE_ENV,
|
|
120
|
+
createCraterWasmLayoutBackend,
|
|
121
|
+
loadCraterWasmModule,
|
|
122
|
+
normalizeCraterLayoutJson,
|
|
123
|
+
summarizeCraterLayout,
|
|
124
|
+
type CraterBoxRect,
|
|
125
|
+
type CraterLayoutDiagnostics,
|
|
126
|
+
type CraterLayoutNode,
|
|
127
|
+
type CraterLayoutRootBox,
|
|
128
|
+
type CraterWasmLayoutBackend,
|
|
129
|
+
type CraterWasmModule,
|
|
130
|
+
type CraterWasmRenderRequest,
|
|
131
|
+
type CraterWasmRenderResult,
|
|
132
|
+
type CraterWasmViewport,
|
|
133
|
+
type LoadCraterWasmModuleOptions,
|
|
134
|
+
} from "./crater-wasm.ts";
|
|
135
|
+
export {
|
|
136
|
+
runCraterBidiSmoke,
|
|
137
|
+
formatCraterSmokeReport,
|
|
138
|
+
type CraterSmokeCheck,
|
|
139
|
+
type CraterSmokeClient,
|
|
140
|
+
type CraterSmokeReport,
|
|
141
|
+
type CraterSmokeStatus,
|
|
142
|
+
} from "./crater-smoke.ts";
|
|
83
143
|
|
|
84
144
|
// ---- Detection / paint-tree types ----
|
|
85
145
|
export * from "./detection-types.ts";
|
|
86
146
|
export { diffPaintTrees } from "./crater-client.ts";
|
|
147
|
+
|
|
148
|
+
// ---- Batch prescan (Crater v0.18.0 batchRender driver) ----
|
|
149
|
+
export {
|
|
150
|
+
hasAnyBatchPrescanSignal,
|
|
151
|
+
mutationsForPropertyRemoval,
|
|
152
|
+
mutationsForSelectorBlockRemoval,
|
|
153
|
+
runBatchPrescan,
|
|
154
|
+
type BatchPrescanOptions,
|
|
155
|
+
type BatchPrescanRequest,
|
|
156
|
+
type BatchPrescanResult,
|
|
157
|
+
} from "./batch-prescan.ts";
|
package/src/prescanner.ts
CHANGED
|
@@ -2,11 +2,23 @@ import type { ViewportDetectionResult } from "./detection-types.ts";
|
|
|
2
2
|
|
|
3
3
|
export type PrescannerResolvedBy = "crater" | "chromium" | "none";
|
|
4
4
|
|
|
5
|
+
/** Which Crater metadata signal first fired for a trial resolved by crater. */
|
|
6
|
+
export type PrescannerCraterSignal =
|
|
7
|
+
| "paint-tree"
|
|
8
|
+
| "computed-style"
|
|
9
|
+
| "forced-state"
|
|
10
|
+
| "visual"
|
|
11
|
+
| "none";
|
|
12
|
+
|
|
5
13
|
export interface PrescannerTrialResolution {
|
|
6
14
|
craterDetected: boolean;
|
|
7
15
|
fallbackUsed: boolean;
|
|
8
16
|
finalDetected: boolean;
|
|
9
17
|
resolvedBy: PrescannerResolvedBy;
|
|
18
|
+
/** Whether the crater path skipped PNG capture entirely (metadata-only). */
|
|
19
|
+
metadataOnly: boolean;
|
|
20
|
+
/** First crater signal type that fired (paint-tree wins over computed-style → forced-state → visual). */
|
|
21
|
+
craterSignal: PrescannerCraterSignal;
|
|
10
22
|
}
|
|
11
23
|
|
|
12
24
|
export interface PrescannerTrialSummary {
|
|
@@ -16,10 +28,24 @@ export interface PrescannerTrialSummary {
|
|
|
16
28
|
chromiumFallbacks: number;
|
|
17
29
|
chromiumDetected: number;
|
|
18
30
|
passedAfterFallback: number;
|
|
31
|
+
/** Trials resolved by crater that never captured a PNG (metadata-only detection). */
|
|
32
|
+
metadataOnly: number;
|
|
33
|
+
/** Per-signal breakdown of crater-resolved trials. */
|
|
34
|
+
craterBySignal: {
|
|
35
|
+
paintTree: number;
|
|
36
|
+
computedStyle: number;
|
|
37
|
+
forcedState: number;
|
|
38
|
+
visual: number;
|
|
39
|
+
};
|
|
19
40
|
}
|
|
20
41
|
|
|
21
42
|
export function hasCraterPrescanSignal(viewports: ViewportDetectionResult[]): boolean {
|
|
22
|
-
return viewports.some((viewport) =>
|
|
43
|
+
return viewports.some((viewport) =>
|
|
44
|
+
viewport.visualDiffDetected ||
|
|
45
|
+
viewport.computedStyleDiffCount > 0 ||
|
|
46
|
+
viewport.hoverDiffDetected ||
|
|
47
|
+
viewport.paintTreeDiffCount > 0
|
|
48
|
+
);
|
|
23
49
|
}
|
|
24
50
|
|
|
25
51
|
export function hasAnyDetectionSignal(viewports: ViewportDetectionResult[]): boolean {
|
|
@@ -32,17 +58,35 @@ export function hasAnyDetectionSignal(viewports: ViewportDetectionResult[]): boo
|
|
|
32
58
|
);
|
|
33
59
|
}
|
|
34
60
|
|
|
61
|
+
/** Classify the first crater signal that fired. */
|
|
62
|
+
function classifyCraterSignal(viewports: ViewportDetectionResult[]): PrescannerCraterSignal {
|
|
63
|
+
if (viewports.some((v) => v.paintTreeDiffCount > 0)) return "paint-tree";
|
|
64
|
+
if (viewports.some((v) => v.computedStyleDiffCount > 0)) return "computed-style";
|
|
65
|
+
if (viewports.some((v) => v.hoverDiffDetected)) return "forced-state";
|
|
66
|
+
if (viewports.some((v) => v.visualDiffDetected)) return "visual";
|
|
67
|
+
return "none";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** True when every viewport in the crater capture skipped its PNG. */
|
|
71
|
+
function isMetadataOnly(viewports: ViewportDetectionResult[]): boolean {
|
|
72
|
+
if (viewports.length === 0) return false;
|
|
73
|
+
return viewports.every((v) => v.visualCaptureSkipped === true);
|
|
74
|
+
}
|
|
75
|
+
|
|
35
76
|
export function resolvePrescannerTrial(
|
|
36
77
|
craterViewports: ViewportDetectionResult[],
|
|
37
78
|
chromiumViewports: ViewportDetectionResult[] = [],
|
|
38
79
|
): PrescannerTrialResolution {
|
|
39
80
|
const craterDetected = hasCraterPrescanSignal(craterViewports);
|
|
81
|
+
const metadataOnly = isMetadataOnly(craterViewports);
|
|
40
82
|
if (craterDetected) {
|
|
41
83
|
return {
|
|
42
84
|
craterDetected: true,
|
|
43
85
|
fallbackUsed: false,
|
|
44
86
|
finalDetected: true,
|
|
45
87
|
resolvedBy: "crater",
|
|
88
|
+
metadataOnly,
|
|
89
|
+
craterSignal: classifyCraterSignal(craterViewports),
|
|
46
90
|
};
|
|
47
91
|
}
|
|
48
92
|
|
|
@@ -52,18 +96,28 @@ export function resolvePrescannerTrial(
|
|
|
52
96
|
fallbackUsed: true,
|
|
53
97
|
finalDetected: chromiumDetected,
|
|
54
98
|
resolvedBy: chromiumDetected ? "chromium" : "none",
|
|
99
|
+
metadataOnly,
|
|
100
|
+
craterSignal: "none",
|
|
55
101
|
};
|
|
56
102
|
}
|
|
57
103
|
|
|
58
104
|
export function summarizePrescannerTrials(
|
|
59
105
|
resolutions: PrescannerTrialResolution[],
|
|
60
106
|
): PrescannerTrialSummary {
|
|
107
|
+
const craterResolved = resolutions.filter((r) => r.resolvedBy === "crater");
|
|
61
108
|
return {
|
|
62
109
|
total: resolutions.length,
|
|
63
110
|
detected: resolutions.filter((resolution) => resolution.finalDetected).length,
|
|
64
|
-
craterResolved:
|
|
111
|
+
craterResolved: craterResolved.length,
|
|
65
112
|
chromiumFallbacks: resolutions.filter((resolution) => resolution.fallbackUsed).length,
|
|
66
113
|
chromiumDetected: resolutions.filter((resolution) => resolution.resolvedBy === "chromium").length,
|
|
67
114
|
passedAfterFallback: resolutions.filter((resolution) => resolution.resolvedBy === "none").length,
|
|
115
|
+
metadataOnly: craterResolved.filter((r) => r.metadataOnly).length,
|
|
116
|
+
craterBySignal: {
|
|
117
|
+
paintTree: craterResolved.filter((r) => r.craterSignal === "paint-tree").length,
|
|
118
|
+
computedStyle: craterResolved.filter((r) => r.craterSignal === "computed-style").length,
|
|
119
|
+
forcedState: craterResolved.filter((r) => r.craterSignal === "forced-state").length,
|
|
120
|
+
visual: craterResolved.filter((r) => r.craterSignal === "visual").length,
|
|
121
|
+
},
|
|
68
122
|
};
|
|
69
123
|
}
|
|
@@ -23,17 +23,39 @@ export interface ResponsiveBreakpoint {
|
|
|
23
23
|
ruleCount: number;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/** Where a viewport candidate originated. */
|
|
27
|
+
export type ViewportSource =
|
|
28
|
+
| "standard"
|
|
29
|
+
| "regex-boundary"
|
|
30
|
+
| "regex-sample"
|
|
31
|
+
| "crater-required"
|
|
32
|
+
| "crater-rule-map";
|
|
33
|
+
|
|
26
34
|
export interface ViewportSpec {
|
|
27
35
|
width: number;
|
|
28
36
|
height: number;
|
|
29
37
|
label: string;
|
|
30
38
|
reason: string; // why this viewport was chosen
|
|
39
|
+
/**
|
|
40
|
+
* Optional provenance for downstream reports — present when discovery
|
|
41
|
+
* went through `generateViewports` / the Crater discovery path. Older
|
|
42
|
+
* callers that build a plain spec without this field still validate.
|
|
43
|
+
*/
|
|
44
|
+
source?: ViewportSource;
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
export interface DiscoveryResult {
|
|
34
48
|
breakpoints: Breakpoint[];
|
|
35
49
|
responsiveBreakpoints: ResponsiveBreakpoint[];
|
|
36
50
|
viewports: ViewportSpec[];
|
|
51
|
+
/**
|
|
52
|
+
* Which backend supplied the viewport list. `"crater"` means the
|
|
53
|
+
* BiDi v0.18.0 viewport intelligence APIs returned a usable set;
|
|
54
|
+
* `"regex"` means the inline-CSS regex discovery did. `"hybrid"`
|
|
55
|
+
* means Crater seeded the list and regex contributed additional
|
|
56
|
+
* widths (or vice versa).
|
|
57
|
+
*/
|
|
58
|
+
backend: "regex" | "crater" | "hybrid";
|
|
37
59
|
}
|
|
38
60
|
|
|
39
61
|
type ViewportBreakpoint = Breakpoint | ResponsiveBreakpoint;
|
|
@@ -67,12 +89,46 @@ export function extractBreakpoints(css: string): Breakpoint[] {
|
|
|
67
89
|
return [...breakpoints.values()].sort((a, b) => a.value - b.value);
|
|
68
90
|
}
|
|
69
91
|
|
|
92
|
+
function extractInlineStyleCss(html: string): string {
|
|
93
|
+
const styleMatch = html.match(/<style[^>]*>([\s\S]*?)<\/style>/g);
|
|
94
|
+
if (!styleMatch) return "";
|
|
95
|
+
return styleMatch.map((s) => s.replace(/<\/?style[^>]*>/g, "")).join("\n");
|
|
96
|
+
}
|
|
97
|
+
|
|
70
98
|
/** Extract breakpoints from HTML <style> */
|
|
71
99
|
export function extractBreakpointsFromHtml(html: string): Breakpoint[] {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
100
|
+
return extractBreakpoints(extractInlineStyleCss(html));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function readAttr(tag: string, name: string): string | undefined {
|
|
104
|
+
const re = new RegExp(`\\b${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i");
|
|
105
|
+
const m = tag.match(re);
|
|
106
|
+
return m?.[1] ?? m?.[2] ?? m?.[3];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isStylesheetLink(tag: string): boolean {
|
|
110
|
+
if (!/^<link\b/i.test(tag)) return false;
|
|
111
|
+
const rel = readAttr(tag, "rel");
|
|
112
|
+
if (!rel) return false;
|
|
113
|
+
return rel.split(/\s+/).some((part) => part.toLowerCase() === "stylesheet");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function extractStylesheetHrefsFromHtml(html: string): string[] {
|
|
117
|
+
const linkTags = html.match(/<link\b[^>]*>/gi) ?? [];
|
|
118
|
+
return linkTags
|
|
119
|
+
.filter(isStylesheetLink)
|
|
120
|
+
.map((tag) => readAttr(tag, "href"))
|
|
121
|
+
.filter((href): href is string => !!href);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function extractBreakpointsFromHtmlWithStylesheets(
|
|
125
|
+
html: string,
|
|
126
|
+
stylesheetTexts: string[],
|
|
127
|
+
): Breakpoint[] {
|
|
128
|
+
return extractBreakpoints([
|
|
129
|
+
extractInlineStyleCss(html),
|
|
130
|
+
...stylesheetTexts,
|
|
131
|
+
].join("\n"));
|
|
76
132
|
}
|
|
77
133
|
|
|
78
134
|
function isResponsiveBreakpoint(
|
|
@@ -156,6 +212,15 @@ export function extractResponsiveBreakpointsFromHtml(
|
|
|
156
212
|
return toResponsiveBreakpoints(extractBreakpointsFromHtml(html));
|
|
157
213
|
}
|
|
158
214
|
|
|
215
|
+
export function extractResponsiveBreakpointsFromHtmlWithStylesheets(
|
|
216
|
+
html: string,
|
|
217
|
+
stylesheetTexts: string[],
|
|
218
|
+
): ResponsiveBreakpoint[] {
|
|
219
|
+
return toResponsiveBreakpoints(
|
|
220
|
+
extractBreakpointsFromHtmlWithStylesheets(html, stylesheetTexts),
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
159
224
|
// ---- Viewport generation ----
|
|
160
225
|
|
|
161
226
|
export interface ViewportOptions {
|
|
@@ -193,34 +258,34 @@ export function generateViewports(
|
|
|
193
258
|
|
|
194
259
|
const viewportMap = new Map<number, ViewportSpec>();
|
|
195
260
|
|
|
196
|
-
function add(width: number, label: string, reason: string) {
|
|
261
|
+
function add(width: number, label: string, reason: string, source: ViewportSource) {
|
|
197
262
|
if (width < 320 || width > 2560) return;
|
|
198
263
|
if (!viewportMap.has(width)) {
|
|
199
|
-
viewportMap.set(width, { width, height, label, reason });
|
|
264
|
+
viewportMap.set(width, { width, height, label, reason, source });
|
|
200
265
|
}
|
|
201
266
|
}
|
|
202
267
|
|
|
203
268
|
// Standard viewports
|
|
204
269
|
if (includeStandard) {
|
|
205
270
|
for (const sv of STANDARD_VIEWPORTS) {
|
|
206
|
-
add(sv.width, sv.label, "standard");
|
|
271
|
+
add(sv.width, sv.label, "standard", "standard");
|
|
207
272
|
}
|
|
208
273
|
}
|
|
209
274
|
|
|
210
275
|
// Boundary viewports for each breakpoint
|
|
211
276
|
for (const bp of responsiveBreakpoints) {
|
|
212
277
|
if (bp.op === "ge") {
|
|
213
|
-
add(bp.valuePx - 1, `below-${bp.valuePx}`, `${bp.raw} boundary-below
|
|
214
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at
|
|
278
|
+
add(bp.valuePx - 1, `below-${bp.valuePx}`, `${bp.raw} boundary-below`, "regex-boundary");
|
|
279
|
+
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
215
280
|
} else if (bp.op === "gt") {
|
|
216
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at
|
|
217
|
-
add(bp.valuePx + 1, `above-${bp.valuePx}`, `${bp.raw} boundary-above
|
|
281
|
+
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
282
|
+
add(bp.valuePx + 1, `above-${bp.valuePx}`, `${bp.raw} boundary-above`, "regex-boundary");
|
|
218
283
|
} else if (bp.op === "le") {
|
|
219
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at
|
|
220
|
-
add(bp.valuePx + 1, `above-${bp.valuePx}`, `${bp.raw} boundary-above
|
|
284
|
+
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
285
|
+
add(bp.valuePx + 1, `above-${bp.valuePx}`, `${bp.raw} boundary-above`, "regex-boundary");
|
|
221
286
|
} else {
|
|
222
|
-
add(bp.valuePx - 1, `below-${bp.valuePx}`, `${bp.raw} boundary-below
|
|
223
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at
|
|
287
|
+
add(bp.valuePx - 1, `below-${bp.valuePx}`, `${bp.raw} boundary-below`, "regex-boundary");
|
|
288
|
+
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
224
289
|
}
|
|
225
290
|
}
|
|
226
291
|
|
|
@@ -243,7 +308,7 @@ export function generateViewports(
|
|
|
243
308
|
if (hi - lo < 2) continue;
|
|
244
309
|
for (let j = 0; j < randomSamples; j++) {
|
|
245
310
|
const w = Math.round(lo + rand() * (hi - lo));
|
|
246
|
-
add(w, `sample-${w}`, `random sample in [${lo}, ${hi}]
|
|
311
|
+
add(w, `sample-${w}`, `random sample in [${lo}, ${hi}]`, "regex-sample");
|
|
247
312
|
}
|
|
248
313
|
}
|
|
249
314
|
}
|
|
@@ -263,5 +328,128 @@ export function discoverViewports(
|
|
|
263
328
|
const breakpoints = extractBreakpointsFromHtml(html);
|
|
264
329
|
const responsiveBreakpoints = toResponsiveBreakpoints(breakpoints);
|
|
265
330
|
const viewports = generateViewports(responsiveBreakpoints, options);
|
|
266
|
-
return { breakpoints, responsiveBreakpoints, viewports };
|
|
331
|
+
return { breakpoints, responsiveBreakpoints, viewports, backend: "regex" };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// ---- Crater viewport intelligence ----
|
|
335
|
+
|
|
336
|
+
export interface CraterViewportSource {
|
|
337
|
+
getRequiredTestViewports(): Promise<{ viewports: Array<{ width: number; reason: string }> }>;
|
|
338
|
+
getCssRuleViewportMap?(viewportWidths?: number[]): Promise<{
|
|
339
|
+
rules: Array<{ activeAtWidths?: number[]; inactiveAtWidths?: number[] }>;
|
|
340
|
+
}>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function clampViewport(width: number): boolean {
|
|
344
|
+
return Number.isFinite(width) && width >= 320 && width <= 2560;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function buildCraterViewport(
|
|
348
|
+
width: number,
|
|
349
|
+
height: number,
|
|
350
|
+
reason: string,
|
|
351
|
+
source: ViewportSource,
|
|
352
|
+
): ViewportSpec | null {
|
|
353
|
+
if (!clampViewport(width)) return null;
|
|
354
|
+
return {
|
|
355
|
+
width,
|
|
356
|
+
height,
|
|
357
|
+
label: `at-${width}`,
|
|
358
|
+
reason,
|
|
359
|
+
source,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Drive Crater v0.18.0 viewport intelligence (`getRequiredTestViewports`
|
|
365
|
+
* + optional `getCssRuleViewportMap`) to seed the viewport list with
|
|
366
|
+
* breakpoints the renderer itself determined are load-bearing for the
|
|
367
|
+
* current document. Falls back gracefully when either RPC fails.
|
|
368
|
+
*/
|
|
369
|
+
export async function discoverViewportsViaCrater(
|
|
370
|
+
client: CraterViewportSource,
|
|
371
|
+
options: ViewportOptions = {},
|
|
372
|
+
): Promise<{ viewports: ViewportSpec[]; widthsSeen: Set<number> }> {
|
|
373
|
+
const height = options.height ?? 900;
|
|
374
|
+
const widthsSeen = new Set<number>();
|
|
375
|
+
const viewports: ViewportSpec[] = [];
|
|
376
|
+
|
|
377
|
+
function pushIfNew(spec: ViewportSpec | null) {
|
|
378
|
+
if (!spec) return;
|
|
379
|
+
if (widthsSeen.has(spec.width)) return;
|
|
380
|
+
widthsSeen.add(spec.width);
|
|
381
|
+
viewports.push(spec);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
try {
|
|
385
|
+
const required = await client.getRequiredTestViewports();
|
|
386
|
+
for (const v of required.viewports ?? []) {
|
|
387
|
+
pushIfNew(buildCraterViewport(v.width, height, v.reason || "crater required", "crater-required"));
|
|
388
|
+
}
|
|
389
|
+
} catch {
|
|
390
|
+
// ignored — the regex fallback will still run from discoverViewportsWithBackend.
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (client.getCssRuleViewportMap) {
|
|
394
|
+
try {
|
|
395
|
+
const map = await client.getCssRuleViewportMap();
|
|
396
|
+
const extraWidths = new Set<number>();
|
|
397
|
+
for (const rule of map.rules ?? []) {
|
|
398
|
+
for (const w of rule.activeAtWidths ?? []) extraWidths.add(w);
|
|
399
|
+
for (const w of rule.inactiveAtWidths ?? []) extraWidths.add(w);
|
|
400
|
+
}
|
|
401
|
+
for (const width of extraWidths) {
|
|
402
|
+
pushIfNew(buildCraterViewport(width, height, "crater rule/viewport map", "crater-rule-map"));
|
|
403
|
+
}
|
|
404
|
+
} catch {
|
|
405
|
+
// ignored
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return { viewports, widthsSeen };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Hybrid viewport discovery: prefer Crater v0.18.0 viewport intelligence
|
|
414
|
+
* when available, fall back to regex breakpoints for any widths Crater
|
|
415
|
+
* didn't surface, and tag each viewport with its `source`. The returned
|
|
416
|
+
* `backend` field tells downstream reports which path supplied the list.
|
|
417
|
+
*/
|
|
418
|
+
export async function discoverViewportsWithBackend(
|
|
419
|
+
html: string,
|
|
420
|
+
options: ViewportOptions & { craterClient?: CraterViewportSource | null } = {},
|
|
421
|
+
): Promise<DiscoveryResult> {
|
|
422
|
+
const breakpoints = extractBreakpointsFromHtml(html);
|
|
423
|
+
const responsiveBreakpoints = toResponsiveBreakpoints(breakpoints);
|
|
424
|
+
const regexViewports = generateViewports(responsiveBreakpoints, options);
|
|
425
|
+
|
|
426
|
+
const client = options.craterClient ?? null;
|
|
427
|
+
if (!client) {
|
|
428
|
+
return { breakpoints, responsiveBreakpoints, viewports: regexViewports, backend: "regex" };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const craterResult = await discoverViewportsViaCrater(client, options);
|
|
432
|
+
if (craterResult.viewports.length === 0) {
|
|
433
|
+
return { breakpoints, responsiveBreakpoints, viewports: regexViewports, backend: "regex" };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Fold regex output in as fallback, keeping anything Crater didn't already cover.
|
|
437
|
+
const merged: ViewportSpec[] = [...craterResult.viewports];
|
|
438
|
+
let usedFallback = false;
|
|
439
|
+
for (const v of regexViewports) {
|
|
440
|
+
if (craterResult.widthsSeen.has(v.width)) continue;
|
|
441
|
+
merged.push(v);
|
|
442
|
+
if (v.source !== "standard") usedFallback = true;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const maxViewports = options.maxViewports ?? 20;
|
|
446
|
+
merged.sort((a, b) => a.width - b.width);
|
|
447
|
+
const limited = merged.slice(0, maxViewports);
|
|
448
|
+
|
|
449
|
+
return {
|
|
450
|
+
breakpoints,
|
|
451
|
+
responsiveBreakpoints,
|
|
452
|
+
viewports: limited,
|
|
453
|
+
backend: usedFallback ? "hybrid" : "crater",
|
|
454
|
+
};
|
|
267
455
|
}
|