diff options
author | Mubashshir <ahmubashshir@gmail.com> | 2021-04-02 15:36:54 +0600 |
---|---|---|
committer | Mubashshir <ahmubashshir@gmail.com> | 2021-04-02 15:39:04 +0600 |
commit | 99be879a64ff068263082088d98b475d3be4692c (patch) | |
tree | 09423452f729934a45a2d4ea965ec1bf560a57c2 | |
parent | 143a82299be4b581bc97198678982ec1e032cda4 (diff) |
Return ResponseBuffer and close tab after each request.
Fix for issue #16:
Always return `ResponseBuffer` as body instead of `ResponseText`.
Fix for issue #13:
Move `page.close()` out of try block, so that the tab closes before
returning the response.
Closes #16, Closes #13
-rw-r--r-- | index.js | 12 |
1 files changed, 8 insertions, 4 deletions
@@ -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 +})(); |