turno-browser 0.2.0 → 0.4.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 +228 -0
- package/README.md +205 -16
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +116 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +599 -0
- package/dist/api.d.ts +96 -0
- package/dist/api.js +88 -0
- package/dist/argv.d.ts +55 -3
- package/dist/argv.js +190 -10
- package/dist/batch.d.ts +19 -0
- package/dist/batch.js +148 -0
- package/dist/bench.d.ts +1 -1
- package/dist/bench.js +21 -5
- package/dist/bin/mcp.js +108 -12
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +333 -0
- package/dist/cdp-relay.d.ts +20 -0
- package/dist/cdp-relay.js +101 -0
- package/dist/chrome-features.d.ts +23 -0
- package/dist/chrome-features.js +44 -0
- package/dist/detectors.d.ts +11 -0
- package/dist/detectors.js +46 -4
- package/dist/diagnose.d.ts +45 -0
- package/dist/diagnose.js +326 -0
- package/dist/doctor.d.ts +85 -0
- package/dist/doctor.js +556 -0
- package/dist/engine-attach.d.ts +27 -0
- package/dist/engine-attach.js +165 -0
- package/dist/engine-camoufox.d.ts +32 -0
- package/dist/engine-camoufox.js +270 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +292 -0
- package/dist/engine.d.ts +119 -0
- package/dist/engine.js +355 -0
- package/dist/flows.d.ts +127 -0
- package/dist/flows.js +294 -0
- package/dist/governor.d.ts +119 -0
- package/dist/governor.js +325 -0
- package/dist/health.d.ts +60 -0
- package/dist/health.js +86 -0
- package/dist/host.d.ts +95 -0
- package/dist/host.js +1181 -0
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +443 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.d.ts +23 -10
- package/dist/identities.js +45 -37
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/ids.d.ts +9 -0
- package/dist/ids.js +34 -0
- package/dist/index.d.ts +21 -6
- package/dist/index.js +19 -18
- package/dist/lifecycle.d.ts +99 -0
- package/dist/lifecycle.js +172 -0
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +93 -0
- package/dist/live-page.d.ts +11 -0
- package/dist/live-page.js +143 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +640 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +203 -0
- package/dist/login-tool.d.ts +45 -0
- package/dist/login-tool.js +233 -0
- package/dist/login.d.ts +8 -3
- package/dist/login.js +65 -26
- package/dist/network-chrome.d.ts +74 -0
- package/dist/network-chrome.js +178 -0
- package/dist/network.d.ts +125 -0
- package/dist/network.js +263 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +151 -0
- package/dist/patchright.d.ts +4 -2
- package/dist/patchright.js +4 -2
- package/dist/recorder.d.ts +88 -0
- package/dist/recorder.js +538 -0
- package/dist/recordings.d.ts +116 -0
- package/dist/recordings.js +474 -0
- package/dist/redact.d.ts +34 -0
- package/dist/redact.js +135 -0
- package/dist/replay.d.ts +20 -0
- package/dist/replay.js +382 -0
- package/dist/reputation.d.ts +89 -0
- package/dist/reputation.js +203 -0
- package/dist/resources.d.ts +76 -0
- package/dist/resources.js +341 -0
- package/dist/runtime-lock.d.ts +59 -0
- package/dist/runtime-lock.js +131 -0
- package/dist/sealed.d.ts +8 -0
- package/dist/sealed.js +96 -0
- package/dist/session-export.d.ts +67 -0
- package/dist/session-export.js +194 -0
- package/dist/session.d.ts +20 -0
- package/dist/session.js +30 -0
- package/dist/settings.d.ts +246 -0
- package/dist/settings.js +530 -0
- package/dist/sharing.d.ts +96 -0
- package/dist/sharing.js +269 -0
- package/dist/snippet.d.ts +8 -0
- package/dist/snippet.js +22 -0
- package/dist/tab-locks.d.ts +63 -0
- package/dist/tab-locks.js +179 -0
- package/dist/tools.d.ts +111 -0
- package/dist/tools.js +59 -0
- package/dist/totp.d.ts +32 -0
- package/dist/totp.js +82 -0
- package/dist/tuning.d.ts +76 -0
- package/dist/tuning.js +69 -0
- package/dist/units.d.ts +8 -0
- package/dist/units.js +20 -0
- package/dist/upstream.d.ts +243 -0
- package/dist/upstream.js +46 -0
- package/dist/vault.d.ts +91 -0
- package/dist/vault.js +273 -0
- package/npm-shrinkwrap.json +1074 -3
- package/package.json +5 -4
- package/dist/paths.d.ts +0 -17
- package/dist/paths.js +0 -17
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { BrowserContext } from "patchright-core";
|
|
2
|
+
export interface BlockPolicy {
|
|
3
|
+
trackers: boolean;
|
|
4
|
+
types: readonly string[];
|
|
5
|
+
patterns: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
export interface ResourcePolicy {
|
|
8
|
+
block?: BlockPolicy;
|
|
9
|
+
/** Size cap of the cache in bytes; undefined: no cache. */
|
|
10
|
+
cacheBytes?: number;
|
|
11
|
+
}
|
|
12
|
+
/** Default cache size when identity.json has `"cache": {}`. */
|
|
13
|
+
export declare const DEFAULT_CACHE_BYTES: number;
|
|
14
|
+
/**
|
|
15
|
+
* The block check of a policy: why a request is blocked, or undefined. The page itself (a main
|
|
16
|
+
* frame navigation) never is.
|
|
17
|
+
*/
|
|
18
|
+
export declare function blocker(policy: BlockPolicy): (url: URL, type: string, mainFrameNavigation: boolean) => string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* How long a response stays fresh, in ms, from its headers (RFC 9111, as a private cache);
|
|
21
|
+
* undefined when it must not be stored or reused without asking the server.
|
|
22
|
+
*/
|
|
23
|
+
export declare function freshness(headers: Record<string, string>, now?: number): number | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Whether a finished response may go into the cache. Never one with Set-Cookie, never one asked
|
|
26
|
+
* for with Authorization, and a Vary only on Accept-Encoding (the body is stored decoded) or
|
|
27
|
+
* Origin (kept with the entry).
|
|
28
|
+
*/
|
|
29
|
+
export declare function storable(method: string, type: string, status: number, request: Record<string, string>, response: Record<string, string>): boolean;
|
|
30
|
+
interface EntryMeta {
|
|
31
|
+
url: string;
|
|
32
|
+
status: number;
|
|
33
|
+
headers: Record<string, string>;
|
|
34
|
+
expiresAt: number;
|
|
35
|
+
/** The request's Origin when the response varies on it. */
|
|
36
|
+
origin?: string | null;
|
|
37
|
+
size: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The files of the cache: `<key>.json` (written last, so its presence means the entry is whole)
|
|
41
|
+
* and `<key>.bin`, both mode 600 in a directory of mode 700, written to a temporary name and
|
|
42
|
+
* renamed. Least recently used entries go first when the total passes the cap.
|
|
43
|
+
*/
|
|
44
|
+
export declare class ResourceCache {
|
|
45
|
+
private readonly dir;
|
|
46
|
+
private readonly maxBytes;
|
|
47
|
+
private readonly now;
|
|
48
|
+
private readonly index;
|
|
49
|
+
private total;
|
|
50
|
+
constructor(dir: string, maxBytes: number, now?: () => number);
|
|
51
|
+
/** Largest single entry: a tenth of the cache, and never more than 20 MB. */
|
|
52
|
+
get maxEntry(): number;
|
|
53
|
+
get size(): number;
|
|
54
|
+
static key(url: string): string;
|
|
55
|
+
/** A fresh entry for this URL and Origin, or undefined. */
|
|
56
|
+
get(url: string, origin: string | undefined): Promise<{
|
|
57
|
+
meta: EntryMeta;
|
|
58
|
+
body: Buffer;
|
|
59
|
+
} | undefined>;
|
|
60
|
+
/** Stores an entry; false when it is too large or could not be written. */
|
|
61
|
+
put(meta: EntryMeta, body: Buffer): Promise<boolean>;
|
|
62
|
+
private evict;
|
|
63
|
+
private remove;
|
|
64
|
+
}
|
|
65
|
+
/** What the route did so far in this browser. */
|
|
66
|
+
export interface ResourceStats {
|
|
67
|
+
blocked: number;
|
|
68
|
+
hits: number;
|
|
69
|
+
stored: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Routes the context's requests through the policy. Does nothing (and leaves Chrome's HTTP cache
|
|
73
|
+
* on) when the policy blocks nothing and has no cache, or the cache has no directory.
|
|
74
|
+
*/
|
|
75
|
+
export declare function installResources(context: BrowserContext, policy: ResourcePolicy, cacheDir: string | undefined): Promise<ResourceStats | undefined>;
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import { readFile, rename, rm, utimes, writeFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
/**
|
|
6
|
+
* Opt-in resource blocking and an opt-in on-disk resource cache, per identity. Both
|
|
7
|
+
* are off by default; identity.json `block` and `cache` turn them on.
|
|
8
|
+
*
|
|
9
|
+
* One context route sees every request. Blocking aborts it (`blockedbyclient`), as an ad
|
|
10
|
+
* blocker does. The cache serves a fresh copy of an image, font, stylesheet or script from disk
|
|
11
|
+
* with `route.fulfill`, and otherwise lets the request go on in Chrome (`route.fallback`), so
|
|
12
|
+
* Chrome's own network stack and its TLS fingerprint fetch it; the body is stored when the
|
|
13
|
+
* request finishes. Nothing is fetched from Node.
|
|
14
|
+
*
|
|
15
|
+
* A route turns off Chrome's own HTTP cache for the context (Playwright does that whenever it
|
|
16
|
+
* intercepts), which is why the cache is worth having next to blocking.
|
|
17
|
+
*/
|
|
18
|
+
/** Ad and tracker hosts the `trackers` switch blocks, with their subdomains. Kept short on purpose. */
|
|
19
|
+
const TRACKER_HOSTS = Object.freeze([
|
|
20
|
+
"doubleclick.net",
|
|
21
|
+
"googlesyndication.com",
|
|
22
|
+
"googleadservices.com",
|
|
23
|
+
"google-analytics.com",
|
|
24
|
+
"googletagmanager.com",
|
|
25
|
+
"googletagservices.com",
|
|
26
|
+
"adservice.google.com",
|
|
27
|
+
"connect.facebook.net",
|
|
28
|
+
"ads-twitter.com",
|
|
29
|
+
"analytics.tiktok.com",
|
|
30
|
+
"bat.bing.com",
|
|
31
|
+
"clarity.ms",
|
|
32
|
+
"hotjar.com",
|
|
33
|
+
"scorecardresearch.com",
|
|
34
|
+
"quantserve.com",
|
|
35
|
+
"moatads.com",
|
|
36
|
+
"adnxs.com",
|
|
37
|
+
"adsrvr.org",
|
|
38
|
+
"amazon-adsystem.com",
|
|
39
|
+
"criteo.com",
|
|
40
|
+
"criteo.net",
|
|
41
|
+
"taboola.com",
|
|
42
|
+
"outbrain.com",
|
|
43
|
+
"pubmatic.com",
|
|
44
|
+
"rubiconproject.com",
|
|
45
|
+
"openx.net",
|
|
46
|
+
"casalemedia.com",
|
|
47
|
+
"mathtag.com",
|
|
48
|
+
]);
|
|
49
|
+
/** Resource types the cache keeps: the static ones. */
|
|
50
|
+
const CACHED_TYPES = new Set(["image", "font", "stylesheet", "script"]);
|
|
51
|
+
/** Response headers never replayed from the cache: the body is stored decoded, and no cookie. */
|
|
52
|
+
const DROPPED_HEADERS = new Set([
|
|
53
|
+
"content-encoding",
|
|
54
|
+
"content-length",
|
|
55
|
+
"transfer-encoding",
|
|
56
|
+
"connection",
|
|
57
|
+
"keep-alive",
|
|
58
|
+
"set-cookie",
|
|
59
|
+
"age",
|
|
60
|
+
]);
|
|
61
|
+
/** Default cache size when identity.json has `"cache": {}`. */
|
|
62
|
+
export const DEFAULT_CACHE_BYTES = 100 * 1024 ** 2;
|
|
63
|
+
function matcher(pattern) {
|
|
64
|
+
const p = pattern.trim().toLowerCase();
|
|
65
|
+
if (!p.includes("/") && !p.includes("*"))
|
|
66
|
+
return (url) => url.hostname === p || url.hostname.endsWith(`.${p}`);
|
|
67
|
+
const glob = p.includes("://") || p.startsWith("*") ? p : `*://${p}`;
|
|
68
|
+
const re = new RegExp(`^${glob
|
|
69
|
+
.split("*")
|
|
70
|
+
.map((s) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
|
|
71
|
+
.join(".*")}$`);
|
|
72
|
+
return (url) => re.test(url.href.toLowerCase());
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The block check of a policy: why a request is blocked, or undefined. The page itself (a main
|
|
76
|
+
* frame navigation) never is.
|
|
77
|
+
*/
|
|
78
|
+
export function blocker(policy) {
|
|
79
|
+
const patterns = policy.patterns.map(matcher);
|
|
80
|
+
const trackers = policy.trackers ? TRACKER_HOSTS.map((h) => matcher(h)) : [];
|
|
81
|
+
return (url, type, mainFrameNavigation) => {
|
|
82
|
+
if (mainFrameNavigation)
|
|
83
|
+
return undefined;
|
|
84
|
+
if (policy.types.includes(type))
|
|
85
|
+
return `type ${type}`;
|
|
86
|
+
if (trackers.some((m) => m(url)))
|
|
87
|
+
return "tracker";
|
|
88
|
+
if (patterns.some((m) => m(url)))
|
|
89
|
+
return "pattern";
|
|
90
|
+
return undefined;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* How long a response stays fresh, in ms, from its headers (RFC 9111, as a private cache);
|
|
95
|
+
* undefined when it must not be stored or reused without asking the server.
|
|
96
|
+
*/
|
|
97
|
+
export function freshness(headers, now = Date.now()) {
|
|
98
|
+
const cc = (headers["cache-control"] ?? "").toLowerCase();
|
|
99
|
+
const directives = new Map(cc
|
|
100
|
+
.split(",")
|
|
101
|
+
.map((d) => d.trim())
|
|
102
|
+
.filter(Boolean)
|
|
103
|
+
.map((d) => {
|
|
104
|
+
const [k, v] = d.split("=", 2);
|
|
105
|
+
return [k.trim(), v?.trim().replace(/^"|"$/g, "")];
|
|
106
|
+
}));
|
|
107
|
+
if (directives.has("no-store") || directives.has("no-cache"))
|
|
108
|
+
return undefined;
|
|
109
|
+
let lifetime;
|
|
110
|
+
const maxAge = directives.get("max-age");
|
|
111
|
+
if (maxAge !== undefined) {
|
|
112
|
+
const n = Number(maxAge);
|
|
113
|
+
if (Number.isFinite(n))
|
|
114
|
+
lifetime = n * 1000;
|
|
115
|
+
}
|
|
116
|
+
else if (headers.expires) {
|
|
117
|
+
const expires = Date.parse(headers.expires);
|
|
118
|
+
const date = headers.date ? Date.parse(headers.date) : now;
|
|
119
|
+
if (Number.isFinite(expires) && Number.isFinite(date))
|
|
120
|
+
lifetime = expires - date;
|
|
121
|
+
}
|
|
122
|
+
if (lifetime === undefined)
|
|
123
|
+
return undefined;
|
|
124
|
+
const age = Number(headers.age ?? 0);
|
|
125
|
+
if (Number.isFinite(age) && age > 0)
|
|
126
|
+
lifetime -= age * 1000;
|
|
127
|
+
return lifetime > 0 ? lifetime : undefined;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Whether a finished response may go into the cache. Never one with Set-Cookie, never one asked
|
|
131
|
+
* for with Authorization, and a Vary only on Accept-Encoding (the body is stored decoded) or
|
|
132
|
+
* Origin (kept with the entry).
|
|
133
|
+
*/
|
|
134
|
+
export function storable(method, type, status, request, response) {
|
|
135
|
+
if (method !== "GET" || status !== 200 || !CACHED_TYPES.has(type))
|
|
136
|
+
return false;
|
|
137
|
+
if (request.authorization !== undefined || response["set-cookie"] !== undefined)
|
|
138
|
+
return false;
|
|
139
|
+
const vary = (response.vary ?? "")
|
|
140
|
+
.toLowerCase()
|
|
141
|
+
.split(",")
|
|
142
|
+
.map((v) => v.trim())
|
|
143
|
+
.filter(Boolean);
|
|
144
|
+
return vary.every((v) => v === "accept-encoding" || v === "origin");
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The files of the cache: `<key>.json` (written last, so its presence means the entry is whole)
|
|
148
|
+
* and `<key>.bin`, both mode 600 in a directory of mode 700, written to a temporary name and
|
|
149
|
+
* renamed. Least recently used entries go first when the total passes the cap.
|
|
150
|
+
*/
|
|
151
|
+
export class ResourceCache {
|
|
152
|
+
dir;
|
|
153
|
+
maxBytes;
|
|
154
|
+
now;
|
|
155
|
+
index = new Map();
|
|
156
|
+
total = 0;
|
|
157
|
+
constructor(dir, maxBytes, now = Date.now) {
|
|
158
|
+
this.dir = dir;
|
|
159
|
+
this.maxBytes = maxBytes;
|
|
160
|
+
this.now = now;
|
|
161
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
162
|
+
for (const name of readdirSync(dir)) {
|
|
163
|
+
if (!name.endsWith(".json"))
|
|
164
|
+
continue;
|
|
165
|
+
const key = name.slice(0, -5);
|
|
166
|
+
try {
|
|
167
|
+
const meta = JSON.parse(readFileSync(join(dir, name), "utf8"));
|
|
168
|
+
this.index.set(key, { size: meta.size, used: statSync(join(dir, name)).mtimeMs });
|
|
169
|
+
this.total += meta.size;
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
void this.remove(key);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** Largest single entry: a tenth of the cache, and never more than 20 MB. */
|
|
177
|
+
get maxEntry() {
|
|
178
|
+
return Math.min(20 * 1024 ** 2, Math.floor(this.maxBytes / 10));
|
|
179
|
+
}
|
|
180
|
+
get size() {
|
|
181
|
+
return this.total;
|
|
182
|
+
}
|
|
183
|
+
static key(url) {
|
|
184
|
+
return createHash("sha256").update(url).digest("hex");
|
|
185
|
+
}
|
|
186
|
+
/** A fresh entry for this URL and Origin, or undefined. */
|
|
187
|
+
async get(url, origin) {
|
|
188
|
+
const key = ResourceCache.key(url);
|
|
189
|
+
if (!this.index.has(key))
|
|
190
|
+
return undefined;
|
|
191
|
+
try {
|
|
192
|
+
const meta = JSON.parse(await readFile(join(this.dir, `${key}.json`), "utf8"));
|
|
193
|
+
if (meta.url !== url)
|
|
194
|
+
return undefined;
|
|
195
|
+
if (meta.expiresAt <= this.now()) {
|
|
196
|
+
await this.remove(key);
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
if (meta.origin !== undefined && meta.origin !== (origin ?? null))
|
|
200
|
+
return undefined;
|
|
201
|
+
const body = await readFile(join(this.dir, `${key}.bin`));
|
|
202
|
+
const now = this.now();
|
|
203
|
+
this.index.set(key, { size: meta.size, used: now });
|
|
204
|
+
void utimes(join(this.dir, `${key}.json`), now / 1000, now / 1000).catch(() => { });
|
|
205
|
+
return { meta, body };
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
await this.remove(key);
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/** Stores an entry; false when it is too large or could not be written. */
|
|
213
|
+
async put(meta, body) {
|
|
214
|
+
if (body.length > this.maxEntry)
|
|
215
|
+
return false;
|
|
216
|
+
const key = ResourceCache.key(meta.url);
|
|
217
|
+
const tmp = `.${randomBytes(6).toString("hex")}.tmp`;
|
|
218
|
+
const binTmp = join(this.dir, `${key}.bin${tmp}`);
|
|
219
|
+
const jsonTmp = join(this.dir, `${key}.json${tmp}`);
|
|
220
|
+
try {
|
|
221
|
+
await this.remove(key);
|
|
222
|
+
await writeFile(binTmp, body, { mode: 0o600 });
|
|
223
|
+
await rename(binTmp, join(this.dir, `${key}.bin`));
|
|
224
|
+
await writeFile(jsonTmp, JSON.stringify({ ...meta, size: body.length }), { mode: 0o600 });
|
|
225
|
+
await rename(jsonTmp, join(this.dir, `${key}.json`));
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
await rm(binTmp, { force: true }).catch(() => { });
|
|
229
|
+
await rm(jsonTmp, { force: true }).catch(() => { });
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
this.index.set(key, { size: body.length, used: this.now() });
|
|
233
|
+
this.total += body.length;
|
|
234
|
+
await this.evict();
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
async evict() {
|
|
238
|
+
if (this.total <= this.maxBytes)
|
|
239
|
+
return;
|
|
240
|
+
const oldest = [...this.index].sort((a, b) => a[1].used - b[1].used);
|
|
241
|
+
for (const [key] of oldest) {
|
|
242
|
+
if (this.total <= this.maxBytes)
|
|
243
|
+
break;
|
|
244
|
+
await this.remove(key);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async remove(key) {
|
|
248
|
+
const entry = this.index.get(key);
|
|
249
|
+
if (entry) {
|
|
250
|
+
this.total -= entry.size;
|
|
251
|
+
this.index.delete(key);
|
|
252
|
+
}
|
|
253
|
+
await rm(join(this.dir, `${key}.json`), { force: true }).catch(() => { });
|
|
254
|
+
await rm(join(this.dir, `${key}.bin`), { force: true }).catch(() => { });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function isMainFrameNavigation(request) {
|
|
258
|
+
try {
|
|
259
|
+
return request.isNavigationRequest() && request.frame().parentFrame() === null;
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Routes the context's requests through the policy. Does nothing (and leaves Chrome's HTTP cache
|
|
267
|
+
* on) when the policy blocks nothing and has no cache, or the cache has no directory.
|
|
268
|
+
*/
|
|
269
|
+
export async function installResources(context, policy, cacheDir) {
|
|
270
|
+
const block = policy.block;
|
|
271
|
+
const blocking = !!block && (block.trackers || block.types.length > 0 || block.patterns.length > 0);
|
|
272
|
+
const cache = policy.cacheBytes !== undefined && cacheDir ? new ResourceCache(cacheDir, policy.cacheBytes) : undefined;
|
|
273
|
+
if (!blocking && !cache)
|
|
274
|
+
return undefined;
|
|
275
|
+
const blocked = block ? blocker(block) : undefined;
|
|
276
|
+
const stats = { blocked: 0, hits: 0, stored: 0 };
|
|
277
|
+
const candidates = new WeakSet();
|
|
278
|
+
await context.route("**/*", async (route, request) => {
|
|
279
|
+
let url;
|
|
280
|
+
try {
|
|
281
|
+
url = new URL(request.url());
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return route.fallback();
|
|
285
|
+
}
|
|
286
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
287
|
+
return route.fallback();
|
|
288
|
+
const type = request.resourceType();
|
|
289
|
+
if (blocked) {
|
|
290
|
+
const reason = blocked(url, type, isMainFrameNavigation(request));
|
|
291
|
+
if (reason) {
|
|
292
|
+
stats.blocked++;
|
|
293
|
+
return route.abort("blockedbyclient");
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (cache && request.method() === "GET" && CACHED_TYPES.has(type)) {
|
|
297
|
+
const headers = await request.allHeaders().catch(() => ({}));
|
|
298
|
+
if (headers.authorization === undefined) {
|
|
299
|
+
const hit = await cache.get(request.url(), headers.origin);
|
|
300
|
+
if (hit) {
|
|
301
|
+
stats.hits++;
|
|
302
|
+
const replay = Object.fromEntries(Object.entries(hit.meta.headers).filter(([k]) => !DROPPED_HEADERS.has(k)));
|
|
303
|
+
return route.fulfill({ status: hit.meta.status, headers: replay, body: hit.body });
|
|
304
|
+
}
|
|
305
|
+
candidates.add(request);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return route.fallback();
|
|
309
|
+
});
|
|
310
|
+
if (cache) {
|
|
311
|
+
context.on("requestfinished", (request) => {
|
|
312
|
+
if (!candidates.has(request))
|
|
313
|
+
return;
|
|
314
|
+
void (async () => {
|
|
315
|
+
const response = await request.response();
|
|
316
|
+
if (!response)
|
|
317
|
+
return;
|
|
318
|
+
const [reqHeaders, resHeaders] = await Promise.all([request.allHeaders(), response.allHeaders()]);
|
|
319
|
+
if (!storable(request.method(), request.resourceType(), response.status(), reqHeaders, resHeaders))
|
|
320
|
+
return;
|
|
321
|
+
const lifetime = freshness(resHeaders);
|
|
322
|
+
if (lifetime === undefined)
|
|
323
|
+
return;
|
|
324
|
+
const body = await response.body();
|
|
325
|
+
const varyOrigin = (resHeaders.vary ?? "").toLowerCase().includes("origin");
|
|
326
|
+
const stored = await cache.put({
|
|
327
|
+
url: request.url(),
|
|
328
|
+
status: response.status(),
|
|
329
|
+
headers: resHeaders,
|
|
330
|
+
expiresAt: Date.now() + lifetime,
|
|
331
|
+
...(varyOrigin ? { origin: reqHeaders.origin ?? null } : {}),
|
|
332
|
+
size: body.length,
|
|
333
|
+
}, body);
|
|
334
|
+
if (stored)
|
|
335
|
+
stats.stored++;
|
|
336
|
+
})().catch(() => { });
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
return stats;
|
|
340
|
+
}
|
|
341
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { IdentityError } from "./ids.js";
|
|
2
|
+
/**
|
|
3
|
+
* The runtime lock of an identity: which process drives its Chrome right now.
|
|
4
|
+
* `<state>/browser/identities/<id>/lock.json`, mode 600:
|
|
5
|
+
*
|
|
6
|
+
* { "nodePid": 123, "chromePid": 456, "pgid": 456, "holder": "mcp", "startedAt": "..." }
|
|
7
|
+
*
|
|
8
|
+
* The host takes it just before it launches Chrome for an identity and gives it back when that
|
|
9
|
+
* Chrome closes (idle limit, `browser_close`, the session ending) or the process exits;
|
|
10
|
+
* `turno browser login` holds it while its window is open. Chrome's own `SingletonLock` only
|
|
11
|
+
* says that some process holds the profile; this lock says which one and how to stop it, so a
|
|
12
|
+
* second user of the identity gets that instead of "Browser is already in use".
|
|
13
|
+
*
|
|
14
|
+
* The file is created with O_EXCL, so two processes never both take it. A lock whose `nodePid`
|
|
15
|
+
* is gone is stale and is taken over.
|
|
16
|
+
*/
|
|
17
|
+
export interface RuntimeLock {
|
|
18
|
+
/** The Node process that holds the identity (turno-browser-mcp, or turno for a login). */
|
|
19
|
+
nodePid: number;
|
|
20
|
+
/** The Chrome browser process, once it runs. */
|
|
21
|
+
chromePid?: number;
|
|
22
|
+
/** Chrome's process group (Playwright makes Chrome its leader). */
|
|
23
|
+
pgid?: number;
|
|
24
|
+
/** `export` and `import`: `turno browser export` and `import`, for a few seconds. */
|
|
25
|
+
holder: "mcp" | "login" | "export" | "import";
|
|
26
|
+
startedAt: string;
|
|
27
|
+
}
|
|
28
|
+
export interface HeldLock {
|
|
29
|
+
readonly id: string;
|
|
30
|
+
/** Adds what is known once Chrome runs. No-op when the lock is no longer this process's. */
|
|
31
|
+
update(fields: {
|
|
32
|
+
chromePid?: number;
|
|
33
|
+
pgid?: number;
|
|
34
|
+
}): void;
|
|
35
|
+
/** Removes the lock if it is still this process's. Safe to call twice. */
|
|
36
|
+
release(): void;
|
|
37
|
+
}
|
|
38
|
+
export declare class IdentityInUseError extends IdentityError {
|
|
39
|
+
readonly lock: RuntimeLock;
|
|
40
|
+
constructor(message: string, lock: RuntimeLock);
|
|
41
|
+
}
|
|
42
|
+
export declare function runtimeLockPath(id: string, env?: NodeJS.ProcessEnv): string;
|
|
43
|
+
/** The lock file's content, undefined when there is none or it is not a lock. */
|
|
44
|
+
export declare function readRuntimeLock(id: string, env?: NodeJS.ProcessEnv): RuntimeLock | undefined;
|
|
45
|
+
/** True while the lock's Node process lives. */
|
|
46
|
+
export declare function runtimeLockAlive(lock: RuntimeLock): boolean;
|
|
47
|
+
/** Who holds the identity and how to get it back, for errors and `turno browser doctor`. */
|
|
48
|
+
export declare function inUseMessage(id: string, lock: RuntimeLock): string;
|
|
49
|
+
export interface AcquireOptions {
|
|
50
|
+
holder: RuntimeLock["holder"];
|
|
51
|
+
/** The process that holds it (default: this one). Tests pass another. */
|
|
52
|
+
nodePid?: number;
|
|
53
|
+
now?: Date;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Takes the identity's runtime lock, or throws `IdentityInUseError` naming the live holder. The
|
|
57
|
+
* same process may take it again (a relaunch after the idle close).
|
|
58
|
+
*/
|
|
59
|
+
export declare function acquireRuntimeLock(id: string, env: NodeJS.ProcessEnv | undefined, opts: AcquireOptions): HeldLock;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { pidAlive } from "turno-core/process";
|
|
4
|
+
import { IdentityError, identityStateDir } from "./ids.js";
|
|
5
|
+
export class IdentityInUseError extends IdentityError {
|
|
6
|
+
lock;
|
|
7
|
+
constructor(message, lock) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.lock = lock;
|
|
10
|
+
this.name = "IdentityInUseError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function runtimeLockPath(id, env = process.env) {
|
|
14
|
+
return join(identityStateDir(id, env), "lock.json");
|
|
15
|
+
}
|
|
16
|
+
function isLock(v) {
|
|
17
|
+
const l = v;
|
|
18
|
+
const pid = (n) => typeof n === "number" && Number.isSafeInteger(n) && n > 0;
|
|
19
|
+
return (typeof l === "object" &&
|
|
20
|
+
l !== null &&
|
|
21
|
+
pid(l.nodePid) &&
|
|
22
|
+
(l.chromePid === undefined || pid(l.chromePid)) &&
|
|
23
|
+
(l.pgid === undefined || pid(l.pgid)) &&
|
|
24
|
+
(l.holder === "mcp" || l.holder === "login" || l.holder === "export" || l.holder === "import") &&
|
|
25
|
+
typeof l.startedAt === "string");
|
|
26
|
+
}
|
|
27
|
+
/** The lock file's content, undefined when there is none or it is not a lock. */
|
|
28
|
+
export function readRuntimeLock(id, env = process.env) {
|
|
29
|
+
try {
|
|
30
|
+
const v = JSON.parse(readFileSync(runtimeLockPath(id, env), "utf8"));
|
|
31
|
+
return isLock(v) ? v : undefined;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** True while the lock's Node process lives. */
|
|
38
|
+
export function runtimeLockAlive(lock) {
|
|
39
|
+
return pidAlive(lock.nodePid);
|
|
40
|
+
}
|
|
41
|
+
/** Who holds the identity and how to get it back, for errors and `turno browser doctor`. */
|
|
42
|
+
export function inUseMessage(id, lock) {
|
|
43
|
+
const pids = `pid ${lock.nodePid}${lock.chromePid ? `, Chrome pid ${lock.chromePid}` : ""}`;
|
|
44
|
+
if (lock.holder === "login") {
|
|
45
|
+
return `browser identity "${id}" is in use by turno browser login (${pids}) since ${lock.startedAt}. Close that Chrome window to finish the login, then try again.`;
|
|
46
|
+
}
|
|
47
|
+
if (lock.holder === "export" || lock.holder === "import") {
|
|
48
|
+
return `browser identity "${id}" is in use by turno browser ${lock.holder} (${pids}) since ${lock.startedAt}. Wait for it to finish, then try again.`;
|
|
49
|
+
}
|
|
50
|
+
return (`browser identity "${id}" is in use by turno-browser-mcp (${pids}) since ${lock.startedAt}. ` +
|
|
51
|
+
"One identity drives one Chrome at a time: wait for that session to end (mcpd stops the server after its lease or idle time), " +
|
|
52
|
+
`or stop it now with \`kill ${lock.nodePid}\`, which closes its Chrome first.`);
|
|
53
|
+
}
|
|
54
|
+
function writeAtomic(file, lock) {
|
|
55
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
56
|
+
writeFileSync(tmp, `${JSON.stringify(lock)}\n`, { mode: 0o600 });
|
|
57
|
+
renameSync(tmp, file);
|
|
58
|
+
}
|
|
59
|
+
/** Locks this process holds, released on exit (sync: an exit handler cannot wait). */
|
|
60
|
+
const held = new Map();
|
|
61
|
+
/** The newest handle per lock file, so an older handle of a re-taken lock never removes it. */
|
|
62
|
+
const newest = new Map();
|
|
63
|
+
let exitHook = false;
|
|
64
|
+
/**
|
|
65
|
+
* Takes the identity's runtime lock, or throws `IdentityInUseError` naming the live holder. The
|
|
66
|
+
* same process may take it again (a relaunch after the idle close).
|
|
67
|
+
*/
|
|
68
|
+
export function acquireRuntimeLock(id, env = process.env, opts) {
|
|
69
|
+
const file = runtimeLockPath(id, env);
|
|
70
|
+
mkdirSync(identityStateDir(id, env), { recursive: true, mode: 0o700 });
|
|
71
|
+
const nodePid = opts.nodePid ?? process.pid;
|
|
72
|
+
let lock = { nodePid, holder: opts.holder, startedAt: (opts.now ?? new Date()).toISOString() };
|
|
73
|
+
for (let attempt = 0;; attempt++) {
|
|
74
|
+
try {
|
|
75
|
+
writeFileSync(file, `${JSON.stringify(lock)}\n`, { mode: 0o600, flag: "wx" });
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
if (err.code !== "EEXIST" || attempt >= 3)
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
const current = readRuntimeLock(id, env);
|
|
83
|
+
if (current && current.nodePid !== nodePid && runtimeLockAlive(current))
|
|
84
|
+
throw new IdentityInUseError(inUseMessage(id, current), current);
|
|
85
|
+
// Stale, unreadable, or ours already: replace it.
|
|
86
|
+
rmSync(file, { force: true });
|
|
87
|
+
}
|
|
88
|
+
const token = Symbol(file);
|
|
89
|
+
newest.set(file, token);
|
|
90
|
+
const mine = () => newest.get(file) === token && readRuntimeLock(id, env)?.nodePid === nodePid;
|
|
91
|
+
let released = false;
|
|
92
|
+
const release = () => {
|
|
93
|
+
if (released)
|
|
94
|
+
return;
|
|
95
|
+
released = true;
|
|
96
|
+
if (held.get(file) === release)
|
|
97
|
+
held.delete(file);
|
|
98
|
+
if (mine())
|
|
99
|
+
rmSync(file, { force: true });
|
|
100
|
+
if (newest.get(file) === token)
|
|
101
|
+
newest.delete(file);
|
|
102
|
+
};
|
|
103
|
+
if (nodePid === process.pid) {
|
|
104
|
+
held.set(file, release);
|
|
105
|
+
if (!exitHook) {
|
|
106
|
+
exitHook = true;
|
|
107
|
+
process.on("exit", () => {
|
|
108
|
+
for (const r of [...held.values()])
|
|
109
|
+
r();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
id,
|
|
115
|
+
update(fields) {
|
|
116
|
+
if (released || !mine())
|
|
117
|
+
return;
|
|
118
|
+
lock = { ...lock, ...fields };
|
|
119
|
+
// Key order as documented: who, then what it runs, then since when.
|
|
120
|
+
writeAtomic(file, {
|
|
121
|
+
nodePid: lock.nodePid,
|
|
122
|
+
...(lock.chromePid !== undefined ? { chromePid: lock.chromePid } : {}),
|
|
123
|
+
...(lock.pgid !== undefined ? { pgid: lock.pgid } : {}),
|
|
124
|
+
holder: lock.holder,
|
|
125
|
+
startedAt: lock.startedAt,
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
release,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=runtime-lock.js.map
|
package/dist/sealed.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const SEALED_FORMAT = "turno-browser-session";
|
|
2
|
+
export declare class SealError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
/** Encrypt `payload` (any JSON) under `passphrase`; the result is one JSON document. */
|
|
6
|
+
export declare function seal(payload: unknown, passphrase: string): Promise<string>;
|
|
7
|
+
/** Decrypt an export; a wrong passphrase and a changed file give the same error. */
|
|
8
|
+
export declare function unseal(text: string, passphrase: string): Promise<unknown>;
|