@captchafox/node 1.1.1 → 1.3.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/README.md CHANGED
@@ -18,6 +18,10 @@ yarn add @captchafox/node
18
18
  pnpm add @captchafox/node
19
19
  ```
20
20
 
21
+ ```sh
22
+ bun add @captchafox/node
23
+ ```
24
+
21
25
  ## Usage
22
26
 
23
27
  ### ESM / await
@@ -29,16 +33,15 @@ const secret = 'organization_secret';
29
33
  const token = 'widget_token';
30
34
 
31
35
  try {
32
- const data = await verify(secret, token);
33
- if (data.success) {
34
- console.log('success!', data);
35
- } else {
36
- console.log('verification failed');
37
- }
38
- } catch(error) {
39
- console.log(error);
36
+ const data = await verify(secret, token);
37
+ if (data.success) {
38
+ console.log('success!', data);
39
+ } else {
40
+ console.log('verification failed');
41
+ }
42
+ } catch (error) {
43
+ console.log(error);
40
44
  }
41
-
42
45
  ```
43
46
 
44
47
  ### Require
package/dist/index.d.mts CHANGED
@@ -20,11 +20,24 @@ type VerifyPayload = NodeJS.Dict<string> & {
20
20
  secret: string;
21
21
  /** The response token from the widget */
22
22
  response: string;
23
- /** (Optional) the sitekey that was use to issue the token */
23
+ /** (Optional) The sitekey that was used to issue the token */
24
24
  sitekey?: string;
25
+ /** (Optional) The IP address of the requesting user */
26
+ remoteIp?: string;
25
27
  };
26
- declare function verify(secret: string, token: string, sitekey?: string): Promise<VerifyResponse>;
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
+ }
27
40
 
28
41
  declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
29
42
 
30
- export { CAPTCHA_RESPONSE_KEY, VerifyPayload, VerifyResponse, verify };
43
+ export { CAPTCHA_RESPONSE_KEY, ParseError, type VerifyPayload, type VerifyResponse, verify };
package/dist/index.d.ts CHANGED
@@ -20,11 +20,24 @@ type VerifyPayload = NodeJS.Dict<string> & {
20
20
  secret: string;
21
21
  /** The response token from the widget */
22
22
  response: string;
23
- /** (Optional) the sitekey that was use to issue the token */
23
+ /** (Optional) The sitekey that was used to issue the token */
24
24
  sitekey?: string;
25
+ /** (Optional) The IP address of the requesting user */
26
+ remoteIp?: string;
25
27
  };
26
- declare function verify(secret: string, token: string, sitekey?: string): Promise<VerifyResponse>;
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
+ }
27
40
 
28
41
  declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
29
42
 
30
- export { CAPTCHA_RESPONSE_KEY, VerifyPayload, VerifyResponse, verify };
43
+ export { CAPTCHA_RESPONSE_KEY, ParseError, type VerifyPayload, type VerifyResponse, verify };
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  CAPTCHA_RESPONSE_KEY: () => CAPTCHA_RESPONSE_KEY,
24
+ ParseError: () => ParseError,
24
25
  verify: () => verify
25
26
  });
26
27
  module.exports = __toCommonJS(src_exports);
@@ -28,14 +29,30 @@ module.exports = __toCommonJS(src_exports);
28
29
  // src/verify.ts
29
30
  var import_https = require("https");
30
31
  var import_querystring = require("querystring");
32
+
33
+ // src/errors/ParseError.ts
34
+ var ParseError = class extends Error {
35
+ code;
36
+ body;
37
+ constructor({ code, message, body }) {
38
+ super(message);
39
+ this.code = code;
40
+ this.body = body;
41
+ }
42
+ };
43
+
44
+ // src/verify.ts
31
45
  var HOST = "api.captchafox.com";
32
46
  var API_PATH = "/siteverify";
33
- async function verify(secret, token, sitekey) {
47
+ async function verify(secret, token, sitekey, remoteIp) {
34
48
  return new Promise(function verifyPromise(resolve, reject) {
35
49
  const payload = { secret, response: token };
36
50
  if (sitekey) {
37
51
  payload.sitekey = sitekey;
38
52
  }
53
+ if (remoteIp) {
54
+ payload.remoteIp = remoteIp;
55
+ }
39
56
  const data = (0, import_querystring.stringify)(payload);
40
57
  const options = {
41
58
  host: HOST,
@@ -49,16 +66,29 @@ async function verify(secret, token, sitekey) {
49
66
  const apiRequest = (0, import_https.request)(options, (response) => {
50
67
  response.setEncoding("utf8");
51
68
  let responseBuffer = "";
52
- response.on("error", reject).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
69
+ response.on("error", (error) => {
70
+ reject(error);
71
+ }).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
53
72
  try {
54
73
  const json = JSON.parse(responseBuffer);
55
74
  resolve(json);
56
75
  } catch (error) {
76
+ if (error instanceof SyntaxError) {
77
+ const errorResponse = new ParseError({
78
+ code: response.statusCode,
79
+ message: error.message,
80
+ body: responseBuffer
81
+ });
82
+ reject(errorResponse);
83
+ return;
84
+ }
57
85
  reject(error);
58
86
  }
59
87
  });
60
88
  });
61
- apiRequest.on("error", reject);
89
+ apiRequest.on("error", (error) => {
90
+ reject(error);
91
+ });
62
92
  apiRequest.write(data);
63
93
  apiRequest.end();
64
94
  });
@@ -69,5 +99,6 @@ var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
69
99
  // Annotate the CommonJS export names for ESM import in node:
70
100
  0 && (module.exports = {
71
101
  CAPTCHA_RESPONSE_KEY,
102
+ ParseError,
72
103
  verify
73
104
  });
package/dist/index.mjs CHANGED
@@ -1,14 +1,30 @@
1
1
  // src/verify.ts
2
2
  import { request } from "https";
3
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
4
17
  var HOST = "api.captchafox.com";
5
18
  var API_PATH = "/siteverify";
6
- async function verify(secret, token, sitekey) {
19
+ async function verify(secret, token, sitekey, remoteIp) {
7
20
  return new Promise(function verifyPromise(resolve, reject) {
8
21
  const payload = { secret, response: token };
9
22
  if (sitekey) {
10
23
  payload.sitekey = sitekey;
11
24
  }
25
+ if (remoteIp) {
26
+ payload.remoteIp = remoteIp;
27
+ }
12
28
  const data = stringify(payload);
13
29
  const options = {
14
30
  host: HOST,
@@ -22,16 +38,29 @@ async function verify(secret, token, sitekey) {
22
38
  const apiRequest = request(options, (response) => {
23
39
  response.setEncoding("utf8");
24
40
  let responseBuffer = "";
25
- response.on("error", reject).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
41
+ response.on("error", (error) => {
42
+ reject(error);
43
+ }).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
26
44
  try {
27
45
  const json = JSON.parse(responseBuffer);
28
46
  resolve(json);
29
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
+ }
30
57
  reject(error);
31
58
  }
32
59
  });
33
60
  });
34
- apiRequest.on("error", reject);
61
+ apiRequest.on("error", (error) => {
62
+ reject(error);
63
+ });
35
64
  apiRequest.write(data);
36
65
  apiRequest.end();
37
66
  });
@@ -41,5 +70,6 @@ async function verify(secret, token, sitekey) {
41
70
  var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
42
71
  export {
43
72
  CAPTCHA_RESPONSE_KEY,
73
+ ParseError,
44
74
  verify
45
75
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@captchafox/node",
3
- "version": "1.1.1",
3
+ "version": "1.3.0",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -32,14 +32,18 @@
32
32
  "devDependencies": {
33
33
  "@captchafox/tsconfig": "*",
34
34
  "@captchafox/internal": "*",
35
- "@types/jest": "^29.5.5",
35
+ "@jest/globals": "^29.7.0",
36
36
  "@types/node": "^18.13.0",
37
37
  "jest": "^29.7.0",
38
38
  "nock": "^13.3.3",
39
- "tsup": "^7.2.0",
39
+ "tsup": "^8.3.5",
40
40
  "ts-jest": "^29.1.1",
41
41
  "typescript": "^5.0.2"
42
42
  },
43
+ "keywords": [
44
+ "node",
45
+ "captchafox"
46
+ ],
43
47
  "repository": {
44
48
  "type": "git",
45
49
  "url": "https://github.com/CaptchaFox/javascript-integrations.git",