From 99be879a64ff068263082088d98b475d3be4692c Mon Sep 17 00:00:00 2001 From: Mubashshir Date: Fri, 2 Apr 2021 15:36:54 +0600 Subject: 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 --- index.js | 12 ++++++++---- 1 file 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 +})(); -- cgit v1.2.3