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-01-11 03:33:22 +00:00
|
|
|
const {helper} = require('./helper');
|
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');
|
2018-02-15 01:51:29 +00:00
|
|
|
const Pipe = require('./Pipe');
|
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>}
|
|
|
|
*/
|
2018-02-15 01:51:29 +00:00
|
|
|
static async createForWebSocket(url, delay = 0) {
|
2017-08-15 21:29:42 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-15 01:51:29 +00:00
|
|
|
/**
|
|
|
|
* @param {!NodeJS.WritableStream} pipeWrite
|
|
|
|
* @param {!NodeJS.ReadableStream} pipeRead
|
|
|
|
* @param {number=} delay
|
|
|
|
* @return {!Connection}
|
|
|
|
*/
|
|
|
|
static createForPipe(pipeWrite, pipeRead, delay = 0) {
|
|
|
|
return new Connection('', new Pipe(pipeWrite, pipeRead), delay);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
this._transport.on('message', this._onMessage.bind(this));
|
|
|
|
this._transport.on('close', 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();
|
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-11-04 01:46:17 +00:00
|
|
|
/**
|
|
|
|
* @param {function()} callback
|
|
|
|
*/
|
|
|
|
setClosedCallback(callback) {
|
|
|
|
this._closeCallback = callback;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): ${object.error.message} ${object.error.data}`));
|
|
|
|
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() {
|
2017-11-04 01:46:17 +00:00
|
|
|
if (this._closeCallback) {
|
|
|
|
this._closeCallback();
|
|
|
|
this._closeCallback = null;
|
|
|
|
}
|
2018-02-15 01:51:29 +00:00
|
|
|
this._transport.removeAllListeners();
|
2018-02-23 00:23:20 +00:00
|
|
|
// If transport throws any error at this point of time, we don't care and should swallow it.
|
|
|
|
this._transport.on('error', () => {});
|
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();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} targetId
|
2018-01-11 03:33:22 +00:00
|
|
|
* @return {!Promise<!CDPSession>}
|
2017-08-09 23:14:00 +00:00
|
|
|
*/
|
|
|
|
async createSession(targetId) {
|
|
|
|
const {sessionId} = await this.send('Target.attachToTarget', {targetId});
|
2018-01-11 03:33:22 +00:00
|
|
|
const session = new CDPSession(this, targetId, 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-01-11 03:33:22 +00:00
|
|
|
class CDPSession extends EventEmitter {
|
2017-08-09 23:14:00 +00:00
|
|
|
/**
|
|
|
|
* @param {!Connection} connection
|
|
|
|
* @param {string} targetId
|
|
|
|
* @param {string} sessionId
|
|
|
|
*/
|
|
|
|
constructor(connection, targetId, sessionId) {
|
|
|
|
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();
|
|
|
|
this._connection = connection;
|
|
|
|
this._targetId = targetId;
|
|
|
|
this._sessionId = sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-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)
|
2017-12-09 03:05:46 +00:00
|
|
|
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): ${object.error.message} ${object.error.data}`));
|
2017-08-09 23:14:00 +00:00
|
|
|
else
|
|
|
|
callback.resolve(object.result);
|
|
|
|
} else {
|
|
|
|
console.assert(!object.id);
|
|
|
|
this.emit(object.method, object.params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 03:33:22 +00:00
|
|
|
async detach() {
|
|
|
|
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-01-11 03:33:22 +00:00
|
|
|
helper.tracePublicAPI(CDPSession);
|
2017-08-09 23:14:00 +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};
|