effect-browser 0.3.0-beta.20 → 0.3.0-beta.22

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 (243) hide show
  1. package/README.md +221 -158
  2. package/dist/Action-24sLOubi.d.mts +110 -0
  3. package/dist/Action.d.mts +2 -0
  4. package/dist/Action.mjs +141 -0
  5. package/dist/Action.mjs.map +1 -0
  6. package/dist/ActionPort.d.mts +15 -0
  7. package/dist/ActionPort.mjs +101 -0
  8. package/dist/ActionPort.mjs.map +1 -0
  9. package/dist/Browser-Cq2K1FEA.mjs +4075 -0
  10. package/dist/Browser-Cq2K1FEA.mjs.map +1 -0
  11. package/dist/Browser-wZhwZSlG.d.mts +167 -0
  12. package/dist/Browser.d.mts +2 -129
  13. package/dist/Browser.mjs +1 -1
  14. package/dist/{BrowserError-Coy7EW1N.d.mts → BrowserError-BlcmLVzn.d.mts} +22 -7
  15. package/dist/BrowserError.d.mts +2 -2
  16. package/dist/BrowserError.mjs +46 -9
  17. package/dist/BrowserError.mjs.map +1 -1
  18. package/dist/{BrowserEvent-dq6FRT8w.d.mts → BrowserEvent-lLqJ3GJS.d.mts} +93 -37
  19. package/dist/BrowserEvent.d.mts +2 -2
  20. package/dist/BrowserEvent.mjs +29 -24
  21. package/dist/BrowserEvent.mjs.map +1 -1
  22. package/dist/{Change-DqING3tr.d.mts → Change-Ba7INmay.d.mts} +4 -6
  23. package/dist/Change.d.mts +2 -2
  24. package/dist/Change.mjs +97 -2
  25. package/dist/Change.mjs.map +1 -0
  26. package/dist/Clock.d.mts +57 -0
  27. package/dist/Clock.mjs +80 -0
  28. package/dist/Clock.mjs.map +1 -0
  29. package/dist/{Frame-DkNdjjjr.d.mts → Frame-UEH2hO5T.d.mts} +10 -3
  30. package/dist/Frame.d.mts +2 -2
  31. package/dist/Frame.mjs +10 -2
  32. package/dist/Frame.mjs.map +1 -1
  33. package/dist/{Motion-DND6I-64.d.mts → Geometry-DJx9qrYy.d.mts} +7 -10
  34. package/dist/Geometry.d.mts +2 -0
  35. package/dist/Geometry.mjs +32 -0
  36. package/dist/Geometry.mjs.map +1 -0
  37. package/dist/LICENSE-jpeg-js +28 -0
  38. package/dist/Matching.d.mts +37 -0
  39. package/dist/{match.inpage-xOe9LNO0.mjs → Matching.mjs} +28 -4
  40. package/dist/Matching.mjs.map +1 -0
  41. package/dist/{Page-CfB6Zn1p.mjs → Page-BF5izr8R.mjs} +216 -31
  42. package/dist/Page-BF5izr8R.mjs.map +1 -0
  43. package/dist/{Page-ku6j5t-v.d.mts → Page-CvsOEorc.d.mts} +158 -185
  44. package/dist/Page.d.mts +2 -2
  45. package/dist/Page.mjs +1 -1
  46. package/dist/Released-tURbFmal.d.mts +15 -0
  47. package/dist/{Snapshot-B34hZ4Jl.d.mts → Snapshot-CtBVOtuu.d.mts} +6 -1
  48. package/dist/Snapshot.d.mts +1 -1
  49. package/dist/Snapshot.mjs.map +1 -1
  50. package/dist/Stillness-CNlvayIo.mjs +136 -0
  51. package/dist/Stillness-CNlvayIo.mjs.map +1 -0
  52. package/dist/Style-AO4V7Czt.mjs +357 -0
  53. package/dist/Style-AO4V7Czt.mjs.map +1 -0
  54. package/dist/Style.d.mts +72 -0
  55. package/dist/Style.mjs +2 -0
  56. package/dist/Url.d.mts +26 -0
  57. package/dist/Url.mjs +74 -0
  58. package/dist/Url.mjs.map +1 -0
  59. package/dist/Window-CMddGMt1.d.mts +34 -0
  60. package/dist/Window.d.mts +2 -0
  61. package/dist/Window.mjs +35 -0
  62. package/dist/Window.mjs.map +1 -0
  63. package/dist/connection-2KHI4_KN.d.mts +41 -0
  64. package/dist/connection-BTeO0Hiz.mjs +68 -0
  65. package/dist/connection-BTeO0Hiz.mjs.map +1 -0
  66. package/dist/index.d.mts +15 -17
  67. package/dist/index.mjs +11 -14
  68. package/dist/moment/Moment.d.mts +86 -0
  69. package/dist/{Moment.mjs → moment/Moment.mjs} +44 -38
  70. package/dist/moment/Moment.mjs.map +1 -0
  71. package/dist/{Plan-DJ4hEEna.d.mts → plan/index.d.mts} +169 -64
  72. package/dist/{Plan-C-d5QYS8.mjs → plan/index.mjs} +122 -155
  73. package/dist/plan/index.mjs.map +1 -0
  74. package/dist/{Policy.d.mts → policy/Policy.d.mts} +5 -9
  75. package/dist/{Policy.mjs → policy/Policy.mjs} +11 -21
  76. package/dist/policy/Policy.mjs.map +1 -0
  77. package/dist/presentation/Motion.d.mts +12 -0
  78. package/dist/{Motion.mjs → presentation/Motion.mjs} +4 -27
  79. package/dist/presentation/Motion.mjs.map +1 -0
  80. package/dist/presentation/Presentation.d.mts +30 -0
  81. package/dist/presentation/Presentation.mjs +105 -0
  82. package/dist/presentation/Presentation.mjs.map +1 -0
  83. package/dist/{Stage.d.mts → presentation/Stage.d.mts} +5 -8
  84. package/dist/{Stage.mjs → presentation/Stage.mjs} +68 -76
  85. package/dist/presentation/Stage.mjs.map +1 -0
  86. package/dist/presentation/feed.d.mts +20 -0
  87. package/dist/presentation/feed.mjs +18 -0
  88. package/dist/presentation/feed.mjs.map +1 -0
  89. package/dist/providers/Cdp.d.mts +9 -0
  90. package/dist/providers/Cdp.mjs +15 -0
  91. package/dist/providers/Cdp.mjs.map +1 -0
  92. package/dist/{Chromium.d.mts → providers/Chromium.d.mts} +6 -7
  93. package/dist/{Chromium.mjs → providers/Chromium.mjs} +7 -16
  94. package/dist/providers/Chromium.mjs.map +1 -0
  95. package/dist/providers/Released.d.mts +2 -0
  96. package/dist/providers/Released.mjs +12 -0
  97. package/dist/providers/Released.mjs.map +1 -0
  98. package/dist/stillness/Stillness.d.mts +12 -0
  99. package/dist/stillness/Stillness.mjs +2 -0
  100. package/dist/supervisor/Supervisor.d.mts +104 -0
  101. package/dist/supervisor/Supervisor.mjs +513 -0
  102. package/dist/supervisor/Supervisor.mjs.map +1 -0
  103. package/dist/testing/index.d.mts +337 -0
  104. package/dist/testing/index.mjs +515 -0
  105. package/dist/testing/index.mjs.map +1 -0
  106. package/dist/workerd/Cdp.d.mts +8 -0
  107. package/dist/workerd/Cdp.mjs +13 -0
  108. package/dist/workerd/Cdp.mjs.map +1 -0
  109. package/package.json +94 -33
  110. package/src/Action.ts +127 -0
  111. package/src/ActionPort.ts +128 -0
  112. package/src/Browser.ts +366 -225
  113. package/src/BrowserError.ts +63 -6
  114. package/src/BrowserEvent.ts +26 -25
  115. package/src/Change.ts +0 -2
  116. package/src/{internal/pictures/clock.ts → Clock.ts} +17 -6
  117. package/src/Frame.ts +14 -0
  118. package/src/Geometry.ts +48 -0
  119. package/src/{internal/reading/match.inpage.ts → Matching.ts} +34 -4
  120. package/src/Page.ts +111 -92
  121. package/src/Snapshot.ts +5 -0
  122. package/src/{internal/input/style.ts → Style.ts} +121 -14
  123. package/src/Window.ts +30 -0
  124. package/src/index.ts +8 -13
  125. package/src/internal/input/actions.ts +61 -119
  126. package/src/internal/input/dispatch.ts +3 -3
  127. package/src/internal/input/gate.ts +21 -18
  128. package/src/internal/input/guard.inpage.ts +25 -58
  129. package/src/internal/input/guard.ts +11 -20
  130. package/src/internal/input/keyboard.ts +12 -3
  131. package/src/internal/input/perform.ts +78 -60
  132. package/src/internal/input/pointer.ts +4 -2
  133. package/src/internal/input/settle.ts +7 -5
  134. package/src/internal/input/targets.inpage.ts +1 -1
  135. package/src/internal/input/targets.ts +4 -9
  136. package/src/internal/page/bridge.ts +210 -114
  137. package/src/internal/page/context.ts +23 -73
  138. package/src/internal/page/dialogs.ts +58 -0
  139. package/src/internal/page/identity.ts +6 -23
  140. package/src/internal/page/lane.ts +14 -15
  141. package/src/internal/page/navigation.ts +50 -42
  142. package/src/internal/page/openings.ts +11 -5
  143. package/src/internal/page/page.ts +44 -81
  144. package/src/internal/page/port.ts +125 -0
  145. package/src/internal/{input → page}/replies.ts +1 -1
  146. package/src/internal/page/request-owner.ts +269 -0
  147. package/src/internal/page/requests.ts +357 -0
  148. package/src/internal/page/scripts.ts +124 -0
  149. package/src/internal/pictures/capture.ts +255 -332
  150. package/src/internal/pictures/dimensions.ts +162 -0
  151. package/src/internal/pictures/frame.ts +68 -0
  152. package/src/internal/pictures/pictures.ts +57 -23
  153. package/src/internal/pictures/quiescence.ts +1 -1
  154. package/src/internal/pictures/state.ts +26 -0
  155. package/src/internal/pictures/transport.ts +21 -4
  156. package/src/internal/reading/controls.inpage.ts +5 -70
  157. package/src/internal/reading/expect.inpage.ts +13 -17
  158. package/src/internal/reading/names.inpage.ts +89 -45
  159. package/src/internal/reading/outline.inpage.ts +6 -7
  160. package/src/internal/reading/reading.ts +144 -176
  161. package/src/internal/reading/ready.inpage.ts +6 -17
  162. package/src/internal/reading/ready.ts +13 -135
  163. package/src/internal/reading/subjects.inpage.ts +83 -43
  164. package/src/internal/reading/walk.inpage.ts +33 -13
  165. package/src/internal/timeline/changes.ts +55 -6
  166. package/src/internal/timeline/events.ts +15 -5
  167. package/src/internal/timeline/history.inpage.ts +1 -1
  168. package/src/internal/timeline/record.inpage.ts +4 -1
  169. package/src/internal/timeline/window.ts +2 -1
  170. package/src/{Moment.ts → moment/Moment.ts} +13 -31
  171. package/src/{internal/timeline → moment}/words.ts +2 -2
  172. package/src/{internal/reading → plan}/choose.ts +3 -3
  173. package/src/plan/data.ts +117 -0
  174. package/src/plan/execute.ts +411 -0
  175. package/src/plan/index.ts +36 -0
  176. package/src/plan/record.ts +71 -0
  177. package/src/plan/targets.ts +71 -0
  178. package/src/{Policy.ts → policy/Policy.ts} +13 -10
  179. package/src/{Motion.ts → presentation/Motion.ts} +12 -46
  180. package/src/presentation/Presentation.ts +176 -0
  181. package/src/{Stage.ts → presentation/Stage.ts} +147 -112
  182. package/src/presentation/feed.ts +21 -0
  183. package/src/providers/Cdp.ts +16 -0
  184. package/src/{Chromium.ts → providers/Chromium.ts} +7 -10
  185. package/src/providers/Released.ts +13 -0
  186. package/src/providers/connection.ts +134 -0
  187. package/src/{internal/pictures/motion.ts → stillness/Pixels.ts} +4 -4
  188. package/src/stillness/Stillness.ts +166 -0
  189. package/src/supervisor/Supervisor.ts +170 -0
  190. package/src/supervisor/contract.ts +105 -0
  191. package/src/supervisor/lifecycle.ts +272 -0
  192. package/src/supervisor/runtime.ts +234 -0
  193. package/src/testing/TestBrowser.ts +115 -0
  194. package/src/testing/TestPage.ts +10 -0
  195. package/src/testing/evidence.ts +154 -0
  196. package/src/testing/index.ts +3 -0
  197. package/src/testing/model.ts +222 -0
  198. package/src/testing/page.ts +360 -0
  199. package/src/workerd/Cdp.ts +11 -0
  200. package/dist/Browser-BwCldIff.mjs +0 -629
  201. package/dist/Browser-BwCldIff.mjs.map +0 -1
  202. package/dist/Cdp.d.mts +0 -32
  203. package/dist/Cdp.mjs +0 -61
  204. package/dist/Cdp.mjs.map +0 -1
  205. package/dist/Change-DNvztNLu.mjs +0 -138
  206. package/dist/Change-DNvztNLu.mjs.map +0 -1
  207. package/dist/Chromium.mjs.map +0 -1
  208. package/dist/Expect.d.mts +0 -62
  209. package/dist/Expect.mjs +0 -114
  210. package/dist/Expect.mjs.map +0 -1
  211. package/dist/Moment.d.mts +0 -2
  212. package/dist/Moment.mjs.map +0 -1
  213. package/dist/Motion.d.mts +0 -2
  214. package/dist/Motion.mjs.map +0 -1
  215. package/dist/Page-CfB6Zn1p.mjs.map +0 -1
  216. package/dist/Plan-C-d5QYS8.mjs.map +0 -1
  217. package/dist/Plan.d.mts +0 -2
  218. package/dist/Plan.mjs +0 -2
  219. package/dist/Policy.mjs.map +0 -1
  220. package/dist/Presentation.d.mts +0 -48
  221. package/dist/Presentation.mjs +0 -118
  222. package/dist/Presentation.mjs.map +0 -1
  223. package/dist/Stage.mjs.map +0 -1
  224. package/dist/Supervisor-2mPjIYYs.mjs +0 -676
  225. package/dist/Supervisor-2mPjIYYs.mjs.map +0 -1
  226. package/dist/Supervisor-mVPeEUp8.d.mts +0 -317
  227. package/dist/Supervisor.d.mts +0 -2
  228. package/dist/Supervisor.mjs +0 -2
  229. package/dist/context-w3NJPRLo.mjs +0 -335
  230. package/dist/context-w3NJPRLo.mjs.map +0 -1
  231. package/dist/match.inpage-xOe9LNO0.mjs.map +0 -1
  232. package/dist/page-BKwfF6z-.mjs +0 -5099
  233. package/dist/page-BKwfF6z-.mjs.map +0 -1
  234. package/dist/url-Cd04Okid.mjs +0 -264
  235. package/dist/url-Cd04Okid.mjs.map +0 -1
  236. package/src/Cdp.ts +0 -101
  237. package/src/Expect.ts +0 -180
  238. package/src/Plan.ts +0 -674
  239. package/src/Presentation.ts +0 -196
  240. package/src/Supervisor.ts +0 -632
  241. package/src/internal/supervisor/lifecycle.ts +0 -484
  242. /package/src/{internal/page/url.ts → Url.ts} +0 -0
  243. /package/src/{internal/pictures → stillness}/utilsBundle.d.ts +0 -0
@@ -3,206 +3,174 @@
3
3
  * read takes its turn on the page and shares its work with identical reads, as `lane.ts`
4
4
  * describes. The viewport's text as it was last read is kept, for `Page.state`.
5
5
  */
6
- import { Duration, Effect, Ref, Schema } from "effect";
6
+ import { Duration, Effect, Schedule, Schema } from "effect";
7
7
 
8
- import { StaleRef, Timeout } from "../../BrowserError.ts";
9
- import { type FindQuery, Found, Text, type TextOptions, type WaitCondition } from "../../Page.ts";
8
+ import { StaleRef } from "../../BrowserError.ts";
9
+ import { request as askedOf, type FindRequest } from "../../Matching.ts";
10
+ import { type FindQuery, Found, Text, type TextOptions } from "../../Page.ts";
10
11
  import { Snapshot, type SnapshotOptions } from "../../Snapshot.ts";
12
+ import * as Url from "../../Url.ts";
11
13
  import { type Bridge, scriptCall } from "../page/bridge.ts";
12
14
  import { contextGone, decodeWith, failWith, type PageContext, refuse } from "../page/context.ts";
13
- import * as Url from "../page/url.ts";
14
- import { type ConditionRequest, ConditionResultSchema } from "./controls.inpage.ts";
15
15
  import type { Want } from "./expect.inpage.ts";
16
- import type { FindRequest, Wanted } from "./match.inpage.ts";
17
16
  import { type SnapshotRequest, SnapshotResultSchema } from "./outline.inpage.ts";
18
17
  import { type TextRequest, TextResultSchema } from "./text.inpage.ts";
19
18
 
20
- // What `find` reads back, with the next ref the page may give.
21
- const FindResults = Schema.Struct({ found: Schema.Array(Found), nextRef: Schema.Finite });
22
-
23
- const UntilResult = Schema.Struct({ met: Schema.Boolean, mark: Schema.String });
24
-
25
- // A pattern crosses into the page as its source and flags.
26
- const wanted = (value: string | RegExp | undefined): Wanted | null =>
27
- value === undefined
28
- ? null
29
- : typeof value === "string"
30
- ? value
31
- : { source: value.source, flags: value.flags };
32
-
33
- /** A query as the page takes it, but for the first ref it may give. */
34
- const askedOf = (query: FindQuery, scope: "viewport" | "document") => ({
35
- role: query.role ?? null,
36
- name: wanted(query.name),
37
- text: wanted(query.text),
38
- near: query.near ?? null,
39
- at: query.at ?? null,
40
- scope: query.scope ?? scope,
41
- });
42
-
43
- export const make = Effect.fnUntraced(function* (page: PageContext, bridge: Bridge) {
44
- const { now, span, owned, within, lane } = page;
45
- const { evaluate } = bridge;
46
- // Refs count up across the page's documents, so one never names two elements.
47
- const nextRef = yield* Ref.make(1);
48
- const counted = (next: number) => Ref.update(nextRef, (current) => Math.max(current, next));
49
- const snapshots = lane.shared<Snapshot>("snapshot", true);
50
- const finds = lane.shared<ReadonlyArray<Found>>("find", true);
51
- const texts = lane.shared<Text>("text", true);
52
- let viewed: Text | undefined;
53
-
54
- const snapshot = (snapshotOptions: SnapshotOptions = {}) => {
55
- const asked = {
56
- full: snapshotOptions.full ?? false,
57
- query: snapshotOptions.query ?? null,
58
- within: snapshotOptions.within ?? null,
59
- maxChars: snapshotOptions.maxChars ?? 12_000,
60
- };
19
+ // What `find` reads back. The bridge envelope owns ref allocation across documents.
20
+ const FindResults = Schema.Union([
21
+ Schema.Struct({ found: Schema.Array(Found) }),
22
+ Schema.Struct({ invalid: Schema.String }),
23
+ ]);
24
+
25
+ const UntilResult = Schema.Union([
26
+ Schema.Struct({ met: Schema.Boolean, mark: Schema.String }),
27
+ Schema.Struct({ invalid: Schema.String }),
28
+ ]);
29
+
30
+ export const make = (page: PageContext, bridge: Bridge) =>
31
+ Effect.sync(() => {
32
+ const { now, span, owned, within, lane } = page;
33
+ const { evaluate } = bridge;
34
+ const snapshots = lane.shared<Snapshot>("snapshot", true);
35
+ const finds = lane.shared<ReadonlyArray<Found>>("find", true);
36
+ const texts = lane.shared<Text>("text", true);
37
+ let viewed: Text | undefined;
38
+
39
+ const snapshot = (snapshotOptions: SnapshotOptions = {}) => {
40
+ const asked = {
41
+ full: snapshotOptions.full ?? false,
42
+ query: snapshotOptions.query ?? null,
43
+ within: snapshotOptions.within ?? null,
44
+ maxChars: snapshotOptions.maxChars ?? 12_000,
45
+ boxes: snapshotOptions.boxes ?? false,
46
+ };
61
47
 
62
- return snapshots(
63
- JSON.stringify(asked),
64
- Effect.gen(function* () {
65
- const request: SnapshotRequest = { ...asked, firstRef: yield* Ref.get(nextRef) };
48
+ return snapshots(
49
+ JSON.stringify(asked),
50
+ Effect.gen(function* () {
51
+ const request: SnapshotRequest = asked;
66
52
 
67
- const result = yield* evaluate("snapshot", scriptCall("snapshot", request)).pipe(
68
- Effect.flatMap(decodeWith("snapshot", SnapshotResultSchema)),
69
- );
53
+ const result = yield* evaluate("snapshot", scriptCall("snapshot", request)).pipe(
54
+ Effect.flatMap(decodeWith("snapshot", SnapshotResultSchema)),
55
+ );
70
56
 
71
- if ("invalid" in result) return yield* refuse("snapshot", result.invalid);
72
- const { text, truncated, url } = result.snapshot;
57
+ if ("invalid" in result) return yield* refuse("snapshot", result.invalid);
58
+ const { text, truncated, url } = result.snapshot;
73
59
 
74
- yield* counted(result.nextRef);
75
- yield* Effect.annotateCurrentSpan({ chars: text.length, truncated });
60
+ yield* Effect.annotateCurrentSpan({ chars: text.length, truncated });
76
61
 
77
- return new Snapshot({ ...result.snapshot, url: Url.redact(url) });
78
- }).pipe(within("snapshot")),
79
- ).pipe(span("Page.snapshot", { full: asked.full }), owned);
80
- };
62
+ return new Snapshot({ ...result.snapshot, url: Url.redact(url) });
63
+ }).pipe(within("snapshot")),
64
+ ).pipe(span("Page.snapshot", { full: asked.full }), owned);
65
+ };
81
66
 
82
- const find = (query: FindQuery = {}) => {
83
- const asked = askedOf(query, "viewport");
67
+ const find = (query: FindQuery = {}) => {
68
+ const asked = askedOf(query, "viewport");
84
69
 
85
- return finds(
86
- JSON.stringify(asked),
87
- Effect.gen(function* () {
88
- const request: FindRequest = { ...asked, firstRef: yield* Ref.get(nextRef) };
70
+ return finds(
71
+ JSON.stringify(asked),
72
+ Effect.gen(function* () {
73
+ const request: FindRequest = asked;
89
74
 
90
- const result = yield* evaluate("find", scriptCall("find", request)).pipe(
91
- Effect.flatMap(decodeWith("find", FindResults)),
92
- );
75
+ const result = yield* evaluate("find", scriptCall("find", request)).pipe(
76
+ Effect.flatMap(decodeWith("find", FindResults)),
77
+ );
93
78
 
94
- yield* counted(result.nextRef);
95
- yield* Effect.annotateCurrentSpan({ found: result.found.length });
96
-
97
- return result.found;
98
- }).pipe(within("find")),
99
- ).pipe(
100
- // The query's words are the caller's, and may be anything; only its shape is traced.
101
- span("Page.find", { scope: asked.scope, role: asked.role ?? "" }),
102
- owned,
103
- );
104
- };
105
-
106
- const text = (options: TextOptions = {}) => {
107
- const scope = options.scope ?? "viewport";
108
-
109
- const request: TextRequest = {
110
- ref: scope === "viewport" ? null : scope,
111
- maxChars: options.maxChars ?? 12_000,
112
- unmask: options.unmask ?? false,
113
- };
79
+ if ("invalid" in result) return yield* refuse("find", result.invalid);
114
80
 
115
- return (
116
- scope !== "viewport" && !/^e\d+$/.test(scope)
117
- ? refuse("text", `${scope} is neither "viewport" nor a ref such as e12`)
118
- : texts(
119
- JSON.stringify(request),
120
- Effect.gen(function* () {
121
- const result = yield* evaluate("text", scriptCall("text", request)).pipe(
122
- Effect.flatMap(decodeWith("text", TextResultSchema)),
123
- );
124
-
125
- if (result === null) return yield* failWith("text", new StaleRef({ ref: scope }));
126
- yield* Effect.annotateCurrentSpan({
127
- chars: result.text.length,
128
- truncated: result.truncated,
129
- });
130
- const shown = new Text({ ...result, url: Url.redact(result.url), at: now() });
131
-
132
- if (request.ref === null) viewed = shown;
133
-
134
- return shown;
135
- }).pipe(within("text")),
136
- )
137
- ).pipe(span("Page.text", { scope: scope === "viewport" ? "viewport" : "ref" }), owned);
138
- };
139
-
140
- // One call, which the page answers once the condition holds or at the deadline. It follows the
141
- // action in flight, as a read does, but holds no later one back while it waits, and it waits on
142
- // in a document that replaces the one it began in.
143
- const waitFor = (condition: WaitCondition, timeout: Duration.Input = Duration.seconds(10)) => {
144
- const request: ConditionRequest = {
145
- selector: condition.selector ?? null,
146
- text: condition.text ?? null,
147
- state: condition.state ?? "visible",
148
- };
81
+ yield* Effect.annotateCurrentSpan({ found: result.found.length });
149
82
 
150
- // JSON has no infinity; the deadline here ends a wait without one in any case.
151
- const millis = Math.min(Duration.toMillis(timeout), Number.MAX_SAFE_INTEGER);
152
- const unmet = () => failWith("waitFor", new Timeout({ millis }));
153
-
154
- const look = evaluate("waitFor", scriptCall("waitUntil", request, millis)).pipe(
155
- Effect.flatMap(decodeWith("waitFor", ConditionResultSchema)),
156
- Effect.flatMap((result) =>
157
- "invalid" in result
158
- ? refuse("waitFor", result.invalid)
159
- : result.met
160
- ? Effect.void
161
- : unmet(),
162
- ),
163
- );
164
-
165
- return lane
166
- .read("waitFor")(Effect.void)
167
- .pipe(
168
- Effect.andThen(Effect.retry(look, { while: contextGone })),
169
- Effect.timeoutOrElse({ duration: timeout, orElse: unmet }),
170
- span("Page.waitFor", { state: request.state }),
83
+ return result.found;
84
+ }).pipe(within("find")),
85
+ ).pipe(
86
+ // The query's words are the caller's, and may be anything; only its shape is traced.
87
+ span("Page.find", { scope: asked.scope, role: asked.role ?? "" }),
171
88
  owned,
172
89
  );
173
- };
174
-
175
- // One call, which the page answers once `want` holds of what `query` matches, the whole document
176
- // unless it says otherwise, or at the deadline, with the mark of what matched then. It follows
177
- // the action in flight, as `waitFor` does, and the page has the action timeout past the deadline
178
- // to answer.
179
- const until = (query: FindQuery, want: Want, millis: number) => {
180
- const look = Effect.gen(function* () {
181
- const request: FindRequest = {
182
- ...askedOf(query, "document"),
183
- firstRef: yield* Ref.get(nextRef),
90
+ };
91
+
92
+ const text = (options: TextOptions = {}) => {
93
+ const scope = options.scope ?? "viewport";
94
+
95
+ const request: TextRequest = {
96
+ ref: scope === "viewport" ? null : scope,
97
+ maxChars: options.maxChars ?? 12_000,
98
+ unmask: options.unmask ?? false,
184
99
  };
185
100
 
186
- return yield* evaluate("expect", scriptCall("until", request, want, millis)).pipe(
187
- Effect.flatMap(decodeWith("expect", UntilResult)),
188
- );
189
- });
190
-
191
- return lane
192
- .read("expect")(Effect.void)
193
- .pipe(
194
- Effect.andThen(Effect.retry(look, { while: contextGone })),
195
- within("expect", Duration.sum(Duration.millis(millis), page.settings.actionTimeout)),
196
- span("Page.expect", { want: typeof want === "string" ? want : "other" }),
197
- owned,
198
- );
199
- };
101
+ return (
102
+ scope !== "viewport" && !/^e\d+$/.test(scope)
103
+ ? refuse("text", `${scope} is neither "viewport" nor a ref such as e12`)
104
+ : texts(
105
+ JSON.stringify(request),
106
+ Effect.gen(function* () {
107
+ const result = yield* evaluate("text", scriptCall("text", request)).pipe(
108
+ Effect.flatMap(decodeWith("text", TextResultSchema)),
109
+ );
110
+
111
+ if (result === null) return yield* failWith("text", new StaleRef({ ref: scope }));
112
+ yield* Effect.annotateCurrentSpan({
113
+ chars: result.text.length,
114
+ truncated: result.truncated,
115
+ });
116
+ const shown = new Text({ ...result, url: Url.redact(result.url), at: now() });
117
+
118
+ if (request.ref === null) viewed = shown;
119
+
120
+ return shown;
121
+ }).pipe(within("text")),
122
+ )
123
+ ).pipe(span("Page.text", { scope: scope === "viewport" ? "viewport" : "ref" }), owned);
124
+ };
125
+
126
+ // One call, which the page answers once `want` holds of what `query` matches, the whole document
127
+ // unless it says otherwise, or at the deadline, with the mark of what matched then. It follows
128
+ // the action in flight, and the page has the action timeout past the deadline
129
+ // to answer.
130
+ const until = (query: FindQuery, want: Want, millis: number) => {
131
+ if (!Number.isFinite(millis) || millis < 0)
132
+ return refuse("until", "the wait duration must be finite and nonnegative");
133
+
134
+ const look = Effect.gen(function* () {
135
+ const deadline = now() + millis;
136
+ const request: FindRequest = askedOf(query, "document");
137
+
138
+ return yield* Effect.suspend(() =>
139
+ evaluate("expect", scriptCall("until", request, want, Math.max(0, deadline - now()))),
140
+ ).pipe(
141
+ Effect.flatMap(decodeWith("expect", UntilResult)),
142
+ Effect.filterOrElse(
143
+ (result) => "met" in result,
144
+ (result) => refuse("expect", result.invalid),
145
+ ),
146
+ Effect.retry({
147
+ while: (error) => contextGone(error) && now() < deadline,
148
+ schedule: Schedule.spaced("10 millis"),
149
+ }),
150
+ // No stable document answered by this deadline. An unmet verdict carries no sampled
151
+ // mark; a zero-window baseline still fails rather than inventing comparison evidence.
152
+ Effect.catchIf(
153
+ (error) => millis > 0 && contextGone(error) && now() >= deadline,
154
+ () => Effect.succeed({ met: false, mark: "" }),
155
+ ),
156
+ );
157
+ });
158
+
159
+ return lane
160
+ .read("expect")(Effect.void)
161
+ .pipe(
162
+ Effect.andThen(look),
163
+ within("expect", Duration.sum(Duration.millis(millis), page.settings.actionTimeout)),
164
+ span("Page.expect", { want: typeof want === "string" ? want : "other" }),
165
+ owned,
166
+ );
167
+ };
200
168
 
201
- /** The viewport's text as it was last read, if after `since`, as a document began. */
202
- const viewedSince = (since: number) =>
203
- viewed !== undefined && viewed.at > since ? viewed : undefined;
169
+ /** The viewport's text as it was last read, if after `since`, as a document began. */
170
+ const viewedSince = (since: number) =>
171
+ viewed !== undefined && viewed.at > since ? viewed : undefined;
204
172
 
205
- return { snapshot, find, text, waitFor, until, viewedSince };
206
- });
173
+ return { snapshot, find, text, until, viewedSince };
174
+ });
207
175
 
208
176
  export type Reading = Effect.Success<ReturnType<typeof make>>;
@@ -6,14 +6,11 @@
6
6
  * frame. So a canvas mounted blank, or a spinner alone, is still loading. A screen that only says
7
7
  * "Loading…" in words reads as ready, and a WebGL canvas drawn once, without keeping its drawing,
8
8
  * reads as blank. Asked for a quiet spell, where the page's changes are recorded, nothing in view
9
- * may have changed for that long either. Content mode starts and refreshes the recorder and
10
- * observes a full quiet interval from its first check in each document. After input, it waits a
11
- * task and a frame. See `names.inpage.ts` for what a page-side part may use.
9
+ * may have changed for that long either. After input, it waits a task and a frame. See `names.inpage.ts` for what a page-side part may use.
12
10
  */
13
11
  import { Schema } from "effect";
14
12
 
15
13
  import type { History } from "../timeline/history.inpage.ts";
16
- import type { Recorder } from "../timeline/record.inpage.ts";
17
14
  import type { Texts } from "./text.inpage.ts";
18
15
  import type { Walk } from "./walk.inpage.ts";
19
16
 
@@ -33,7 +30,7 @@ export const ReadinessSchema = Schema.Array(
33
30
 
34
31
  export type Readiness = typeof ReadinessSchema.Type;
35
32
 
36
- export const ready = (walked: Walk, texts: Texts, kept: History, recorder: Recorder) => {
33
+ export const ready = (walked: Walk, texts: Texts, kept: History) => {
37
34
  const { inView, visible } = walked;
38
35
  // A canvas is read through a small copy: reading it directly would give it a context of ours.
39
36
  let copy: OffscreenCanvasRenderingContext2D | null | undefined;
@@ -88,7 +85,7 @@ export const ready = (walked: Walk, texts: Texts, kept: History, recorder: Recor
88
85
  };
89
86
 
90
87
  // In a frame, before it is drawn: a canvas drawn every frame still holds its drawing then.
91
- const check = (quietMillis: number, observed: number): Promise<Readiness> =>
88
+ const check = (quietMillis: number): Promise<Readiness> =>
92
89
  painted().then((paints) => {
93
90
  const waiting: Array<Readiness[number]> = [];
94
91
 
@@ -115,10 +112,7 @@ export const ready = (walked: Walk, texts: Texts, kept: History, recorder: Recor
115
112
  if (document.fonts.status !== "loaded") waiting.push("fonts");
116
113
  if (showing(document.querySelectorAll("[aria-busy=true]")).length > 0) waiting.push("busy");
117
114
  if (!content()) waiting.push("content");
118
- if (
119
- Math.max(observed, kept.changing()) >
120
- performance.timeOrigin + performance.now() - quietMillis
121
- )
115
+ if (kept.changing() > performance.timeOrigin + performance.now() - quietMillis)
122
116
  waiting.push("changing");
123
117
 
124
118
  return waiting;
@@ -128,16 +122,11 @@ export const ready = (walked: Walk, texts: Texts, kept: History, recorder: Recor
128
122
  * Check until the page is ready, or for `millis`, and say what it is still waiting for; with
129
123
  * `quietMillis`, until nothing the record saw in view has changed for that long, too.
130
124
  */
131
- const wait = async (millis: number, quietMillis = 0, contentOnly = false): Promise<Readiness> => {
125
+ const wait = async (millis: number, quietMillis = 0): Promise<Readiness> => {
132
126
  const until = performance.now() + millis;
133
- let observed = contentOnly ? undefined : Number.NEGATIVE_INFINITY;
134
127
 
135
128
  for (;;) {
136
- if (contentOnly) {
137
- recorder.start();
138
- if (kept.begun()) observed ??= performance.timeOrigin + performance.now();
139
- }
140
- const waiting = await check(quietMillis, observed ?? Number.POSITIVE_INFINITY);
129
+ const waiting = await check(quietMillis);
141
130
 
142
131
  if (waiting.length === 0 || performance.now() >= until) return waiting;
143
132
  const { promise, resolve } = Promise.withResolvers<void>();
@@ -1,41 +1,19 @@
1
- /**
2
- * Waiting until a page is ready to be shown. The page's own evidence leads: one page-script call
3
- * that checks in the page until nothing is left to wait for, or the time is up. A caller that also
4
- * needs the screen still, such as after spinning a canvas game's reels, gives a quiet spell, and
5
- * the capture must then show no frame that long. A capture's silence cannot prove the last paint
6
- * arrived: a frame can be held on its way by a stalled connection, and Chromium sends frames only
7
- * while few acknowledgements are unanswered. So the spell counts only while every acknowledgement
8
- * is answered, and it ends with the page's evidence once more and, where the capture runs on a
9
- * connection of its own, a round trip there: each answer comes behind every frame sent before it on
10
- * its connection, and a frame that arrives first starts the spell again. Where the page's changes
11
- * are recorded, its evidence includes them: nothing in view may have changed for the spell, which a
12
- * DOM page's changes show better than its paint, and no stall on the way can hide. Content
13
- * stillness starts that record itself and ignores capture silence. Foreground stillness lets
14
- * frames keep coming but compares them, so that only motion outside what was moving before the
15
- * page's latest input counts; it still waits out unanswered acknowledgements while no frame
16
- * comes, and its last look follows the round trip too. Each new document starts a
17
- * new observation, while retries of destroyed script contexts share the caller's deadline.
18
- */
19
- import { Duration, Effect, Option, Schedule, Stream } from "effect";
1
+ /** Document readiness and content quiet, independent of screen-capture features. */
2
+ import { Duration, Effect, Schedule } from "effect";
20
3
 
21
- import { Failed, Timeout } from "../../BrowserError.ts";
22
- import type { Frame } from "../../Frame.ts";
4
+ import { Timeout, failWith, refuse } from "../../BrowserError.ts";
23
5
  import type { ReadyOptions } from "../../Page.ts";
24
6
  import { type Bridge, scriptCall } from "../page/bridge.ts";
25
- import {
26
- contextGone,
27
- decodeWith,
28
- failWith,
29
- type PageContext,
30
- undispatched,
31
- } from "../page/context.ts";
32
- import type * as Capture from "../pictures/capture.ts";
33
- import * as Motion from "../pictures/motion.ts";
7
+ import { contextGone, decodeWith, type PageContext } from "../page/context.ts";
34
8
  import { ReadinessSchema } from "./ready.inpage.ts";
35
9
 
36
10
  export const make =
37
- (page: PageContext, bridge: Bridge, capture: Capture.Controller) =>
11
+ (page: PageContext, bridge: Bridge) =>
38
12
  (options: ReadyOptions = {}) => {
13
+ const quietMillis = options.quietMillis ?? 0;
14
+
15
+ if (!Number.isFinite(quietMillis) || quietMillis < 0)
16
+ return refuse("ready", "quietMillis must be finite and nonnegative");
39
17
  const timeout = options.timeout ?? Duration.seconds(15);
40
18
 
41
19
  // The page's own evidence, checked in the page until nothing is left or `until` has come.
@@ -43,12 +21,7 @@ export const make =
43
21
  Effect.suspend(() =>
44
22
  bridge.evaluate(
45
23
  "ready",
46
- scriptCall(
47
- "ready",
48
- Math.max(0, Math.round(until - page.now())),
49
- options.quietMillis ?? 0,
50
- options.still === "content",
51
- ),
24
+ scriptCall("ready", Math.max(0, Math.round(until - page.now())), quietMillis),
52
25
  ),
53
26
  ).pipe(
54
27
  Effect.retry({ while: contextGone, schedule: Schedule.spaced(Duration.millis(10)) }),
@@ -60,108 +33,13 @@ export const make =
60
33
  ),
61
34
  );
62
35
 
63
- // A capture the wait starts sends its first frame late, over half a second from a hosted
64
- // browser, so the spell counts from that frame (#202); Chrome sends one as a capture starts,
65
- // even of a still page.
66
- const quiet = (millis: number, until: number) =>
67
- Effect.gen(function* () {
68
- const running = yield* capture.active;
69
- const foreground = options.still === "foreground";
70
- let counts = yield* capture.stats();
71
- let stirred = running ? page.now() : Number.POSITIVE_INFINITY;
72
- let reference: Motion.Picture | undefined;
73
-
74
- const decoded = (frame: Frame) =>
75
- Effect.try({
76
- try: () => Motion.picture(frame),
77
- catch: (error) =>
78
- undispatched("ready", new Failed({ detail: `no picture: ${String(error)}` })),
79
- });
80
-
81
- // The background: what moved in up to eight frames of the document's 1.5 s before its
82
- // latest input, as far back as the capture keeps them.
83
- const { inputAt } = page.activity;
84
- const document = Option.getOrUndefined(yield* capture.latest)?.document;
85
-
86
- const before = (foreground ? yield* capture.recent : []).filter(
87
- (frame) =>
88
- frame.hostTime < inputAt &&
89
- frame.hostTime >= inputAt - 1500 &&
90
- frame.document === document,
91
- );
92
-
93
- const sampled = before.filter((_, index) => index % Math.ceil(before.length / 8) === 0);
94
- const mask = Motion.background(yield* Effect.forEach(sampled, decoded));
95
-
96
- // How long the screen has been still: since a frame, or an unanswered acknowledgement. In
97
- // the foreground, since a frame moved outside the background, or acknowledgements went
98
- // unanswered with no frame coming.
99
- const look = Effect.gen(function* () {
100
- const stats = yield* capture.stats();
101
- const latest = Option.getOrUndefined(yield* capture.latest);
102
- const arrived = stats.received !== counts.received;
103
- const unseen = stats.accepted - counts.accepted;
104
-
105
- counts = stats;
106
- if (latest === undefined || !capture.current(latest)) stirred = page.now();
107
- else if (!foreground ? arrived || stats.ackBacklog > 0 : !arrived && stats.ackBacklog > 0)
108
- stirred = page.now();
109
- else if (foreground && (unseen > 0 || reference === undefined)) {
110
- // A move and its return between looks both count. Bound decoding; any frames lost
111
- // from history, or beyond this batch, deny a quiet spell rather than hiding motion.
112
- const frames = (yield* capture.recent).slice(-Math.max(1, Math.min(8, unseen)));
113
-
114
- if (unseen > frames.length) stirred = page.now();
115
- for (const frame of frames) {
116
- const seen = yield* decoded(frame);
117
-
118
- if (reference === undefined || Motion.stirs(reference, seen, mask)) {
119
- reference = seen;
120
- stirred = page.now();
121
- }
122
- }
123
- }
124
-
125
- return page.now() - stirred;
126
- });
127
-
128
- // Decode at a fixed cadence: adding a delay after a full batch lets its backlog grow.
129
- const spell = Effect.repeat(look, {
130
- schedule: Schedule.fixed(Duration.millis(foreground ? 100 : 25)),
131
- until: (still) => still >= millis,
132
- });
133
-
134
- const capturing = running
135
- ? Effect.never
136
- : capture.stream().pipe(Stream.runDrain, Effect.andThen(Effect.never));
137
-
138
- const still = Effect.gen(function* () {
139
- for (;;) {
140
- yield* spell;
141
- const before = counts.received;
142
-
143
- yield* Effect.all([settled(until), capture.sync], { concurrency: 2, discard: true });
144
- if ((yield* look) >= millis && (foreground || counts.received === before)) return;
145
- }
146
- });
147
-
148
- return yield* Effect.raceFirst(capturing, still);
149
- });
150
-
151
- // The wait follows the action in flight, as a read does, but holds no later one back while it
152
- // watches the page settle, which can take its whole timeout.
153
36
  return page.lane
154
37
  .read("ready")(Effect.void)
155
38
  .pipe(
156
39
  Effect.andThen(
157
- Effect.gen(function* () {
158
- const until = page.now() + Duration.toMillis(timeout);
159
-
160
- if (options.still === "content") yield* bridge.registerRecorder("ready");
161
- yield* settled(until);
162
- if (options.quietMillis !== undefined && options.still !== "content")
163
- yield* quiet(options.quietMillis, until);
164
- }).pipe(page.within("ready", timeout)),
40
+ Effect.suspend(() => settled(page.now() + Duration.toMillis(timeout))).pipe(
41
+ page.within("ready", timeout),
42
+ ),
165
43
  ),
166
44
  page.span("Page.ready"),
167
45
  page.owned,