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.
|
|
|
|
*/
|
2018-05-31 23:53:51 +00:00
|
|
|
const {helper, assert} = require('./helper');
|
2017-08-12 00:47:33 +00:00
|
|
|
const debugProtocol = require('debug')('puppeteer:protocol');
|
|
|
|
const debugSession = require('debug')('puppeteer:session');
|
2017-08-02 19:06:47 +00:00
|
|
|
const EventEmitter = require('events');
|
2017-06-14 22:45:59 +00:00
|
|
|
|
|
|
|
class Connection extends EventEmitter {
|
2017-08-15 21:29:42 +00:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
2018-02-15 01:51:29 +00:00
|
|
|
* @param {!Puppeteer.ConnectionTransport} transport
|
2017-08-01 01:47:56 +00:00
|
|
|
* @param {number=} delay
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2018-02-15 01:51:29 +00:00
|
|
|
constructor(url, transport, 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-12-09 03:05:46 +00:00
|
|
|
/** @type {!Map<number, {resolve: function, reject: function, error: !Error, 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
|
|
|
|
2018-02-15 01:51:29 +00:00
|
|
|
this._transport = transport;
|
2018-09-07 20:36:16 +00:00
|
|
|
this._transport.onmessage = this._onMessage.bind(this);
|
|
|
|
this._transport.onclose = this._onClose.bind(this);
|
2018-01-11 03:33:22 +00:00
|
|
|
/** @type {!Map<string, !CDPSession>}*/
|
2017-08-09 23:14:00 +00:00
|
|
|
this._sessions = new Map();
|
2018-09-14 18:44:54 +00:00
|
|
|
this._closed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!CDPSession} session
|
|
|
|
* @return {!Connection}
|
|
|
|
*/
|
|
|
|
static fromSession(session) {
|
|
|
|
let connection = session._connection;
|
|
|
|
// TODO(lushnikov): move to flatten protocol to avoid this.
|
|
|
|
while (connection instanceof CDPSession)
|
|
|
|
connection = connection._connection;
|
|
|
|
return connection;
|
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);
|
2018-02-15 01:51:29 +00:00
|
|
|
this._transport.send(message);
|
2017-06-21 20:51:06 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-09 03:05:46 +00:00
|
|
|
this._callbacks.set(id, {resolve, reject, error: new Error(), method});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
|
|
|
}
|
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);
|
2018-04-08 00:59:22 +00:00
|
|
|
if (object.id) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const callback = this._callbacks.get(object.id);
|
2018-04-08 00:59:22 +00:00
|
|
|
// Callbacks could be all rejected if someone has called `.dispose()`.
|
|
|
|
if (callback) {
|
|
|
|
this._callbacks.delete(object.id);
|
|
|
|
if (object.error)
|
2018-06-14 22:27:59 +00:00
|
|
|
callback.reject(createProtocolError(callback.error, callback.method, object));
|
2018-04-08 00:59:22 +00:00
|
|
|
else
|
|
|
|
callback.resolve(object.result);
|
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
} else {
|
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() {
|
2018-09-14 18:44:54 +00:00
|
|
|
if (this._closed)
|
|
|
|
return;
|
|
|
|
this._closed = true;
|
2018-09-07 20:36:16 +00:00
|
|
|
this._transport.onmessage = null;
|
|
|
|
this._transport.onclose = null;
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const callback of this._callbacks.values())
|
2017-12-09 03:05:46 +00:00
|
|
|
callback.reject(rewriteError(callback.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();
|
2018-09-14 18:44:54 +00:00
|
|
|
this.emit(Connection.Events.Disconnected);
|
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();
|
2018-02-15 01:51:29 +00:00
|
|
|
this._transport.close();
|
2017-08-09 23:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-07 01:16:17 +00:00
|
|
|
* @param {Protocol.Target.TargetInfo} targetInfo
|
2018-01-11 03:33:22 +00:00
|
|
|
* @return {!Promise<!CDPSession>}
|
2017-08-09 23:14:00 +00:00
|
|
|
*/
|
2018-06-07 01:16:17 +00:00
|
|
|
async createSession(targetInfo) {
|
|
|
|
const {sessionId} = await this.send('Target.attachToTarget', {targetId: targetInfo.targetId});
|
|
|
|
const session = new CDPSession(this, targetInfo.type, sessionId);
|
2017-08-09 23:14:00 +00:00
|
|
|
this._sessions.set(sessionId, session);
|
|
|
|
return session;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-14 22:45:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 18:44:54 +00:00
|
|
|
Connection.Events = {
|
|
|
|
Disconnected: Symbol('Connection.Events.Disconnected'),
|
|
|
|
};
|
|
|
|
|
2018-01-11 03:33:22 +00:00
|
|
|
class CDPSession extends EventEmitter {
|
2017-08-09 23:14:00 +00:00
|
|
|
/**
|
2018-05-21 21:31:11 +00:00
|
|
|
* @param {!Connection|!CDPSession} connection
|
2018-06-07 01:16:17 +00:00
|
|
|
* @param {string} targetType
|
2017-08-09 23:14:00 +00:00
|
|
|
* @param {string} sessionId
|
|
|
|
*/
|
2018-06-07 01:16:17 +00:00
|
|
|
constructor(connection, targetType, sessionId) {
|
2017-08-09 23:14:00 +00:00
|
|
|
super();
|
|
|
|
this._lastId = 0;
|
2017-12-09 03:05:46 +00:00
|
|
|
/** @type {!Map<number, {resolve: function, reject: function, error: !Error, method: string}>}*/
|
2017-08-09 23:14:00 +00:00
|
|
|
this._callbacks = new Map();
|
2018-08-24 22:17:36 +00:00
|
|
|
/** @type {null|Connection|CDPSession} */
|
2017-08-09 23:14:00 +00:00
|
|
|
this._connection = connection;
|
2018-06-07 01:16:17 +00:00
|
|
|
this._targetType = targetType;
|
2017-08-09 23:14:00 +00:00
|
|
|
this._sessionId = sessionId;
|
2018-05-21 21:31:11 +00:00
|
|
|
/** @type {!Map<string, !CDPSession>}*/
|
|
|
|
this._sessions = new Map();
|
2017-08-09 23:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
2018-06-07 01:16:17 +00:00
|
|
|
return Promise.reject(new Error(`Protocol error (${method}): Session closed. Most likely the ${this._targetType} 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-12-09 03:05:46 +00:00
|
|
|
callback.reject(rewriteError(callback.error, e && e.message));
|
2017-08-09 23:14:00 +00:00
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-09 03:05:46 +00:00
|
|
|
this._callbacks.set(id, {resolve, reject, error: new Error(), method});
|
2017-08-09 23:14:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
2018-06-14 22:27:59 +00:00
|
|
|
callback.reject(createProtocolError(callback.error, callback.method, object));
|
2017-08-09 23:14:00 +00:00
|
|
|
else
|
|
|
|
callback.resolve(object.result);
|
|
|
|
} else {
|
2018-05-21 21:31:11 +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);
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!object.id);
|
2017-08-09 23:14:00 +00:00
|
|
|
this.emit(object.method, object.params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 03:33:22 +00:00
|
|
|
async detach() {
|
2018-08-24 22:17:36 +00:00
|
|
|
if (!this._connection)
|
|
|
|
throw new Error(`Session already detached. Most likely the ${this._targetType} has been closed.`);
|
2018-01-11 03:33:22 +00:00
|
|
|
await this._connection.send('Target.detachFromTarget', {sessionId: this._sessionId});
|
2017-08-09 23:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onClosed() {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const callback of this._callbacks.values())
|
2017-12-09 03:05:46 +00:00
|
|
|
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): Target closed.`));
|
2017-08-09 23:14:00 +00:00
|
|
|
this._callbacks.clear();
|
2017-08-15 21:54:02 +00:00
|
|
|
this._connection = null;
|
2017-08-09 23:14:00 +00:00
|
|
|
}
|
2018-05-21 21:31:11 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-07 01:16:17 +00:00
|
|
|
* @param {string} targetType
|
2018-05-21 21:31:11 +00:00
|
|
|
* @param {string} sessionId
|
|
|
|
*/
|
2018-06-07 01:16:17 +00:00
|
|
|
_createSession(targetType, sessionId) {
|
|
|
|
const session = new CDPSession(this, targetType, sessionId);
|
2018-05-21 21:31:11 +00:00
|
|
|
this._sessions.set(sessionId, session);
|
|
|
|
return session;
|
|
|
|
}
|
2017-08-09 23:14:00 +00:00
|
|
|
}
|
2018-01-11 03:33:22 +00:00
|
|
|
helper.tracePublicAPI(CDPSession);
|
2017-08-09 23:14:00 +00:00
|
|
|
|
2018-06-14 22:27:59 +00:00
|
|
|
/**
|
|
|
|
* @param {!Error} error
|
|
|
|
* @param {string} method
|
|
|
|
* @param {{error: {message: string, data: any}}} object
|
|
|
|
* @return {!Error}
|
|
|
|
*/
|
|
|
|
function createProtocolError(error, method, object) {
|
|
|
|
let message = `Protocol error (${method}): ${object.error.message}`;
|
|
|
|
if ('data' in object.error)
|
|
|
|
message += ` ${object.error.data}`;
|
2018-08-24 22:17:36 +00:00
|
|
|
return rewriteError(error, message);
|
2018-06-14 22:27:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 03:05:46 +00:00
|
|
|
/**
|
|
|
|
* @param {!Error} error
|
|
|
|
* @param {string} message
|
|
|
|
* @return {!Error}
|
|
|
|
*/
|
|
|
|
function rewriteError(error, message) {
|
|
|
|
error.message = message;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2018-01-11 03:33:22 +00:00
|
|
|
module.exports = {Connection, CDPSession};
|