Skip to content

Commit

Permalink
chore: connecting to electron browser (#26471)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile authored Apr 13, 2023
1 parent 9c5fffb commit 66bd182
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/server/lib/browsers/cdp_automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import debugModule from 'debug'
import { URL } from 'url'

import type { ResourceType, BrowserPreRequest, BrowserResponseReceived } from '@packages/proxy'
import type { WriteVideoFrame } from '@packages/types'
import type { CDPClient, WriteVideoFrame } from '@packages/types'
import type { Automation } from '../automation'
import { cookieMatches, CyCookie, CyCookieFilter } from '../automation/util'

Expand Down Expand Up @@ -154,10 +154,16 @@ const ffToStandardResourceTypeMap: { [ff: string]: ResourceType } = {
'webmanifest': 'manifest',
}

export class CdpAutomation {
export class CdpAutomation implements CDPClient {
on: OnFn
send: SendDebuggerCommand

private constructor (private sendDebuggerCommandFn: SendDebuggerCommand, private onFn: OnFn, private sendCloseCommandFn: SendCloseCommand, private automation: Automation) {
onFn('Network.requestWillBeSent', this.onNetworkRequestWillBeSent)
onFn('Network.responseReceived', this.onResponseReceived)

this.on = onFn
this.send = sendDebuggerCommandFn
}

async startVideoRecording (writeVideoFrame: WriteVideoFrame, screencastOpts) {
Expand Down
9 changes: 4 additions & 5 deletions packages/server/lib/browsers/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as errors from '../errors'
import type { Browser, BrowserInstance } from './types'
import type { BrowserWindow, WebContents } from 'electron'
import type { Automation } from '../automation'
import type { BrowserLaunchOpts, Preferences, RunModeVideoApi } from '@packages/types'
import type { BrowserLaunchOpts, Preferences, ProtocolManager, RunModeVideoApi } from '@packages/types'
import memory from './memory'

// TODO: unmix these two types
Expand Down Expand Up @@ -202,9 +202,7 @@ export = {
win.maximize()
}

const launched = await this._launch(win, url, automation, preferences, options.videoApi)

automation.use(await _getAutomation(win, preferences, automation))
const launched = await this._launch(win, url, automation, preferences, options.videoApi, options.protocolManager)

return launched
},
Expand Down Expand Up @@ -235,7 +233,7 @@ export = {
return this._launch(win, url, automation, electronOptions)
},

async _launch (win: BrowserWindow, url: string, automation: Automation, options: ElectronOpts, videoApi?: RunModeVideoApi) {
async _launch (win: BrowserWindow, url: string, automation: Automation, options: ElectronOpts, videoApi?: RunModeVideoApi, protocolManager?: ProtocolManager) {
if (options.show) {
menu.set({ withInternalDevTools: true })
}
Expand Down Expand Up @@ -283,6 +281,7 @@ export = {
automation.use(cdpAutomation)

await Promise.all([
protocolManager?.connectToBrowser(cdpAutomation),
videoApi && recordVideo(cdpAutomation, videoApi),
this._handleDownloads(win, options.downloadsFolder, automation),
])
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Command<T extends keyof Commands> = Commands[T]
type Events = ProtocolMapping.Events
type Event<T extends keyof Events> = Events[T]

interface CDPClient {
export interface CDPClient {
send<T extends Extract<keyof Commands, string>> (command: T, params?: Command<T>['paramsType'][0]): Promise<Command<T>['returnType']>
on<T extends Extract<keyof Events, string>> (eventName: T, cb: (event: Event<T>[0]) => void): void
}
Expand Down

0 comments on commit 66bd182

Please sign in to comment.