@captchafox/node 1.3.0 → 1.5.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.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 verify(secret, token, sitekey, remoteIp) {
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) {
@@ -60,7 +80,8 @@ async function verify(secret, token, sitekey, remoteIp) {
60
80
  method: "POST",
61
81
  headers: {
62
82
  "content-type": "application/x-www-form-urlencoded",
63
- "content-length": Buffer.byteLength(data)
83
+ "content-length": Buffer.byteLength(data),
84
+ "x-spark": `nj-${"1.5.0"}`
64
85
  }
65
86
  };
66
87
  const apiRequest = (0, import_https.request)(options, (response) => {
@@ -93,6 +114,11 @@ async function verify(secret, token, sitekey, remoteIp) {
93
114
  apiRequest.end();
94
115
  });
95
116
  }
117
+ async function verify(secret, token, options) {
118
+ return withRetry(() => {
119
+ return verifyRequest(secret, token, options == null ? void 0 : options.sitekey, options == null ? void 0 : options.remoteIp);
120
+ }, options == null ? void 0 : options.retry);
121
+ }
96
122
 
97
123
  // src/index.ts
98
124
  var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@captchafox/node",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  }
23
23
  },
24
24
  "scripts": {
25
- "build": "tsup src/index.ts --format esm,cjs --dts",
25
+ "build": "tsup src/index.ts",
26
26
  "dev": "tsup src/index.ts --format esm,cjs --watch --dts",
27
27
  "lint": "TIMING=1 eslint \"src/**/*.ts*\"",
28
28
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
package/dist/index.d.mts DELETED
@@ -1,43 +0,0 @@
1
- /**
2
- * The response returned from the /siteverify
3
- * endpoint used for verifying the challenge token.
4
- */
5
- type VerifyResponse = {
6
- /** Whether verification succeeded or not */
7
- success: boolean;
8
- /**
9
- * ISO timestamp of the challenge (yyyy-MM-dd'T'HH:mm:ssZZ).
10
- * Only included on success.
11
- */
12
- challenge_ts?: string;
13
- /** Hostname of the site where the challenge was solved. Optional */
14
- hostname?: string;
15
- /** List of error codes. Only included if success is false */
16
- 'error-codes'?: string[];
17
- };
18
- type VerifyPayload = NodeJS.Dict<string> & {
19
- /** The organization secret */
20
- secret: string;
21
- /** The response token from the widget */
22
- response: string;
23
- /** (Optional) The sitekey that was used to issue the token */
24
- sitekey?: string;
25
- /** (Optional) The IP address of the requesting user */
26
- remoteIp?: string;
27
- };
28
- declare function verify(secret: string, token: string, sitekey?: string, remoteIp?: string): Promise<VerifyResponse>;
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
- }
40
-
41
- declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
42
-
43
- export { CAPTCHA_RESPONSE_KEY, ParseError, type VerifyPayload, type VerifyResponse, verify };
package/dist/index.d.ts DELETED
@@ -1,43 +0,0 @@
1
- /**
2
- * The response returned from the /siteverify
3
- * endpoint used for verifying the challenge token.
4
- */
5
- type VerifyResponse = {
6
- /** Whether verification succeeded or not */
7
- success: boolean;
8
- /**
9
- * ISO timestamp of the challenge (yyyy-MM-dd'T'HH:mm:ssZZ).
10
- * Only included on success.
11
- */
12
- challenge_ts?: string;
13
- /** Hostname of the site where the challenge was solved. Optional */
14
- hostname?: string;
15
- /** List of error codes. Only included if success is false */
16
- 'error-codes'?: string[];
17
- };
18
- type VerifyPayload = NodeJS.Dict<string> & {
19
- /** The organization secret */
20
- secret: string;
21
- /** The response token from the widget */
22
- response: string;
23
- /** (Optional) The sitekey that was used to issue the token */
24
- sitekey?: string;
25
- /** (Optional) The IP address of the requesting user */
26
- remoteIp?: string;
27
- };
28
- declare function verify(secret: string, token: string, sitekey?: string, remoteIp?: string): Promise<VerifyResponse>;
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
- }
40
-
41
- declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
42
-
43
- export { CAPTCHA_RESPONSE_KEY, ParseError, type VerifyPayload, type VerifyResponse, verify };
package/dist/index.mjs DELETED
@@ -1,75 +0,0 @@
1
- // src/verify.ts
2
- import { request } from "https";
3
- import { stringify } from "querystring";
4
-
5
- // src/errors/ParseError.ts
6
- var ParseError = class extends Error {
7
- code;
8
- body;
9
- constructor({ code, message, body }) {
10
- super(message);
11
- this.code = code;
12
- this.body = body;
13
- }
14
- };
15
-
16
- // src/verify.ts
17
- var HOST = "api.captchafox.com";
18
- var API_PATH = "/siteverify";
19
- async function verify(secret, token, sitekey, remoteIp) {
20
- return new Promise(function verifyPromise(resolve, reject) {
21
- const payload = { secret, response: token };
22
- if (sitekey) {
23
- payload.sitekey = sitekey;
24
- }
25
- if (remoteIp) {
26
- payload.remoteIp = remoteIp;
27
- }
28
- const data = stringify(payload);
29
- const options = {
30
- host: HOST,
31
- path: API_PATH,
32
- method: "POST",
33
- headers: {
34
- "content-type": "application/x-www-form-urlencoded",
35
- "content-length": Buffer.byteLength(data)
36
- }
37
- };
38
- const apiRequest = request(options, (response) => {
39
- response.setEncoding("utf8");
40
- let responseBuffer = "";
41
- response.on("error", (error) => {
42
- reject(error);
43
- }).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
44
- try {
45
- const json = JSON.parse(responseBuffer);
46
- resolve(json);
47
- } catch (error) {
48
- if (error instanceof SyntaxError) {
49
- const errorResponse = new ParseError({
50
- code: response.statusCode,
51
- message: error.message,
52
- body: responseBuffer
53
- });
54
- reject(errorResponse);
55
- return;
56
- }
57
- reject(error);
58
- }
59
- });
60
- });
61
- apiRequest.on("error", (error) => {
62
- reject(error);
63
- });
64
- apiRequest.write(data);
65
- apiRequest.end();
66
- });
67
- }
68
-
69
- // src/index.ts
70
- var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
71
- export {
72
- CAPTCHA_RESPONSE_KEY,
73
- ParseError,
74
- verify
75
- };