[DEBUG] add "session" namespace to trace target protocol messages (#249)

This commit is contained in:
Andrey Lushnikov 2017-08-11 17:47:33 -07:00 committed by GitHub
parent 56b0d8722b
commit 3ee4951506
2 changed files with 8 additions and 4 deletions

View File

@ -146,6 +146,7 @@ browser.close();
``` ```
Tips-n-tricks: Tips-n-tricks:
- `DEBUG=*:session node script.js` - dump protocol session messages (protocol messages to targets)
- `DEBUG=*,-*:protocol node script.js` - dump everything BUT protocol messages - `DEBUG=*,-*:protocol node script.js` - dump everything BUT protocol messages
- `DEBUG=*:page node script.js` - dump only Page's API calls - `DEBUG=*:page node script.js` - dump only Page's API calls
- `DEBUG=*:mouse,*:keyboard node script.js` - dump only Mouse and Keyboard API calls - `DEBUG=*:mouse,*:keyboard node script.js` - dump only Mouse and Keyboard API calls

View File

@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
const debug = require('debug')('puppeteer:protocol'); const debugProtocol = require('debug')('puppeteer:protocol');
const debugSession = require('debug')('puppeteer:session');
const EventEmitter = require('events'); const EventEmitter = require('events');
const WebSocket = require('ws'); const WebSocket = require('ws');
@ -48,7 +49,7 @@ class Connection extends EventEmitter {
send(method, params = {}) { send(method, params = {}) {
let id = ++this._lastId; let id = ++this._lastId;
let message = JSON.stringify({id, method, params}); let message = JSON.stringify({id, method, params});
debug('SEND ► ' + message); debugProtocol('SEND ► ' + message);
this._ws.send(message); this._ws.send(message);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._callbacks.set(id, {resolve, reject, method}); this._callbacks.set(id, {resolve, reject, method});
@ -61,7 +62,7 @@ class Connection extends EventEmitter {
async _onMessage(message) { async _onMessage(message) {
if (this._delay) if (this._delay)
await new Promise(f => setTimeout(f, this._delay)); await new Promise(f => setTimeout(f, this._delay));
debug('◀ RECV ' + message); debugProtocol('◀ RECV ' + message);
let object = JSON.parse(message); let object = JSON.parse(message);
if (object.id && this._callbacks.has(object.id)) { if (object.id && this._callbacks.has(object.id)) {
let callback = this._callbacks.get(object.id); let callback = this._callbacks.get(object.id);
@ -166,9 +167,10 @@ class Session extends EventEmitter {
* @param {!Object=} params * @param {!Object=} params
* @return {!Promise<?Object>} * @return {!Promise<?Object>}
*/ */
async send(method, params = {}) { send(method, params = {}) {
let id = ++this._lastId; let id = ++this._lastId;
let message = JSON.stringify({id, method, params}); let message = JSON.stringify({id, method, params});
debugSession('SEND ► ' + message);
this._connection.send('Target.sendMessageToTarget', {sessionId: this._sessionId, message}).catch(e => { this._connection.send('Target.sendMessageToTarget', {sessionId: this._sessionId, message}).catch(e => {
// The response from target might have been already dispatched. // The response from target might have been already dispatched.
if (!this._callbacks.has(id)) if (!this._callbacks.has(id))
@ -186,6 +188,7 @@ class Session extends EventEmitter {
* @param {string} message * @param {string} message
*/ */
_onMessage(message) { _onMessage(message) {
debugSession('◀ RECV ' + message);
let object = JSON.parse(message); let object = JSON.parse(message);
if (object.id && this._callbacks.has(object.id)) { if (object.id && this._callbacks.has(object.id)) {
let callback = this._callbacks.get(object.id); let callback = this._callbacks.get(object.id);