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: add sendRaw function #441

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,30 @@ For example:
client.send('Page.navigate', {url: 'https://github.com'}, console.log);
```

#### client.sendRaw(message, [callback])

Issue a raw message to the remote instance.

`message` is a raw message object according to the Chrome DevTools protocol.
`message.id` does not need to be set, if it is not passed, a unique ID will
be generated.

`sendRaw` can be useful when you need lower-level control, like if you need to
pass a `sessionId` with your command to scope it to a specific target.

A `callback` can be passed. If omitted, a `Promise` is returned. See
`client.send` for more information.

For example:

```js
client.sendRaw({
method: 'Page.navigate',
params: { url: 'https://github.com' },
sessionId: 'some session ID',
}, console.log);
```

#### client.`<domain>`.`<method>`([params], [callback])

Just a shorthand for:
Expand Down
24 changes: 14 additions & 10 deletions lib/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,26 @@ class Chrome extends EventEmitter {
callback = params;
params = undefined;
}

return this.sendRaw({
method,
params: params || {}
}, callback);
}

sendRaw(message, callback) {
// return a promise when a callback is not provided
if (typeof callback === 'function') {
this._enqueueCommand(method, params, callback);
this._enqueueCommand(message, callback);
return undefined;
} else {
return new Promise((fulfill, reject) => {
this._enqueueCommand(method, params, (error, response) => {
this._enqueueCommand(message, (error, response) => {
if (error) {
const request = {method, params};
reject(
error instanceof Error
? error // low-level WebSocket error
: new ProtocolError(request, response)
: new ProtocolError(message, response)
);
} else {
fulfill(response);
Expand Down Expand Up @@ -272,20 +279,17 @@ class Chrome extends EventEmitter {
}

// send a command to the remote endpoint and register a callback for the reply
_enqueueCommand(method, params, callback) {
_enqueueCommand(message, callback) {
const id = this._nextCommandId++;
const message = {
id, method,
params: params || {}
};
message = { id, ...message };
this._ws.send(JSON.stringify(message), (err) => {
if (err) {
// handle low-level WebSocket errors
if (typeof callback === 'function') {
callback(err);
}
} else {
this._callbacks[id] = callback;
this._callbacks[message.id] = callback;
}
});
}
Expand Down
13 changes: 13 additions & 0 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,17 @@ describe('sending a command', () => {
});
});
});
describe('using sendRaw', () => {
it('should succeed', (done) => {
Chrome((chrome) => {
chrome.sendRaw({
method: 'Network.setCacheDisabled',
params: {'cacheDisabled': true}
}, (error, response) => {
assert(!error);
chrome.close(done);
});
});
});
});
});