Skip to content

Commit

Permalink
Merge pull request #291 from pixeleye-io/upgrading-rrweb-snapshot
Browse files Browse the repository at this point in the history
Upgrading rrweb snapshot
  • Loading branch information
AlfieJones authored May 22, 2024
2 parents b7f52dc + 7758352 commit bd89dbb
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 293 deletions.
11 changes: 0 additions & 11 deletions .changeset/heavy-eggs-dream.md

This file was deleted.

11 changes: 0 additions & 11 deletions .changeset/moody-cameras-talk.md

This file was deleted.

35 changes: 0 additions & 35 deletions .changeset/pre.json

This file was deleted.

10 changes: 10 additions & 0 deletions .changeset/shaggy-snakes-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@pixeleye/cli-capture": minor
"@pixeleye/cli-booth": minor
"pixeleye": minor
"@pixeleye/playwright": minor
"@pixeleye/puppeteer": minor
"@pixeleye/cypress": minor
---

Adding support for HTML canvas elements
3 changes: 1 addition & 2 deletions configs/esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as esbuild from "esbuild";


const forceExternal = [
"jsdom",
"playwright-core",
"playwright-core/lib/server"
]
Expand Down Expand Up @@ -30,7 +29,7 @@ export function build(entryPoints, outdir, banner = {}, formats = ["cjs", "esm"]
minify: false,
platform: "node",
target: "node20",
external: devBuild ? ["jsdom", "playwright-core", "rrweb-snapshot/dist/rrweb-snapshot.min.js"] : [],
external: devBuild ? ["playwright-core", "rrweb-snapshot/dist/rrweb-snapshot.min.js"] : [],
outdir,
format,
outExtension: {
Expand Down
2 changes: 1 addition & 1 deletion integrations/cli/cli-booth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pixeleye/cli-booth",
"version": "0.5.7",
"version": "0.5.8",
"private": false,
"license": "AGPL-3.0",
"main": "./src/index.ts",
Expand Down
10 changes: 1 addition & 9 deletions integrations/cli/cli-booth/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import bodyParser from "body-parser";
import { Build } from "@pixeleye/api";
import { getEnvConfig } from "@pixeleye/cli-config";
import { QueuedSnap, handleQueue, queue } from "./snapshotQueue";
import {
CaptureScreenshotData,
getBrowser,
getBuildContent,
} from "@pixeleye/cli-capture";
import { CaptureScreenshotData, getBrowser } from "@pixeleye/cli-capture";
import { createBus } from "./bus";
import { API, uploadSnapshots } from "@pixeleye/cli-api";
import { serializedNodeWithId } from "rrweb-snapshot";
Expand Down Expand Up @@ -86,10 +82,6 @@ export function startServer(options: BoothServerOptions) {
app.post("/snapshot", (req, res) => {
const body = req.body as SnapshotRequest;

body.content = body.serializedDom
? getBuildContent(body.serializedDom)
: undefined;

body.devices.forEach((device) => {
queue.add(async () =>
handleQueue({
Expand Down
3 changes: 3 additions & 0 deletions integrations/cli/cli-booth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@pixeleye/tsconfig/node.json",
"include": ["."],
"compilerOptions": {
"lib": ["dom"]
},
"exclude": ["dist", "build", "node_modules"]
}
8 changes: 3 additions & 5 deletions integrations/cli/cli-capture/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pixeleye/cli-capture",
"version": "0.5.1",
"version": "0.5.2",
"private": false,
"license": "AGPL-3.0",
"main": "./src/index.ts",
Expand Down Expand Up @@ -35,10 +35,8 @@
"dependencies": {
"@pixeleye/cli-devices": "workspace:*",
"@pixeleye/cli-logger": "workspace:*",
"@types/jsdom": "^21.1.6",
"jsdom": "^24.0.0",
"rrweb-snapshot": "2.0.0-alpha.13",
"object-hash": "^3.0.0",
"playwright-core": "^1.43.0",
"rrweb-snapshot": "2.0.0-alpha.13"
"playwright-core": "^1.43.0"
}
}
53 changes: 33 additions & 20 deletions integrations/cli/cli-capture/src/capture.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {
createCache,
createMirror,
rebuild,
serializedNodeWithId,
} from "rrweb-snapshot";
import rrweb, { serializedNodeWithId } from "rrweb-snapshot";
import { getPage } from "./browsers";
import { DeviceDescriptor } from "@pixeleye/cli-devices";
import { SnapshotDefinition, defaultConfig } from "@pixeleye/cli-config";
import { JSDOM } from "jsdom";
import { logger } from "@pixeleye/cli-logger";
import { Page } from "playwright-core";
import { createRequire } from "module";

let rrwebScript: string | undefined;
try {
rrwebScript = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
} catch {
const require = createRequire(import.meta.url);
rrwebScript = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
}

type RRWeb = typeof rrweb;

const blankPage = "<!DOCTYPE html><html><head></head><body></body></html>";

type Only<T, U> = {
[P in keyof T]: T[P];
Expand All @@ -30,16 +37,7 @@ export type CaptureScreenshotData<
> = T extends string | number | symbol
? Omit<CaptureScreenshotConfigOptions, T>
: CaptureScreenshotConfigOptions &
Either<{ url: string }, { content: string }>;

export function getBuildContent(serializedDom: serializedNodeWithId): string {
const doc = new JSDOM().window.document;
const cache = createCache();
const mirror = createMirror();
rebuild(serializedDom, { doc, cache, mirror });

return `<!DOCTYPE html>${doc.documentElement.outerHTML}`;
}
Either<{ url: string }, { serializedDom: serializedNodeWithId }>;

const retries = 3;

Expand Down Expand Up @@ -80,10 +78,25 @@ async function internalCaptureScreenshot(
await page.goto(data.url, {
timeout: 60_000,
});
} else if (data.content) {
await page.setContent(data.content, {
timeout: 60_000,
} else if (data.serializedDom) {
await page.setContent(blankPage);

await (page as Page).addScriptTag({
path: rrwebScript,
});

await page.evaluate((serializedDom) => {
const r: RRWeb = (window as any).rrwebSnapshot;

const cache = r.createCache();
const mirror = r.createMirror();

r.rebuild(serializedDom, {
doc: document,
cache,
mirror,
});
}, data.serializedDom);
} else {
await page.close();
throw new Error("No url or serializedDom provided");
Expand Down
3 changes: 3 additions & 0 deletions integrations/cli/cli-capture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@pixeleye/tsconfig/node.json",
"include": ["."],
"compilerOptions": {
"lib": ["dom"]
},
"exclude": ["dist", "build", "node_modules"]
}
3 changes: 3 additions & 0 deletions integrations/cli/pixeleye/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@pixeleye/tsconfig/node.json",
"include": ["."],
"compilerOptions": {
"lib": ["es2021", "dom"]
},
"exclude": ["dist", "bin", "build", "node_modules"]
}
1 change: 1 addition & 0 deletions integrations/cypress/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const pixeleyeSnapshot = (options: Options) => {
const serializedDom = domSnapshot(doc, {
recordCanvas: true,
inlineImages: true,
inlineStylesheet: true,
});
if (!serializedDom) {
throw new Error("Failed to serialize DOM");
Expand Down
3 changes: 2 additions & 1 deletion integrations/cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"include": ["."],
"compilerOptions": {
"types": ["cypress"],
"lib": ["dom"]
},
"exclude": ["dist", "build", "node_modules"],
"exclude": ["dist", "build", "node_modules"]
}
25 changes: 13 additions & 12 deletions integrations/playwright/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import {
SnapshotRequest,
} from "@pixeleye/cli-booth";
import { createRequire } from "node:module";
import { snapshot as rrwebSnapshotFn } from "rrweb-snapshot";
import rrweb from "rrweb-snapshot";

let rrwebSnapshot: string | undefined;
let rrwebScript: string | undefined;
try {
rrwebSnapshot = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
rrwebScript = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
} catch {
const require = createRequire(import.meta.url);
rrwebSnapshot = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
rrwebScript = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
}

type RRWeb = typeof rrweb;

export interface Options {
name: string;
fullPage?: boolean;
Expand Down Expand Up @@ -57,20 +59,19 @@ export async function pixeleyeSnapshot(page: Page, options: Options) {
: undefined;

await (page as Page).addScriptTag({
path: rrwebSnapshot,
path: rrwebScript,
});

if (options.waitForSelector)
await page.waitForSelector(options.waitForSelector);

const domSnapshot = await (page as Page).evaluate(() => {
return ((rrwebSnapshot as any).snapshot as typeof rrwebSnapshotFn)(
document,
{
recordCanvas: true,
inlineImages: true,
}
);
const r: RRWeb = (window as any).rrwebSnapshot;
return r.snapshot(document, {
recordCanvas: true,
inlineImages: true,
inlineStylesheet: true,
});
});

if (!domSnapshot) {
Expand Down
5 changes: 4 additions & 1 deletion integrations/playwright/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@pixeleye/tsconfig/node.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"lib": ["dom"]
},
"exclude": ["dist", "build", "node_modules"]
}
35 changes: 19 additions & 16 deletions integrations/puppeteer/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import {
} from "@pixeleye/cli-booth";
import { createRequire } from "node:module";
import { snapshot as rrwebSnapshotFn } from "rrweb-snapshot";
import rrweb from "rrweb-snapshot";

type RRWeb = typeof rrweb;

let rrwebScript: string | undefined;
try {
rrwebScript = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
} catch {
const require = createRequire(import.meta.url);
rrwebScript = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
}

export interface Options {
fullPage?: boolean;
Expand All @@ -22,14 +33,6 @@ export interface Options {
wait?: number;
}

let rrwebSnapshot: string | undefined;
try {
rrwebSnapshot = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
} catch {
const require = createRequire(import.meta.url);
rrwebSnapshot = require.resolve("rrweb-snapshot/dist/rrweb-snapshot.min.js");
}

export async function pixeleyeSnapshot(
page: Page | PageCore,
options: Options
Expand Down Expand Up @@ -60,17 +63,17 @@ export async function pixeleyeSnapshot(
: undefined;

await (page as Page).addScriptTag({
path: rrwebSnapshot,
path: rrwebScript,
});

const domSnapshot = await (page as Page).evaluate(() => {
return ((rrwebSnapshot as any).snapshot as typeof rrwebSnapshotFn)(
document,
{
recordCanvas: true,
inlineImages: true,
}
);
const r: RRWeb = (window as any).rrwebSnapshot;

return r.snapshot(document, {
recordCanvas: true,
inlineImages: true,
inlineStylesheet: true,
});
});

if (!domSnapshot) {
Expand Down
3 changes: 3 additions & 0 deletions integrations/puppeteer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@pixeleye/tsconfig/node.json",
"include": ["."],
"compilerOptions": {
"lib": ["dom"]
},
"exclude": ["dist", "build", "node_modules"]
}
Loading

0 comments on commit bd89dbb

Please sign in to comment.