2017-06-14 22:45:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-08-12 00:47:33 +00:00
|
|
|
const debugProtocol = require('debug')('puppeteer:protocol');
|
|
|
|
const debugSession = require('debug')('puppeteer:session');
|
2017-06-14 22:45:59 +00:00
|
|
|
|
2017-08-02 19:06:47 +00:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const WebSocket = require('ws');
|
2017-06-14 22:45:59 +00:00
|
|
|
|
|
|
|
class Connection extends EventEmitter {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-08-15 21:29:42 +00:00
|
|
|
* @param {string} url
|
|
|
|
* @param {number=} delay
|
|
|
|
* @return {!Promise<!Connection>}
|
|
|
|
*/
|
|
|
|
static async create(url, delay = 0) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const ws = new WebSocket(url, { perMessageDeflate: false });
|
2017-08-15 21:29:42 +00:00
|
|
|
ws.on('open', () => resolve(new Connection(url, ws, delay)));
|
|
|
|
ws.on('error', reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!WebSocket} ws
|
2017-08-01 01:47:56 +00:00
|
|
|
* @param {number=} delay
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-08-15 21:29:42 +00:00
|
|
|
constructor(url, ws, delay = 0) {
|
2017-06-21 20:51:06 +00:00
|
|
|
super();
|
2017-08-15 21:29:42 +00:00
|
|
|
this._url = url;
|
2017-06-21 20:51:06 +00:00
|
|
|
this._lastId = 0;
|
2017-07-26 16:58:03 +00:00
|
|
|
/** @type {!Map<number, {resolve: function, reject: function, method: string}>}*/
|
2017-06-21 20:51:06 +00:00
|
|
|
this._callbacks = new Map();
|
2017-08-15 21:29:42 +00:00
|
|
|
this._delay = delay;
|
2017-06-14 22:45:59 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
this._ws = ws;
|
|
|
|
this._ws.on('message', this._onMessage.bind(this));
|
|
|
|
this._ws.on('close', this._onClose.bind(this));
|
2017-08-09 23:14:00 +00:00
|
|
|
/** @type {!Map<string, !Session>}*/
|
|
|
|
this._sessions = new Map();
|
2017-07-19 05:10:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:29:42 +00:00
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {string} method
|
2017-07-30 20:46:56 +00:00
|
|
|
* @param {!Object=} params
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {!Promise<?Object>}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
send(method, params = {}) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const id = ++this._lastId;
|
|
|
|
const message = JSON.stringify({id, method, params});
|
2017-08-12 00:47:33 +00:00
|
|
|
debugProtocol('SEND ► ' + message);
|
2017-06-21 20:51:06 +00:00
|
|
|
this._ws.send(message);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._callbacks.set(id, {resolve, reject, method});
|
|
|
|
});
|
|
|
|
}
|
2017-06-14 22:45:59 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {string} message
|
|
|
|
*/
|
2017-08-01 01:47:56 +00:00
|
|
|
async _onMessage(message) {
|
|
|
|
if (this._delay)
|
|
|
|
await new Promise(f => setTimeout(f, this._delay));
|
2017-08-12 00:47:33 +00:00
|
|
|
debugProtocol('◀ RECV ' + message);
|
2017-08-21 23:39:04 +00:00
|
|
|
const object = JSON.parse(message);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (object.id && this._callbacks.has(object.id)) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const callback = this._callbacks.get(object.id);
|
2017-06-21 20:51:06 +00:00
|
|
|
this._callbacks.delete(object.id);
|
|
|
|
if (object.error)
|
2017-07-06 00:21:42 +00:00
|
|
|
callback.reject(new Error(`Protocol error (${callback.method}): ${object.error.message} ${object.error.data}`));
|
2017-06-21 20:51:06 +00:00
|
|
|
else
|
|
|
|
callback.resolve(object.result);
|
|
|
|
} else {
|
|
|
|
console.assert(!object.id);
|
2017-08-09 23:14:00 +00:00
|
|
|
if (object.method === 'Target.receivedMessageFromTarget') {
|
|
|
|
const session = this._sessions.get(object.params.sessionId);
|
|
|
|
if (session)
|
|
|
|
session._onMessage(object.params.message);
|
|
|
|
} else if (object.method === 'Target.detachedFromTarget') {
|
|
|
|
const session = this._sessions.get(object.params.sessionId);
|
|
|
|
if (session)
|
|
|
|
session._onClosed();
|
|
|
|
this._sessions.delete(object.params.sessionId);
|
|
|
|
} else {
|
|
|
|
this.emit(object.method, object.params);
|
|
|
|
}
|
2017-06-14 22:45:59 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-14 22:45:59 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
_onClose() {
|
|
|
|
this._ws.removeAllListeners();
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const callback of this._callbacks.values())
|
2017-07-19 07:40:52 +00:00
|
|
|
callback.reject(new Error(`Protocol error (${callback.method}): Target closed.`));
|
2017-08-09 23:14:00 +00:00
|
|
|
this._callbacks.clear();
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const session of this._sessions.values())
|
2017-08-09 23:14:00 +00:00
|
|
|
session._onClosed();
|
|
|
|
this._sessions.clear();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-14 22:45:59 +00:00
|
|
|
|
2017-08-15 01:08:06 +00:00
|
|
|
dispose() {
|
|
|
|
this._onClose();
|
2017-08-09 23:14:00 +00:00
|
|
|
this._ws.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} targetId
|
|
|
|
* @return {!Promise<!Session>}
|
|
|
|
*/
|
|
|
|
async createSession(targetId) {
|
|
|
|
const {sessionId} = await this.send('Target.attachToTarget', {targetId});
|
|
|
|
const session = new Session(this, targetId, sessionId);
|
|
|
|
this._sessions.set(sessionId, session);
|
|
|
|
return session;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-14 22:45:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 23:14:00 +00:00
|
|
|
class Session extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* @param {!Connection} connection
|
|
|
|
* @param {string} targetId
|
|
|
|
* @param {string} sessionId
|
|
|
|
*/
|
|
|
|
constructor(connection, targetId, sessionId) {
|
|
|
|
super();
|
|
|
|
this._lastId = 0;
|
|
|
|
/** @type {!Map<number, {resolve: function, reject: function, method: string}>}*/
|
|
|
|
this._callbacks = new Map();
|
|
|
|
this._connection = connection;
|
|
|
|
this._targetId = targetId;
|
|
|
|
this._sessionId = sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
targetId() {
|
|
|
|
return this._targetId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} method
|
|
|
|
* @param {!Object=} params
|
|
|
|
* @return {!Promise<?Object>}
|
|
|
|
*/
|
2017-08-12 00:47:33 +00:00
|
|
|
send(method, params = {}) {
|
2017-08-15 21:54:02 +00:00
|
|
|
if (!this._connection)
|
|
|
|
return Promise.reject(new Error(`Protocol error (${method}): Session closed. Most likely the page has been closed.`));
|
2017-08-21 23:39:04 +00:00
|
|
|
const id = ++this._lastId;
|
|
|
|
const message = JSON.stringify({id, method, params});
|
2017-08-12 00:47:33 +00:00
|
|
|
debugSession('SEND ► ' + message);
|
2017-08-09 23:14:00 +00:00
|
|
|
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))
|
|
|
|
return;
|
2017-08-21 23:39:04 +00:00
|
|
|
const callback = this._callbacks.get(id);
|
2017-10-10 05:31:40 +00:00
|
|
|
this._callbacks.delete(id);
|
2017-08-09 23:14:00 +00:00
|
|
|
callback.reject(e);
|
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._callbacks.set(id, {resolve, reject, method});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} message
|
|
|
|
*/
|
|
|
|
_onMessage(message) {
|
2017-08-12 00:47:33 +00:00
|
|
|
debugSession('◀ RECV ' + message);
|
2017-08-21 23:39:04 +00:00
|
|
|
const object = JSON.parse(message);
|
2017-08-09 23:14:00 +00:00
|
|
|
if (object.id && this._callbacks.has(object.id)) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const callback = this._callbacks.get(object.id);
|
2017-08-09 23:14:00 +00:00
|
|
|
this._callbacks.delete(object.id);
|
|
|
|
if (object.error)
|
|
|
|
callback.reject(new Error(`Protocol error (${callback.method}): ${object.error.message} ${object.error.data}`));
|
|
|
|
else
|
|
|
|
callback.resolve(object.result);
|
|
|
|
} else {
|
|
|
|
console.assert(!object.id);
|
|
|
|
this.emit(object.method, object.params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async dispose() {
|
|
|
|
await this._connection.send('Target.closeTarget', {targetId: this._targetId});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onClosed() {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const callback of this._callbacks.values())
|
2017-08-09 23:14:00 +00:00
|
|
|
callback.reject(new Error(`Protocol error (${callback.method}): Target closed.`));
|
|
|
|
this._callbacks.clear();
|
2017-08-15 21:54:02 +00:00
|
|
|
this._connection = null;
|
2017-08-09 23:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:31:40 +00:00
|
|
|
module.exports = {Connection, Session};
|