Skip to content

Commit

Permalink
Merge branch 'master' into stdio-transport
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Dec 29, 2020
2 parents baad1bd + 2adff57 commit 2a84361
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,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 @@ -80,19 +80,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 @@ -289,20 +296,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._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);
});
});
});
});
});

0 comments on commit 2a84361

Please sign in to comment.