Skip to content

Commit 71b1d0d

Browse files
committed
feat: try simplify headers setting
1 parent 795d63c commit 71b1d0d

2 files changed

Lines changed: 20 additions & 40 deletions

File tree

src/McpContext.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export class McpContext implements Context {
134134
this.#options = options;
135135
this.#headers = options.headers;
136136

137-
this.#networkCollector = new NetworkCollector(this.browser);
137+
this.#networkCollector = new NetworkCollector(this.browser, undefined, {
138+
headers: this.#headers,
139+
});
138140

139141
this.#consoleCollector = new ConsoleCollector(this.browser, collect => {
140142
return {
@@ -159,42 +161,9 @@ export class McpContext implements Context {
159161

160162
async #init() {
161163
const pages = await this.createPagesSnapshot();
162-
await this.#networkCollector.init(pages, this.#headers);
164+
await this.#networkCollector.init(pages);
163165
await this.#consoleCollector.init(pages);
164166
}
165-
166-
/**
167-
* Gets the custom headers to add to all network requests.
168-
*/
169-
getHeaders(): Record<string, string> | undefined {
170-
return this.#headers;
171-
}
172-
173-
/**
174-
* Sets custom headers to add to all network requests.
175-
*/
176-
setHeaders(headers: Record<string, string> | undefined): void {
177-
this.#headers = headers;
178-
// Update all existing pages with the new headers
179-
for (const page of this.#pages) {
180-
this.#applyHeadersToPage(page, headers);
181-
}
182-
}
183-
184-
/**
185-
* Applies custom headers to a specific page.
186-
*/
187-
async #applyHeadersToPage(page: Page, headers: Record<string, string> | undefined): Promise<void> {
188-
try {
189-
if (headers) {
190-
await page.setExtraHTTPHeaders(headers);
191-
} else {
192-
await page.setExtraHTTPHeaders({});
193-
}
194-
} catch (error) {
195-
this.logger('Error applying headers to page:', error);
196-
}
197-
}
198167

199168
dispose() {
200169
this.#networkCollector.dispose();
@@ -714,6 +683,8 @@ export class McpContext implements Context {
714683
collect(req);
715684
},
716685
} as ListenerMap;
686+
}, {
687+
headers: this.#headers,
717688
});
718689
await this.#networkCollector.init(await this.browser.pages());
719690
}

src/PageCollector.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,24 @@ export class NetworkCollector extends PageCollector<HTTPRequest> {
355355
browser: Browser,
356356
listeners: (
357357
collector: (item: HTTPRequest) => void,
358-
) => ListenerMap<PageEvents> = collect => ({
359-
request: req => collect(req),
360-
} as ListenerMap),
358+
) => ListenerMap<PageEvents> = collect => {
359+
return {
360+
request: req => {
361+
collect(req);
362+
},
363+
} as ListenerMap;
364+
},
365+
options?: Record<string, unknown> & {
366+
headers?: Record<string, string>
367+
}
361368
) {
362369
super(browser, listeners);
370+
if (options?.headers) {
371+
this.#headers = options?.headers;
372+
}
363373
}
364374

365-
override async init(pages: Page[], headers?: Record<string, string>): Promise<void> {
366-
this.#headers = headers;
375+
override async init(pages: Page[]): Promise<void> {
367376
for (const page of pages) {
368377
await this.#applyHeadersToPage(page);
369378
}

0 commit comments

Comments
 (0)