effect-browser 0.3.0-beta.21 → 0.3.0-beta.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -159
- package/dist/Action-24sLOubi.d.mts +110 -0
- package/dist/Action.d.mts +2 -0
- package/dist/Action.mjs +141 -0
- package/dist/Action.mjs.map +1 -0
- package/dist/ActionPort.d.mts +15 -0
- package/dist/ActionPort.mjs +101 -0
- package/dist/ActionPort.mjs.map +1 -0
- package/dist/Browser-BKPAbyys.mjs +4076 -0
- package/dist/Browser-BKPAbyys.mjs.map +1 -0
- package/dist/{Browser-CiXHT1yO.d.mts → Browser-wZhwZSlG.d.mts} +51 -13
- package/dist/Browser.d.mts +2 -2
- package/dist/Browser.mjs +1 -1
- package/dist/{BrowserError-Coy7EW1N.d.mts → BrowserError-BlcmLVzn.d.mts} +22 -7
- package/dist/BrowserError.d.mts +2 -2
- package/dist/BrowserError.mjs +46 -9
- package/dist/BrowserError.mjs.map +1 -1
- package/dist/{BrowserEvent-dq6FRT8w.d.mts → BrowserEvent-lLqJ3GJS.d.mts} +93 -37
- package/dist/BrowserEvent.d.mts +2 -2
- package/dist/BrowserEvent.mjs +29 -24
- package/dist/BrowserEvent.mjs.map +1 -1
- package/dist/{Change-DqING3tr.d.mts → Change-Ba7INmay.d.mts} +4 -6
- package/dist/Change.d.mts +2 -2
- package/dist/Change.mjs +97 -2
- package/dist/Change.mjs.map +1 -0
- package/dist/Clock.d.mts +57 -0
- package/dist/Clock.mjs +80 -0
- package/dist/Clock.mjs.map +1 -0
- package/dist/{Frame-DkNdjjjr.d.mts → Frame-UEH2hO5T.d.mts} +10 -3
- package/dist/Frame.d.mts +2 -2
- package/dist/Frame.mjs +10 -2
- package/dist/Frame.mjs.map +1 -1
- package/dist/{Motion-DND6I-64.d.mts → Geometry-DJx9qrYy.d.mts} +7 -10
- package/dist/Geometry.d.mts +2 -0
- package/dist/Geometry.mjs +32 -0
- package/dist/Geometry.mjs.map +1 -0
- package/dist/Matching.d.mts +37 -0
- package/dist/Matching.mjs +72 -0
- package/dist/Matching.mjs.map +1 -0
- package/dist/Page-BF5izr8R.mjs +795 -0
- package/dist/Page-BF5izr8R.mjs.map +1 -0
- package/dist/{Page-ku6j5t-v.d.mts → Page-CvsOEorc.d.mts} +158 -185
- package/dist/Page.d.mts +2 -2
- package/dist/Page.mjs +1 -1
- package/dist/Released-tURbFmal.d.mts +15 -0
- package/dist/{Snapshot-B34hZ4Jl.d.mts → Snapshot-CtBVOtuu.d.mts} +6 -1
- package/dist/Snapshot.d.mts +1 -1
- package/dist/Snapshot.mjs.map +1 -1
- package/dist/Stillness-CNlvayIo.mjs +136 -0
- package/dist/Stillness-CNlvayIo.mjs.map +1 -0
- package/dist/Style-AO4V7Czt.mjs +357 -0
- package/dist/Style-AO4V7Czt.mjs.map +1 -0
- package/dist/Style.d.mts +72 -0
- package/dist/Style.mjs +2 -0
- package/dist/Url.d.mts +26 -0
- package/dist/Url.mjs +74 -0
- package/dist/Url.mjs.map +1 -0
- package/dist/Window-CMddGMt1.d.mts +34 -0
- package/dist/Window.d.mts +2 -0
- package/dist/Window.mjs +35 -0
- package/dist/Window.mjs.map +1 -0
- package/dist/{cdp-DL7XfEj5.d.mts → connection-2KHI4_KN.d.mts} +4 -4
- package/dist/{cdp-BMpYMWYe.mjs → connection-CK83pvqP.mjs} +5 -6
- package/dist/connection-CK83pvqP.mjs.map +1 -0
- package/dist/index.d.mts +15 -18
- package/dist/index.mjs +11 -15
- package/dist/moment/Moment.d.mts +86 -0
- package/dist/{Moment.mjs → moment/Moment.mjs} +44 -38
- package/dist/moment/Moment.mjs.map +1 -0
- package/dist/{Plan-DJ4hEEna.d.mts → plan/index.d.mts} +169 -64
- package/dist/{Plan-BG1WifW2.mjs → plan/index.mjs} +122 -199
- package/dist/plan/index.mjs.map +1 -0
- package/dist/{Policy-BGWjbqCl.d.mts → policy/Policy.d.mts} +16 -20
- package/dist/{Policy.mjs → policy/Policy.mjs} +11 -21
- package/dist/policy/Policy.mjs.map +1 -0
- package/dist/presentation/Motion.d.mts +12 -0
- package/dist/{Motion.mjs → presentation/Motion.mjs} +4 -27
- package/dist/presentation/Motion.mjs.map +1 -0
- package/dist/presentation/Presentation.d.mts +30 -0
- package/dist/presentation/Presentation.mjs +105 -0
- package/dist/presentation/Presentation.mjs.map +1 -0
- package/dist/{Stage-NHp8N81k.d.mts → presentation/Stage.d.mts} +12 -15
- package/dist/{Stage.mjs → presentation/Stage.mjs} +68 -76
- package/dist/presentation/Stage.mjs.map +1 -0
- package/dist/presentation/feed.d.mts +20 -0
- package/dist/presentation/feed.mjs +18 -0
- package/dist/presentation/feed.mjs.map +1 -0
- package/dist/providers/Cdp.d.mts +9 -0
- package/dist/{Cdp.mjs → providers/Cdp.mjs} +3 -8
- package/dist/providers/Cdp.mjs.map +1 -0
- package/dist/{Chromium.d.mts → providers/Chromium.d.mts} +6 -7
- package/dist/{Chromium.mjs → providers/Chromium.mjs} +6 -15
- package/dist/providers/Chromium.mjs.map +1 -0
- package/dist/providers/Released.d.mts +2 -0
- package/dist/providers/Released.mjs +12 -0
- package/dist/providers/Released.mjs.map +1 -0
- package/dist/stillness/Stillness.d.mts +12 -0
- package/dist/stillness/Stillness.mjs +2 -0
- package/dist/supervisor/Supervisor.d.mts +104 -0
- package/dist/supervisor/Supervisor.mjs +515 -0
- package/dist/supervisor/Supervisor.mjs.map +1 -0
- package/dist/testing/index.d.mts +337 -0
- package/dist/testing/index.mjs +515 -0
- package/dist/testing/index.mjs.map +1 -0
- package/dist/workerd/Cdp.d.mts +8 -3
- package/dist/workerd/Cdp.mjs +2 -7
- package/dist/workerd/Cdp.mjs.map +1 -1
- package/package.json +89 -39
- package/src/Action.ts +127 -0
- package/src/ActionPort.ts +128 -0
- package/src/Browser.ts +352 -201
- package/src/BrowserError.ts +63 -6
- package/src/BrowserEvent.ts +26 -25
- package/src/Change.ts +0 -2
- package/src/{internal/pictures/clock.ts → Clock.ts} +17 -6
- package/src/Frame.ts +14 -0
- package/src/Geometry.ts +48 -0
- package/src/{internal/reading/match.inpage.ts → Matching.ts} +34 -4
- package/src/Page.ts +111 -92
- package/src/Snapshot.ts +5 -0
- package/src/{internal/input/style.ts → Style.ts} +121 -14
- package/src/Window.ts +30 -0
- package/src/index.ts +15 -6
- package/src/internal/input/actions.ts +61 -119
- package/src/internal/input/dispatch.ts +3 -3
- package/src/internal/input/gate.ts +21 -18
- package/src/internal/input/guard.inpage.ts +25 -58
- package/src/internal/input/guard.ts +11 -20
- package/src/internal/input/keyboard.ts +12 -3
- package/src/internal/input/perform.ts +78 -60
- package/src/internal/input/pointer.ts +4 -2
- package/src/internal/input/settle.ts +7 -5
- package/src/internal/input/targets.inpage.ts +1 -1
- package/src/internal/input/targets.ts +4 -9
- package/src/internal/page/bridge.ts +209 -113
- package/src/internal/page/context.ts +12 -59
- package/src/internal/page/dialogs.ts +58 -0
- package/src/internal/page/identity.ts +6 -5
- package/src/internal/page/lane.ts +14 -15
- package/src/internal/page/navigation.ts +50 -42
- package/src/internal/page/openings.ts +11 -5
- package/src/internal/page/page.ts +44 -81
- package/src/internal/page/port.ts +125 -0
- package/src/internal/{input → page}/replies.ts +1 -1
- package/src/internal/page/request-owner.ts +269 -0
- package/src/internal/page/requests.ts +357 -0
- package/src/internal/page/scripts.ts +124 -0
- package/src/internal/pictures/capture.ts +255 -332
- package/src/internal/pictures/dimensions.ts +162 -0
- package/src/internal/pictures/frame.ts +68 -0
- package/src/internal/pictures/pictures.ts +57 -23
- package/src/internal/pictures/quiescence.ts +1 -1
- package/src/internal/pictures/state.ts +26 -0
- package/src/internal/pictures/transport.ts +21 -4
- package/src/internal/reading/controls.inpage.ts +5 -70
- package/src/internal/reading/expect.inpage.ts +13 -17
- package/src/internal/reading/names.inpage.ts +89 -45
- package/src/internal/reading/outline.inpage.ts +6 -7
- package/src/internal/reading/reading.ts +144 -176
- package/src/internal/reading/ready.inpage.ts +6 -17
- package/src/internal/reading/ready.ts +13 -135
- package/src/internal/reading/subjects.inpage.ts +83 -43
- package/src/internal/reading/walk.inpage.ts +33 -13
- package/src/internal/timeline/changes.ts +55 -6
- package/src/internal/timeline/events.ts +15 -5
- package/src/internal/timeline/history.inpage.ts +1 -1
- package/src/internal/timeline/record.inpage.ts +4 -1
- package/src/internal/timeline/window.ts +2 -1
- package/src/{Moment.ts → moment/Moment.ts} +13 -31
- package/src/{internal/timeline → moment}/words.ts +2 -2
- package/src/{internal/reading → plan}/choose.ts +3 -3
- package/src/plan/data.ts +117 -0
- package/src/plan/execute.ts +411 -0
- package/src/plan/index.ts +36 -0
- package/src/plan/record.ts +71 -0
- package/src/plan/targets.ts +71 -0
- package/src/{Policy.ts → policy/Policy.ts} +13 -10
- package/src/{Motion.ts → presentation/Motion.ts} +12 -46
- package/src/presentation/Presentation.ts +176 -0
- package/src/{Stage.ts → presentation/Stage.ts} +147 -112
- package/src/presentation/feed.ts +21 -0
- package/src/{Cdp.ts → providers/Cdp.ts} +2 -2
- package/src/{Chromium.ts → providers/Chromium.ts} +7 -10
- package/src/providers/Released.ts +13 -0
- package/src/{internal/cdp.ts → providers/connection.ts} +2 -2
- package/src/{internal/pictures/motion.ts → stillness/Pixels.ts} +1 -1
- package/src/stillness/Stillness.ts +166 -0
- package/src/supervisor/Supervisor.ts +170 -0
- package/src/supervisor/contract.ts +105 -0
- package/src/supervisor/lifecycle.ts +272 -0
- package/src/supervisor/runtime.ts +238 -0
- package/src/testing/TestBrowser.ts +115 -0
- package/src/testing/TestPage.ts +10 -0
- package/src/testing/evidence.ts +154 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/model.ts +222 -0
- package/src/testing/page.ts +360 -0
- package/src/workerd/Cdp.ts +2 -2
- package/dist/Browser-CxXUjxOp.mjs +0 -619
- package/dist/Browser-CxXUjxOp.mjs.map +0 -1
- package/dist/Cdp-CzCRnMlM.d.mts +0 -10
- package/dist/Cdp.d.mts +0 -11
- package/dist/Cdp.mjs.map +0 -1
- package/dist/Change-DNvztNLu.mjs +0 -138
- package/dist/Change-DNvztNLu.mjs.map +0 -1
- package/dist/Chromium.mjs.map +0 -1
- package/dist/Expect-u3P_ENLX.d.mts +0 -62
- package/dist/Expect.d.mts +0 -2
- package/dist/Expect.mjs +0 -114
- package/dist/Expect.mjs.map +0 -1
- package/dist/Moment.d.mts +0 -2
- package/dist/Moment.mjs.map +0 -1
- package/dist/Motion.d.mts +0 -2
- package/dist/Motion.mjs.map +0 -1
- package/dist/Page-BP02bmNv.mjs +0 -264
- package/dist/Page-BP02bmNv.mjs.map +0 -1
- package/dist/Plan-BG1WifW2.mjs.map +0 -1
- package/dist/Plan.d.mts +0 -2
- package/dist/Plan.mjs +0 -2
- package/dist/Policy.d.mts +0 -2
- package/dist/Policy.mjs.map +0 -1
- package/dist/Presentation-DxVikn9_.d.mts +0 -48
- package/dist/Presentation.d.mts +0 -2
- package/dist/Presentation.mjs +0 -118
- package/dist/Presentation.mjs.map +0 -1
- package/dist/Stage.d.mts +0 -2
- package/dist/Stage.mjs.map +0 -1
- package/dist/Supervisor-2mPjIYYs.mjs +0 -676
- package/dist/Supervisor-2mPjIYYs.mjs.map +0 -1
- package/dist/Supervisor-CfRxbaso.d.mts +0 -317
- package/dist/Supervisor.d.mts +0 -2
- package/dist/Supervisor.mjs +0 -2
- package/dist/cdp-BMpYMWYe.mjs.map +0 -1
- package/dist/context-EGhOqjNN.mjs +0 -335
- package/dist/context-EGhOqjNN.mjs.map +0 -1
- package/dist/page-PTM8m5wC.mjs +0 -4057
- package/dist/page-PTM8m5wC.mjs.map +0 -1
- package/dist/url-Bsp2y55M.mjs +0 -264
- package/dist/url-Bsp2y55M.mjs.map +0 -1
- package/dist/workerd/index.d.mts +0 -16
- package/dist/workerd/index.mjs +0 -17
- package/src/Expect.ts +0 -180
- package/src/Plan.ts +0 -674
- package/src/Presentation.ts +0 -196
- package/src/Supervisor.ts +0 -632
- package/src/internal/supervisor/lifecycle.ts +0 -484
- package/src/workerd/index.ts +0 -17
- /package/src/{internal/page/url.ts → Url.ts} +0 -0
- /package/src/{internal/pictures → stillness}/utilsBundle.d.ts +0 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/** Proves a paused request's context before the connection's policy may see it. */
|
|
2
|
+
import { Cache, Clock, Duration, Effect, Exit, Option, Schema } from "effect";
|
|
3
|
+
import type { BrowserContext, CDPSession } from "playwright-core";
|
|
4
|
+
|
|
5
|
+
import { type BrowserError, InvalidRequest, undispatched } from "../../BrowserError.ts";
|
|
6
|
+
import * as Port from "./port.ts";
|
|
7
|
+
|
|
8
|
+
export type Native = <A>(
|
|
9
|
+
run: (signal: AbortSignal) => Promise<A>,
|
|
10
|
+
closing?: boolean,
|
|
11
|
+
) => Effect.Effect<A, BrowserError>;
|
|
12
|
+
|
|
13
|
+
interface Tree {
|
|
14
|
+
readonly frame: { readonly id: string };
|
|
15
|
+
readonly childFrames?: ReadonlyArray<Tree> | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const Tree: Schema.Codec<Tree> = Schema.Struct({
|
|
19
|
+
frame: Schema.Struct({ id: Schema.String }),
|
|
20
|
+
childFrames: Schema.optional(Schema.Array(Schema.suspend(() => Tree))),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const FrameTree = Schema.Struct({ frameTree: Tree });
|
|
24
|
+
|
|
25
|
+
const Reply = Schema.fromJsonString(
|
|
26
|
+
Schema.Struct({
|
|
27
|
+
id: Schema.optional(Schema.Finite),
|
|
28
|
+
result: Schema.optional(Schema.Unknown),
|
|
29
|
+
error: Schema.optional(Schema.Struct({ message: Schema.String })),
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const contains = (tree: Tree, frame: string): boolean =>
|
|
34
|
+
tree.frame.id === frame || tree.childFrames?.some((child) => contains(child, frame)) === true;
|
|
35
|
+
|
|
36
|
+
const queryTarget = async (
|
|
37
|
+
chrome: CDPSession,
|
|
38
|
+
target: string,
|
|
39
|
+
method: string,
|
|
40
|
+
params: object,
|
|
41
|
+
signal: AbortSignal,
|
|
42
|
+
session: Port.Session,
|
|
43
|
+
hooks: Port.Hooks,
|
|
44
|
+
) => {
|
|
45
|
+
const { sessionId } = await chrome.send("Target.attachToTarget", {
|
|
46
|
+
targetId: target,
|
|
47
|
+
flatten: false,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (signal.aborted) {
|
|
51
|
+
await chrome.send("Target.detachFromTarget", { sessionId });
|
|
52
|
+
throw new Error("request ownership query was interrupted");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const response = Promise.withResolvers<unknown>();
|
|
56
|
+
const interruption = Promise.withResolvers<never>();
|
|
57
|
+
|
|
58
|
+
const receive = (event: { sessionId: string; message: string }) => {
|
|
59
|
+
if (event.sessionId !== sessionId) return;
|
|
60
|
+
const reply = Schema.decodeOption(Reply)(event.message);
|
|
61
|
+
|
|
62
|
+
if (Option.isNone(reply)) {
|
|
63
|
+
response.reject(new Error("request ownership query received an invalid response"));
|
|
64
|
+
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (reply.value.id !== 1) return;
|
|
68
|
+
if (reply.value.error !== undefined) response.reject(new Error(reply.value.error.message));
|
|
69
|
+
else response.resolve(reply.value.result);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const detached = (event: { sessionId: string }) => {
|
|
73
|
+
if (event.sessionId === sessionId)
|
|
74
|
+
interruption.reject(new Error("request ownership target detached"));
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const aborted = () => interruption.reject(new Error("request ownership query was interrupted"));
|
|
78
|
+
|
|
79
|
+
chrome.on("Target.receivedMessageFromTarget", receive);
|
|
80
|
+
chrome.on("Target.detachedFromTarget", detached);
|
|
81
|
+
signal.addEventListener("abort", aborted, { once: true });
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const send = () => {
|
|
85
|
+
void chrome
|
|
86
|
+
.send("Target.sendMessageToTarget", {
|
|
87
|
+
sessionId,
|
|
88
|
+
message: JSON.stringify({ id: 1, method, params }),
|
|
89
|
+
})
|
|
90
|
+
.catch(response.reject);
|
|
91
|
+
|
|
92
|
+
return response.promise;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// The logical child command stays faultable without inspecting serialized CDP messages.
|
|
96
|
+
const query = hooks.query;
|
|
97
|
+
|
|
98
|
+
return await Promise.race([
|
|
99
|
+
query === undefined ? send() : query(session, method, send),
|
|
100
|
+
interruption.promise,
|
|
101
|
+
]);
|
|
102
|
+
} finally {
|
|
103
|
+
chrome.off("Target.receivedMessageFromTarget", receive);
|
|
104
|
+
chrome.off("Target.detachedFromTarget", detached);
|
|
105
|
+
signal.removeEventListener("abort", aborted);
|
|
106
|
+
await chrome.send("Target.detachFromTarget", { sessionId });
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const make = Effect.fnUntraced(function* (
|
|
111
|
+
context: BrowserContext,
|
|
112
|
+
chrome: CDPSession,
|
|
113
|
+
native: Native,
|
|
114
|
+
closing: () => boolean,
|
|
115
|
+
) {
|
|
116
|
+
const clock = yield* Clock.Clock;
|
|
117
|
+
const hooks = yield* Port.Current;
|
|
118
|
+
const protocol = yield* Effect.context<never>();
|
|
119
|
+
|
|
120
|
+
const owned = yield* native(async (signal) => {
|
|
121
|
+
const existing = context.pages()[0];
|
|
122
|
+
const page = existing ?? (await context.newPage());
|
|
123
|
+
let cdp: CDPSession | undefined;
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
if (signal.aborted) throw new Error("request ownership setup was interrupted");
|
|
127
|
+
cdp = await Effect.runPromiseWith(protocol)(
|
|
128
|
+
Port.openSession(
|
|
129
|
+
async () => {
|
|
130
|
+
if (signal.aborted) throw new Error("request ownership setup was interrupted");
|
|
131
|
+
const created = await context.newCDPSession(page);
|
|
132
|
+
|
|
133
|
+
if (signal.aborted) {
|
|
134
|
+
await created.detach();
|
|
135
|
+
throw new Error("request ownership setup was interrupted");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
cdp = created;
|
|
139
|
+
|
|
140
|
+
return created;
|
|
141
|
+
},
|
|
142
|
+
page,
|
|
143
|
+
"page",
|
|
144
|
+
),
|
|
145
|
+
{ signal },
|
|
146
|
+
);
|
|
147
|
+
if (signal.aborted) throw new Error("request ownership setup was interrupted");
|
|
148
|
+
const { targetInfo } = await cdp.send("Target.getTargetInfo");
|
|
149
|
+
|
|
150
|
+
if (targetInfo.browserContextId === undefined)
|
|
151
|
+
throw new Error("request ownership requires an identifiable browser context");
|
|
152
|
+
|
|
153
|
+
return targetInfo.browserContextId;
|
|
154
|
+
} finally {
|
|
155
|
+
try {
|
|
156
|
+
if (cdp !== undefined) await cdp.detach();
|
|
157
|
+
} finally {
|
|
158
|
+
if (existing === undefined) await page.close();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const refresh = native(() => chrome.send("Target.getTargets")).pipe(
|
|
164
|
+
Effect.flatMap(({ targetInfos }) => {
|
|
165
|
+
const targets = targetInfos.filter((target) => target.browserContextId === owned);
|
|
166
|
+
|
|
167
|
+
if (targets.some((target) => target.type === "shared_worker"))
|
|
168
|
+
return undispatched(
|
|
169
|
+
"requests",
|
|
170
|
+
new InvalidRequest({
|
|
171
|
+
detail: "request policies cannot protect preexisting shared workers",
|
|
172
|
+
}),
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// Empty target patterns refresh existing documents under the browser interceptor.
|
|
176
|
+
return Effect.forEach(
|
|
177
|
+
targets.filter((target) => ["page", "iframe", "service_worker"].includes(target.type)),
|
|
178
|
+
(target) =>
|
|
179
|
+
native((signal) =>
|
|
180
|
+
queryTarget(
|
|
181
|
+
chrome,
|
|
182
|
+
target.targetId,
|
|
183
|
+
"Fetch.enable",
|
|
184
|
+
{ patterns: [] },
|
|
185
|
+
signal,
|
|
186
|
+
{ channel: "browser", page: target.targetId, clock },
|
|
187
|
+
hooks,
|
|
188
|
+
),
|
|
189
|
+
),
|
|
190
|
+
{ concurrency: 8, discard: true },
|
|
191
|
+
);
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const ownership = yield* Cache.makeWith(
|
|
196
|
+
(frame: string) =>
|
|
197
|
+
native(async (signal) => {
|
|
198
|
+
const direct = await chrome
|
|
199
|
+
.send("Target.getTargetInfo", { targetId: frame })
|
|
200
|
+
.catch((cause: unknown) => {
|
|
201
|
+
if (cause instanceof Error && /No target with given id found/.test(cause.message))
|
|
202
|
+
return undefined;
|
|
203
|
+
|
|
204
|
+
throw cause;
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (signal.aborted) throw new Error("request ownership query was interrupted");
|
|
208
|
+
|
|
209
|
+
if (direct?.targetInfo.browserContextId !== undefined)
|
|
210
|
+
return direct.targetInfo.browserContextId === owned
|
|
211
|
+
? { page: direct.targetInfo.type === "page" ? direct.targetInfo.targetId : undefined }
|
|
212
|
+
: false;
|
|
213
|
+
const { targetInfos } = await chrome.send("Target.getTargets");
|
|
214
|
+
|
|
215
|
+
for (const target of targetInfos) {
|
|
216
|
+
if (signal.aborted) throw new Error("request ownership query was interrupted");
|
|
217
|
+
if (target.type !== "page" && target.type !== "iframe") continue;
|
|
218
|
+
|
|
219
|
+
const result = await queryTarget(
|
|
220
|
+
chrome,
|
|
221
|
+
target.targetId,
|
|
222
|
+
"Page.getFrameTree",
|
|
223
|
+
{},
|
|
224
|
+
signal,
|
|
225
|
+
{
|
|
226
|
+
channel: "browser",
|
|
227
|
+
page: target.targetId,
|
|
228
|
+
clock,
|
|
229
|
+
},
|
|
230
|
+
hooks,
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const { frameTree } = Schema.decodeUnknownSync(FrameTree)(result);
|
|
234
|
+
|
|
235
|
+
if (contains(frameTree, frame)) {
|
|
236
|
+
if (target.browserContextId === undefined)
|
|
237
|
+
throw new Error("request frame has no browser context evidence");
|
|
238
|
+
|
|
239
|
+
return target.browserContextId === owned
|
|
240
|
+
? { page: target.type === "page" ? frameTree.frame.id : undefined }
|
|
241
|
+
: false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
throw new Error("request frame ownership could not be established");
|
|
246
|
+
}, closing()),
|
|
247
|
+
{
|
|
248
|
+
capacity: 1024,
|
|
249
|
+
// A child frame may move into its own OOPIF target; do not cache its parent-page label.
|
|
250
|
+
timeToLive: (exit, frame) =>
|
|
251
|
+
Exit.isSuccess(exit) &&
|
|
252
|
+
(exit.value === false || exit.value.page === undefined || exit.value.page === frame)
|
|
253
|
+
? Duration.infinity
|
|
254
|
+
: Duration.zero,
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const owns = (frame: string | undefined) =>
|
|
259
|
+
frame === undefined
|
|
260
|
+
? Effect.fail(
|
|
261
|
+
undispatched(
|
|
262
|
+
"requests",
|
|
263
|
+
new InvalidRequest({ detail: "request has no frame ownership evidence" }),
|
|
264
|
+
),
|
|
265
|
+
)
|
|
266
|
+
: Cache.get(ownership, frame);
|
|
267
|
+
|
|
268
|
+
return { owns, refresh };
|
|
269
|
+
});
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/** Context-owned request decisions over bounded CDP interception, including every redirect hop. */
|
|
2
|
+
import { Cause, Clock, Duration, Effect, Exit, Option, Queue, Result, Scope } from "effect";
|
|
3
|
+
import { Base64 } from "effect/encoding";
|
|
4
|
+
import type { BrowserContext } from "playwright-core";
|
|
5
|
+
|
|
6
|
+
import type { RequestDecision, RequestPolicy } from "../../Browser.ts";
|
|
7
|
+
import {
|
|
8
|
+
type BrowserError,
|
|
9
|
+
refuse,
|
|
10
|
+
reasonOf,
|
|
11
|
+
Threw,
|
|
12
|
+
Timeout,
|
|
13
|
+
undispatched,
|
|
14
|
+
} from "../../BrowserError.ts";
|
|
15
|
+
import { type BrowserEvent, RequestRefused } from "../../BrowserEvent.ts";
|
|
16
|
+
import * as Url from "../../Url.ts";
|
|
17
|
+
import * as Port from "./port.ts";
|
|
18
|
+
import * as Owner from "./request-owner.ts";
|
|
19
|
+
|
|
20
|
+
interface Paused {
|
|
21
|
+
readonly requestId: string;
|
|
22
|
+
readonly page?: string | undefined;
|
|
23
|
+
readonly frameId?: string | undefined;
|
|
24
|
+
readonly request: {
|
|
25
|
+
readonly hasPostData?: boolean | undefined;
|
|
26
|
+
readonly headers?: Readonly<Record<string, string>> | undefined;
|
|
27
|
+
readonly url: string;
|
|
28
|
+
readonly method: string;
|
|
29
|
+
readonly postData?: string | undefined;
|
|
30
|
+
readonly postDataEntries?: ReadonlyArray<{ readonly bytes?: string | undefined }> | undefined;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const responseOf = (decision: Extract<RequestDecision, { readonly _tag: "Fulfilled" }>) =>
|
|
35
|
+
Number.isInteger(decision.status) &&
|
|
36
|
+
decision.status >= 100 &&
|
|
37
|
+
decision.status <= 599 &&
|
|
38
|
+
new TextEncoder().encode(decision.body).byteLength <= 8_000_000 &&
|
|
39
|
+
!/[\r\n]/.test(decision.contentType) &&
|
|
40
|
+
Object.entries(decision.headers ?? {}).every(
|
|
41
|
+
([name, value]) => name !== "" && !/[\r\n]/.test(name + value),
|
|
42
|
+
)
|
|
43
|
+
? {
|
|
44
|
+
responseCode: decision.status,
|
|
45
|
+
responseHeaders: [
|
|
46
|
+
{ name: "Content-Type", value: decision.contentType },
|
|
47
|
+
...Object.entries(decision.headers ?? {}).map(([name, value]) => ({ name, value })),
|
|
48
|
+
],
|
|
49
|
+
body: Base64.encode(decision.body),
|
|
50
|
+
}
|
|
51
|
+
: undefined;
|
|
52
|
+
|
|
53
|
+
const bounded =
|
|
54
|
+
(clock: Clock.Clock, duration: Duration.Duration): Owner.Native =>
|
|
55
|
+
<A>(run: (signal: AbortSignal) => Promise<A>, closing = false) => {
|
|
56
|
+
const bound = closing ? Duration.min(duration, Duration.seconds(1)) : duration;
|
|
57
|
+
|
|
58
|
+
return Effect.tryPromise({
|
|
59
|
+
try: run,
|
|
60
|
+
catch: (cause) => undispatched("requests", reasonOf(cause, undefined, "connection")),
|
|
61
|
+
}).pipe(
|
|
62
|
+
Effect.timeoutOrElse({
|
|
63
|
+
duration: bound,
|
|
64
|
+
orElse: () =>
|
|
65
|
+
Effect.fail(undispatched("requests", new Timeout({ millis: Duration.toMillis(bound) }))),
|
|
66
|
+
}),
|
|
67
|
+
Effect.provideService(Clock.Clock, clock),
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type Reply = (
|
|
72
|
+
paused: Paused,
|
|
73
|
+
method: "Fetch.continueRequest" | "Fetch.failRequest" | "Fetch.fulfillRequest",
|
|
74
|
+
params?: object,
|
|
75
|
+
authorize?: boolean,
|
|
76
|
+
) => Effect.Effect<void, BrowserError>;
|
|
77
|
+
|
|
78
|
+
const bodyOf = (request: Paused["request"]): string | null | undefined => {
|
|
79
|
+
const hasBody =
|
|
80
|
+
request.hasPostData === true ||
|
|
81
|
+
request.postData !== undefined ||
|
|
82
|
+
(request.postDataEntries?.length ?? 0) > 0 ||
|
|
83
|
+
Object.entries(request.headers ?? {}).some(
|
|
84
|
+
([name, value]) => name.toLowerCase() === "content-length" && Number(value) > 0,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (!hasBody) return null;
|
|
88
|
+
if (request.postData === undefined || request.postDataEntries === undefined) return undefined;
|
|
89
|
+
const chunks: Uint8Array[] = [];
|
|
90
|
+
|
|
91
|
+
for (const entry of request.postDataEntries) {
|
|
92
|
+
if (entry.bytes === undefined) return undefined;
|
|
93
|
+
const decoded = Base64.decode(entry.bytes);
|
|
94
|
+
|
|
95
|
+
if (Result.isFailure(decoded)) return undefined;
|
|
96
|
+
chunks.push(decoded.success);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const bytes = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.length, 0));
|
|
100
|
+
let offset = 0;
|
|
101
|
+
|
|
102
|
+
for (const chunk of chunks) {
|
|
103
|
+
bytes.set(chunk, offset);
|
|
104
|
+
offset += chunk.length;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
109
|
+
|
|
110
|
+
return text === request.postData ? text : undefined;
|
|
111
|
+
} catch {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const deciding =
|
|
117
|
+
(
|
|
118
|
+
policy: RequestPolicy,
|
|
119
|
+
clock: Clock.Clock,
|
|
120
|
+
duration: Duration.Duration,
|
|
121
|
+
closing: () => boolean,
|
|
122
|
+
lost: () => void,
|
|
123
|
+
deny: (
|
|
124
|
+
paused: Paused,
|
|
125
|
+
reason: string,
|
|
126
|
+
failure?: BrowserError,
|
|
127
|
+
) => Effect.Effect<void, BrowserError>,
|
|
128
|
+
reply: Reply,
|
|
129
|
+
) =>
|
|
130
|
+
(paused: Paused) =>
|
|
131
|
+
Effect.suspend(() => {
|
|
132
|
+
const body = bodyOf(paused.request);
|
|
133
|
+
|
|
134
|
+
if (body === undefined)
|
|
135
|
+
return Effect.succeed({
|
|
136
|
+
_tag: "Refused" as const,
|
|
137
|
+
reason: "request body was not fully observable as UTF-8 text",
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return policy.decide({
|
|
141
|
+
url: paused.request.url,
|
|
142
|
+
method: paused.request.method,
|
|
143
|
+
body,
|
|
144
|
+
});
|
|
145
|
+
}).pipe(
|
|
146
|
+
Effect.timeoutOrElse({
|
|
147
|
+
duration: duration,
|
|
148
|
+
orElse: () =>
|
|
149
|
+
Effect.fail(
|
|
150
|
+
undispatched("requests", new Timeout({ millis: Duration.toMillis(duration) })),
|
|
151
|
+
),
|
|
152
|
+
}),
|
|
153
|
+
Effect.catchCause((cause) => {
|
|
154
|
+
if (closing() && Cause.hasInterrupts(cause)) return Effect.failCause(cause);
|
|
155
|
+
|
|
156
|
+
const error = Option.getOrElse(Cause.findErrorOption(cause), () =>
|
|
157
|
+
undispatched("requests", new Threw({})),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
lost();
|
|
161
|
+
|
|
162
|
+
return deny(
|
|
163
|
+
paused,
|
|
164
|
+
error.reason._tag === "Timeout"
|
|
165
|
+
? "request decision timed out"
|
|
166
|
+
: `request decision failed: ${error.reason._tag}`,
|
|
167
|
+
error,
|
|
168
|
+
).pipe(Effect.andThen(Effect.fail(error)));
|
|
169
|
+
}),
|
|
170
|
+
Effect.flatMap((decision) => {
|
|
171
|
+
if (decision._tag === "Allowed") return reply(paused, "Fetch.continueRequest", {}, true);
|
|
172
|
+
if (decision._tag === "Refused") return deny(paused, decision.reason);
|
|
173
|
+
const response = responseOf(decision);
|
|
174
|
+
|
|
175
|
+
return response === undefined
|
|
176
|
+
? deny(paused, "request fulfillment is invalid or exceeds 8MB")
|
|
177
|
+
: reply(paused, "Fetch.fulfillRequest", response, true);
|
|
178
|
+
}),
|
|
179
|
+
Effect.provideService(Clock.Clock, clock),
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const retire = (
|
|
183
|
+
owner: Effect.Success<ReturnType<typeof Owner.make>>,
|
|
184
|
+
held: ReadonlyMap<string, Paused>,
|
|
185
|
+
reply: Reply,
|
|
186
|
+
) =>
|
|
187
|
+
Effect.forEach(
|
|
188
|
+
held.values(),
|
|
189
|
+
(paused) =>
|
|
190
|
+
owner.owns(paused.frameId).pipe(
|
|
191
|
+
Effect.matchEffect({
|
|
192
|
+
onFailure: () => reply(paused, "Fetch.failRequest", { errorReason: "BlockedByClient" }),
|
|
193
|
+
onSuccess: (owned) =>
|
|
194
|
+
reply(
|
|
195
|
+
paused,
|
|
196
|
+
owned ? "Fetch.failRequest" : "Fetch.continueRequest",
|
|
197
|
+
owned ? { errorReason: "BlockedByClient" } : {},
|
|
198
|
+
),
|
|
199
|
+
}),
|
|
200
|
+
),
|
|
201
|
+
{ concurrency: 8, discard: true },
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
export const install = Effect.fnUntraced(function* (
|
|
205
|
+
context: BrowserContext,
|
|
206
|
+
policy: RequestPolicy,
|
|
207
|
+
clock: Clock.Clock,
|
|
208
|
+
publish: (event: BrowserEvent) => number,
|
|
209
|
+
now: () => number,
|
|
210
|
+
lost: () => void,
|
|
211
|
+
unavailable: () => boolean,
|
|
212
|
+
) {
|
|
213
|
+
const browser = context.browser();
|
|
214
|
+
|
|
215
|
+
if (browser === null)
|
|
216
|
+
return yield* refuse("requests", "this provider cannot intercept browser requests");
|
|
217
|
+
const bound = Duration.fromInput(policy.timeout ?? "5 seconds");
|
|
218
|
+
|
|
219
|
+
if (
|
|
220
|
+
bound._tag === "None" ||
|
|
221
|
+
!Duration.isFinite(bound.value) ||
|
|
222
|
+
!Duration.isPositive(bound.value) ||
|
|
223
|
+
policy.patterns.length === 0
|
|
224
|
+
)
|
|
225
|
+
return yield* refuse("requests", "request patterns and a finite positive timeout are required");
|
|
226
|
+
|
|
227
|
+
const native = bounded(clock, bound.value);
|
|
228
|
+
const protocol = yield* Effect.context<never>();
|
|
229
|
+
|
|
230
|
+
const chrome = yield* Effect.acquireRelease(
|
|
231
|
+
native((signal) =>
|
|
232
|
+
Effect.runPromiseWith(protocol)(
|
|
233
|
+
Port.openSession(() => browser.newBrowserCDPSession(), undefined, "browser"),
|
|
234
|
+
{ signal },
|
|
235
|
+
).then(async (session) => {
|
|
236
|
+
if (signal.aborted) await session.detach().catch(() => undefined);
|
|
237
|
+
|
|
238
|
+
return session;
|
|
239
|
+
}),
|
|
240
|
+
),
|
|
241
|
+
(chrome) => native(() => chrome.detach(), true).pipe(Effect.orDie),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
let closing = false;
|
|
245
|
+
const owner = yield* Owner.make(context, chrome, native, () => closing);
|
|
246
|
+
const held = new Map<string, Paused>();
|
|
247
|
+
const pending = yield* Queue.unbounded<Paused>();
|
|
248
|
+
const workers = yield* Scope.make();
|
|
249
|
+
|
|
250
|
+
const record = (paused: Paused, reason: string, failure?: BrowserError) =>
|
|
251
|
+
publish(
|
|
252
|
+
new RequestRefused({
|
|
253
|
+
at: now(),
|
|
254
|
+
page: paused.page,
|
|
255
|
+
method: paused.request.method,
|
|
256
|
+
url: Url.redact(paused.request.url),
|
|
257
|
+
reason: Url.redactWithin(reason).slice(0, 2000),
|
|
258
|
+
...(failure === undefined
|
|
259
|
+
? {}
|
|
260
|
+
: {
|
|
261
|
+
failure: {
|
|
262
|
+
reason: failure.reason._tag,
|
|
263
|
+
...(failure.reason._tag === "Timeout"
|
|
264
|
+
? { timeoutMillis: failure.reason.millis }
|
|
265
|
+
: {}),
|
|
266
|
+
},
|
|
267
|
+
}),
|
|
268
|
+
}),
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const reply: Reply = (paused, method, params = {}, authorize = false) =>
|
|
272
|
+
native(async () => {
|
|
273
|
+
// Check at dispatch: a policy may finish after a sibling failed or retirement began.
|
|
274
|
+
const ended = authorize && (closing || unavailable());
|
|
275
|
+
|
|
276
|
+
if (ended) record(paused, "request authority has ended");
|
|
277
|
+
try {
|
|
278
|
+
await chrome.send(ended ? "Fetch.failRequest" : method, {
|
|
279
|
+
requestId: paused.requestId,
|
|
280
|
+
...(ended ? { errorReason: "BlockedByClient" } : params),
|
|
281
|
+
});
|
|
282
|
+
} catch (cause) {
|
|
283
|
+
// A cancelled request is already settled; it does not revoke the browser's authority.
|
|
284
|
+
if (!(cause instanceof Error && /Invalid InterceptionId\.?$/.test(cause.message)))
|
|
285
|
+
throw cause;
|
|
286
|
+
}
|
|
287
|
+
held.delete(paused.requestId);
|
|
288
|
+
}, closing).pipe(Effect.uninterruptible);
|
|
289
|
+
|
|
290
|
+
const deny = (paused: Paused, reason: string, failure?: BrowserError) =>
|
|
291
|
+
Effect.sync(() => record(paused, reason, failure)).pipe(
|
|
292
|
+
Effect.andThen(reply(paused, "Fetch.failRequest", { errorReason: "BlockedByClient" })),
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const decide = deciding(policy, clock, bound.value, () => closing, lost, deny, reply);
|
|
296
|
+
|
|
297
|
+
const handle = (paused: Paused) =>
|
|
298
|
+
owner.owns(paused.frameId).pipe(
|
|
299
|
+
Effect.matchEffect({
|
|
300
|
+
onFailure: () => deny(paused, "request ownership could not be established"),
|
|
301
|
+
onSuccess: (owned) =>
|
|
302
|
+
!owned
|
|
303
|
+
? reply(paused, "Fetch.continueRequest")
|
|
304
|
+
: closing || unavailable()
|
|
305
|
+
? deny({ ...paused, page: owned.page }, "request authority has ended")
|
|
306
|
+
: decide({ ...paused, page: owned.page }),
|
|
307
|
+
}),
|
|
308
|
+
Effect.catchCauseIf(
|
|
309
|
+
(cause) => !Cause.hasInterrupts(cause),
|
|
310
|
+
(cause) => {
|
|
311
|
+
const error = Option.getOrElse(Cause.findErrorOption(cause), () =>
|
|
312
|
+
undispatched("requests", new Threw({})),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
return (
|
|
316
|
+
unavailable()
|
|
317
|
+
? Effect.void
|
|
318
|
+
: Effect.sync(() => record(paused, "request control failed", error))
|
|
319
|
+
).pipe(Effect.andThen(Effect.sync(lost)));
|
|
320
|
+
},
|
|
321
|
+
),
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
const onRequest = (paused: Paused) => {
|
|
325
|
+
held.set(paused.requestId, paused);
|
|
326
|
+
Queue.offerUnsafe(pending, paused);
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
chrome.on("Fetch.requestPaused", onRequest);
|
|
330
|
+
yield* Effect.addFinalizer(() =>
|
|
331
|
+
Effect.sync(() => {
|
|
332
|
+
closing = true;
|
|
333
|
+
}).pipe(
|
|
334
|
+
// This stops new pauses without releasing the existing ones, unlike detach or disable.
|
|
335
|
+
Effect.andThen(native(() => chrome.send("Fetch.enable", { patterns: [] }), true)),
|
|
336
|
+
Effect.ensuring(Scope.close(workers, Exit.void)),
|
|
337
|
+
Effect.andThen(Effect.suspend(() => retire(owner, held, reply))),
|
|
338
|
+
Effect.ensuring(Effect.sync(() => chrome.off("Fetch.requestPaused", onRequest))),
|
|
339
|
+
Effect.orDie,
|
|
340
|
+
),
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
for (let index = 0; index < 8; index++)
|
|
344
|
+
yield* Queue.take(pending).pipe(
|
|
345
|
+
Effect.flatMap(handle),
|
|
346
|
+
Effect.forever,
|
|
347
|
+
Effect.forkScoped,
|
|
348
|
+
Scope.provide(workers),
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
yield* native(() =>
|
|
352
|
+
chrome.send("Fetch.enable", {
|
|
353
|
+
patterns: policy.patterns.map((urlPattern) => ({ urlPattern })),
|
|
354
|
+
}),
|
|
355
|
+
);
|
|
356
|
+
yield* owner.refresh;
|
|
357
|
+
});
|