screw-bugs 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +83 -13
  2. package/dist/browser-playwright/launch.js +27 -10
  3. package/dist/browser-playwright/page.d.ts +50 -28
  4. package/dist/browser-playwright/page.js +101 -91
  5. package/dist/browser-playwright/recorder.d.ts +154 -0
  6. package/dist/browser-playwright/recorder.js +382 -0
  7. package/dist/build-info.json +3 -3
  8. package/dist/cli/commands.js +56 -11
  9. package/dist/cli/mcp.d.ts +11 -4
  10. package/dist/cli/mcp.js +86 -23
  11. package/dist/cli/operations.d.ts +30 -2
  12. package/dist/cli/operations.js +126 -20
  13. package/dist/cli/report-links.js +2 -2
  14. package/dist/cli/report-server.d.ts +41 -9
  15. package/dist/cli/report-server.js +139 -23
  16. package/dist/cli/share.d.ts +71 -4
  17. package/dist/cli/share.js +540 -109
  18. package/dist/cli/sign-in.d.ts +22 -10
  19. package/dist/cli/sign-in.js +23 -23
  20. package/dist/core/engine.js +54 -13
  21. package/dist/core/liveness.js +3 -1
  22. package/dist/core/services.d.ts +29 -4
  23. package/dist/core/share.d.ts +31 -0
  24. package/dist/core/share.js +139 -0
  25. package/dist/core/steps.d.ts +5 -0
  26. package/dist/core/steps.js +15 -1
  27. package/dist/core/video.d.ts +6 -0
  28. package/dist/core/video.js +8 -0
  29. package/dist/report/html.d.ts +4 -2
  30. package/dist/report/html.js +36 -13
  31. package/dist/report/icons.d.ts +3 -0
  32. package/dist/report/icons.js +5 -1
  33. package/dist/report/index.d.ts +3 -1
  34. package/dist/report/index.js +3 -1
  35. package/dist/report/live.d.ts +7 -0
  36. package/dist/report/live.js +135 -0
  37. package/dist/report/menu-bar.d.ts +3 -3
  38. package/dist/report/menu-bar.js +3 -3
  39. package/dist/report/player.d.ts +3 -3
  40. package/dist/report/player.js +3 -3
  41. package/dist/report/share-page.d.ts +48 -0
  42. package/dist/report/share-page.js +291 -0
  43. package/dist/report/styles.d.ts +2 -0
  44. package/dist/report/styles.js +2 -2
  45. package/dist/report/view.js +2 -0
  46. package/dist/report/walkthrough.d.ts +5 -2
  47. package/dist/report/walkthrough.js +17 -5
  48. package/dist/schema/accounts.js +10 -1
  49. package/dist/schema/evidence.d.ts +15 -8
  50. package/dist/schema/evidence.js +9 -4
  51. package/dist/schema/findings.js +1 -1
  52. package/dist/schema/job.d.ts +205 -65
  53. package/dist/schema/job.js +63 -11
  54. package/dist/schema/ledger.d.ts +80 -16
  55. package/dist/schema/ledger.js +36 -0
  56. package/dist/schema/share.d.ts +17 -0
  57. package/dist/schema/share.js +22 -0
  58. package/package.json +1 -1
@@ -17,8 +17,8 @@ export const NAVIGATION_TIMEOUT_MS = DEFAULT_LOAD_MS;
17
17
  export const EXPECT_TIMEOUT_MS = DEFAULT_WAIT_MS;
18
18
  export const SCREENSHOT_TIMEOUT_MS = 10_000;
19
19
  /**
20
- * How long a click or key press waits after it is dispatched, so that a navigation it causes
21
- * starts while it is still the current step, such as a form that the Enter key submits.
20
+ * How long a click, hover or key press waits after it is dispatched, so that a navigation it
21
+ * causes starts while it is still the current step, such as a form that the Enter key submits.
22
22
  */
23
23
  export const NAVIGATION_START_MS = 250;
24
24
  const seconds = (ms) => `${ms / 1000} s`;
@@ -31,16 +31,28 @@ export const MOBILE_DEVICE = 'iPhone 17';
31
31
  /** The device as reports show it; the engine is named because mobile is not real Safari. */
32
32
  export const deviceLabel = (device) => device === 'mobile' ? `${MOBILE_DEVICE} (Chromium)` : 'Desktop';
33
33
  const contextOptions = (device) => device === 'mobile' ? devices[MOBILE_DEVICE] : { viewport: { width: 1280, height: 800 } };
34
+ /** The device pixels per CSS pixel `device` is shown at (3 on the phone). */
35
+ export const deviceScale = (device) => {
36
+ const options = contextOptions(device);
37
+ return 'deviceScaleFactor' in options ? options.deviceScaleFactor : 1;
38
+ };
39
+ /**
40
+ * Chromium's flags for a browser whose sessions are on `device`: one above 1x renders at its
41
+ * device pixels (`--force-device-scale-factor`), so the screencast, which sends frames at the
42
+ * browser's own scale and ignores the emulated one, records the phone at full resolution. The
43
+ * emulated scale stays what pages see (`devicePixelRatio`) and what screenshots are taken at.
44
+ */
45
+ export const launchArgs = (device) => {
46
+ const scale = deviceScale(device);
47
+ return scale > 1 ? [`--force-device-scale-factor=${scale}`] : [];
48
+ };
34
49
  /**
35
- * Session videos fit in this box, keeping the viewport's shape. It is larger than every viewport
36
- * we set (desktop 1280x800), so videos are recorded 1:1: scaling frames down (as a 1280x720 box
37
- * once did, to 1152x720) resamples every glyph and blurs text. Chromium's screencast sends frames
38
- * in CSS pixels, so a box larger than the viewport would only pad the frame, not sharpen it.
50
+ * The size of a session's video: the viewport at the scale the browser renders at (`launchArgs`),
51
+ * the size of the screencast's frames, so a video is never scaled. Each side is rounded down to
52
+ * an even number, as video needs: an odd last row or column of the page (an iPhone 17's 2043rd
53
+ * row at 3x) is left out (see `ffmpegArgs`).
39
54
  */
40
- export const VIDEO_MAX = { width: 1920, height: 1080 };
41
- /** The size of a session's video for a viewport: the viewport, scaled down into `VIDEO_MAX`. */
42
- export const videoSize = (viewport) => {
43
- const scale = Math.min(1, VIDEO_MAX.width / viewport.width, VIDEO_MAX.height / viewport.height);
55
+ export const videoSize = (viewport, scale) => {
44
56
  const even = (n) => Math.max(2, Math.floor((n * scale) / 2) * 2);
45
57
  return { width: even(viewport.width), height: even(viewport.height) };
46
58
  };
@@ -954,7 +966,9 @@ const MeasuredOnScreen = Schema.Struct({
954
966
  const FILE_INPUT = 'input[type=file]';
955
967
  const pageOperations = (page, watch, input,
956
968
  /** How the page is drawn, for placing a press in the video (see `framePoint`). */
957
- drawn) => {
969
+ drawn,
970
+ /** Keeps what a screenshot does to the page's rendering off the video (`Recorder.hold`). */
971
+ hold) => {
958
972
  const { settle, refused } = watch;
959
973
  /** Finds the first visible match, or fails with a plain "not found". */
960
974
  const element = (target, timeout) => Effect.gen(function* () {
@@ -1317,6 +1331,19 @@ drawn) => {
1317
1331
  : firstLine(error)));
1318
1332
  return yield* tracedAfterPause(began, dispatched, true);
1319
1333
  }),
1334
+ hover: (target, wait) => Effect.gen(function* () {
1335
+ const timeout = wait?.timeoutMs ?? ACTION_TIMEOUT_MS;
1336
+ const locator = yield* element(target, timeout);
1337
+ // Not watched: a page can make trusted mouse moves happen itself (see
1338
+ // `StepAction.atMs`), so a hover records no time or point.
1339
+ yield* attempt(() => locator.hover({ timeout }), (error) => isTimeout(error)
1340
+ ? `The element was found but the mouse could not be moved over it within ${seconds(timeout)}.`
1341
+ : firstLine(error));
1342
+ // As after a click: a navigation the hover starts (a `mouseenter` handler's) starts while
1343
+ // it is still the current step, so its load is judged as the hover's.
1344
+ yield* Effect.sleep(NAVIGATION_START_MS);
1345
+ return {};
1346
+ }),
1320
1347
  fill: (target, value, wait) => Effect.gen(function* () {
1321
1348
  const timeout = wait?.timeoutMs ?? ACTION_TIMEOUT_MS;
1322
1349
  const locator = yield* element(target, timeout);
@@ -1802,7 +1829,7 @@ drawn) => {
1802
1829
  // page's own were (`seen`), or the page was zoomed meanwhile and the boxes cannot be placed.
1803
1830
  const scaleNow = seen === undefined ? undefined : yield* visualScaleNow;
1804
1831
  const visual = visualOf(seen, scaleNow, fullPage);
1805
- const bytes = yield* attempt(() => page.screenshot({ path, fullPage, timeout: SCREENSHOT_TIMEOUT_MS }), (error) => `Could not take a screenshot: ${firstLine(error)}`);
1832
+ const bytes = yield* hold(attempt(() => page.screenshot({ path, fullPage, timeout: SCREENSHOT_TIMEOUT_MS }), (error) => `Could not take a screenshot: ${firstLine(error)}`));
1806
1833
  const { width, height } = pngSize(bytes);
1807
1834
  const scale = state?.scale ?? (viewport ? width / viewport.width : 1);
1808
1835
  const [dx, dy] = fullPage && state ? [state.scrollX, state.scrollY] : [0, 0];
@@ -1994,59 +2021,50 @@ export const FIRST_FRAME_TIMEOUT_MS = 2_000;
1994
2021
  /** How often the blank tab is repainted until its video has a first frame. */
1995
2022
  const REPAINT_EVERY_MS = 50;
1996
2023
  /**
1997
- * When a recorded tab's video starts, in epoch ms; undefined when no frame came in time.
2024
+ * When a recorded tab's video starts, in epoch ms (`first`, the recorder's first frame);
2025
+ * undefined when no frame came in time.
1998
2026
  *
1999
- * Playwright's video starts at the first frame the browser sends, not when the tab opens, and
2000
- * every frame sits on the video at its paint time from there. Steps are timed from the video's
2001
- * start, so it has to be known. The blank tab's own paint often comes before the browser starts
2002
- * sending frames; then the first frame was the first page's, after its load, and every step came
2003
- * up on the video early by that long: a field filled on the video while the walkthrough still
2004
- * played the step before. So the blank tab is repainted (white and almost white, by turns) until
2005
- * a frame comes, before any page of the run loads; a repaint that only arrives once a page has
2006
- * loaded leaves it alone. A listener that starts after a frame is handed the latest one at once,
2007
- * which for a blank tab is its first. Frame times are the browser's wall clock, the one `Clock`
2008
- * reads in a run. The listener is let go of when this ends, timed out or not.
2027
+ * The video starts at the first frame the browser sends, not when the tab opens, and every
2028
+ * frame sits on the video at its paint time from there. Steps are timed from the video's start,
2029
+ * so it has to be known. The blank tab's own paint often comes before the browser starts sending
2030
+ * frames; then the first frame was the first page's, after its load, and every step came up on
2031
+ * the video early by that long: a field filled on the video while the walkthrough still played
2032
+ * the step before. So the blank tab is repainted (white and almost white, by turns) until a frame
2033
+ * comes, before any page of the run loads; a repaint that only arrives once a page has loaded
2034
+ * leaves it alone. Frame times are the browser's wall clock, the one `Clock` reads in a run.
2009
2035
  */
2010
- export const firstFrameAt = (page) => Effect.gen(function* () {
2011
- let frame;
2012
- let seen = () => undefined;
2013
- // Boundary: Playwright hands frames to a callback.
2014
- const first = new Promise((resolve) => {
2015
- seen = resolve;
2016
- });
2017
- yield* Effect.acquireRelease(attempt(() => page.screencast.start({
2018
- onFrame: ({ timestamp }) => {
2019
- frame ??= timestamp;
2020
- seen();
2021
- },
2022
- }), firstLine), (listening) => attempt(() => listening.dispose(), firstLine).pipe(Effect.timeoutOption(ACTION_TIMEOUT_MS), Effect.ignore));
2023
- for (let turn = 0; frame === undefined; turn++) {
2036
+ export const firstFrameAt = (page, first) => Effect.gen(function* () {
2037
+ for (let turn = 0;; turn++) {
2024
2038
  const color = turn % 2 === 1 ? '#fff' : '#fefefe';
2025
2039
  yield* attempt(() => page.evaluate(`location.href === 'about:blank' && (document.documentElement.style.backgroundColor = '${color}')`), firstLine).pipe(Effect.ignore);
2026
- yield* Effect.promise(() => first).pipe(Effect.timeoutOption(REPAINT_EVERY_MS));
2040
+ const frame = yield* first.pipe(Effect.timeoutOption(REPAINT_EVERY_MS));
2041
+ if (frame._tag === 'Some')
2042
+ return frame.value;
2027
2043
  }
2028
- return frame;
2029
- }).pipe(Effect.scoped, Effect.timeoutOption(FIRST_FRAME_TIMEOUT_MS), Effect.map((first) => (first._tag === 'Some' ? first.value : undefined)), Effect.orElseSucceed(() => undefined));
2030
- /**
2031
- * One session: a fresh context (no cookies or storage from other runs) with one tab that every
2032
- * case of the session runs in, recorded as one continuous video. Tabs and windows a case opens
2033
- * are in the same context and are closed before the next case. Closed with its scope; the video
2034
- * is saved then too, so a run that is stopped keeps it.
2035
- *
2036
- * Playwright records each tab's video separately, so only the session's own tab is kept: the
2037
- * videos of tabs the cases opened are deleted. Recording needs Playwright's ffmpeg: a session
2038
- * with `video` that cannot be recorded fails rather than run without one.
2039
- */
2044
+ }).pipe(Effect.timeoutOption(FIRST_FRAME_TIMEOUT_MS), Effect.map((first) => (first._tag === 'Some' ? first.value : undefined)));
2040
2045
  /** A saved storage state as Playwright takes it; its cookies and origins are Playwright's own. */
2041
2046
  const StorageStateShape = Schema.Struct({
2042
2047
  cookies: Schema.Array(Schema.Unknown),
2043
2048
  origins: Schema.Array(Schema.Unknown),
2044
2049
  });
2050
+ /**
2051
+ * One session: a fresh context (no cookies or storage from other runs) with one tab that every
2052
+ * case of the session runs in, recorded as one continuous video. Tabs and windows a case opens
2053
+ * are in the same context and are closed before the next case; only the session's own tab is
2054
+ * recorded. Closed with its scope; the video is saved then too, so a run that is stopped keeps
2055
+ * it.
2056
+ *
2057
+ * The video is recorded by screw-bugs (`startRecorder`), not Playwright's `recordVideo`, so that
2058
+ * a step's screenshot never shows on it (see `recorder.ts`). Recording needs Playwright's ffmpeg:
2059
+ * a session with `video` that cannot be recorded fails rather than run without one.
2060
+ */
2045
2061
  export const newSession = (browser, device, video,
2046
2062
  /** Whether to watch the input the page gets, for recording actions; for tests to compare. */
2047
2063
  observed = true,
2048
2064
  /** A saved session (an account's storage state, as JSON text) the context starts from. */
2049
- savedSession) => Effect.gen(function* () {
2065
+ savedSession,
2066
+ /** The scale `browser` renders at (see `launchArgs`): its screencast frames, so its videos. */
2067
+ renderScale = 1) => Effect.gen(function* () {
2050
2068
  const clock = yield* Effect.clockWith((clock) => Effect.succeed(clock));
2051
2069
  const options = contextOptions(device);
2052
2070
  const failed = (error) => `Could not open a new browser tab: ${firstLine(error)}`;
@@ -2054,34 +2072,38 @@ savedSession) => Effect.gen(function* () {
2054
2072
  const storageState = savedSession === undefined
2055
2073
  ? undefined
2056
2074
  : yield* Schema.decodeUnknownEffect(Schema.fromJsonString(StorageStateShape))(savedSession).pipe(Effect.mapError((cause) => new PageActionFailed({ message: 'The saved session is not valid.', cause })));
2057
- /** A context with its tab; with `record`, both are recorded. Boundary: Playwright promises. */
2058
- const open = (record) => Effect.acquireRelease(attempt(() => browser.newContext({
2075
+ /** A context with its tab. Boundary: Playwright promises. */
2076
+ const { context, page } = yield* Effect.acquireRelease(attempt(() => browser.newContext({
2059
2077
  ...options,
2060
2078
  ...(storageState !== undefined
2061
2079
  ? {
2062
2080
  storageState: storageState,
2063
2081
  }
2064
2082
  : {}),
2065
- ...(record && video !== undefined
2066
- ? { recordVideo: { dir: video.dir, size: videoSize(options.viewport) } }
2067
- : {}),
2068
2083
  }), failed),
2069
2084
  // Only reached when opening it went wrong, or once the session closed it already
2070
- // (`closeAll`): a failure to close it again cannot change what the session found.
2085
+ // (`close`): a failure to close it again cannot change what the session found.
2071
2086
  (context) => attempt(() => context.close(), firstLine).pipe(Effect.ignore)).pipe(Effect.flatMap((context) => attempt(() => context.newPage(), failed).pipe(Effect.map((page) => ({ context, page })))));
2072
2087
  // Video is not optional: a session that cannot be recorded does not run.
2073
- const { context, page } = yield* video === undefined
2074
- ? open(false)
2075
- : open(true).pipe(Effect.mapError((error) => new PageActionFailed({
2076
- message: `The session could not be recorded as video (run \`npx screw-bugs doctor\` to see what is missing): ${error.message}`,
2088
+ const recorder = video === undefined
2089
+ ? undefined
2090
+ : yield* video.record(page, videoSize(options.viewport, renderScale)).pipe(Effect.mapError((error) => new PageActionFailed({
2091
+ message: `The session could not be recorded as video (run \`npx screw-bugs doctor\` to see what is missing): ${firstLine(error)}`,
2092
+ cause: error,
2077
2093
  })));
2078
2094
  const tabOpenedAt = clock.currentTimeMillisUnsafe();
2079
- const videoStartedAt = video === undefined ? tabOpenedAt : ((yield* firstFrameAt(page)) ?? tabOpenedAt);
2095
+ const videoStartedAt = recorder === undefined
2096
+ ? tabOpenedAt
2097
+ : ((yield* firstFrameAt(page, recorder.firstFrame)) ?? tabOpenedAt);
2098
+ // An ffmpeg that cannot write the video ends once it has a frame: nothing runs unrecorded.
2099
+ if (recorder !== undefined)
2100
+ yield* recorder.ready.pipe(Effect.mapError((error) => new PageActionFailed({
2101
+ message: `The session could not be recorded as video (run \`npx screw-bugs doctor\` to see what is missing): ${error.message}`,
2102
+ cause: error,
2103
+ })));
2080
2104
  // Boundary: Playwright reports crashes and closes through event callbacks.
2081
2105
  let closing = false;
2082
2106
  let crashed;
2083
- const opened = [];
2084
- context.on('page', (other) => opened.push(other));
2085
2107
  page.on('crash', () => {
2086
2108
  crashed ??= 'the page crashed.';
2087
2109
  });
@@ -2093,33 +2115,14 @@ savedSession) => Effect.gen(function* () {
2093
2115
  if (!closing)
2094
2116
  crashed ??= 'the browser closed.';
2095
2117
  });
2096
- /** Closes the context, then saves the session's video and deletes the other tabs' videos. */
2097
- let closed;
2098
- const closeAll = () => (closed ??= (async () => {
2118
+ /** Closes the context, then ends and saves the session's video. */
2119
+ const closeAll = yield* Effect.cached(Effect.gen(function* () {
2099
2120
  closing = true;
2100
- await context.close().catch(() => undefined);
2101
- const recorded = page.video();
2102
- if (video === undefined)
2103
- return undefined;
2104
- if (recorded === null)
2105
- return 'The session was not recorded.';
2106
- try {
2107
- await recorded.saveAs(video.path);
2108
- await recorded.delete();
2109
- }
2110
- catch (error) {
2111
- return `The video could not be saved: ${firstLine(error)}`;
2112
- }
2113
- for (const other of opened)
2114
- if (other !== page)
2115
- await other
2116
- .video()
2117
- ?.delete()
2118
- .catch(() => undefined);
2119
- return undefined;
2120
- })());
2121
+ yield* attempt(() => context.close(), firstLine).pipe(Effect.ignore);
2122
+ return recorder === undefined ? undefined : yield* recorder.finish;
2123
+ }));
2121
2124
  // Closing never fails the session: what went wrong is said about the video, which it is for.
2122
- const close = attempt(closeAll, (error) => `The video could not be saved: ${firstLine(error)}`).pipe(Effect.catchTag('PageActionFailed', (error) => Effect.succeed(video === undefined ? undefined : error.message)), Effect.timeoutOption(CLOSE_TIMEOUT_MS), Effect.map((problem) => ({
2125
+ const close = closeAll.pipe(Effect.timeoutOption(CLOSE_TIMEOUT_MS), Effect.map((problem) => ({
2123
2126
  videoProblem: problem._tag === 'Some'
2124
2127
  ? problem.value
2125
2128
  : `The video was not saved within ${seconds(CLOSE_TIMEOUT_MS)}.`,
@@ -2133,13 +2136,20 @@ savedSession) => Effect.gen(function* () {
2133
2136
  atOrigin: device === 'mobile' && observed ? yield* widgetAtOrigin(browser) : false,
2134
2137
  };
2135
2138
  return {
2136
- page: pageOperations(page, watch, input, drawn),
2139
+ page: pageOperations(page, watch, input, drawn,
2140
+ // Only a capture in device pixels disturbs the screencast, also in a browser rendering at
2141
+ // that scale: one at 1x (a desktop's) sends the page as it is, and holding would only
2142
+ // drop frames of what it does meanwhile.
2143
+ recorder !== undefined && deviceScale(device) > 1 ? recorder.hold : (effect) => effect),
2137
2144
  beginCase: (options) => Effect.tryPromise(async () => {
2138
2145
  for (const other of context.pages())
2139
2146
  if (other !== page)
2140
2147
  await other.close();
2141
2148
  }).pipe(Effect.timeoutOption(ACTION_TIMEOUT_MS), Effect.ignore, Effect.andThen(Effect.sync(() => watch.reset(options?.continues === true)))),
2142
- crashed: Effect.sync(() => crashed),
2149
+ // A recording that stopped ends the session as a crash does: it does not go on unrecorded.
2150
+ crashed: Effect.suspend(() => crashed !== undefined || recorder === undefined
2151
+ ? Effect.succeed(crashed)
2152
+ : recorder.stopped),
2143
2153
  videoStartedAt,
2144
2154
  close,
2145
2155
  };
@@ -0,0 +1,154 @@
1
+ import { Effect, FileSystem } from 'effect';
2
+ import { ChildProcessSpawner } from 'effect/process';
3
+ import type { Page } from 'playwright-core';
4
+ /**
5
+ * A session's video, recorded by screw-bugs itself rather than by Playwright's `recordVideo`:
6
+ * the tab's screencast frames (JPEG, from `page.screencast`) go to Playwright's ffmpeg as
7
+ * Matroska on its stdin, which writes VP9 WebM at a constant frame rate, as Playwright's own
8
+ * recorder does (same container, encoder settings, frame times and last frame).
9
+ *
10
+ * Two frames are left out, so the video only ever shows the page as it renders on its own:
11
+ *
12
+ * - A frame that does not fit the video's size (`fits`). Chromium sends frames at the browser's
13
+ * own scale whatever size is asked for, except around a device-pixel capture on an emulated
14
+ * phone in a browser at 1x, where one may come at full device size (3x); Playwright's recorder
15
+ * crops it into the video, which shows the page's top left corner magnified for one frame.
16
+ * - A frame painted while a screenshot is taken (`hold`). Every device-pixel capture (the step
17
+ * screenshots) changes how the page renders for a moment: on a phone, text comes out crisper
18
+ * and darker, then pale again, a flicker at every step. Such a frame is painted during the
19
+ * capture and often only arrives after it, so frames are judged by when they were painted.
20
+ *
21
+ * The video then holds the last frame it kept, as for any frame the browser does not send.
22
+ */
23
+ /**
24
+ * The most frames a second the video has (Playwright's recorder's rate). Unlike Playwright's, it
25
+ * has only the frames the page changed in (the screencast sends one only then), so a still page
26
+ * costs next to nothing to encode and store (`framePacer`).
27
+ */
28
+ export declare const VIDEO_FPS = 25;
29
+ /**
30
+ * How much smaller than the video a frame may be (Chromium rounds the screencast's size). One a
31
+ * pixel larger is the page's odd last row or column, which the video leaves out (`videoSize`).
32
+ */
33
+ export declare const FRAME_SLACK = 4;
34
+ /** How long the video shows its last frame at least, after it came (as in Playwright). */
35
+ export declare const LAST_FRAME_MS = 1000;
36
+ /**
37
+ * How many frames may wait for ffmpeg (seconds of video at `VIDEO_FPS`, some MB of JPEG). While
38
+ * it is that far behind, new frames are dropped and the video holds the last one it has.
39
+ */
40
+ export declare const BACKLOG_FRAMES: number;
41
+ /** How long ffmpeg may take, after the first frame, to open the video it writes. */
42
+ export declare const READY_TIMEOUT_MS = 3000;
43
+ export interface FrameSize {
44
+ readonly width: number;
45
+ readonly height: number;
46
+ }
47
+ /** A JPEG's size, from its first start-of-frame segment; undefined when it has none. */
48
+ export declare const jpegSize: (bytes: Uint8Array) => FrameSize | undefined;
49
+ /** Whether a frame of `frame`'s size shows the page whole in a video of `video`'s. */
50
+ export declare const fits: (frame: FrameSize, video: FrameSize) => boolean;
51
+ /**
52
+ * Which frames go on the video, and where: a frame's time on the video is its paint time (epoch
53
+ * ms) after the first kept frame's, so the video starts at its first frame, as Playwright's does.
54
+ */
55
+ export declare const frameGate: (video: FrameSize) => {
56
+ /** A screenshot starts being taken `at`; call the result when it was taken. */
57
+ hold: (at: number) => (end: number) => void;
58
+ /**
59
+ * The frame's time on the video in ms when it is kept, else undefined: when it does not fit
60
+ * (`size` undefined is a frame that could not be read), was painted while a screenshot was
61
+ * taken, or does not come after the last kept frame (the video's frames only go forward).
62
+ */
63
+ admit: (size: FrameSize | undefined, timestamp: number) => number | undefined;
64
+ /** When the first kept frame was painted, in epoch ms. */
65
+ first: () => number | undefined;
66
+ /** The time on the video of a frame painted at `timestamp`. */
67
+ at: (timestamp: number) => number;
68
+ };
69
+ export type FrameGate = ReturnType<typeof frameGate>;
70
+ /**
71
+ * When the video's last frame goes again, to end it: as Playwright does, at least `LAST_FRAME_MS`
72
+ * after it was painted, or as long after as the session went on without a new one.
73
+ */
74
+ export declare const lastFrameTime: (paintedAt: number, sinceItCameMs: number) => number;
75
+ /**
76
+ * At most one frame for each `1000 / VIDEO_FPS` ms of the video, never losing the page's latest
77
+ * state: the first frame in a slot after the last written one is written at once; a later one in
78
+ * that slot waits, replacing the one waiting, and is written at the start of the next slot when a
79
+ * frame comes after that slot, or at the end (`flush`). `ms` (time on the video) only grows.
80
+ */
81
+ export declare const framePacer: <F>() => {
82
+ /** What to write now, in order, for a frame at `ms` on the video. */
83
+ add: (frame: F, ms: number) => Array<{
84
+ readonly frame: F;
85
+ readonly ms: number;
86
+ }>;
87
+ /** The frame still waiting, if one is, to write before the video ends. */
88
+ flush: () => {
89
+ frame: F;
90
+ ms: number;
91
+ }[];
92
+ };
93
+ /** EBML's variable-length size. */
94
+ export declare const vint: (value: number) => Uint8Array;
95
+ /** The stream's start: its EBML header, a segment of unknown size, its info and its track. */
96
+ export declare const matroskaHeader: () => Uint8Array;
97
+ /** One frame: a cluster at `ms` holding it as a key frame of track 1. */
98
+ export declare const matroskaFrame: (ms: number, jpeg: Uint8Array) => Uint8Array;
99
+ /** Playwright's ffmpeg arguments for its recorder, writing WebM to `file`. */
100
+ export declare const ffmpegArgs: (size: FrameSize, file: string) => ReadonlyArray<string>;
101
+ export interface Recorder {
102
+ /** When the first frame on the video was painted, in epoch ms; waits for it. */
103
+ readonly firstFrame: Effect.Effect<number>;
104
+ /** Runs `effect` (a screenshot) keeping the frames painted meanwhile off the video. */
105
+ readonly hold: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
106
+ /**
107
+ * Ends the video and saves it at its path: why it could not be, else undefined. Call once the
108
+ * tab is closed, so no frame comes after. Only the first call ends it; later ones say the same.
109
+ */
110
+ readonly finish: Effect.Effect<string | undefined>;
111
+ /**
112
+ * Waits, after the first frame (or once there is none), until ffmpeg has opened the video it
113
+ * writes, for at most `READY_TIMEOUT_MS`; fails when ffmpeg has ended instead, or has not
114
+ * opened it by then (no frame came, or it is stuck).
115
+ */
116
+ readonly ready: Effect.Effect<void, Error>;
117
+ /** Why the video stopped being recorded before `finish`, once ffmpeg ended early. */
118
+ readonly stopped: Effect.Effect<string | undefined>;
119
+ }
120
+ /**
121
+ * Records `page`'s video into `file` (a `.webm`) with the ffmpeg at `ffmpeg`, at `size` (see
122
+ * `videoSize`). Written to a file beside it first and moved there by `finish`, so a run whose
123
+ * process dies leaves no half-written video where a video is looked for. Fails when ffmpeg or
124
+ * the screencast cannot start; with the scope, ffmpeg and the screencast are stopped.
125
+ */
126
+ export declare const startRecorder: (page: Page, options: {
127
+ readonly ffmpeg: string;
128
+ readonly file: string;
129
+ readonly size: FrameSize;
130
+ }) => Effect.Effect<{
131
+ firstFrame: Effect.Effect<number, never, never>;
132
+ hold: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
133
+ finish: Effect.Effect<string | undefined, never, never>;
134
+ ready: Effect.Effect<undefined, Error, never>;
135
+ stopped: Effect.Effect<string | undefined, never, never>;
136
+ }, Error | import("effect/PlatformError").PlatformError, ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | import("effect/Scope").Scope>;
137
+ /**
138
+ * How a session with a video at `path` (in the folder `dir`, made when missing) is recorded with
139
+ * the ffmpeg at `ffmpeg`, on the platform this runs on.
140
+ */
141
+ export declare const videoRecording: (video: {
142
+ readonly dir: string;
143
+ readonly path: string;
144
+ readonly ffmpeg: string;
145
+ }) => Effect.Effect<{
146
+ path: string;
147
+ record: (page: Page, size: FrameSize) => Effect.Effect<{
148
+ firstFrame: Effect.Effect<number, never, never>;
149
+ hold: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
150
+ finish: Effect.Effect<string | undefined, never, never>;
151
+ ready: Effect.Effect<undefined, Error, never>;
152
+ stopped: Effect.Effect<string | undefined, never, never>;
153
+ }, Error | import("effect/PlatformError").PlatformError, import("effect/Scope").Scope>;
154
+ }, never, ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem>;