summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉmilien Devos <contact@emiliendevos.be>2021-04-14 00:12:02 +0200
committerGitHub <noreply@github.com>2021-04-14 00:12:02 +0200
commitfb8c05c9644e80eb6aeb25de2f095f3dbd75e655 (patch)
tree09423452f729934a45a2d4ea965ec1bf560a57c2
parent143a82299be4b581bc97198678982ec1e032cda4 (diff)
parent99be879a64ff068263082088d98b475d3be4692c (diff)
Merge pull request #18 from ahmubashshir/fix-16-13
Return ResponseBuffer and close tab after each request.
-rw-r--r--index.js12
1 files changed, 8 insertions, 4 deletions
diff --git a/index.js b/index.js
index 3fd1d00..a266cd6 100644
--- a/index.js
+++ b/index.js
@@ -29,6 +29,7 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive"
if (ctx.query.url) {
const url = ctx.url.replace("/?url=", "");
let responseBody;
+ let responseData;
let responseHeaders;
const page = await browser.newPage();
if (ctx.method == "POST") {
@@ -58,7 +59,7 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive"
interceptionId: e.interceptionId
}).then((result) => {
if (result.base64Encoded) {
- responseBody = Buffer.from(result.body, 'base64');
+ responseData = Buffer.from(result.body, 'base64');
}
});
obj['errorReason'] = 'BlockedByClient';
@@ -78,9 +79,11 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive"
let tryCount = 0;
response = await page.goto(url, { timeout: 30000, waitUntil: 'domcontentloaded' });
responseBody = await response.text();
+ responseData = await response.buffer();
while (responseBody.includes("cf-browser-verification") && tryCount <= 10) {
response = await page.waitForNavigation({ timeout: 30000, waitUntil: 'domcontentloaded' });
responseBody = await response.text();
+ responseData = await response.buffer();
tryCount++;
}
responseHeaders = response.headers();
@@ -90,20 +93,21 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive"
const { name, value, secure, expires, domain, ...options } = cookie;
ctx.cookies.set(cookie.name, cookie.value, options);
});
- await page.close();
} catch (error) {
if (!error.toString().includes("ERR_BLOCKED_BY_CLIENT")) {
ctx.status = 500;
ctx.body = error;
}
}
+
+ await page.close();
responseHeadersToRemove.forEach(header => delete responseHeaders[header]);
Object.keys(responseHeaders).forEach(header => ctx.set(header, jsesc(responseHeaders[header])));
- ctx.body = responseBody;
+ ctx.body = responseData;
}
else {
ctx.body = "Please specify the URL in the 'url' query string.";
}
});
app.listen(3000);
-})(); \ No newline at end of file
+})();