Skip to content

Commit e129c0f

Browse files
committed
support for fetching the protocol remotely via stdio pipes
1 parent c9c7356 commit e129c0f

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lib/chrome.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,26 @@ class Chrome extends EventEmitter {
118118

119119
// initiate the connection process
120120
async _start() {
121-
const options = {
122-
host: this.host,
123-
port: this.port,
124-
secure: this.secure,
125-
useHostName: this.useHostName,
126-
alterPath: this.alterPath,
127-
...this.connectOptions,
128-
};
129121
try {
130-
if (!this.process) {
122+
if (this.process)
123+
{
124+
const options = {local: this.local, cdp: this};
125+
// we first "connect" to stdio pipes, so that we can
126+
// first the protocol remotely via the pipe.
127+
await this._connect();
128+
const protocol = await this._fetchProtocol(options);
129+
api.prepare(this, protocol);
130+
}
131+
else
132+
{
133+
const options = {
134+
host: this.host,
135+
port: this.port,
136+
secure: this.secure,
137+
useHostName: this.useHostName,
138+
alterPath: this.alterPath,
139+
...this.connectOptions,
140+
};
131141
// fetch the WebSocket debugger URL
132142
const url = await this._fetchDebuggerURL(options);
133143
// allow the user to alter the URL
@@ -137,12 +147,12 @@ class Chrome extends EventEmitter {
137147
// update the connection parameters using the debugging URL
138148
options.host = urlObject.hostname;
139149
options.port = urlObject.port || options.port;
150+
// fetch the protocol and prepare the API
151+
const protocol = await this._fetchProtocol(options);
152+
api.prepare(this, protocol);
153+
// finally connect to the WebSocket
154+
await this._connect();
140155
}
141-
// fetch the protocol and prepare the API
142-
const protocol = await this._fetchProtocol(options);
143-
api.prepare(this, protocol);
144-
// finally connect to the WebSocket or stdio
145-
await this._connect();
146156
// since the handler is executed synchronously, the emit() must be
147157
// performed in the next tick so that uncaught errors in the client code
148158
// are not intercepted by the Promise mechanism and therefore reported

lib/devtools.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ function promisesWrapper(func) {
4848
}
4949

5050
function Protocol(options, callback) {
51+
// fetch remotely via CDP when using stdio pipes (Bright Data exclusive)
52+
if (options.cdp)
53+
{
54+
return options.cdp.send('Browser.getProtocolJson', (err, data)=>{
55+
if (err)
56+
return callback(err);
57+
callback(null, JSON.parse(data.result));
58+
});
59+
}
5160
// if the local protocol is requested
5261
if (options.local) {
5362
const localDescriptor = require('./protocol.json');

0 commit comments

Comments
 (0)