Backport changes from Cloudproxy (#11)

This commit is contained in:
Alexandre Beloin
2020-12-12 17:09:03 -05:00
committed by GitHub
parent 5ed7c09160
commit a422756ae6
18 changed files with 3918 additions and 322 deletions

35
src/captcha/index.ts Normal file
View File

@@ -0,0 +1,35 @@
export enum CaptchaType {
re = 'reCaptcha',
h = 'hCaptcha'
}
export interface SolverOptions {
url: string
sitekey: string
type: CaptchaType
}
export type Solver = (options: SolverOptions) => Promise<string>
const captchaSolvers: { [key: string]: Solver } = {}
export default (): Solver => {
const method = process.env.CAPTCHA_SOLVER
if (!method) { return null }
if (!(method in captchaSolvers)) {
try {
captchaSolvers[method] = require('./' + method).default as Solver
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw Error(`The solver '${method}' is not a valid captcha solving method.`)
} else {
console.error(e)
throw Error(`An error occured loading the solver '${method}'.`)
}
}
}
return captchaSolvers[method]
}