Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to send headers via ws connection to browser in node.js environment #9314

Merged
merged 1 commit into from Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/api/puppeteer.connectoptions.headers.md
@@ -0,0 +1,19 @@
---
sidebar_label: ConnectOptions.headers
---

# ConnectOptions.headers property

Headers to use for the web socket connection.

#### Signature:

```typescript
interface ConnectOptions {
headers?: Record<string, string>;
}
```
OrKoN marked this conversation as resolved.
Show resolved Hide resolved

## Remarks

Only works in the Node.js environment.
11 changes: 6 additions & 5 deletions docs/api/puppeteer.connectoptions.md
Expand Up @@ -14,8 +14,9 @@ export interface ConnectOptions extends BrowserConnectOptions

## Properties

| Property | Modifiers | Type | Description | Default |
| --------------------------------------------------------------------- | --------- | --------------------------------------------------------- | ----------------- | ------- |
| [browserURL?](./puppeteer.connectoptions.browserurl.md) | | string | <i>(Optional)</i> | |
| [browserWSEndpoint?](./puppeteer.connectoptions.browserwsendpoint.md) | | string | <i>(Optional)</i> | |
| [transport?](./puppeteer.connectoptions.transport.md) | | [ConnectionTransport](./puppeteer.connectiontransport.md) | <i>(Optional)</i> | |
| Property | Modifiers | Type | Description | Default |
| --------------------------------------------------------------------- | --------- | --------------------------------------------------------- | --------------------------------------------------------------- | ------- |
| [browserURL?](./puppeteer.connectoptions.browserurl.md) | | string | <i>(Optional)</i> | |
| [browserWSEndpoint?](./puppeteer.connectoptions.browserwsendpoint.md) | | string | <i>(Optional)</i> | |
| [headers?](./puppeteer.connectoptions.headers.md) | | Record&lt;string, string&gt; | <i>(Optional)</i> Headers to use for the web socket connection. | |
| [transport?](./puppeteer.connectoptions.transport.md) | | [ConnectionTransport](./puppeteer.connectiontransport.md) | <i>(Optional)</i> | |
11 changes: 5 additions & 6 deletions packages/puppeteer-core/src/common/BrowserConnector.ts
Expand Up @@ -24,6 +24,8 @@ import {Connection} from './Connection.js';
import {ConnectionTransport} from './ConnectionTransport.js';
import {getFetch} from './fetch.js';
import {Viewport} from './PuppeteerViewport.js';

import type {ConnectOptions} from './Puppeteer.js';
/**
* Generic browser options that can be passed when launching any browser or when
* connecting to an existing browser instance.
Expand Down Expand Up @@ -73,18 +75,15 @@ const getWebSocketTransportClass = async () => {
* @internal
*/
export async function _connectToCDPBrowser(
options: BrowserConnectOptions & {
browserWSEndpoint?: string;
browserURL?: string;
transport?: ConnectionTransport;
}
options: BrowserConnectOptions & ConnectOptions
): Promise<CDPBrowser> {
const {
browserWSEndpoint,
browserURL,
ignoreHTTPSErrors = false,
defaultViewport = {width: 800, height: 600},
transport,
headers = {},
slowMo = 0,
targetFilter,
_isPageTarget: isPageTarget,
Expand All @@ -102,7 +101,7 @@ export async function _connectToCDPBrowser(
} else if (browserWSEndpoint) {
const WebSocketClass = await getWebSocketTransportClass();
const connectionTransport: ConnectionTransport =
await WebSocketClass.create(browserWSEndpoint);
await WebSocketClass.create(browserWSEndpoint, headers);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how difficult it is to provide a custom transport instead (already supported feature)? I guess there could be potentially many transport options and I am not sure we should include all of them in the connect options. Although headers is probably one of the most common options and might be worth including.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how difficult it is to provide a custom transport instead (already supported feature)?

It is not difficult because for this all I have to do is copy code from NodeWebSocketTransport - https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/common/NodeWebSocketTransport.ts and add some headers. But it seems that such a need may arise not only for me. So it might be worth supporting it out of the box.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it have been requested before, at least I found this one #7218. Do you know of any other open or closed issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know of any other open or closed issues?

No, I don't know any more

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a line Closes #7218 to the end of your commit message/PR description?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

OrKoN marked this conversation as resolved.
Show resolved Hide resolved
connection = new Connection(browserWSEndpoint, connectionTransport, slowMo);
} else if (browserURL) {
const connectionURL = await getWSEndpoint(browserURL);
Expand Down
6 changes: 5 additions & 1 deletion packages/puppeteer-core/src/common/NodeWebSocketTransport.ts
Expand Up @@ -21,14 +21,18 @@ import {packageVersion} from '../generated/version.js';
* @internal
*/
export class NodeWebSocketTransport implements ConnectionTransport {
static create(url: string): Promise<NodeWebSocketTransport> {
static create(
url: string,
headers?: Record<string, string>
): Promise<NodeWebSocketTransport> {
return new Promise((resolve, reject) => {
const ws = new NodeWebSocket(url, [], {
followRedirects: true,
perMessageDeflate: false,
maxPayload: 256 * 1024 * 1024, // 256Mb
headers: {
'User-Agent': `Puppeteer ${packageVersion}`,
...headers,
},
});

Expand Down
6 changes: 6 additions & 0 deletions packages/puppeteer-core/src/common/Puppeteer.ts
Expand Up @@ -42,6 +42,12 @@ export interface ConnectOptions extends BrowserConnectOptions {
browserWSEndpoint?: string;
browserURL?: string;
transport?: ConnectionTransport;
/**
* Headers to use for the web socket connection.
* @remarks
* Only works in the Node.js environment.
*/
headers?: Record<string, string>;
OrKoN marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down