uiport 0.0.0-bootstrap.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/CHANGELOG.md +23 -0
- package/CODE_OF_CONDUCT.md +83 -0
- package/CONTRIBUTING.md +30 -0
- package/GOVERNANCE.md +9 -0
- package/LICENSE +21 -0
- package/NOTICE +12 -0
- package/README.md +101 -0
- package/README.pt-BR.md +97 -0
- package/SECURITY.md +12 -0
- package/bin/cli.mjs +114 -0
- package/docs/architecture.md +32 -0
- package/docs/demo.png +0 -0
- package/docs/migration.md +29 -0
- package/docs/support.md +49 -0
- package/examples/css-motion/index.html +3 -0
- package/examples/responsive-hero/index.html +39 -0
- package/examples/responsive-hero/section.svg +1 -0
- package/package.json +79 -0
- package/scripts/browser-host.mjs +41 -0
- package/scripts/browser-process.mjs +115 -0
- package/scripts/browser-resources.mjs +74 -0
- package/scripts/capture.mjs +307 -0
- package/scripts/css-context.mjs +174 -0
- package/scripts/dom.mjs +412 -0
- package/scripts/lib.mjs +365 -0
- package/scripts/options.mjs +178 -0
- package/scripts/resources.mjs +265 -0
- package/scripts/serve.mjs +26 -0
- package/scripts/validate.mjs +212 -0
- package/skills/uiport/SKILL.md +20 -0
- package/uiport.md +155 -0
package/docs/support.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# UIport 0.1 support matrix
|
|
2
|
+
|
|
3
|
+
UIport exports a rendered section or a selected whole page. It does not reconstruct a complete application backend or crawl a site. A successful capture is not a general proof of fidelity.
|
|
4
|
+
|
|
5
|
+
| Capability | Scope in 0.1 | Evidence / limitation |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| DOM and original CSS | Supported for section containers and body/html roots | Original fixtures tested at four viewports; entire stylesheets retained conservatively |
|
|
8
|
+
| Responsive CSS | Media queries retained | The first DOM is exported; an observed change in element structure at another viewport produces a limitation |
|
|
9
|
+
| Images, srcset, CSS resources (including image-set) | Reuse captured browser responses, then HTTP fallback | Copied resources have byte/deadline budgets; missing resources produce partial output |
|
|
10
|
+
| Fonts and CSS imports | Supported | Local font fixture; nested imports, layers, supports and media tested |
|
|
11
|
+
| SVG fragments | Preserved | Hash stays on local URL, e.g. `assets/…svg#shape` |
|
|
12
|
+
| Hover/focus and CSS animations | Original CSS preserved | Example states and animations exercised by integration tests |
|
|
13
|
+
| Web Animations API | Observed keyframes/timing on DocumentTimeline | Paused/infinite timing tested; does not reconstruct triggering events |
|
|
14
|
+
| Open Shadow DOM | Partial | Declarative shadow tree and inline/adopted styles; simple local fixture covered |
|
|
15
|
+
| Source scripts, handlers and visual libraries | Preserved as in the prototype; capture --omit-scripts opts out | Scripts in the selection and recognized visual libraries are copied; initialization outside the selection, module dependencies and arbitrary listeners still need review. Preserved source code reports SOURCE_RUNTIME_UNVERIFIED |
|
|
16
|
+
| Canvas | Static readable frame only | An unreadable frame is reported; no dynamic WebGL guarantee |
|
|
17
|
+
| Iframes, object/embed | Disabled | Embedded documents are not extracted or allowed to run automatically |
|
|
18
|
+
| Closed shadow roots | Unsupported | They cannot be generically inspected; absence cannot be reliably detected |
|
|
19
|
+
| Dynamic layouts and interactions | Manual review | Structural diagnostics do not detect every text/state change; no arbitrary listener equivalence proof |
|
|
20
|
+
|
|
21
|
+
## Validation semantics
|
|
22
|
+
|
|
23
|
+
`validate` visits the source and output in separate browser contexts. Each requested viewport gets a screenshot of the selected root. Animations are disabled for the screenshot; pixelmatch uses threshold 0.1 and excludes antialias differences. Defaults permit a 0.02 differing-pixel ratio, identical width and up to 5 px height difference.
|
|
24
|
+
|
|
25
|
+
Output requests to any other HTTP(S) origin are blocked by default, even another localhost port. Request failures, HTTP errors and browser errors fail the check. `--allow-external` permits external requests but still reports them; the result does not establish offline portability. Links not clicked, hover states not exercised and future media loads are not certified by screenshots.
|
|
26
|
+
|
|
27
|
+
The report always says `behavior: "not-tested"`. Capture limitations are also embedded as JSON inside `index.html` and surfaced by validation, so moving the artifact does not discard that context. A partial capture may pass a static visual check without becoming a complete behavioral clone.
|
|
28
|
+
|
|
29
|
+
Contextual custom-property rules that matched the source but lose structural context in the export are adapted for the captured elements. Their original declarations, specificity and media/supports/container/layer conditions remain intact. This does not infer unseen states or guarantee arbitrary CSS/runtime layouts.
|
|
30
|
+
|
|
31
|
+
Existing prototype exports are accepted by their legacy root marker; validate does not rewrite them. The locale default remains pt-BR.
|
|
32
|
+
|
|
33
|
+
## Limits and diagnostics
|
|
34
|
+
|
|
35
|
+
- One CSS selector matching one section container, body or html. Body/html export a single whole page without nesting another html/body; html also includes original head scripts.
|
|
36
|
+
- Viewports: 1–8, dimensions 200–3840 pixels. Screenshot comparisons capped at 20 megapixels.
|
|
37
|
+
- Total operation: default 120 seconds, configurable 1–600 seconds. The deadline also covers browser startup; cancellation closes the owned browser process tree. Process cleanup can add a short shutdown interval. Source navigation is bounded; resource fetches have up to 15 seconds each.
|
|
38
|
+
- Lazy scroll: default 80 steps, configurable 1–1000. `SCROLL_LIMIT` means content may remain unloaded.
|
|
39
|
+
- Export resource budget: default 20 MiB/file, 100 MiB total; controls downloaded copies, not source browser memory. Successful responses from the fresh capture context are reused, including Referer-dependent resources. Retention has the same per-file/aggregate budgets, but Playwright materializes response bodies before actual-size validation; peak memory is not bounded by these budgets. All referenced CSS resources may be retained, including resources unused by the selected DOM, because CSS is conservative.
|
|
40
|
+
- Public HTTP(S) only; no session/cookie import. Local development URLs are supported. The capture reuses its own navigation responses; fallback fetch does not import browser cookies.
|
|
41
|
+
- Source JavaScript executes in the capture browser. The optional --omit-scripts mode is not a general-purpose sanitizer or sandbox for hostile websites.
|
|
42
|
+
- Source style values and content remain in the HTML. Review artifacts before redistribution. The CLI redacts URL credentials, queries and fragments in resource diagnostics and operational errors and does not print source page content or form values.
|
|
43
|
+
- Network/rendering differences, DOM changed by runtime, special CSS/HTML features and states not observed can still require manual work. No invented replacement effects.
|
|
44
|
+
|
|
45
|
+
Common partial-result codes: `SOURCE_RUNTIME_UNVERIFIED`, `SOURCE_RUNTIME_OMITTED`, `RESOURCE_UNAVAILABLE`, `STYLESHEET_UNAVAILABLE`, `SCROLL_LIMIT`, `RESPONSIVE_DOM_CHANGED`, `CANVAS_STATIC`, `CANVAS_UNREADABLE`, `EMBED_OMITTED`, `ANIMATION_UNSUPPORTED`, `SOURCE_REQUEST_FAILED`, `SOURCE_SCRIPT_ERROR`, `CSS_IMPORT_CYCLE`, `CSS_IMPORT_UNSUPPORTED`.
|
|
46
|
+
|
|
47
|
+
## Supported environments
|
|
48
|
+
|
|
49
|
+
Node >=22.14; Node 24 LTS recommended. CI covers Linux (Node 22.14 and 24), macOS and Windows (Node 24). Browser installation uses the Playwright version pinned in the package. Chrome/Edge fallback is supported, but controlled visual comparisons are most repeatable with bundled Chromium. Firefox/WebKit and browser extension workflows are outside 0.1.
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>UIport — CSS motion example</title><style>
|
|
2
|
+
*{box-sizing:border-box}body{margin:0;background:#f5f1e9;color:#252b23;font-family:system-ui,sans-serif}#motion{max-width:1100px;margin:auto;padding:clamp(28px,7vw,80px)}.label{font:12px ui-monospace,monospace;letter-spacing:2px}h1{font-size:clamp(38px,6vw,72px);letter-spacing:-.05em;line-height:1.1;max-width:700px;font-weight:550}.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:20px}.card{padding:28px;background:#e6e9da;border:1px solid #c9cebe;border-radius:16px;min-height:210px;transition:background .2s ease,transform .2s ease}.card:hover,.card:focus-visible{background:#d1e5ab;transform:translateY(-6px);outline:2px solid #3f6029;outline-offset:3px}.dot{width:28px;height:28px;border-radius:50%;background:#62833e;animation:breathe 2s ease-in-out infinite alternate}.square{width:28px;height:28px;background:#a27539;animation:turn 6s linear infinite}.card p{font-size:14px;line-height:1.6;color:#5c6456}.footer{margin-top:40px;font-size:12px;color:#5c6456}@keyframes breathe{from{transform:scale(.8);opacity:.45}to{transform:scale(1.2);opacity:1}}@keyframes turn{to{transform:rotate(360deg)}}@media(max-width:700px){.grid{grid-template-columns:1fr}}@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation:none!important;transition:none!important}}
|
|
3
|
+
</style></head><body><section id="motion"><p class="label">UIPORT / MOTION STUDY</p><h1>Small details.<br>Real CSS.</h1><div class="grid"><article tabindex="0" class="card"><div class="dot"></div><h2>A little rhythm</h2><p>An original CSS keyframe animation with its timing intact.</p></article><article tabindex="0" class="card"><div class="square"></div><h2>A different angle</h2><p>A continuous rotation. The animation travels with the stylesheet.</p></article><article tabindex="0" class="card"><div style="font-size:28px">↗</div><h2>Try a state</h2><p>Hover or focus this card. The original transition remains editable.</p></article></div><p class="footer">Original UIport example · MIT · No JavaScript required</p></section></body></html>
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
5
|
+
<title>UIport — original responsive example</title>
|
|
6
|
+
<style>
|
|
7
|
+
:root { color-scheme: dark; --paper: #eef0e6; --accent: #bded7b; }
|
|
8
|
+
* { box-sizing: border-box; }
|
|
9
|
+
body { margin: 0; background: #111713; color: var(--paper); font-family: system-ui, sans-serif; }
|
|
10
|
+
a { color: inherit; }
|
|
11
|
+
#hero { padding: clamp(24px, 5vw, 80px); max-width: 1600px; margin: auto; }
|
|
12
|
+
.top { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #3b443b; padding-bottom: 24px; }
|
|
13
|
+
.wordmark { font-size: 23px; font-weight: 750; letter-spacing: -1px; }
|
|
14
|
+
.tag { color: #b7c2b5; font-size: 12px; text-transform: uppercase; letter-spacing: 2px; }
|
|
15
|
+
.layout { display: grid; grid-template-columns: 1.2fr 1fr; align-items: center; gap: 64px; padding-block: 80px 72px; }
|
|
16
|
+
.eyebrow { color: var(--accent); font: 12px ui-monospace, monospace; letter-spacing: 2px; }
|
|
17
|
+
h1 { margin: 24px 0; font-size: clamp(44px, 5.6vw, 84px); line-height: 1.04; font-weight: 550; letter-spacing: -0.065em; }
|
|
18
|
+
h1 span { color: var(--accent); }
|
|
19
|
+
.intro { max-width: 430px; color: #b7c2b5; font-size: 17px; line-height: 1.7; }
|
|
20
|
+
.button { display: inline-flex; margin-top: 24px; padding: 16px 24px; background: var(--accent); color: #15220d; border-radius: 8px; font-weight: 650; text-decoration: none; transition: background 160ms ease, transform 160ms ease; }
|
|
21
|
+
.button:hover, .button:focus-visible { background: #d6ff9e; transform: translateY(-3px); outline: 2px solid white; outline-offset: 4px; }
|
|
22
|
+
.art { position: relative; min-width: 0; padding: 24px; border: 1px solid #4a5649; border-radius: 18px; background: #1c271e; box-shadow: 0 28px 70px #050a0766; }
|
|
23
|
+
.art img { display: block; width: 100%; border-radius: 10px; }
|
|
24
|
+
.caption { display: flex; justify-content: space-between; margin-top: 24px; color: #b7c2b5; font: 11px ui-monospace,monospace; }
|
|
25
|
+
.bottom { display: flex; gap: 24px; justify-content: space-between; border-top: 1px solid #3b443b; padding-top: 24px; color: #b7c2b5; font: 11px ui-monospace, monospace; }
|
|
26
|
+
@media (max-width: 760px) { .layout { grid-template-columns: 1fr; gap: 40px; padding-block: 48px; } .tag { letter-spacing: 0; font-size: 10px; } .bottom { flex-direction: column; gap: 12px; } }
|
|
27
|
+
</style>
|
|
28
|
+
</head>
|
|
29
|
+
<body>
|
|
30
|
+
<main id="hero">
|
|
31
|
+
<header class="top"><span class="wordmark">UIport ↗</span><span class="tag">An original open source example</span></header>
|
|
32
|
+
<div class="layout">
|
|
33
|
+
<div><div class="eyebrow">FROM BROWSER TO BUILDER</div><h1>Start with<br>a section.<br><span>Make it yours.</span></h1><p class="intro">A small, editable starting point for your next idea. Capture a section, inspect the details, and keep building.</p><a class="button" href="#details">Explore the details ↗</a></div>
|
|
34
|
+
<figure class="art"><img src="./section.svg" alt="Abstract green layers representing a portable web section" width="480" height="420"><figcaption class="caption"><span>HTML + CSS + ASSETS</span><span>01 / PORTABLE</span></figcaption></figure>
|
|
35
|
+
</div>
|
|
36
|
+
<footer class="bottom" id="details"><span>CREATED FOR UIPORT · MIT LICENSE</span><span>Local files. Your workflow.</span></footer>
|
|
37
|
+
</main>
|
|
38
|
+
</body>
|
|
39
|
+
</html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 420"><defs><linearGradient id="g" x2="1" y2="1"><stop stop-color="#98c063"/><stop offset="1" stop-color="#344b2e"/></linearGradient></defs><rect width="480" height="420" rx="12" fill="#121c14"/><g fill="none" stroke="#334933"><path d="M0 84h480M0 168h480M0 252h480M0 336h480M96 0v420M192 0v420M288 0v420M384 0v420"/></g><g transform="translate(240 190) rotate(-12)"><rect x="-146" y="-92" width="300" height="222" rx="12" fill="#243725" stroke="#52634b"/><rect x="-164" y="-120" width="300" height="222" rx="12" fill="url(#g)" stroke="#bddb98"/><path d="M-144-78H116" stroke="#d8eabd"/><circle cx="-142" cy="-99" r="3" fill="#f0ffda"/><circle cx="-128" cy="-99" r="3" fill="#f0ffda"/><path d="M-136-48h133M-136-24h102M-136 0h115" stroke="#d3edb4" stroke-width="10"/><rect x="-138" y="33" width="85" height="30" rx="5" fill="#162a15"/><rect x="26" y="-51" width="82" height="120" rx="7" fill="#213a21"/><path d="m42 34 50-54m-41 0h41v41" stroke="#c4f386" stroke-width="5" fill="none"/></g></svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "uiport",
|
|
3
|
+
"version": "0.0.0-bootstrap.0",
|
|
4
|
+
"description": "Bring a rendered web section into your next project as editable HTML, CSS and assets.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"uiport": "bin/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=22.14.0"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Lorran Hippolyte (https://github.com/LorranHippolyte)",
|
|
14
|
+
"contributors": [
|
|
15
|
+
"LuminaSoft"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/LorranHippolyte/uiport.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/LorranHippolyte/uiport#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/LorranHippolyte/uiport/issues"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"cli",
|
|
27
|
+
"html",
|
|
28
|
+
"css",
|
|
29
|
+
"web-development",
|
|
30
|
+
"playwright",
|
|
31
|
+
"extraction",
|
|
32
|
+
"ai-agents"
|
|
33
|
+
],
|
|
34
|
+
"files": [
|
|
35
|
+
"bin/",
|
|
36
|
+
"scripts/",
|
|
37
|
+
"examples/",
|
|
38
|
+
"skills/",
|
|
39
|
+
"docs/support.md",
|
|
40
|
+
"README.pt-BR.md",
|
|
41
|
+
"uiport.md",
|
|
42
|
+
"NOTICE",
|
|
43
|
+
"docs/architecture.md",
|
|
44
|
+
"docs/migration.md",
|
|
45
|
+
"docs/demo.png",
|
|
46
|
+
"CONTRIBUTING.md",
|
|
47
|
+
"SECURITY.md",
|
|
48
|
+
"CODE_OF_CONDUCT.md",
|
|
49
|
+
"GOVERNANCE.md",
|
|
50
|
+
"CHANGELOG.md"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"lint": "eslint bin scripts tools test",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"build": "node tools/check-syntax.mjs",
|
|
56
|
+
"test": "node --test --test-concurrency=1 test/*.test.mjs",
|
|
57
|
+
"test:unit": "node --test test/unit.test.mjs",
|
|
58
|
+
"test:package": "node tools/test-package.mjs",
|
|
59
|
+
"check": "npm run format:check && npm run lint && npm run typecheck && npm run build && npm test && npm run test:package",
|
|
60
|
+
"format:check": "prettier --check bin scripts test tools/*.mjs *.mjs *.json .github",
|
|
61
|
+
"demo": "node tools/demo.mjs"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"parse-srcset": "1.0.2",
|
|
65
|
+
"parse5": "7.3.0",
|
|
66
|
+
"pixelmatch": "7.1.0",
|
|
67
|
+
"playwright-core": "1.62.1",
|
|
68
|
+
"pngjs": "7.0.0",
|
|
69
|
+
"postcss": "8.5.28",
|
|
70
|
+
"postcss-value-parser": "4.2.0"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@eslint/js": "10.0.1",
|
|
74
|
+
"@types/node": "22.19.0",
|
|
75
|
+
"eslint": "10.11.0",
|
|
76
|
+
"prettier": "3.6.2",
|
|
77
|
+
"typescript": "5.9.3"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Own the browser in a separate process so startup can be cancelled through
|
|
2
|
+
// Playwright's process signal handlers, before a BrowserServer is available.
|
|
3
|
+
import { chromium } from "playwright-core";
|
|
4
|
+
|
|
5
|
+
let server;
|
|
6
|
+
let closing;
|
|
7
|
+
const close = () =>
|
|
8
|
+
(closing ||= (async () => {
|
|
9
|
+
if (server) {
|
|
10
|
+
// Keep the owner alive until Playwright has reaped the browser and removed
|
|
11
|
+
// its profiles/artifacts, including on Windows.
|
|
12
|
+
await server.close();
|
|
13
|
+
if (process.connected) process.disconnect();
|
|
14
|
+
} else if (process.platform !== "win32") {
|
|
15
|
+
process.kill(process.pid, "SIGTERM");
|
|
16
|
+
}
|
|
17
|
+
})());
|
|
18
|
+
process.on("message", (message) => {
|
|
19
|
+
if (message.action === "close") void close();
|
|
20
|
+
});
|
|
21
|
+
process.once("message", async (options) => {
|
|
22
|
+
try {
|
|
23
|
+
server = await chromium.launchServer({
|
|
24
|
+
...options,
|
|
25
|
+
host: "127.0.0.1",
|
|
26
|
+
handleSIGINT: true,
|
|
27
|
+
handleSIGTERM: true,
|
|
28
|
+
handleSIGHUP: true,
|
|
29
|
+
});
|
|
30
|
+
if (!process.connected) {
|
|
31
|
+
await server.kill();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
process.send({ endpoint: server.wsEndpoint() });
|
|
35
|
+
} catch {
|
|
36
|
+
// Browser startup logs can contain arbitrary executable output. The parent
|
|
37
|
+
// supplies a controlled, actionable diagnostic instead of forwarding it.
|
|
38
|
+
if (process.connected) process.disconnect();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
process.once("disconnect", () => void close());
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { fork, spawn } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
export async function startBrowser(chromium, options, life) {
|
|
8
|
+
life?.check();
|
|
9
|
+
const temporary = await fs.mkdtemp(path.join(os.tmpdir(), "uiport-browser-"));
|
|
10
|
+
const removeTemporary = () =>
|
|
11
|
+
fs.rm(temporary, {
|
|
12
|
+
recursive: true,
|
|
13
|
+
force: true,
|
|
14
|
+
maxRetries: 3,
|
|
15
|
+
retryDelay: 100,
|
|
16
|
+
});
|
|
17
|
+
let child;
|
|
18
|
+
try {
|
|
19
|
+
life?.check();
|
|
20
|
+
child = fork(
|
|
21
|
+
fileURLToPath(new URL("./browser-host.mjs", import.meta.url)),
|
|
22
|
+
[],
|
|
23
|
+
{
|
|
24
|
+
stdio: ["ignore", "ignore", "ignore", "ipc"],
|
|
25
|
+
env: {
|
|
26
|
+
...process.env,
|
|
27
|
+
TMPDIR: temporary,
|
|
28
|
+
TMP: temporary,
|
|
29
|
+
TEMP: temporary,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
await removeTemporary();
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
let closed = false;
|
|
38
|
+
const exited = new Promise((resolve) =>
|
|
39
|
+
child.once("close", () => {
|
|
40
|
+
closed = true;
|
|
41
|
+
resolve();
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
let closing;
|
|
45
|
+
const close = () =>
|
|
46
|
+
(closing ||= (async () => {
|
|
47
|
+
if (closed) {
|
|
48
|
+
await removeTemporary();
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (child.connected) child.send({ action: "close" }, () => {});
|
|
52
|
+
let treeKill;
|
|
53
|
+
// A second POSIX signal makes Playwright force-close a browser that ignores
|
|
54
|
+
// graceful shutdown. Its handler owns the browser's separate process group.
|
|
55
|
+
const retry = setTimeout(
|
|
56
|
+
() => {
|
|
57
|
+
if (closed) return;
|
|
58
|
+
if (process.platform === "win32" && child.pid) {
|
|
59
|
+
// Only force the owned tree if cooperative cleanup has not finished.
|
|
60
|
+
const killer = spawn(
|
|
61
|
+
"taskkill",
|
|
62
|
+
["/pid", String(child.pid), "/t", "/f"],
|
|
63
|
+
{ stdio: "ignore", timeout: 1500 },
|
|
64
|
+
);
|
|
65
|
+
treeKill = new Promise((resolve) => {
|
|
66
|
+
killer.once("error", resolve);
|
|
67
|
+
killer.once("close", resolve);
|
|
68
|
+
});
|
|
69
|
+
} else child.kill("SIGTERM");
|
|
70
|
+
},
|
|
71
|
+
process.platform === "win32" ? 1000 : 250,
|
|
72
|
+
);
|
|
73
|
+
const force = setTimeout(
|
|
74
|
+
() => {
|
|
75
|
+
if (!closed) child.kill("SIGKILL");
|
|
76
|
+
},
|
|
77
|
+
process.platform === "win32" ? 3000 : 1500,
|
|
78
|
+
);
|
|
79
|
+
await exited;
|
|
80
|
+
clearTimeout(retry);
|
|
81
|
+
clearTimeout(force);
|
|
82
|
+
await treeKill;
|
|
83
|
+
// The parent owns this directory, so forced startup cancellation can also
|
|
84
|
+
// remove profiles whose child-side cleanup could not finish.
|
|
85
|
+
await removeTemporary();
|
|
86
|
+
})());
|
|
87
|
+
life?.add(close);
|
|
88
|
+
let timeout;
|
|
89
|
+
try {
|
|
90
|
+
const endpoint = await new Promise((resolve, reject) => {
|
|
91
|
+
child.once("error", () =>
|
|
92
|
+
reject(new Error("Browser host could not start")),
|
|
93
|
+
);
|
|
94
|
+
child.once("close", () => reject(new Error("Browser startup failed")));
|
|
95
|
+
child.once("message", (message) => resolve(message.endpoint));
|
|
96
|
+
timeout = setTimeout(() => void close(), options.timeout);
|
|
97
|
+
child.send(options, (error) => {
|
|
98
|
+
if (error) reject(new Error("Browser host could not start"));
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
life?.check();
|
|
102
|
+
const browser = await chromium.connect(endpoint, {
|
|
103
|
+
timeout: life?.remaining() ?? options.timeout,
|
|
104
|
+
});
|
|
105
|
+
browser.on("disconnected", () => void close());
|
|
106
|
+
life?.check();
|
|
107
|
+
return { browser, close };
|
|
108
|
+
} catch (error) {
|
|
109
|
+
await close();
|
|
110
|
+
life?.check();
|
|
111
|
+
throw error;
|
|
112
|
+
} finally {
|
|
113
|
+
clearTimeout(timeout);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { normalizeUrl } from "./lib.mjs";
|
|
2
|
+
|
|
3
|
+
// Retain the bytes received by this capture's fresh browser context. No personal
|
|
4
|
+
// browser profile/cookies are imported. Playwright materializes response.body();
|
|
5
|
+
// these budgets limit retention, not the source browser's peak memory usage.
|
|
6
|
+
export function browserResources(page, options, signal) {
|
|
7
|
+
const entries = new Map();
|
|
8
|
+
let retained = 0;
|
|
9
|
+
let pending = Promise.resolve();
|
|
10
|
+
const receive = (response) => {
|
|
11
|
+
const request = response.request();
|
|
12
|
+
if (
|
|
13
|
+
!["stylesheet", "image", "media", "font", "script"].includes(
|
|
14
|
+
request.resourceType(),
|
|
15
|
+
) ||
|
|
16
|
+
response.status() < 200 ||
|
|
17
|
+
response.status() >= 300 ||
|
|
18
|
+
!/^https?:/i.test(response.url())
|
|
19
|
+
)
|
|
20
|
+
return;
|
|
21
|
+
const url = normalizeUrl(response.url());
|
|
22
|
+
if (entries.has(url)) return;
|
|
23
|
+
const task = pending.then(async () => {
|
|
24
|
+
if (signal.aborted || retained >= options.maxTotalBytes) return null;
|
|
25
|
+
const length = Number(response.headers()["content-length"] || 0);
|
|
26
|
+
if (
|
|
27
|
+
length > options.maxResourceBytes ||
|
|
28
|
+
retained + length > options.maxTotalBytes
|
|
29
|
+
)
|
|
30
|
+
return null;
|
|
31
|
+
try {
|
|
32
|
+
const body = await response.body();
|
|
33
|
+
if (
|
|
34
|
+
signal.aborted ||
|
|
35
|
+
body.length > options.maxResourceBytes ||
|
|
36
|
+
retained + body.length > options.maxTotalBytes
|
|
37
|
+
)
|
|
38
|
+
return null;
|
|
39
|
+
retained += body.length;
|
|
40
|
+
return {
|
|
41
|
+
body,
|
|
42
|
+
contentType: response.headers()["content-type"] || "",
|
|
43
|
+
url: response.url(),
|
|
44
|
+
resourceType: request.resourceType(),
|
|
45
|
+
};
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
pending = task.then(() => {});
|
|
51
|
+
entries.set(url, task);
|
|
52
|
+
for (let alias = request; alias; alias = alias.redirectedFrom()) {
|
|
53
|
+
const key = normalizeUrl(alias.url());
|
|
54
|
+
if (!entries.has(key)) entries.set(key, task);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
page.on("response", receive);
|
|
58
|
+
return {
|
|
59
|
+
async get(url) {
|
|
60
|
+
signal.throwIfAborted();
|
|
61
|
+
const resource = await entries.get(normalizeUrl(url));
|
|
62
|
+
signal.throwIfAborted();
|
|
63
|
+
return resource || null;
|
|
64
|
+
},
|
|
65
|
+
async settle() {
|
|
66
|
+
await pending;
|
|
67
|
+
signal.throwIfAborted();
|
|
68
|
+
},
|
|
69
|
+
dispose() {
|
|
70
|
+
page.off("response", receive);
|
|
71
|
+
entries.clear();
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|