@docsxai/engine 0.2.1-rc.1 → 0.3.0-rc.1
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 +18 -5
- package/dist/address-class.d.ts +14 -0
- package/dist/address-class.js +151 -0
- package/dist/auth/api-login.d.ts +3 -3
- package/dist/auth/email-otp.d.ts +60 -60
- package/dist/auth/jwt-injection.d.ts +45 -45
- package/dist/auth/pat-header.d.ts +2 -2
- package/dist/auth/types.d.ts +2 -2
- package/dist/auth/ui-form.d.ts +54 -54
- package/dist/auth/webauthn.d.ts +22 -22
- package/dist/backend-client-contracts.d.ts +4 -2
- package/dist/calibrate.d.ts +2 -2
- package/dist/calibrate.js +5 -0
- package/dist/cli-commands-authoring.js +23 -4
- package/dist/cli-commands-backend.js +2 -2
- package/dist/cli-commands-docpack.d.ts +5 -0
- package/dist/cli-commands-docpack.js +70 -24
- package/dist/cli-commands-pack.d.ts +2 -0
- package/dist/cli-commands-pack.js +69 -0
- package/dist/cli-commands-session.js +92 -89
- package/dist/cli-retired.d.ts +6 -0
- package/dist/cli-retired.js +12 -0
- package/dist/cli-usage.d.ts +1 -1
- package/dist/cli-usage.js +52 -4
- package/dist/cli-verify.d.ts +14 -0
- package/dist/cli-verify.js +54 -0
- package/dist/cli.js +14 -1
- package/dist/deprecation.d.ts +19 -0
- package/dist/deprecation.js +32 -0
- package/dist/diagnose.d.ts +25 -0
- package/dist/diagnose.js +69 -3
- package/dist/diff-compute.js +1 -0
- package/dist/doc-pack-io.d.ts +14 -0
- package/dist/doc-pack-io.js +121 -14
- package/dist/doc-pack.d.ts +2282 -387
- package/dist/doc-pack.js +135 -43
- package/dist/doctor-checks.d.ts +1 -1
- package/dist/doctor-checks.js +25 -1
- package/dist/driver-budget.d.ts +6 -0
- package/dist/driver-budget.js +15 -0
- package/dist/egress-guard.d.ts +58 -0
- package/dist/egress-guard.js +136 -0
- package/dist/environment-spec.d.ts +67 -0
- package/dist/environment-spec.js +38 -0
- package/dist/export/playwright-test.js +42 -10
- package/dist/flow-file.d.ts +9 -3
- package/dist/flow-file.js +21 -1
- package/dist/flow-halt.d.ts +16 -0
- package/dist/flow-halt.js +76 -0
- package/dist/flow-hidden.d.ts +9 -0
- package/dist/flow-hidden.js +20 -0
- package/dist/flow-lint.js +91 -1
- package/dist/flow-matrix-lint.d.ts +10 -0
- package/dist/flow-matrix-lint.js +40 -0
- package/dist/flow-matrix.d.ts +44 -0
- package/dist/flow-matrix.js +238 -0
- package/dist/flow-name-rules.d.ts +15 -0
- package/dist/flow-name-rules.js +39 -0
- package/dist/flow-runtime.d.ts +52 -22
- package/dist/flow-runtime.js +63 -117
- package/dist/flow-success.d.ts +3 -0
- package/dist/flow-success.js +37 -0
- package/dist/flow-tree.d.ts +2 -0
- package/dist/flow-tree.js +17 -5
- package/dist/flow-wait.d.ts +3 -0
- package/dist/flow-wait.js +22 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/matrix-spec.d.ts +145 -0
- package/dist/matrix-spec.js +120 -0
- package/dist/page-hide.d.ts +10 -0
- package/dist/page-hide.js +45 -0
- package/dist/page-settle.d.ts +39 -0
- package/dist/page-settle.js +142 -0
- package/dist/playwright-driver.d.ts +28 -8
- package/dist/playwright-driver.js +73 -28
- package/dist/plugins/manifest.d.ts +9 -9
- package/dist/run-flows.d.ts +40 -0
- package/dist/run-flows.js +129 -0
- package/dist/style.d.ts +2 -2
- package/dist/upload-path.d.ts +15 -0
- package/dist/upload-path.js +63 -0
- package/dist/verify-compare.d.ts +43 -0
- package/dist/verify-compare.js +237 -0
- package/dist/verify-determinism.d.ts +16 -0
- package/dist/verify-determinism.js +88 -0
- package/dist/verify-report.d.ts +53 -0
- package/dist/verify-report.js +141 -0
- package/dist/verify-tree.d.ts +27 -0
- package/dist/verify-tree.js +95 -0
- package/dist/viewer-bin.d.ts +6 -0
- package/dist/viewer-bin.js +24 -0
- package/dist/zip.d.ts +2 -2
- package/package.json +3 -2
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Flow matrix schema: the `matrix:` block, the `only:` / `skip:` variant selectors, and the
|
|
2
|
+
// `variant` record `annotations.json` carries. A leaf module (imports zod and the environment
|
|
3
|
+
// schema); `doc-pack.ts` re-exports it. Expansion itself lives in `flow-matrix.ts`.
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { ColorScheme, LocaleTag, VIEWPORT_PRESETS, ViewportPreset, } from "./environment-spec.js";
|
|
6
|
+
/** Most variants one flow may expand to. A flow over the cap is rejected at parse time. */
|
|
7
|
+
export const MAX_MATRIX_VARIANTS = 64;
|
|
8
|
+
/** A viewport's matrix name: also a directory segment, so no dots, slashes or capitals. */
|
|
9
|
+
const ViewportName = z
|
|
10
|
+
.string()
|
|
11
|
+
.regex(/^[a-z0-9][a-z0-9-]*$/, "must be lowercase letters, digits and dashes (e.g. mobile-390)");
|
|
12
|
+
/** A matrix viewport: a preset name, or a size with an optional name (default `<width>x<height>`). */
|
|
13
|
+
export const MatrixViewport = z.union([
|
|
14
|
+
ViewportPreset,
|
|
15
|
+
z
|
|
16
|
+
.object({
|
|
17
|
+
name: ViewportName.optional(),
|
|
18
|
+
width: z.number().int().positive(),
|
|
19
|
+
height: z.number().int().positive(),
|
|
20
|
+
})
|
|
21
|
+
.strict(),
|
|
22
|
+
]);
|
|
23
|
+
export function resolveMatrixViewport(entry) {
|
|
24
|
+
if (typeof entry === "string")
|
|
25
|
+
return { name: entry, ...VIEWPORT_PRESETS[entry] };
|
|
26
|
+
return {
|
|
27
|
+
name: entry.name ?? `${entry.width}x${entry.height}`,
|
|
28
|
+
width: entry.width,
|
|
29
|
+
height: entry.height,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const axisList = (item) => z.array(item).min(1).max(MAX_MATRIX_VARIANTS);
|
|
33
|
+
function duplicates(values) {
|
|
34
|
+
const seen = new Set();
|
|
35
|
+
const dupes = new Set();
|
|
36
|
+
for (const v of values) {
|
|
37
|
+
if (seen.has(v))
|
|
38
|
+
dupes.add(v);
|
|
39
|
+
seen.add(v);
|
|
40
|
+
}
|
|
41
|
+
return [...dupes];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The `matrix:` block of a flow-file. Each axis is a bounded list; the flow expands to the product
|
|
45
|
+
* of the axes it names. Locale and viewport entries must be unique (locales ignoring case,
|
|
46
|
+
* viewports by name), and the product may not pass {@link MAX_MATRIX_VARIANTS}.
|
|
47
|
+
*/
|
|
48
|
+
export const MatrixSpec = z
|
|
49
|
+
.object({
|
|
50
|
+
locales: axisList(LocaleTag).optional(),
|
|
51
|
+
color_schemes: axisList(ColorScheme).optional(),
|
|
52
|
+
viewports: axisList(MatrixViewport).optional(),
|
|
53
|
+
})
|
|
54
|
+
.strict()
|
|
55
|
+
.superRefine((m, ctx) => {
|
|
56
|
+
const sizes = [m.locales, m.color_schemes, m.viewports].map((l) => l?.length);
|
|
57
|
+
if (sizes.every((n) => n === undefined)) {
|
|
58
|
+
ctx.addIssue({
|
|
59
|
+
code: "custom",
|
|
60
|
+
message: "`matrix` needs at least one of `locales`, `color_schemes`, `viewports`",
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const lists = [
|
|
65
|
+
["locales", m.locales?.map((l) => l.toLowerCase())],
|
|
66
|
+
["color_schemes", m.color_schemes],
|
|
67
|
+
["viewports", m.viewports?.map((v) => resolveMatrixViewport(v).name)],
|
|
68
|
+
];
|
|
69
|
+
for (const [key, values] of lists) {
|
|
70
|
+
const dupes = values ? duplicates(values) : [];
|
|
71
|
+
if (dupes.length) {
|
|
72
|
+
ctx.addIssue({
|
|
73
|
+
code: "custom",
|
|
74
|
+
path: [key],
|
|
75
|
+
message: `duplicate entries: ${dupes.map((d) => `"${d}"`).join(", ")}`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const product = sizes.reduce((n, s) => n * (s ?? 1), 1);
|
|
80
|
+
if (product > MAX_MATRIX_VARIANTS) {
|
|
81
|
+
ctx.addIssue({
|
|
82
|
+
code: "custom",
|
|
83
|
+
message: `matrix expands to ${product} variants (${sizes
|
|
84
|
+
.map((s, i) => `${["locales", "color_schemes", "viewports"][i]}: ${s ?? 0}`)
|
|
85
|
+
.join(", ")}); the limit is ${MAX_MATRIX_VARIANTS}`,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* Which variants a step or annotation applies to (`only`) or is dropped from (`skip`). A clause
|
|
91
|
+
* names axes; it matches a variant when every axis it names matches (any value in the list).
|
|
92
|
+
* `viewport` values are matrix viewport names, `locale` values match exactly or by language
|
|
93
|
+
* (`es` matches `es-ES`).
|
|
94
|
+
*/
|
|
95
|
+
export const VariantSelector = z
|
|
96
|
+
.object({
|
|
97
|
+
viewport: z.array(ViewportName).min(1).optional(),
|
|
98
|
+
color_scheme: z.array(ColorScheme).min(1).optional(),
|
|
99
|
+
locale: z.array(LocaleTag).min(1).optional(),
|
|
100
|
+
})
|
|
101
|
+
.strict()
|
|
102
|
+
.refine((s) => Object.keys(s).length > 0, {
|
|
103
|
+
message: "name at least one of `viewport`, `color_scheme`, `locale`",
|
|
104
|
+
});
|
|
105
|
+
/** The variant a run executed, recorded as `variant` in `annotations.json`. */
|
|
106
|
+
export const VariantInfo = z
|
|
107
|
+
.object({
|
|
108
|
+
id: z.string().min(1),
|
|
109
|
+
locale: LocaleTag.optional(),
|
|
110
|
+
color_scheme: ColorScheme.optional(),
|
|
111
|
+
viewport: z
|
|
112
|
+
.object({
|
|
113
|
+
name: z.string().min(1),
|
|
114
|
+
width: z.number().int().positive(),
|
|
115
|
+
height: z.number().int().positive(),
|
|
116
|
+
})
|
|
117
|
+
.strict()
|
|
118
|
+
.optional(),
|
|
119
|
+
})
|
|
120
|
+
.strict();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Attribute that marks an element hidden by a `hide` step. The stylesheet rule keys on it. */
|
|
2
|
+
export declare const HIDDEN_ATTR = "data-docsxai-hidden";
|
|
3
|
+
/**
|
|
4
|
+
* Mark `els` hidden and make sure each one's root carries the rule that hides marked elements.
|
|
5
|
+
* Throws, before marking anything, when the page can't take a constructable stylesheet: leaving
|
|
6
|
+
* the element visible would put exactly what the step meant to remove into the screenshot.
|
|
7
|
+
*/
|
|
8
|
+
export declare function markHidden(els: unknown[], attr: string): void;
|
|
9
|
+
/** Remove the hidden mark from `els`; the stylesheet stays, and matches nothing. */
|
|
10
|
+
export declare function unmarkHidden(els: unknown[], attr: string): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// In-page helpers behind `BrowserDriver.hideElements` / `showElements`.
|
|
2
|
+
//
|
|
3
|
+
// Both functions run inside the page (Playwright serializes them with `locator.evaluateAll`), so
|
|
4
|
+
// they stay self-contained: no imports of values, no references to module scope. They are fixed
|
|
5
|
+
// engine code. The flow supplies only a selector, which Playwright resolves to elements outside
|
|
6
|
+
// the page; nothing from the flow reaches the page as CSS or script.
|
|
7
|
+
//
|
|
8
|
+
// Hiding is `visibility: hidden !important`, applied through a constructable stylesheet adopted
|
|
9
|
+
// onto the element's own root (the document, or the shadow root the element lives in) and keyed
|
|
10
|
+
// by a data attribute. Visibility keeps the element's box, so nothing around it reflows, and it
|
|
11
|
+
// inherits into a shadow tree, so hiding a host hides its contents too. Constructable stylesheets
|
|
12
|
+
// and attribute writes are not blocked by a page's Content-Security-Policy; a `<style>` element
|
|
13
|
+
// would be. `transition: none` on the element and its descendants keeps the hide instant: a page
|
|
14
|
+
// that gives every element a tiny `transition-duration` (the usual reduced-motion reset) would
|
|
15
|
+
// otherwise hold `visibility: visible` for one more frame, long enough for a check that runs right
|
|
16
|
+
// after the step to see the element. The attribute name is reserved (see the flow-file reference). The DOM lib isn't in this package's TypeScript config, so the shapes used are local.
|
|
17
|
+
/** Attribute that marks an element hidden by a `hide` step. The stylesheet rule keys on it. */
|
|
18
|
+
export const HIDDEN_ATTR = "data-docsxai-hidden";
|
|
19
|
+
/**
|
|
20
|
+
* Mark `els` hidden and make sure each one's root carries the rule that hides marked elements.
|
|
21
|
+
* Throws, before marking anything, when the page can't take a constructable stylesheet: leaving
|
|
22
|
+
* the element visible would put exactly what the step meant to remove into the screenshot.
|
|
23
|
+
*/
|
|
24
|
+
export function markHidden(els, attr) {
|
|
25
|
+
const sheetCtor = globalThis.CSSStyleSheet;
|
|
26
|
+
const roots = els.map((el) => el.getRootNode());
|
|
27
|
+
if (!sheetCtor || roots.some((r) => !r || !Array.isArray(r.adoptedStyleSheets))) {
|
|
28
|
+
throw new Error("docsxai: cannot hide: this browser has no constructable stylesheets (CSSStyleSheet / adoptedStyleSheets) for the element's document or shadow root");
|
|
29
|
+
}
|
|
30
|
+
for (const [i, el] of els.entries()) {
|
|
31
|
+
const root = roots[i];
|
|
32
|
+
el.setAttribute(attr, "");
|
|
33
|
+
const present = root.adoptedStyleSheets.some((s) => s.cssRules[0]?.cssText.startsWith(`[${attr}]`));
|
|
34
|
+
if (present)
|
|
35
|
+
continue;
|
|
36
|
+
const sheet = new sheetCtor();
|
|
37
|
+
sheet.replaceSync(`[${attr}] { visibility: hidden !important; } [${attr}], [${attr}] * { transition: none !important; }`);
|
|
38
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Remove the hidden mark from `els`; the stylesheet stays, and matches nothing. */
|
|
42
|
+
export function unmarkHidden(els, attr) {
|
|
43
|
+
for (const el of els)
|
|
44
|
+
el.removeAttribute(attr);
|
|
45
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Pause between two polls, in ms. */
|
|
2
|
+
export declare const SETTLE_POLL_INTERVAL_MS = 50;
|
|
3
|
+
/** Consecutive calm polls that count as settled. */
|
|
4
|
+
export declare const SETTLE_STABLE_POLLS = 2;
|
|
5
|
+
/** Most elements one poll reads boxes for. */
|
|
6
|
+
export declare const SETTLE_MAX_VISITED = 4000;
|
|
7
|
+
/** Budget when the step sets no `timeout_ms`, in ms. */
|
|
8
|
+
export declare const SETTLE_DEFAULT_TIMEOUT_MS = 10000;
|
|
9
|
+
export interface SettleArgs {
|
|
10
|
+
timeoutMs: number;
|
|
11
|
+
pollMs: number;
|
|
12
|
+
stablePolls: number;
|
|
13
|
+
maxVisited: number;
|
|
14
|
+
}
|
|
15
|
+
export interface SettleOutcome {
|
|
16
|
+
settled: boolean;
|
|
17
|
+
/** Polls run. Capped at `ceil(timeoutMs / pollMs)`. */
|
|
18
|
+
polls: number;
|
|
19
|
+
/** What was still unsettled at the last poll: `fonts`, `images` and `layout`. Empty when settled. */
|
|
20
|
+
pending: string[];
|
|
21
|
+
/** Visible images still loading at the last poll. */
|
|
22
|
+
pendingImages: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolves with the page's state once it settles or the budget is spent. Every clock and promise
|
|
26
|
+
* in here (`Date.now`, `setTimeout`, `document.fonts.ready`) belongs to the page and a page can
|
|
27
|
+
* stall or override them, so the deadline in this function is a courtesy. The bound that holds is
|
|
28
|
+
* the Node-side `budgetMs + 2000` guard in `runSettle`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function awaitSettled(a: SettleArgs): Promise<SettleOutcome>;
|
|
31
|
+
/** The message a driver rejects with when the budget runs out; the runtime's halt-cause hint keys on its start. */
|
|
32
|
+
export declare function settleFailure(timeoutMs: number, outcome: SettleOutcome): string;
|
|
33
|
+
/**
|
|
34
|
+
* Run the settle poll through `evaluate` (the driver's page-evaluate of {@link awaitSettled}) and
|
|
35
|
+
* reject with {@link settleFailure} when it does not settle. The in-page poll keeps its own
|
|
36
|
+
* deadline; the outer timer covers a page that never answers (a blocked main thread), where that
|
|
37
|
+
* deadline cannot fire.
|
|
38
|
+
*/
|
|
39
|
+
export declare function runSettle(evaluate: (args: SettleArgs) => Promise<SettleOutcome>, timeoutMs?: number): Promise<void>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// In-page poll behind `BrowserDriver.waitForSettled`.
|
|
2
|
+
//
|
|
3
|
+
// `awaitSettled` runs inside the page (Playwright serializes it with `page.evaluate`), so it must
|
|
4
|
+
// stay self-contained: no imports of values, no references to module scope. It is fixed engine
|
|
5
|
+
// code that takes four numbers and nothing else: no selector and no flow value reaches the page.
|
|
6
|
+
// The DOM lib isn't in this package's TypeScript config, so the few DOM shapes it touches are
|
|
7
|
+
// declared locally.
|
|
8
|
+
//
|
|
9
|
+
// Settled means all of these hold on {@link SETTLE_STABLE_POLLS} polls in a row: no web font is
|
|
10
|
+
// loading, every `<img>` inside the viewport has finished loading, and the boxes of the elements
|
|
11
|
+
// inside the viewport did not move between two animation frames. A poll is one animation frame
|
|
12
|
+
// plus a fixed pause, repeated until the budget runs out. The geometry read is a box walk over the
|
|
13
|
+
// light DOM and open shadow roots, capped at `maxVisited` elements so a huge page stays cheap.
|
|
14
|
+
/** Pause between two polls, in ms. */
|
|
15
|
+
export const SETTLE_POLL_INTERVAL_MS = 50;
|
|
16
|
+
/** Consecutive calm polls that count as settled. */
|
|
17
|
+
export const SETTLE_STABLE_POLLS = 2;
|
|
18
|
+
/** Most elements one poll reads boxes for. */
|
|
19
|
+
export const SETTLE_MAX_VISITED = 4000;
|
|
20
|
+
/** Budget when the step sets no `timeout_ms`, in ms. */
|
|
21
|
+
export const SETTLE_DEFAULT_TIMEOUT_MS = 10_000;
|
|
22
|
+
/**
|
|
23
|
+
* Resolves with the page's state once it settles or the budget is spent. Every clock and promise
|
|
24
|
+
* in here (`Date.now`, `setTimeout`, `document.fonts.ready`) belongs to the page and a page can
|
|
25
|
+
* stall or override them, so the deadline in this function is a courtesy. The bound that holds is
|
|
26
|
+
* the Node-side `budgetMs + 2000` guard in `runSettle`.
|
|
27
|
+
*/
|
|
28
|
+
export async function awaitSettled(a) {
|
|
29
|
+
const g = globalThis;
|
|
30
|
+
const deadline = Date.now() + a.timeoutMs;
|
|
31
|
+
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
32
|
+
// rAF is throttled in a hidden tab, so a timer backs the frame up and the poll still advances.
|
|
33
|
+
const frame = () => new Promise((resolve) => {
|
|
34
|
+
let done = false;
|
|
35
|
+
const finish = () => {
|
|
36
|
+
if (!done) {
|
|
37
|
+
done = true;
|
|
38
|
+
resolve();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
g.requestAnimationFrame(finish);
|
|
42
|
+
setTimeout(finish, 250);
|
|
43
|
+
});
|
|
44
|
+
/** Boxes of the viewport-visible elements as a flat number list, plus how many images still load. */
|
|
45
|
+
const read = () => {
|
|
46
|
+
const boxes = [];
|
|
47
|
+
let pendingImages = 0;
|
|
48
|
+
let visited = 0;
|
|
49
|
+
const stack = g.document.body ? [g.document.body] : [];
|
|
50
|
+
while (stack.length > 0 && visited < a.maxVisited) {
|
|
51
|
+
const el = stack.pop();
|
|
52
|
+
visited++;
|
|
53
|
+
for (let i = el.children.length - 1; i >= 0; i--)
|
|
54
|
+
stack.push(el.children[i]);
|
|
55
|
+
if (el.shadowRoot) {
|
|
56
|
+
for (let i = el.shadowRoot.children.length - 1; i >= 0; i--)
|
|
57
|
+
stack.push(el.shadowRoot.children[i]);
|
|
58
|
+
}
|
|
59
|
+
const r = el.getBoundingClientRect();
|
|
60
|
+
// display:none (and detached) elements report a zero box at the origin; they paint nothing.
|
|
61
|
+
if (r.width === 0 && r.height === 0 && r.left === 0 && r.top === 0)
|
|
62
|
+
continue;
|
|
63
|
+
if (r.bottom < 0 || r.right < 0 || r.top >= g.innerHeight || r.left >= g.innerWidth)
|
|
64
|
+
continue;
|
|
65
|
+
// An image still loading can have no size yet, so it counts from the position it sits at.
|
|
66
|
+
if (el.tagName === "IMG" && el.complete === false)
|
|
67
|
+
pendingImages++;
|
|
68
|
+
if (r.width > 0 && r.height > 0)
|
|
69
|
+
boxes.push(r.left, r.top, r.width, r.height);
|
|
70
|
+
}
|
|
71
|
+
return { boxes, pendingImages };
|
|
72
|
+
};
|
|
73
|
+
const sameBoxes = (x, y) => x.length === y.length && x.every((v, i) => Math.abs(v - y[i]) <= 0.5);
|
|
74
|
+
// Fonts the page asked for so far. Fonts requested later show up as `loading` on a later poll.
|
|
75
|
+
const fonts = g.document.fonts;
|
|
76
|
+
if (fonts)
|
|
77
|
+
await Promise.race([fonts.ready, pause(Math.max(0, deadline - Date.now()))]);
|
|
78
|
+
let previous = null;
|
|
79
|
+
let calm = 0;
|
|
80
|
+
let polls = 0;
|
|
81
|
+
let pending = ["layout"];
|
|
82
|
+
let pendingImages = 0;
|
|
83
|
+
const maxPolls = Math.max(1, Math.ceil(a.timeoutMs / a.pollMs));
|
|
84
|
+
while (polls < maxPolls && Date.now() < deadline) {
|
|
85
|
+
await frame();
|
|
86
|
+
polls++;
|
|
87
|
+
const now = read();
|
|
88
|
+
pendingImages = now.pendingImages;
|
|
89
|
+
pending = [];
|
|
90
|
+
if (fonts && fonts.status === "loading")
|
|
91
|
+
pending.push("fonts");
|
|
92
|
+
if (now.pendingImages > 0)
|
|
93
|
+
pending.push("images");
|
|
94
|
+
if (previous === null || !sameBoxes(previous, now.boxes))
|
|
95
|
+
pending.push("layout");
|
|
96
|
+
previous = now.boxes;
|
|
97
|
+
calm = pending.length === 0 ? calm + 1 : 0;
|
|
98
|
+
if (calm >= a.stablePolls)
|
|
99
|
+
return { settled: true, polls, pending: [], pendingImages: 0 };
|
|
100
|
+
await pause(a.pollMs);
|
|
101
|
+
}
|
|
102
|
+
return { settled: false, polls, pending, pendingImages };
|
|
103
|
+
}
|
|
104
|
+
/** The message a driver rejects with when the budget runs out; the runtime's halt-cause hint keys on its start. */
|
|
105
|
+
export function settleFailure(timeoutMs, outcome) {
|
|
106
|
+
const still = outcome.pending.map((p) => p === "fonts"
|
|
107
|
+
? "web fonts still loading"
|
|
108
|
+
: p === "images"
|
|
109
|
+
? `${outcome.pendingImages} visible image(s) still loading`
|
|
110
|
+
: "layout of the visible elements still changing");
|
|
111
|
+
const detail = still.length > 0 ? `: ${still.join("; ")}` : "";
|
|
112
|
+
return `settled: page did not settle within ${timeoutMs} ms after ${outcome.polls} poll(s)${detail}`;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Run the settle poll through `evaluate` (the driver's page-evaluate of {@link awaitSettled}) and
|
|
116
|
+
* reject with {@link settleFailure} when it does not settle. The in-page poll keeps its own
|
|
117
|
+
* deadline; the outer timer covers a page that never answers (a blocked main thread), where that
|
|
118
|
+
* deadline cannot fire.
|
|
119
|
+
*/
|
|
120
|
+
export async function runSettle(evaluate, timeoutMs) {
|
|
121
|
+
const budgetMs = timeoutMs ?? SETTLE_DEFAULT_TIMEOUT_MS;
|
|
122
|
+
let guard;
|
|
123
|
+
const unresponsive = new Promise((_, reject) => {
|
|
124
|
+
guard = setTimeout(() => reject(new Error(`settled: page did not settle within ${budgetMs} ms (the page stopped responding)`)), budgetMs + 2000);
|
|
125
|
+
});
|
|
126
|
+
try {
|
|
127
|
+
const outcome = await Promise.race([
|
|
128
|
+
evaluate({
|
|
129
|
+
timeoutMs: budgetMs,
|
|
130
|
+
pollMs: SETTLE_POLL_INTERVAL_MS,
|
|
131
|
+
stablePolls: SETTLE_STABLE_POLLS,
|
|
132
|
+
maxVisited: SETTLE_MAX_VISITED,
|
|
133
|
+
}),
|
|
134
|
+
unresponsive,
|
|
135
|
+
]);
|
|
136
|
+
if (!outcome.settled)
|
|
137
|
+
throw new Error(settleFailure(budgetMs, outcome));
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
clearTimeout(guard);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Browser, type BrowserContext, type Page } from "playwright-core";
|
|
2
2
|
import { type BoundingBox, type EnvironmentSpec, type ViewportSize } from "./doc-pack.js";
|
|
3
|
+
import { type EgressGuardOptions } from "./egress-guard.js";
|
|
3
4
|
import { type ActionableState, type BrowserDriver, type ResolvedRedaction } from "./flow-runtime.js";
|
|
4
5
|
import { type StorageState } from "./auth.js";
|
|
5
6
|
import { type NearbyBoxes } from "./obstacles.js";
|
|
@@ -38,6 +39,11 @@ export interface PlaywrightSessionOptions {
|
|
|
38
39
|
chromiumArgs?: string[];
|
|
39
40
|
/** Doc-pack root that screenshot paths are resolved against. */
|
|
40
41
|
docPackRoot?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Where screenshots are written, when that differs from `docPackRoot` (which keeps resolving
|
|
44
|
+
* `upload` sources). `run --verify-determinism` points it at an isolated per-run root. Default: `docPackRoot`.
|
|
45
|
+
*/
|
|
46
|
+
outputRoot?: string;
|
|
41
47
|
/** If set, attach to a running Chrome at this CDP endpoint (e.g. `http://localhost:9222`) instead of launching one. `close()` won't close it. */
|
|
42
48
|
connectOverCdp?: string;
|
|
43
49
|
/**
|
|
@@ -49,6 +55,8 @@ export interface PlaywrightSessionOptions {
|
|
|
49
55
|
environment?: EnvironmentSpec;
|
|
50
56
|
/** Extra `browser.newContext` options — see {@link SessionContextOptions}. Ignored with `connectOverCdp`. */
|
|
51
57
|
contextOptions?: SessionContextOptions;
|
|
58
|
+
/** Refuse requests to link-local and metadata addresses (see `egress-guard.ts`). Default: `DOCSX_EGRESS_GUARD` in the environment decides. */
|
|
59
|
+
egressGuard?: EgressGuardOptions;
|
|
52
60
|
}
|
|
53
61
|
/** Map an {@link EnvironmentSpec} to the Playwright context options it pins (clock excluded — that's a page-level install). */
|
|
54
62
|
export declare function environmentContextOptions(env: EnvironmentSpec): {
|
|
@@ -73,18 +81,30 @@ export declare function launchPlaywrightSession(opts?: PlaywrightSessionOptions)
|
|
|
73
81
|
export declare class PlaywrightDriver implements BrowserDriver {
|
|
74
82
|
private readonly page;
|
|
75
83
|
private readonly docPackRoot;
|
|
76
|
-
|
|
84
|
+
private readonly outputRoot;
|
|
85
|
+
constructor(page: Page, docPackRoot?: string, outputRoot?: string);
|
|
77
86
|
goto(url: string): Promise<void>;
|
|
78
|
-
click(selector: string): Promise<void>;
|
|
79
|
-
fill(selector: string, value: string): Promise<void>;
|
|
80
|
-
upload(selector: string, filePath: string): Promise<void>;
|
|
81
|
-
press(selector: string | null, key: string): Promise<void>;
|
|
82
|
-
hover(selector: string): Promise<void>;
|
|
83
|
-
selectOption(selector: string, value: string): Promise<void>;
|
|
84
|
-
setChecked(selector: string, checked: boolean): Promise<void>;
|
|
87
|
+
click(selector: string, timeoutMs?: number): Promise<void>;
|
|
88
|
+
fill(selector: string, value: string, timeoutMs?: number): Promise<void>;
|
|
89
|
+
upload(selector: string, filePath: string, timeoutMs?: number): Promise<void>;
|
|
90
|
+
press(selector: string | null, key: string, timeoutMs?: number): Promise<void>;
|
|
91
|
+
hover(selector: string, timeoutMs?: number): Promise<void>;
|
|
92
|
+
selectOption(selector: string, value: string, timeoutMs?: number): Promise<void>;
|
|
93
|
+
setChecked(selector: string, checked: boolean, timeoutMs?: number): Promise<void>;
|
|
94
|
+
/** Selectors a `hide` step registered; re-applied before every screenshot (see {@link syncHidden}). */
|
|
95
|
+
private readonly hidden;
|
|
96
|
+
hideElements(selector: string, timeoutMs?: number): Promise<void>;
|
|
97
|
+
showElements(selector: string | null): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Re-mark every element a registered selector matches right now. Navigation drops the marks and
|
|
100
|
+
* a framework re-render can replace the elements, so the registry (not the DOM) is the source of
|
|
101
|
+
* truth and the marks are rebuilt from it. Unmark first so a `show` takes effect.
|
|
102
|
+
*/
|
|
103
|
+
private syncHidden;
|
|
85
104
|
waitForNetworkIdle(): Promise<void>;
|
|
86
105
|
waitForLoad(): Promise<void>;
|
|
87
106
|
waitForElementStable(selector: string): Promise<void>;
|
|
107
|
+
waitForSettled(timeoutMs?: number): Promise<void>;
|
|
88
108
|
waitForSelector(selector: string, timeoutMs?: number): Promise<void>;
|
|
89
109
|
waitForTimeout(ms: number): Promise<void>;
|
|
90
110
|
isVisible(selector: string): Promise<boolean>;
|
|
@@ -11,8 +11,13 @@ import { existsSync, promises as fs } from "node:fs";
|
|
|
11
11
|
import * as path from "node:path";
|
|
12
12
|
import { chromium, } from "playwright-core";
|
|
13
13
|
import { VIEWPORT_PRESETS, } from "./doc-pack.js";
|
|
14
|
+
import { egressContextOptions, installEgressGuard, resolveEgressGuard, } from "./egress-guard.js";
|
|
15
|
+
import { budget, withTimeout } from "./driver-budget.js";
|
|
16
|
+
import { HIDDEN_ATTR, markHidden, unmarkHidden } from "./page-hide.js";
|
|
14
17
|
import { collectNearbyBoxes } from "./page-nearby-boxes.js";
|
|
18
|
+
import { awaitSettled, runSettle } from "./page-settle.js";
|
|
15
19
|
import { applyRedactions } from "./redact.js";
|
|
20
|
+
import { resolveUploadPath } from "./upload-path.js";
|
|
16
21
|
import { resolveWorkspacePathReal } from "./workspace.js";
|
|
17
22
|
/**
|
|
18
23
|
* The cached Chromium binary path, or `undefined` when `playwright-core` has none installed.
|
|
@@ -51,9 +56,11 @@ async function installClock(page, env) {
|
|
|
51
56
|
}
|
|
52
57
|
/** Launch Chromium (or, with `connectOverCdp`, attach to a running one) and return a {@link PlaywrightSession}. */
|
|
53
58
|
export async function launchPlaywrightSession(opts = {}) {
|
|
59
|
+
const guard = resolveEgressGuard(opts.egressGuard);
|
|
54
60
|
if (opts.connectOverCdp) {
|
|
55
61
|
const browser = await chromium.connectOverCDP(opts.connectOverCdp);
|
|
56
62
|
const context = browser.contexts()[0] ?? (await browser.newContext());
|
|
63
|
+
await installEgressGuard(context, guard);
|
|
57
64
|
const pages = context.pages();
|
|
58
65
|
const page = pages.find((p) => /^https?:/.test(p.url())) ?? pages[0] ?? (await context.newPage());
|
|
59
66
|
// The attached context is externally owned — context-level environment fields can't apply.
|
|
@@ -66,7 +73,7 @@ export async function launchPlaywrightSession(opts = {}) {
|
|
|
66
73
|
].join(", ")}\n`);
|
|
67
74
|
}
|
|
68
75
|
await installClock(page, opts.environment);
|
|
69
|
-
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".");
|
|
76
|
+
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".", opts.outputRoot);
|
|
70
77
|
return {
|
|
71
78
|
browser,
|
|
72
79
|
context,
|
|
@@ -88,10 +95,12 @@ export async function launchPlaywrightSession(opts = {}) {
|
|
|
88
95
|
...(opts.ignoreHTTPSErrors ? { ignoreHTTPSErrors: true } : {}),
|
|
89
96
|
...(opts.environment ? environmentContextOptions(opts.environment) : {}),
|
|
90
97
|
...(opts.contextOptions ?? {}),
|
|
98
|
+
...egressContextOptions(guard),
|
|
91
99
|
});
|
|
100
|
+
await installEgressGuard(context, guard);
|
|
92
101
|
const page = await context.newPage();
|
|
93
102
|
await installClock(page, opts.environment);
|
|
94
|
-
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".");
|
|
103
|
+
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".", opts.outputRoot);
|
|
95
104
|
return {
|
|
96
105
|
browser,
|
|
97
106
|
context,
|
|
@@ -104,46 +113,76 @@ export async function launchPlaywrightSession(opts = {}) {
|
|
|
104
113
|
},
|
|
105
114
|
};
|
|
106
115
|
}
|
|
107
|
-
/** Rejects with a timeout error if `work` takes longer than `ms` (no limit when `ms` is undefined). */
|
|
108
|
-
function withTimeout(work, ms) {
|
|
109
|
-
if (ms === undefined)
|
|
110
|
-
return work;
|
|
111
|
-
let timer;
|
|
112
|
-
const timeout = new Promise((_, reject) => {
|
|
113
|
-
timer = setTimeout(() => reject(new Error(`timed out after ${ms}ms`)), ms);
|
|
114
|
-
});
|
|
115
|
-
return Promise.race([work, timeout]).finally(() => clearTimeout(timer));
|
|
116
|
-
}
|
|
117
116
|
export class PlaywrightDriver {
|
|
118
117
|
page;
|
|
119
118
|
docPackRoot;
|
|
120
|
-
|
|
119
|
+
outputRoot;
|
|
120
|
+
constructor(page, docPackRoot = ".", outputRoot) {
|
|
121
121
|
this.page = page;
|
|
122
122
|
this.docPackRoot = docPackRoot;
|
|
123
|
+
this.outputRoot = outputRoot ?? docPackRoot;
|
|
123
124
|
}
|
|
124
125
|
goto(url) {
|
|
125
126
|
return this.page.goto(url).then(() => undefined);
|
|
126
127
|
}
|
|
127
|
-
click(selector) {
|
|
128
|
-
return this.page.click(selector);
|
|
128
|
+
click(selector, timeoutMs) {
|
|
129
|
+
return this.page.click(selector, budget(timeoutMs));
|
|
130
|
+
}
|
|
131
|
+
fill(selector, value, timeoutMs) {
|
|
132
|
+
return this.page.fill(selector, value, budget(timeoutMs));
|
|
133
|
+
}
|
|
134
|
+
async upload(selector, filePath, timeoutMs) {
|
|
135
|
+
const file = await resolveUploadPath(this.docPackRoot, filePath);
|
|
136
|
+
return this.page.setInputFiles(selector, file, budget(timeoutMs));
|
|
129
137
|
}
|
|
130
|
-
|
|
131
|
-
return
|
|
138
|
+
press(selector, key, timeoutMs) {
|
|
139
|
+
return selector
|
|
140
|
+
? this.page.press(selector, key, budget(timeoutMs))
|
|
141
|
+
: this.page.keyboard.press(key);
|
|
132
142
|
}
|
|
133
|
-
|
|
134
|
-
return this.page.
|
|
143
|
+
hover(selector, timeoutMs) {
|
|
144
|
+
return this.page.hover(selector, budget(timeoutMs));
|
|
135
145
|
}
|
|
136
|
-
|
|
137
|
-
return
|
|
146
|
+
selectOption(selector, value, timeoutMs) {
|
|
147
|
+
return this.page.selectOption(selector, value, budget(timeoutMs)).then(() => undefined);
|
|
138
148
|
}
|
|
139
|
-
|
|
140
|
-
return this.page.
|
|
149
|
+
setChecked(selector, checked, timeoutMs) {
|
|
150
|
+
return this.page.setChecked(selector, checked, budget(timeoutMs));
|
|
151
|
+
}
|
|
152
|
+
/** Selectors a `hide` step registered; re-applied before every screenshot (see {@link syncHidden}). */
|
|
153
|
+
hidden = new Set();
|
|
154
|
+
async hideElements(selector, timeoutMs) {
|
|
155
|
+
// Wait for the element to exist, not to be visible: it may already be hidden by the page.
|
|
156
|
+
await this.page
|
|
157
|
+
.locator(selector)
|
|
158
|
+
.first()
|
|
159
|
+
.waitFor({ state: "attached", ...budget(timeoutMs) });
|
|
160
|
+
this.hidden.add(selector);
|
|
161
|
+
try {
|
|
162
|
+
await this.syncHidden();
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
this.hidden.delete(selector); // a rule the page can't take must not poison every later screenshot
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
141
168
|
}
|
|
142
|
-
|
|
143
|
-
|
|
169
|
+
async showElements(selector) {
|
|
170
|
+
if (selector === null)
|
|
171
|
+
this.hidden.clear();
|
|
172
|
+
else
|
|
173
|
+
this.hidden.delete(selector);
|
|
174
|
+
await this.syncHidden();
|
|
144
175
|
}
|
|
145
|
-
|
|
146
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Re-mark every element a registered selector matches right now. Navigation drops the marks and
|
|
178
|
+
* a framework re-render can replace the elements, so the registry (not the DOM) is the source of
|
|
179
|
+
* truth and the marks are rebuilt from it. Unmark first so a `show` takes effect.
|
|
180
|
+
*/
|
|
181
|
+
async syncHidden() {
|
|
182
|
+
await this.page.locator(`[${HIDDEN_ATTR}]`).evaluateAll(unmarkHidden, HIDDEN_ATTR);
|
|
183
|
+
for (const selector of this.hidden) {
|
|
184
|
+
await this.page.locator(selector).evaluateAll(markHidden, HIDDEN_ATTR);
|
|
185
|
+
}
|
|
147
186
|
}
|
|
148
187
|
waitForNetworkIdle() {
|
|
149
188
|
return this.page.waitForLoadState("networkidle");
|
|
@@ -172,6 +211,9 @@ export class PlaywrightDriver {
|
|
|
172
211
|
await this.page.waitForTimeout(100);
|
|
173
212
|
}
|
|
174
213
|
}
|
|
214
|
+
async waitForSettled(timeoutMs) {
|
|
215
|
+
await runSettle((args) => this.page.evaluate(awaitSettled, args), timeoutMs);
|
|
216
|
+
}
|
|
175
217
|
waitForSelector(selector, timeoutMs) {
|
|
176
218
|
return this.page
|
|
177
219
|
.waitForSelector(selector, timeoutMs ? { timeout: timeoutMs } : {})
|
|
@@ -284,12 +326,15 @@ export class PlaywrightDriver {
|
|
|
284
326
|
async screenshot(relPath, redactions = []) {
|
|
285
327
|
// relPath segments carry flow names + step ids from the flow-file — containment-checked
|
|
286
328
|
// (symlink-aware) against the doc-pack root before writing.
|
|
287
|
-
const abs = await resolveWorkspacePathReal(this.
|
|
329
|
+
const abs = await resolveWorkspacePathReal(this.outputRoot, relPath);
|
|
288
330
|
await fs.mkdir(path.dirname(abs), { recursive: true });
|
|
289
331
|
// `animations: "disabled"` fast-forwards finite CSS animations/transitions to their end state
|
|
290
332
|
// and cancels infinite ones, so an element transitioning in (opacity/transform) is captured
|
|
291
333
|
// fully settled instead of mid-fade. `caret: "hide"` keeps a blinking text caret out of shots.
|
|
292
334
|
const shotOptions = { animations: "disabled", caret: "hide" };
|
|
335
|
+
// Elements a `hide` step registered are re-marked first: a navigation or re-render since may have dropped the marks.
|
|
336
|
+
if (this.hidden.size > 0)
|
|
337
|
+
await this.syncHidden();
|
|
293
338
|
if (redactions.length === 0) {
|
|
294
339
|
await this.page.screenshot({ path: abs, ...shotOptions });
|
|
295
340
|
return;
|
|
@@ -20,35 +20,35 @@ declare const manifestSchema: z.ZodObject<{
|
|
|
20
20
|
plugin: z.ZodString;
|
|
21
21
|
version: z.ZodString;
|
|
22
22
|
}, "strict", z.ZodTypeAny, {
|
|
23
|
-
plugin: string;
|
|
24
23
|
version: string;
|
|
25
|
-
}, {
|
|
26
24
|
plugin: string;
|
|
25
|
+
}, {
|
|
27
26
|
version: string;
|
|
27
|
+
plugin: string;
|
|
28
28
|
}>, "many">>;
|
|
29
29
|
trust: z.ZodDefault<z.ZodEnum<["kalebtec", "community", "local"]>>;
|
|
30
30
|
}, "strict", z.ZodTypeAny, {
|
|
31
|
+
capabilities: string[];
|
|
31
32
|
apiVersion: string;
|
|
32
33
|
namespace: string;
|
|
33
34
|
register: string;
|
|
34
|
-
kinds: ("
|
|
35
|
-
capabilities: string[];
|
|
35
|
+
kinds: ("publisher" | "renderer" | "lint-rules" | "auth-strategy")[];
|
|
36
36
|
dependsOn: {
|
|
37
|
-
plugin: string;
|
|
38
37
|
version: string;
|
|
38
|
+
plugin: string;
|
|
39
39
|
}[];
|
|
40
|
-
trust: "
|
|
40
|
+
trust: "local" | "kalebtec" | "community";
|
|
41
41
|
}, {
|
|
42
42
|
apiVersion: string;
|
|
43
43
|
namespace: string;
|
|
44
44
|
register: string;
|
|
45
|
-
kinds: ("
|
|
45
|
+
kinds: ("publisher" | "renderer" | "lint-rules" | "auth-strategy")[];
|
|
46
46
|
capabilities?: string[] | undefined;
|
|
47
47
|
dependsOn?: {
|
|
48
|
-
plugin: string;
|
|
49
48
|
version: string;
|
|
49
|
+
plugin: string;
|
|
50
50
|
}[] | undefined;
|
|
51
|
-
trust?: "
|
|
51
|
+
trust?: "local" | "kalebtec" | "community" | undefined;
|
|
52
52
|
}>;
|
|
53
53
|
export type PluginManifest = z.infer<typeof manifestSchema>;
|
|
54
54
|
export declare class PluginManifestError extends Error {
|