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