@open-qa/qa-stage 0.0.0-stage → 0.1.0-alpha.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/LICENSE +204 -0
- package/NOTICE +7 -0
- package/THIRD_PARTY.md +13 -0
- package/THIRD_PARTY_NOTICES.md +11 -0
- package/package.json +68 -4
- package/scripts/bundle.d.ts +7 -0
- package/scripts/bundle.js +78 -0
- package/src/auth.d.ts +10 -0
- package/src/auth.js +26 -0
- package/src/blackboard-tools.d.ts +2 -0
- package/src/blackboard-tools.js +46 -0
- package/src/config.d.ts +61 -0
- package/src/config.js +64 -0
- package/src/discord-api.d.ts +47 -0
- package/src/discord-api.js +151 -0
- package/src/discord-surface.d.ts +4 -0
- package/src/discord-surface.js +218 -0
- package/src/drivers.d.ts +21 -0
- package/src/drivers.js +12 -0
- package/src/email-message.d.ts +22 -0
- package/src/email-message.js +114 -0
- package/src/email-surface.d.ts +4 -0
- package/src/email-surface.js +140 -0
- package/src/event-log.d.ts +18 -0
- package/src/event-log.js +46 -0
- package/src/expect-engine.d.ts +15 -0
- package/src/expect-engine.js +114 -0
- package/src/expect-evidence.d.ts +7 -0
- package/src/expect-evidence.js +68 -0
- package/src/flow-tools.d.ts +2 -0
- package/src/flow-tools.js +26 -0
- package/src/http-expect.d.ts +4 -0
- package/src/http-expect.js +68 -0
- package/src/index.d.ts +13 -0
- package/src/index.js +37 -0
- package/src/main.d.ts +1 -0
- package/src/main.js +51 -0
- package/src/mcp.d.ts +14 -0
- package/src/mcp.js +23 -0
- package/src/observation-tools.d.ts +3 -0
- package/src/observation-tools.js +39 -0
- package/src/page-diagnostics.d.ts +19 -0
- package/src/page-diagnostics.js +53 -0
- package/src/page-pixels.d.ts +35 -0
- package/src/page-pixels.js +73 -0
- package/src/page-policy.d.ts +48 -0
- package/src/page-policy.js +125 -0
- package/src/page-recording.d.ts +39 -0
- package/src/page-recording.js +532 -0
- package/src/page-replay.d.ts +7 -0
- package/src/page-replay.js +69 -0
- package/src/page-views.d.ts +85 -0
- package/src/page-views.js +338 -0
- package/src/rendered.d.ts +6 -0
- package/src/rendered.js +135 -0
- package/src/replay-artifacts.d.ts +2 -0
- package/src/replay-artifacts.js +100 -0
- package/src/replay-assets.d.ts +21 -0
- package/src/replay-assets.js +106 -0
- package/src/replay-budget.d.ts +22 -0
- package/src/replay-budget.js +47 -0
- package/src/replay-files.d.ts +3 -0
- package/src/replay-files.js +18 -0
- package/src/replay-privacy.d.ts +10 -0
- package/src/replay-privacy.js +48 -0
- package/src/replay-script.d.ts +2 -0
- package/src/replay-script.js +65 -0
- package/src/replay-warnings.d.ts +6 -0
- package/src/replay-warnings.js +16 -0
- package/src/replay-writer.d.ts +24 -0
- package/src/replay-writer.js +92 -0
- package/src/screen-tools.d.ts +5 -0
- package/src/screen-tools.js +168 -0
- package/src/server.d.ts +50 -0
- package/src/server.js +537 -0
- package/src/signed-artifacts.d.ts +21 -0
- package/src/signed-artifacts.js +127 -0
- package/src/sqlite-parser.d.ts +3 -0
- package/src/stage-clock.d.ts +2 -0
- package/src/stage-clock.js +7 -0
- package/src/surface-adapter.d.ts +65 -0
- package/src/surface-adapter.js +0 -0
- package/src/system-common.d.ts +34 -0
- package/src/system-common.js +123 -0
- package/src/system-db.d.ts +9 -0
- package/src/system-db.js +143 -0
- package/src/system-github.d.ts +6 -0
- package/src/system-github.js +73 -0
- package/src/system-http.d.ts +4 -0
- package/src/system-http.js +86 -0
- package/src/system-logs.d.ts +6 -0
- package/src/system-logs.js +78 -0
- package/src/telegram-api.d.ts +162 -0
- package/src/telegram-api.js +170 -0
- package/src/telegram-markup.d.ts +4 -0
- package/src/telegram-markup.js +129 -0
- package/src/telegram-surface.d.ts +39 -0
- package/src/telegram-surface.js +448 -0
- package/src/tool-gateway.d.ts +41 -0
- package/src/tool-gateway.js +696 -0
- package/src/tool-limits.d.ts +12 -0
- package/src/tool-limits.js +41 -0
- package/src/tool-secrets.d.ts +8 -0
- package/src/tool-secrets.js +38 -0
- package/src/vendor/gmail.d.ts +45 -0
- package/src/vendor/gmail.js +193 -0
- package/src/watch.d.ts +11 -0
- package/src/watch.js +30 -0
- package/src/web-expect.d.ts +5 -0
- package/src/web-expect.js +130 -0
- package/src/web-tools.d.ts +15 -0
- package/src/web-tools.js +340 -0
- package/README.md +0 -3
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { chromium } from "playwright";
|
|
5
|
+
import sharp from "sharp";
|
|
6
|
+
import { StageError } from "./drivers.js";
|
|
7
|
+
import {
|
|
8
|
+
assertPageUrl,
|
|
9
|
+
installPagePolicy,
|
|
10
|
+
installRedirectPolicy,
|
|
11
|
+
storageStateSchema
|
|
12
|
+
} from "./page-policy.js";
|
|
13
|
+
import { pixelScreenshot } from "./page-pixels.js";
|
|
14
|
+
import { screenTools, screenshotFallback } from "./screen-tools.js";
|
|
15
|
+
import { webTools } from "./web-tools.js";
|
|
16
|
+
import { PageDiagnostics } from "./page-diagnostics.js";
|
|
17
|
+
import { webExpect } from "./web-expect.js";
|
|
18
|
+
import {
|
|
19
|
+
browserStateSecrets,
|
|
20
|
+
redactExpectEvidence
|
|
21
|
+
} from "./expect-evidence.js";
|
|
22
|
+
import { RENDERED_PATH, renderContent } from "./rendered.js";
|
|
23
|
+
const MAX_PAGE_VIEWS_PER_ACTOR = 4;
|
|
24
|
+
const MAX_PAGE_VIEWS_PER_STAGE = 8;
|
|
25
|
+
const BROWSER_ENVIRONMENT_KEYS = [
|
|
26
|
+
"PATH",
|
|
27
|
+
"HOME",
|
|
28
|
+
"TMPDIR",
|
|
29
|
+
"DISPLAY",
|
|
30
|
+
"XAUTHORITY",
|
|
31
|
+
"LANG",
|
|
32
|
+
"LD_LIBRARY_PATH",
|
|
33
|
+
"XDG_CONFIG_HOME",
|
|
34
|
+
"XDG_CACHE_HOME",
|
|
35
|
+
"XDG_RUNTIME_DIR",
|
|
36
|
+
"WAYLAND_DISPLAY"
|
|
37
|
+
];
|
|
38
|
+
const PAGE_VIEWPORT = { width: 1568, height: 980 };
|
|
39
|
+
class PageViews {
|
|
40
|
+
constructor(config, dir, actors, fallback, lifecycle) {
|
|
41
|
+
this.config = config;
|
|
42
|
+
this.dir = dir;
|
|
43
|
+
this.actors = actors;
|
|
44
|
+
this.lifecycle = lifecycle;
|
|
45
|
+
this.surface = {
|
|
46
|
+
name: "web",
|
|
47
|
+
views: this.filteredViews(false),
|
|
48
|
+
tools: [...webTools(this, fallback ?? screenshotFallback), ...screenTools(this, dir)],
|
|
49
|
+
expect: webExpect(this)
|
|
50
|
+
};
|
|
51
|
+
this.drivers = {
|
|
52
|
+
setStep: async (request, context) => {
|
|
53
|
+
context.signal.throwIfAborted();
|
|
54
|
+
if (!this.actors.includes(request.actor))
|
|
55
|
+
throw new StageError(403, "actor_forbidden", "Unknown actor");
|
|
56
|
+
if (request.step === void 0) this.steps.delete(request.actor);
|
|
57
|
+
else this.steps.set(request.actor, request.step);
|
|
58
|
+
for (const entry of this.entries.values())
|
|
59
|
+
if (entry.info.actor === request.actor) entry.diagnostics.start(request.step);
|
|
60
|
+
return { ...request };
|
|
61
|
+
},
|
|
62
|
+
openView: (request, context) => this.open(request, context),
|
|
63
|
+
renderView: (request, context) => this.render(request, context),
|
|
64
|
+
closeView: async ({ view }, context) => {
|
|
65
|
+
const entry = this.entry(view);
|
|
66
|
+
entry.emit = context.emit;
|
|
67
|
+
try {
|
|
68
|
+
await this.lifecycle?.closing(view);
|
|
69
|
+
} finally {
|
|
70
|
+
await entry.browser.close();
|
|
71
|
+
}
|
|
72
|
+
this.retire(entry);
|
|
73
|
+
return { view, closed: true };
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
config;
|
|
78
|
+
dir;
|
|
79
|
+
actors;
|
|
80
|
+
lifecycle;
|
|
81
|
+
entries = /* @__PURE__ */ new Map();
|
|
82
|
+
opening = /* @__PURE__ */ new Map();
|
|
83
|
+
launches = /* @__PURE__ */ new Set();
|
|
84
|
+
steps = /* @__PURE__ */ new Map();
|
|
85
|
+
stateValues = /* @__PURE__ */ new Set();
|
|
86
|
+
stopped = false;
|
|
87
|
+
renderedUrl;
|
|
88
|
+
addRedactions(values) {
|
|
89
|
+
for (const value of values) if (value) this.stateValues.add(value);
|
|
90
|
+
}
|
|
91
|
+
setRenderedOrigin(origin) {
|
|
92
|
+
this.renderedUrl = new URL(RENDERED_PATH, origin).href;
|
|
93
|
+
}
|
|
94
|
+
async render(request, driver) {
|
|
95
|
+
const { view, content } = request;
|
|
96
|
+
const entry = this.entry(view);
|
|
97
|
+
if (entry.template !== content.template)
|
|
98
|
+
throw new StageError(
|
|
99
|
+
400,
|
|
100
|
+
"template_mismatch",
|
|
101
|
+
"Content must match the rendered view template"
|
|
102
|
+
);
|
|
103
|
+
return this.run(
|
|
104
|
+
view,
|
|
105
|
+
{ actor: entry.info.actor, signal: driver.signal },
|
|
106
|
+
async (page, context) => {
|
|
107
|
+
await renderContent(page, content, context.signal);
|
|
108
|
+
return { view, rendered: true };
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
surface;
|
|
113
|
+
drivers;
|
|
114
|
+
rememberState(state) {
|
|
115
|
+
for (const cookie of state.cookies)
|
|
116
|
+
for (const value of browserStateSecrets(cookie.value)) this.stateValues.add(value);
|
|
117
|
+
for (const origin of state.origins)
|
|
118
|
+
for (const item of origin.localStorage)
|
|
119
|
+
for (const value of browserStateSecrets(item.value)) this.stateValues.add(value);
|
|
120
|
+
}
|
|
121
|
+
redactEvidence(value, secrets = [], options) {
|
|
122
|
+
return redactExpectEvidence(value, [...this.stateValues, ...secrets], options);
|
|
123
|
+
}
|
|
124
|
+
retire(entry) {
|
|
125
|
+
if (this.entries.get(entry.info.view) !== entry) return;
|
|
126
|
+
this.entries.delete(entry.info.view);
|
|
127
|
+
try {
|
|
128
|
+
entry.emit({ kind: "view_closed", by: "conductor", view: entry.info.view });
|
|
129
|
+
} catch {
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
list() {
|
|
133
|
+
return [...this.entries.values()].map(({ info, page }) => ({
|
|
134
|
+
...info,
|
|
135
|
+
viewport: { ...info.viewport },
|
|
136
|
+
url: page.url()
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
filteredViews(discord) {
|
|
140
|
+
return {
|
|
141
|
+
list: () => this.list().filter((view) => !!this.entry(view.view).source === discord),
|
|
142
|
+
observe: (view, context) => this.observe(view, context),
|
|
143
|
+
snapshot: (view, context) => this.snapshot(view, context),
|
|
144
|
+
screenshot: (view, context) => this.screenshot(view, context),
|
|
145
|
+
text: (view, context) => this.text(view, context)
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
entry(view, actor) {
|
|
149
|
+
const entry = this.entries.get(view);
|
|
150
|
+
if (!entry || actor && entry.info.actor !== actor)
|
|
151
|
+
throw new StageError(403, "view_forbidden", "View is not available to this actor");
|
|
152
|
+
return entry;
|
|
153
|
+
}
|
|
154
|
+
open(request, context) {
|
|
155
|
+
const pending = this.openPage(request, context);
|
|
156
|
+
this.launches.add(pending);
|
|
157
|
+
void pending.finally(() => this.launches.delete(pending)).catch(() => {
|
|
158
|
+
});
|
|
159
|
+
return pending;
|
|
160
|
+
}
|
|
161
|
+
async openPage(request, driver) {
|
|
162
|
+
driver.signal.throwIfAborted();
|
|
163
|
+
if (this.stopped) throw new StageError(503, "stage_stopping", "Stage is stopping");
|
|
164
|
+
if (!this.actors.includes(request.actor))
|
|
165
|
+
throw new StageError(403, "actor_forbidden", "Unknown actor");
|
|
166
|
+
if (this.entries.has(request.view) || this.opening.has(request.view))
|
|
167
|
+
throw new StageError(409, "view_exists", "View already exists");
|
|
168
|
+
const actorCount = [...this.entries.values()].filter((entry) => entry.info.actor === request.actor).length + [...this.opening.values()].filter((actor) => actor === request.actor).length;
|
|
169
|
+
if (actorCount >= MAX_PAGE_VIEWS_PER_ACTOR || this.entries.size + this.opening.size >= MAX_PAGE_VIEWS_PER_STAGE)
|
|
170
|
+
throw new StageError(429, "view_limit", "Browser view limit reached");
|
|
171
|
+
const url = request.type === "page" ? request.url : this.renderedUrl;
|
|
172
|
+
if (!url) throw new StageError(503, "rendered_unavailable", "Rendered page is not configured");
|
|
173
|
+
if (request.type === "page") assertPageUrl(url, this.config);
|
|
174
|
+
const parsed = request.type !== "page" || request.storageState === void 0 ? void 0 : storageStateSchema.safeParse(request.storageState);
|
|
175
|
+
if (parsed && !parsed.success)
|
|
176
|
+
throw new StageError(400, "invalid_storage_state", "Invalid browser storage state");
|
|
177
|
+
if (parsed?.data) this.rememberState(parsed.data);
|
|
178
|
+
this.opening.set(request.view, request.actor);
|
|
179
|
+
let browser;
|
|
180
|
+
try {
|
|
181
|
+
browser = await chromium.launch({
|
|
182
|
+
headless: !this.config.headed,
|
|
183
|
+
timeout: this.config.timeoutMs,
|
|
184
|
+
env: Object.fromEntries(
|
|
185
|
+
Object.entries(process.env).filter(
|
|
186
|
+
([name, value]) => value !== void 0 && BROWSER_ENVIRONMENT_KEYS.includes(name)
|
|
187
|
+
)
|
|
188
|
+
)
|
|
189
|
+
});
|
|
190
|
+
driver.signal.throwIfAborted();
|
|
191
|
+
const context = await browser.newContext({
|
|
192
|
+
viewport: request.viewport ?? PAGE_VIEWPORT,
|
|
193
|
+
storageState: parsed?.data,
|
|
194
|
+
serviceWorkers: "block",
|
|
195
|
+
acceptDownloads: false
|
|
196
|
+
});
|
|
197
|
+
context.setDefaultTimeout(this.config.timeoutMs);
|
|
198
|
+
context.setDefaultNavigationTimeout(this.config.timeoutMs);
|
|
199
|
+
const page = await context.newPage();
|
|
200
|
+
const diagnostics = new PageDiagnostics(page, this.steps.get(request.actor));
|
|
201
|
+
if (request.type === "rendered") {
|
|
202
|
+
await context.route("**/*", (route) => {
|
|
203
|
+
const req = route.request();
|
|
204
|
+
return req.url() === url && req.method() === "GET" && req.isNavigationRequest() && req.frame() === page.mainFrame() ? route.continue() : route.abort("blockedbyclient");
|
|
205
|
+
});
|
|
206
|
+
await context.routeWebSocket("**/*", (socket) => socket.close());
|
|
207
|
+
} else {
|
|
208
|
+
await installPagePolicy(context, this.config, page);
|
|
209
|
+
await installRedirectPolicy(page, this.config);
|
|
210
|
+
}
|
|
211
|
+
context.on("page", (popup) => {
|
|
212
|
+
void popup.close().catch(() => {
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
const entry = {
|
|
216
|
+
browser,
|
|
217
|
+
context,
|
|
218
|
+
page,
|
|
219
|
+
emit: driver.emit,
|
|
220
|
+
diagnostics,
|
|
221
|
+
...request.type === "rendered" ? { template: request.template, source: request.source, email: request.email } : {},
|
|
222
|
+
info: {
|
|
223
|
+
actor: request.actor,
|
|
224
|
+
view: request.view,
|
|
225
|
+
type: request.type,
|
|
226
|
+
url,
|
|
227
|
+
viewport: request.viewport ?? PAGE_VIEWPORT
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
await this.lifecycle?.prepare(entry, parsed?.data);
|
|
231
|
+
await page.goto(url, { waitUntil: "domcontentloaded", signal: driver.signal });
|
|
232
|
+
driver.signal.throwIfAborted();
|
|
233
|
+
if (this.stopped) throw new Error("Stage is stopping");
|
|
234
|
+
const info = {
|
|
235
|
+
actor: request.actor,
|
|
236
|
+
view: request.view,
|
|
237
|
+
type: request.type,
|
|
238
|
+
url: page.url(),
|
|
239
|
+
viewport: request.viewport ?? PAGE_VIEWPORT
|
|
240
|
+
};
|
|
241
|
+
const eventUrl = new URL(info.url);
|
|
242
|
+
eventUrl.search = "";
|
|
243
|
+
eventUrl.hash = "";
|
|
244
|
+
driver.emit({
|
|
245
|
+
kind: "view_opened",
|
|
246
|
+
by: "conductor",
|
|
247
|
+
actor: info.actor,
|
|
248
|
+
view: info.view,
|
|
249
|
+
type: request.type,
|
|
250
|
+
url: eventUrl.href
|
|
251
|
+
});
|
|
252
|
+
entry.info = info;
|
|
253
|
+
this.opening.delete(request.view);
|
|
254
|
+
this.entries.set(request.view, entry);
|
|
255
|
+
browser.on("disconnected", () => this.retire(entry));
|
|
256
|
+
if (!browser.isConnected()) this.retire(entry);
|
|
257
|
+
return { ...info };
|
|
258
|
+
} catch {
|
|
259
|
+
await browser?.close();
|
|
260
|
+
throw new StageError(500, "view_open_failed", "Could not open browser view");
|
|
261
|
+
} finally {
|
|
262
|
+
this.opening.delete(request.view);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async close() {
|
|
266
|
+
this.stopped = true;
|
|
267
|
+
await Promise.allSettled([...this.launches]);
|
|
268
|
+
await Promise.all(
|
|
269
|
+
[...this.entries.values()].map(async ({ info, browser }) => {
|
|
270
|
+
try {
|
|
271
|
+
await this.lifecycle?.closing(info.view);
|
|
272
|
+
} finally {
|
|
273
|
+
await browser.close();
|
|
274
|
+
}
|
|
275
|
+
})
|
|
276
|
+
);
|
|
277
|
+
this.entries.clear();
|
|
278
|
+
}
|
|
279
|
+
async run(view, context, operation) {
|
|
280
|
+
if (context.signal?.aborted)
|
|
281
|
+
throw new StageError(408, "canceled", "Browser operation canceled");
|
|
282
|
+
const entry = this.entry(view, context.actor);
|
|
283
|
+
const controller = new AbortController();
|
|
284
|
+
let closing;
|
|
285
|
+
let interrupted;
|
|
286
|
+
const abort = (reason) => {
|
|
287
|
+
interrupted ??= reason;
|
|
288
|
+
controller.abort();
|
|
289
|
+
closing ??= entry.browser.close();
|
|
290
|
+
};
|
|
291
|
+
const cancel = () => abort("canceled");
|
|
292
|
+
const timer = setTimeout(() => abort("timeout"), this.config.timeoutMs * 3);
|
|
293
|
+
context.signal?.addEventListener("abort", cancel, { once: true });
|
|
294
|
+
try {
|
|
295
|
+
return await operation(entry.page, { ...context, signal: controller.signal });
|
|
296
|
+
} catch (error) {
|
|
297
|
+
if (interrupted) throw new StageError(408, interrupted, "Browser operation interrupted");
|
|
298
|
+
if (!entry.browser.isConnected() || entry.page.isClosed())
|
|
299
|
+
throw new StageError(410, "browser_closed", "Browser view closed");
|
|
300
|
+
throw error;
|
|
301
|
+
} finally {
|
|
302
|
+
clearTimeout(timer);
|
|
303
|
+
context.signal?.removeEventListener("abort", cancel);
|
|
304
|
+
await closing;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
async snapshot(view, context) {
|
|
308
|
+
return this.entry(view, context.actor).page.ariaSnapshot({
|
|
309
|
+
mode: "ai",
|
|
310
|
+
signal: context.signal
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
async screenshot(view, context) {
|
|
314
|
+
return pixelScreenshot(this.entry(view, context.actor).page, context);
|
|
315
|
+
}
|
|
316
|
+
async text(view, context) {
|
|
317
|
+
return this.entry(view, context.actor).page.locator("body").innerText({ signal: context.signal });
|
|
318
|
+
}
|
|
319
|
+
async observe(view, context) {
|
|
320
|
+
const page = this.entry(view, context.actor).page;
|
|
321
|
+
const fingerprint = await this.snapshot(view, context);
|
|
322
|
+
const buffer = await page.screenshot({ type: "png", signal: context.signal });
|
|
323
|
+
const shot = `shots/${randomUUID()}.png`;
|
|
324
|
+
await mkdir(join(this.dir, "shots"), { recursive: true });
|
|
325
|
+
await writeFile(join(this.dir, shot), buffer);
|
|
326
|
+
const thumbnail = new Uint8Array(
|
|
327
|
+
await sharp(buffer).resize(32, 20, { fit: "fill" }).greyscale().raw().toBuffer()
|
|
328
|
+
);
|
|
329
|
+
return { shot, fingerprint, thumbnail, pixels: buffer };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
export {
|
|
333
|
+
BROWSER_ENVIRONMENT_KEYS,
|
|
334
|
+
MAX_PAGE_VIEWS_PER_ACTOR,
|
|
335
|
+
MAX_PAGE_VIEWS_PER_STAGE,
|
|
336
|
+
PAGE_VIEWPORT,
|
|
337
|
+
PageViews
|
|
338
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Page } from "playwright";
|
|
2
|
+
import type { RenderedContent } from "@open-qa/qa-engine/protocol";
|
|
3
|
+
export declare const RENDERED_CSP = "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'";
|
|
4
|
+
export declare const RENDERED_PATH = "/rendered";
|
|
5
|
+
export declare const renderedShell = "<!doctype html><html lang=\"en\"><meta charset=\"utf-8\"><title>Rendered view</title><style>\n*{box-sizing:border-box}\nbody{margin:0;background:#f3f4f6;color:#18212f;font:16px/1.5 Arial,sans-serif}\nmain{max-width:980px;margin:40px auto;padding:32px;background:white;border:1px solid #d5dbe3;border-radius:12px}\nh1{font-size:26px;margin:0 0 24px}\nh2{font-size:18px;margin:0 0 8px}\narticle{border-top:1px solid #e5e7eb;padding:20px 0}\nheader{display:flex;gap:16px;align-items:baseline}\ntime{color:#526073;font-size:13px}\n.content{white-space:pre-wrap;overflow-wrap:anywhere}\n.embed{border-left:4px solid #5573cd;background:#f6f8fc;padding:16px;margin:14px 0;border-radius:4px}\n.fields{display:flex;gap:24px;flex-wrap:wrap}\n.field{min-width:120px}\nfooter{font-size:13px;color:#526073;margin-top:12px}\nbutton{font:inherit;border:1px solid #c3cada;border-radius:6px;background:#edf1f8;color:#384968;padding:8px 14px;margin:8px 8px 0 0}\n.sender{color:#526073;border-bottom:1px solid #e5e7eb;padding-bottom:20px;margin-bottom:24px}\ntable{border-collapse:collapse}\ntd,th{border:1px solid #d5dbe3;padding:8px}\npre{white-space:pre-wrap}\naside{border:2px solid #e29420;padding:12px;background:#fff4df}\n</style><main aria-label=\"Rendered content\"><h1>Waiting for content</h1></main></html>";
|
|
6
|
+
export declare function renderContent(page: Page, content: RenderedContent, signal?: AbortSignal): Promise<void>;
|
package/src/rendered.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const RENDERED_CSP = "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'";
|
|
2
|
+
const RENDERED_PATH = "/rendered";
|
|
3
|
+
const renderedShell = `<!doctype html><html lang="en"><meta charset="utf-8"><title>Rendered view</title><style>
|
|
4
|
+
*{box-sizing:border-box}
|
|
5
|
+
body{margin:0;background:#f3f4f6;color:#18212f;font:16px/1.5 Arial,sans-serif}
|
|
6
|
+
main{max-width:980px;margin:40px auto;padding:32px;background:white;border:1px solid #d5dbe3;border-radius:12px}
|
|
7
|
+
h1{font-size:26px;margin:0 0 24px}
|
|
8
|
+
h2{font-size:18px;margin:0 0 8px}
|
|
9
|
+
article{border-top:1px solid #e5e7eb;padding:20px 0}
|
|
10
|
+
header{display:flex;gap:16px;align-items:baseline}
|
|
11
|
+
time{color:#526073;font-size:13px}
|
|
12
|
+
.content{white-space:pre-wrap;overflow-wrap:anywhere}
|
|
13
|
+
.embed{border-left:4px solid #5573cd;background:#f6f8fc;padding:16px;margin:14px 0;border-radius:4px}
|
|
14
|
+
.fields{display:flex;gap:24px;flex-wrap:wrap}
|
|
15
|
+
.field{min-width:120px}
|
|
16
|
+
footer{font-size:13px;color:#526073;margin-top:12px}
|
|
17
|
+
button{font:inherit;border:1px solid #c3cada;border-radius:6px;background:#edf1f8;color:#384968;padding:8px 14px;margin:8px 8px 0 0}
|
|
18
|
+
.sender{color:#526073;border-bottom:1px solid #e5e7eb;padding-bottom:20px;margin-bottom:24px}
|
|
19
|
+
table{border-collapse:collapse}
|
|
20
|
+
td,th{border:1px solid #d5dbe3;padding:8px}
|
|
21
|
+
pre{white-space:pre-wrap}
|
|
22
|
+
aside{border:2px solid #e29420;padding:12px;background:#fff4df}
|
|
23
|
+
</style><main aria-label="Rendered content"><h1>Waiting for content</h1></main></html>`;
|
|
24
|
+
const escapeHtml = (value) => value.replace(
|
|
25
|
+
/[&<>"']/g,
|
|
26
|
+
(ch) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[ch]
|
|
27
|
+
);
|
|
28
|
+
function chatMarkup(content) {
|
|
29
|
+
const messages = content.messages.map((message) => {
|
|
30
|
+
const embeds = (message.embeds ?? []).map((embed) => {
|
|
31
|
+
const fields = (embed.fields ?? []).map(
|
|
32
|
+
(field) => `<div class="field"><strong>${escapeHtml(field.name)}</strong><div class="content">${escapeHtml(field.value)}</div></div>`
|
|
33
|
+
).join("");
|
|
34
|
+
return `<section class="embed">` + (embed.title ? `<h2>${escapeHtml(embed.title)}</h2>` : "") + (embed.description ? `<p class="content">${escapeHtml(embed.description)}</p>` : "") + `<div class="fields">${fields}</div>` + (embed.footer ? `<footer>${escapeHtml(embed.footer)}</footer>` : "") + `</section>`;
|
|
35
|
+
}).join("");
|
|
36
|
+
const buttons = (message.buttons ?? []).map(
|
|
37
|
+
(button) => `<button type="button" disabled${button.url ? ` title="${escapeHtml(button.url)}"` : ""}>${escapeHtml(button.label)}</button>${button.url ? `<small class="meta">${escapeHtml(button.url)}</small>` : ""}`
|
|
38
|
+
).join("");
|
|
39
|
+
return `<article><header><strong>${escapeHtml(message.author)}</strong><time>${escapeHtml(message.time)}</time></header><p class="content">${escapeHtml(message.content)}</p>${embeds}${buttons}</article>`;
|
|
40
|
+
}).join("");
|
|
41
|
+
return `<h1>${escapeHtml(content.title)}</h1>${messages}`;
|
|
42
|
+
}
|
|
43
|
+
async function renderContent(page, content, signal) {
|
|
44
|
+
const html = content.template === "telegram" ? `<h1>${escapeHtml(content.title)}</h1>` + content.messages.map(
|
|
45
|
+
(message, i) => `<article><header><strong>${escapeHtml(message.author)}</strong><time>${escapeHtml(message.time)}</time></header><section data-message="${i}"></section>${(message.buttons ?? []).map((button) => `<button disabled>${escapeHtml(button.label)}</button>`).join("")}</article>`
|
|
46
|
+
).join("") : content.template === "chat" ? chatMarkup(content) : `<h1>${escapeHtml(content.subject)}</h1><div class="sender">From: ${escapeHtml(content.from)}</div><section aria-label="Email body"></section>`;
|
|
47
|
+
signal?.throwIfAborted();
|
|
48
|
+
await page.evaluate(
|
|
49
|
+
({ html: html2, fragments }) => {
|
|
50
|
+
const root = document.querySelector("main");
|
|
51
|
+
root.innerHTML = html2;
|
|
52
|
+
for (const [selector, fragment] of fragments) {
|
|
53
|
+
const parsed = new DOMParser().parseFromString(fragment, "text/html");
|
|
54
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
55
|
+
"aside",
|
|
56
|
+
"p",
|
|
57
|
+
"div",
|
|
58
|
+
"span",
|
|
59
|
+
"br",
|
|
60
|
+
"hr",
|
|
61
|
+
"h1",
|
|
62
|
+
"h2",
|
|
63
|
+
"h3",
|
|
64
|
+
"h4",
|
|
65
|
+
"h5",
|
|
66
|
+
"h6",
|
|
67
|
+
"strong",
|
|
68
|
+
"b",
|
|
69
|
+
"em",
|
|
70
|
+
"i",
|
|
71
|
+
"u",
|
|
72
|
+
"s",
|
|
73
|
+
"ul",
|
|
74
|
+
"ol",
|
|
75
|
+
"li",
|
|
76
|
+
"blockquote",
|
|
77
|
+
"pre",
|
|
78
|
+
"code",
|
|
79
|
+
"table",
|
|
80
|
+
"thead",
|
|
81
|
+
"tbody",
|
|
82
|
+
"tfoot",
|
|
83
|
+
"tr",
|
|
84
|
+
"td",
|
|
85
|
+
"th"
|
|
86
|
+
]);
|
|
87
|
+
const dropped = /* @__PURE__ */ new Set([
|
|
88
|
+
"script",
|
|
89
|
+
"style",
|
|
90
|
+
"iframe",
|
|
91
|
+
"object",
|
|
92
|
+
"embed",
|
|
93
|
+
"svg",
|
|
94
|
+
"math",
|
|
95
|
+
"form",
|
|
96
|
+
"input",
|
|
97
|
+
"button",
|
|
98
|
+
"select",
|
|
99
|
+
"textarea",
|
|
100
|
+
"link",
|
|
101
|
+
"meta",
|
|
102
|
+
"base",
|
|
103
|
+
"template"
|
|
104
|
+
]);
|
|
105
|
+
const body = root.querySelector(selector);
|
|
106
|
+
const pending = [];
|
|
107
|
+
for (const node of Array.from(parsed.body.childNodes).reverse())
|
|
108
|
+
pending.push({ node, parent: body });
|
|
109
|
+
while (pending.length) {
|
|
110
|
+
const { node, parent } = pending.pop();
|
|
111
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
112
|
+
parent.appendChild(document.createTextNode(node.textContent ?? ""));
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (!(node instanceof Element) || node.namespaceURI !== "http://www.w3.org/1999/xhtml" || dropped.has(node.localName))
|
|
116
|
+
continue;
|
|
117
|
+
const target = allowed.has(node.localName) ? parent.appendChild(document.createElement(node.localName)) : parent;
|
|
118
|
+
for (const child of Array.from(node.childNodes).reverse())
|
|
119
|
+
pending.push({ node: child, parent: target });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
html,
|
|
125
|
+
fragments: content.template === "email" ? [["section", content.html]] : content.template === "telegram" ? content.messages.map((message, i) => [`[data-message="${i}"]`, message.html]) : []
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
signal?.throwIfAborted();
|
|
129
|
+
}
|
|
130
|
+
export {
|
|
131
|
+
RENDERED_CSP,
|
|
132
|
+
RENDERED_PATH,
|
|
133
|
+
renderContent,
|
|
134
|
+
renderedShell
|
|
135
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { lstat, mkdir, readFile, readdir, realpath, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { StageError } from "./drivers.js";
|
|
4
|
+
import { sha256, writeAtomic } from "./replay-files.js";
|
|
5
|
+
async function stat(path) {
|
|
6
|
+
try {
|
|
7
|
+
return await lstat(path);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (error.code === "ENOENT") return void 0;
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async function canonical(path) {
|
|
14
|
+
if (await stat(path)) return realpath(path);
|
|
15
|
+
return join(await canonical(dirname(path)), path.slice(dirname(path).length + 1));
|
|
16
|
+
}
|
|
17
|
+
async function safeDirectory(path, root) {
|
|
18
|
+
if (path !== root) await safeDirectory(dirname(path), root);
|
|
19
|
+
const info = await stat(path);
|
|
20
|
+
if (info && (!info.isDirectory() || info.isSymbolicLink()))
|
|
21
|
+
throw new StageError(
|
|
22
|
+
400,
|
|
23
|
+
"unsafe_destination",
|
|
24
|
+
"Artifact directories must not contain symbolic links"
|
|
25
|
+
);
|
|
26
|
+
if (!info) await mkdir(path, { recursive: path === root, mode: 448 });
|
|
27
|
+
}
|
|
28
|
+
async function flushReplayArtifacts(source, destination) {
|
|
29
|
+
if ((await stat(resolve(destination)))?.isSymbolicLink())
|
|
30
|
+
throw new StageError(
|
|
31
|
+
400,
|
|
32
|
+
"unsafe_destination",
|
|
33
|
+
"Artifact destination must not be a symbolic link"
|
|
34
|
+
);
|
|
35
|
+
const from = await canonical(resolve(source)), to = await canonical(resolve(destination));
|
|
36
|
+
if (from !== to && (to.startsWith(`${from}${sep}`) || from.startsWith(`${to}${sep}`)))
|
|
37
|
+
throw new StageError(
|
|
38
|
+
400,
|
|
39
|
+
"unsafe_destination",
|
|
40
|
+
"Artifact destination must not overlap the stage directory"
|
|
41
|
+
);
|
|
42
|
+
if (await stat(join(to, "run.json")))
|
|
43
|
+
throw new StageError(
|
|
44
|
+
400,
|
|
45
|
+
"reserved_destination",
|
|
46
|
+
"Flush artifacts into the run's stage subdirectory"
|
|
47
|
+
);
|
|
48
|
+
await safeDirectory(to, to);
|
|
49
|
+
const files = [];
|
|
50
|
+
async function collect(path) {
|
|
51
|
+
const info = await stat(path);
|
|
52
|
+
if (!info) return;
|
|
53
|
+
if (info.isSymbolicLink())
|
|
54
|
+
throw new StageError(400, "unsafe_artifact", "Artifact symbolic links are not allowed");
|
|
55
|
+
if (info.isDirectory()) {
|
|
56
|
+
for (const entry of await readdir(path)) await collect(join(path, entry));
|
|
57
|
+
} else if (info.isFile()) {
|
|
58
|
+
if (!path.endsWith(".tmp")) files.push(path);
|
|
59
|
+
} else throw new StageError(400, "unsafe_artifact", "Unsupported artifact file");
|
|
60
|
+
}
|
|
61
|
+
for (const name of ["manifest.json", "events.ndjson", "views", "assets", "shots"])
|
|
62
|
+
await collect(join(from, name));
|
|
63
|
+
const artifacts = [];
|
|
64
|
+
for (const path of files.sort()) {
|
|
65
|
+
const name = relative(from, path).split(sep).join("/");
|
|
66
|
+
const target = join(to, name);
|
|
67
|
+
const data = await readFile(path);
|
|
68
|
+
await safeDirectory(dirname(target), to);
|
|
69
|
+
if (from !== to) {
|
|
70
|
+
const existing = await stat(target);
|
|
71
|
+
if (existing) {
|
|
72
|
+
if (!existing.isFile() || existing.isSymbolicLink())
|
|
73
|
+
throw new StageError(
|
|
74
|
+
409,
|
|
75
|
+
"artifact_exists",
|
|
76
|
+
"Artifact destination is not a regular file"
|
|
77
|
+
);
|
|
78
|
+
const previous = await readFile(target);
|
|
79
|
+
if (sha256(previous) !== sha256(data)) {
|
|
80
|
+
if (name !== "events.ndjson" || !data.subarray(0, previous.length).equals(previous))
|
|
81
|
+
throw new StageError(
|
|
82
|
+
409,
|
|
83
|
+
"artifact_exists",
|
|
84
|
+
"Artifact destination already contains different data"
|
|
85
|
+
);
|
|
86
|
+
writeAtomic(target, data);
|
|
87
|
+
}
|
|
88
|
+
} else await writeFile(target, data, { flag: "wx", mode: 384 });
|
|
89
|
+
}
|
|
90
|
+
artifacts.push({
|
|
91
|
+
path: name,
|
|
92
|
+
bytes: data.length,
|
|
93
|
+
contentType: name.endsWith(".gz") ? "application/gzip" : name.endsWith(".png") ? "image/png" : name.endsWith(".ndjson") ? "application/x-ndjson" : name.endsWith(".json") ? "application/json" : "application/octet-stream"
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return artifacts;
|
|
97
|
+
}
|
|
98
|
+
export {
|
|
99
|
+
flushReplayArtifacts
|
|
100
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Response } from "playwright";
|
|
2
|
+
import { type ReplayAssets } from "@open-qa/qa-engine/protocol";
|
|
3
|
+
import type { ReplayPrivacy } from "./replay-privacy.js";
|
|
4
|
+
import { ReplayWarnings } from "./replay-warnings.js";
|
|
5
|
+
export declare class ReplayAssetCache {
|
|
6
|
+
private readonly dir;
|
|
7
|
+
private readonly privacy;
|
|
8
|
+
readonly assets: ReplayAssets;
|
|
9
|
+
readonly warnings: ReplayWarnings;
|
|
10
|
+
private readonly pending;
|
|
11
|
+
private readonly queue;
|
|
12
|
+
private totalBytes;
|
|
13
|
+
private captures;
|
|
14
|
+
private entries;
|
|
15
|
+
private readonly hashes;
|
|
16
|
+
constructor(dir: string, privacy: ReplayPrivacy);
|
|
17
|
+
capture(response: Response): void;
|
|
18
|
+
private pump;
|
|
19
|
+
private save;
|
|
20
|
+
flush(): Promise<void>;
|
|
21
|
+
}
|