@mizchi/vlmkit-capture 0.8.0 → 0.9.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 +1 -1
- package/dist/batch-prescan.d.mts +51 -0
- package/dist/batch-prescan.mjs +66 -0
- package/dist/capture-config.d.mts +50 -0
- package/dist/capture-config.mjs +164 -0
- package/dist/capturer.d.mts +45 -0
- package/dist/capturer.mjs +94 -0
- package/dist/cloudflare-quick-actions.d.mts +114 -0
- package/dist/cloudflare-quick-actions.mjs +142 -0
- package/dist/crater-client.d.mts +159 -0
- package/dist/crater-client.mjs +362 -0
- package/dist/crater-smoke.d.mts +98 -0
- package/dist/crater-smoke.mjs +269 -0
- package/dist/crater-wasm.d.mts +65 -0
- package/dist/crater-wasm.mjs +132 -0
- package/{src/detection-types.ts → dist/detection-types.d.mts} +4 -2
- package/dist/detection-types.mjs +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +11 -0
- package/dist/playwright-analyzer.d.mts +38 -0
- package/dist/playwright-analyzer.mjs +117 -0
- package/dist/playwright-launch-error.d.mts +7 -0
- package/dist/playwright-launch-error.mjs +25 -0
- package/dist/prescanner.d.mts +39 -0
- package/dist/prescanner.mjs +61 -0
- package/dist/viewport-discovery.d.mts +114 -0
- package/dist/viewport-discovery.mjs +294 -0
- package/package.json +18 -6
- package/src/batch-prescan.ts +0 -115
- package/src/capture-config.ts +0 -219
- package/src/capturer.ts +0 -138
- package/src/cloudflare-quick-actions.ts +0 -304
- package/src/crater-client.ts +0 -537
- package/src/crater-smoke.ts +0 -336
- package/src/crater-wasm.ts +0 -260
- package/src/index.ts +0 -157
- package/src/playwright-analyzer.ts +0 -180
- package/src/playwright-launch-error.ts +0 -41
- package/src/prescanner.ts +0 -123
- package/src/viewport-discovery.ts +0 -455
|
@@ -1,304 +0,0 @@
|
|
|
1
|
-
export type CloudflareQuickAction = "screenshot" | "crawl";
|
|
2
|
-
|
|
3
|
-
export interface CloudflareQuickActionsConfig {
|
|
4
|
-
accountId: string;
|
|
5
|
-
apiToken: string;
|
|
6
|
-
apiBase?: string;
|
|
7
|
-
fetch?: typeof fetch;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface CloudflareQuickActionsEnv {
|
|
11
|
-
CLOUDFLARE_ACCOUNT_ID?: string;
|
|
12
|
-
CLOUDFLARE_API_TOKEN?: string;
|
|
13
|
-
CLOUDFLARE_BROWSER_RENDERING_API_BASE?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface CloudflareQuickActionEndpointInput {
|
|
17
|
-
accountId: string;
|
|
18
|
-
action: CloudflareQuickAction;
|
|
19
|
-
jobId?: string;
|
|
20
|
-
apiBase?: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CloudflareScreenshotRequest {
|
|
24
|
-
url?: string;
|
|
25
|
-
html?: string;
|
|
26
|
-
viewport?: {
|
|
27
|
-
width: number;
|
|
28
|
-
height: number;
|
|
29
|
-
deviceScaleFactor?: number;
|
|
30
|
-
};
|
|
31
|
-
screenshotOptions?: {
|
|
32
|
-
fullPage?: boolean;
|
|
33
|
-
omitBackground?: boolean;
|
|
34
|
-
type?: "png" | "jpeg" | "webp";
|
|
35
|
-
quality?: number;
|
|
36
|
-
clip?: {
|
|
37
|
-
x: number;
|
|
38
|
-
y: number;
|
|
39
|
-
width: number;
|
|
40
|
-
height: number;
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
selector?: string;
|
|
44
|
-
gotoOptions?: Record<string, unknown>;
|
|
45
|
-
authenticate?: Record<string, unknown>;
|
|
46
|
-
cookies?: unknown[];
|
|
47
|
-
setExtraHTTPHeaders?: Record<string, string>;
|
|
48
|
-
addScriptTag?: Array<Record<string, unknown>>;
|
|
49
|
-
addStyleTag?: Array<Record<string, unknown>>;
|
|
50
|
-
userAgent?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface CloudflareScreenshotResult {
|
|
54
|
-
bytes: ArrayBuffer;
|
|
55
|
-
contentType: string;
|
|
56
|
-
browserMsUsed?: number;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface CloudflareCrawlRequest {
|
|
60
|
-
url: string;
|
|
61
|
-
limit?: number;
|
|
62
|
-
depth?: number;
|
|
63
|
-
formats?: Array<"html" | "markdown" | "json">;
|
|
64
|
-
render?: boolean;
|
|
65
|
-
maxAge?: number;
|
|
66
|
-
options?: {
|
|
67
|
-
includePatterns?: string[];
|
|
68
|
-
excludePatterns?: string[];
|
|
69
|
-
includeExternalLinks?: boolean;
|
|
70
|
-
includeSubdomains?: boolean;
|
|
71
|
-
};
|
|
72
|
-
gotoOptions?: Record<string, unknown>;
|
|
73
|
-
authenticate?: Record<string, unknown>;
|
|
74
|
-
cookies?: unknown[];
|
|
75
|
-
setExtraHTTPHeaders?: Record<string, string>;
|
|
76
|
-
rejectResourceTypes?: string[];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export interface CloudflareCrawlStartResult {
|
|
80
|
-
jobId: string;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type CloudflareCrawlRecordStatus =
|
|
84
|
-
| "queued"
|
|
85
|
-
| "errored"
|
|
86
|
-
| "completed"
|
|
87
|
-
| "disallowed"
|
|
88
|
-
| "skipped"
|
|
89
|
-
| "cancelled";
|
|
90
|
-
|
|
91
|
-
export interface CloudflareCrawlRecord {
|
|
92
|
-
url: string;
|
|
93
|
-
status: CloudflareCrawlRecordStatus;
|
|
94
|
-
metadata?: {
|
|
95
|
-
status?: number;
|
|
96
|
-
url?: string;
|
|
97
|
-
title?: string;
|
|
98
|
-
};
|
|
99
|
-
html?: string;
|
|
100
|
-
markdown?: string;
|
|
101
|
-
json?: unknown;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface CloudflareCrawlResult {
|
|
105
|
-
id: string;
|
|
106
|
-
status: string;
|
|
107
|
-
browserSecondsUsed?: number;
|
|
108
|
-
total?: number;
|
|
109
|
-
finished?: number;
|
|
110
|
-
records: CloudflareCrawlRecord[];
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export interface CloudflareCrawlRoute {
|
|
114
|
-
url: string;
|
|
115
|
-
path: string;
|
|
116
|
-
title?: string;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface ExtractCloudflareCrawlRoutesOptions {
|
|
120
|
-
baseUrl?: string;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function buildCloudflareQuickActionEndpoint(input: CloudflareQuickActionEndpointInput): string {
|
|
124
|
-
const apiBase = (input.apiBase ?? "https://api.cloudflare.com/client/v4").replace(/\/$/u, "");
|
|
125
|
-
const account = encodeURIComponent(input.accountId);
|
|
126
|
-
const action = input.action;
|
|
127
|
-
const base = `${apiBase}/accounts/${account}/browser-rendering/${action}`;
|
|
128
|
-
return input.jobId ? `${base}/${encodeURIComponent(input.jobId)}` : base;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function resolveCloudflareQuickActionsConfig(
|
|
132
|
-
env: CloudflareQuickActionsEnv = process.env,
|
|
133
|
-
): CloudflareQuickActionsConfig {
|
|
134
|
-
const accountId = env.CLOUDFLARE_ACCOUNT_ID?.trim();
|
|
135
|
-
if (!accountId) {
|
|
136
|
-
throw new Error("CLOUDFLARE_ACCOUNT_ID is required for Cloudflare Quick Actions");
|
|
137
|
-
}
|
|
138
|
-
const apiToken = env.CLOUDFLARE_API_TOKEN?.trim();
|
|
139
|
-
if (!apiToken) {
|
|
140
|
-
throw new Error("CLOUDFLARE_API_TOKEN is required for Cloudflare Quick Actions");
|
|
141
|
-
}
|
|
142
|
-
return {
|
|
143
|
-
accountId,
|
|
144
|
-
apiToken,
|
|
145
|
-
apiBase: env.CLOUDFLARE_BROWSER_RENDERING_API_BASE?.trim() || undefined,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export function createCloudflareQuickActionsClient(config: CloudflareQuickActionsConfig) {
|
|
150
|
-
const fetchImpl = config.fetch ?? fetch;
|
|
151
|
-
const headers = {
|
|
152
|
-
Authorization: `Bearer ${config.apiToken}`,
|
|
153
|
-
"Content-Type": "application/json",
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
async function post(action: CloudflareQuickAction, body: unknown): Promise<Response> {
|
|
157
|
-
const response = await fetchImpl(buildCloudflareQuickActionEndpoint({
|
|
158
|
-
accountId: config.accountId,
|
|
159
|
-
action,
|
|
160
|
-
apiBase: config.apiBase,
|
|
161
|
-
}), {
|
|
162
|
-
method: "POST",
|
|
163
|
-
headers,
|
|
164
|
-
body: JSON.stringify(body),
|
|
165
|
-
});
|
|
166
|
-
if (!response.ok) {
|
|
167
|
-
throw new Error(`Cloudflare ${action} failed: ${response.status} ${await response.text()}`);
|
|
168
|
-
}
|
|
169
|
-
return response;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return {
|
|
173
|
-
async screenshot(input: CloudflareScreenshotRequest): Promise<CloudflareScreenshotResult> {
|
|
174
|
-
if (!input.url && !input.html) {
|
|
175
|
-
throw new Error("Cloudflare screenshot requires url or html");
|
|
176
|
-
}
|
|
177
|
-
const response = await post("screenshot", input);
|
|
178
|
-
const browserMsHeader = response.headers.get("x-browser-ms-used");
|
|
179
|
-
return {
|
|
180
|
-
bytes: await response.arrayBuffer(),
|
|
181
|
-
contentType: response.headers.get("content-type") ?? "image/png",
|
|
182
|
-
browserMsUsed: browserMsHeader ? Number(browserMsHeader) : undefined,
|
|
183
|
-
};
|
|
184
|
-
},
|
|
185
|
-
|
|
186
|
-
async startCrawl(input: CloudflareCrawlRequest): Promise<CloudflareCrawlStartResult> {
|
|
187
|
-
const response = await post("crawl", input);
|
|
188
|
-
const parsed = await response.json() as unknown;
|
|
189
|
-
return {
|
|
190
|
-
jobId: normalizeCrawlJobId(parsed),
|
|
191
|
-
};
|
|
192
|
-
},
|
|
193
|
-
|
|
194
|
-
async getCrawlResult(jobId: string): Promise<CloudflareCrawlResult> {
|
|
195
|
-
const response = await fetchImpl(buildCloudflareQuickActionEndpoint({
|
|
196
|
-
accountId: config.accountId,
|
|
197
|
-
action: "crawl",
|
|
198
|
-
jobId,
|
|
199
|
-
apiBase: config.apiBase,
|
|
200
|
-
}), {
|
|
201
|
-
method: "GET",
|
|
202
|
-
headers: { Authorization: `Bearer ${config.apiToken}` },
|
|
203
|
-
});
|
|
204
|
-
if (!response.ok) {
|
|
205
|
-
throw new Error(`Cloudflare crawl result failed: ${response.status} ${await response.text()}`);
|
|
206
|
-
}
|
|
207
|
-
const parsed = await response.json() as unknown;
|
|
208
|
-
return normalizeCrawlResult(parsed);
|
|
209
|
-
},
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export function extractCloudflareCrawlRoutes(
|
|
214
|
-
result: CloudflareCrawlResult,
|
|
215
|
-
options: ExtractCloudflareCrawlRoutesOptions = {},
|
|
216
|
-
): CloudflareCrawlRoute[] {
|
|
217
|
-
const base = options.baseUrl ? new URL(options.baseUrl) : undefined;
|
|
218
|
-
const seen = new Set<string>();
|
|
219
|
-
const routes: CloudflareCrawlRoute[] = [];
|
|
220
|
-
for (const record of result.records) {
|
|
221
|
-
if (record.status !== "completed") continue;
|
|
222
|
-
const rawUrl = record.metadata?.url ?? record.url;
|
|
223
|
-
let parsed: URL;
|
|
224
|
-
try {
|
|
225
|
-
parsed = new URL(rawUrl);
|
|
226
|
-
} catch {
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
if (base && parsed.origin !== base.origin) continue;
|
|
230
|
-
const path = parsed.pathname + parsed.search;
|
|
231
|
-
if (seen.has(path)) continue;
|
|
232
|
-
seen.add(path);
|
|
233
|
-
const route: CloudflareCrawlRoute = {
|
|
234
|
-
url: rawUrl,
|
|
235
|
-
path,
|
|
236
|
-
};
|
|
237
|
-
if (record.metadata?.title) route.title = record.metadata.title;
|
|
238
|
-
routes.push(route);
|
|
239
|
-
}
|
|
240
|
-
return routes;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function normalizeCrawlJobId(parsed: unknown): string {
|
|
244
|
-
if (typeof parsed === "string") return parsed;
|
|
245
|
-
if (isRecord(parsed)) {
|
|
246
|
-
if (typeof parsed.result === "string") return parsed.result;
|
|
247
|
-
if (isRecord(parsed.result) && typeof parsed.result.id === "string") return parsed.result.id;
|
|
248
|
-
if (typeof parsed.id === "string") return parsed.id;
|
|
249
|
-
}
|
|
250
|
-
throw new Error("Cloudflare crawl response did not include a job id");
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function normalizeCrawlResult(parsed: unknown): CloudflareCrawlResult {
|
|
254
|
-
const value = isRecord(parsed) && isRecord(parsed.result) ? parsed.result : parsed;
|
|
255
|
-
if (!isRecord(value)) {
|
|
256
|
-
throw new Error("Cloudflare crawl result must be an object");
|
|
257
|
-
}
|
|
258
|
-
return {
|
|
259
|
-
id: readString(value, "id"),
|
|
260
|
-
status: readString(value, "status"),
|
|
261
|
-
browserSecondsUsed: readNumber(value, "browserSecondsUsed"),
|
|
262
|
-
total: readNumber(value, "total"),
|
|
263
|
-
finished: readNumber(value, "finished"),
|
|
264
|
-
records: Array.isArray(value.records)
|
|
265
|
-
? value.records.filter(isRecord).map(normalizeCrawlRecord)
|
|
266
|
-
: [],
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
function normalizeCrawlRecord(value: Record<string, unknown>): CloudflareCrawlRecord {
|
|
271
|
-
return {
|
|
272
|
-
url: readString(value, "url"),
|
|
273
|
-
status: readString(value, "status") as CloudflareCrawlRecordStatus,
|
|
274
|
-
metadata: isRecord(value.metadata)
|
|
275
|
-
? {
|
|
276
|
-
status: readNumber(value.metadata, "status"),
|
|
277
|
-
url: readOptionalString(value.metadata, "url"),
|
|
278
|
-
title: readOptionalString(value.metadata, "title"),
|
|
279
|
-
}
|
|
280
|
-
: undefined,
|
|
281
|
-
html: readOptionalString(value, "html"),
|
|
282
|
-
markdown: readOptionalString(value, "markdown"),
|
|
283
|
-
json: value.json,
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
288
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function readString(record: Record<string, unknown>, key: string): string {
|
|
292
|
-
const value = record[key];
|
|
293
|
-
return typeof value === "string" ? value : "";
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function readOptionalString(record: Record<string, unknown>, key: string): string | undefined {
|
|
297
|
-
const value = record[key];
|
|
298
|
-
return typeof value === "string" ? value : undefined;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function readNumber(record: Record<string, unknown>, key: string): number | undefined {
|
|
302
|
-
const value = record[key];
|
|
303
|
-
return typeof value === "number" ? value : undefined;
|
|
304
|
-
}
|