@captchafox/node 1.0.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 ADDED
@@ -0,0 +1,61 @@
1
+ # @captchafox/node
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@captchafox/node.svg)](https://www.npmjs.com/package/@captchafox/node)
4
+
5
+ ## Installation
6
+
7
+ Install the library using your prefered package manager
8
+
9
+ ```sh
10
+ npm install @captchafox/node
11
+ ```
12
+
13
+ ```sh
14
+ yarn add @captchafox/node
15
+ ```
16
+
17
+ ```sh
18
+ pnpm add @captchafox/node
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### ESM / await
24
+
25
+ ```ts
26
+ import { verify } from '@captchafox/node';
27
+
28
+ const secret = 'organization_secret';
29
+ const token = 'widget_token';
30
+
31
+ 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);
40
+ }
41
+
42
+ ```
43
+
44
+ ### Require
45
+
46
+ ```js
47
+ const { verify } = require('@captchafox/node');
48
+
49
+ const secret = 'organization_secret';
50
+ const token = 'widget_token';
51
+
52
+ verify(secret, token)
53
+ .then((data) => {
54
+ if (data.success) {
55
+ console.log('success!', data);
56
+ } else {
57
+ console.log('verification failed');
58
+ }
59
+ })
60
+ .catch(console.error);
61
+ ```
@@ -0,0 +1,30 @@
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 use to issue the token */
24
+ sitekey?: string;
25
+ };
26
+ declare function verify(secret: string, token: string, sitekey?: string): Promise<VerifyResponse>;
27
+
28
+ declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
29
+
30
+ export { CAPTCHA_RESPONSE_KEY, VerifyPayload, VerifyResponse, verify };
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ CAPTCHA_RESPONSE_KEY: () => CAPTCHA_RESPONSE_KEY,
24
+ verify: () => verify
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/verify.ts
29
+ var import_https = require("https");
30
+ var import_querystring = require("querystring");
31
+ var HOST = "api.captchafox.com";
32
+ var API_PATH = "/siteverify";
33
+ async function verify(secret, token, sitekey) {
34
+ return new Promise(function verifyPromise(resolve, reject) {
35
+ const payload = { secret, response: token };
36
+ if (sitekey) {
37
+ payload.sitekey = sitekey;
38
+ }
39
+ const data = (0, import_querystring.stringify)(payload);
40
+ const options = {
41
+ host: HOST,
42
+ path: API_PATH,
43
+ method: "POST",
44
+ headers: {
45
+ "content-type": "application/x-www-form-urlencoded",
46
+ "content-length": Buffer.byteLength(data)
47
+ }
48
+ };
49
+ const apiRequest = (0, import_https.request)(options, (response) => {
50
+ response.setEncoding("utf8");
51
+ let responseBuffer = "";
52
+ response.on("error", reject).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
53
+ try {
54
+ const json = JSON.parse(responseBuffer);
55
+ resolve(json);
56
+ } catch (error) {
57
+ reject(error);
58
+ }
59
+ });
60
+ });
61
+ apiRequest.on("error", reject);
62
+ apiRequest.write(data);
63
+ apiRequest.end();
64
+ });
65
+ }
66
+
67
+ // src/index.ts
68
+ var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
69
+ // Annotate the CommonJS export names for ESM import in node:
70
+ 0 && (module.exports = {
71
+ CAPTCHA_RESPONSE_KEY,
72
+ verify
73
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,45 @@
1
+ // src/verify.ts
2
+ import { request } from "https";
3
+ import { stringify } from "querystring";
4
+ var HOST = "api.captchafox.com";
5
+ var API_PATH = "/siteverify";
6
+ async function verify(secret, token, sitekey) {
7
+ return new Promise(function verifyPromise(resolve, reject) {
8
+ const payload = { secret, response: token };
9
+ if (sitekey) {
10
+ payload.sitekey = sitekey;
11
+ }
12
+ const data = stringify(payload);
13
+ const options = {
14
+ host: HOST,
15
+ path: API_PATH,
16
+ method: "POST",
17
+ headers: {
18
+ "content-type": "application/x-www-form-urlencoded",
19
+ "content-length": Buffer.byteLength(data)
20
+ }
21
+ };
22
+ const apiRequest = request(options, (response) => {
23
+ response.setEncoding("utf8");
24
+ let responseBuffer = "";
25
+ response.on("error", reject).on("data", (chunk) => responseBuffer += chunk).on("end", () => {
26
+ try {
27
+ const json = JSON.parse(responseBuffer);
28
+ resolve(json);
29
+ } catch (error) {
30
+ reject(error);
31
+ }
32
+ });
33
+ });
34
+ apiRequest.on("error", reject);
35
+ apiRequest.write(data);
36
+ apiRequest.end();
37
+ });
38
+ }
39
+
40
+ // src/index.ts
41
+ var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
42
+ export {
43
+ CAPTCHA_RESPONSE_KEY,
44
+ verify
45
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@captchafox/node",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "sideEffects": false,
8
+ "license": "MIT",
9
+ "files": [
10
+ "dist/**"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsup src/index.ts --format esm,cjs --dts",
14
+ "dev": "tsup src/index.ts --format esm,cjs --watch --dts",
15
+ "lint": "TIMING=1 eslint \"src/**/*.ts*\"",
16
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
17
+ "test": "jest --coverage",
18
+ "test:watch": "jest --watch"
19
+ },
20
+ "devDependencies": {
21
+ "@captchafox/internal": "*",
22
+ "@types/jest": "^29.0.5",
23
+ "@types/node": "^18.13.0",
24
+ "jest": "^29.5.0",
25
+ "nock": "^13.3.0",
26
+ "tsup": "^6.6.3",
27
+ "ts-jest": "^29.0.5",
28
+ "typescript": "^4.9.5"
29
+ },
30
+ "bugs": {
31
+ "url": "https://github.com/CaptchaFox/javascript-integrations/issues"
32
+ },
33
+ "homepage": "https://github.com/CaptchaFox/javascript-integrations#readme"
34
+ }