@captchafox/node 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +17 -18
- package/dist/index.d.ts +17 -18
- package/dist/index.js +30 -5
- package/dist/index.mjs +30 -5
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
type ParseErrorProps = {
|
|
2
|
+
code?: number;
|
|
3
|
+
message: string;
|
|
4
|
+
body: string;
|
|
5
|
+
};
|
|
6
|
+
declare class ParseError extends Error {
|
|
7
|
+
code: number | undefined;
|
|
8
|
+
body: string;
|
|
9
|
+
constructor({ code, message, body }: ParseErrorProps);
|
|
10
|
+
}
|
|
11
|
+
|
|
1
12
|
/**
|
|
2
13
|
* The response returned from the /siteverify
|
|
3
14
|
* endpoint used for verifying the challenge token.
|
|
@@ -15,29 +26,17 @@ type VerifyResponse = {
|
|
|
15
26
|
/** List of error codes. Only included if success is false */
|
|
16
27
|
'error-codes'?: string[];
|
|
17
28
|
};
|
|
18
|
-
type
|
|
19
|
-
/** The organization secret */
|
|
20
|
-
secret: string;
|
|
21
|
-
/** The response token from the widget */
|
|
22
|
-
response: string;
|
|
29
|
+
type OptionalProps = {
|
|
23
30
|
/** (Optional) The sitekey that was used to issue the token */
|
|
24
31
|
sitekey?: string;
|
|
25
32
|
/** (Optional) The IP address of the requesting user */
|
|
26
33
|
remoteIp?: string;
|
|
34
|
+
retry?: {
|
|
35
|
+
attempts?: number;
|
|
36
|
+
};
|
|
27
37
|
};
|
|
28
|
-
declare function verify(secret: string, token: string,
|
|
29
|
-
|
|
30
|
-
type ParseErrorProps = {
|
|
31
|
-
code?: number;
|
|
32
|
-
message: string;
|
|
33
|
-
body: string;
|
|
34
|
-
};
|
|
35
|
-
declare class ParseError extends Error {
|
|
36
|
-
code: number | undefined;
|
|
37
|
-
body: string;
|
|
38
|
-
constructor({ code, message, body }: ParseErrorProps);
|
|
39
|
-
}
|
|
38
|
+
declare function verify(secret: string, token: string, options?: OptionalProps): Promise<VerifyResponse>;
|
|
40
39
|
|
|
41
40
|
declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
|
|
42
41
|
|
|
43
|
-
export { CAPTCHA_RESPONSE_KEY, ParseError, type
|
|
42
|
+
export { CAPTCHA_RESPONSE_KEY, ParseError, type VerifyResponse, verify };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
type ParseErrorProps = {
|
|
2
|
+
code?: number;
|
|
3
|
+
message: string;
|
|
4
|
+
body: string;
|
|
5
|
+
};
|
|
6
|
+
declare class ParseError extends Error {
|
|
7
|
+
code: number | undefined;
|
|
8
|
+
body: string;
|
|
9
|
+
constructor({ code, message, body }: ParseErrorProps);
|
|
10
|
+
}
|
|
11
|
+
|
|
1
12
|
/**
|
|
2
13
|
* The response returned from the /siteverify
|
|
3
14
|
* endpoint used for verifying the challenge token.
|
|
@@ -15,29 +26,17 @@ type VerifyResponse = {
|
|
|
15
26
|
/** List of error codes. Only included if success is false */
|
|
16
27
|
'error-codes'?: string[];
|
|
17
28
|
};
|
|
18
|
-
type
|
|
19
|
-
/** The organization secret */
|
|
20
|
-
secret: string;
|
|
21
|
-
/** The response token from the widget */
|
|
22
|
-
response: string;
|
|
29
|
+
type OptionalProps = {
|
|
23
30
|
/** (Optional) The sitekey that was used to issue the token */
|
|
24
31
|
sitekey?: string;
|
|
25
32
|
/** (Optional) The IP address of the requesting user */
|
|
26
33
|
remoteIp?: string;
|
|
34
|
+
retry?: {
|
|
35
|
+
attempts?: number;
|
|
36
|
+
};
|
|
27
37
|
};
|
|
28
|
-
declare function verify(secret: string, token: string,
|
|
29
|
-
|
|
30
|
-
type ParseErrorProps = {
|
|
31
|
-
code?: number;
|
|
32
|
-
message: string;
|
|
33
|
-
body: string;
|
|
34
|
-
};
|
|
35
|
-
declare class ParseError extends Error {
|
|
36
|
-
code: number | undefined;
|
|
37
|
-
body: string;
|
|
38
|
-
constructor({ code, message, body }: ParseErrorProps);
|
|
39
|
-
}
|
|
38
|
+
declare function verify(secret: string, token: string, options?: OptionalProps): Promise<VerifyResponse>;
|
|
40
39
|
|
|
41
40
|
declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
|
|
42
41
|
|
|
43
|
-
export { CAPTCHA_RESPONSE_KEY, ParseError, type
|
|
42
|
+
export { CAPTCHA_RESPONSE_KEY, ParseError, type VerifyResponse, verify };
|
package/dist/index.js
CHANGED
|
@@ -26,10 +26,6 @@ __export(src_exports, {
|
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(src_exports);
|
|
28
28
|
|
|
29
|
-
// src/verify.ts
|
|
30
|
-
var import_https = require("https");
|
|
31
|
-
var import_querystring = require("querystring");
|
|
32
|
-
|
|
33
29
|
// src/errors/ParseError.ts
|
|
34
30
|
var ParseError = class extends Error {
|
|
35
31
|
code;
|
|
@@ -41,10 +37,34 @@ var ParseError = class extends Error {
|
|
|
41
37
|
}
|
|
42
38
|
};
|
|
43
39
|
|
|
40
|
+
// src/verify.ts
|
|
41
|
+
var import_https = require("https");
|
|
42
|
+
var import_querystring = require("querystring");
|
|
43
|
+
|
|
44
|
+
// src/withRetry.ts
|
|
45
|
+
var backoff = (retryCount) => Math.exp(retryCount) * 100;
|
|
46
|
+
var withRetry = async (callback, { attempts = 4 } = {}) => {
|
|
47
|
+
let res;
|
|
48
|
+
let error;
|
|
49
|
+
for (let i = 0; i < attempts; i++) {
|
|
50
|
+
try {
|
|
51
|
+
res = await callback();
|
|
52
|
+
break;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
error = err;
|
|
55
|
+
await new Promise((r) => setTimeout(r, backoff(i)));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!res) {
|
|
59
|
+
throw error ?? new Error("Exhausted all retries");
|
|
60
|
+
}
|
|
61
|
+
return res;
|
|
62
|
+
};
|
|
63
|
+
|
|
44
64
|
// src/verify.ts
|
|
45
65
|
var HOST = "api.captchafox.com";
|
|
46
66
|
var API_PATH = "/siteverify";
|
|
47
|
-
async function
|
|
67
|
+
async function verifyRequest(secret, token, sitekey, remoteIp) {
|
|
48
68
|
return new Promise(function verifyPromise(resolve, reject) {
|
|
49
69
|
const payload = { secret, response: token };
|
|
50
70
|
if (sitekey) {
|
|
@@ -93,6 +113,11 @@ async function verify(secret, token, sitekey, remoteIp) {
|
|
|
93
113
|
apiRequest.end();
|
|
94
114
|
});
|
|
95
115
|
}
|
|
116
|
+
async function verify(secret, token, options) {
|
|
117
|
+
return withRetry(() => {
|
|
118
|
+
return verifyRequest(secret, token, options == null ? void 0 : options.sitekey, options == null ? void 0 : options.remoteIp);
|
|
119
|
+
}, options == null ? void 0 : options.retry);
|
|
120
|
+
}
|
|
96
121
|
|
|
97
122
|
// src/index.ts
|
|
98
123
|
var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// src/verify.ts
|
|
2
|
-
import { request } from "https";
|
|
3
|
-
import { stringify } from "querystring";
|
|
4
|
-
|
|
5
1
|
// src/errors/ParseError.ts
|
|
6
2
|
var ParseError = class extends Error {
|
|
7
3
|
code;
|
|
@@ -13,10 +9,34 @@ var ParseError = class extends Error {
|
|
|
13
9
|
}
|
|
14
10
|
};
|
|
15
11
|
|
|
12
|
+
// src/verify.ts
|
|
13
|
+
import { request } from "https";
|
|
14
|
+
import { stringify } from "querystring";
|
|
15
|
+
|
|
16
|
+
// src/withRetry.ts
|
|
17
|
+
var backoff = (retryCount) => Math.exp(retryCount) * 100;
|
|
18
|
+
var withRetry = async (callback, { attempts = 4 } = {}) => {
|
|
19
|
+
let res;
|
|
20
|
+
let error;
|
|
21
|
+
for (let i = 0; i < attempts; i++) {
|
|
22
|
+
try {
|
|
23
|
+
res = await callback();
|
|
24
|
+
break;
|
|
25
|
+
} catch (err) {
|
|
26
|
+
error = err;
|
|
27
|
+
await new Promise((r) => setTimeout(r, backoff(i)));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!res) {
|
|
31
|
+
throw error ?? new Error("Exhausted all retries");
|
|
32
|
+
}
|
|
33
|
+
return res;
|
|
34
|
+
};
|
|
35
|
+
|
|
16
36
|
// src/verify.ts
|
|
17
37
|
var HOST = "api.captchafox.com";
|
|
18
38
|
var API_PATH = "/siteverify";
|
|
19
|
-
async function
|
|
39
|
+
async function verifyRequest(secret, token, sitekey, remoteIp) {
|
|
20
40
|
return new Promise(function verifyPromise(resolve, reject) {
|
|
21
41
|
const payload = { secret, response: token };
|
|
22
42
|
if (sitekey) {
|
|
@@ -65,6 +85,11 @@ async function verify(secret, token, sitekey, remoteIp) {
|
|
|
65
85
|
apiRequest.end();
|
|
66
86
|
});
|
|
67
87
|
}
|
|
88
|
+
async function verify(secret, token, options) {
|
|
89
|
+
return withRetry(() => {
|
|
90
|
+
return verifyRequest(secret, token, options == null ? void 0 : options.sitekey, options == null ? void 0 : options.remoteIp);
|
|
91
|
+
}, options == null ? void 0 : options.retry);
|
|
92
|
+
}
|
|
68
93
|
|
|
69
94
|
// src/index.ts
|
|
70
95
|
var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
|