effect-browser 0.3.0-beta.21 → 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 (248) hide show
  1. package/README.md +130 -159
  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-CiXHT1yO.d.mts → Browser-wZhwZSlG.d.mts} +51 -13
  12. package/dist/Browser.d.mts +2 -2
  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/Matching.d.mts +37 -0
  38. package/dist/Matching.mjs +72 -0
  39. package/dist/Matching.mjs.map +1 -0
  40. package/dist/Page-BF5izr8R.mjs +795 -0
  41. package/dist/Page-BF5izr8R.mjs.map +1 -0
  42. package/dist/{Page-ku6j5t-v.d.mts → Page-CvsOEorc.d.mts} +158 -185
  43. package/dist/Page.d.mts +2 -2
  44. package/dist/Page.mjs +1 -1
  45. package/dist/Released-tURbFmal.d.mts +15 -0
  46. package/dist/{Snapshot-B34hZ4Jl.d.mts → Snapshot-CtBVOtuu.d.mts} +6 -1
  47. package/dist/Snapshot.d.mts +1 -1
  48. package/dist/Snapshot.mjs.map +1 -1
  49. package/dist/Stillness-CNlvayIo.mjs +136 -0
  50. package/dist/Stillness-CNlvayIo.mjs.map +1 -0
  51. package/dist/Style-AO4V7Czt.mjs +357 -0
  52. package/dist/Style-AO4V7Czt.mjs.map +1 -0
  53. package/dist/Style.d.mts +72 -0
  54. package/dist/Style.mjs +2 -0
  55. package/dist/Url.d.mts +26 -0
  56. package/dist/Url.mjs +74 -0
  57. package/dist/Url.mjs.map +1 -0
  58. package/dist/Window-CMddGMt1.d.mts +34 -0
  59. package/dist/Window.d.mts +2 -0
  60. package/dist/Window.mjs +35 -0
  61. package/dist/Window.mjs.map +1 -0
  62. package/dist/{cdp-DL7XfEj5.d.mts → connection-2KHI4_KN.d.mts} +4 -4
  63. package/dist/{cdp-BMpYMWYe.mjs → connection-BTeO0Hiz.mjs} +5 -6
  64. package/dist/connection-BTeO0Hiz.mjs.map +1 -0
  65. package/dist/index.d.mts +15 -18
  66. package/dist/index.mjs +11 -15
  67. package/dist/moment/Moment.d.mts +86 -0
  68. package/dist/{Moment.mjs → moment/Moment.mjs} +44 -38
  69. package/dist/moment/Moment.mjs.map +1 -0
  70. package/dist/{Plan-DJ4hEEna.d.mts → plan/index.d.mts} +169 -64
  71. package/dist/{Plan-BG1WifW2.mjs → plan/index.mjs} +122 -199
  72. package/dist/plan/index.mjs.map +1 -0
  73. package/dist/{Policy-BGWjbqCl.d.mts → policy/Policy.d.mts} +16 -20
  74. package/dist/{Policy.mjs → policy/Policy.mjs} +11 -21
  75. package/dist/policy/Policy.mjs.map +1 -0
  76. package/dist/presentation/Motion.d.mts +12 -0
  77. package/dist/{Motion.mjs → presentation/Motion.mjs} +4 -27
  78. package/dist/presentation/Motion.mjs.map +1 -0
  79. package/dist/presentation/Presentation.d.mts +30 -0
  80. package/dist/presentation/Presentation.mjs +105 -0
  81. package/dist/presentation/Presentation.mjs.map +1 -0
  82. package/dist/{Stage-NHp8N81k.d.mts → presentation/Stage.d.mts} +12 -15
  83. package/dist/{Stage.mjs → presentation/Stage.mjs} +68 -76
  84. package/dist/presentation/Stage.mjs.map +1 -0
  85. package/dist/presentation/feed.d.mts +20 -0
  86. package/dist/presentation/feed.mjs +18 -0
  87. package/dist/presentation/feed.mjs.map +1 -0
  88. package/dist/providers/Cdp.d.mts +9 -0
  89. package/dist/{Cdp.mjs → providers/Cdp.mjs} +3 -8
  90. package/dist/providers/Cdp.mjs.map +1 -0
  91. package/dist/{Chromium.d.mts → providers/Chromium.d.mts} +6 -7
  92. package/dist/{Chromium.mjs → providers/Chromium.mjs} +6 -15
  93. package/dist/providers/Chromium.mjs.map +1 -0
  94. package/dist/providers/Released.d.mts +2 -0
  95. package/dist/providers/Released.mjs +12 -0
  96. package/dist/providers/Released.mjs.map +1 -0
  97. package/dist/stillness/Stillness.d.mts +12 -0
  98. package/dist/stillness/Stillness.mjs +2 -0
  99. package/dist/supervisor/Supervisor.d.mts +104 -0
  100. package/dist/supervisor/Supervisor.mjs +513 -0
  101. package/dist/supervisor/Supervisor.mjs.map +1 -0
  102. package/dist/testing/index.d.mts +337 -0
  103. package/dist/testing/index.mjs +515 -0
  104. package/dist/testing/index.mjs.map +1 -0
  105. package/dist/workerd/Cdp.d.mts +8 -3
  106. package/dist/workerd/Cdp.mjs +2 -7
  107. package/dist/workerd/Cdp.mjs.map +1 -1
  108. package/package.json +86 -36
  109. package/src/Action.ts +127 -0
  110. package/src/ActionPort.ts +128 -0
  111. package/src/Browser.ts +352 -201
  112. package/src/BrowserError.ts +63 -6
  113. package/src/BrowserEvent.ts +26 -25
  114. package/src/Change.ts +0 -2
  115. package/src/{internal/pictures/clock.ts → Clock.ts} +17 -6
  116. package/src/Frame.ts +14 -0
  117. package/src/Geometry.ts +48 -0
  118. package/src/{internal/reading/match.inpage.ts → Matching.ts} +34 -4
  119. package/src/Page.ts +111 -92
  120. package/src/Snapshot.ts +5 -0
  121. package/src/{internal/input/style.ts → Style.ts} +121 -14
  122. package/src/Window.ts +30 -0
  123. package/src/index.ts +15 -6
  124. package/src/internal/input/actions.ts +61 -119
  125. package/src/internal/input/dispatch.ts +3 -3
  126. package/src/internal/input/gate.ts +21 -18
  127. package/src/internal/input/guard.inpage.ts +25 -58
  128. package/src/internal/input/guard.ts +11 -20
  129. package/src/internal/input/keyboard.ts +12 -3
  130. package/src/internal/input/perform.ts +78 -60
  131. package/src/internal/input/pointer.ts +4 -2
  132. package/src/internal/input/settle.ts +7 -5
  133. package/src/internal/input/targets.inpage.ts +1 -1
  134. package/src/internal/input/targets.ts +4 -9
  135. package/src/internal/page/bridge.ts +209 -113
  136. package/src/internal/page/context.ts +12 -59
  137. package/src/internal/page/dialogs.ts +58 -0
  138. package/src/internal/page/lane.ts +14 -15
  139. package/src/internal/page/navigation.ts +50 -42
  140. package/src/internal/page/openings.ts +11 -5
  141. package/src/internal/page/page.ts +44 -81
  142. package/src/internal/page/port.ts +125 -0
  143. package/src/internal/{input → page}/replies.ts +1 -1
  144. package/src/internal/page/request-owner.ts +269 -0
  145. package/src/internal/page/requests.ts +357 -0
  146. package/src/internal/page/scripts.ts +124 -0
  147. package/src/internal/pictures/capture.ts +255 -332
  148. package/src/internal/pictures/dimensions.ts +162 -0
  149. package/src/internal/pictures/frame.ts +68 -0
  150. package/src/internal/pictures/pictures.ts +57 -23
  151. package/src/internal/pictures/quiescence.ts +1 -1
  152. package/src/internal/pictures/state.ts +26 -0
  153. package/src/internal/pictures/transport.ts +21 -4
  154. package/src/internal/reading/controls.inpage.ts +5 -70
  155. package/src/internal/reading/expect.inpage.ts +13 -17
  156. package/src/internal/reading/names.inpage.ts +89 -45
  157. package/src/internal/reading/outline.inpage.ts +6 -7
  158. package/src/internal/reading/reading.ts +144 -176
  159. package/src/internal/reading/ready.inpage.ts +6 -17
  160. package/src/internal/reading/ready.ts +13 -135
  161. package/src/internal/reading/subjects.inpage.ts +83 -43
  162. package/src/internal/reading/walk.inpage.ts +33 -13
  163. package/src/internal/timeline/changes.ts +55 -6
  164. package/src/internal/timeline/events.ts +15 -5
  165. package/src/internal/timeline/history.inpage.ts +1 -1
  166. package/src/internal/timeline/record.inpage.ts +4 -1
  167. package/src/internal/timeline/window.ts +2 -1
  168. package/src/{Moment.ts → moment/Moment.ts} +13 -31
  169. package/src/{internal/timeline → moment}/words.ts +2 -2
  170. package/src/{internal/reading → plan}/choose.ts +3 -3
  171. package/src/plan/data.ts +117 -0
  172. package/src/plan/execute.ts +411 -0
  173. package/src/plan/index.ts +36 -0
  174. package/src/plan/record.ts +71 -0
  175. package/src/plan/targets.ts +71 -0
  176. package/src/{Policy.ts → policy/Policy.ts} +13 -10
  177. package/src/{Motion.ts → presentation/Motion.ts} +12 -46
  178. package/src/presentation/Presentation.ts +176 -0
  179. package/src/{Stage.ts → presentation/Stage.ts} +147 -112
  180. package/src/presentation/feed.ts +21 -0
  181. package/src/{Cdp.ts → providers/Cdp.ts} +2 -2
  182. package/src/{Chromium.ts → providers/Chromium.ts} +7 -10
  183. package/src/providers/Released.ts +13 -0
  184. package/src/{internal/cdp.ts → providers/connection.ts} +2 -2
  185. package/src/{internal/pictures/motion.ts → stillness/Pixels.ts} +1 -1
  186. package/src/stillness/Stillness.ts +166 -0
  187. package/src/supervisor/Supervisor.ts +170 -0
  188. package/src/supervisor/contract.ts +105 -0
  189. package/src/supervisor/lifecycle.ts +272 -0
  190. package/src/supervisor/runtime.ts +234 -0
  191. package/src/testing/TestBrowser.ts +115 -0
  192. package/src/testing/TestPage.ts +10 -0
  193. package/src/testing/evidence.ts +154 -0
  194. package/src/testing/index.ts +3 -0
  195. package/src/testing/model.ts +222 -0
  196. package/src/testing/page.ts +360 -0
  197. package/src/workerd/Cdp.ts +2 -2
  198. package/dist/Browser-CxXUjxOp.mjs +0 -619
  199. package/dist/Browser-CxXUjxOp.mjs.map +0 -1
  200. package/dist/Cdp-CzCRnMlM.d.mts +0 -10
  201. package/dist/Cdp.d.mts +0 -11
  202. package/dist/Cdp.mjs.map +0 -1
  203. package/dist/Change-DNvztNLu.mjs +0 -138
  204. package/dist/Change-DNvztNLu.mjs.map +0 -1
  205. package/dist/Chromium.mjs.map +0 -1
  206. package/dist/Expect-u3P_ENLX.d.mts +0 -62
  207. package/dist/Expect.d.mts +0 -2
  208. package/dist/Expect.mjs +0 -114
  209. package/dist/Expect.mjs.map +0 -1
  210. package/dist/Moment.d.mts +0 -2
  211. package/dist/Moment.mjs.map +0 -1
  212. package/dist/Motion.d.mts +0 -2
  213. package/dist/Motion.mjs.map +0 -1
  214. package/dist/Page-BP02bmNv.mjs +0 -264
  215. package/dist/Page-BP02bmNv.mjs.map +0 -1
  216. package/dist/Plan-BG1WifW2.mjs.map +0 -1
  217. package/dist/Plan.d.mts +0 -2
  218. package/dist/Plan.mjs +0 -2
  219. package/dist/Policy.d.mts +0 -2
  220. package/dist/Policy.mjs.map +0 -1
  221. package/dist/Presentation-DxVikn9_.d.mts +0 -48
  222. package/dist/Presentation.d.mts +0 -2
  223. package/dist/Presentation.mjs +0 -118
  224. package/dist/Presentation.mjs.map +0 -1
  225. package/dist/Stage.d.mts +0 -2
  226. package/dist/Stage.mjs.map +0 -1
  227. package/dist/Supervisor-2mPjIYYs.mjs +0 -676
  228. package/dist/Supervisor-2mPjIYYs.mjs.map +0 -1
  229. package/dist/Supervisor-CfRxbaso.d.mts +0 -317
  230. package/dist/Supervisor.d.mts +0 -2
  231. package/dist/Supervisor.mjs +0 -2
  232. package/dist/cdp-BMpYMWYe.mjs.map +0 -1
  233. package/dist/context-EGhOqjNN.mjs +0 -335
  234. package/dist/context-EGhOqjNN.mjs.map +0 -1
  235. package/dist/page-PTM8m5wC.mjs +0 -4057
  236. package/dist/page-PTM8m5wC.mjs.map +0 -1
  237. package/dist/url-Bsp2y55M.mjs +0 -264
  238. package/dist/url-Bsp2y55M.mjs.map +0 -1
  239. package/dist/workerd/index.d.mts +0 -16
  240. package/dist/workerd/index.mjs +0 -17
  241. package/src/Expect.ts +0 -180
  242. package/src/Plan.ts +0 -674
  243. package/src/Presentation.ts +0 -196
  244. package/src/Supervisor.ts +0 -632
  245. package/src/internal/supervisor/lifecycle.ts +0 -484
  246. package/src/workerd/index.ts +0 -17
  247. /package/src/{internal/page/url.ts → Url.ts} +0 -0
  248. /package/src/{internal/pictures → stillness}/utilsBundle.d.ts +0 -0
@@ -1,619 +0,0 @@
1
- import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
- import { DialogShown, Disconnected, PageClosed, PageOpened, PageUntracked, RecordedEvent, SessionEnding } from "./BrowserEvent.mjs";
3
- import { BrowserError, Busy, Closed, EventHistoryExpired, Failed, InvalidRequest, Limit, Suspended } from "./BrowserError.mjs";
4
- import { c as undispatched, d as monotonicMillis, i as failWith, s as refuse, t as call, u as mapping } from "./context-EGhOqjNN.mjs";
5
- import { r as redact, s as FailFast } from "./url-Bsp2y55M.mjs";
6
- import { n as make$5 } from "./page-PTM8m5wC.mjs";
7
- import { Cause, Clock, Context, Crypto, DateTime, Deferred, Duration, Effect, Exit, Option, Queue, Schema, Scope, Semaphore, Stream } from "effect";
8
- //#region src/internal/input/gate.ts
9
- /**
10
- * A browser's input authority: stop admitting writes, drain admitted operations through their
11
- * records and native replies, then retain the stop for its scopes. Reads never enter this gate.
12
- */
13
- const without = (set, id) => new Set([...set].filter((each) => each !== id));
14
- const step = (state, input, stopped) => {
15
- switch (input._tag) {
16
- case "Enter": return state._tag === "Open" ? {
17
- ...state,
18
- inputs: /* @__PURE__ */ new Set([...state.inputs, input.id])
19
- } : state;
20
- case "End": {
21
- if (state._tag === "Held") return state;
22
- const inputs = without(state.inputs, input.id);
23
- return state._tag === "Draining" && inputs.size === 0 ? {
24
- _tag: "Held",
25
- claims: state.claims,
26
- stopped
27
- } : {
28
- ...state,
29
- inputs
30
- };
31
- }
32
- case "Suspend":
33
- if (state._tag !== "Open") return {
34
- ...state,
35
- claims: /* @__PURE__ */ new Set([...state.claims, input.id])
36
- };
37
- return state.inputs.size === 0 ? {
38
- _tag: "Held",
39
- claims: /* @__PURE__ */ new Set([input.id]),
40
- stopped
41
- } : {
42
- _tag: "Draining",
43
- inputs: state.inputs,
44
- claims: /* @__PURE__ */ new Set([input.id]),
45
- startedAt: stopped.at,
46
- deadline: stopped.at + input.drain
47
- };
48
- case "Leave": {
49
- if (state._tag === "Open" || !state.claims.has(input.id)) return state;
50
- const claims = without(state.claims, input.id);
51
- return claims.size > 0 ? {
52
- ...state,
53
- claims
54
- } : {
55
- _tag: "Open",
56
- inputs: state._tag === "Draining" ? state.inputs : /* @__PURE__ */ new Set()
57
- };
58
- }
59
- case "Expire": return state._tag === "Draining" && stopped.at >= state.deadline ? {
60
- _tag: "Open",
61
- inputs: state.inputs
62
- } : state;
63
- }
64
- };
65
- const make$4 = (options) => {
66
- let state = {
67
- _tag: "Open",
68
- inputs: /* @__PURE__ */ new Set()
69
- };
70
- let next = 0;
71
- let refused = Deferred.makeUnsafe();
72
- let ready = Deferred.makeUnsafe();
73
- const error = (operation) => new BrowserError({
74
- operation,
75
- reason: options.closedBy() === void 0 ? new Suspended() : new Closed({ cause: options.closedBy() ?? "connection" }),
76
- dispatched: false
77
- });
78
- const stopped = () => ({
79
- since: options.sequence(),
80
- at: options.now()
81
- });
82
- const apply = (input) => {
83
- if (input._tag !== "Expire" && state._tag === "Draining" && options.now() >= state.deadline) apply({ _tag: "Expire" });
84
- const before = state;
85
- state = step(state, input, stopped());
86
- if (input._tag === "Expire" && before._tag === "Draining" && state._tag === "Open") Deferred.doneUnsafe(ready, Exit.fail(new BrowserError({
87
- operation: "suspend",
88
- reason: new Busy({
89
- waitedMillis: Math.round(options.now() - before.startedAt),
90
- ahead: before.inputs.size
91
- }),
92
- dispatched: false
93
- })));
94
- if (before._tag === "Open" && state._tag !== "Open") Deferred.doneUnsafe(refused, Exit.void);
95
- if (before._tag === "Draining" && state._tag === "Held") Deferred.doneUnsafe(ready, Exit.succeed(state.stopped));
96
- if (before._tag !== "Open" && state._tag === "Open") {
97
- refused = Deferred.makeUnsafe();
98
- ready = Deferred.makeUnsafe();
99
- }
100
- return state;
101
- };
102
- const check = (operation) => Effect.suspend(() => state._tag === "Open" && options.closedBy() === void 0 ? Effect.void : Effect.fail(error(operation)));
103
- const suspend = (drain = options.drain) => Effect.suspend(() => {
104
- const duration = Duration.fromInput(drain);
105
- if (Option.isNone(duration) || !Duration.isFinite(duration.value) || Duration.toMillis(duration.value) < 0) return Effect.fail(new BrowserError({
106
- operation: "suspend",
107
- reason: new InvalidRequest({ detail: "drain must be a finite nonnegative duration" }),
108
- dispatched: false
109
- }));
110
- const id = ++next;
111
- const leave = Effect.sync(() => apply({
112
- _tag: "Leave",
113
- id
114
- }));
115
- return Effect.acquireRelease(Effect.gen(function* () {
116
- if (options.closedBy() !== void 0) return yield* error("suspend");
117
- const current = apply({
118
- _tag: "Suspend",
119
- id,
120
- drain: Duration.toMillis(duration.value)
121
- });
122
- if (current._tag === "Held") return current.stopped;
123
- if (current._tag !== "Draining") return yield* Effect.die("suspension did not retain authority");
124
- const pending = ready;
125
- const expire = Effect.suspend(() => {
126
- const left = current.deadline - options.now();
127
- return left > 0 ? Effect.sleep(Duration.millis(left)).pipe(Effect.andThen(expire)) : Effect.sync(() => apply({ _tag: "Expire" })).pipe(Effect.andThen(Deferred.await(pending)));
128
- });
129
- const wait = Effect.raceFirst(Deferred.await(pending), expire);
130
- return yield* Effect.raceFirst(wait, options.lost.pipe(Effect.flatMap(() => Effect.fail(error("suspend"))))).pipe(Effect.interruptible, Effect.filterOrFail(() => options.closedBy() === void 0, () => error("suspend")), Effect.onError(() => leave));
131
- }), () => leave);
132
- }).pipe(Effect.provideService(Clock.Clock, options.clock));
133
- return {
134
- check,
135
- suspend,
136
- refused: (operation) => Effect.suspend(() => state._tag === "Open" ? Deferred.await(refused).pipe(Effect.andThen(Effect.fail(error(operation)))) : Effect.fail(error(operation))),
137
- enter: (operation) => Effect.suspend(() => {
138
- if (state._tag !== "Open" || options.closedBy() !== void 0) return Effect.fail(error(operation));
139
- const id = ++next;
140
- apply({
141
- _tag: "Enter",
142
- id
143
- });
144
- return Effect.succeed(() => {
145
- apply({
146
- _tag: "End",
147
- id
148
- });
149
- });
150
- })
151
- };
152
- };
153
- //#endregion
154
- //#region src/internal/page/identity.ts
155
- /** Connection identity uses the supplied Crypto service, or the host's Web Crypto on Node/Bun. */
156
- const failed = () => undispatched("make", new Failed({ detail: "Could not name connection" }));
157
- const make$3 = Effect.gen(function* () {
158
- const supplied = yield* Effect.serviceOption(Crypto.Crypto);
159
- return yield* Option.isSome(supplied) ? supplied.value.randomUUIDv4().pipe(Effect.mapError(failed)) : Effect.try({
160
- try: () => globalThis.crypto.randomUUID(),
161
- catch: failed
162
- });
163
- });
164
- //#endregion
165
- //#region src/internal/page/openings.ts
166
- /** A page's input waits for the actual tabs it opened to finish the browser's registration. */
167
- const make$2 = Effect.fnUntraced(function* (playwright, cdp, registrations, closedBy) {
168
- const failed = (reason) => Exit.fail(new BrowserError({
169
- operation: "newPage",
170
- reason,
171
- dispatched: false
172
- }));
173
- let active;
174
- const gone = Deferred.makeUnsafe();
175
- const windowOpened = () => {
176
- if (active !== void 0) active.windows += 1;
177
- };
178
- const popup = (opened) => {
179
- if (active === void 0) return;
180
- const registered = registrations.get(opened);
181
- active.registrations.push(registered === void 0 ? failed(new Failed({ detail: "the popup has no registration" })) : Deferred.await(registered));
182
- Deferred.doneUnsafe(active.changed, Exit.void);
183
- active.changed = Deferred.makeUnsafe();
184
- };
185
- const clear = Effect.sync(() => {
186
- active = void 0;
187
- });
188
- yield* Effect.acquireRelease(Effect.sync(() => {
189
- cdp.on("Page.windowOpen", windowOpened);
190
- playwright.on("popup", popup);
191
- }), () => Effect.sync(() => {
192
- active = void 0;
193
- cdp.off("Page.windowOpen", windowOpened);
194
- playwright.off("popup", popup);
195
- Deferred.doneUnsafe(gone, failed(new Closed({ cause: closedBy() })));
196
- }));
197
- return {
198
- mark: () => {
199
- const mark = {
200
- windows: 0,
201
- registrations: [],
202
- changed: Deferred.makeUnsafe()
203
- };
204
- active = mark;
205
- return mark;
206
- },
207
- clear,
208
- settle: (mark) => Effect.gen(function* () {
209
- while (mark.registrations.length < mark.windows) yield* Deferred.await(mark.changed);
210
- yield* Effect.forEach(mark.registrations, (registration) => registration);
211
- }).pipe(Effect.raceFirst(Deferred.await(gone)), Effect.ensuring(clear))
212
- };
213
- });
214
- //#endregion
215
- //#region src/internal/timeline/events.ts
216
- /**
217
- * The browser's bounded event history and replay cursors. Readers share the same records and
218
- * wakeup generation, so a delayed reader cannot block input or retain an unbounded private queue.
219
- */
220
- /** The owning Browser validates capacity and closes this timeline with its scope. */
221
- const make$1 = (capacity) => {
222
- let records = [];
223
- let sequence = 0;
224
- let closed = false;
225
- let changed = Deferred.makeUnsafe();
226
- const wake = () => {
227
- const previous = changed;
228
- changed = Deferred.makeUnsafe();
229
- Deferred.doneUnsafe(previous, Effect.void);
230
- };
231
- const publish = (event) => {
232
- if (closed) return sequence;
233
- sequence += 1;
234
- const recorded = new RecordedEvent({
235
- sequence,
236
- event
237
- });
238
- records = records.length < capacity ? [...records, recorded] : [...records.slice(1), recorded];
239
- wake();
240
- return sequence;
241
- };
242
- const stream = (after) => Stream.unwrap(Effect.sync(() => {
243
- let cursor = after ?? sequence;
244
- if (!Number.isSafeInteger(cursor) || cursor < 0 || cursor > sequence) return Stream.fromEffect(refuse("events", "the event cursor must be a nonnegative integer no greater than the latest sequence"));
245
- return Stream.fromEffectRepeat(Effect.gen(function* () {
246
- while (true) {
247
- const oldest = records[0]?.sequence ?? sequence + 1;
248
- if (cursor < oldest - 1) return yield* undispatched("events", new EventHistoryExpired({
249
- after: cursor,
250
- oldest,
251
- latest: sequence
252
- }));
253
- const next = records[cursor - oldest + 1];
254
- if (next !== void 0) {
255
- cursor = next.sequence;
256
- return next;
257
- }
258
- if (closed) return yield* Cause.done();
259
- const waiting = changed;
260
- yield* Deferred.await(waiting);
261
- }
262
- }));
263
- }));
264
- return {
265
- publish,
266
- sequence: () => sequence,
267
- recent: Effect.sync(() => records),
268
- stream,
269
- close: Effect.sync(() => {
270
- if (closed) return;
271
- closed = true;
272
- wake();
273
- })
274
- };
275
- };
276
- //#endregion
277
- //#region src/Browser.ts
278
- /**
279
- * A live browser: its pages and what happens in it.
280
- *
281
- * `Browser` is a service with one implementation, built by a provider over a Playwright browser
282
- * context: `Chromium` launches one locally, `Cdp` connects to any DevTools endpoint and
283
- * `effect-browserbase` allocates a hosted one. Code written against `Browser` runs on all three.
284
- * A page is named by its CDP target id, so a new connection to the same browser finds it again.
285
- *
286
- * @since 0.3.0
287
- */
288
- var Browser_exports = /* @__PURE__ */ __exportAll({
289
- Browser: () => Browser,
290
- Suspension: () => Suspension,
291
- make: () => make
292
- });
293
- /** When admitted input finished; events after `since` describe the handoff interval. */
294
- var Suspension = class extends Schema.Class("effect-browser/Suspension")({
295
- since: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
296
- at: Schema.Finite
297
- }) {};
298
- var Browser = class extends Context.Service()("effect-browser/Browser") {};
299
- /** The settings every page shares, and how many events the browser keeps, from its options. */
300
- const settingsOf = Effect.fnUntraced(function* (options) {
301
- const invalid = (detail) => undispatched("make", new InvalidRequest({ detail }));
302
- const bound = (name, input, fallback) => {
303
- const decoded = Duration.fromInput(input ?? fallback);
304
- return Option.isSome(decoded) && Number.isFinite(Duration.toMillis(decoded.value)) && Duration.isPositive(decoded.value) ? Effect.succeed(decoded.value) : Effect.fail(invalid(`${name} must be finite and greater than zero`));
305
- };
306
- const settings = {
307
- actionTimeout: yield* bound("actionTimeout", options.actionTimeout, Duration.seconds(10)),
308
- navigationTimeout: yield* bound("navigationTimeout", options.navigationTimeout, Duration.seconds(30)),
309
- frameHistory: yield* bound("frameHistory", options.frameHistory, Duration.seconds(5)),
310
- guard: options.guard,
311
- policyTimeout: yield* bound("policyTimeout", options.policyTimeout, Duration.minutes(5))
312
- };
313
- const eventHistory = options.eventHistory ?? 4096;
314
- const { maxPages } = options;
315
- if (!Number.isSafeInteger(eventHistory) || eventHistory < 1) return yield* invalid("eventHistory must be a positive safe integer");
316
- if (maxPages !== void 0 && (!Number.isSafeInteger(maxPages) || maxPages < 1)) return yield* invalid("maxPages must be a positive safe integer");
317
- return {
318
- settings,
319
- eventHistory,
320
- maxPages
321
- };
322
- });
323
- /**
324
- * A provider that ends a session at `expiresAt` by its own clock can cut the connection a little
325
- * before this host's clock gets there.
326
- */
327
- const endMargin = Duration.seconds(5);
328
- /**
329
- * The browser's loss, once: its context closing from the other side, at or after the session's
330
- * end or before it, or `lose`, as the owner's scope releases it. Playwright leaves the calls in
331
- * flight on the library's own sessions unanswered when the connection drops, so `untilLost` fails
332
- * each with the browser, as a closed page's call fails.
333
- */
334
- const watchLoss = Effect.fnUntraced(function* (context, expiresAt, clock, disconnected) {
335
- let lostBy;
336
- const lost = yield* Deferred.make();
337
- const inFlight = /* @__PURE__ */ new Set();
338
- const gone = () => /* @__PURE__ */ new Error("Target page, context or browser has been closed");
339
- const lose = (cause) => {
340
- if (lostBy !== void 0) return;
341
- lostBy = cause;
342
- for (const reject of inFlight) reject(gone());
343
- disconnected(cause);
344
- Deferred.doneUnsafe(lost, Exit.succeed(cause));
345
- };
346
- const untilLost = (reply) => {
347
- if (lostBy !== void 0) {
348
- reply.catch(() => void 0);
349
- return Promise.reject(gone());
350
- }
351
- return new Promise((resolve, reject) => {
352
- inFlight.add(reject);
353
- reply.then(resolve, reject).finally(() => inFlight.delete(reject));
354
- });
355
- };
356
- const ended = () => expiresAt !== void 0 && clock.currentTimeMillisUnsafe() >= DateTime.toEpochMillis(expiresAt) - Duration.toMillis(endMargin);
357
- const onClose = () => lose(ended() ? "session" : "connection");
358
- context.on("close", onClose);
359
- yield* Effect.addFinalizer(() => Effect.sync(() => context.off("close", onClose)));
360
- if (context.browser()?.isConnected() === false) onClose();
361
- const native = (operation, run) => call(operation, run, () => lostBy ?? "page");
362
- return {
363
- lostBy: () => lostBy,
364
- lost: Deferred.await(lost),
365
- lose,
366
- untilLost,
367
- native
368
- };
369
- });
370
- /**
371
- * What a page's own Playwright events publish, its close and crash aside, while its scope is
372
- * open. Dialogs are answered: alerts accepted, others dismissed. The page's own session tells its
373
- * documents' commits and loads.
374
- */
375
- const listen = (playwright, page, publish, now, on) => {
376
- const dialog = (shown) => {
377
- const kind = shown.type();
378
- const answer = kind === "alert" || kind === "beforeunload" ? "accepted" : "dismissed";
379
- const message = shown.message().slice(0, 500);
380
- publish(new DialogShown({
381
- at: now(),
382
- page,
383
- kind,
384
- message,
385
- answer
386
- }));
387
- (answer === "accepted" ? shown.accept() : shown.dismiss()).catch(() => void 0);
388
- };
389
- return Effect.acquireRelease(Effect.sync(() => {
390
- playwright.on("close", on.close);
391
- playwright.on("crash", on.crash);
392
- playwright.on("dialog", dialog);
393
- }), () => Effect.sync(() => {
394
- playwright.off("close", on.close);
395
- playwright.off("crash", on.crash);
396
- playwright.off("dialog", dialog);
397
- }));
398
- };
399
- /**
400
- * Register each page the site opens, such as a popup, as Playwright reports it, for as long as the
401
- * scope is open. One that could not be tracked is published, unless it closed as it opened.
402
- */
403
- const followOpened = Effect.fnUntraced(function* (context, register, publish, now, registrations) {
404
- const opened = yield* Queue.unbounded();
405
- const onPage = (playwright) => {
406
- registrations.set(playwright, Deferred.makeUnsafe());
407
- Queue.offerUnsafe(opened, playwright);
408
- };
409
- const untracked = (playwright) => (error) => Effect.sync(() => {
410
- const url = redact(playwright.url());
411
- if (!playwright.isClosed()) publish(new PageUntracked({
412
- at: now(),
413
- url,
414
- detail: error.message
415
- }));
416
- });
417
- context.on("page", onPage);
418
- yield* Effect.addFinalizer(() => Effect.sync(() => context.off("page", onPage)));
419
- yield* Queue.take(opened).pipe(Effect.flatMap((playwright) => register(playwright).pipe(Effect.asVoid, Effect.onExit((exit) => Effect.sync(() => {
420
- const done = registrations.get(playwright);
421
- if (done !== void 0) Deferred.doneUnsafe(done, exit);
422
- })), Effect.catch(untracked(playwright)))), Effect.forever, Effect.forkScoped);
423
- });
424
- /** Release something native, on the owner's clock, giving up after a second: cleanup only. */
425
- const releaseNative = (clock) => (run) => Effect.tryPromise(run).pipe(Effect.interruptible, Effect.timeoutOrElse({
426
- duration: Duration.seconds(1),
427
- orElse: () => Effect.void
428
- }), Effect.provideService(Clock.Clock, clock), Effect.ignore);
429
- /** A page's own events among the browser's retained ones, and the openings of tabs it opened. */
430
- const eventsOf = (recent, page) => Effect.map(recent, (records) => records.flatMap(({ event }) => "page" in event && event.page === page || event._tag === "PageOpened" && event.opener === page ? [event] : []));
431
- /** A script as its documents run it: everywhere, or as a block where its `match` allows. */
432
- const scoped = ({ match, source }) => match === void 0 ? source : `if (${String(match)}.test(location.href)) {\n${source}\n}`;
433
- /**
434
- * At most `maxPages` open pages, those a site opened included, which are never refused: an open
435
- * past it waits for one to close, within `wait` on the owner's clock, then fails `Limit`, or fails
436
- * at once under `FailFast`. An open counts until its page is tracked.
437
- */
438
- const pageBudget = (maxPages, tracked, wait, clock) => {
439
- let opening = 0;
440
- let roomMade = Deferred.makeUnsafe();
441
- const makeRoom = () => {
442
- Deferred.doneUnsafe(roomMade, Exit.void);
443
- roomMade = Deferred.makeUnsafe();
444
- };
445
- const full = (limit) => tracked() + opening >= limit;
446
- const room = (limit) => Effect.gen(function* () {
447
- const refused = undispatched("newPage", new Limit({ maxPages: limit }));
448
- if ((yield* FailFast) && full(limit)) return yield* refused;
449
- yield* Effect.gen(function* () {
450
- while (full(limit)) yield* Deferred.await(roomMade);
451
- opening += 1;
452
- }).pipe(Effect.timeoutOrElse({
453
- duration: wait,
454
- orElse: () => Effect.fail(refused)
455
- }), Effect.provideService(Clock.Clock, clock));
456
- });
457
- const open = (opened) => maxPages === void 0 ? opened : Effect.uninterruptibleMask((restore) => restore(room(maxPages)).pipe(Effect.andThen(restore(opened).pipe(Effect.ensuring(Effect.sync(() => {
458
- opening -= 1;
459
- makeRoom();
460
- }))))));
461
- return {
462
- makeRoom,
463
- open
464
- };
465
- };
466
- /** A call sent now and awaited later, by whatever needs its answer. */
467
- const sentNow = (sent) => {
468
- sent.catch(() => void 0);
469
- return sent;
470
- };
471
- /** Close tab resources after publishing their close; a browser loss speaks for all its tabs. */
472
- const closures = (lostBy, publish, now) => Effect.gen(function* () {
473
- const closing = yield* Queue.unbounded();
474
- yield* Queue.take(closing).pipe(Effect.tap(({ page, cause }) => Effect.sync(() => {
475
- if (lostBy() === void 0) publish(new PageClosed({
476
- at: now(),
477
- page,
478
- cause
479
- }));
480
- })), Effect.flatMap(({ scope }) => Scope.close(scope, Exit.void)), Effect.forever, Effect.forkScoped);
481
- return closing;
482
- });
483
- /**
484
- * Build the service over a context the provider owns. The provider closes the context; this only
485
- * tracks its pages and events, for as long as the surrounding scope is open. Each page's protocol
486
- * session, capture and listeners end when that page closes or when the scope does, so a closed
487
- * tab retains nothing and the context keeps none of them afterwards.
488
- */
489
- const make = Effect.fn("Browser.make")(function* (context, info, options = {}) {
490
- const clock = yield* Clock.Clock;
491
- const connection = yield* make$3;
492
- const now = () => monotonicMillis(clock);
493
- const { settings, eventHistory, maxPages } = yield* settingsOf(options);
494
- const mapping$1 = mapping(now);
495
- const timeline = make$1(eventHistory);
496
- yield* Effect.addFinalizer(() => timeline.close);
497
- const publish = timeline.publish;
498
- if (info.expiresAt !== void 0) publish(new SessionEnding({
499
- at: now(),
500
- expiresAt: info.expiresAt
501
- }));
502
- const { lostBy, lost, lose, untilLost, native } = yield* watchLoss(context, info.expiresAt, clock, (cause) => publish(new Disconnected({
503
- at: now(),
504
- cause
505
- })));
506
- const inputGate = make$4({
507
- now,
508
- sequence: timeline.sequence,
509
- closedBy: lostBy,
510
- lost,
511
- clock,
512
- drain: settings.navigationTimeout
513
- });
514
- const registry = /* @__PURE__ */ new Map();
515
- const registrations = /* @__PURE__ */ new WeakMap();
516
- const registering = yield* Semaphore.make(1);
517
- const pagesScope = yield* Scope.fork(yield* Scope.Scope);
518
- const release = releaseNative(clock);
519
- const budget = pageBudget(maxPages, () => registry.size, settings.actionTimeout, clock);
520
- const closing = yield* closures(lostBy, publish, now);
521
- const register = (playwright) => registering.withPermits(1)(Effect.gen(function* () {
522
- const known = registry.get(playwright);
523
- if (known !== void 0) return known;
524
- if (playwright.isClosed()) return yield* failWith("newPage", new Failed({ detail: "the page closed as it opened" }));
525
- const scope = yield* Scope.fork(pagesScope);
526
- return yield* Effect.gen(function* () {
527
- let crashed = false;
528
- const closedBy = () => lostBy() ?? (crashed ? "crashed" : "page");
529
- const cdp = yield* native("newPage", () => context.newCDPSession(playwright));
530
- yield* Effect.addFinalizer(() => release(() => cdp.detach()));
531
- const send = (method, params) => untilLost(cdp.send(method, params));
532
- const focusing = sentNow(send("Emulation.setFocusEmulationEnabled", { enabled: true }));
533
- const paging = sentNow(send("Page.enable"));
534
- const { targetInfo } = yield* native("newPage", () => send("Target.getTargetInfo"));
535
- const id = targetInfo.targetId;
536
- const page = yield* make$5({
537
- id,
538
- session: info.id,
539
- connection,
540
- url: targetInfo.url,
541
- playwright,
542
- cdp,
543
- untilLost,
544
- settings,
545
- inputGate,
546
- clock,
547
- mapping: mapping$1,
548
- publish,
549
- recentEvents: eventsOf(timeline.recent, id),
550
- openings: yield* make$2(playwright, cdp, registrations, closedBy),
551
- focused: call("focus", () => focusing, closedBy).pipe(Effect.asVoid),
552
- paging: (operation) => call(operation, () => paging, closedBy).pipe(Effect.asVoid),
553
- closedBy,
554
- capture: info.capture
555
- });
556
- const close = () => {
557
- if (registry.get(playwright) !== page) return;
558
- registry.delete(playwright);
559
- budget.makeRoom();
560
- Queue.offerUnsafe(closing, {
561
- scope,
562
- page: id,
563
- cause: crashed ? "crashed" : "page"
564
- });
565
- };
566
- const crash = () => {
567
- crashed = true;
568
- playwright.close().catch(() => void 0);
569
- };
570
- registry.set(playwright, page);
571
- publish(new PageOpened({
572
- at: now(),
573
- page: id,
574
- url: redact(targetInfo.url),
575
- opener: targetInfo.openerId
576
- }));
577
- yield* listen(playwright, id, publish, now, {
578
- close,
579
- crash
580
- });
581
- if (playwright.isClosed()) close();
582
- return page;
583
- }).pipe(Scope.provide(scope), Effect.onError(() => Scope.close(scope, Exit.void)));
584
- }));
585
- for (const script of options.initScripts ?? []) yield* native("initScripts", () => context.addInitScript({ content: scoped(script) }));
586
- yield* followOpened(context, register, publish, now, registrations);
587
- for (const existing of context.pages()) yield* register(existing);
588
- const pages = Effect.sync(() => [...registry.values()]);
589
- const newPage = Effect.fn("Browser.newPage")(function* (url) {
590
- const page = yield* budget.open(native("newPage", () => context.newPage()).pipe(Effect.flatMap((playwright) => register(playwright).pipe(Effect.onError(() => release(() => playwright.close()))))));
591
- if (url !== void 0) yield* page.goto(url).pipe(Effect.onError(() => page.close.pipe(Effect.ignore)));
592
- return page;
593
- });
594
- yield* Effect.addFinalizer(() => Effect.sync(() => lose("released")));
595
- const service = {
596
- id: info.id,
597
- connection,
598
- provider: info.provider,
599
- expiresAt: info.expiresAt,
600
- now: Effect.sync(now),
601
- suspend: (options = {}) => Effect.map(inputGate.suspend(options.drain), (held) => new Suspension(held)),
602
- context,
603
- pages,
604
- page: (id) => Effect.map(pages, (open) => Option.fromNullishOr(open.find((page) => page.id === id))),
605
- firstPage: pages.pipe(Effect.map((open) => Option.fromNullishOr(open[0])), Effect.flatMap(Option.match({
606
- onNone: () => newPage(),
607
- onSome: Effect.succeed
608
- }))),
609
- newPage,
610
- disconnected: lost,
611
- events: (eventOptions = {}) => timeline.stream(eventOptions.after),
612
- recentEvents: timeline.recent.pipe(Effect.map((records) => records.map((record) => record.event)))
613
- };
614
- return Browser.of(service);
615
- });
616
- //#endregion
617
- export { make as i, Browser_exports as n, Suspension as r, Browser as t };
618
-
619
- //# sourceMappingURL=Browser-CxXUjxOp.mjs.map