Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/screencast/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@
module.exports = (page, opts) => {
const cdp = page._client()
let onFrame
let isStopped = false

cdp.on('Page.screencastFrame', ({ data, metadata, sessionId }) => {
const onScreencastFrame = ({ data, metadata, sessionId }) => {
cdp.send('Page.screencastFrameAck', { sessionId }).catch(() => {})
if (metadata.timestamp) onFrame(data, metadata)
})
if (metadata.timestamp && typeof onFrame === 'function') onFrame(data, metadata)
}

cdp.on('Page.screencastFrame', onScreencastFrame)

return {
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-startScreencast
start: () => cdp.send('Page.startScreencast', opts),
onFrame: fn => (onFrame = fn),
stop: () => cdp.send('Page.stopScreencast').catch(() => {})
stop: () => {
if (isStopped) return Promise.resolve()
isStopped = true
if (typeof cdp.off === 'function') {
cdp.off('Page.screencastFrame', onScreencastFrame)
} else if (typeof cdp.removeListener === 'function') {
cdp.removeListener('Page.screencastFrame', onScreencastFrame)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screencast cannot restart after first stop

Medium Severity

stop() now permanently removes the Page.screencastFrame listener and flips isStopped, but start() never reattaches that listener or resets state. Reusing the same instance after one stop causes frames to stop being processed and acknowledged, and later stop() calls become no-ops for subsequent recording sessions.

Fix in Cursor Fix in Web

}
return cdp.send('Page.stopScreencast').catch(() => {})
}
}
}
69 changes: 69 additions & 0 deletions packages/screencast/test/unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const { EventEmitter } = require('events')
const test = require('ava')

const createScreencast = require('..')

test('it does not throw if frame arrives before onFrame is set', async t => {
const cdp = new EventEmitter()
const calls = []

cdp.send = async (method, payload) => {
calls.push({ method, payload })
}

const page = {
_client: () => cdp
}

const screencast = createScreencast(page, {})

t.notThrows(() => {
cdp.emit('Page.screencastFrame', {
data: 'frame',
metadata: { timestamp: 1 },
sessionId: 42
})
})

await Promise.resolve()
t.true(calls.some(({ method }) => method === 'Page.screencastFrameAck'))

let frameCalls = 0
screencast.onFrame(() => {
frameCalls += 1
})

cdp.emit('Page.screencastFrame', {
data: 'frame',
metadata: { timestamp: 2 },
sessionId: 43
})

t.is(frameCalls, 1)
})

test('stop removes screencast listener and is idempotent', async t => {
const cdp = new EventEmitter()
const calls = []

cdp.send = async (method, payload) => {
calls.push({ method, payload })
}

const page = {
_client: () => cdp
}

const screencast = createScreencast(page, {})
t.is(cdp.listenerCount('Page.screencastFrame'), 1)

await screencast.stop()
t.is(cdp.listenerCount('Page.screencastFrame'), 0)

await screencast.stop()
t.is(cdp.listenerCount('Page.screencastFrame'), 0)

t.is(calls.filter(({ method }) => method === 'Page.stopScreencast').length, 1)
})
Loading