nicer logs
This commit is contained in:
@@ -29,7 +29,54 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {})
|
|||||||
}>()
|
}>()
|
||||||
let extensionMessageId = 0
|
let extensionMessageId = 0
|
||||||
|
|
||||||
function sendToPlaywright(message: CDPResponse | CDPEvent, source: 'extension' | 'server' = 'extension') {
|
function logCdpMessage({
|
||||||
|
direction,
|
||||||
|
method,
|
||||||
|
sessionId,
|
||||||
|
params,
|
||||||
|
id,
|
||||||
|
source
|
||||||
|
}: {
|
||||||
|
direction: 'to-playwright' | 'from-playwright' | 'from-extension'
|
||||||
|
method: string
|
||||||
|
sessionId?: string
|
||||||
|
params?: any
|
||||||
|
id?: number
|
||||||
|
source?: 'extension' | 'server'
|
||||||
|
}) {
|
||||||
|
const details: string[] = []
|
||||||
|
|
||||||
|
if (id !== undefined) {
|
||||||
|
details.push(`id=${id}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sessionId) {
|
||||||
|
details.push(`sessionId=${sessionId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params) {
|
||||||
|
if (params.targetId) {
|
||||||
|
details.push(`targetId=${params.targetId}`)
|
||||||
|
}
|
||||||
|
if (params.sessionId && params.sessionId !== sessionId) {
|
||||||
|
details.push(`sessionIdParam=${params.sessionId}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const detailsStr = details.length > 0 ? ` ${chalk.gray(details.join(', '))}` : ''
|
||||||
|
|
||||||
|
if (direction === 'from-playwright') {
|
||||||
|
console.log(chalk.cyan('← Playwright:'), method + detailsStr)
|
||||||
|
} else if (direction === 'from-extension') {
|
||||||
|
console.log(chalk.yellow('← Extension:'), method + detailsStr)
|
||||||
|
} else if (direction === 'to-playwright') {
|
||||||
|
const color = source === 'server' ? chalk.magenta : chalk.green
|
||||||
|
const sourceLabel = source === 'server' ? chalk.gray(' (server-generated)') : ''
|
||||||
|
console.log(color('→ Playwright:'), method + detailsStr + sourceLabel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendToPlaywright({ message, source = 'extension' }: { message: CDPResponse | CDPEvent; source?: 'extension' | 'server' }) {
|
||||||
if (!playwrightWs) {
|
if (!playwrightWs) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -39,8 +86,13 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {})
|
|||||||
: message
|
: message
|
||||||
|
|
||||||
if ('method' in message) {
|
if ('method' in message) {
|
||||||
const color = source === 'server' ? chalk.magenta : chalk.green
|
logCdpMessage({
|
||||||
console.log(color('→ Playwright:'), message.method, source === 'server' ? chalk.gray('(server-generated)') : '')
|
direction: 'to-playwright',
|
||||||
|
method: message.method,
|
||||||
|
sessionId: 'sessionId' in message ? message.sessionId : undefined,
|
||||||
|
params: 'params' in message ? message.params : undefined,
|
||||||
|
source
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
playwrightWs.send(JSON.stringify(messageToSend))
|
playwrightWs.send(JSON.stringify(messageToSend))
|
||||||
@@ -155,15 +207,22 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {})
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(chalk.cyan('← Playwright:'), `${message.method} (id=${message.id})`)
|
|
||||||
|
|
||||||
const { id, sessionId, method, params } = message
|
const { id, sessionId, method, params } = message
|
||||||
|
|
||||||
|
logCdpMessage({
|
||||||
|
direction: 'from-playwright',
|
||||||
|
method,
|
||||||
|
sessionId,
|
||||||
|
id
|
||||||
|
})
|
||||||
|
|
||||||
if (!extensionWs) {
|
if (!extensionWs) {
|
||||||
sendToPlaywright({
|
sendToPlaywright({
|
||||||
id,
|
message: {
|
||||||
sessionId,
|
id,
|
||||||
error: { message: 'Extension not connected' }
|
sessionId,
|
||||||
|
error: { message: 'Extension not connected' }
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -174,26 +233,31 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {})
|
|||||||
if (method === 'Target.setAutoAttach' && !sessionId) {
|
if (method === 'Target.setAutoAttach' && !sessionId) {
|
||||||
for (const target of connectedTargets.values()) {
|
for (const target of connectedTargets.values()) {
|
||||||
sendToPlaywright({
|
sendToPlaywright({
|
||||||
method: 'Target.attachedToTarget',
|
message: {
|
||||||
params: {
|
method: 'Target.attachedToTarget',
|
||||||
sessionId: target.sessionId,
|
params: {
|
||||||
targetInfo: {
|
sessionId: target.sessionId,
|
||||||
...target.targetInfo,
|
targetInfo: {
|
||||||
attached: true
|
...target.targetInfo,
|
||||||
},
|
attached: true
|
||||||
waitingForDebugger: false
|
},
|
||||||
}
|
waitingForDebugger: false
|
||||||
} satisfies CDPEvent, 'server')
|
}
|
||||||
|
} satisfies CDPEvent,
|
||||||
|
source: 'server'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendToPlaywright({ id, sessionId, result })
|
sendToPlaywright({ message: { id, sessionId, result } })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error handling CDP command:', e)
|
console.error('Error handling CDP command:', e)
|
||||||
sendToPlaywright({
|
sendToPlaywright({
|
||||||
id,
|
message: {
|
||||||
sessionId,
|
id,
|
||||||
error: { message: (e as Error).message }
|
sessionId,
|
||||||
|
error: { message: (e as Error).message }
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -258,7 +322,12 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {})
|
|||||||
|
|
||||||
const { method, params, sessionId } = extensionEvent.params
|
const { method, params, sessionId } = extensionEvent.params
|
||||||
|
|
||||||
console.log(chalk.yellow('← Extension:'), method)
|
logCdpMessage({
|
||||||
|
direction: 'from-extension',
|
||||||
|
method,
|
||||||
|
sessionId,
|
||||||
|
params
|
||||||
|
})
|
||||||
|
|
||||||
if (method === 'Target.attachedToTarget') {
|
if (method === 'Target.attachedToTarget') {
|
||||||
const targetParams = params as Protocol.Target.AttachedToTargetEvent
|
const targetParams = params as Protocol.Target.AttachedToTargetEvent
|
||||||
@@ -269,23 +338,32 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {})
|
|||||||
})
|
})
|
||||||
|
|
||||||
sendToPlaywright({
|
sendToPlaywright({
|
||||||
method: 'Target.attachedToTarget',
|
message: {
|
||||||
params: targetParams
|
method: 'Target.attachedToTarget',
|
||||||
} as CDPEvent, 'extension')
|
params: targetParams
|
||||||
|
} as CDPEvent,
|
||||||
|
source: 'extension'
|
||||||
|
})
|
||||||
} else if (method === 'Target.detachedFromTarget') {
|
} else if (method === 'Target.detachedFromTarget') {
|
||||||
const detachParams = params as Protocol.Target.DetachedFromTargetEvent
|
const detachParams = params as Protocol.Target.DetachedFromTargetEvent
|
||||||
connectedTargets.delete(detachParams.sessionId)
|
connectedTargets.delete(detachParams.sessionId)
|
||||||
|
|
||||||
sendToPlaywright({
|
sendToPlaywright({
|
||||||
method: 'Target.detachedFromTarget',
|
message: {
|
||||||
params: detachParams
|
method: 'Target.detachedFromTarget',
|
||||||
} as CDPEvent, 'extension')
|
params: detachParams
|
||||||
|
} as CDPEvent,
|
||||||
|
source: 'extension'
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
sendToPlaywright({
|
sendToPlaywright({
|
||||||
sessionId,
|
message: {
|
||||||
method,
|
sessionId,
|
||||||
params
|
method,
|
||||||
} as CDPEvent, 'extension')
|
params
|
||||||
|
} as CDPEvent,
|
||||||
|
source: 'extension'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user