Commit 501b2ce3 authored by Mathis Poncet's avatar Mathis Poncet
Browse files

fix(#369)-feat: infinite loop cause by requestInterception and load file as buffer

parent df24df75
Pipeline #32761 failed with stages
in 43 seconds
......@@ -58,7 +58,7 @@ class Printer extends EventEmitter {
return this.browser;
}
async render(input) {
async render(html) {
let resolver;
let rendered = new Promise(function(resolve, reject) {
resolver = resolve;
......@@ -77,72 +77,7 @@ class Printer extends EventEmitter {
page._client.send('Emulation.setDefaultBackgroundColorOverride', { color: this.overrideDefaultBackgroundColor });
}
let uri, url, relativePath, html;
if (typeof input === "string") {
try {
uri = new URL(input);
url = input;
} catch (error) {
relativePath = path.resolve(dir, input);
if (this.browserWSEndpoint) {
html = fs.readFileSync(relativePath, 'utf-8')
} else {
url = "file://" + relativePath;
}
}
} else {
url = input.url;
html = input.html;
}
await page.setRequestInterception(true);
page.on('request', (request) => {
let uri = new URL(request.url());
let { host, protocol, pathname } = uri;
let local = protocol === "file:"
if (local && this.withinAllowedPath(pathname) === false) {
request.abort();
return;
}
if (local && !this.allowLocal) {
request.abort();
return;
}
if (host && this.isAllowedDomain(host) === false) {
request.abort();
return;
}
if (host && !this.allowRemote) {
request.abort();
return;
}
request.continue();
});
if (html) {
await page.setContent(html);
if (url) {
await page.evaluate((url) => {
let base = document.querySelector("base");
if (!base) {
base = document.createElement("base");
document.querySelector("head").appendChild(base);
}
base.setAttribute("href", url);
}, url);
}
} else {
await page.goto(url);
}
await page.setContent(html, { waitUntil: "networkidle0" });
await page.evaluate(() => {
window.PagedConfig = window.PagedConfig || {};
......@@ -154,7 +89,6 @@ class Printer extends EventEmitter {
await page.addScriptTag({
path: scriptPath
});
for (const script of this.additionalScripts) {
await page.addScriptTag({
path: script
......@@ -290,7 +224,8 @@ class Printer extends EventEmitter {
}
async pdf(input, options={}) {
let page = await this.render(input)
let bufferInput = fs.readFileSync(input, "utf8");
let page = await this.render(bufferInput, options)
.catch((e) => {
throw e;
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment