cdp: make logging available via optional cli flag

This commit is contained in:
Oj 2023-01-05 16:01:54 +00:00
parent 5f4d96b961
commit c15dca1967

View File

@ -1,6 +1,8 @@
import WebSocket from 'ws'; import WebSocket from 'ws';
import { get } from 'http'; import { get } from 'http';
const logCDP = process.argv.includes('--cdp-logging');
export default async ({ pipe: { pipeWrite, pipeRead } = {}, port }) => { export default async ({ pipe: { pipeWrite, pipeRead } = {}, port }) => {
let messageCallbacks = [], onReply = {}; let messageCallbacks = [], onReply = {};
const onMessage = msg => { const onMessage = msg => {
@ -8,7 +10,7 @@ export default async ({ pipe: { pipeWrite, pipeRead } = {}, port }) => {
msg = JSON.parse(msg); msg = JSON.parse(msg);
// log('received', msg); if (logCDP) log('received', msg);
if (onReply[msg.id]) { if (onReply[msg.id]) {
onReply[msg.id](msg); onReply[msg.id](msg);
delete onReply[msg.id]; delete onReply[msg.id];
@ -38,7 +40,7 @@ export default async ({ pipe: { pipeWrite, pipeRead } = {}, port }) => {
_send(JSON.stringify(msg)); _send(JSON.stringify(msg));
// log('sent', msg); if (logCDP) log('sent', msg);
const reply = await new Promise(res => { const reply = await new Promise(res => {
onReply[id] = msg => res(msg); onReply[id] = msg => res(msg);