Skip to content

Commit

Permalink
Rewrite request timeout handling (maxTimeout) resolves #42
Browse files Browse the repository at this point in the history
  • Loading branch information
ngosang committed Dec 20, 2020
1 parent d2b6805 commit a23fa09
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"url": "https://github.com/ngosang/FlareSolverr"
},
"dependencies": {
"await-timeout": "^1.1.1",
"console-log-level": "^1.4.1",
"got": "^11.5.1",
"hcaptcha-solver": "^1.0.2",
Expand All @@ -29,6 +30,7 @@
"uuid": "^8.2.0"
},
"devDependencies": {
"@types/await-timeout": "^0.3.1",
"@types/node": "^14.0.23",
"@types/puppeteer": "^3.0.1",
"@types/uuid": "^8.0.0",
Expand Down
28 changes: 18 additions & 10 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SetCookie, Request, Headers, HttpMethod, Overrides, Cookie } from 'pupp
import { TimeoutError } from 'puppeteer/Errors'
import getCaptchaSolver, { CaptchaType } from './captcha'
import * as Puppeteer from "puppeteer-extra/dist/puppeteer";
const Timeout = require('await-timeout');

export interface BaseAPICall {
cmd: string
Expand Down Expand Up @@ -119,9 +120,22 @@ async function interceptResponse(page: Puppeteer.Page, callback: (payload: Chall
});
}

async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, download, returnOnlyCookies }: BaseRequestAPICall, page: Puppeteer.Page): Promise<ChallengeResolutionT | void> {
async function resolveChallengeWithTimeout(ctx: RequestContext, params: BaseRequestAPICall, page: Puppeteer.Page) {
const maxTimeout = params.maxTimeout || 60000
const timer = new Timeout();
try {
const promise = resolveChallenge(ctx, params, page);
return await Promise.race([
promise,
timer.set(maxTimeout, `Maximum timeout reached. maxTimeout=${maxTimeout} (ms)`)
]);
} finally {
timer.clear();
}
}

async function resolveChallenge(ctx: RequestContext, { url, proxy, download, returnOnlyCookies }: BaseRequestAPICall, page: Puppeteer.Page): Promise<ChallengeResolutionT | void> {

maxTimeout = maxTimeout || 60000
let status = 'ok'
let message = ''

Expand Down Expand Up @@ -162,8 +176,7 @@ async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, d
});
}

// TODO: find out why these pages hang sometimes
while (Date.now() - ctx.startTimestamp < maxTimeout) {
while (true) {
await page.waitFor(1000)
try {
// catch exception timeout in waitForNavigation
Expand All @@ -188,11 +201,6 @@ async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, d
log.html(await page.content())
}

if (Date.now() - ctx.startTimestamp >= maxTimeout) {
ctx.errorResponse(`Maximum timeout reached. maxTimeout=${maxTimeout} (ms)`)
return
}

log.debug('Validating HTML code...')
break
} else {
Expand Down Expand Up @@ -418,7 +426,7 @@ const browserRequest = async (ctx: RequestContext, params: BaseRequestAPICall) =

try {
const page = await setupPage(ctx, params, session.browser)
const data = await resolveChallenge(ctx, params, page)
const data = await resolveChallengeWithTimeout(ctx, params, page)

if (data) {
const { status } = data
Expand Down

0 comments on commit a23fa09

Please sign in to comment.