onPacket: function(packet) { // Pick off event packets const type = packet.type || undefined; if (this._clientSpec.events && this._clientSpec.events.has(type)) { const event = this._clientSpec.events.get(packet.type); let args; try { args = event.request.read(packet, this); } catch (ex) { console.error("Error reading event: " + packet.type); console.exception(ex); throw ex; } if (event.pre) { const results = event.pre.map(pre => pre.apply(this, args)); // Check to see if any of the preEvents returned a promise -- if so, // wait for their resolution before emitting. Otherwise, emit synchronously. if (results.some(result => result && typeof result.then === "function")) { Promise.all(results).then(() => { return EventEmitter.emit.apply(null, [this, event.name].concat(args)); }); return; } } EventEmitter.emit.apply(null, [this, event.name].concat(args)); return; } // Remaining packets must be responses. if (this._requests.length === 0) { const msg = "Unexpected packet " + this.actorID + ", " + JSON.stringify(packet); const err = Error(msg); console.error(err); throw err; } const { deferred, stack } = this._requests.shift(); callFunctionWithAsyncStack(() => { if (packet.error) { // "Protocol error" is here to avoid TBPL heuristics. See also // https://dxr.mozilla.org/webtools-central/source/tbpl/php/inc/GeneralErrorFilter.php let message; if (packet.error && packet.message) { message = "Protocol error (" + packet.error + "): " + packet.message; } else { message = packet.error; } deferred.reject(message); } else { deferred.resolve(packet); } }, stack, "DevTools RDP"); },
Promise.all(results).then(() => { return EventEmitter.emit.apply(null, [this, event.name].concat(args)); });