@scwrap/browser 0.0.0 → 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jakob Kruse
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # Browser providers
2
+
3
+ `@scwrap/browser` provides the `Browser` Capability whose port, `Browser.open` and `Browser.openIn`, lives in `@scwrap/domain`. Scrapers never import Playwright; a Release or a test picks one of these providers.
4
+
5
+ | Export | Import from | Provides | Source |
6
+ | --- | --- | --- | --- |
7
+ | `browserRun` | `@scwrap/browser/browser-run` | `Browser`: Cloudflare Browser Run through the release Worker's `BROWSER` binding, with `@cloudflare/playwright`. Covers no location. | [browser-run.ts](src/browser-run.ts) |
8
+ | `browserUse`, `browserUseWith({ timeoutMinutes })` | `@scwrap/browser/browser-use` | `Browser` for every country: a Browser Use cloud session over CDP whose `proxyCountryCode` is the page's location. Reads `BROWSER_USE_API_KEY`; stops the session and verifies it stopped when the scope closes. | [browser-use.ts](src/browser-use.ts) |
9
+ | `nodeBrowser({ prepare? })` | `@scwrap/browser/node` | `Browser` over a local Playwright Chromium, for acceptance tests and scripts. `prepare` receives each context, for example to `route` fixture pages. Needs the optional peer `playwright`. | [node.ts](src/node.ts) |
10
+ | `playwrightBrowser({ connect, prepare? })` | `@scwrap/browser` | A provider over any Playwright connection, covering the countries it is typed with. | [playwright.ts](src/playwright.ts) |
11
+
12
+ `browserRun` and `browserUse` load `@cloudflare/playwright` and `cloudflare:workers` only when a page opens, so Node, such as the `scwrap` CLI, can import them.
13
+
14
+ The reference is [The Browser Capability](../../docs/browser/browser.md) and [Providing a Browser](../../docs/browser/providing-a-browser.md).
15
+
16
+ ## Tests
17
+
18
+ ```sh
19
+ pnpm --filter @scwrap/browser test
20
+ ```
21
+
22
+ [node.test.ts](test/node.test.ts) drives a real Chromium through the `Page` API: navigation status, clicks, waits, the time zone option, and a page failure as `BrowserError`.
@@ -0,0 +1,2 @@
1
+ export declare const browserRun: import("effect/Layer").Layer<import("@scwrap/domain").Browser<never>, never, never>;
2
+ //# sourceMappingURL=browser-run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-run.d.ts","sourceRoot":"","sources":["../src/browser-run.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,UAAU,qFAUrB,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { cloudflarePlaywright } from "./cloudflare-playwright.js";
2
+ import { playwrightBrowser } from "./playwright.js";
3
+ export const browserRun = playwrightBrowser({
4
+ // @effect-diagnostics-next-line asyncFunction:off -- Playwright's launch is the Promise boundary. @cloudflare/playwright and the Worker env load only when a Run opens a browser, so Node can import this provider.
5
+ connect: async () => {
6
+ const [{ launch }, { env }] = await Promise.all([
7
+ cloudflarePlaywright(),
8
+ import("cloudflare:workers"),
9
+ ]);
10
+ return await launch(env.BROWSER);
11
+ },
12
+ });
13
+ //# sourceMappingURL=browser-run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-run.js","sourceRoot":"","sources":["../src/browser-run.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,CAAC;IAC1C,oNAAoN;IACpN,OAAO,EAAE,KAAK,IAAI,EAAE;QAClB,MAAM,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YAC9C,oBAAoB,EAAE;YACtB,MAAM,CAAC,oBAAoB,CAAC;SAC7B,CAAC,CAAC;QAEH,OAAO,MAAM,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { Browser } from "@scwrap/domain";
2
+ export declare const browserUseWith: (options: {
3
+ readonly timeoutMinutes: number;
4
+ }) => import("effect/Layer").Layer<Browser<"AD" | "AE" | "AF" | "AG" | "AI" | "AL" | "AM" | "AO" | "AQ" | "AR" | "AS" | "AT" | "AU" | "AW" | "AX" | "AZ" | "BA" | "BB" | "BD" | "BE" | "BF" | "BG" | "BH" | "BI" | "BJ" | "BL" | "BM" | "BN" | "BO" | "BQ" | "BR" | "BS" | "BT" | "BV" | "BW" | "BY" | "BZ" | "CA" | "CC" | "CD" | "CF" | "CG" | "CH" | "CI" | "CK" | "CL" | "CM" | "CN" | "CO" | "CR" | "CU" | "CV" | "CW" | "CX" | "CY" | "CZ" | "DE" | "DJ" | "DK" | "DM" | "DO" | "DZ" | "EC" | "EE" | "EG" | "EH" | "ER" | "ES" | "ET" | "FI" | "FJ" | "FK" | "FM" | "FO" | "FR" | "GA" | "GB" | "GD" | "GE" | "GF" | "GG" | "GH" | "GI" | "GL" | "GM" | "GN" | "GP" | "GQ" | "GR" | "GS" | "GT" | "GU" | "GW" | "GY" | "HK" | "HM" | "HN" | "HR" | "HT" | "HU" | "ID" | "IE" | "IL" | "IM" | "IN" | "IO" | "IQ" | "IR" | "IS" | "IT" | "JE" | "JM" | "JO" | "JP" | "KE" | "KG" | "KH" | "KI" | "KM" | "KN" | "KP" | "KR" | "KW" | "KY" | "KZ" | "LA" | "LB" | "LC" | "LI" | "LK" | "LR" | "LS" | "LT" | "LU" | "LV" | "LY" | "MA" | "MC" | "MD" | "ME" | "MF" | "MG" | "MH" | "MK" | "ML" | "MM" | "MN" | "MO" | "MP" | "MQ" | "MR" | "MS" | "MT" | "MU" | "MV" | "MW" | "MX" | "MY" | "MZ" | "NA" | "NC" | "NE" | "NF" | "NG" | "NI" | "NL" | "NO" | "NP" | "NR" | "NU" | "NZ" | "OM" | "PA" | "PE" | "PF" | "PG" | "PH" | "PK" | "PL" | "PM" | "PN" | "PR" | "PS" | "PT" | "PW" | "PY" | "QA" | "RE" | "RO" | "RS" | "RU" | "RW" | "SA" | "SB" | "SC" | "SD" | "SE" | "SG" | "SH" | "SI" | "SJ" | "SK" | "SL" | "SM" | "SN" | "SO" | "SR" | "SS" | "ST" | "SV" | "SX" | "SY" | "SZ" | "TC" | "TD" | "TF" | "TG" | "TH" | "TJ" | "TK" | "TL" | "TM" | "TN" | "TO" | "TR" | "TT" | "TV" | "TW" | "TZ" | "UA" | "UG" | "UM" | "US" | "UY" | "UZ" | "VA" | "VC" | "VE" | "VG" | "VI" | "VN" | "VU" | "WF" | "WS" | "YE" | "YT" | "ZA" | "ZM" | "ZW">, never, never>;
5
+ export declare const browserUse: import("effect/Layer").Layer<Browser<"AD" | "AE" | "AF" | "AG" | "AI" | "AL" | "AM" | "AO" | "AQ" | "AR" | "AS" | "AT" | "AU" | "AW" | "AX" | "AZ" | "BA" | "BB" | "BD" | "BE" | "BF" | "BG" | "BH" | "BI" | "BJ" | "BL" | "BM" | "BN" | "BO" | "BQ" | "BR" | "BS" | "BT" | "BV" | "BW" | "BY" | "BZ" | "CA" | "CC" | "CD" | "CF" | "CG" | "CH" | "CI" | "CK" | "CL" | "CM" | "CN" | "CO" | "CR" | "CU" | "CV" | "CW" | "CX" | "CY" | "CZ" | "DE" | "DJ" | "DK" | "DM" | "DO" | "DZ" | "EC" | "EE" | "EG" | "EH" | "ER" | "ES" | "ET" | "FI" | "FJ" | "FK" | "FM" | "FO" | "FR" | "GA" | "GB" | "GD" | "GE" | "GF" | "GG" | "GH" | "GI" | "GL" | "GM" | "GN" | "GP" | "GQ" | "GR" | "GS" | "GT" | "GU" | "GW" | "GY" | "HK" | "HM" | "HN" | "HR" | "HT" | "HU" | "ID" | "IE" | "IL" | "IM" | "IN" | "IO" | "IQ" | "IR" | "IS" | "IT" | "JE" | "JM" | "JO" | "JP" | "KE" | "KG" | "KH" | "KI" | "KM" | "KN" | "KP" | "KR" | "KW" | "KY" | "KZ" | "LA" | "LB" | "LC" | "LI" | "LK" | "LR" | "LS" | "LT" | "LU" | "LV" | "LY" | "MA" | "MC" | "MD" | "ME" | "MF" | "MG" | "MH" | "MK" | "ML" | "MM" | "MN" | "MO" | "MP" | "MQ" | "MR" | "MS" | "MT" | "MU" | "MV" | "MW" | "MX" | "MY" | "MZ" | "NA" | "NC" | "NE" | "NF" | "NG" | "NI" | "NL" | "NO" | "NP" | "NR" | "NU" | "NZ" | "OM" | "PA" | "PE" | "PF" | "PG" | "PH" | "PK" | "PL" | "PM" | "PN" | "PR" | "PS" | "PT" | "PW" | "PY" | "QA" | "RE" | "RO" | "RS" | "RU" | "RW" | "SA" | "SB" | "SC" | "SD" | "SE" | "SG" | "SH" | "SI" | "SJ" | "SK" | "SL" | "SM" | "SN" | "SO" | "SR" | "SS" | "ST" | "SV" | "SX" | "SY" | "SZ" | "TC" | "TD" | "TF" | "TG" | "TH" | "TJ" | "TK" | "TL" | "TM" | "TN" | "TO" | "TR" | "TT" | "TV" | "TW" | "TZ" | "UA" | "UG" | "UM" | "US" | "UY" | "UZ" | "VA" | "VC" | "VE" | "VG" | "VI" | "VN" | "VU" | "WF" | "WS" | "YE" | "YT" | "ZA" | "ZM" | "ZW">, never, never>;
6
+ //# sourceMappingURL=browser-use.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-use.d.ts","sourceRoot":"","sources":["../src/browser-use.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAgB,MAAM,gBAAgB,CAAC;AAwGvD,eAAO,MAAM,cAAc,YAAa;IAAE,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;CAAE,swDAiBxE,CAAC;AAEJ,eAAO,MAAM,UAAU,mwDAAyC,CAAC"}
@@ -0,0 +1,47 @@
1
+ import { Browser, BrowserError } from "@scwrap/domain";
2
+ import { Config, Effect, Redacted, Schedule, Schema } from "effect";
3
+ import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse, } from "effect/http";
4
+ import { cloudflarePlaywright } from "./cloudflare-playwright.js";
5
+ import { openPlaywrightPage } from "./playwright.js";
6
+ const Session = Schema.Struct({
7
+ cdpUrl: Schema.NonEmptyString,
8
+ id: Schema.String,
9
+ });
10
+ const proxyCountry = (location) => {
11
+ if (location === undefined) {
12
+ return null;
13
+ }
14
+ return location === "GB" ? "uk" : location.toLowerCase();
15
+ };
16
+ const failed = (reason) => new BrowserError({ operation: "open", reason });
17
+ const session = Effect.fn("BrowserUse.session")(function* session(location, timeoutMinutes) {
18
+ const key = yield* Config.Redacted("BROWSER_USE_API_KEY").pipe(Effect.mapError(() => failed("Browser Use is not configured")));
19
+ const client = (yield* HttpClient.HttpClient).pipe(HttpClient.mapRequest(HttpClientRequest.prependUrl("https://api.browser-use.com/api/v4")), HttpClient.mapRequest(HttpClientRequest.setHeader("X-Browser-Use-API-Key", Redacted.value(key))), HttpClient.filterStatusOk);
20
+ const opened = yield* Effect.acquireRelease(client
21
+ .execute(HttpClientRequest.post("/browsers").pipe(HttpClientRequest.bodyJsonUnsafe({
22
+ proxyCountryCode: proxyCountry(location),
23
+ timeout: timeoutMinutes,
24
+ })))
25
+ .pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Session)), Effect.timeout("30 seconds"), Effect.mapError(() => failed("Browser Use session creation failed"))), ({ id }) => client
26
+ .execute(HttpClientRequest.patch(`/browsers/${id}`).pipe(HttpClientRequest.bodyJsonUnsafe({ action: "stop" })))
27
+ .pipe(Effect.andThen(() => client.get(`/browsers/${id}`)), Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Struct({ status: Schema.Literal("stopped") }))), Effect.timeout("10 seconds"), Effect.retry({ schedule: Schedule.spaced("1 second"), times: 2 }), Effect.tapError(() => Effect.logError(`Browser Use session ${id} could not be stopped; its timeout remains active`)), Effect.ignore));
28
+ yield* Effect.annotateCurrentSpan({
29
+ "browserUse.location": location ?? "any",
30
+ "browserUse.session.id": opened.id,
31
+ });
32
+ return opened;
33
+ });
34
+ const webSocketOf = (url) => ({
35
+ // oxlint-disable-next-line typescript/promise-function-async -- @cloudflare/playwright calls this Fetcher-shaped object.
36
+ fetch: () =>
37
+ // @effect-diagnostics-next-line globalFetch:off -- a CDP WebSocket upgrade, handed to @cloudflare/playwright, is not an HTTP request.
38
+ fetch(url.replace(/^ws/u, "http"), { headers: { Upgrade: "websocket" } }),
39
+ });
40
+ export const browserUseWith = (options) => Browser.layer((location, browserOptions) => session(location, options.timeoutMinutes).pipe(Effect.flatMap(({ cdpUrl, id }) => openPlaywrightPage(
41
+ // @effect-diagnostics-next-line asyncFunction:off -- connect is the Promise boundary; @cloudflare/playwright loads only when a Run opens a browser, so Node can import this provider.
42
+ async () => {
43
+ const { connect } = await cloudflarePlaywright();
44
+ return await connect(webSocketOf(cdpUrl), id);
45
+ }, browserOptions, () => Effect.void)), Effect.provide(FetchHttpClient.layer)));
46
+ export const browserUse = browserUseWith({ timeoutMinutes: 10 });
47
+ //# sourceMappingURL=browser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,GAAG,MAAM,oBAAoB,EAAE,CAAC;IAEjD,OAAO,MAAM,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;AAChD,CAAC,EACD,cAAc,EACd,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAClB,CACF,EACD,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CACtC,CACF,CAAC;AAEJ,MAAM,CAAC,MAAM,UAAU,GAAG,cAAc,CAAC,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type * as Playwright from "playwright-core";
2
+ export interface BrowserBinding {
3
+ readonly fetch: (input: string, init?: {
4
+ readonly headers?: Record<string, string>;
5
+ }) => Promise<Response>;
6
+ }
7
+ interface CloudflarePlaywright {
8
+ readonly connect: (endpoint: BrowserBinding, sessionId: string) => Promise<Playwright.Browser>;
9
+ readonly launch: (endpoint: BrowserBinding) => Promise<Playwright.Browser>;
10
+ }
11
+ export declare const cloudflarePlaywright: () => Promise<CloudflarePlaywright>;
12
+ export {};
13
+ //# sourceMappingURL=cloudflare-playwright.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare-playwright.d.ts","sourceRoot":"","sources":["../src/cloudflare-playwright.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,UAAU,MAAM,iBAAiB,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,CACd,KAAK,EAAE,MAAM,EACb,IAAI,CAAC,EAAE;QAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,KACjD,OAAO,CAAC,QAAQ,CAAC,CAAC;CACxB;AAED,UAAU,oBAAoB;IAC5B,QAAQ,CAAC,OAAO,EAAE,CAChB,QAAQ,EAAE,cAAc,EACxB,SAAS,EAAE,MAAM,KACd,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,cAAc,KAAK,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;CAC5E;AAGD,eAAO,MAAM,oBAAoB,qCAGiC,CAAC"}
@@ -0,0 +1,6 @@
1
+ // @effect-diagnostics-next-line asyncFunction:off -- a dynamic import is a Promise; @cloudflare/playwright loads only when a Run opens a browser, so Node can import its providers.
2
+ export const cloudflarePlaywright = async () =>
3
+ // SAFETY: @cloudflare/playwright's index.d.ts imports './types/types' without an extension, which NodeNext cannot resolve, so its Browser types are lost; its connect and launch return Playwright's Browser.
4
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the package's own declarations do not resolve under NodeNext.
5
+ (await import("@cloudflare/playwright"));
6
+ //# sourceMappingURL=cloudflare-playwright.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare-playwright.js","sourceRoot":"","sources":["../src/cloudflare-playwright.ts"],"names":[],"mappings":"AAiBA,oLAAoL;AACpL,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,IAAI,EAAE;AAC7C,8MAA8M;AAC9M,gIAAgI;AAChI,CAAC,MAAM,MAAM,CAAC,wBAAwB,CAAC,CAAyB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { playwrightBrowser } from "./playwright.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { playwrightBrowser } from "./playwright.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/node.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type * as Playwright from "playwright-core";
2
+ export declare const nodeBrowser: (options?: {
3
+ readonly prepare?: (context: Playwright.BrowserContext) => Promise<void>;
4
+ }) => import("effect/Layer").Layer<import("@scwrap/domain").Browser<never>, never, never>;
5
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,KAAK,UAAU,MAAM,iBAAiB,CAAC;AAcnD,eAAO,MAAM,WAAW,aACb;IACP,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,CAAC,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1E,wFAYC,CAAC"}
package/dist/node.js ADDED
@@ -0,0 +1,23 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off -- The Node provider runs acceptance tests and scripts, never a Worker release.
2
+ import { existsSync } from "node:fs";
3
+ import { BrowserError } from "@scwrap/domain";
4
+ import { Effect } from "effect";
5
+ import { chromium } from "playwright";
6
+ import { playwrightBrowser } from "./playwright.js";
7
+ const executablePath = [
8
+ chromium.executablePath(),
9
+ "/usr/bin/google-chrome",
10
+ ].find(existsSync);
11
+ const launchOptions = executablePath === undefined
12
+ ? { args: ["--no-sandbox"] }
13
+ : { args: ["--no-sandbox"], executablePath };
14
+ export const nodeBrowser = (options = {}) => playwrightBrowser({
15
+ connect: chromium.launch.bind(chromium, launchOptions),
16
+ prepare: (context) => options.prepare === undefined
17
+ ? Effect.void
18
+ : Effect.tryPromise({
19
+ catch: (cause) => new BrowserError({ operation: "open", reason: String(cause) }),
20
+ try: options.prepare.bind(undefined, context),
21
+ }),
22
+ });
23
+ //# sourceMappingURL=node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,4HAA4H;AAC5H,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,cAAc,GAAG;IACrB,QAAQ,CAAC,cAAc,EAAE;IACzB,wBAAwB;CACzB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;AAEnB,MAAM,aAAa,GACjB,cAAc,KAAK,SAAS;IAC1B,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,cAAc,CAAC,EAAE;IAC5B,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,cAAc,CAAC,EAAE,cAAc,EAAE,CAAC;AAEjD,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,OAAO,GAEH,EAAE,EACN,EAAE,CACF,iBAAiB,CAAC;IAChB,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,aAAa,CAAC;IACtD,OAAO,EAAE,CAAC,OAAO,EAAE,EAAE,CACnB,OAAO,CAAC,OAAO,KAAK,SAAS;QAC3B,CAAC,CAAC,MAAM,CAAC,IAAI;QACb,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;YAChB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CACf,IAAI,YAAY,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAChE,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC;SAC9C,CAAC;CACT,CAAC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { Browser, BrowserError } from "@scwrap/domain";
2
+ import type { BrowserOptions, CountryCode, Page } from "@scwrap/domain";
3
+ import { Effect } from "effect";
4
+ import type { Scope } from "effect";
5
+ import type * as Playwright from "playwright-core";
6
+ type Code = typeof CountryCode.Type;
7
+ export declare const openPlaywrightPage: (connect: () => Promise<Playwright.Browser>, options: BrowserOptions, prepare: (context: Playwright.BrowserContext) => Effect.Effect<void, BrowserError, Scope.Scope>) => Effect.Effect<Page, BrowserError, Scope.Scope>;
8
+ export declare const playwrightBrowser: <Country extends Code = never>(options: {
9
+ readonly connect: (location: Country | undefined) => Promise<Playwright.Browser>;
10
+ readonly prepare?: (context: Playwright.BrowserContext) => Effect.Effect<void, BrowserError, Scope.Scope>;
11
+ }) => import("effect/Layer").Layer<Browser<Country>, never, never>;
12
+ export {};
13
+ //# sourceMappingURL=playwright.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"playwright.d.ts","sourceRoot":"","sources":["../src/playwright.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,EAAY,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAEpC,OAAO,KAAK,KAAK,UAAU,MAAM,iBAAiB,CAAC;AAEnD,KAAK,IAAI,GAAG,OAAO,WAAW,CAAC,IAAI,CAAC;AA4DpC,eAAO,MAAM,kBAAkB,kBACd,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,8CAG/B,UAAU,CAAC,cAAc,KAC/B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,mDA2BnD,CAAC;AAEH,eAAO,MAAM,iBAAiB,GAAI,OAAO,SAAS,IAAI,GAAG,KAAK,WAAW;IACvE,QAAQ,CAAC,OAAO,EAAE,CAChB,QAAQ,EAAE,OAAO,GAAG,SAAS,KAC1B,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,CAAC,OAAO,CAAC,EAAE,CACjB,OAAO,EAAE,UAAU,CAAC,cAAc,KAC/B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;CACrD,iEAOE,CAAC"}
@@ -0,0 +1,58 @@
1
+ import { Browser, BrowserError } from "@scwrap/domain";
2
+ import { Duration, Effect } from "effect";
3
+ const failure = (operation) => (cause) => new BrowserError({
4
+ operation,
5
+ reason: cause instanceof Error ? cause.message : String(cause),
6
+ });
7
+ const pageOf = (raw) => ({
8
+ click: (selector) => Effect.tryPromise({
9
+ catch: failure("click"),
10
+ try: raw.locator(selector).click.bind(raw.locator(selector), {}),
11
+ }),
12
+ evaluate: (script, arg) => Effect.tryPromise({
13
+ catch: failure("evaluate"),
14
+ // oxlint-disable-next-line typescript/promise-function-async -- evaluate's Promise is the boundary this Effect wraps.
15
+ try: () =>
16
+ // SAFETY: Playwright serializes the script and its argument into the page and resolves with the script's awaited result.
17
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Playwright's Unboxed argument type cannot be named for a generic Arg.
18
+ raw.evaluate(script, arg),
19
+ }),
20
+ goto: (url) => Effect.tryPromise({
21
+ catch: failure("goto"),
22
+ try: raw.goto.bind(raw, url, {}),
23
+ }).pipe(Effect.map((response) => response?.status() ?? 0)),
24
+ waitFor: (predicate, arg) => Effect.tryPromise({
25
+ catch: failure("waitFor"),
26
+ try: async () => {
27
+ // SAFETY: as for evaluate, Playwright serializes the predicate and its argument into the page.
28
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Playwright's Unboxed argument type cannot be named for a generic Arg.
29
+ await raw.waitForFunction(predicate, arg);
30
+ },
31
+ }),
32
+ });
33
+ const contextOptions = (options) => {
34
+ const context = {};
35
+ if (options.timezoneId !== undefined) {
36
+ context.timezoneId = options.timezoneId;
37
+ }
38
+ if (options.userAgent !== undefined) {
39
+ context.userAgent = options.userAgent;
40
+ }
41
+ return context;
42
+ };
43
+ export const openPlaywrightPage = Effect.fnUntraced(function* openPage(connect, options, prepare) {
44
+ const browser = yield* Effect.acquireRelease(Effect.tryPromise({ catch: failure("open"), try: connect }), (opened) => Effect.tryPromise(opened.close.bind(opened, {})).pipe(Effect.ignore));
45
+ const context = yield* Effect.acquireRelease(Effect.tryPromise({
46
+ catch: failure("open"),
47
+ try: browser.newContext.bind(browser, contextOptions(options)),
48
+ }), (opened) => Effect.tryPromise(opened.close.bind(opened, {})).pipe(Effect.ignore));
49
+ yield* prepare(context);
50
+ const raw = yield* Effect.tryPromise({
51
+ catch: failure("open"),
52
+ try: context.newPage.bind(context),
53
+ });
54
+ raw.setDefaultTimeout(Duration.toMillis(options.timeout ?? "10 seconds"));
55
+ return pageOf(raw);
56
+ });
57
+ export const playwrightBrowser = (options) => Browser.layer((location, browserOptions) => openPlaywrightPage(options.connect.bind(undefined, location), browserOptions, options.prepare ?? (() => Effect.void)));
58
+ //# sourceMappingURL=playwright.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"playwright.js","sourceRoot":"","sources":["../src/playwright.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAc1C,MAAM,OAAO,GAAG,CAAC,SAAoB,EAAE,EAAE,CAAC,CAAC,KAAc,EAAE,EAAE,CAC3D,IAAI,YAAY,CAAC;IACf,SAAS;IACT,MAAM,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;CAC/D,CAAC,CAAC;AAEL,MAAM,MAAM,GAAG,CAAC,GAAoB,EAAQ,EAAE,CAAC,CAAC;IAC9C,KAAK,EAAE,CAAC,QAAQ,EAAE,EAAE,CAClB,MAAM,CAAC,U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AG,KAAK,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;QACnC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC;QACtB,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;KACnC,CAAC,CAAC;IAEH,GAAG,CAAC,iBAAiB,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,IAAI,YAAY,CAAC,CAAC,CAAC;IAE1E,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAA+B,OAO/D,EAAE,EAAE,CACH,OAAO,CAAC,KAAK,CAAU,CAAC,QAAQ,EAAE,cAAc,EAAE,EAAE,CAClD,kBAAkB,CAChB,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,EACzC,cAAc,EACd,OAAO,CAAC,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CACvC,CACF,CAAC"}
package/package.json CHANGED
@@ -1 +1,69 @@
1
- {"name":"@scwrap/browser","version":"0.0.0","description":"Placeholder. Use 0.1.0 or later.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/J4K4-Dev/scwrap.git","directory":"packages/browser"}}
1
+ {
2
+ "name": "@scwrap/browser",
3
+ "version": "0.2.0",
4
+ "description": "Browser providers for Scwrap's Browser Capability: Cloudflare Browser Run, Browser Use and a local Playwright for tests.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/J4K4-Dev/scwrap.git",
9
+ "directory": "packages/browser"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "src",
14
+ "README.md"
15
+ ],
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js",
21
+ "default": "./dist/index.js"
22
+ },
23
+ "./browser-run": {
24
+ "types": "./dist/browser-run.d.ts",
25
+ "import": "./dist/browser-run.js",
26
+ "default": "./dist/browser-run.js"
27
+ },
28
+ "./browser-use": {
29
+ "types": "./dist/browser-use.d.ts",
30
+ "import": "./dist/browser-use.js",
31
+ "default": "./dist/browser-use.js"
32
+ },
33
+ "./node": {
34
+ "types": "./dist/node.d.ts",
35
+ "import": "./dist/node.js",
36
+ "default": "./dist/node.js"
37
+ }
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "dependencies": {
43
+ "@cloudflare/playwright": "1.4.0",
44
+ "playwright-core": "1.63.0",
45
+ "@scwrap/domain": "0.2.0"
46
+ },
47
+ "devDependencies": {
48
+ "@effect/vitest": "4.0.5",
49
+ "@types/node": "24.13.6",
50
+ "effect": "4.0.5",
51
+ "playwright": "1.63.0",
52
+ "typescript": "7.0.2",
53
+ "vitest": "5.0.1"
54
+ },
55
+ "peerDependencies": {
56
+ "effect": "4.0.5",
57
+ "playwright": "1.63.0"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "playwright": {
61
+ "optional": true
62
+ }
63
+ },
64
+ "scripts": {
65
+ "typecheck": "tsc --noEmit -p tsconfig.json",
66
+ "build": "tsc -p tsconfig.build.json",
67
+ "test": "vitest run"
68
+ }
69
+ }
@@ -0,0 +1,14 @@
1
+ import { cloudflarePlaywright } from "./cloudflare-playwright.js";
2
+ import { playwrightBrowser } from "./playwright.js";
3
+
4
+ export const browserRun = playwrightBrowser({
5
+ // @effect-diagnostics-next-line asyncFunction:off -- Playwright's launch is the Promise boundary. @cloudflare/playwright and the Worker env load only when a Run opens a browser, so Node can import this provider.
6
+ connect: async () => {
7
+ const [{ launch }, { env }] = await Promise.all([
8
+ cloudflarePlaywright(),
9
+ import("cloudflare:workers"),
10
+ ]);
11
+
12
+ return await launch(env.BROWSER);
13
+ },
14
+ });
@@ -0,0 +1,124 @@
1
+ import { Browser, BrowserError } from "@scwrap/domain";
2
+ import type { CountryCode } from "@scwrap/domain";
3
+ import { Config, Effect, Redacted, Schedule, Schema } from "effect";
4
+ import {
5
+ FetchHttpClient,
6
+ HttpClient,
7
+ HttpClientRequest,
8
+ HttpClientResponse,
9
+ } from "effect/http";
10
+
11
+ import { cloudflarePlaywright } from "./cloudflare-playwright.js";
12
+ import { openPlaywrightPage } from "./playwright.js";
13
+
14
+ type Code = typeof CountryCode.Type;
15
+
16
+ const Session = Schema.Struct({
17
+ cdpUrl: Schema.NonEmptyString,
18
+ id: Schema.String,
19
+ });
20
+
21
+ const proxyCountry = (location: Code | undefined) => {
22
+ if (location === undefined) {
23
+ return null;
24
+ }
25
+
26
+ return location === "GB" ? "uk" : location.toLowerCase();
27
+ };
28
+
29
+ const failed = (reason: string) =>
30
+ new BrowserError({ operation: "open", reason });
31
+
32
+ const session = Effect.fn("BrowserUse.session")(function* session(
33
+ location: Code | undefined,
34
+ timeoutMinutes: number
35
+ ) {
36
+ const key = yield* Config.Redacted("BROWSER_USE_API_KEY").pipe(
37
+ Effect.mapError(() => failed("Browser Use is not configured"))
38
+ );
39
+
40
+ const client = (yield* HttpClient.HttpClient).pipe(
41
+ HttpClient.mapRequest(
42
+ HttpClientRequest.prependUrl("https://api.browser-use.com/api/v4")
43
+ ),
44
+ HttpClient.mapRequest(
45
+ HttpClientRequest.setHeader("X-Browser-Use-API-Key", Redacted.value(key))
46
+ ),
47
+ HttpClient.filterStatusOk
48
+ );
49
+
50
+ const opened = yield* Effect.acquireRelease(
51
+ client
52
+ .execute(
53
+ HttpClientRequest.post("/browsers").pipe(
54
+ HttpClientRequest.bodyJsonUnsafe({
55
+ proxyCountryCode: proxyCountry(location),
56
+ timeout: timeoutMinutes,
57
+ })
58
+ )
59
+ )
60
+ .pipe(
61
+ Effect.flatMap(HttpClientResponse.schemaBodyJson(Session)),
62
+ Effect.timeout("30 seconds"),
63
+ Effect.mapError(() => failed("Browser Use session creation failed"))
64
+ ),
65
+ ({ id }) =>
66
+ client
67
+ .execute(
68
+ HttpClientRequest.patch(`/browsers/${id}`).pipe(
69
+ HttpClientRequest.bodyJsonUnsafe({ action: "stop" })
70
+ )
71
+ )
72
+ .pipe(
73
+ Effect.andThen(() => client.get(`/browsers/${id}`)),
74
+ Effect.flatMap(
75
+ HttpClientResponse.schemaBodyJson(
76
+ Schema.Struct({ status: Schema.Literal("stopped") })
77
+ )
78
+ ),
79
+ Effect.timeout("10 seconds"),
80
+ Effect.retry({ schedule: Schedule.spaced("1 second"), times: 2 }),
81
+ Effect.tapError(() =>
82
+ Effect.logError(
83
+ `Browser Use session ${id} could not be stopped; its timeout remains active`
84
+ )
85
+ ),
86
+ Effect.ignore
87
+ )
88
+ );
89
+
90
+ yield* Effect.annotateCurrentSpan({
91
+ "browserUse.location": location ?? "any",
92
+ "browserUse.session.id": opened.id,
93
+ });
94
+
95
+ return opened;
96
+ });
97
+
98
+ const webSocketOf = (url: string) => ({
99
+ // oxlint-disable-next-line typescript/promise-function-async -- @cloudflare/playwright calls this Fetcher-shaped object.
100
+ fetch: () =>
101
+ // @effect-diagnostics-next-line globalFetch:off -- a CDP WebSocket upgrade, handed to @cloudflare/playwright, is not an HTTP request.
102
+ fetch(url.replace(/^ws/u, "http"), { headers: { Upgrade: "websocket" } }),
103
+ });
104
+
105
+ export const browserUseWith = (options: { readonly timeoutMinutes: number }) =>
106
+ Browser.layer<Code>((location, browserOptions) =>
107
+ session(location, options.timeoutMinutes).pipe(
108
+ Effect.flatMap(({ cdpUrl, id }) =>
109
+ openPlaywrightPage(
110
+ // @effect-diagnostics-next-line asyncFunction:off -- connect is the Promise boundary; @cloudflare/playwright loads only when a Run opens a browser, so Node can import this provider.
111
+ async () => {
112
+ const { connect } = await cloudflarePlaywright();
113
+
114
+ return await connect(webSocketOf(cdpUrl), id);
115
+ },
116
+ browserOptions,
117
+ () => Effect.void
118
+ )
119
+ ),
120
+ Effect.provide(FetchHttpClient.layer)
121
+ )
122
+ );
123
+
124
+ export const browserUse = browserUseWith({ timeoutMinutes: 10 });
@@ -0,0 +1,22 @@
1
+ import type * as Playwright from "playwright-core";
2
+
3
+ export interface BrowserBinding {
4
+ readonly fetch: (
5
+ input: string,
6
+ init?: { readonly headers?: Record<string, string> }
7
+ ) => Promise<Response>;
8
+ }
9
+
10
+ interface CloudflarePlaywright {
11
+ readonly connect: (
12
+ endpoint: BrowserBinding,
13
+ sessionId: string
14
+ ) => Promise<Playwright.Browser>;
15
+ readonly launch: (endpoint: BrowserBinding) => Promise<Playwright.Browser>;
16
+ }
17
+
18
+ // @effect-diagnostics-next-line asyncFunction:off -- a dynamic import is a Promise; @cloudflare/playwright loads only when a Run opens a browser, so Node can import its providers.
19
+ export const cloudflarePlaywright = async () =>
20
+ // SAFETY: @cloudflare/playwright's index.d.ts imports './types/types' without an extension, which NodeNext cannot resolve, so its Browser types are lost; its connect and launch return Playwright's Browser.
21
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the package's own declarations do not resolve under NodeNext.
22
+ (await import("@cloudflare/playwright")) as CloudflarePlaywright;
@@ -0,0 +1,3 @@
1
+ import type { BrowserBinding } from "./cloudflare-playwright.js";
2
+
3
+ export declare const env: { readonly BROWSER: BrowserBinding };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { playwrightBrowser } from "./playwright.js";
package/src/node.ts ADDED
@@ -0,0 +1,36 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off -- The Node provider runs acceptance tests and scripts, never a Worker release.
2
+ import { existsSync } from "node:fs";
3
+
4
+ import { BrowserError } from "@scwrap/domain";
5
+ import { Effect } from "effect";
6
+ import { chromium } from "playwright";
7
+ import type * as Playwright from "playwright-core";
8
+
9
+ import { playwrightBrowser } from "./playwright.js";
10
+
11
+ const executablePath = [
12
+ chromium.executablePath(),
13
+ "/usr/bin/google-chrome",
14
+ ].find(existsSync);
15
+
16
+ const launchOptions =
17
+ executablePath === undefined
18
+ ? { args: ["--no-sandbox"] }
19
+ : { args: ["--no-sandbox"], executablePath };
20
+
21
+ export const nodeBrowser = (
22
+ options: {
23
+ readonly prepare?: (context: Playwright.BrowserContext) => Promise<void>;
24
+ } = {}
25
+ ) =>
26
+ playwrightBrowser({
27
+ connect: chromium.launch.bind(chromium, launchOptions),
28
+ prepare: (context) =>
29
+ options.prepare === undefined
30
+ ? Effect.void
31
+ : Effect.tryPromise({
32
+ catch: (cause) =>
33
+ new BrowserError({ operation: "open", reason: String(cause) }),
34
+ try: options.prepare.bind(undefined, context),
35
+ }),
36
+ });
@@ -0,0 +1,116 @@
1
+ import { Browser, BrowserError } from "@scwrap/domain";
2
+ import type { BrowserOptions, CountryCode, Page } from "@scwrap/domain";
3
+ import { Duration, Effect } from "effect";
4
+ import type { Scope } from "effect";
5
+ // @effect-diagnostics asyncFunction:off -- This module is the boundary that wraps Playwright's Promise API in Effect.
6
+ import type * as Playwright from "playwright-core";
7
+
8
+ type Code = typeof CountryCode.Type;
9
+
10
+ type Operation = (typeof BrowserError.fields.operation)["Type"];
11
+
12
+ interface ContextOptions {
13
+ timezoneId?: string;
14
+ userAgent?: string;
15
+ }
16
+
17
+ const failure = (operation: Operation) => (cause: unknown) =>
18
+ new BrowserError({
19
+ operation,
20
+ reason: cause instanceof Error ? cause.message : String(cause),
21
+ });
22
+
23
+ const pageOf = (raw: Playwright.Page): Page => ({
24
+ click: (selector) =>
25
+ Effect.tryPromise({
26
+ catch: failure("click"),
27
+ try: raw.locator(selector).click.bind(raw.locator(selector), {}),
28
+ }),
29
+ evaluate: <A, Arg>(script: (arg: Arg) => A, arg?: Arg) =>
30
+ Effect.tryPromise({
31
+ catch: failure("evaluate"),
32
+ // oxlint-disable-next-line typescript/promise-function-async -- evaluate's Promise is the boundary this Effect wraps.
33
+ try: () =>
34
+ // SAFETY: Playwright serializes the script and its argument into the page and resolves with the script's awaited result.
35
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Playwright's Unboxed argument type cannot be named for a generic Arg.
36
+ raw.evaluate<Awaited<A>, never>(script as never, arg as never),
37
+ }),
38
+ goto: (url) =>
39
+ Effect.tryPromise({
40
+ catch: failure("goto"),
41
+ try: raw.goto.bind(raw, url, {}),
42
+ }).pipe(Effect.map((response) => response?.status() ?? 0)),
43
+ waitFor: (predicate, arg) =>
44
+ Effect.tryPromise({
45
+ catch: failure("waitFor"),
46
+ try: async () => {
47
+ // SAFETY: as for evaluate, Playwright serializes the predicate and its argument into the page.
48
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Playwright's Unboxed argument type cannot be named for a generic Arg.
49
+ await raw.waitForFunction(predicate as never, arg as never);
50
+ },
51
+ }),
52
+ });
53
+
54
+ const contextOptions = (options: BrowserOptions) => {
55
+ const context: ContextOptions = {};
56
+
57
+ if (options.timezoneId !== undefined) {
58
+ context.timezoneId = options.timezoneId;
59
+ }
60
+
61
+ if (options.userAgent !== undefined) {
62
+ context.userAgent = options.userAgent;
63
+ }
64
+
65
+ return context;
66
+ };
67
+
68
+ export const openPlaywrightPage = Effect.fnUntraced(function* openPage(
69
+ connect: () => Promise<Playwright.Browser>,
70
+ options: BrowserOptions,
71
+ prepare: (
72
+ context: Playwright.BrowserContext
73
+ ) => Effect.Effect<void, BrowserError, Scope.Scope>
74
+ ) {
75
+ const browser = yield* Effect.acquireRelease(
76
+ Effect.tryPromise({ catch: failure("open"), try: connect }),
77
+ (opened) =>
78
+ Effect.tryPromise(opened.close.bind(opened, {})).pipe(Effect.ignore)
79
+ );
80
+
81
+ const context = yield* Effect.acquireRelease(
82
+ Effect.tryPromise({
83
+ catch: failure("open"),
84
+ try: browser.newContext.bind(browser, contextOptions(options)),
85
+ }),
86
+ (opened) =>
87
+ Effect.tryPromise(opened.close.bind(opened, {})).pipe(Effect.ignore)
88
+ );
89
+
90
+ yield* prepare(context);
91
+
92
+ const raw = yield* Effect.tryPromise({
93
+ catch: failure("open"),
94
+ try: context.newPage.bind(context),
95
+ });
96
+
97
+ raw.setDefaultTimeout(Duration.toMillis(options.timeout ?? "10 seconds"));
98
+
99
+ return pageOf(raw);
100
+ });
101
+
102
+ export const playwrightBrowser = <Country extends Code = never>(options: {
103
+ readonly connect: (
104
+ location: Country | undefined
105
+ ) => Promise<Playwright.Browser>;
106
+ readonly prepare?: (
107
+ context: Playwright.BrowserContext
108
+ ) => Effect.Effect<void, BrowserError, Scope.Scope>;
109
+ }) =>
110
+ Browser.layer<Country>((location, browserOptions) =>
111
+ openPlaywrightPage(
112
+ options.connect.bind(undefined, location),
113
+ browserOptions,
114
+ options.prepare ?? (() => Effect.void)
115
+ )
116
+ );