@effect-browser/chromium 0.0.0-stage → 0.3.0-beta.25
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 +21 -0
- package/README.md +34 -2
- package/dist/Chromium.d.mts +60 -0
- package/dist/Chromium.mjs +82 -0
- package/dist/Chromium.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/internal/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/package.json +58 -4
- package/src/Chromium.ts +128 -0
- package/src/index.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Research delivery contributors
|
|
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
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @effect-browser/chromium
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A local Chromium for [effect-browser](../browser), launched through `playwright-core` for a scope
|
|
4
|
+
and closed with it, as a `Browser` layer.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @effect-browser/chromium@beta effect-browser@beta effect playwright-core
|
|
8
|
+
npx playwright-core install chromium
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import * as Chromium from "@effect-browser/chromium/Chromium";
|
|
13
|
+
|
|
14
|
+
const browser = Chromium.layer({ viewport: { width: 1280, height: 720 } });
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`Chromium.make` and `Chromium.layer` take `effect-browser`'s `Browser.Options` and the launch's own:
|
|
18
|
+
`headless` (default true), `sandbox`, `viewport`, `deviceScaleFactor`, `executablePath`, `args`,
|
|
19
|
+
`userAgent` and `locale`. It uses the Chromium build that `playwright-core` installs unless
|
|
20
|
+
`executablePath` names another. `Chromium.layerConfig` takes the same options as `Config` values.
|
|
21
|
+
|
|
22
|
+
`Chromium.layer` launches that Chromium without its sandbox, as Playwright does, so an exploit in a
|
|
23
|
+
page's renderer runs with your user's privileges. `Chromium.layer({ sandbox: true })` runs it
|
|
24
|
+
sandboxed. Hosts that don't allow unprivileged user namespaces, such as many containers and Ubuntu
|
|
25
|
+
23.10 or later by default, can't start the sandbox; opening then fails and says so.
|
|
26
|
+
|
|
27
|
+
`Chromium.layer` leaves signals to your program: Playwright's own handlers, which close every
|
|
28
|
+
browser and, on SIGINT, exit before any finalizer runs, are off. Run the program with
|
|
29
|
+
`NodeRuntime.runMain` from `@effect/platform-node`, which interrupts it on SIGINT and SIGTERM, so
|
|
30
|
+
the browser closes with its scope. A signal that ends the process outright still takes Chromium
|
|
31
|
+
with it, since its pipe closes, but leaves Playwright's temporary profile behind.
|
|
32
|
+
|
|
33
|
+
It reaches effect-browser only through its published entries, and peers on `effect`,
|
|
34
|
+
`effect-browser` and `playwright-core`. A remote browser, or one on Workers, is
|
|
35
|
+
`effect-browser/Cdp`'s.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Config, Effect, Layer } from "effect";
|
|
2
|
+
import * as Browser from "effect-browser/Browser";
|
|
3
|
+
import { BrowserError } from "effect-browser/BrowserError";
|
|
4
|
+
declare namespace Chromium_d_exports {
|
|
5
|
+
export { Options, layer, layerConfig, make };
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @stability stable
|
|
9
|
+
* @category options
|
|
10
|
+
* @since 0.3.0
|
|
11
|
+
*/
|
|
12
|
+
export interface Options extends Browser.Options {
|
|
13
|
+
/** Defaults to true. */
|
|
14
|
+
readonly headless?: boolean | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Run Chromium's sandbox. Defaults to false, as in Playwright, so an exploit in a page's renderer
|
|
17
|
+
* runs with this process's privileges. Hosts that don't allow unprivileged user namespaces, such
|
|
18
|
+
* as many containers and Ubuntu 23.10 or later by default, can't start the sandbox; opening then
|
|
19
|
+
* fails and says so.
|
|
20
|
+
*/
|
|
21
|
+
readonly sandbox?: boolean | undefined;
|
|
22
|
+
/** Defaults to 1280x720 CSS pixels. */
|
|
23
|
+
readonly viewport?: {
|
|
24
|
+
readonly width: number;
|
|
25
|
+
readonly height: number;
|
|
26
|
+
} | undefined;
|
|
27
|
+
/** Device pixels per CSS pixel, as a high-density screen has two. Defaults to 1. */
|
|
28
|
+
readonly deviceScaleFactor?: number | undefined;
|
|
29
|
+
readonly executablePath?: string | undefined;
|
|
30
|
+
readonly args?: ReadonlyArray<string> | undefined;
|
|
31
|
+
readonly userAgent?: string | undefined;
|
|
32
|
+
readonly locale?: string | undefined;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Launch Chromium and open a `Browser` over a fresh context.
|
|
36
|
+
*
|
|
37
|
+
* @stability stable
|
|
38
|
+
* @category constructors
|
|
39
|
+
* @since 0.3.0
|
|
40
|
+
*/
|
|
41
|
+
export declare const make: (options?: Options | undefined) => Effect.Effect<Browser.Service, BrowserError, import("effect/Scope").Scope>;
|
|
42
|
+
/**
|
|
43
|
+
* A `Browser` Layer that launches Chromium when built and closes it when released.
|
|
44
|
+
*
|
|
45
|
+
* @stability stable
|
|
46
|
+
* @category layers
|
|
47
|
+
* @since 0.3.0
|
|
48
|
+
*/
|
|
49
|
+
export declare const layer: (options?: Options) => Layer.Layer<Browser.Browser, BrowserError>;
|
|
50
|
+
/**
|
|
51
|
+
* `layer`, with the options as `Config` values.
|
|
52
|
+
*
|
|
53
|
+
* @stability stable
|
|
54
|
+
* @category layers
|
|
55
|
+
* @since 0.3.0
|
|
56
|
+
*/
|
|
57
|
+
export declare const layerConfig: (config: Config.Wrap<Options>) => Layer.Layer<Browser.Browser, BrowserError | Config.ConfigError>;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { Chromium_d_exports as t };
|
|
60
|
+
//# sourceMappingURL=Chromium.d.mts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { t as __exportAll } from "./internal/rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { Config, Effect, Layer } from "effect";
|
|
3
|
+
import * as Browser from "effect-browser/Browser";
|
|
4
|
+
import { Failed, reasonOf, undispatched } from "effect-browser/BrowserError";
|
|
5
|
+
import { chromium } from "playwright-core";
|
|
6
|
+
//#region src/Chromium.ts
|
|
7
|
+
/**
|
|
8
|
+
* A local Chromium, launched for the scope and closed with it.
|
|
9
|
+
*
|
|
10
|
+
* It uses the Chromium build that `playwright-core` installs
|
|
11
|
+
* (`npx playwright-core install chromium`), unless `executablePath` names another.
|
|
12
|
+
*
|
|
13
|
+
* Signals are the program's to handle: run it with `NodeRuntime.runMain`, which interrupts it on
|
|
14
|
+
* SIGINT and SIGTERM, so the browser closes with its scope.
|
|
15
|
+
*
|
|
16
|
+
* @stability stable
|
|
17
|
+
* @since 0.3.0
|
|
18
|
+
*/
|
|
19
|
+
var Chromium_exports = /* @__PURE__ */ __exportAll({
|
|
20
|
+
layer: () => layer,
|
|
21
|
+
layerConfig: () => layerConfig,
|
|
22
|
+
make: () => make
|
|
23
|
+
});
|
|
24
|
+
const launchTimeoutMillis = 18e4;
|
|
25
|
+
const failed = (operation, timeoutMillis) => (cause) => undispatched(operation, reasonOf(cause, timeoutMillis));
|
|
26
|
+
const sandboxFailed = /Chromium sandboxing failed|No usable sandbox/;
|
|
27
|
+
const launchFailed = (cause) => sandboxFailed.test(String(cause)) ? undispatched("launch", new Failed({ detail: "Chromium's sandbox could not start on this host: it needs unprivileged user namespaces, and a user other than root" })) : failed("launch", launchTimeoutMillis)(cause);
|
|
28
|
+
/**
|
|
29
|
+
* Launch Chromium and open a `Browser` over a fresh context.
|
|
30
|
+
*
|
|
31
|
+
* @stability stable
|
|
32
|
+
* @category constructors
|
|
33
|
+
* @since 0.3.0
|
|
34
|
+
*/
|
|
35
|
+
const make = Effect.fn("Chromium.make")(function* (options = {}) {
|
|
36
|
+
const launched = yield* Effect.acquireRelease(Effect.tryPromise({
|
|
37
|
+
try: () => chromium.launch({
|
|
38
|
+
headless: options.headless ?? true,
|
|
39
|
+
chromiumSandbox: options.sandbox ?? false,
|
|
40
|
+
timeout: launchTimeoutMillis,
|
|
41
|
+
handleSIGINT: false,
|
|
42
|
+
handleSIGTERM: false,
|
|
43
|
+
handleSIGHUP: false,
|
|
44
|
+
...options.executablePath === void 0 ? {} : { executablePath: options.executablePath },
|
|
45
|
+
...options.args === void 0 ? {} : { args: [...options.args] }
|
|
46
|
+
}),
|
|
47
|
+
catch: launchFailed
|
|
48
|
+
}).pipe(Effect.withSpan("Chromium.launch", {}, { captureStackTrace: false })), (browser) => Effect.tryPromise(() => browser.close()).pipe(Effect.ignore));
|
|
49
|
+
const context = yield* Effect.tryPromise({
|
|
50
|
+
try: () => launched.newContext({
|
|
51
|
+
viewport: options.viewport ?? {
|
|
52
|
+
width: 1280,
|
|
53
|
+
height: 720
|
|
54
|
+
},
|
|
55
|
+
deviceScaleFactor: options.deviceScaleFactor ?? 1,
|
|
56
|
+
...options.userAgent === void 0 ? {} : { userAgent: options.userAgent },
|
|
57
|
+
...options.locale === void 0 ? {} : { locale: options.locale }
|
|
58
|
+
}),
|
|
59
|
+
catch: failed("launch")
|
|
60
|
+
});
|
|
61
|
+
return yield* Browser.make(context, { provider: "chromium" }, options);
|
|
62
|
+
});
|
|
63
|
+
/**
|
|
64
|
+
* A `Browser` Layer that launches Chromium when built and closes it when released.
|
|
65
|
+
*
|
|
66
|
+
* @stability stable
|
|
67
|
+
* @category layers
|
|
68
|
+
* @since 0.3.0
|
|
69
|
+
*/
|
|
70
|
+
const layer = (options = {}) => Layer.effect(Browser.Browser, make(options));
|
|
71
|
+
/**
|
|
72
|
+
* `layer`, with the options as `Config` values.
|
|
73
|
+
*
|
|
74
|
+
* @stability stable
|
|
75
|
+
* @category layers
|
|
76
|
+
* @since 0.3.0
|
|
77
|
+
*/
|
|
78
|
+
const layerConfig = (config) => Layer.effect(Browser.Browser, Config.unwrap(config).pipe(Effect.flatMap(make)));
|
|
79
|
+
//#endregion
|
|
80
|
+
export { layer, layerConfig, make, Chromium_exports as t };
|
|
81
|
+
|
|
82
|
+
//# sourceMappingURL=Chromium.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chromium.mjs","names":[],"sources":["../src/Chromium.ts"],"sourcesContent":["/**\n * A local Chromium, launched for the scope and closed with it.\n *\n * It uses the Chromium build that `playwright-core` installs\n * (`npx playwright-core install chromium`), unless `executablePath` names another.\n *\n * Signals are the program's to handle: run it with `NodeRuntime.runMain`, which interrupts it on\n * SIGINT and SIGTERM, so the browser closes with its scope.\n *\n * @stability stable\n * @since 0.3.0\n */\nimport { Config, Effect, Layer } from \"effect\";\nimport * as Browser from \"effect-browser/Browser\";\nimport { type BrowserError, Failed, reasonOf, undispatched } from \"effect-browser/BrowserError\";\nimport { chromium } from \"playwright-core\";\n\n/**\n * @stability stable\n * @category options\n * @since 0.3.0\n */\nexport interface Options extends Browser.Options {\n /** Defaults to true. */\n readonly headless?: boolean | undefined;\n /**\n * Run Chromium's sandbox. Defaults to false, as in Playwright, so an exploit in a page's renderer\n * runs with this process's privileges. Hosts that don't allow unprivileged user namespaces, such\n * as many containers and Ubuntu 23.10 or later by default, can't start the sandbox; opening then\n * fails and says so.\n */\n readonly sandbox?: boolean | undefined;\n /** Defaults to 1280x720 CSS pixels. */\n readonly viewport?: { readonly width: number; readonly height: number } | undefined;\n /** Device pixels per CSS pixel, as a high-density screen has two. Defaults to 1. */\n readonly deviceScaleFactor?: number | undefined;\n readonly executablePath?: string | undefined;\n readonly args?: ReadonlyArray<string> | undefined;\n readonly userAgent?: string | undefined;\n readonly locale?: string | undefined;\n}\n\n// Playwright's own default, stated so that a launch timeout reports the bound it exceeded.\nconst launchTimeoutMillis = 180_000;\n\nconst failed = (operation: string, timeoutMillis?: number) => (cause: unknown) =>\n undispatched(operation, reasonOf(cause, timeoutMillis));\n\n// Playwright puts this in place of Chromium's startup log when the sandbox could not start, in an\n// error that otherwise reads as a closed browser.\nconst sandboxFailed = /Chromium sandboxing failed|No usable sandbox/;\n\nconst launchFailed = (cause: unknown) =>\n sandboxFailed.test(String(cause))\n ? undispatched(\n \"launch\",\n new Failed({\n detail:\n \"Chromium's sandbox could not start on this host: it needs unprivileged user namespaces, and a user other than root\",\n }),\n )\n : failed(\"launch\", launchTimeoutMillis)(cause);\n\n/**\n * Launch Chromium and open a `Browser` over a fresh context.\n *\n * @stability stable\n * @category constructors\n * @since 0.3.0\n */\nexport const make = Effect.fn(\"Chromium.make\")(function* (options: Options = {}) {\n const launched = yield* Effect.acquireRelease(\n Effect.tryPromise({\n try: () =>\n chromium.launch({\n headless: options.headless ?? true,\n chromiumSandbox: options.sandbox ?? false,\n timeout: launchTimeoutMillis,\n // The program owns its signals. Playwright's handlers close every browser on any of\n // these, and on SIGINT then exit the process, so no finalizer would run.\n handleSIGINT: false,\n handleSIGTERM: false,\n handleSIGHUP: false,\n ...(options.executablePath === undefined\n ? {}\n : { executablePath: options.executablePath }),\n ...(options.args === undefined ? {} : { args: [...options.args] }),\n }),\n catch: launchFailed,\n }).pipe(Effect.withSpan(\"Chromium.launch\", {}, { captureStackTrace: false })),\n (browser) => Effect.tryPromise(() => browser.close()).pipe(Effect.ignore),\n );\n\n const context = yield* Effect.tryPromise({\n try: () =>\n launched.newContext({\n viewport: options.viewport ?? { width: 1280, height: 720 },\n deviceScaleFactor: options.deviceScaleFactor ?? 1,\n ...(options.userAgent === undefined ? {} : { userAgent: options.userAgent }),\n ...(options.locale === undefined ? {} : { locale: options.locale }),\n }),\n catch: failed(\"launch\"),\n });\n\n return yield* Browser.make(context, { provider: \"chromium\" }, options);\n});\n\n/**\n * A `Browser` Layer that launches Chromium when built and closes it when released.\n *\n * @stability stable\n * @category layers\n * @since 0.3.0\n */\nexport const layer = (options: Options = {}): Layer.Layer<Browser.Browser, BrowserError> =>\n Layer.effect(Browser.Browser, make(options));\n\n/**\n * `layer`, with the options as `Config` values.\n *\n * @stability stable\n * @category layers\n * @since 0.3.0\n */\nexport const layerConfig = (\n config: Config.Wrap<Options>,\n): Layer.Layer<Browser.Browser, BrowserError | Config.ConfigError> =>\n Layer.effect(Browser.Browser, Config.unwrap(config).pipe(Effect.flatMap(make)));\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAM,sBAAsB;AAE5B,MAAM,UAAU,WAAmB,mBAA4B,UAC7D,aAAa,WAAW,SAAS,OAAO,aAAa,CAAC;AAIxD,MAAM,gBAAgB;AAEtB,MAAM,gBAAgB,UACpB,cAAc,KAAK,OAAO,KAAK,CAAC,IAC5B,aACE,UACA,IAAI,OAAO,EACT,QACE,qHACJ,CAAC,CACH,IACA,OAAO,UAAU,mBAAmB,CAAC,CAAC,KAAK;;;;;;;;AASjD,MAAa,OAAO,OAAO,GAAG,eAAe,CAAC,CAAC,WAAW,UAAmB,CAAC,GAAG;CAC/E,MAAM,WAAW,OAAO,OAAO,eAC7B,OAAO,WAAW;EAChB,WACE,SAAS,OAAO;GACd,UAAU,QAAQ,YAAY;GAC9B,iBAAiB,QAAQ,WAAW;GACpC,SAAS;GAGT,cAAc;GACd,eAAe;GACf,cAAc;GACd,GAAI,QAAQ,mBAAmB,KAAA,IAC3B,CAAC,IACD,EAAE,gBAAgB,QAAQ,eAAe;GAC7C,GAAI,QAAQ,SAAS,KAAA,IAAY,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,QAAQ,IAAI,EAAE;EAClE,CAAC;EACH,OAAO;CACT,CAAC,CAAC,CAAC,KAAK,OAAO,SAAS,mBAAmB,CAAC,GAAG,EAAE,mBAAmB,MAAM,CAAC,CAAC,IAC3E,YAAY,OAAO,iBAAiB,QAAQ,MAAM,CAAC,CAAC,CAAC,KAAK,OAAO,MAAM,CAC1E;CAEA,MAAM,UAAU,OAAO,OAAO,WAAW;EACvC,WACE,SAAS,WAAW;GAClB,UAAU,QAAQ,YAAY;IAAE,OAAO;IAAM,QAAQ;GAAI;GACzD,mBAAmB,QAAQ,qBAAqB;GAChD,GAAI,QAAQ,cAAc,KAAA,IAAY,CAAC,IAAI,EAAE,WAAW,QAAQ,UAAU;GAC1E,GAAI,QAAQ,WAAW,KAAA,IAAY,CAAC,IAAI,EAAE,QAAQ,QAAQ,OAAO;EACnE,CAAC;EACH,OAAO,OAAO,QAAQ;CACxB,CAAC;CAED,OAAO,OAAO,QAAQ,KAAK,SAAS,EAAE,UAAU,WAAW,GAAG,OAAO;AACvE,CAAC;;;;;;;;AASD,MAAa,SAAS,UAAmB,CAAC,MACxC,MAAM,OAAO,QAAQ,SAAS,KAAK,OAAO,CAAC;;;;;;;;AAS7C,MAAa,eACX,WAEA,MAAM,OAAO,QAAQ,SAAS,OAAO,OAAO,MAAM,CAAC,CAAC,KAAK,OAAO,QAAQ,IAAI,CAAC,CAAC"}
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect-browser/chromium",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0-beta.25",
|
|
4
|
+
"description": "A local Chromium for effect-browser, launched through playwright-core.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/mannyc2/effect-agent-browserbase.git",
|
|
9
|
+
"directory": "packages/chromium"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": [],
|
|
17
|
+
"exports": {
|
|
18
|
+
"./package.json": "./package.json",
|
|
19
|
+
".": {
|
|
20
|
+
"@effect-browser/source": "./src/index.ts",
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"default": "./dist/index.mjs"
|
|
23
|
+
},
|
|
24
|
+
"./*": {
|
|
25
|
+
"@effect-browser/source": "./src/*.ts",
|
|
26
|
+
"types": "./dist/*.d.mts",
|
|
27
|
+
"default": "./dist/*.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./internal/*": null,
|
|
30
|
+
"./index": null,
|
|
31
|
+
"./*/index": null
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "vp pack",
|
|
38
|
+
"check": "tsc --noEmit -p tsconfig.json",
|
|
39
|
+
"test": "vp test --run"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@effect/vitest": "4.1.2",
|
|
43
|
+
"@types/node": "26.1.2",
|
|
44
|
+
"effect": "4.1.2",
|
|
45
|
+
"effect-browser": "0.3.0-beta.25",
|
|
46
|
+
"playwright-core": "1.63.0",
|
|
47
|
+
"typescript": "7.0.2",
|
|
48
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.1.0",
|
|
49
|
+
"vite-plus": "1.1.0",
|
|
50
|
+
"vitest": "5.0.3"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"effect": "~4.1.2",
|
|
54
|
+
"effect-browser": "^0.3.0-beta.25",
|
|
55
|
+
"playwright-core": "^1.63.0"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": "^22.18.0 || >=24.11.0"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/Chromium.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A local Chromium, launched for the scope and closed with it.
|
|
3
|
+
*
|
|
4
|
+
* It uses the Chromium build that `playwright-core` installs
|
|
5
|
+
* (`npx playwright-core install chromium`), unless `executablePath` names another.
|
|
6
|
+
*
|
|
7
|
+
* Signals are the program's to handle: run it with `NodeRuntime.runMain`, which interrupts it on
|
|
8
|
+
* SIGINT and SIGTERM, so the browser closes with its scope.
|
|
9
|
+
*
|
|
10
|
+
* @stability stable
|
|
11
|
+
* @since 0.3.0
|
|
12
|
+
*/
|
|
13
|
+
import { Config, Effect, Layer } from "effect";
|
|
14
|
+
import * as Browser from "effect-browser/Browser";
|
|
15
|
+
import { type BrowserError, Failed, reasonOf, undispatched } from "effect-browser/BrowserError";
|
|
16
|
+
import { chromium } from "playwright-core";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @stability stable
|
|
20
|
+
* @category options
|
|
21
|
+
* @since 0.3.0
|
|
22
|
+
*/
|
|
23
|
+
export interface Options extends Browser.Options {
|
|
24
|
+
/** Defaults to true. */
|
|
25
|
+
readonly headless?: boolean | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Run Chromium's sandbox. Defaults to false, as in Playwright, so an exploit in a page's renderer
|
|
28
|
+
* runs with this process's privileges. Hosts that don't allow unprivileged user namespaces, such
|
|
29
|
+
* as many containers and Ubuntu 23.10 or later by default, can't start the sandbox; opening then
|
|
30
|
+
* fails and says so.
|
|
31
|
+
*/
|
|
32
|
+
readonly sandbox?: boolean | undefined;
|
|
33
|
+
/** Defaults to 1280x720 CSS pixels. */
|
|
34
|
+
readonly viewport?: { readonly width: number; readonly height: number } | undefined;
|
|
35
|
+
/** Device pixels per CSS pixel, as a high-density screen has two. Defaults to 1. */
|
|
36
|
+
readonly deviceScaleFactor?: number | undefined;
|
|
37
|
+
readonly executablePath?: string | undefined;
|
|
38
|
+
readonly args?: ReadonlyArray<string> | undefined;
|
|
39
|
+
readonly userAgent?: string | undefined;
|
|
40
|
+
readonly locale?: string | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Playwright's own default, stated so that a launch timeout reports the bound it exceeded.
|
|
44
|
+
const launchTimeoutMillis = 180_000;
|
|
45
|
+
|
|
46
|
+
const failed = (operation: string, timeoutMillis?: number) => (cause: unknown) =>
|
|
47
|
+
undispatched(operation, reasonOf(cause, timeoutMillis));
|
|
48
|
+
|
|
49
|
+
// Playwright puts this in place of Chromium's startup log when the sandbox could not start, in an
|
|
50
|
+
// error that otherwise reads as a closed browser.
|
|
51
|
+
const sandboxFailed = /Chromium sandboxing failed|No usable sandbox/;
|
|
52
|
+
|
|
53
|
+
const launchFailed = (cause: unknown) =>
|
|
54
|
+
sandboxFailed.test(String(cause))
|
|
55
|
+
? undispatched(
|
|
56
|
+
"launch",
|
|
57
|
+
new Failed({
|
|
58
|
+
detail:
|
|
59
|
+
"Chromium's sandbox could not start on this host: it needs unprivileged user namespaces, and a user other than root",
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
: failed("launch", launchTimeoutMillis)(cause);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Launch Chromium and open a `Browser` over a fresh context.
|
|
66
|
+
*
|
|
67
|
+
* @stability stable
|
|
68
|
+
* @category constructors
|
|
69
|
+
* @since 0.3.0
|
|
70
|
+
*/
|
|
71
|
+
export const make = Effect.fn("Chromium.make")(function* (options: Options = {}) {
|
|
72
|
+
const launched = yield* Effect.acquireRelease(
|
|
73
|
+
Effect.tryPromise({
|
|
74
|
+
try: () =>
|
|
75
|
+
chromium.launch({
|
|
76
|
+
headless: options.headless ?? true,
|
|
77
|
+
chromiumSandbox: options.sandbox ?? false,
|
|
78
|
+
timeout: launchTimeoutMillis,
|
|
79
|
+
// The program owns its signals. Playwright's handlers close every browser on any of
|
|
80
|
+
// these, and on SIGINT then exit the process, so no finalizer would run.
|
|
81
|
+
handleSIGINT: false,
|
|
82
|
+
handleSIGTERM: false,
|
|
83
|
+
handleSIGHUP: false,
|
|
84
|
+
...(options.executablePath === undefined
|
|
85
|
+
? {}
|
|
86
|
+
: { executablePath: options.executablePath }),
|
|
87
|
+
...(options.args === undefined ? {} : { args: [...options.args] }),
|
|
88
|
+
}),
|
|
89
|
+
catch: launchFailed,
|
|
90
|
+
}).pipe(Effect.withSpan("Chromium.launch", {}, { captureStackTrace: false })),
|
|
91
|
+
(browser) => Effect.tryPromise(() => browser.close()).pipe(Effect.ignore),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const context = yield* Effect.tryPromise({
|
|
95
|
+
try: () =>
|
|
96
|
+
launched.newContext({
|
|
97
|
+
viewport: options.viewport ?? { width: 1280, height: 720 },
|
|
98
|
+
deviceScaleFactor: options.deviceScaleFactor ?? 1,
|
|
99
|
+
...(options.userAgent === undefined ? {} : { userAgent: options.userAgent }),
|
|
100
|
+
...(options.locale === undefined ? {} : { locale: options.locale }),
|
|
101
|
+
}),
|
|
102
|
+
catch: failed("launch"),
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return yield* Browser.make(context, { provider: "chromium" }, options);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A `Browser` Layer that launches Chromium when built and closes it when released.
|
|
110
|
+
*
|
|
111
|
+
* @stability stable
|
|
112
|
+
* @category layers
|
|
113
|
+
* @since 0.3.0
|
|
114
|
+
*/
|
|
115
|
+
export const layer = (options: Options = {}): Layer.Layer<Browser.Browser, BrowserError> =>
|
|
116
|
+
Layer.effect(Browser.Browser, make(options));
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* `layer`, with the options as `Config` values.
|
|
120
|
+
*
|
|
121
|
+
* @stability stable
|
|
122
|
+
* @category layers
|
|
123
|
+
* @since 0.3.0
|
|
124
|
+
*/
|
|
125
|
+
export const layerConfig = (
|
|
126
|
+
config: Config.Wrap<Options>,
|
|
127
|
+
): Layer.Layer<Browser.Browser, BrowserError | Config.ConfigError> =>
|
|
128
|
+
Layer.effect(Browser.Browser, Config.unwrap(config).pipe(Effect.flatMap(make)));
|