From 0414dfa98bd96ba2337f6d7b1137e467273a04f2 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 17 Jul 2017 15:15:07 -0700 Subject: [PATCH] Introduce DEBUG module This patch re-introduces the DEBUG module to expose some of the puppeteer's internals. Currently, only the protocol message communication is exposed under the 'puppeteer:protocol' namespace. --- CONTRIBUTING.md | 14 +++++++++++++- lib/Connection.js | 3 +++ package-lock.json | 29 ++++++++++++++++++++++------- package.json | 1 + 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 421e985b..70ab772f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,7 +40,7 @@ are used to test `phantom_shim`. To run puppeteer tests, use: ``` -npm run test-puppeteer +npm run unit ``` To run phantom-shim against phantomjs tests, use: @@ -53,3 +53,15 @@ To run both puppeteer and phantom_shim tests, use: npm test ``` +## DEBUG module +Puppeteer uses [debug](https://github.com/visionmedia/debug) module to expose some of it's inner guts under the `puppeteer` namespace. +Try putting the following script in the `script.js` and running it via `DEBUG=* node script.js`: + +```js +const {Browser} = require('puppeteer'); +const browser = new Browser(); +browser.newPage().then(async page => { + await page.navigate('https://example.com'); + browser.close(); +}); +``` diff --git a/lib/Connection.js b/lib/Connection.js index 600b0602..ade5eb72 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +let debug = require('debug')('puppeteer:protocol'); let EventEmitter = require('events'); let WebSocket = require('ws'); @@ -46,6 +47,7 @@ class Connection extends EventEmitter { send(method, params = {}) { let id = ++this._lastId; let message = JSON.stringify({id, method, params}); + debug('◀ SEND ' + message); this._ws.send(message); return new Promise((resolve, reject) => { this._callbacks.set(id, {resolve, reject, method}); @@ -56,6 +58,7 @@ class Connection extends EventEmitter { * @param {string} message */ _onMessage(message) { + debug('RECV ► ' + message); let object = JSON.parse(message); if (object.id && this._callbacks.has(object.id)) { let callback = this._callbacks.get(object.id); diff --git a/package-lock.json b/package-lock.json index 7da024d9..8cc1c15c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -247,11 +247,11 @@ } }, "debug": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz", - "integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=", + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz", + "integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw=", "requires": { - "ms": "0.7.1" + "ms": "2.0.0" } }, "deep-is": { @@ -477,6 +477,21 @@ "debug": "2.2.0", "mkdirp": "0.5.0", "yauzl": "2.4.1" + }, + "dependencies": { + "debug": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz", + "integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=", + "requires": { + "ms": "0.7.1" + } + }, + "ms": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz", + "integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=" + } } }, "fast-levenshtein": { @@ -973,9 +988,9 @@ } }, "ms": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz", - "integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "mute-stream": { "version": "0.0.7", diff --git a/package.json b/package.json index 3646f88a..e1340f07 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "author": "The Chromium Authors", "license": "SEE LICENSE IN LICENSE", "dependencies": { + "debug": "^2.6.8", "extract-zip": "^1.6.5", "mime": "^1.3.4", "progress": "^2.0.0",