|
f7eb47b…
|
lmata
|
1 |
"use strict"; |
|
f7eb47b…
|
lmata
|
2 |
var __create = Object.create; |
|
f7eb47b…
|
lmata
|
3 |
var __defProp = Object.defineProperty; |
|
f7eb47b…
|
lmata
|
4 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
|
f7eb47b…
|
lmata
|
5 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
|
f7eb47b…
|
lmata
|
6 |
var __getProtoOf = Object.getPrototypeOf; |
|
f7eb47b…
|
lmata
|
7 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
|
f7eb47b…
|
lmata
|
8 |
var __export = (target, all) => { |
|
f7eb47b…
|
lmata
|
9 |
for (var name in all) |
|
f7eb47b…
|
lmata
|
10 |
__defProp(target, name, { get: all[name], enumerable: true }); |
|
f7eb47b…
|
lmata
|
11 |
}; |
|
f7eb47b…
|
lmata
|
12 |
var __copyProps = (to, from, except, desc) => { |
|
f7eb47b…
|
lmata
|
13 |
if (from && typeof from === "object" || typeof from === "function") { |
|
f7eb47b…
|
lmata
|
14 |
for (let key of __getOwnPropNames(from)) |
|
f7eb47b…
|
lmata
|
15 |
if (!__hasOwnProp.call(to, key) && key !== except) |
|
f7eb47b…
|
lmata
|
16 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
|
f7eb47b…
|
lmata
|
17 |
} |
|
f7eb47b…
|
lmata
|
18 |
return to; |
|
f7eb47b…
|
lmata
|
19 |
}; |
|
f7eb47b…
|
lmata
|
20 |
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
|
f7eb47b…
|
lmata
|
21 |
// If the importer is in node compatibility mode or this is not an ESM |
|
f7eb47b…
|
lmata
|
22 |
// file that has been converted to a CommonJS file using a Babel- |
|
f7eb47b…
|
lmata
|
23 |
// compatible transform (i.e. "__esModule" has not been set), then set |
|
f7eb47b…
|
lmata
|
24 |
// "default" to the CommonJS "module.exports" for node compatibility. |
|
f7eb47b…
|
lmata
|
25 |
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
|
f7eb47b…
|
lmata
|
26 |
mod |
|
f7eb47b…
|
lmata
|
27 |
)); |
|
f7eb47b…
|
lmata
|
28 |
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
|
f7eb47b…
|
lmata
|
29 |
var context_exports = {}; |
|
f7eb47b…
|
lmata
|
30 |
__export(context_exports, { |
|
f7eb47b…
|
lmata
|
31 |
Context: () => Context |
|
f7eb47b…
|
lmata
|
32 |
}); |
|
f7eb47b…
|
lmata
|
33 |
module.exports = __toCommonJS(context_exports); |
|
f7eb47b…
|
lmata
|
34 |
var import_utilsBundle = require("playwright-core/lib/utilsBundle"); |
|
f7eb47b…
|
lmata
|
35 |
var import_utils = require("playwright-core/lib/utils"); |
|
f7eb47b…
|
lmata
|
36 |
var import_playwright_core = require("playwright-core"); |
|
f7eb47b…
|
lmata
|
37 |
var import_url = require("url"); |
|
f7eb47b…
|
lmata
|
38 |
var import_os = __toESM(require("os")); |
|
f7eb47b…
|
lmata
|
39 |
var import_log = require("../log"); |
|
f7eb47b…
|
lmata
|
40 |
var import_tab = require("./tab"); |
|
f7eb47b…
|
lmata
|
41 |
var import_config = require("./config"); |
|
f7eb47b…
|
lmata
|
42 |
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test"); |
|
f7eb47b…
|
lmata
|
43 |
class Context { |
|
f7eb47b…
|
lmata
|
44 |
constructor(options) { |
|
f7eb47b…
|
lmata
|
45 |
this._tabs = []; |
|
f7eb47b…
|
lmata
|
46 |
this._abortController = new AbortController(); |
|
f7eb47b…
|
lmata
|
47 |
this.config = options.config; |
|
f7eb47b…
|
lmata
|
48 |
this.sessionLog = options.sessionLog; |
|
f7eb47b…
|
lmata
|
49 |
this.options = options; |
|
f7eb47b…
|
lmata
|
50 |
this._browserContextFactory = options.browserContextFactory; |
|
f7eb47b…
|
lmata
|
51 |
this._clientInfo = options.clientInfo; |
|
f7eb47b…
|
lmata
|
52 |
testDebug("create context"); |
|
f7eb47b…
|
lmata
|
53 |
Context._allContexts.add(this); |
|
f7eb47b…
|
lmata
|
54 |
} |
|
f7eb47b…
|
lmata
|
55 |
static { |
|
f7eb47b…
|
lmata
|
56 |
this._allContexts = /* @__PURE__ */ new Set(); |
|
f7eb47b…
|
lmata
|
57 |
} |
|
f7eb47b…
|
lmata
|
58 |
static async disposeAll() { |
|
f7eb47b…
|
lmata
|
59 |
await Promise.all([...Context._allContexts].map((context) => context.dispose())); |
|
f7eb47b…
|
lmata
|
60 |
} |
|
f7eb47b…
|
lmata
|
61 |
tabs() { |
|
f7eb47b…
|
lmata
|
62 |
return this._tabs; |
|
f7eb47b…
|
lmata
|
63 |
} |
|
f7eb47b…
|
lmata
|
64 |
currentTab() { |
|
f7eb47b…
|
lmata
|
65 |
return this._currentTab; |
|
f7eb47b…
|
lmata
|
66 |
} |
|
f7eb47b…
|
lmata
|
67 |
currentTabOrDie() { |
|
f7eb47b…
|
lmata
|
68 |
if (!this._currentTab) |
|
f7eb47b…
|
lmata
|
69 |
throw new Error("No open pages available."); |
|
f7eb47b…
|
lmata
|
70 |
return this._currentTab; |
|
f7eb47b…
|
lmata
|
71 |
} |
|
f7eb47b…
|
lmata
|
72 |
async newTab() { |
|
f7eb47b…
|
lmata
|
73 |
const { browserContext } = await this._ensureBrowserContext({}); |
|
f7eb47b…
|
lmata
|
74 |
const page = await browserContext.newPage(); |
|
f7eb47b…
|
lmata
|
75 |
this._currentTab = this._tabs.find((t) => t.page === page); |
|
f7eb47b…
|
lmata
|
76 |
return this._currentTab; |
|
f7eb47b…
|
lmata
|
77 |
} |
|
f7eb47b…
|
lmata
|
78 |
async selectTab(index) { |
|
f7eb47b…
|
lmata
|
79 |
const tab = this._tabs[index]; |
|
f7eb47b…
|
lmata
|
80 |
if (!tab) |
|
f7eb47b…
|
lmata
|
81 |
throw new Error(`Tab ${index} not found`); |
|
f7eb47b…
|
lmata
|
82 |
await tab.page.bringToFront(); |
|
f7eb47b…
|
lmata
|
83 |
this._currentTab = tab; |
|
f7eb47b…
|
lmata
|
84 |
return tab; |
|
f7eb47b…
|
lmata
|
85 |
} |
|
f7eb47b…
|
lmata
|
86 |
async ensureTab(options = {}) { |
|
f7eb47b…
|
lmata
|
87 |
const { browserContext } = await this._ensureBrowserContext(options); |
|
f7eb47b…
|
lmata
|
88 |
if (!this._currentTab) |
|
f7eb47b…
|
lmata
|
89 |
await browserContext.newPage(); |
|
f7eb47b…
|
lmata
|
90 |
return this._currentTab; |
|
f7eb47b…
|
lmata
|
91 |
} |
|
f7eb47b…
|
lmata
|
92 |
async closeTab(index) { |
|
f7eb47b…
|
lmata
|
93 |
const tab = index === void 0 ? this._currentTab : this._tabs[index]; |
|
f7eb47b…
|
lmata
|
94 |
if (!tab) |
|
f7eb47b…
|
lmata
|
95 |
throw new Error(`Tab ${index} not found`); |
|
f7eb47b…
|
lmata
|
96 |
const url = tab.page.url(); |
|
f7eb47b…
|
lmata
|
97 |
await tab.page.close(); |
|
f7eb47b…
|
lmata
|
98 |
return url; |
|
f7eb47b…
|
lmata
|
99 |
} |
|
f7eb47b…
|
lmata
|
100 |
async outputFile(fileName, options) { |
|
f7eb47b…
|
lmata
|
101 |
return (0, import_config.outputFile)(this.config, this._clientInfo, fileName, options); |
|
f7eb47b…
|
lmata
|
102 |
} |
|
f7eb47b…
|
lmata
|
103 |
_onPageCreated(page) { |
|
f7eb47b…
|
lmata
|
104 |
const tab = new import_tab.Tab(this, page, (tab2) => this._onPageClosed(tab2)); |
|
f7eb47b…
|
lmata
|
105 |
this._tabs.push(tab); |
|
f7eb47b…
|
lmata
|
106 |
if (!this._currentTab) |
|
f7eb47b…
|
lmata
|
107 |
this._currentTab = tab; |
|
f7eb47b…
|
lmata
|
108 |
} |
|
f7eb47b…
|
lmata
|
109 |
_onPageClosed(tab) { |
|
f7eb47b…
|
lmata
|
110 |
const index = this._tabs.indexOf(tab); |
|
f7eb47b…
|
lmata
|
111 |
if (index === -1) |
|
f7eb47b…
|
lmata
|
112 |
return; |
|
f7eb47b…
|
lmata
|
113 |
this._tabs.splice(index, 1); |
|
f7eb47b…
|
lmata
|
114 |
if (this._currentTab === tab) |
|
f7eb47b…
|
lmata
|
115 |
this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)]; |
|
f7eb47b…
|
lmata
|
116 |
if (!this._tabs.length) |
|
f7eb47b…
|
lmata
|
117 |
void this.closeBrowserContext(); |
|
f7eb47b…
|
lmata
|
118 |
} |
|
f7eb47b…
|
lmata
|
119 |
async closeBrowserContext() { |
|
f7eb47b…
|
lmata
|
120 |
if (!this._closeBrowserContextPromise) |
|
f7eb47b…
|
lmata
|
121 |
this._closeBrowserContextPromise = this._closeBrowserContextImpl().catch(import_log.logUnhandledError); |
|
f7eb47b…
|
lmata
|
122 |
await this._closeBrowserContextPromise; |
|
f7eb47b…
|
lmata
|
123 |
this._closeBrowserContextPromise = void 0; |
|
f7eb47b…
|
lmata
|
124 |
} |
|
f7eb47b…
|
lmata
|
125 |
isRunningTool() { |
|
f7eb47b…
|
lmata
|
126 |
return this._runningToolName !== void 0; |
|
f7eb47b…
|
lmata
|
127 |
} |
|
f7eb47b…
|
lmata
|
128 |
setRunningTool(name) { |
|
f7eb47b…
|
lmata
|
129 |
this._runningToolName = name; |
|
f7eb47b…
|
lmata
|
130 |
} |
|
f7eb47b…
|
lmata
|
131 |
async _closeBrowserContextImpl() { |
|
f7eb47b…
|
lmata
|
132 |
if (!this._browserContextPromise) |
|
f7eb47b…
|
lmata
|
133 |
return; |
|
f7eb47b…
|
lmata
|
134 |
testDebug("close context"); |
|
f7eb47b…
|
lmata
|
135 |
const promise = this._browserContextPromise; |
|
f7eb47b…
|
lmata
|
136 |
this._browserContextPromise = void 0; |
|
f7eb47b…
|
lmata
|
137 |
this._browserContextOption = void 0; |
|
f7eb47b…
|
lmata
|
138 |
await promise.then(async ({ browserContext, close }) => { |
|
f7eb47b…
|
lmata
|
139 |
if (this.config.saveTrace) |
|
f7eb47b…
|
lmata
|
140 |
await browserContext.tracing.stop(); |
|
f7eb47b…
|
lmata
|
141 |
await close(); |
|
f7eb47b…
|
lmata
|
142 |
}); |
|
f7eb47b…
|
lmata
|
143 |
} |
|
f7eb47b…
|
lmata
|
144 |
async dispose() { |
|
f7eb47b…
|
lmata
|
145 |
this._abortController.abort("MCP context disposed"); |
|
f7eb47b…
|
lmata
|
146 |
await this.closeBrowserContext(); |
|
f7eb47b…
|
lmata
|
147 |
Context._allContexts.delete(this); |
|
f7eb47b…
|
lmata
|
148 |
} |
|
f7eb47b…
|
lmata
|
149 |
async _setupRequestInterception(context) { |
|
f7eb47b…
|
lmata
|
150 |
if (this.config.network?.allowedOrigins?.length) { |
|
f7eb47b…
|
lmata
|
151 |
await context.route("**", (route) => route.abort("blockedbyclient")); |
|
f7eb47b…
|
lmata
|
152 |
for (const origin of this.config.network.allowedOrigins) |
|
f7eb47b…
|
lmata
|
153 |
await context.route(originOrHostGlob(origin), (route) => route.continue()); |
|
f7eb47b…
|
lmata
|
154 |
} |
|
f7eb47b…
|
lmata
|
155 |
if (this.config.network?.blockedOrigins?.length) { |
|
f7eb47b…
|
lmata
|
156 |
for (const origin of this.config.network.blockedOrigins) |
|
f7eb47b…
|
lmata
|
157 |
await context.route(originOrHostGlob(origin), (route) => route.abort("blockedbyclient")); |
|
f7eb47b…
|
lmata
|
158 |
} |
|
f7eb47b…
|
lmata
|
159 |
} |
|
f7eb47b…
|
lmata
|
160 |
async ensureBrowserContext(options = {}) { |
|
f7eb47b…
|
lmata
|
161 |
const { browserContext } = await this._ensureBrowserContext(options); |
|
f7eb47b…
|
lmata
|
162 |
return browserContext; |
|
f7eb47b…
|
lmata
|
163 |
} |
|
f7eb47b…
|
lmata
|
164 |
_ensureBrowserContext(options) { |
|
f7eb47b…
|
lmata
|
165 |
if (this._browserContextPromise && (options.forceHeadless === void 0 || this._browserContextOption?.forceHeadless === options.forceHeadless)) |
|
f7eb47b…
|
lmata
|
166 |
return this._browserContextPromise; |
|
f7eb47b…
|
lmata
|
167 |
const closePrework = this._browserContextPromise ? this.closeBrowserContext() : Promise.resolve(); |
|
f7eb47b…
|
lmata
|
168 |
this._browserContextPromise = closePrework.then(() => this._setupBrowserContext(options)); |
|
f7eb47b…
|
lmata
|
169 |
this._browserContextPromise.catch(() => { |
|
f7eb47b…
|
lmata
|
170 |
this._browserContextPromise = void 0; |
|
f7eb47b…
|
lmata
|
171 |
this._browserContextOption = void 0; |
|
f7eb47b…
|
lmata
|
172 |
}); |
|
f7eb47b…
|
lmata
|
173 |
this._browserContextOption = options; |
|
f7eb47b…
|
lmata
|
174 |
return this._browserContextPromise; |
|
f7eb47b…
|
lmata
|
175 |
} |
|
f7eb47b…
|
lmata
|
176 |
async _setupBrowserContext(options) { |
|
f7eb47b…
|
lmata
|
177 |
if (this._closeBrowserContextPromise) |
|
f7eb47b…
|
lmata
|
178 |
throw new Error("Another browser context is being closed."); |
|
f7eb47b…
|
lmata
|
179 |
if (this.config.testIdAttribute) |
|
f7eb47b…
|
lmata
|
180 |
import_playwright_core.selectors.setTestIdAttribute(this.config.testIdAttribute); |
|
f7eb47b…
|
lmata
|
181 |
const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal, { toolName: this._runningToolName, ...options }); |
|
f7eb47b…
|
lmata
|
182 |
const { browserContext } = result; |
|
f7eb47b…
|
lmata
|
183 |
if (!this.config.allowUnrestrictedFileAccess) { |
|
f7eb47b…
|
lmata
|
184 |
browserContext._setAllowedProtocols(["http:", "https:", "about:", "data:"]); |
|
f7eb47b…
|
lmata
|
185 |
browserContext._setAllowedDirectories(allRootPaths(this._clientInfo)); |
|
f7eb47b…
|
lmata
|
186 |
} |
|
f7eb47b…
|
lmata
|
187 |
await this._setupRequestInterception(browserContext); |
|
f7eb47b…
|
lmata
|
188 |
for (const page of browserContext.pages()) |
|
f7eb47b…
|
lmata
|
189 |
this._onPageCreated(page); |
|
f7eb47b…
|
lmata
|
190 |
browserContext.on("page", (page) => this._onPageCreated(page)); |
|
f7eb47b…
|
lmata
|
191 |
if (this.config.saveTrace) { |
|
f7eb47b…
|
lmata
|
192 |
await browserContext.tracing.start({ |
|
f7eb47b…
|
lmata
|
193 |
name: "trace-" + Date.now(), |
|
f7eb47b…
|
lmata
|
194 |
screenshots: true, |
|
f7eb47b…
|
lmata
|
195 |
snapshots: true, |
|
f7eb47b…
|
lmata
|
196 |
_live: true |
|
f7eb47b…
|
lmata
|
197 |
}); |
|
f7eb47b…
|
lmata
|
198 |
} |
|
f7eb47b…
|
lmata
|
199 |
return result; |
|
f7eb47b…
|
lmata
|
200 |
} |
|
f7eb47b…
|
lmata
|
201 |
lookupSecret(secretName) { |
|
f7eb47b…
|
lmata
|
202 |
if (!this.config.secrets?.[secretName]) |
|
f7eb47b…
|
lmata
|
203 |
return { value: secretName, code: (0, import_utils.escapeWithQuotes)(secretName, "'") }; |
|
f7eb47b…
|
lmata
|
204 |
return { |
|
f7eb47b…
|
lmata
|
205 |
value: this.config.secrets[secretName], |
|
f7eb47b…
|
lmata
|
206 |
code: `process.env['${secretName}']` |
|
f7eb47b…
|
lmata
|
207 |
}; |
|
f7eb47b…
|
lmata
|
208 |
} |
|
f7eb47b…
|
lmata
|
209 |
firstRootPath() { |
|
f7eb47b…
|
lmata
|
210 |
return allRootPaths(this._clientInfo)[0]; |
|
f7eb47b…
|
lmata
|
211 |
} |
|
f7eb47b…
|
lmata
|
212 |
} |
|
f7eb47b…
|
lmata
|
213 |
function allRootPaths(clientInfo) { |
|
f7eb47b…
|
lmata
|
214 |
const paths = []; |
|
f7eb47b…
|
lmata
|
215 |
for (const root of clientInfo.roots) { |
|
f7eb47b…
|
lmata
|
216 |
const url = new URL(root.uri); |
|
f7eb47b…
|
lmata
|
217 |
let rootPath; |
|
f7eb47b…
|
lmata
|
218 |
try { |
|
f7eb47b…
|
lmata
|
219 |
rootPath = (0, import_url.fileURLToPath)(url); |
|
f7eb47b…
|
lmata
|
220 |
} catch (e) { |
|
f7eb47b…
|
lmata
|
221 |
if (e.code === "ERR_INVALID_FILE_URL_PATH" && import_os.default.platform() === "win32") |
|
f7eb47b…
|
lmata
|
222 |
rootPath = decodeURIComponent(url.pathname); |
|
f7eb47b…
|
lmata
|
223 |
} |
|
f7eb47b…
|
lmata
|
224 |
if (!rootPath) |
|
f7eb47b…
|
lmata
|
225 |
continue; |
|
f7eb47b…
|
lmata
|
226 |
paths.push(rootPath); |
|
f7eb47b…
|
lmata
|
227 |
} |
|
f7eb47b…
|
lmata
|
228 |
if (paths.length === 0) |
|
f7eb47b…
|
lmata
|
229 |
paths.push(process.cwd()); |
|
f7eb47b…
|
lmata
|
230 |
return paths; |
|
f7eb47b…
|
lmata
|
231 |
} |
|
f7eb47b…
|
lmata
|
232 |
function originOrHostGlob(originOrHost) { |
|
f7eb47b…
|
lmata
|
233 |
try { |
|
f7eb47b…
|
lmata
|
234 |
const url = new URL(originOrHost); |
|
f7eb47b…
|
lmata
|
235 |
if (url.origin !== "null") |
|
f7eb47b…
|
lmata
|
236 |
return `${url.origin}/**`; |
|
f7eb47b…
|
lmata
|
237 |
} catch { |
|
f7eb47b…
|
lmata
|
238 |
} |
|
f7eb47b…
|
lmata
|
239 |
return `*://${originOrHost}/**`; |
|
f7eb47b…
|
lmata
|
240 |
} |
|
f7eb47b…
|
lmata
|
241 |
// Annotate the CommonJS export names for ESM import in node: |
|
f7eb47b…
|
lmata
|
242 |
0 && (module.exports = { |
|
f7eb47b…
|
lmata
|
243 |
Context |
|
f7eb47b…
|
lmata
|
244 |
}); |