Skip to content

Commit

Permalink
Convert mhtml to html behind a feature toggle (usehMhtmlConverter)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmatter committed Nov 19, 2024
1 parent 8f7a1eb commit 740d656
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
20 changes: 18 additions & 2 deletions server/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function buildFastify(renderFn, compareFn) {
fullpage: { type: "boolean" },
viewport: { $ref: "#viewport" },
style: { type: "string" },
usehMhtmlConverter: { type: "boolean" },
},
required: ["actualHtml", "expected", "viewport"],
});
Expand All @@ -43,6 +44,7 @@ export function buildFastify(renderFn, compareFn) {
fullpage: { type: "boolean" },
viewport: { $ref: "#viewport" },
style: { type: "string" },
usehMhtmlConverter: { type: "boolean" },
},
required: ["actualHtml", "viewport"],
});
Expand All @@ -59,8 +61,15 @@ export function buildFastify(renderFn, compareFn) {
const viewport = request.body.viewport;
const fullpage = request.body.fullpage ?? false;
const style = request.body.style;
const usehMhtmlConverter = request.body.usehMhtmlConverter ?? false;

const actual = await renderFn(actualHtml, viewport, fullpage, style);
const actual = await renderFn(
actualHtml,
viewport,
fullpage,
style,
usehMhtmlConverter,
);
const result = await compareFn(expected, actual);

return {
Expand All @@ -83,8 +92,15 @@ export function buildFastify(renderFn, compareFn) {
const viewport = request.body.viewport;
const fullpage = request.body.fullpage ?? false;
const style = request.body.style;
const usehMhtmlConverter = request.body.usehMhtmlConverter ?? false;

const actual = await renderFn(actualHtml, viewport, fullpage, style);
const actual = await renderFn(
actualHtml,
viewport,
fullpage,
style,
usehMhtmlConverter,
);

return {
actual: actual.toString("base64"),
Expand Down
24 changes: 21 additions & 3 deletions server/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@ import path from "node:path";
import os from "node:os";
import { chromium } from "playwright";
import pino from "pino";
import mhtml2html from "mhtml2html";
import { JSDOM } from "jsdom";

const logger = pino({
level: process.env.LOG_LEVEL || "info",
});

export async function render(actualMhtml, viewport, fullpage, style) {
export async function render(
actualMhtml,
viewport,
fullpage,
style,
usehMhtmlConverter,
) {
const renderer = new BrowserRenderer();
const workspaceDirectory = await fs.mkdtemp(
path.join(os.tmpdir(), "pixelpact-"),
);
const indexFile = `${workspaceDirectory}/index.mhtml`;
await fs.writeFile(indexFile, actualMhtml);
let indexFile;
if (usehMhtmlConverter) {
indexFile = `${workspaceDirectory}/index.html`;
const convertedHtml = mhtml2html.convert(actualMhtml, {
parseDOM: (html) => new JSDOM(html),
});
const actualHtml = convertedHtml.serialize();
await fs.writeFile(indexFile, actualHtml);
} else {
indexFile = `${workspaceDirectory}/index.mhtml`;
await fs.writeFile(indexFile, actualMhtml);
}
try {
await renderer.start();
return await renderer.screenshot(
Expand Down

0 comments on commit 740d656

Please sign in to comment.