record canvas mutations (#296)

* record canvas mutations

close #60, #261

This patch implements the canvas mutation observer.
It consists of both the record and the replay side changes.

In the record side, we add a `recordCanvas` flag to indicate
whether to record canvas elements and the flag defaults to false.
Different from our other observers, the canvas observer was
disabled by default. Because some applications with heavy canvas
usage may emit a lot of data as canvas changed, especially the
scenarios that use a lot of `drawImage` API.
So the behavior should be audited by users and only record canvas
when the flag was set to true.

In the replay side, we add a `UNSAFE_replayCanvas` flag to indicate
whether to replay canvas mutations.
Similar to the `recordCanvas` flag, `UNSAFE_replayCanvas` defaults
to false. But unlike the record canvas implementation is stable and
safe, the replay canvas implementation is UNSAFE.
It's unsafe because we need to add `allow-scripts` to the replay
sandbox, which may cause some unexpected script execution. Currently,
users should be aware of this implementation detail and enable this
feature carefully.

* update canvas integration test
This commit is contained in:
yz-yu
2020-08-22 16:44:02 +08:00
committed by GitHub
parent a9719e302e
commit 772c0e021a
11 changed files with 528 additions and 31 deletions

View File

@@ -81,7 +81,7 @@ function getCode(): string {
width: 1600,
height: 900,
},
args: ['--start-maximized'],
args: ['--start-maximized', '--ignore-certificate-errors'],
});
const page = await browser.newPage();
await page.goto(url, {
@@ -94,7 +94,8 @@ function getCode(): string {
await page.evaluate(`;${code}
window.__IS_RECORDING__ = true
rrweb.record({
emit: event => window._replLog(event)
emit: event => window._replLog(event),
recordCanvas: true
});
`);
page.on('framenavigated', async () => {
@@ -103,14 +104,14 @@ function getCode(): string {
await page.evaluate(`;${code}
window.__IS_RECORDING__ = true
rrweb.record({
emit: event => window._replLog(event)
emit: event => window._replLog(event),
recordCanvas: true
});
`);
}
});
emitter.once('done', async shouldReplay => {
console.log(`Recorded ${events.length} events`);
emitter.once('done', async (shouldReplay) => {
await browser.close();
if (shouldReplay) {
await replay();
@@ -170,7 +171,9 @@ function getCode(): string {
'<\\/script>',
)};
/*-->*/
const replayer = new rrweb.Replayer(events);
const replayer = new rrweb.Replayer(events, {
UNSAFE_replayCanvas: true
});
replayer.play();
</script>
</body>
@@ -182,10 +185,10 @@ function getCode(): string {
}
process
.on('uncaughtException', error => {
.on('uncaughtException', (error) => {
console.error(error);
})
.on('unhandledRejection', error => {
.on('unhandledRejection', (error) => {
console.error(error);
});
})();