Skip to content

Commit

Permalink
Detect disconnected session (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh committed May 28, 2022
1 parent a12aee6 commit 8d4ef98
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/PuppeteerCaptureBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export abstract class PuppeteerCaptureBase extends EventEmitter implements Puppe
protected readonly _frameInterval: number
protected readonly _captureFrame: () => void
protected readonly _onPageClose: () => void
protected readonly _onSessionDisconnected: () => void
protected readonly _startStopMutex: Mutex
protected _target: string | Writable | null
protected _session: puppeteer.CDPSession | null
Expand Down Expand Up @@ -62,6 +63,7 @@ export abstract class PuppeteerCaptureBase extends EventEmitter implements Puppe
this._frameInterval = 1000.0 / this._options.fps
this._captureFrame = this.captureFrame.bind(this)
this._onPageClose = this.onPageClose.bind(this)
this._onSessionDisconnected = this.onSessionDisconnected.bind(this)
this._startStopMutex = new Mutex()
this._target = null
this._session = null
Expand Down Expand Up @@ -159,6 +161,7 @@ export abstract class PuppeteerCaptureBase extends EventEmitter implements Puppe
}

const session = await this._page.target().createCDPSession()
session.on('CDPSession.Disconnected', this._onSessionDisconnected)
await this.configureSession(session)

this._target = target
Expand Down Expand Up @@ -284,6 +287,10 @@ export abstract class PuppeteerCaptureBase extends EventEmitter implements Puppe

if (this._session != null) {
await this.deconfigureSession(this._session)
this._session.off('CDPSession.Disconnected', this._onSessionDisconnected)
if (this._session.connection() != null) {
await this._session.detach()
}
this._session = null
}

Expand Down Expand Up @@ -370,6 +377,13 @@ export abstract class PuppeteerCaptureBase extends EventEmitter implements Puppe
.catch(() => { })
}

protected onSessionDisconnected (): void {
this._error = new Error('Session was disconnected')
this._startStopMutex.runExclusive(async () => await this._stop())
.then(() => { })
.catch(() => { })
}

private static async findFfmpeg (): Promise<string> {
if (process.env.FFMPEG != null) {
return process.env.FFMPEG
Expand Down
13 changes: 13 additions & 0 deletions src/PuppeteerCaptureViaHeadlessExperimental.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,16 @@ test('that capture stops gracefully on page close', async () => {
await capture.stop()
}).rejects.toThrow('Page was closed')
})

test('that capture stops gracefully on session connection drop', async () => {
browser = await launch({ args: PUPPETEER_LAUNCH_ARGS })
const page = await browser.newPage()
const capture = new PuppeteerCaptureViaHeadlessExperimental(page)
const stream = new PassThrough()
await page.goto('https://google.com')
await capture.start(stream)
capture['_session']!.emit('CDPSession.Disconnected') // eslint-disable-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/dot-notation
await expect(async () => {
await capture.stop()
}).rejects.toThrow('Session was disconnected')
})
2 changes: 1 addition & 1 deletion src/PuppeteerCaptureViaHeadlessExperimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class PuppeteerCaptureViaHeadlessExperimental extends PuppeteerCaptureBas
}

protected override async deconfigureSession (session: puppeteer.CDPSession): Promise<void> {
if (!this._page.isClosed()) {
if (session.connection() != null) {
await session.send('HeadlessExperimental.disable')
}
}
Expand Down

0 comments on commit 8d4ef98

Please sign in to comment.