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
@@ -1,629 +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-w3NJPRLo.mjs";
5
- import { c as FailFast, i as redact } from "./url-Cd04Okid.mjs";
6
- import { n as make$5 } from "./page-BKwfF6z-.mjs";
7
- import { Cause, Clock, Context, Crypto, DateTime, Deferred, Duration, Effect, Exit, Option, PlatformError, 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 native = () => Crypto.make({
157
- randomBytes: (size) => globalThis.crypto.getRandomValues(new Uint8Array(size)),
158
- digest: (algorithm, data) => Effect.tryPromise({
159
- try: () => globalThis.crypto.subtle.digest(algorithm, new Uint8Array(data)),
160
- catch: (cause) => PlatformError.systemError({
161
- module: "Crypto",
162
- method: "digest",
163
- _tag: "Unknown",
164
- description: "Could not compute digest",
165
- cause
166
- })
167
- }).pipe(Effect.map((bytes) => new Uint8Array(bytes)))
168
- });
169
- const make$3 = Effect.gen(function* () {
170
- const supplied = yield* Effect.serviceOption(Crypto.Crypto);
171
- const crypto = Option.getOrElse(supplied, native);
172
- return yield* Effect.mapError(crypto.randomUUIDv4(), () => undispatched("make", new Failed({ detail: "Could not name connection" })));
173
- });
174
- //#endregion
175
- //#region src/internal/page/openings.ts
176
- /** A page's input waits for the actual tabs it opened to finish the browser's registration. */
177
- const make$2 = Effect.fnUntraced(function* (playwright, cdp, registrations, closedBy) {
178
- const failed = (reason) => Exit.fail(new BrowserError({
179
- operation: "newPage",
180
- reason,
181
- dispatched: false
182
- }));
183
- let active;
184
- const gone = Deferred.makeUnsafe();
185
- const windowOpened = () => {
186
- if (active !== void 0) active.windows += 1;
187
- };
188
- const popup = (opened) => {
189
- if (active === void 0) return;
190
- const registered = registrations.get(opened);
191
- active.registrations.push(registered === void 0 ? failed(new Failed({ detail: "the popup has no registration" })) : Deferred.await(registered));
192
- Deferred.doneUnsafe(active.changed, Exit.void);
193
- active.changed = Deferred.makeUnsafe();
194
- };
195
- const clear = Effect.sync(() => {
196
- active = void 0;
197
- });
198
- yield* Effect.acquireRelease(Effect.sync(() => {
199
- cdp.on("Page.windowOpen", windowOpened);
200
- playwright.on("popup", popup);
201
- }), () => Effect.sync(() => {
202
- active = void 0;
203
- cdp.off("Page.windowOpen", windowOpened);
204
- playwright.off("popup", popup);
205
- Deferred.doneUnsafe(gone, failed(new Closed({ cause: closedBy() })));
206
- }));
207
- return {
208
- mark: () => {
209
- const mark = {
210
- windows: 0,
211
- registrations: [],
212
- changed: Deferred.makeUnsafe()
213
- };
214
- active = mark;
215
- return mark;
216
- },
217
- clear,
218
- settle: (mark) => Effect.gen(function* () {
219
- while (mark.registrations.length < mark.windows) yield* Deferred.await(mark.changed);
220
- yield* Effect.forEach(mark.registrations, (registration) => registration);
221
- }).pipe(Effect.raceFirst(Deferred.await(gone)), Effect.ensuring(clear))
222
- };
223
- });
224
- //#endregion
225
- //#region src/internal/timeline/events.ts
226
- /**
227
- * The browser's bounded event history and replay cursors. Readers share the same records and
228
- * wakeup generation, so a delayed reader cannot block input or retain an unbounded private queue.
229
- */
230
- /** The owning Browser validates capacity and closes this timeline with its scope. */
231
- const make$1 = (capacity) => {
232
- let records = [];
233
- let sequence = 0;
234
- let closed = false;
235
- let changed = Deferred.makeUnsafe();
236
- const wake = () => {
237
- const previous = changed;
238
- changed = Deferred.makeUnsafe();
239
- Deferred.doneUnsafe(previous, Effect.void);
240
- };
241
- const publish = (event) => {
242
- if (closed) return sequence;
243
- sequence += 1;
244
- const recorded = new RecordedEvent({
245
- sequence,
246
- event
247
- });
248
- records = records.length < capacity ? [...records, recorded] : [...records.slice(1), recorded];
249
- wake();
250
- return sequence;
251
- };
252
- const stream = (after) => Stream.unwrap(Effect.sync(() => {
253
- let cursor = after ?? sequence;
254
- 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"));
255
- return Stream.fromEffectRepeat(Effect.gen(function* () {
256
- while (true) {
257
- const oldest = records[0]?.sequence ?? sequence + 1;
258
- if (cursor < oldest - 1) return yield* undispatched("events", new EventHistoryExpired({
259
- after: cursor,
260
- oldest,
261
- latest: sequence
262
- }));
263
- const next = records[cursor - oldest + 1];
264
- if (next !== void 0) {
265
- cursor = next.sequence;
266
- return next;
267
- }
268
- if (closed) return yield* Cause.done();
269
- const waiting = changed;
270
- yield* Deferred.await(waiting);
271
- }
272
- }));
273
- }));
274
- return {
275
- publish,
276
- sequence: () => sequence,
277
- recent: Effect.sync(() => records),
278
- stream,
279
- close: Effect.sync(() => {
280
- if (closed) return;
281
- closed = true;
282
- wake();
283
- })
284
- };
285
- };
286
- //#endregion
287
- //#region src/Browser.ts
288
- /**
289
- * A live browser: its pages and what happens in it.
290
- *
291
- * `Browser` is a service with one implementation, built by a provider over a Playwright browser
292
- * context: `Chromium` launches one locally, `Cdp` connects to any DevTools endpoint and
293
- * `effect-browserbase` allocates a hosted one. Code written against `Browser` runs on all three.
294
- * A page is named by its CDP target id, so a new connection to the same browser finds it again.
295
- *
296
- * @since 0.3.0
297
- */
298
- var Browser_exports = /* @__PURE__ */ __exportAll({
299
- Browser: () => Browser,
300
- Suspension: () => Suspension,
301
- make: () => make
302
- });
303
- /** When admitted input finished; events after `since` describe the handoff interval. */
304
- var Suspension = class extends Schema.Class("effect-browser/Suspension")({
305
- since: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
306
- at: Schema.Finite
307
- }) {};
308
- var Browser = class extends Context.Service()("effect-browser/Browser") {};
309
- /** The settings every page shares, and how many events the browser keeps, from its options. */
310
- const settingsOf = Effect.fnUntraced(function* (options) {
311
- const invalid = (detail) => undispatched("make", new InvalidRequest({ detail }));
312
- const bound = (name, input, fallback) => {
313
- const decoded = Duration.fromInput(input ?? fallback);
314
- 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`));
315
- };
316
- const settings = {
317
- actionTimeout: yield* bound("actionTimeout", options.actionTimeout, Duration.seconds(10)),
318
- navigationTimeout: yield* bound("navigationTimeout", options.navigationTimeout, Duration.seconds(30)),
319
- frameHistory: yield* bound("frameHistory", options.frameHistory, Duration.seconds(5)),
320
- guard: options.guard,
321
- policyTimeout: yield* bound("policyTimeout", options.policyTimeout, Duration.minutes(5))
322
- };
323
- const eventHistory = options.eventHistory ?? 4096;
324
- const { maxPages } = options;
325
- if (!Number.isSafeInteger(eventHistory) || eventHistory < 1) return yield* invalid("eventHistory must be a positive safe integer");
326
- if (maxPages !== void 0 && (!Number.isSafeInteger(maxPages) || maxPages < 1)) return yield* invalid("maxPages must be a positive safe integer");
327
- return {
328
- settings,
329
- eventHistory,
330
- maxPages
331
- };
332
- });
333
- /**
334
- * A provider that ends a session at `expiresAt` by its own clock can cut the connection a little
335
- * before this host's clock gets there.
336
- */
337
- const endMargin = Duration.seconds(5);
338
- /**
339
- * The browser's loss, once: its context closing from the other side, at or after the session's
340
- * end or before it, or `lose`, as the owner's scope releases it. Playwright leaves the calls in
341
- * flight on the library's own sessions unanswered when the connection drops, so `untilLost` fails
342
- * each with the browser, as a closed page's call fails.
343
- */
344
- const watchLoss = Effect.fnUntraced(function* (context, expiresAt, clock, disconnected) {
345
- let lostBy;
346
- const lost = yield* Deferred.make();
347
- const inFlight = /* @__PURE__ */ new Set();
348
- const gone = () => /* @__PURE__ */ new Error("Target page, context or browser has been closed");
349
- const lose = (cause) => {
350
- if (lostBy !== void 0) return;
351
- lostBy = cause;
352
- for (const reject of inFlight) reject(gone());
353
- disconnected(cause);
354
- Deferred.doneUnsafe(lost, Exit.succeed(cause));
355
- };
356
- const untilLost = (reply) => {
357
- if (lostBy !== void 0) {
358
- reply.catch(() => void 0);
359
- return Promise.reject(gone());
360
- }
361
- return new Promise((resolve, reject) => {
362
- inFlight.add(reject);
363
- reply.then(resolve, reject).finally(() => inFlight.delete(reject));
364
- });
365
- };
366
- const ended = () => expiresAt !== void 0 && clock.currentTimeMillisUnsafe() >= DateTime.toEpochMillis(expiresAt) - Duration.toMillis(endMargin);
367
- const onClose = () => lose(ended() ? "session" : "connection");
368
- context.on("close", onClose);
369
- yield* Effect.addFinalizer(() => Effect.sync(() => context.off("close", onClose)));
370
- if (context.browser()?.isConnected() === false) onClose();
371
- const native = (operation, run) => call(operation, run, () => lostBy ?? "page");
372
- return {
373
- lostBy: () => lostBy,
374
- lost: Deferred.await(lost),
375
- lose,
376
- untilLost,
377
- native
378
- };
379
- });
380
- /**
381
- * What a page's own Playwright events publish, its close and crash aside, while its scope is
382
- * open. Dialogs are answered: alerts accepted, others dismissed. The page's own session tells its
383
- * documents' commits and loads.
384
- */
385
- const listen = (playwright, page, publish, now, on) => {
386
- const dialog = (shown) => {
387
- const kind = shown.type();
388
- const answer = kind === "alert" || kind === "beforeunload" ? "accepted" : "dismissed";
389
- const message = shown.message().slice(0, 500);
390
- publish(new DialogShown({
391
- at: now(),
392
- page,
393
- kind,
394
- message,
395
- answer
396
- }));
397
- (answer === "accepted" ? shown.accept() : shown.dismiss()).catch(() => void 0);
398
- };
399
- return Effect.acquireRelease(Effect.sync(() => {
400
- playwright.on("close", on.close);
401
- playwright.on("crash", on.crash);
402
- playwright.on("dialog", dialog);
403
- }), () => Effect.sync(() => {
404
- playwright.off("close", on.close);
405
- playwright.off("crash", on.crash);
406
- playwright.off("dialog", dialog);
407
- }));
408
- };
409
- /**
410
- * Register each page the site opens, such as a popup, as Playwright reports it, for as long as the
411
- * scope is open. One that could not be tracked is published, unless it closed as it opened.
412
- */
413
- const followOpened = Effect.fnUntraced(function* (context, register, publish, now, registrations) {
414
- const opened = yield* Queue.unbounded();
415
- const onPage = (playwright) => {
416
- registrations.set(playwright, Deferred.makeUnsafe());
417
- Queue.offerUnsafe(opened, playwright);
418
- };
419
- const untracked = (playwright) => (error) => Effect.sync(() => {
420
- const url = redact(playwright.url());
421
- if (!playwright.isClosed()) publish(new PageUntracked({
422
- at: now(),
423
- url,
424
- detail: error.message
425
- }));
426
- });
427
- context.on("page", onPage);
428
- yield* Effect.addFinalizer(() => Effect.sync(() => context.off("page", onPage)));
429
- yield* Queue.take(opened).pipe(Effect.flatMap((playwright) => register(playwright).pipe(Effect.asVoid, Effect.onExit((exit) => Effect.sync(() => {
430
- const done = registrations.get(playwright);
431
- if (done !== void 0) Deferred.doneUnsafe(done, exit);
432
- })), Effect.catch(untracked(playwright)))), Effect.forever, Effect.forkScoped);
433
- });
434
- /** Release something native, on the owner's clock, giving up after a second: cleanup only. */
435
- const releaseNative = (clock) => (run) => Effect.tryPromise(run).pipe(Effect.interruptible, Effect.timeoutOrElse({
436
- duration: Duration.seconds(1),
437
- orElse: () => Effect.void
438
- }), Effect.provideService(Clock.Clock, clock), Effect.ignore);
439
- /** A page's own events among the browser's retained ones, and the openings of tabs it opened. */
440
- const eventsOf = (recent, page) => Effect.map(recent, (records) => records.flatMap(({ event }) => "page" in event && event.page === page || event._tag === "PageOpened" && event.opener === page ? [event] : []));
441
- /** A script as its documents run it: everywhere, or as a block where its `match` allows. */
442
- const scoped = ({ match, source }) => match === void 0 ? source : `if (${String(match)}.test(location.href)) {\n${source}\n}`;
443
- /**
444
- * At most `maxPages` open pages, those a site opened included, which are never refused: an open
445
- * past it waits for one to close, within `wait` on the owner's clock, then fails `Limit`, or fails
446
- * at once under `FailFast`. An open counts until its page is tracked.
447
- */
448
- const pageBudget = (maxPages, tracked, wait, clock) => {
449
- let opening = 0;
450
- let roomMade = Deferred.makeUnsafe();
451
- const makeRoom = () => {
452
- Deferred.doneUnsafe(roomMade, Exit.void);
453
- roomMade = Deferred.makeUnsafe();
454
- };
455
- const full = (limit) => tracked() + opening >= limit;
456
- const room = (limit) => Effect.gen(function* () {
457
- const refused = undispatched("newPage", new Limit({ maxPages: limit }));
458
- if ((yield* FailFast) && full(limit)) return yield* refused;
459
- yield* Effect.gen(function* () {
460
- while (full(limit)) yield* Deferred.await(roomMade);
461
- opening += 1;
462
- }).pipe(Effect.timeoutOrElse({
463
- duration: wait,
464
- orElse: () => Effect.fail(refused)
465
- }), Effect.provideService(Clock.Clock, clock));
466
- });
467
- const open = (opened) => maxPages === void 0 ? opened : Effect.uninterruptibleMask((restore) => restore(room(maxPages)).pipe(Effect.andThen(restore(opened).pipe(Effect.ensuring(Effect.sync(() => {
468
- opening -= 1;
469
- makeRoom();
470
- }))))));
471
- return {
472
- makeRoom,
473
- open
474
- };
475
- };
476
- /** A call sent now and awaited later, by whatever needs its answer. */
477
- const sentNow = (sent) => {
478
- sent.catch(() => void 0);
479
- return sent;
480
- };
481
- /** Close tab resources after publishing their close; a browser loss speaks for all its tabs. */
482
- const closures = (lostBy, publish, now) => Effect.gen(function* () {
483
- const closing = yield* Queue.unbounded();
484
- yield* Queue.take(closing).pipe(Effect.tap(({ page, cause }) => Effect.sync(() => {
485
- if (lostBy() === void 0) publish(new PageClosed({
486
- at: now(),
487
- page,
488
- cause
489
- }));
490
- })), Effect.flatMap(({ scope }) => Scope.close(scope, Exit.void)), Effect.forever, Effect.forkScoped);
491
- return closing;
492
- });
493
- /**
494
- * Build the service over a context the provider owns. The provider closes the context; this only
495
- * tracks its pages and events, for as long as the surrounding scope is open. Each page's protocol
496
- * session, capture and listeners end when that page closes or when the scope does, so a closed
497
- * tab retains nothing and the context keeps none of them afterwards.
498
- */
499
- const make = Effect.fn("Browser.make")(function* (context, info, options = {}) {
500
- const clock = yield* Clock.Clock;
501
- const connection = yield* make$3;
502
- const now = () => monotonicMillis(clock);
503
- const { settings, eventHistory, maxPages } = yield* settingsOf(options);
504
- const mapping$1 = mapping(now);
505
- const timeline = make$1(eventHistory);
506
- yield* Effect.addFinalizer(() => timeline.close);
507
- const publish = timeline.publish;
508
- if (info.expiresAt !== void 0) publish(new SessionEnding({
509
- at: now(),
510
- expiresAt: info.expiresAt
511
- }));
512
- const { lostBy, lost, lose, untilLost, native } = yield* watchLoss(context, info.expiresAt, clock, (cause) => publish(new Disconnected({
513
- at: now(),
514
- cause
515
- })));
516
- const inputGate = make$4({
517
- now,
518
- sequence: timeline.sequence,
519
- closedBy: lostBy,
520
- lost,
521
- clock,
522
- drain: settings.navigationTimeout
523
- });
524
- const registry = /* @__PURE__ */ new Map();
525
- const registrations = /* @__PURE__ */ new WeakMap();
526
- const registering = yield* Semaphore.make(1);
527
- const pagesScope = yield* Scope.fork(yield* Scope.Scope);
528
- const release = releaseNative(clock);
529
- const budget = pageBudget(maxPages, () => registry.size, settings.actionTimeout, clock);
530
- const closing = yield* closures(lostBy, publish, now);
531
- const register = (playwright) => registering.withPermits(1)(Effect.gen(function* () {
532
- const known = registry.get(playwright);
533
- if (known !== void 0) return known;
534
- if (playwright.isClosed()) return yield* failWith("newPage", new Failed({ detail: "the page closed as it opened" }));
535
- const scope = yield* Scope.fork(pagesScope);
536
- return yield* Effect.gen(function* () {
537
- let crashed = false;
538
- const closedBy = () => lostBy() ?? (crashed ? "crashed" : "page");
539
- const cdp = yield* native("newPage", () => context.newCDPSession(playwright));
540
- yield* Effect.addFinalizer(() => release(() => cdp.detach()));
541
- const send = (method, params) => untilLost(cdp.send(method, params));
542
- const focusing = sentNow(send("Emulation.setFocusEmulationEnabled", { enabled: true }));
543
- const paging = sentNow(send("Page.enable"));
544
- const { targetInfo } = yield* native("newPage", () => send("Target.getTargetInfo"));
545
- const id = targetInfo.targetId;
546
- const page = yield* make$5({
547
- id,
548
- session: info.id,
549
- connection,
550
- url: targetInfo.url,
551
- playwright,
552
- cdp,
553
- untilLost,
554
- settings,
555
- inputGate,
556
- clock,
557
- mapping: mapping$1,
558
- publish,
559
- recentEvents: eventsOf(timeline.recent, id),
560
- openings: yield* make$2(playwright, cdp, registrations, closedBy),
561
- focused: call("focus", () => focusing, closedBy).pipe(Effect.asVoid),
562
- paging: (operation) => call(operation, () => paging, closedBy).pipe(Effect.asVoid),
563
- closedBy,
564
- capture: info.capture
565
- });
566
- const close = () => {
567
- if (registry.get(playwright) !== page) return;
568
- registry.delete(playwright);
569
- budget.makeRoom();
570
- Queue.offerUnsafe(closing, {
571
- scope,
572
- page: id,
573
- cause: crashed ? "crashed" : "page"
574
- });
575
- };
576
- const crash = () => {
577
- crashed = true;
578
- playwright.close().catch(() => void 0);
579
- };
580
- registry.set(playwright, page);
581
- publish(new PageOpened({
582
- at: now(),
583
- page: id,
584
- url: redact(targetInfo.url),
585
- opener: targetInfo.openerId
586
- }));
587
- yield* listen(playwright, id, publish, now, {
588
- close,
589
- crash
590
- });
591
- if (playwright.isClosed()) close();
592
- return page;
593
- }).pipe(Scope.provide(scope), Effect.onError(() => Scope.close(scope, Exit.void)));
594
- }));
595
- for (const script of options.initScripts ?? []) yield* native("initScripts", () => context.addInitScript({ content: scoped(script) }));
596
- yield* followOpened(context, register, publish, now, registrations);
597
- for (const existing of context.pages()) yield* register(existing);
598
- const pages = Effect.sync(() => [...registry.values()]);
599
- const newPage = Effect.fn("Browser.newPage")(function* (url) {
600
- const page = yield* budget.open(native("newPage", () => context.newPage()).pipe(Effect.flatMap((playwright) => register(playwright).pipe(Effect.onError(() => release(() => playwright.close()))))));
601
- if (url !== void 0) yield* page.goto(url).pipe(Effect.onError(() => page.close.pipe(Effect.ignore)));
602
- return page;
603
- });
604
- yield* Effect.addFinalizer(() => Effect.sync(() => lose("released")));
605
- const service = {
606
- id: info.id,
607
- connection,
608
- provider: info.provider,
609
- expiresAt: info.expiresAt,
610
- now: Effect.sync(now),
611
- suspend: (options = {}) => Effect.map(inputGate.suspend(options.drain), (held) => new Suspension(held)),
612
- context,
613
- pages,
614
- page: (id) => Effect.map(pages, (open) => Option.fromNullishOr(open.find((page) => page.id === id))),
615
- firstPage: pages.pipe(Effect.map((open) => Option.fromNullishOr(open[0])), Effect.flatMap(Option.match({
616
- onNone: () => newPage(),
617
- onSome: Effect.succeed
618
- }))),
619
- newPage,
620
- disconnected: lost,
621
- events: (eventOptions = {}) => timeline.stream(eventOptions.after),
622
- recentEvents: timeline.recent.pipe(Effect.map((records) => records.map((record) => record.event)))
623
- };
624
- return Browser.of(service);
625
- });
626
- //#endregion
627
- export { make as i, Browser_exports as n, Suspension as r, Browser as t };
628
-
629
- //# sourceMappingURL=Browser-BwCldIff.mjs.map