From 3ee4951506d300e83fa3a670c378de5d5566b6a3 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 11 Aug 2017 17:47:33 -0700 Subject: [PATCH] [DEBUG] add "session" namespace to trace target protocol messages (#249) --- CONTRIBUTING.md | 1 + lib/Connection.js | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 844649df..3b6521ba 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -146,6 +146,7 @@ browser.close(); ``` 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=*:page node script.js` - dump only Page's API calls - `DEBUG=*:mouse,*:keyboard node script.js` - dump only Mouse and Keyboard API calls diff --git a/lib/Connection.js b/lib/Connection.js index 90ae5544..d41f6304 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -13,7 +13,8 @@ * See the License for the specific language governing permissions and * 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 WebSocket = require('ws'); @@ -48,7 +49,7 @@ class Connection extends EventEmitter { send(method, params = {}) { let id = ++this._lastId; let message = JSON.stringify({id, method, params}); - debug('SEND ► ' + message); + debugProtocol('SEND ► ' + message); this._ws.send(message); return new Promise((resolve, reject) => { this._callbacks.set(id, {resolve, reject, method}); @@ -61,7 +62,7 @@ class Connection extends EventEmitter { async _onMessage(message) { if (this._delay) await new Promise(f => setTimeout(f, this._delay)); - debug('◀ RECV ' + message); + debugProtocol('◀ RECV ' + message); let object = JSON.parse(message); if (object.id && this._callbacks.has(object.id)) { let callback = this._callbacks.get(object.id); @@ -166,9 +167,10 @@ class Session extends EventEmitter { * @param {!Object=} params * @return {!Promise} */ - async send(method, params = {}) { + send(method, params = {}) { let id = ++this._lastId; let message = JSON.stringify({id, method, params}); + debugSession('SEND ► ' + message); this._connection.send('Target.sendMessageToTarget', {sessionId: this._sessionId, message}).catch(e => { // The response from target might have been already dispatched. if (!this._callbacks.has(id)) @@ -186,6 +188,7 @@ class Session extends EventEmitter { * @param {string} message */ _onMessage(message) { + debugSession('◀ RECV ' + message); let object = JSON.parse(message); if (object.id && this._callbacks.has(object.id)) { let callback = this._callbacks.get(object.id);