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.
This commit is contained in:
Andrey Lushnikov 2017-07-17 15:15:07 -07:00
parent 4581ada210
commit 0414dfa98b
4 changed files with 39 additions and 8 deletions

View File

@ -40,7 +40,7 @@ are used to test `phantom_shim`.
To run puppeteer tests, use: To run puppeteer tests, use:
``` ```
npm run test-puppeteer npm run unit
``` ```
To run phantom-shim against phantomjs tests, use: To run phantom-shim against phantomjs tests, use:
@ -53,3 +53,15 @@ To run both puppeteer and phantom_shim tests, use:
npm test 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();
});
```

View File

@ -13,6 +13,7 @@
* 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.
*/ */
let debug = require('debug')('puppeteer:protocol');
let EventEmitter = require('events'); let EventEmitter = require('events');
let WebSocket = require('ws'); let WebSocket = require('ws');
@ -46,6 +47,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);
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});
@ -56,6 +58,7 @@ class Connection extends EventEmitter {
* @param {string} message * @param {string} message
*/ */
_onMessage(message) { _onMessage(message) {
debug('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);

29
package-lock.json generated
View File

@ -247,11 +247,11 @@
} }
}, },
"debug": { "debug": {
"version": "2.2.0", "version": "2.6.8",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz",
"integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=", "integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw=",
"requires": { "requires": {
"ms": "0.7.1" "ms": "2.0.0"
} }
}, },
"deep-is": { "deep-is": {
@ -477,6 +477,21 @@
"debug": "2.2.0", "debug": "2.2.0",
"mkdirp": "0.5.0", "mkdirp": "0.5.0",
"yauzl": "2.4.1" "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": { "fast-levenshtein": {
@ -973,9 +988,9 @@
} }
}, },
"ms": { "ms": {
"version": "0.7.1", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=" "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}, },
"mute-stream": { "mute-stream": {
"version": "0.0.7", "version": "0.0.7",

View File

@ -20,6 +20,7 @@
"author": "The Chromium Authors", "author": "The Chromium Authors",
"license": "SEE LICENSE IN LICENSE", "license": "SEE LICENSE IN LICENSE",
"dependencies": { "dependencies": {
"debug": "^2.6.8",
"extract-zip": "^1.6.5", "extract-zip": "^1.6.5",
"mime": "^1.3.4", "mime": "^1.3.4",
"progress": "^2.0.0", "progress": "^2.0.0",